001/*
002 *  Copyright 2010 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.cms.workflow;
017
018import java.util.HashSet;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang.StringUtils;
025
026import org.ametys.cms.contenttype.ContentType;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.workflow.copy.CreateContentByCopyFunction;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031
032import com.opensymphony.workflow.WorkflowException;
033
034/**
035 * Condition for checking if an user has the right to create a content of the requested type.
036 */
037public class CreateContentByCopyRightCondition extends CreateContentRightCondition
038{
039    private AmetysObjectResolver _resolver;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
046    }
047    
048    @Override
049    protected Set<String> getRights(Map transientVars) throws WorkflowException
050    {
051        Set<String> rights = new HashSet<>();
052        
053        String[] contentTypeIds;
054        
055        if (transientVars.get(CreateContentFunction.CONTENT_TYPES_KEY) != null)
056        {
057            contentTypeIds = (String[]) transientVars.get(CreateContentFunction.CONTENT_TYPES_KEY);
058        }
059        else
060        {
061            Content baseContent = (Content) transientVars.get(CreateContentByCopyFunction.BASE_CONTENT_KEY);
062            if (baseContent == null)
063            {
064                String baseContentId = (String) transientVars.get(CreateContentByCopyFunction.BASE_CONTENT_ID);
065                
066                try
067                {
068                    baseContent = _resolver.resolveById(baseContentId);
069                }
070                catch (UnknownAmetysObjectException e)
071                {
072                    throw new WorkflowException("Unable to retrieve the base content for the duplication.");
073                }
074            }
075            
076            contentTypeIds = baseContent.getTypes();
077        }
078        
079        for (String contentTypeId : contentTypeIds)
080        {
081            ContentType contentType = _contentTypeExtensionPoint.getExtension(contentTypeId);
082            
083            if (contentType == null)
084            {
085                throw new WorkflowException("Unknown content type: " + contentType);
086            }
087            
088            String right = contentType.getRight();
089            if (StringUtils.isNotEmpty(right))
090            {
091                rights.add(right);
092            }
093        }
094        
095        
096        return rights;
097    }
098    
099}