001/*
002 *  Copyright 2017 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.odf.workflow.copy;
017
018import java.util.Map;
019import java.util.Optional;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.repository.ModifiableContent;
026import org.ametys.odf.orgunit.OrgUnit;
027import org.ametys.odf.orgunit.OrgUnitFactory;
028import org.ametys.odf.orgunit.RootOrgUnitProvider;
029import org.ametys.odf.workflow.CreateOrgUnitFunction;
030import org.ametys.plugins.repository.data.holder.values.SynchronizableValue;
031import org.ametys.runtime.i18n.I18nizableText;
032
033import com.opensymphony.workflow.WorkflowException;
034
035/**
036 * OSWorkflow function to create a course by copy of another
037 */
038public class CreateOrgUnitByCopyFunction extends AbstractCreateODFContentByCopyFunction
039{
040    /** The RootOrgUnitProvider */
041    private RootOrgUnitProvider _rootOrgUnitProvider;
042    
043    
044    @Override
045    public void service(ServiceManager smanager) throws ServiceException
046    {
047        super.service(smanager);
048        _rootOrgUnitProvider = (RootOrgUnitProvider) smanager.lookup(RootOrgUnitProvider.ROLE);
049    }
050    
051    @Override
052    protected String _getContentNamePrefix()
053    {
054        return CreateOrgUnitFunction.CONTENT_NAME_PREFIX;
055    }
056    
057    @Override
058    protected String _getObjectType(Map transientVars, Map args)
059    {
060        return OrgUnitFactory.ORGUNIT_NODETYPE;
061    }
062    
063    @Override
064    protected void processValues(Map transientVars, ModifiableContent targetContent, Map<String, Object> values) throws WorkflowException
065    {
066        super.processValues(transientVars, targetContent, values);
067        
068        // Set a unique CDM code
069        values.put(OrgUnit.CODE_UAI, org.ametys.core.util.StringUtils.generateKey().toUpperCase());
070    }
071    
072    @Override
073    protected Content _getParent(Map transientVars)
074    {
075        return Optional.ofNullable(super._getParent(transientVars))
076            .filter(OrgUnit.class::isInstance)
077            .orElseGet(_rootOrgUnitProvider::getRoot);
078    }
079
080    @Override
081    protected boolean _isCompatibleParent(Content parent)
082    {
083        return parent instanceof OrgUnit;
084    }
085
086    @Override
087    protected String _getParentAttributeName(Content parent)
088    {
089        return OrgUnit.PARENT_ORGUNIT;
090    }
091
092    @Override
093    protected SynchronizableValue _buildParentSynchronizableValue(Content parent)
094    {
095        return new SynchronizableValue(parent.getId());
096    }
097    
098    @Override
099    public I18nizableText getLabel()
100    {
101        return new I18nizableText("plugin.odf", "PLUGINS_ODF_CREATE_ORGUNIT_BY_COPY_FUNCTION_LABEL");
102    }
103}