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.odf;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configurable;
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
029import org.ametys.cms.workflow.CreateContentFunction;
030import org.ametys.core.util.I18nUtils;
031import org.ametys.odf.orgunit.OrgUnit;
032import org.ametys.odf.orgunit.OrgUnitFactory;
033import org.ametys.odf.orgunit.RootOrgUnitProvider;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.repository.RepositoryConstants;
036import org.ametys.plugins.repository.data.external.ExternalizableDataProvider.ExternalizableDataStatus;
037import org.ametys.plugins.workflow.AbstractWorkflowComponent;
038import org.ametys.plugins.workflow.support.WorkflowProvider;
039import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow;
040import org.ametys.runtime.config.Config;
041import org.ametys.runtime.i18n.I18nizableText;
042import org.ametys.runtime.plugin.component.AbstractLogEnabled;
043
044/**
045 * Odf plugin initialization class
046 */
047public class Init extends AbstractLogEnabled implements org.ametys.runtime.plugin.Init, Serviceable, Configurable
048{
049    /** The odf root node name */
050    public static final String _ODF_ROOT_NODE = RepositoryConstants.NAMESPACE_PREFIX + ":odf";
051
052    /** The odf contents root node name */
053    public static final String _ODF_CONTENTS_ROOT_NODE = RepositoryConstants.NAMESPACE_PREFIX + ":contents";
054    
055    /** The Ametys object resolver */
056    protected AmetysObjectResolver _resolver;
057    /** The orgunit provider */
058    protected RootOrgUnitProvider _orgUnitProvider;
059    /** The workflow */
060    protected WorkflowProvider _workflowProvider;
061    /** The i18n utils */
062    protected I18nUtils _i18Utils;
063
064    private String _orgUnitWorkflowName;
065    private String _orgUnitWorkflowId;
066    private I18nizableText _orgUnitTitle;
067
068    private ODFHelper _odfHelper;
069
070
071    @Override
072    public void service(ServiceManager manager) throws ServiceException
073    {
074        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
075        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
076        _orgUnitProvider = (RootOrgUnitProvider) manager.lookup(RootOrgUnitProvider.ROLE);
077        _i18Utils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
078        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
079    }
080
081    @Override
082    public void configure(Configuration configuration) throws ConfigurationException
083    {
084        _orgUnitWorkflowName = configuration.getChild("orgunit-workflow-name").getValue(null);
085        _orgUnitWorkflowId = configuration.getChild("orgunit-workflow-init-action-id").getValue(null);
086        _orgUnitTitle = I18nizableText.parseI18nizableText(configuration.getChild("orgunit-title"), "plugin.odf");
087    }
088
089    @Override
090    public void init() throws Exception
091    {
092        // Create ODF root node if needed
093        _odfHelper.getRootContent(true);
094        
095        OrgUnit rootOrgUnit = _orgUnitProvider.getRoot();
096        
097        if (rootOrgUnit != null)
098        {
099            // Set UAI code if changed
100            String uaiCode = Config.getInstance().getValue("odf.root-orgunit.uaiCode");
101            // The current code is different from the one in configuration
102            if (!uaiCode.equals(rootOrgUnit.getUAICode()))
103            {
104                if (ExternalizableDataStatus.EXTERNAL.equals(rootOrgUnit.getStatus(OrgUnit.CODE_UAI)))
105                {
106                    // Change the status to local if needed...
107                    rootOrgUnit.setStatus(OrgUnit.CODE_UAI, ExternalizableDataStatus.LOCAL);
108                }
109                
110                // ...and set the value found in conf to the current (and local) value
111                rootOrgUnit.setValue(OrgUnit.CODE_UAI, uaiCode);
112                rootOrgUnit.saveChanges();
113            }
114        }
115        else
116        {
117            // Create the root for orgunits
118            Map<String, Object> inputs = new HashMap<>();
119            inputs.put(AbstractWorkflowComponent.RESULT_MAP_KEY, new HashMap<String, Object>());
120            inputs.put(CreateContentFunction.CONTENT_NAME_KEY, OrgUnitFactory.ODF_ORGUNIT_ROOT_NODE);
121            
122            String title = _i18Utils.translate(_orgUnitTitle, Config.getInstance().getValue("odf.programs.lang"));
123            inputs.put(CreateContentFunction.CONTENT_TITLE_KEY, title);
124            inputs.put(CreateContentFunction.CONTENT_TYPES_KEY, new String[] {OrgUnitFactory.ORGUNIT_CONTENT_TYPE});
125            inputs.put(CreateContentFunction.CONTENT_LANGUAGE_KEY, Config.getInstance().getValue("odf.programs.lang"));
126            
127            AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow();
128            workflow.initialize(_orgUnitWorkflowName, Integer.parseInt(_orgUnitWorkflowId), inputs);
129            
130            @SuppressWarnings({"static-access", "unchecked"})
131            Map<String, Object> workflowResult = (Map<String, Object>) inputs.get(AbstractContentWorkflowComponent.RESULT_MAP_KEY);
132            String contentId = (String) workflowResult.get("contentId");
133            
134            rootOrgUnit = _resolver.resolveById(contentId);
135            rootOrgUnit.setValue(OrgUnit.CODE_UAI, Config.getInstance().getValue("odf.root-orgunit.uaiCode"));
136            rootOrgUnit.saveChanges();
137        }
138    }
139}