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.Optional; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.slf4j.Logger; 026 027import org.ametys.cms.ObservationConstants; 028import org.ametys.cms.content.external.ExternalizableMetadataHelper; 029import org.ametys.cms.repository.Content; 030import org.ametys.cms.repository.ModifiableDefaultContent; 031import org.ametys.core.observation.Event; 032import org.ametys.core.user.population.UserPopulationDAO; 033import org.ametys.odf.course.Course; 034import org.ametys.odf.course.ShareableCourseHelper; 035import org.ametys.odf.courselist.CourseList; 036import org.ametys.odf.coursepart.CoursePart; 037import org.ametys.odf.coursepart.CoursePartFactory; 038import org.ametys.odf.enumeration.OdfReferenceTableEntry; 039import org.ametys.odf.enumeration.OdfReferenceTableHelper; 040import org.ametys.plugins.odfsync.apogee.scc.AbstractApogeeSynchronizableContentsWithCatalogCollection; 041import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata; 042 043/** 044 * SCC for course contents. 045 */ 046public class CourseSynchronizableContentsCollection extends AbstractApogeeSynchronizableContentsWithCatalogCollection 047{ 048 /** SCC model id */ 049 public static final String MODEL_ID = "org.ametys.plugins.odfsync.apogee.scc.course"; 050 051 /** The ODF reference table helper */ 052 protected OdfReferenceTableHelper _odfRefTableHelper; 053 054 /** The shareable course helper */ 055 protected ShareableCourseHelper _shareableCourseHelper; 056 057 @Override 058 public void service(ServiceManager manager) throws ServiceException 059 { 060 super.service(manager); 061 _odfRefTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE); 062 _shareableCourseHelper = (ShareableCourseHelper) manager.lookup(ShareableCourseHelper.ROLE); 063 } 064 065 @Override 066 protected List<Map<String, Object>> _search(Map<String, Object> searchParams, Logger logger) 067 { 068 return _convertBigDecimal(_apogeeDAO.searchCourses(getDataSourceId(), getParameterValues(), searchParams)); 069 } 070 071 @Override 072 protected String getMappingName() 073 { 074 return "course"; 075 } 076 077 @Override 078 protected boolean additionalImportOperations(ModifiableDefaultContent content, Map<String, List<Object>> remoteValues, Map<String, Object> importParams, Logger logger) 079 { 080 boolean hasChanges = super.additionalImportOperations(content, remoteValues, importParams, logger); 081 082 Object parentId = importParams != null && importParams.containsKey("parentId") ? importParams.get("parentId") : null; 083 ModifiableDefaultContent parentContent = parentId != null ? _resolver.resolveById(parentId.toString()) : null; 084 085 hasChanges = _shareableCourseHelper.initializeShareableFields((Course) content, (CourseList) parentContent, UserPopulationDAO.SYSTEM_USER_IDENTITY, true) || hasChanges; 086 087 return hasChanges; 088 } 089 090 @Override 091 protected boolean handleParent(ModifiableDefaultContent currentContent, ModifiableDefaultContent parentContent, Logger logger) 092 { 093 boolean hasChanges = false; 094 095 if (parentContent instanceof CourseList) 096 { 097 ModifiableCompositeMetadata cm = currentContent.getMetadataHolder(); 098 hasChanges = ExternalizableMetadataHelper.setMetadata(cm, Course.PARENT_COURSE_LISTS, new Content[] {parentContent}); 099 hasChanges = _updateRelation(parentContent.getMetadataHolder(), CourseList.CHILD_COURSES, currentContent) || hasChanges; 100 } 101 102 return hasChanges; 103 } 104 105 @Override 106 protected boolean handleChildren(ModifiableDefaultContent content, boolean create, Logger logger) 107 { 108 boolean hasChanges = false; 109 hasChanges = importCourseParts(content, logger) || hasChanges; 110 hasChanges = super.handleChildren(content, create, logger) || hasChanges; 111 return hasChanges; 112 } 113 114 @Override 115 protected String getChildrenSCCModelId() 116 { 117 return CourseListSynchronizableContentsCollection.MODEL_ID; 118 } 119 120 @Override 121 protected String getChildrenMetadataName() 122 { 123 return Course.CHILD_COURSE_LISTS; 124 } 125 126 @Override 127 protected String getChildrenInvertMetadataName() 128 { 129 return CourseList.PARENT_COURSES; 130 } 131 132 /** 133 * Set the course parts of the current {@link Course}. 134 * @param content Current content 135 * @param logger The logger 136 * @return <code>true</code> if there are changes 137 */ 138 protected boolean importCourseParts(ModifiableDefaultContent content, Logger logger) 139 { 140 boolean hasChanges = false; 141 142 // Delete old relations to course parts 143 for (CoursePart oldCoursePart : ((Course) content).getCourseParts()) 144 { 145 if (_synchroComponent.updateRelation(oldCoursePart.getMetadataHolder(), CoursePart.PARENT_COURSES, content, true)) 146 { 147 oldCoursePart.saveChanges(); 148 } 149 } 150 hasChanges = ExternalizableMetadataHelper.removeMetadataIfExists(content.getMetadataHolder(), Course.CHILD_COURSE_PARTS); 151 152 // Add the course parts to the current course 153 ModifiableCompositeMetadata cm = content.getMetadataHolder(); 154 String idValue = cm.getString(getIdField()); 155 Map<String, Object> searchParams = putIdParameter(idValue); 156 157 String prefixTitle = content.getTitle() + " - "; 158 for (Map<String, Object> coursePartData : _apogeeDAO.getCourseParts(getDataSourceId(), getParameterValues(), searchParams)) 159 { 160 String typeHeure = coursePartData.get("COD_TYP_HEU").toString(); 161 String nature = Optional.ofNullable(_odfRefTableHelper.getItemFromCode(OdfReferenceTableHelper.ENSEIGNEMENT_NATURE, typeHeure)) 162 .map(OdfReferenceTableEntry::getId) 163 .orElse(null); 164 if (nature == null) 165 { 166 logger.warn("The nature '{}' is unknown for the course '{}'.", typeHeure, content.getTitle()); 167 } 168 else 169 { 170 // Create the course part 171 String coursePartTitle = prefixTitle + typeHeure; 172 ModifiableDefaultContent coursePart = createContentAction(CoursePartFactory.COURSE_PART_CONTENT_TYPE, "course-part", 1, content.getLanguage(), coursePartTitle, logger); 173 if (coursePart != null) 174 { 175 ModifiableCompositeMetadata coursePartCM = coursePart.getMetadataHolder(); 176 coursePartCM.setMetadata("nature", nature); 177 coursePartCM.setMetadata("nbHours", (Double) _convertBigDecimal(CoursePartFactory.COURSE_PART_CONTENT_TYPE, "nbHours", coursePartData.get("NBR_HEU_ELP"))); 178 coursePartCM.setMetadata("courseHolder", content.getId()); 179 coursePartCM.setMetadata("courses", new String[] {content.getId()}); 180 String defaultCatalog = _catalogsManager.getDefaultCatalogName(); 181 if (defaultCatalog != null) 182 { 183 coursePartCM.setMetadata(CoursePart.CATALOG, defaultCatalog); 184 } 185 186 coursePart.saveChanges(); 187 coursePart.checkpoint(); 188 189 // Notify a content was imported 190 Map<String, Object> eventParams = new HashMap<>(); 191 eventParams.put(ObservationConstants.ARGS_CONTENT, coursePart); 192 eventParams.put(ObservationConstants.ARGS_CONTENT_ID, coursePart.getId()); 193 _observationManager.notify(new Event(org.ametys.plugins.contentio.synchronize.observation.ObservationConstants.EVENT_CONTENT_SYNCHRONIZED, _currentUserProvider.getUser(), eventParams)); 194 195 // Add the course part to the course 196 hasChanges = _updateRelation(cm, Course.CHILD_COURSE_PARTS, coursePart) || hasChanges; 197 } 198 } 199 } 200 201 return hasChanges; 202 } 203}