001/* 002 * Copyright 2019 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.odf.migration; 017 018import java.util.HashSet; 019import java.util.Map; 020import java.util.Set; 021 022import org.apache.commons.lang3.StringUtils; 023import org.apache.commons.lang3.tuple.Pair; 024 025import org.ametys.cms.data.ContentDataHelper; 026import org.ametys.cms.repository.Content; 027import org.ametys.cms.repository.ModifiableContent; 028import org.ametys.odf.enumeration.OdfReferenceTableEntry; 029import org.ametys.odf.enumeration.OdfReferenceTableHelper; 030import org.ametys.plugins.repository.AmetysRepositoryException; 031 032import com.opensymphony.workflow.WorkflowException; 033 034/** 035 * Component class to migrate nature enseignement 036 */ 037public class MigrateNatureEnseignementComponent extends MigrateCoursePartComponent 038{ 039 /** The component role. */ 040 @SuppressWarnings("hiding") 041 public static final String ROLE = MigrateNatureEnseignementComponent.class.getName(); 042 043 /** 044 * Migrate the nature enseignement 045 * @throws AmetysRepositoryException if an error occurs 046 * @throws WorkflowException if an error occurs 047 */ 048 public void migrateNaturesEnseignement() throws AmetysRepositoryException, WorkflowException 049 { 050 migrateNaturesEnseignement(null); 051 } 052 053 /** 054 * Migrate the nature enseignement 055 * @param natureEnseignementCategories the map of nature enseignement categories 056 * @throws AmetysRepositoryException if an error occurs 057 * @throws WorkflowException if an error occurs 058 */ 059 public void migrateNaturesEnseignement(Map<String, Pair<String, Long>> natureEnseignementCategories) throws AmetysRepositoryException, WorkflowException 060 { 061 // Migrate categories and get the minimum order to set 062 Pair<Long, Set<String>> orderAndCategories = _getOrderAndCategories(natureEnseignementCategories); 063 Long order = orderAndCategories.getLeft(); 064 Set<String> categories = orderAndCategories.getRight(); 065 066 // Check for each nature if the category has been set with an old value and create a category if needed 067 for (OdfReferenceTableEntry nature : _odfRefTableHelper.getItems(OdfReferenceTableHelper.ENSEIGNEMENT_NATURE)) 068 { 069 String category = ContentDataHelper.getContentIdFromContentData(nature.getContent(), "category"); 070 if (StringUtils.isNotEmpty(category) && !categories.contains(category)) 071 { 072 // Order always incremented, the perfect way would be following numbers but it complexify the source code 073 order++; 074 OdfReferenceTableEntry categoryEntry = _getOrCreateNatureEnseignement(category, category, order); 075 076 // Set attribute category with category entry value 077 Content natureContent = nature.getContent(); 078 if (natureContent instanceof ModifiableContent) 079 { 080 ModifiableContent modifNatureContent = (ModifiableContent) natureContent; 081 modifNatureContent.setValue("category", categoryEntry.getId()); 082 modifNatureContent.saveChanges(); 083 } 084 } 085 } 086 } 087 088 private Pair<Long, Set<String>> _getOrderAndCategories(Map<String, Pair<String, Long>> natureEnseignementCategories) throws AmetysRepositoryException, WorkflowException 089 { 090 // Create CM, TD, TP categories if it doesn't exist 091 _createNatureEnseignementCategories(natureEnseignementCategories); 092 093 Long order = 1L; 094 Set<String> categories = new HashSet<>(); 095 096 for (OdfReferenceTableEntry category : _odfRefTableHelper.getItems(OdfReferenceTableHelper.ENSEIGNEMENT_NATURE_CATEGORY)) 097 { 098 Long categoryOrder = category.getContent().getValue("order", false, 0L); 099 if (categoryOrder > order) 100 { 101 order = categoryOrder; 102 } 103 categories.add(category.getId()); 104 } 105 return Pair.of(order, categories); 106 } 107}