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