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.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.commons.collections4.CollectionUtils;
024import org.slf4j.Logger;
025
026import org.ametys.cms.content.external.ExternalizableMetadataHelper;
027import org.ametys.cms.repository.ModifiableDefaultContent;
028import org.ametys.odf.program.AbstractProgram;
029import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
030import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsWithCatalogCollection;
031import org.ametys.plugins.odfsync.apogee.scc.ApogeeSynchronizableContentsCollection;
032import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
033
034import com.google.common.collect.ImmutableMap;
035import com.google.common.collect.ImmutableSet;
036
037/**
038 * SCC for program contents.
039 */
040public class ProgramSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsWithCatalogCollection
041{
042    /** SCC model id */
043    public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.program";
044    
045    @Override
046    protected List<Map<String, Object>> _search(Map<String, Object> searchParams, Logger logger)
047    {
048        List<Map<String, Object>> requestValues = _convertBigDecimal(_apogeeDAO.searchPrograms(searchParams));
049        if (!searchParams.containsKey("__count"))
050        {
051            for (Map<String, Object> values : requestValues)
052            {
053                String idValue = values.get(getIdColumn()).toString();
054                Map<String, Object> parameters = ImmutableMap.of(getIdField(), idValue);
055                
056                // Get the joint orgunits
057                List<Object> orgUnitCodes = new ArrayList<>();
058                List<Map<String, Object>> jointOrgUnitsValues = _apogeeDAO.getJointOrgUnits(parameters);
059                for (Map<String, Object> jointOrgUnitValues : jointOrgUnitsValues)
060                {
061                    orgUnitCodes.add(jointOrgUnitValues.get("COD_ETB"));
062                }
063                values.put("COD_ETB", orgUnitCodes);
064                
065                // Get the modalities
066                List<Object> formofteachingOrgCodes = new ArrayList<>();
067                List<Map<String, Object>> formofteachingOrgValues = _apogeeDAO.getFormOfTeachingOrg(parameters);
068                for (Map<String, Object> formofteachingOrgValue : formofteachingOrgValues)
069                {
070                    formofteachingOrgCodes.add(formofteachingOrgValue.get("COD_RGI"));
071                }
072                values.put("COD_RGI", formofteachingOrgCodes);
073
074                // Get the add elements
075                List<Object> presentation = new ArrayList<>();
076                List<Object> teachingOrganization = new ArrayList<>();
077                List<Object> accessCondition = new ArrayList<>();
078                List<Object> educationLanguage = new ArrayList<>();
079                List<Map<String, Object>> addElements = _apogeeDAO.getAddElements(parameters);
080                for (Map<String, Object> addElement : addElements)
081                {
082                    CollectionUtils.addIgnoreNull(presentation, _transformClobToString(addElement.get("EXI_PRG"), idValue, logger));
083                    CollectionUtils.addIgnoreNull(teachingOrganization, _transformClobToString(addElement.get("ORG_ETUDE"), idValue, logger));
084                    CollectionUtils.addIgnoreNull(accessCondition, _transformClobToString(addElement.get("LNG_CRS_EXM"), idValue, logger));
085                    CollectionUtils.addIgnoreNull(educationLanguage, _transformClobToString(addElement.get("CND_ACC"), idValue, logger));
086                }
087                values.put("EXI_PRG", presentation);
088                values.put("ORG_ETUDE", teachingOrganization);
089                values.put("LNG_CRS_EXM", accessCondition);
090                values.put("CND_ACC", educationLanguage);
091
092                // FIXME ODF-1240 Get the domains
093            }
094        }
095        return requestValues;
096    }
097
098    @Override
099    protected String getMappingName()
100    {
101        return "program";
102    }
103    
104    @Override
105    protected boolean setAdditionalMetadata(ModifiableDefaultContent content, Map<String, List<Object>> remoteValues, Logger logger)
106    {
107        boolean hasChanges = false;
108        
109        // Get the program synchronization code
110        ModifiableCompositeMetadata cm = content.getMetadataHolder();
111        String syncCode = cm.getString(getIdField());
112        
113        // Set orgUnit metadata
114        SynchronizableContentsCollection scc = _sccHelper.getSCCFromModelId(OrgUnitSynchronizableContentsCollection.MODEL_ID);
115        if (scc != null && scc instanceof ApogeeSynchronizableContentsCollection)
116        {
117            List<ModifiableDefaultContent> children = ((ApogeeSynchronizableContentsCollection) scc).importOrSynchronizeContents(ImmutableMap.of("id_program", syncCode), logger);
118            ExternalizableMetadataHelper.setMetadata(cm, AbstractProgram.ORG_UNITS_REFERENCES, children.toArray(new ModifiableDefaultContent[children.size()]));
119            hasChanges = true;
120        }
121        
122        return hasChanges;
123    }
124    
125    @Override
126    protected Set<String> getRichTextFields()
127    {
128        return ImmutableSet.of("presentation", "teachingOrganization", "accessCondition");
129    }
130}