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;
031
032import com.opensymphony.workflow.WorkflowException;
033
034/**
035 * OSWorkflow function to create a course by copy of another
036 */
037public class CreateOrgUnitByCopyFunction extends AbstractCreateODFContentByCopyFunction
038{
039    /** The RootOrgUnitProvider */
040    private RootOrgUnitProvider _rootOrgUnitProvider;
041    
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _rootOrgUnitProvider = (RootOrgUnitProvider) smanager.lookup(RootOrgUnitProvider.ROLE);
048    }
049    
050    @Override
051    protected String _getContentNamePrefix()
052    {
053        return CreateOrgUnitFunction.CONTENT_NAME_PREFIX;
054    }
055    
056    @Override
057    protected String _getObjectType(Map transientVars, Map args)
058    {
059        return OrgUnitFactory.ORGUNIT_NODETYPE;
060    }
061    
062    @Override
063    protected void processValues(Map transientVars, ModifiableContent targetContent, Map<String, Object> values) throws WorkflowException
064    {
065        super.processValues(transientVars, targetContent, values);
066        
067        // Set a unique CDM code
068        values.put(OrgUnit.CODE_UAI, org.ametys.core.util.StringUtils.generateKey().toUpperCase());
069    }
070    
071    @Override
072    protected Content _getParent(Map transientVars)
073    {
074        return Optional.ofNullable(super._getParent(transientVars))
075            .filter(OrgUnit.class::isInstance)
076            .orElseGet(_rootOrgUnitProvider::getRoot);
077    }
078
079    @Override
080    protected boolean _isCompatibleParent(Content parent)
081    {
082        return parent instanceof OrgUnit;
083    }
084
085    @Override
086    protected String _getParentAttributeName(Content parent)
087    {
088        return OrgUnit.PARENT_ORGUNIT;
089    }
090
091    @Override
092    protected SynchronizableValue _buildParentSynchronizableValue(Content parent)
093    {
094        return new SynchronizableValue(parent.getId());
095    }
096}