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.slf4j.Logger;
022
023import org.ametys.cms.content.external.ExternalizableMetadataHelper;
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.repository.ModifiableDefaultContent;
026import org.ametys.odf.course.Course;
027import org.ametys.odf.courselist.CourseList;
028import org.ametys.odf.program.ProgramPart;
029import org.ametys.odf.program.TraversableProgramPart;
030import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsWithCatalogCollection;
031import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
032
033/**
034 * SCC for course lsit contents.
035 */
036public class CourseListSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsWithCatalogCollection
037{
038    /** SCC model id */
039    public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.courselist";
040    
041    @Override
042    protected List<Map<String, Object>> _search(Map<String, Object> searchParams, Logger logger)
043    {
044        return _convertBigDecimal(_apogeeDAO.searchCourseLists(getDataSourceId(), getParameterValues(), searchParams));
045    }
046
047    @Override
048    protected String getMappingName()
049    {
050        return "courselist";
051    }
052
053    @Override
054    protected boolean handleParent(ModifiableDefaultContent currentContent, ModifiableDefaultContent parentContent, Logger logger)
055    {
056        boolean hasChanges = false;
057        
058        if (parentContent instanceof Course)
059        {
060            ModifiableCompositeMetadata cm = currentContent.getMetadataHolder();
061            hasChanges = ExternalizableMetadataHelper.setMetadata(cm, CourseList.PARENT_COURSES, new Content[] {parentContent});
062            if (_updateRelation(parentContent.getMetadataHolder(), Course.CHILD_COURSE_LISTS, currentContent))
063            {
064                _apogeeSCCHelper.addToHandleParentContents(parentContent.getId());
065            }
066        }
067        else if (parentContent instanceof ProgramPart)
068        {
069            ModifiableCompositeMetadata cm = currentContent.getMetadataHolder();
070            hasChanges = ExternalizableMetadataHelper.setMetadata(cm, ProgramPart.PARENT_PROGRAM_PARTS, new Content[] {parentContent});
071            if (_updateRelation(parentContent.getMetadataHolder(), TraversableProgramPart.CHILD_PROGRAM_PARTS, currentContent))
072            {
073                _apogeeSCCHelper.addToHandleParentContents(parentContent.getId());
074            }
075        }
076        
077        return hasChanges;
078    }
079
080    @Override
081    protected String getChildrenSCCModelId()
082    {
083        return CourseSynchronizableContentsCollection.MODEL_ID;
084    }
085    
086    @Override
087    protected String getChildrenMetadataName()
088    {
089        return CourseList.CHILD_COURSES;
090    }
091    
092    @Override
093    protected String getChildrenInvertMetadataName()
094    {
095        return Course.PARENT_COURSE_LISTS;
096    }
097}