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.content.external.ExternalizableMetadataHelper;
026import org.ametys.cms.repository.ModifiableDefaultContent;
027import org.ametys.odf.program.AbstractProgram;
028import org.ametys.odf.program.ProgramPart;
029import org.ametys.odf.program.TraversableProgramPart;
030import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
031import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsWithCatalogCollection;
032import org.ametys.plugins.odfsync.apogee.scc.ApogeeSynchronizableContentsCollection;
033import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
034
035/**
036 * SCC for program contents.
037 */
038public class ProgramSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsWithCatalogCollection
039{
040    /** SCC model id */
041    public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.program";
042    
043    @Override
044    protected List<Map<String, Object>> _search(Map<String, Object> searchParams, Logger logger)
045    {
046        String dataSourceId = getDataSourceId();
047        
048        List<Map<String, Object>> requestValues = _convertBigDecimal(_apogeeDAO.searchPrograms(dataSourceId, getParameterValues(), searchParams));
049        if (!searchParams.containsKey("__count") && (searchParams.containsKey("apogeeSyncCode") || searchParams.containsKey("parentCode") || searchParams.containsKey("isGlobalSync")))
050        {
051            for (Map<String, Object> values : requestValues)
052            {
053                String idValue = values.get(getIdColumn()).toString();
054                
055                Map<String, Object> parameters = new HashMap<>();
056                parameters.put(getIdField(), idValue);
057                
058                // Get the joint orgunits
059                values.putAll(_transformListOfMap2MapOfList(_apogeeDAO.getJointOrgUnits(dataSourceId, getParameterValues(), parameters)));
060                
061                // Get the modalities
062                values.putAll(_transformListOfMap2MapOfList(_apogeeDAO.getFormOfTeachingOrg(dataSourceId, getParameterValues(), parameters)));
063                
064                // Get the add elements
065                values.putAll(_transformListOfMap2MapOfList(_apogeeDAO.getAddElements(dataSourceId, getParameterValues(), parameters)));
066                
067                // Get the domains
068                values.putAll(_transformListOfMap2MapOfList(_apogeeDAO.getDomains(dataSourceId, getParameterValues(), parameters)));
069            }
070        }
071        return requestValues;
072    }
073    
074    @Override
075    protected String getMappingName()
076    {
077        return "program";
078    }
079    
080    @Override
081    protected boolean setAdditionalMetadata(ModifiableDefaultContent content, Map<String, List<Object>> remoteValues, boolean create, Logger logger)
082    {
083        boolean hasChanges = false;
084        
085        // Get the program synchronization code
086        ModifiableCompositeMetadata cm = content.getMetadataHolder();
087        String syncCode = cm.getString(getIdField());
088        
089        // Set orgUnit metadata
090        SynchronizableContentsCollection scc = _sccHelper.getSCCFromModelId(OrgUnitSynchronizableContentsCollection.MODEL_ID);
091        if (scc != null && scc instanceof ApogeeSynchronizableContentsCollection)
092        {
093            List<ModifiableDefaultContent> children = ((ApogeeSynchronizableContentsCollection) scc).importOrSynchronizeContents(Map.of("id_program", syncCode), logger);
094            Map<String, Object> params = Map.of("contentTypes", List.of(getContentType()));
095            boolean isSynchronizable = getLocalAndExternalFields(params).contains(AbstractProgram.ORG_UNITS_REFERENCES);
096            if (isSynchronizable)
097            {
098                ExternalizableMetadataHelper.setExternalMetadata(cm, AbstractProgram.ORG_UNITS_REFERENCES, children.toArray(new ModifiableDefaultContent[children.size()]), create);
099            }
100            else
101            {
102                ExternalizableMetadataHelper.setMetadata(cm, AbstractProgram.ORG_UNITS_REFERENCES, children.toArray(new ModifiableDefaultContent[children.size()]));
103            }
104            hasChanges = true;
105        }
106        
107        return hasChanges;
108    }
109    
110    @Override
111    protected Set<String> getClobColumns()
112    {
113        return Set.of("EXI_PRG", "ORG_ETUDE", "LNG_CRS_EXM", "CND_ACC");
114    }
115    
116    @Override
117    protected Set<String> getRichTextFields()
118    {
119        return Set.of("presentation", "teachingOrganization", "accessCondition");
120    }
121    
122    @Override
123    protected String getChildrenMetadataName()
124    {
125        return TraversableProgramPart.CHILD_PROGRAM_PARTS;
126    }
127    
128    @Override
129    protected String getChildrenInvertMetadataName()
130    {
131        return ProgramPart.PARENT_PROGRAM_PARTS;
132    }
133}