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.odfsync.apogee.scc.impl;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang3.tuple.Pair;
024import org.slf4j.Logger;
025
026import org.ametys.cms.data.ContentSynchronizationResult;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ModifiableContent;
029import org.ametys.core.user.population.UserPopulationDAO;
030import org.ametys.odf.course.Course;
031import org.ametys.odf.course.ShareableCourseHelper;
032import org.ametys.odf.courselist.CourseList;
033import org.ametys.odf.enumeration.OdfReferenceTableHelper;
034import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
035import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsWithCatalogCollection;
036import org.ametys.plugins.repository.data.holder.values.SynchronizationResult;
037
038/**
039 * SCC for course contents.
040 */
041public class CourseSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsWithCatalogCollection
042{
043    /** SCC model id */
044    public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.course";
045    
046    /** The ODF reference table helper */
047    protected OdfReferenceTableHelper _odfRefTableHelper;
048    
049    /** The shareable course helper */
050    protected ShareableCourseHelper _shareableCourseHelper;
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _odfRefTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE);
057        _shareableCourseHelper = (ShareableCourseHelper) manager.lookup(ShareableCourseHelper.ROLE);
058    }
059    
060    @Override
061    protected List<Map<String, Object>> _search(Map<String, Object> searchParameters, Logger logger)
062    {
063        return _convertBigDecimal(_apogeeDAO.searchCourses(getDataSourceId(), getParameterValues(), searchParameters));
064    }
065
066    @Override
067    protected String getMappingName()
068    {
069        return "course";
070    }
071    
072    @Override
073    protected Map<String, Object> getAdditionalAttributeValues(String idValue, Content content, Map<String, Object> additionalParameters, boolean create, Logger logger)
074    {
075        Map<String, Object> additionalValues = super.getAdditionalAttributeValues(idValue, content, additionalParameters, create, logger);
076        
077        // Handle course parts children
078        SynchronizableContentsCollection scc = _sccHelper.getSCCFromModelId(CoursePartSynchronizableContentsCollection.MODEL_ID);
079        List<ModifiableContent> courseParts = create
080                // Import mode
081                ? _importChildren(idValue, scc, logger)
082                // Synchronization mode
083                : _synchronizeChildren(content, scc, Course.CHILD_COURSE_PARTS, logger);
084        additionalValues.put(Course.CHILD_COURSE_PARTS, courseParts.toArray(new ModifiableContent[courseParts.size()]));
085        
086        return additionalValues;
087    }
088    
089    @Override
090    protected Pair<String, Object> getParentAttribute(ModifiableContent parent)
091    {
092        if (parent instanceof CourseList)
093        {
094            return Pair.of(Course.PARENT_COURSE_LISTS, new ModifiableContent[] {parent});
095        }
096
097        return super.getParentAttribute(parent);
098    }
099    
100    @Override
101    public ContentSynchronizationResult additionalImportOperations(ModifiableContent content, Map<String, Object> additionalParameters, Logger logger)
102    {
103        ContentSynchronizationResult result = super.additionalImportOperations(content, additionalParameters, logger);
104        
105        CourseList parentContent = getParentFromAdditionalParameters(additionalParameters)
106                .map(CourseList.class::cast)
107                .orElse(null);
108        
109        SynchronizationResult additionalResult = new SynchronizationResult();
110        boolean hasChanges = _shareableCourseHelper.initializeShareableFields((Course) content, parentContent, UserPopulationDAO.SYSTEM_USER_IDENTITY, true);
111        additionalResult.setHasChanged(hasChanges);
112        
113        result.aggregateResult(additionalResult);
114        return result;
115    }
116    
117    @Override
118    protected String getChildrenSCCModelId()
119    {
120        return CourseListSynchronizableContentsCollection.MODEL_ID;
121    }
122    
123    @Override
124    protected String getChildrenAttributeName()
125    {
126        return Course.CHILD_COURSE_LISTS;
127    }
128}