001/*
002 *  Copyright 2018 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.odfsync.cdmfr.components;
017
018import java.io.File;
019import java.io.FileInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.List;
026import java.util.Map;
027import java.util.Optional;
028import java.util.Set;
029
030import javax.jcr.RepositoryException;
031
032import org.apache.avalon.framework.activity.Initializable;
033import org.apache.avalon.framework.component.Component;
034import org.apache.avalon.framework.configuration.Configurable;
035import org.apache.avalon.framework.configuration.Configuration;
036import org.apache.avalon.framework.configuration.ConfigurationException;
037import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
038import org.apache.avalon.framework.context.ContextException;
039import org.apache.avalon.framework.context.Contextualizable;
040import org.apache.avalon.framework.service.ServiceException;
041import org.apache.avalon.framework.service.ServiceManager;
042import org.apache.avalon.framework.service.Serviceable;
043import org.apache.cocoon.Constants;
044import org.apache.cocoon.ProcessingException;
045import org.apache.cocoon.environment.Context;
046import org.apache.commons.lang3.StringUtils;
047import org.apache.excalibur.xml.dom.DOMParser;
048import org.apache.excalibur.xml.xpath.XPathProcessor;
049import org.slf4j.Logger;
050import org.w3c.dom.Document;
051import org.w3c.dom.Element;
052import org.w3c.dom.Node;
053import org.w3c.dom.NodeList;
054import org.xml.sax.InputSource;
055import org.xml.sax.SAXException;
056
057import org.ametys.cms.ObservationConstants;
058import org.ametys.cms.contenttype.ContentType;
059import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
060import org.ametys.cms.data.ContentSynchronizationResult;
061import org.ametys.cms.repository.ContentQueryHelper;
062import org.ametys.cms.repository.ContentTypeExpression;
063import org.ametys.cms.repository.LanguageExpression;
064import org.ametys.cms.repository.ModifiableContent;
065import org.ametys.cms.repository.WorkflowAwareContent;
066import org.ametys.cms.workflow.ContentWorkflowHelper;
067import org.ametys.cms.workflow.EditContentFunction;
068import org.ametys.core.observation.Event;
069import org.ametys.core.observation.ObservationManager;
070import org.ametys.core.user.CurrentUserProvider;
071import org.ametys.core.user.population.UserPopulationDAO;
072import org.ametys.odf.ODFHelper;
073import org.ametys.odf.ProgramItem;
074import org.ametys.odf.catalog.CatalogsManager;
075import org.ametys.odf.course.Course;
076import org.ametys.odf.course.ShareableCourseHelper;
077import org.ametys.odf.courselist.CourseList;
078import org.ametys.odf.coursepart.CoursePart;
079import org.ametys.odf.enumeration.OdfReferenceTableEntry;
080import org.ametys.odf.enumeration.OdfReferenceTableHelper;
081import org.ametys.odf.observation.OdfObservationConstants;
082import org.ametys.odf.orgunit.OrgUnit;
083import org.ametys.odf.orgunit.RootOrgUnitProvider;
084import org.ametys.odf.program.Program;
085import org.ametys.odf.translation.TranslationHelper;
086import org.ametys.odf.workflow.AbstractCreateODFContentFunction;
087import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
088import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionDataProvider;
089import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollectionHelper;
090import org.ametys.plugins.contentio.synchronize.workflow.EditSynchronizedContentFunction;
091import org.ametys.plugins.odfsync.cdmfr.CDMFrSyncExtensionPoint;
092import org.ametys.plugins.odfsync.cdmfr.ImportCDMFrContext;
093import org.ametys.plugins.odfsync.cdmfr.extractor.ImportCDMFrValuesExtractor;
094import org.ametys.plugins.odfsync.cdmfr.extractor.ImportCDMFrValuesExtractorFactory;
095import org.ametys.plugins.odfsync.cdmfr.transformers.CDMFrSyncTransformer;
096import org.ametys.plugins.odfsync.utils.ContentWorkflowDescription;
097import org.ametys.plugins.repository.AmetysObjectIterable;
098import org.ametys.plugins.repository.AmetysObjectResolver;
099import org.ametys.plugins.repository.data.external.ExternalizableDataProvider.ExternalizableDataStatus;
100import org.ametys.plugins.repository.data.extractor.ModelAwareValuesExtractor;
101import org.ametys.plugins.repository.data.holder.values.SynchronizationContext;
102import org.ametys.plugins.repository.jcr.NameHelper;
103import org.ametys.plugins.repository.lock.LockAwareAmetysObject;
104import org.ametys.plugins.repository.query.expression.AndExpression;
105import org.ametys.plugins.repository.query.expression.Expression;
106import org.ametys.plugins.repository.query.expression.Expression.Operator;
107import org.ametys.plugins.repository.query.expression.OrExpression;
108import org.ametys.plugins.repository.query.expression.StringExpression;
109import org.ametys.plugins.workflow.AbstractWorkflowComponent;
110import org.ametys.plugins.workflow.component.CheckRightsCondition;
111import org.ametys.runtime.config.Config;
112import org.ametys.runtime.model.ModelItem;
113import org.ametys.runtime.model.View;
114
115import com.opensymphony.workflow.InvalidActionException;
116import com.opensymphony.workflow.WorkflowException;
117
118/**
119 * Abstract class of a component to import a CDM-fr input stream.
120 */
121public abstract class AbstractImportCDMFrComponent implements ImportCDMFrComponent, Serviceable, Initializable, Contextualizable, Configurable, Component
122{
123    /** Tag to identify a program */
124    protected static final String _TAG_PROGRAM = "program";
125
126    /** Tag to identify a subprogram */
127    protected static final String _TAG_SUBPROGRAM = "subProgram";
128    
129    /** Tag to identify a container */
130    protected static final String _TAG_CONTAINER = "container";
131    
132    /** Tag to identify a courseList */
133    protected static final String _TAG_COURSELIST = "coursesReferences";
134    
135    /** Tag to identify a coursePart */
136    protected static final String _TAG_COURSEPART = "coursePart";
137
138    /** The synchronize workflow action id */
139    protected static final int _SYNCHRONIZE_WORKFLOW_ACTION_ID = 800;
140    
141    /** The Cocoon context */
142    protected Context _cocoonContext;
143    
144    /** The DOM parser */
145    protected DOMParser _domParser;
146
147    /** The XPath processor */
148    protected XPathProcessor _xPathProcessor;
149
150    /** Extension point to transform CDM-fr */
151    protected CDMFrSyncExtensionPoint _cdmFrSyncExtensionPoint;
152
153    /** Default language configured for ODF */
154    protected String _odfLang;
155    
156    /** The catalog manager */
157    protected CatalogsManager _catalogsManager;
158
159    /** The ametys object resolver */
160    protected AmetysObjectResolver _resolver;
161
162    /** The ODF TableRef Helper */
163    protected OdfReferenceTableHelper _odfRefTableHelper;
164
165    /** The content type extension point */
166    protected ContentTypeExtensionPoint _contentTypeEP;
167
168    /** The current user provider */
169    protected CurrentUserProvider _currentUserProvider;
170
171    /** The observation manager */
172    protected ObservationManager _observationManager;
173    
174    /** The root orgunit provider */
175    protected RootOrgUnitProvider _rootOUProvider;
176
177    /** The ODF Helper */
178    protected ODFHelper _odfHelper;
179    
180    /** The SCC helper */
181    protected SynchronizableContentsCollectionHelper _sccHelper;
182    
183    /** The content workflow helper */
184    protected ContentWorkflowHelper _contentWorkflowHelper;
185    
186    /** The shareable course helper */
187    protected ShareableCourseHelper _shareableCourseHelper;
188    
189    /** the {@link ImportCDMFrValuesExtractor} factory */
190    protected ImportCDMFrValuesExtractorFactory _valuesExtractorFactory;
191    
192    
193    /** List of imported contents */
194    protected Map<String, Integer> _importedContents;
195    
196    /** List of synchronized contents */
197    protected Set<String> _synchronizedContents;
198
199    /** Number of errors encountered */
200    protected int _nbError;
201    /** The prefix of the contents */
202    protected String _contentPrefix;
203    /** Synchronized fields by content type */
204    protected Map<String, Set<String>> _syncFieldsByContentType;
205
206    public void initialize() throws Exception
207    {
208        _odfLang = Config.getInstance().getValue("odf.programs.lang");
209    }
210
211    @Override
212    public void contextualize(org.apache.avalon.framework.context.Context context) throws ContextException
213    {
214        _cocoonContext = (Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
215    }
216
217    public void configure(Configuration configuration) throws ConfigurationException
218    {
219        _parseSynchronizedFields();
220    }
221    
222    @Override
223    public void service(ServiceManager manager) throws ServiceException
224    {
225        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
226        _domParser = (DOMParser) manager.lookup(DOMParser.ROLE);
227        _xPathProcessor = (XPathProcessor) manager.lookup(XPathProcessor.ROLE);
228        _cdmFrSyncExtensionPoint = (CDMFrSyncExtensionPoint) manager.lookup(CDMFrSyncExtensionPoint.ROLE);
229        _catalogsManager = (CatalogsManager) manager.lookup(CatalogsManager.ROLE);
230        _odfRefTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE);
231        _contentTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
232        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
233        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
234        _rootOUProvider = (RootOrgUnitProvider) manager.lookup(RootOrgUnitProvider.ROLE);
235        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
236        _sccHelper = (SynchronizableContentsCollectionHelper) manager.lookup(SynchronizableContentsCollectionHelper.ROLE);
237        _contentWorkflowHelper = (ContentWorkflowHelper) manager.lookup(ContentWorkflowHelper.ROLE);
238        _shareableCourseHelper = (ShareableCourseHelper) manager.lookup(ShareableCourseHelper.ROLE);
239        _valuesExtractorFactory = (ImportCDMFrValuesExtractorFactory) manager.lookup(ImportCDMFrValuesExtractorFactory.ROLE);
240    }
241
242    @Override
243    public String getIdField()
244    {
245        return "cdmfrSyncCode";
246    }
247
248    /**
249     * Get the synchronized metadata from the configuration file
250     * @throws ConfigurationException if the configuration is not valid.
251     */
252    private void _parseSynchronizedFields() throws ConfigurationException
253    {
254        _syncFieldsByContentType = new HashMap<>();
255        
256        File cdmfrMapping = new File(_cocoonContext.getRealPath("/WEB-INF/param/odf-synchro.xml"));
257        try (InputStream is = !cdmfrMapping.isFile()
258                            ? getClass().getResourceAsStream("/org/ametys/plugins/odfsync/cdmfr/odf-synchro.xml")
259                            : new FileInputStream(cdmfrMapping))
260        {
261            Configuration cfg = new DefaultConfigurationBuilder().build(is);
262
263            Configuration[] cTypesConf = cfg.getChildren("content-type");
264            for (Configuration cTypeConf : cTypesConf)
265            {
266                String contentType = cTypeConf.getAttribute("id");
267                Set<String> syncAttributes = _configureSynchronizedFields(cTypeConf, StringUtils.EMPTY);
268                _syncFieldsByContentType.put(contentType, syncAttributes);
269            }
270        }
271        catch (Exception e)
272        {
273            throw new ConfigurationException("Error while parsing odf-synchro.xml", e);
274        }
275    }
276
277    private Set<String> _configureSynchronizedFields(Configuration configuration, String prefix) throws ConfigurationException
278    {
279        Set<String> syncAttributes = new HashSet<>();
280        Configuration[] attributesConf = configuration.getChildren("attribute");
281        
282        if (attributesConf.length > 0)
283        {
284            for (Configuration attributeConf : attributesConf)
285            {
286                if (attributeConf.getChildren("attribute").length > 0)
287                {
288                    // composite
289                    syncAttributes.addAll(_configureSynchronizedFields(attributeConf, prefix + attributeConf.getAttribute("name") + ModelItem.ITEM_PATH_SEPARATOR));
290                }
291                else
292                {
293                    syncAttributes.add(prefix + attributeConf.getAttribute("name"));
294                }
295            }
296        }
297        else if (configuration.getAttribute("name", null) != null)
298        {
299            syncAttributes.add(prefix + configuration.getAttribute("name"));
300        }
301        
302        return syncAttributes;
303    }
304    
305    @Override
306    @SuppressWarnings("unchecked")
307    public synchronized Map<String, Object> handleInputStream(InputStream input, Map<String, Object> parameters, SynchronizableContentsCollection scc, Logger logger) throws ProcessingException
308    {
309        List<ModifiableContent> importedPrograms = new ArrayList<>();
310        
311        _importedContents = new HashMap<>();
312        _synchronizedContents = (Set<String>) parameters.getOrDefault("updatedContents", new HashSet<>());
313        int nbCreatedContents = (int) parameters.getOrDefault("nbCreatedContents", 0);
314        int nbSynchronizedContents = (int) parameters.getOrDefault("nbSynchronizedContents", 0);
315        _nbError = (int) parameters.getOrDefault("nbError", 0);
316        _contentPrefix = (String) parameters.getOrDefault("contentPrefix", "cdmfr-");
317        additionalParameters(parameters);
318
319        Map<String, Object> resultMap = new HashMap<>();
320        
321        try
322        {
323            Document doc = _domParser.parseDocument(new InputSource(input));
324            doc = transformDocument(doc, new HashMap<String, Object>(), logger);
325            
326            if (doc != null)
327            {
328                String defaultLang = _getXPathString(doc, "CDM/@language", _odfLang);
329                
330                NodeList nodes = doc.getElementsByTagName(_TAG_PROGRAM);
331                
332                for (int i = 0; i < nodes.getLength(); i++)
333                {
334                    Element contentElement = (Element) nodes.item(i);
335                    String syncCode = _xPathProcessor.evaluateAsString(contentElement, "@CDMid");
336                    String contentLang = _getXPathString(contentElement, "@language", defaultLang);
337                    contentLang = StringUtils.substring(contentLang, 0, 2).toLowerCase(); // on keep the language from the locale
338                    
339                    String catalog = getCatalogName(contentElement);
340
341                    ImportCDMFrContext context = new ImportCDMFrContext(scc, doc, contentLang, catalog, logger);
342                    importedPrograms.add(importOrSynchronizeContent(contentElement, ContentWorkflowDescription.PROGRAM_WF_DESCRIPTION, syncCode, syncCode, context));
343                }
344                
345                // Validate newly imported contents
346                if (validateAfterImport())
347                {
348                    for (String contentId : _importedContents.keySet())
349                    {
350                        WorkflowAwareContent content = _resolver.resolveById(contentId);
351                        Integer validationActionId = _importedContents.get(contentId);
352                        if (validationActionId > 0)
353                        {
354                            validateContent(content, validationActionId, logger);
355                        }
356                    }
357                }
358            }
359        }
360        catch (IOException | ProcessingException e)
361        {
362            throw new ProcessingException("An error occured while transforming the stream.", e);
363        }
364        catch (SAXException e)
365        {
366            throw new ProcessingException("An error occured while parsing the stream.", e);
367        }
368        catch (Exception e)
369        {
370            throw new ProcessingException("An error occured while synchronizing values on contents.", e);
371        }
372
373        resultMap.put("importedContents", _importedContents.keySet());
374        resultMap.put("nbCreatedContents", nbCreatedContents + _importedContents.size());
375        resultMap.put("updatedContents", _synchronizedContents);
376        resultMap.put("nbSynchronizedContents", nbSynchronizedContents + _synchronizedContents.size());
377        resultMap.put("nbError", _nbError);
378        resultMap.put("importedPrograms", importedPrograms);
379
380        return resultMap;
381    }
382
383    /**
384     * True to validate the contents after import
385     * @return True to validate the contents after import
386     */
387    protected abstract boolean validateAfterImport();
388
389    /**
390     * When returns true, a content created by a previous synchro will be removed if it does not exist anymore during the current synchro.
391     * @return true if a content created by a previous synchro has to be removed if it does not exist anymore during the current synchro.
392     */
393    protected abstract boolean removalSync();
394    
395    /**
396     * Additional parameters for specific treatments.
397     * @param parameters The parameters map to get
398     */
399    protected abstract void additionalParameters(Map<String, Object> parameters);
400    
401    /**
402     * Transform the document depending of it structure.
403     * @param document Document to transform.
404     * @param parameters Optional parameters for transformation
405     * @param logger The logger
406     * @return The transformed document.
407     * @throws IOException if an error occurs.
408     * @throws SAXException if an error occurs.
409     * @throws ProcessingException if an error occurs.
410     */
411    protected Document transformDocument(Document document, Map<String, Object> parameters, Logger logger) throws IOException, SAXException, ProcessingException
412    {
413        CDMFrSyncTransformer transformer = _cdmFrSyncExtensionPoint.getTransformer(document);
414        if (transformer == null)
415        {
416            logger.error("Cannot match a CDM-fr transformer to this file structure.");
417            return null;
418        }
419        
420        return transformer.transform(document, parameters);
421    }
422
423    public String getCatalogName(Element contentElement)
424    {
425        String defaultCatalog = _catalogsManager.getDefaultCatalogName();
426        
427        String contentCatalog = _getXPathString(contentElement, "catalog", defaultCatalog);
428        if (_catalogsManager.getCatalog(contentCatalog) == null)
429        {
430            // Catalog is empty or do not exist, use the default catalog
431            return defaultCatalog;
432        }
433        
434        return contentCatalog;
435    }
436    
437    public ModifiableContent importOrSynchronizeContent(Element contentElement, ContentWorkflowDescription wfDescription, String title, String syncCode, ImportCDMFrContext context)
438    {
439        ModifiableContent content = _getOrCreateContent(wfDescription, title, syncCode, context);
440        
441        if (content != null)
442        {
443            try
444            {
445                _sccHelper.updateLastSynchronizationProperties(content);
446                content.saveChanges();
447                _synchronizeContent(contentElement, content, wfDescription.getContentType(), syncCode, context);
448            }
449            catch (Exception e)
450            {
451                _nbError++;
452                context.getLogger().error("Failed to synchronize data for content {} and language {}.", content, context.getLang(), e);
453            }
454        }
455        
456        return content;
457    }
458    
459    /**
460     * Get or create the content from the workflow description, the synchronization code and the import context.
461     * @param wfDescription The workflow description
462     * @param title The title
463     * @param syncCode The synchronization code
464     * @param context The import context
465     * @return the retrieved or created content
466     */
467    protected ModifiableContent _getOrCreateContent(ContentWorkflowDescription wfDescription, String title, String syncCode, ImportCDMFrContext context)
468    {
469        ModifiableContent receivedContent = getContent(wfDescription.getContentType(), syncCode, context);
470        if (receivedContent != null)
471        {
472            return receivedContent;
473        }
474        
475        try
476        {
477            context.getLogger().info("Creating content '{}' with the content type '{}' for language {}", title, wfDescription.getContentType(), context.getLang());
478            
479            ModifiableContent content = _createContent(wfDescription, title, context);
480            if (content != null)
481            {
482                _sccHelper.updateSCCProperty(content, context.getSCC().getId());
483                content.setValue(getIdField(), syncCode);
484                content.saveChanges();
485                _importedContents.put(content.getId(), wfDescription.getValidationActionId());
486            }
487            
488            return content;
489        }
490        catch (WorkflowException | RepositoryException e)
491        {
492            context.getLogger().error("Failed to initialize workflow for content {} and language {}", title, context.getLang(), e);
493            _nbError++;
494            return null;
495        }
496    }
497
498    public ModifiableContent getContent(String contentType, String syncCode, ImportCDMFrContext context)
499    {
500        List<Expression> expList = _getExpressionsList(contentType, syncCode, context);
501        AndExpression andExp = new AndExpression(expList.toArray(new Expression[expList.size()]));
502        String xPathQuery = ContentQueryHelper.getContentXPathQuery(andExp);
503
504        AmetysObjectIterable<ModifiableContent> contents = _resolver.query(xPathQuery);
505
506        if (contents.getSize() > 0)
507        {
508            return contents.iterator().next();
509        }
510        
511        return null;
512    }
513    
514    /**
515     * Create the content from the workflow description and the import context.
516     * @param wfDescription The workflow description
517     * @param title The title
518     * @param context The import context
519     * @return the created content
520     * @throws WorkflowException if an error occurs while creating the content 
521     */
522    protected ModifiableContent _createContent(ContentWorkflowDescription wfDescription, String title, ImportCDMFrContext context) throws WorkflowException
523    {
524        String contentName = NameHelper.filterName(_contentPrefix + "-" + title + "-" + context.getLang());
525        
526        Map<String, Object> inputs = _getInputsForContentCreation(wfDescription, context);
527        Map<String, Object> result = _contentWorkflowHelper.createContent(
528                wfDescription.getWorkflowName(),
529                wfDescription.getInitialActionId(),
530                contentName,
531                title,
532                new String[] {wfDescription.getContentType()},
533                null,
534                context.getLang(),
535                null,
536                null,
537                inputs);
538        
539        return _resolver.resolveById((String) result.get("contentId"));
540    }
541    
542    /**
543     * Retrieves the inputs to give for content creation
544     * @param wfDescription The workflow description
545     * @param context The import context
546     * @return the inputs to give for content creation
547     */
548    protected Map<String, Object> _getInputsForContentCreation(ContentWorkflowDescription wfDescription, ImportCDMFrContext context)
549    {
550        Map<String, Object> inputs = new HashMap<>();
551        
552        ContentType contentType = _contentTypeEP.getExtension(wfDescription.getContentType());
553        if (contentType.hasModelItem(ProgramItem.CATALOG) || contentType.hasModelItem(CoursePart.CATALOG))
554        {
555            inputs.put(AbstractCreateODFContentFunction.CONTENT_CATALOG_KEY, context.getCatalog());
556        }
557        
558        return inputs;
559    }
560    
561    /**
562     * Synchronize content 
563     * @param contentElement the DOM content element
564     * @param content The content to synchronize
565     * @param contentTypeId The content type ID
566     * @param syncCode The synchronization code
567     * @param context the import context
568     * @throws Exception if an error occurs while synchronizing the content values
569     */
570    protected void _synchronizeContent(Element contentElement, ModifiableContent content, String contentTypeId, String syncCode, ImportCDMFrContext context) throws Exception
571    {
572        Logger logger = context.getLogger();
573        
574        // Avoid a treatment twice or more
575        if (_synchronizedContents.add(content.getId()))
576        {
577            logger.info("Synchronization of the content '{}' with the content type '{}'", content.getTitle(), contentTypeId);
578            
579            if (content instanceof LockAwareAmetysObject && ((LockAwareAmetysObject) content).isLocked())
580            {
581                logger.warn("The content '{}' ({}) is currently locked by user {}: it cannot be synchronized", content.getTitle(), content.getId(), ((LockAwareAmetysObject) content).getLockOwner());
582            }
583            else if (content instanceof WorkflowAwareContent)
584            {
585                ContentType contentType = _contentTypeEP.getExtension(contentTypeId);
586                ModelAwareValuesExtractor valuesExtractor = _valuesExtractorFactory.getValuesExtractor(contentElement, this, content, contentType, syncCode, context);
587                
588                // Extract the values
589                Map<String, Object> values = valuesExtractor.extractValues();
590                values.putAll(_getAdditionalValuesToSynchronize(content, syncCode, context));
591                
592                // Modify the content with the extracted values
593                boolean create = _importedContents.containsKey(content.getId());
594                Set<String> notSynchronizedContentIds = _getNotSynchronizedRelatedContentIds(content, syncCode, context);
595                _editContent((WorkflowAwareContent) content, Optional.empty(), values, create, notSynchronizedContentIds, context);
596                
597                if (content instanceof OrgUnit)
598                {
599                    _setOrgUnitParent((WorkflowAwareContent) content, context);
600                }
601                
602                // Create translation links
603                _linkTranslationsIfExist(content, contentTypeId, context);
604            }
605        }
606    }
607
608    /**
609     * Retrieves additional values to synchronize for the content
610     * @param content the content
611     * @param syncCode the content synchronization code
612     * @param context the import context
613     * @return the additional values
614     */
615    protected Map<String, Object> _getAdditionalValuesToSynchronize(ModifiableContent content, String syncCode, ImportCDMFrContext context)
616    {
617        Map<String, Object> additionalValues = new HashMap<>();
618        additionalValues.put(getIdField(), syncCode);
619        return additionalValues;
620    }
621    
622    /**
623     * Retrieves the ids of the contents related to the given content but that are not part of the synchronization 
624     * @param content the content
625     * @param syncCode the content synchronization code
626     * @param context the import context
627     * @return the not synchronized content ids
628     */
629    protected Set<String> _getNotSynchronizedRelatedContentIds(ModifiableContent content, String syncCode, ImportCDMFrContext context)
630    {
631        return new HashSet<>();
632    }
633    
634    /**
635     * Synchronize the content with given values.
636     * @param content The content to synchronize
637     * @param view the view containing the item to edit
638     * @param values the values
639     * @param create <code>true</code> if content is creating, false if it is updated
640     * @param notSynchronizedContentIds the ids of the contents related to the given content but that are not part of the synchronization
641     * @param context the import context 
642     * @throws WorkflowException if an error occurs
643     */
644    protected void _editContent(WorkflowAwareContent content, Optional<View> view, Map<String, Object> values, boolean create, Set<String> notSynchronizedContentIds, ImportCDMFrContext context) throws WorkflowException
645    {
646        SynchronizationContext synchronizationContext = SynchronizationContext.newInstance()
647                                                                              .withStatus(ExternalizableDataStatus.EXTERNAL)
648                                                                              .withExternalizableDataContextEntry(SynchronizableContentsCollectionDataProvider.SCC_ID_CONTEXT_KEY, context.getSCC().getId());
649
650        if (view.map(v -> content.hasDifferences(v, values, synchronizationContext))
651                .orElseGet(() -> content.hasDifferences(values, synchronizationContext)))
652        {
653            context.getLogger().info("Some changes were detected for content '{}' and language {}", content.getTitle(), context.getLang());
654            
655            Map<String, Object> inputs = new HashMap<>();
656            inputs.put(EditSynchronizedContentFunction.SCC_KEY, context.getSCC());
657            inputs.put(EditSynchronizedContentFunction.SCC_LOGGER_KEY, context.getLogger());
658            inputs.put(EditSynchronizedContentFunction.NOT_SYNCHRONIZED_RELATED_CONTENT_IDS_KEY, notSynchronizedContentIds);
659            if (ignoreRights())
660            {
661                inputs.put(CheckRightsCondition.FORCE, true);
662            }
663    
664            Map<String, Object> params = new HashMap<>();
665            // Remove catalog data, this value is forced at creation and should not be modified
666            values.remove(ProgramItem.CATALOG);
667            params.put(EditContentFunction.VALUES_KEY, values);
668            view.ifPresent(v -> params.put(EditContentFunction.VIEW, v));
669            params.put(EditContentFunction.QUIT, true);
670            params.put(EditSynchronizedContentFunction.IMPORT, create);
671            inputs.put(AbstractWorkflowComponent.CONTEXT_PARAMETERS_KEY, params);
672            
673            _contentWorkflowHelper.doAction(content, _SYNCHRONIZE_WORKFLOW_ACTION_ID, inputs);
674        }
675        else
676        {
677            context.getLogger().info("No changes detected for content '{}' and language {}", content.getTitle(), context.getLang());
678        }
679    }
680    
681    public ContentSynchronizationResult additionalOperations(ModifiableContent content, Map<String, Object> additionalParameters, Logger logger)
682    {
683        ContentSynchronizationResult result = new ContentSynchronizationResult();
684
685        if (content instanceof Program)
686        {
687            List<ModifiableContent> modifiedContents = _initializeShareableCoursesFields((Program) content);
688            
689            result.addModifiedContents(modifiedContents);
690            result.setHasChanged(!modifiedContents.isEmpty());
691        }
692        
693        return result;
694    }
695    
696    /**
697     * Initialize shareable fields for the courses under the given {@link ProgramItem}
698     * @param programItem the program item
699     * @return the list of contents that have been modified during the initialization
700     */
701    protected List<ModifiableContent> _initializeShareableCoursesFields(ProgramItem programItem)
702    {
703        List<ModifiableContent> modifiedContents = new ArrayList<>();
704
705        List<ProgramItem> children = _odfHelper.getChildProgramItems(programItem);
706        for (ProgramItem child : children)
707        {
708            if (child instanceof Course && programItem instanceof CourseList)
709            {
710                if (_shareableCourseHelper.initializeShareableFields((Course) child, (CourseList) programItem,  UserPopulationDAO.SYSTEM_USER_IDENTITY, true))
711                {
712                    modifiedContents.add((Course) child);
713                }
714            }
715            
716            modifiedContents.addAll(_initializeShareableCoursesFields(child));
717        }
718
719        return modifiedContents;
720    }
721    
722    /**
723     * Search for translated contents
724     * @param importedContent The imported content
725     * @param contentType The content type
726     * @param context the import context
727     */
728    protected void _linkTranslationsIfExist(ModifiableContent importedContent, String contentType, ImportCDMFrContext context)
729    {
730        if (importedContent instanceof ProgramItem)
731        {
732            Expression expression = _getTranslationExpression(importedContent, contentType);
733            String xPathQuery = ContentQueryHelper.getContentXPathQuery(expression);
734    
735            AmetysObjectIterable<ModifiableContent> contents = _resolver.query(xPathQuery);
736            
737            Map<String, String> translations = new HashMap<>();
738            for (ModifiableContent content : contents)
739            {
740                translations.put(content.getLanguage(), content.getId());
741            }
742            
743            for (ModifiableContent content : contents)
744            {
745                TranslationHelper.setTranslations(content, translations);
746    
747                Map<String, Object> eventParams = new HashMap<>();
748                eventParams.put(ObservationConstants.ARGS_CONTENT, content);
749                eventParams.put(ObservationConstants.ARGS_CONTENT_ID, content.getId());
750                _observationManager.notify(new Event(OdfObservationConstants.ODF_CONTENT_TRANSLATED, _currentUserProvider.getUser(), eventParams));
751            }
752        }
753    }
754    
755    private Expression _getTranslationExpression(ModifiableContent content, String contentType)
756    {
757        List<Expression> expList = new ArrayList<>();
758        
759        if (StringUtils.isNotBlank(contentType))
760        {
761            expList.add(new ContentTypeExpression(Operator.EQ, contentType));
762        }
763        
764        String catalog = content.getValue(ProgramItem.CATALOG);
765        if (StringUtils.isNotBlank(catalog))
766        {
767            expList.add(new StringExpression(ProgramItem.CATALOG, Operator.EQ, catalog));
768        }
769        
770        List<Expression> codeExpressionList = new ArrayList<>();
771        String syncValue = content.getValue(getIdField());
772        if (StringUtils.isNotBlank(syncValue))
773        {
774            codeExpressionList.add(new StringExpression(getIdField(), Operator.EQ, syncValue));
775        }
776
777        String code = content.getValue(ProgramItem.CODE);
778        if (StringUtils.isNotBlank(syncValue))
779        {
780            codeExpressionList.add(new StringExpression(ProgramItem.CODE, Operator.EQ, code));
781        }
782        
783        if (!codeExpressionList.isEmpty())
784        {
785            expList.add(new OrExpression(codeExpressionList.toArray(Expression[]::new)));
786        }
787        
788        return new AndExpression(expList.toArray(Expression[]::new));
789    }
790
791    /**
792     * Set the orgUnit parent to rootOrgUnit.
793     * @param orgUnit The orgunit to link
794     * @param context the import context
795     * @throws Exception if an error occurs while synchronizing the content values
796     */
797    protected void _setOrgUnitParent(WorkflowAwareContent orgUnit, ImportCDMFrContext context) throws Exception
798    {
799        // Set the orgUnit parent (if no parent is set)
800        if (!orgUnit.hasValue(OrgUnit.PARENT_ORGUNIT))
801        {
802            OrgUnit rootOrgUnit = _rootOUProvider.getRoot();
803            Map<String, Object> values = new HashMap<>();
804            values.put(OrgUnit.PARENT_ORGUNIT, rootOrgUnit);
805            _editContent(orgUnit, Optional.empty(), values, false, Set.of(rootOrgUnit.getId()), context);
806        }
807    }
808    
809    /**
810     * Validates a content after import
811     * @param content The content to validate
812     * @param validationActionId Validation action ID to use for this content
813     * @param logger The logger
814     */
815    protected void validateContent(WorkflowAwareContent content, int validationActionId, Logger logger)
816    {
817        Map<String, Object> inputs = new HashMap<>();
818        if (ignoreRights())
819        {
820            inputs.put(CheckRightsCondition.FORCE, true);
821        }
822        
823        try
824        {
825            _contentWorkflowHelper.doAction(content, validationActionId, inputs);
826            logger.info("The content {} has been validated after import", content);
827        }
828        catch (WorkflowException | InvalidActionException e)
829        {
830            String failuresAsString = _getActionFailuresAsString(inputs);
831            logger.error("The content {} cannot be validated after import{}", content, failuresAsString, e);
832        }
833    }
834    
835    private String _getActionFailuresAsString(Map<String, Object> actionInputs)
836    {
837        String failuresAsString = "";
838        if (actionInputs.containsKey(AbstractWorkflowComponent.FAIL_CONDITIONS_KEY))
839        {
840            @SuppressWarnings("unchecked")
841            List<String> failures = (List<String>) actionInputs.get(AbstractWorkflowComponent.FAIL_CONDITIONS_KEY);
842            if (!failures.isEmpty())
843            {
844                failuresAsString = ", due to the following error(s):\n" + String.join("\n", failures);
845            }
846        }
847        
848        return failuresAsString;
849    }
850    
851    public String getIdFromCDMThenCode(String tableRefId, String cdmCode)
852    {
853        OdfReferenceTableEntry entry = _odfRefTableHelper.getItemFromCDM(tableRefId, cdmCode);
854        if (entry == null)
855        {
856            entry = _odfRefTableHelper.getItemFromCode(tableRefId, cdmCode);
857        }
858        return entry != null ? entry.getId() : null;
859    }
860    
861    private String _getXPathString(Node metadataNode, String xPath, String defaultValue)
862    {
863        String value = _xPathProcessor.evaluateAsString(metadataNode, xPath);
864        if (StringUtils.isEmpty(value))
865        {
866            value = defaultValue;
867        }
868        return value;
869    }
870
871    /**
872     * If true, bypass the rights check during the import process
873     * @return True if the rights check are bypassed during the import process
874     */
875    protected boolean ignoreRights()
876    {
877        return false;
878    }
879    
880    /**
881     * Construct the query to retrieve the content.
882     * @param contentTypeId The content type
883     * @param syncCode The synchronization code
884     * @param context the import context
885     * @return The {@link List} of {@link Expression}
886     */
887    protected List<Expression> _getExpressionsList(String contentTypeId, String syncCode, ImportCDMFrContext context)
888    {
889        List<Expression> expList = new ArrayList<>();
890        
891        if (StringUtils.isNotBlank(contentTypeId))
892        {
893            expList.add(new ContentTypeExpression(Operator.EQ, contentTypeId));
894            
895            if (StringUtils.isNotBlank(context.getCatalog()))
896            {
897                ContentType contentType = _contentTypeEP.getExtension(contentTypeId);
898                if (contentType.hasModelItem(ProgramItem.CATALOG) || contentType.hasModelItem(CoursePart.CATALOG))
899                {
900                    expList.add(new StringExpression(ProgramItem.CATALOG, Operator.EQ, context.getCatalog()));
901                }
902            }
903        }
904        
905        if (StringUtils.isNotBlank(syncCode))
906        {
907            expList.add(new StringExpression(getIdField(), Operator.EQ, syncCode));
908        }
909        
910        if (StringUtils.isNotBlank(context.getLang()))
911        {
912            expList.add(new LanguageExpression(Operator.EQ, context.getLang()));
913        }
914        
915        return expList;
916    }
917
918    @Override
919    public Set<String> getLocalAndExternalFields(Map<String, Object> additionalParameters)
920    {
921        if (additionalParameters == null || !additionalParameters.containsKey("contentTypes"))
922        {
923            throw new IllegalArgumentException("Content types shouldn't be null.");
924        }
925
926        @SuppressWarnings("unchecked")
927        List<String> contentTypeIds = (List<String>) additionalParameters.get("contentTypes");
928        Set<String> allSyncFields = new HashSet<>();
929        
930        for (String contentTypeId : contentTypeIds)
931        {
932            Set<String> syncFields = _syncFieldsByContentType.computeIfAbsent(contentTypeId, k -> new HashSet<>());
933            allSyncFields.addAll(syncFields);
934        }
935        
936        return allSyncFields;
937    }
938}