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(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.METADATA_PARENT_COURSE_LISTS, new Content[] {parentContent}); 099 hasChanges = _updateRelation(parentContent.getMetadataHolder(), CourseList.METADATA_CHILD_COURSES, currentContent) || hasChanges; 100 } 101 102 return hasChanges; 103 } 104 105 @Override 106 protected boolean handleChildren(ModifiableDefaultContent content, Logger logger) 107 { 108 boolean hasChanges = false; 109 hasChanges = importCourseParts(content, logger) || hasChanges; 110 hasChanges = importOrSynchronizeChildren(content, CourseListSynchronizableContentsCollection.MODEL_ID, Course.METADATA_CHILD_COURSE_LISTS, CourseList.METADATA_PARENT_COURSES, logger) || hasChanges; 111 return hasChanges; 112 } 113 114 /** 115 * Set the course parts of the current {@link Course}. 116 * @param content Current content 117 * @param logger The logger 118 * @return <code>true</code> if there are changes 119 */ 120 protected boolean importCourseParts(ModifiableDefaultContent content, Logger logger) 121 { 122 boolean hasChanges = false; 123 124 // Delete old relations to course parts 125 for (CoursePart oldCoursePart : ((Course) content).getCourseParts()) 126 { 127 if (_synchroComponent.updateRelation(oldCoursePart.getMetadataHolder(), CoursePart.METADATA_PARENT_COURSES, content, true)) 128 { 129 oldCoursePart.saveChanges(); 130 } 131 } 132 hasChanges = ExternalizableMetadataHelper.removeMetadataIfExists(content.getMetadataHolder(), Course.METADATA_CHILD_COURSE_PARTS); 133 134 // Add the course parts to the current course 135 ModifiableCompositeMetadata cm = content.getMetadataHolder(); 136 String idValue = cm.getString(getIdField()); 137 Map<String, Object> searchParams = putIdParameter(idValue); 138 139 String prefixTitle = content.getTitle() + " - "; 140 for (Map<String, Object> coursePartData : _apogeeDAO.getCourseParts(searchParams)) 141 { 142 String typeHeure = coursePartData.get("COD_TYP_HEU").toString(); 143 String nature = Optional.ofNullable(_odfRefTableHelper.getItemFromCode(OdfReferenceTableHelper.ENSEIGNEMENT_NATURE, typeHeure)) 144 .map(OdfReferenceTableEntry::getId) 145 .orElse(null); 146 if (nature == null) 147 { 148 logger.warn("The nature '{}' is unknown for the course '{}'.", typeHeure, content.getTitle()); 149 } 150 else 151 { 152 // Create the course part 153 String coursePartTitle = prefixTitle + typeHeure; 154 ModifiableDefaultContent coursePart = createContentAction(CoursePartFactory.COURSE_PART_CONTENT_TYPE, "course-part", 1, content.getLanguage(), coursePartTitle, logger); 155 if (coursePart != null) 156 { 157 ModifiableCompositeMetadata coursePartCM = coursePart.getMetadataHolder(); 158 coursePartCM.setMetadata("nature", nature); 159 coursePartCM.setMetadata("nbHours", (Double) _convertBigDecimal(CoursePartFactory.COURSE_PART_CONTENT_TYPE, "nbHours", coursePartData.get("NBR_HEU_ELP"))); 160 coursePartCM.setMetadata("courseHolder", content.getId()); 161 coursePartCM.setMetadata("courses", new String[] {content.getId()}); 162 String defaultCatalog = _catalogsManager.getDefaultCatalogName(); 163 if (defaultCatalog != null) 164 { 165 coursePartCM.setMetadata(CoursePart.METADATA_CATALOG, defaultCatalog); 166 } 167 168 coursePart.saveChanges(); 169 coursePart.checkpoint(); 170 171 // Notify a content was imported 172 Map<String, Object> eventParams = new HashMap<>(); 173 eventParams.put(ObservationConstants.ARGS_CONTENT, coursePart); 174 eventParams.put(ObservationConstants.ARGS_CONTENT_ID, coursePart.getId()); 175 _observationManager.notify(new Event(org.ametys.plugins.contentio.synchronize.observation.ObservationConstants.EVENT_CONTENT_SYNCHRONIZED, _currentUserProvider.getUser(), eventParams)); 176 177 // Add the course part to the course 178 hasChanges = _updateRelation(cm, Course.METADATA_CHILD_COURSE_PARTS, coursePart) || hasChanges; 179 } 180 } 181 } 182 183 return hasChanges; 184 } 185}