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.plugins.contentio.synchronize;
017
018import java.util.LinkedHashMap;
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;
024
025import org.ametys.plugins.contentio.synchronize.search.SCCSearchModelConfiguration;
026import org.ametys.runtime.i18n.I18nizableText;
027
028/**
029 * Configuration is separated from {@link AbstractSynchronizableContentsCollection}
030 */
031public abstract class AbstractStaticSynchronizableContentsCollection implements SynchronizableContentsCollection, Configurable
032{
033    /** The id */
034    protected String _id;
035    /** The label */
036    protected I18nizableText _label;
037    /** The path to the metadata holding the 'restricted' property */
038    protected String _restrictedField;
039    /** The handled content type */
040    protected String _contentType;
041    /** The id of controller */
042    protected String _modelId;
043    /** The untyped values of controller's parameters */
044    protected Map<String, Object> _modelParamValues;
045    /** True if removal sync */
046    protected boolean _removalSync;
047    /** The name of the workflow */
048    protected String _workflowName;
049    /** The id of the initial action of the workflow */
050    protected int _initialActionId;
051    /** The id of the validate action of the workflow */
052    protected int _validateActionId;
053    /** The prefix of the contents */
054    protected String _contentPrefix;
055    /** True to validate contents after import */
056    protected boolean _validateAfterImport;
057    /** The report mails */
058    protected String _reportMails;
059    /** The id of the content operator to use */
060    protected String _synchronizingContentOperator;
061    /** The id of the content operator to use */
062    protected boolean _synchronizeExistingContentsOnly;
063    /** Search model configuration for search tool */
064    protected SCCSearchModelConfiguration _searchModelConfiguration;
065
066    @Override
067    public void configure(Configuration configuration) throws ConfigurationException
068    {
069        configureStaticParams(configuration);
070        configureDataSource(configuration);
071        _searchModelConfiguration = new SCCSearchModelConfiguration();
072        configureSearchModel();
073    }
074
075    /**
076     * Called in {@link #configure(Configuration)} for first configurations needed.
077     * @param configuration Configuration to read
078     * @throws ConfigurationException If an error occurs
079     */
080    protected void configureStaticParams(Configuration configuration) throws ConfigurationException
081    {
082        _id = configuration.getAttribute("id");
083        _label = I18nizableText.parseI18nizableText(configuration.getChild("label"), null);
084        _contentType = configuration.getChild("contentType").getValue();
085        _removalSync = configuration.getChild("removalSync").getValueAsBoolean(false);
086        _workflowName = configuration.getChild("workflowName").getValue();
087        _initialActionId = configuration.getChild("initialActionId").getValueAsInteger();
088        _validateActionId = configuration.getChild("validateActionId").getValueAsInteger();
089        _contentPrefix = configuration.getChild("contentPrefix").getValue();
090        _restrictedField = configuration.getChild("restrictedField").getValue(null);
091        _validateAfterImport = configuration.getChild("validateAfterImport").getValueAsBoolean(false);
092        _reportMails = configuration.getChild("reportMails").getValue("");
093        _synchronizingContentOperator = configuration.getChild("contentOperator").getValue();
094        _modelId = configuration.getChild("model").getAttribute("id");
095        _modelParamValues = _parseParameters(configuration.getChild("model"));
096        _synchronizeExistingContentsOnly = configuration.getChild("synchronizeExistingContentsOnly").getValueAsBoolean(false);
097    }
098
099    /**
100     * Configure the data source parameters.
101     * @param configuration Configuration to read
102     * @throws ConfigurationException If an error occurs
103     */
104    protected abstract void configureDataSource(Configuration configuration) throws ConfigurationException;
105    
106    /**
107     * Configure the search model used by SCCSearchTool.
108     */
109    protected abstract void configureSearchModel();
110
111    @Override
112    public String getId()
113    {
114        return _id;
115    }
116    
117    @Override
118    public I18nizableText getLabel()
119    {
120        return _label;
121    }
122    
123    @Override
124    public String getContentType()
125    {
126        return _contentType;
127    }
128
129    @Override
130    public String getRestrictedField()
131    {
132        return _restrictedField;
133    }
134    
135    @Override
136    public String getSynchronizeCollectionModelId()
137    {
138        return _modelId;
139    }
140
141    @Override
142    public Map<String, Object> getParameterValues()
143    {
144        return _modelParamValues;
145    }
146    
147    @Override
148    public boolean removalSync()
149    {
150        return _removalSync;
151    }
152    
153    @Override
154    public String getWorkflowName()
155    {
156        return _workflowName;
157    }
158    
159    @Override
160    public int getInitialActionId()
161    {
162        return _initialActionId;
163    }
164    
165    @Override
166    public int getValidateActionId()
167    {
168        return _validateActionId;
169    }
170    
171    @Override
172    public String getContentPrefix()
173    {
174        return _contentPrefix;
175    }
176    
177    @Override
178    public boolean validateAfterImport()
179    {
180        return _validateAfterImport;
181    }
182    
183    @Override
184    public String getReportMails()
185    {
186        return _reportMails;
187    }
188    
189    @Override
190    public String getSynchronizingContentOperator()
191    {
192        return _synchronizingContentOperator;
193    }
194    
195    @Override
196    public boolean synchronizeExistingContentsOnly()
197    {
198        return _synchronizeExistingContentsOnly;
199    }
200    
201    @Override
202    public SCCSearchModelConfiguration getSearchModelConfiguration()
203    {
204        return _searchModelConfiguration;
205    }
206
207    /**
208     * Parse parameters' values
209     * @param configuration The root configuration
210     * @return The parameters
211     * @throws ConfigurationException if an error occurred
212     */
213    protected Map<String, Object> _parseParameters(Configuration configuration) throws ConfigurationException
214    {
215        Map<String, Object> values = new LinkedHashMap<>();
216        
217        Configuration[] params = configuration.getChildren("param");
218        for (Configuration paramConfig : params)
219        {
220            values.put(paramConfig.getAttribute("name"), paramConfig.getValue(""));
221        }
222        return values;
223    }
224}