001/*
002 *  Copyright 2019 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.userdirectory;
017
018import java.util.Collections;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang3.StringUtils;
026import org.apache.commons.lang3.tuple.Pair;
027import org.slf4j.Logger;
028
029import org.ametys.cms.content.ContentHelper;
030import org.ametys.cms.repository.Content;
031import org.ametys.cms.repository.WorkflowAwareContent;
032import org.ametys.cms.workflow.ContentWorkflowHelper;
033import org.ametys.core.util.I18nUtils;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.runtime.model.ModelItem;
036
037import com.opensymphony.workflow.InvalidActionException;
038import com.opensymphony.workflow.WorkflowException;
039
040/**
041 * Delete user component
042 */
043public class DeleteUserComponent extends AbstractDeleteUDContentComponent
044{
045    /** The avalon role. */
046    public static final String ROLE = DeleteUserComponent.class.getName();
047   
048    /** The user directory helper */
049    protected UserDirectoryHelper _udHelper;
050    
051    /** The content helper */
052    protected ContentHelper _contentHelper;
053    
054    /** The content workflow helper */
055    protected ContentWorkflowHelper _contentWorkflowHelper;
056    
057    /** The i18n utils */
058    protected I18nUtils _i18nUtils;
059    
060    @Override
061    public void service(ServiceManager smanager) throws ServiceException
062    {
063        super.service(smanager);
064        _udHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE);
065        _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE);
066        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
067        _contentWorkflowHelper = (ContentWorkflowHelper) smanager.lookup(ContentWorkflowHelper.ROLE);
068    }
069    
070    @Override
071    public boolean isContentReferenced(Content content, Logger logger)
072    {
073        return _contentHelper.hasReferencingContents(content, Collections.singletonList(OrganisationChartPageHandler.ORGUNIT_CONTENT_TYPE), false);
074    }
075    
076    @Override
077    protected boolean _checkBeforeDeletion(Content content, Map<String, String> rights, Map<String, Object> results, Logger logger)
078    {
079        // Check right and lock on content it self
080        boolean allRight = _canDeleteContent(content, rights, results);
081        
082        // Check lock on orgunits contents
083        allRight = _checkOrgUnitsBeforeDeletion(content, results) && allRight;
084        
085        return allRight;
086    }
087    
088    /**
089     * True if the parent content is not locked
090     * @param content the parent content
091     * @param results the results map
092     * @return true if the parent content is not locked
093     */
094    protected boolean _checkOrgUnitsBeforeDeletion(Content content,  Map<String, Object> results)
095    {
096        boolean allRight = true;
097        
098        // Check if parents are not locked
099        List<Content> orgUnits = _udHelper.getOrgUnits(content);
100        for (Content orgUnit : orgUnits)
101        {
102            if (_isLocked(orgUnit))
103            {
104                @SuppressWarnings("unchecked")
105                List<Content> lockedContents = (List<Content>) results.get("locked-contents");
106                lockedContents.add(orgUnit);
107                
108                allRight = false;
109            }
110        }
111        
112        return allRight;
113    }
114
115    @Override
116    protected boolean _removeRelations(Content content, Logger logger)
117    {
118        boolean success = true;
119        
120        // Delete relation to orgunits
121        List<Pair<String, Content>> incomingReferences = _contentHelper.getReferencingContents(content);
122        for (Pair<String, Content> refPair : incomingReferences)
123        {
124            Content orgUnit = refPair.getValue();
125            
126            try
127            {
128                I18nizableText commentText = new I18nizableText("plugin.user-directory", "PLUGINS_USER_DIRECTORY_WORKFLOW_ACTION_REMOVE_USER_REFERENCE_MSG");
129                String comment = _i18nUtils.translate(commentText, orgUnit.getLanguage());
130    
131                // The valuePath is like users[1]/user
132                String valuePath = refPair.getKey();
133                String entryPath = StringUtils.substringBeforeLast(valuePath, ModelItem.ITEM_PATH_SEPARATOR);
134                _contentWorkflowHelper.removeRepeaterEntry((WorkflowAwareContent) orgUnit, entryPath, _removeReferenceActionId, comment);
135            }
136            catch (WorkflowException | InvalidActionException e)
137            {
138                logger.error("Unable to remove relation to content \"{}\" ({}) for referencing content \"{}\" ({}) ", content.getTitle(), content.getId(), orgUnit.getTitle(), orgUnit.getId(), e);
139                success = false;
140            }
141        }
142        
143        return success;
144    }
145
146    @Override
147    protected Set<String> _getContentIdsToDelete(Content content, Map<String, String> rights, Map<String, Object> results, Logger logger)
148    {
149        return Collections.singleton(content.getId());
150    }
151}