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