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