001/*
002 *  Copyright 2016 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.List;
019import java.util.Map;
020import java.util.Set;
021
022import org.slf4j.Logger;
023
024import org.ametys.cms.data.ContentSynchronizationResult;
025import org.ametys.cms.repository.ModifiableContent;
026import org.ametys.core.right.RightAssignmentContext;
027import org.ametys.core.schedule.progression.ContainerProgressionTracker;
028import org.ametys.plugins.contentio.synchronize.search.SCCSearchModelConfiguration;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * This interface represents a synchronizable collection of contents
033 *
034 */
035public interface SynchronizableContentsCollection
036{
037    /** The name for the metadata indicating the ids of the collections */
038    public static final String COLLECTION_ID_DATA_NAME = "scc";
039    /** The name of the metadata indicating the date of the last synchronization */
040    public static final String LAST_SYNCHRONIZATION_DATA_NAME = "lastSynchronization";
041    /** The name of the metadata indicating the user that launched the last synchronization */
042    public static final String LAST_SYNCHRONIZATION_USER_DATA_NAME = "lastSynchronizationUser";
043    
044    /** The result map key for number of created contents */
045    public static final String RESULT_NB_CREATED_CONTENTS = "nbCreatedContents";
046    /** The result map key for number of synchronized contents */
047    public static final String RESULT_NB_SYNCHRONIZED_CONTENTS = "nbSynchronizedContents";
048    /** The result map key for number of synchronized contents */
049    public static final String RESULT_NB_NOT_CHANGED_CONTENTS = "nbNotChangedContents";
050    /** The result map key for number of deleted contents */
051    public static final String RESULT_NB_DELETED_CONTENTS = "nbDeletedContents";
052    
053    /**
054     * Get the id of synchronizable collection.
055     * @return the id
056     */
057    String getId();
058    
059    /**
060     * Get the label of synchronizable collection
061     * @return the label
062     */
063    I18nizableText getLabel();
064    
065    /**
066     * Get the type of content handled by this collection
067     * @return the type of content
068     */
069    String getContentType();
070    
071    /**
072     * Get the languages handled by this collection
073     * @return the list of language
074     */
075    List<String> getLanguages();
076    
077    /**
078     * Get the id of controller responsible of synchronization of this collection
079     * @return The id of controller
080     */
081    String getSynchronizeCollectionModelId();
082    
083    /**
084     * Get the untyped values of parameters for controller
085     * @return the untyped values
086     */
087    Map<String, Object> getParameterValues();
088    
089    /**
090     * When returns <code>true</code>, a content created by a previous synchro will be removed if it does not exist anymore during the current synchro.
091     * @return <code>true</code> if a content created by a previous synchro has to be removed if it does not exist anymore during the current synchro.
092     */
093    boolean removalSync();
094    
095    /**
096     * When returns <code>true</code>, the process ignore restrictions on attributes to synchronize it.
097     * @return <code>true</code> to ignore restrictions on synchronization
098     */
099    boolean ignoreRestrictions();
100
101    /**
102     * When returns <code>true</code>, the process check the collection of contents to synchronize, can be a list of collections and not only the current one.
103     * @return <code>true</code> to check the collection on synchronization
104     */
105    boolean checkCollection();
106
107    /**
108     * Get the compatible SCC with the current SCC.
109     * @param includeCurrent to include the current SCC in the list.
110     * @return The {@link List} of SCC identifiers compatible with the current SCC.
111     */
112    List<String> getCompatibleSCC(boolean includeCurrent);
113    
114    /**
115     * Get the name of the workflow to use for the synchronized contents
116     * @return the name of the workflow to use for the synchronized contents
117     */
118    String getWorkflowName();
119    
120    /**
121     * Get the id of the initial action of the workflow
122     * @return The id of the initial action of the workflow
123     */
124    int getInitialActionId();
125    
126    /**
127     * Get the id of the synchronize action of the workflow
128     * @return The id of the synchronize action of the workflow
129     */
130    int getSynchronizeActionId();
131    
132    /**
133     * Get the id of the validate action of the workflow
134     * @return The id of the validate action of the workflow
135     */
136    int getValidateActionId();
137    
138    /**
139     * Get the prefix to use for the creation of contents
140     * @return The prefix to use for the creation of contents
141     */
142    String getContentPrefix();
143    
144    /**
145     * True to validate the contents after import
146     * @return True to validate the contents after import
147     */
148    boolean validateAfterImport();
149    
150    /**
151     * If an exception occurs during synchronization, an error report mail will be sent to those email addresses (separated by new lines)
152     * @return The email addresses to send an error report if an exception occurs during synchronization (separated by new lines)
153     */
154    String getReportMails();
155    
156    /**
157     * Gets the id of the {@link SynchronizingContentOperator} extension to use during synchronization
158     * @return the id of the {@link SynchronizingContentOperator} extension to use during synchronization
159     */
160    String getSynchronizingContentOperator();
161    
162    /**
163     * Get the path of boolean metadata for restricted content.
164     * If true, the content will be visible only for connected users.
165     * @return the path to the metadata. Can be null.
166     */
167    String getRestrictedField();
168    
169    /**
170     * Get the path of metadata holding the unique identifier
171     * @return the path to the metadata. Can be null.
172     */
173    String getIdField();
174    
175    /**
176     * Get the path of tri-state fields (with local and external values)
177     * @param additionalParameters Additional parameters
178     * @return the synchronized fields
179     */
180    Set<String> getLocalAndExternalFields(Map<String, Object> additionalParameters);
181    
182    /**
183     * Get the search UI model for search tool (columns, criterias, url for buttons, etc.)
184     * @return A {@link SCCSearchModelConfiguration}
185     */
186    SCCSearchModelConfiguration getSearchModelConfiguration();
187    
188    /**
189     * True to synchronize only existing contents
190     * @return True to synchronize only existing contents
191     */
192    boolean synchronizeExistingContentsOnly();
193    
194    /**
195     * Empty the collection of its synchronized contents
196     * @param logger The logger
197     */
198    public void empty(Logger logger);
199    
200    /**
201     * Populates contents
202     * @param logger The logger
203     * @param progressionTracker A progression tracker
204     * @return Return the populated contents (imported or synchronized)
205     */
206    public List<ModifiableContent> populate(Logger logger, ContainerProgressionTracker progressionTracker);
207    
208    /**
209     * Import a content from remote values.
210     * @param idValue Id (for import/synchronization) of the content to import
211     * @param additionalParameters Additional parameters
212     * @param logger The logger
213     * @return A list of created contents
214     * @throws Exception if an error occurs.
215     */
216    public List<ModifiableContent> importContent(String idValue, Map<String, Object> additionalParameters, Logger logger) throws Exception;
217    
218    /**
219     * Synchronize a content with remove values.
220     * @param content The content to synchronize
221     * @param logger The logger
222     * @throws Exception if an error occurs.
223     */
224    public void synchronizeContent(ModifiableContent content, Logger logger) throws Exception;
225    
226    /**
227     * Add specific fields to the content.
228     * @param content Content to update
229     * @param additionalParameters Additional parameters
230     * @param logger The logger
231     * @return The synchronization result of the import additional operations
232     */
233    public default ContentSynchronizationResult additionalImportOperations(ModifiableContent content, Map<String, Object> additionalParameters, Logger logger)
234    {
235        return additionalCommonOperations(content, additionalParameters, logger);
236    }
237
238    /**
239     * Add specific fields to the content.
240     * @param content Content to update
241     * @param additionalParameters Additional parameters
242     * @param logger The logger
243     * @return The synchronization result of the synchronize additional operations
244     */
245    public default ContentSynchronizationResult additionalSynchronizeOperations(ModifiableContent content, Map<String, Object> additionalParameters, Logger logger)
246    {
247        return additionalCommonOperations(content, additionalParameters, logger);
248    }
249    
250    /**
251     * Add specific fields to the content during import or synchronization.
252     * @param content Content to update
253     * @param additionalParameters Additional parameters
254     * @param logger The logger
255     * @return The synchronization result of the additional operations
256     */
257    public default ContentSynchronizationResult additionalCommonOperations(ModifiableContent content, Map<String, Object> additionalParameters, Logger logger)
258    {
259        // Do nothing by default
260        return new ContentSynchronizationResult();
261    }
262    
263    /**
264     * Gets the content in the repository
265     * @param lang the language
266     * @param idValue the content name
267     * @param forceStrictCheck <code>true</code> to force strict mode to search for the content only in the current collection, otherwise it read the "checkCollection" option
268     * @return the content in the repository, or null if does not exist
269     */
270    public ModifiableContent getContent(String lang, String idValue, boolean forceStrictCheck);
271    
272    /**
273     * Search the data to import from parameters.
274     * @param searchParameters Parameters for the search
275     * @param offset Begin of the search
276     * @param limit Number of results
277     * @param sort Sort of results (ignored for LDAP results)
278     * @param logger The logger
279     * @return A map of remote values by content
280     */
281    public Map<String, Map<String, Object>> search(Map<String, Object> searchParameters, int offset, int limit, List<Object> sort, Logger logger);
282
283    /**
284     * Method to update the synchronisation informations (collection and value of the ID field).
285     * @param content Content to update
286     * @param syncCode New synchronization code
287     * @param logger The logger
288     * @throws Exception if an error occurs.
289     */
290    public void updateSyncInformations(ModifiableContent content, String syncCode, Logger logger) throws Exception;
291    
292    /**
293     * Return the total number of results for the search.
294     * @param searchParameters Parameters for the search
295     * @param logger The logger
296     * @return The total count
297     */
298    public int getTotalCount(Map<String, Object> searchParameters, Logger logger);
299    
300    /**
301     * Retrieves the result of the synchronization as a map of key / content count containing
302     * <ul>
303     * <li>The number of created contents</li>
304     * <li>The number of synchronized contents</li>
305     * <li>The number of unchanged contents</li>
306     * <li>The number of deleted contents</li>
307     * </ul>
308     * @return the result of the synchronization
309     */
310    public Map<String, Integer> getSynchronizationResult();
311    
312    /**
313     * Return true if a {@link RightAssignmentContext} should be automatically generated for the contents of this SCC
314     * @return true to automatically generate a {@link RightAssignmentContext}. Return false if the rights of contents of this SCC are handle by its own {@link RightAssignmentContext}
315     */
316    public default boolean handleRightAssignmentContext()
317    {
318        return true;
319    }
320}