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