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