001/* 002 * Copyright 2018 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; 017 018import java.util.HashSet; 019import java.util.Set; 020 021import org.apache.commons.lang3.StringUtils; 022import org.apache.commons.lang3.tuple.Pair; 023 024import org.ametys.cms.repository.Content; 025import org.ametys.cms.repository.ModifiableContent; 026import org.ametys.odf.enumeration.OdfReferenceTableEntry; 027import org.ametys.odf.enumeration.OdfReferenceTableHelper; 028import org.ametys.plugins.repository.AmetysRepositoryException; 029 030import com.opensymphony.workflow.WorkflowException; 031 032/** 033 * Initialization class to migrate totalDurationOf* metadata. 034 */ 035public class MigrateNatureEnseignement extends MigrateCoursePart 036{ 037 @Override 038 public void init() throws Exception 039 { 040 _migrateNaturesEnseignement(); 041 } 042 043 private void _migrateNaturesEnseignement() throws AmetysRepositoryException, WorkflowException 044 { 045 // Migrate categories and get the minimum order to set 046 Pair<Long, Set<String>> orderAndCategories = _getOrderAndCategories(); 047 Long order = orderAndCategories.getLeft(); 048 Set<String> categories = orderAndCategories.getRight(); 049 050 // Check for each nature if the category has been set with an old value and create a category if needed 051 for (OdfReferenceTableEntry nature : _odfRefTableHelper.getItems(OdfReferenceTableHelper.ENSEIGNEMENT_NATURE)) 052 { 053 String category = nature.getContent().getMetadataHolder().getString("category", StringUtils.EMPTY); 054 if (StringUtils.isNotEmpty(category) && !categories.contains(category)) 055 { 056 // Order always incremented, the perfect way would be following numbers but it complexify the source code 057 order++; 058 OdfReferenceTableEntry categoryEntry = _getOrCreateNatureEnseignement(category, category, order); 059 060 // Set metadata category with category entry value 061 Content natureContent = nature.getContent(); 062 if (natureContent instanceof ModifiableContent) 063 { 064 ModifiableContent modifNatureContent = (ModifiableContent) natureContent; 065 modifNatureContent.getMetadataHolder().setMetadata("category", categoryEntry.getId()); 066 modifNatureContent.saveChanges(); 067 } 068 } 069 } 070 } 071 072 private Pair<Long, Set<String>> _getOrderAndCategories() throws AmetysRepositoryException, WorkflowException 073 { 074 // Create CM, TD, TP categories if it doesn't exist 075 _createNatureEnseignementCategories(); 076 077 Long order = 1L; 078 Set<String> categories = new HashSet<>(); 079 080 for (OdfReferenceTableEntry category : _odfRefTableHelper.getItems(OdfReferenceTableHelper.ENSEIGNEMENT_NATURE_CATEGORY)) 081 { 082 Long categoryOrder = category.getContent().getMetadataHolder().getLong("order", 0L); 083 if (categoryOrder > order) 084 { 085 order = categoryOrder; 086 } 087 categories.add(category.getId()); 088 } 089 return Pair.of(order, categories); 090 } 091}