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.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.slf4j.Logger;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.repository.ModifiableContent;
027import org.ametys.odf.ProgramItem;
028import org.ametys.odf.program.TraversableProgramPart;
029import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
030import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsWithCatalogCollection;
031import org.ametys.plugins.odfsync.apogee.scc.ApogeeSynchronizableContentsCollection;
032
033/**
034 * SCC for program contents.
035 */
036public class ProgramSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsWithCatalogCollection
037{
038    /** SCC model id */
039    public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.program";
040    
041    @Override
042    protected List<Map<String, Object>> _search(Map<String, Object> searchParameters, Logger logger)
043    {
044        String dataSourceId = getDataSourceId();
045        
046        List<Map<String, Object>> requestValues = _convertBigDecimal(_apogeeDAO.searchPrograms(dataSourceId, getParameterValues(), searchParameters));
047        if (!searchParameters.containsKey("__count") && (searchParameters.containsKey("apogeeSyncCode") || searchParameters.containsKey("parentCode") || searchParameters.containsKey("isGlobalSync")))
048        {
049            for (Map<String, Object> values : requestValues)
050            {
051                String idValue = values.get(getIdColumn()).toString();
052                
053                Map<String, Object> parameters = new HashMap<>();
054                parameters.put(getIdField(), idValue);
055                
056                // Get the joint orgunits
057                values.putAll(_transformListOfMap2MapOfList(_apogeeDAO.getJointOrgUnits(dataSourceId, getParameterValues(), parameters)));
058                
059                // Get the modalities
060                values.putAll(_transformListOfMap2MapOfList(_apogeeDAO.getFormOfTeachingOrg(dataSourceId, getParameterValues(), parameters)));
061                
062                // Get the add elements
063                values.putAll(_transformListOfMap2MapOfList(_apogeeDAO.getAddElements(dataSourceId, getParameterValues(), parameters)));
064                
065                // Get the domains
066                values.putAll(_transformListOfMap2MapOfList(_apogeeDAO.getDomains(dataSourceId, getParameterValues(), parameters)));
067            }
068        }
069        return requestValues;
070    }
071    
072    @Override
073    protected String getMappingName()
074    {
075        return "program";
076    }
077    
078    @Override
079    protected Map<String, Object> getAdditionalAttributeValues(String idValue, Content content, Map<String, Object> additionalParameters, boolean create, Logger logger)
080    {
081        Map<String, Object> result = super.getAdditionalAttributeValues(idValue, content, additionalParameters, create, logger);
082        
083        // Synchonize org unit and add it to the values
084        SynchronizableContentsCollection scc = _sccHelper.getSCCFromModelId(OrgUnitSynchronizableContentsCollection.MODEL_ID);
085        if (scc != null && scc instanceof ApogeeSynchronizableContentsCollection)
086        {
087            List<ModifiableContent> children = ((ApogeeSynchronizableContentsCollection) scc).importOrSynchronizeContents(Map.of("id_program", idValue), logger);
088            result.put(ProgramItem.ORG_UNITS_REFERENCES, children.toArray(new ModifiableContent[children.size()]));
089        }
090        
091        return result;
092    }
093    
094    @Override
095    protected Set<String> getClobColumns()
096    {
097        return Set.of("EXI_PRG", "ORG_ETUDE", "LNG_CRS_EXM", "CND_ACC");
098    }
099    
100    @Override
101    protected String getChildrenAttributeName()
102    {
103        return TraversableProgramPart.CHILD_PROGRAM_PARTS;
104    }
105}