diff --git a/src/main/java/org/olat/modules/taxonomy/TaxonomyService.java b/src/main/java/org/olat/modules/taxonomy/TaxonomyService.java index 95b54777a8e51d7ac4d211d8acd8fb612e642f94..32dc8776c2b11c4c2b25bd3f7bde79a39ac27b84 100644 --- a/src/main/java/org/olat/modules/taxonomy/TaxonomyService.java +++ b/src/main/java/org/olat/modules/taxonomy/TaxonomyService.java @@ -70,12 +70,6 @@ public interface TaxonomyService { */ public boolean canDeleteTaxonomyLevel(TaxonomyLevelRef taxonomyLevel); - /** - * - * @return true if the level can be deleted - */ - public boolean deleteTaxonomyLevel(TaxonomyLevelRef taxonomyLevel, TaxonomyLevelRef mergeTo); - /** * The operation move the following elements from the source taxonomy * level to the target taxonomy level: @@ -87,10 +81,10 @@ public interface TaxonomyService { * </ul> * * @param taxonomyLevel The taxonomy level source - * @param mergeTo The taxonomy level target - * @return true if the source was deleted after merging in the target level + * @param mergeTo The taxonomy level target (optional) + * @return true if the level can be deleted */ - public boolean mergeTaxonomyLevel(TaxonomyLevelRef taxonomyLevel, TaxonomyLevelRef mergeTo); + public boolean deleteTaxonomyLevel(TaxonomyLevelRef taxonomyLevel, TaxonomyLevelRef mergeTo); /** * @param ref The root taxonomy (optional) diff --git a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyCompetenceDAO.java b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyCompetenceDAO.java index 5ee06b52b973cdd08d40a180e17604f0fad040fb..f703667a9bb1f85966f81ee6acc799c610680437 100644 --- a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyCompetenceDAO.java +++ b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyCompetenceDAO.java @@ -290,6 +290,21 @@ public class TaxonomyCompetenceDAO { .executeUpdate(); } + /** + * Delete all competences hold by the specified taxonomy + * level. + * + * @param level A taxonomy level + * @return Number of deleted rows + */ + public int deleteCompetences(TaxonomyLevelRef level) { + String q = "delete from ctaxonomycompetence as competence where competence.taxonomyLevel.key=:levelKey"; + return dbInstance.getCurrentEntityManager() + .createQuery(q) + .setParameter("levelKey", level.getKey()) + .executeUpdate(); + } + public void deleteCompetence(TaxonomyCompetence competence) { TaxonomyCompetence reloadedCompetence = dbInstance.getCurrentEntityManager() .getReference(TaxonomyCompetenceImpl.class, competence.getKey()); diff --git a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyLevelDAO.java b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyLevelDAO.java index b09c7ffc597c516e9f69d841dbfff3af1bbf4483..f94e8254ddb222d41500396557eeba15c9547bea 100644 --- a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyLevelDAO.java +++ b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyLevelDAO.java @@ -370,7 +370,7 @@ public class TaxonomyLevelDAO implements InitializingBean { } public boolean canDelete(TaxonomyLevelRef taxonomyLevel) { - if(!hasChildren(taxonomyLevel) && !hasItemUsing(taxonomyLevel) &&!hasCompetenceUsing(taxonomyLevel)) { + if(!hasChildren(taxonomyLevel) && !hasItemUsing(taxonomyLevel) && !hasCompetenceUsing(taxonomyLevel)) { return true; } return false; diff --git a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyRelationsDAO.java b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyRelationsDAO.java index be71516b8f8d48f07503dcff64f5ad59e03da9ce..84fab93f0fd671b35b1c5eeb81f49a591b4b1ad0 100644 --- a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyRelationsDAO.java +++ b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyRelationsDAO.java @@ -24,7 +24,6 @@ import java.util.stream.Collectors; import org.olat.core.commons.persistence.DB; import org.olat.modules.qpool.model.QItemDocument; -import org.olat.modules.taxonomy.TaxonomyLevel; import org.olat.modules.taxonomy.TaxonomyLevelRef; import org.olat.search.service.indexer.LifeFullIndexer; import org.springframework.beans.factory.annotation.Autowired; @@ -63,7 +62,7 @@ public class TaxonomyRelationsDAO { return counts != null && counts.size() == 1 && counts.get(0) != null ? counts.get(0).intValue() : 0; } - public int replaceQuestionItem(TaxonomyLevel source, TaxonomyLevel target) { + public int replaceQuestionItem(TaxonomyLevelRef source, TaxonomyLevelRef target) { List<Long> questionItemKeys = getQuestionItemKeys(source); int row; @@ -85,12 +84,32 @@ public class TaxonomyRelationsDAO { return row; } - private List<Long> getQuestionItemKeys(TaxonomyLevel level) { + public int removeFromQuestionItems(TaxonomyLevelRef level) { + List<Long> questionItemKeys = getQuestionItemKeys(level); + + int row; + if(questionItemKeys.isEmpty()) { + row = 0; + } else { + String q = "update questionitem item set item.taxonomyLevel.key=null where item.taxonomyLevel.key=:levelKey"; + row = dbInstance.getCurrentEntityManager() + .createQuery(q) + .setParameter("levelKey", level.getKey()) + .executeUpdate(); + dbInstance.commit(); + for(Long questionItemKey:questionItemKeys) { + lifeIndexer.indexDocument(QItemDocument.TYPE, questionItemKey); + } + } + + return row; + } + + private List<Long> getQuestionItemKeys(TaxonomyLevelRef level) { String q = "select item.key from questionitem item where item.taxonomyLevel.key=:levelKey"; return dbInstance.getCurrentEntityManager() .createQuery(q, Long.class) .setParameter("levelKey", level.getKey()) .getResultList(); } - } diff --git a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyServiceImpl.java b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyServiceImpl.java index febaf2b4922c47d30e0602f1eacca4c6177e896a..624cd9be6da723f14294c7e5ccfe9b1d81ba44e1 100644 --- a/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyServiceImpl.java +++ b/src/main/java/org/olat/modules/taxonomy/manager/TaxonomyServiceImpl.java @@ -166,18 +166,14 @@ public class TaxonomyServiceImpl implements TaxonomyService { dir += "_" + taxonomyLevel.getKey(); VFSContainer lastStorage = lostAndFound.createChildContainer(dir); VFSManager.copyContent(library, lastStorage); + //delete the competences + taxonomyCompetenceDao.deleteCompetences(taxonomyLevel); + //questions + taxonomyRelationsDao.removeFromQuestionItems(taxonomyLevel); } return taxonomyLevelDao.delete(reloadedTaxonomyLevel); } - - @Override - public boolean mergeTaxonomyLevel(TaxonomyLevelRef taxonomyLevel, TaxonomyLevelRef mergeTo) { - TaxonomyLevel reloadedTaxonomyLevel = taxonomyLevelDao.loadByKey(taxonomyLevel.getKey()); - TaxonomyLevel reloadedMergeTo = taxonomyLevelDao.loadByKey(mergeTo.getKey()); - merge(reloadedTaxonomyLevel, reloadedMergeTo); - return taxonomyLevelDao.delete(reloadedTaxonomyLevel); - } private void merge(TaxonomyLevel taxonomyLevel, TaxonomyLevel mergeTo) { //documents diff --git a/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyLevelOverviewController.java b/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyLevelOverviewController.java index 6d23df2c99a2264055c3c20b8879177a470d3657..2abbda811b173d599a3fd9b753c4109427958ebe 100644 --- a/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyLevelOverviewController.java +++ b/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyLevelOverviewController.java @@ -209,19 +209,19 @@ public class TaxonomyLevelOverviewController extends BasicController implements private void doConfirmDelete(UserRequest ureq) { taxonomyLevel = taxonomyService.getTaxonomyLevel(taxonomyLevel); - Taxonomy taxonomy = taxonomyLevel.getTaxonomy(); + if(TaxonomyLevelManagedFlag.isManaged(taxonomyLevel.getManagedFlags(), TaxonomyLevelManagedFlag.delete)) { + showWarning("warning.atleastone.level"); + return; + } - if(taxonomyService.canDeleteTaxonomyLevel(taxonomyLevel)) { - confirmDeleteCtrl = new DeleteTaxonomyLevelController(ureq, getWindowControl(), null, taxonomy); - listenTo(confirmDeleteCtrl); + Taxonomy taxonomy = taxonomyLevel.getTaxonomy(); + confirmDeleteCtrl = new DeleteTaxonomyLevelController(ureq, getWindowControl(), null, taxonomy); + listenTo(confirmDeleteCtrl); - String title = translate("confirmation.delete.level.title"); - cmc = new CloseableModalController(getWindowControl(), "close", moveLevelCtrl.getInitialComponent(), true, title); - listenTo(cmc); - cmc.activate(); - } else { - showWarning("warning.delete.level"); - } + String title = translate("confirmation.delete.level.title"); + cmc = new CloseableModalController(getWindowControl(), "close", moveLevelCtrl.getInitialComponent(), true, title); + listenTo(cmc); + cmc.activate(); } private void doMove(UserRequest ureq) { diff --git a/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyTreeTableController.java b/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyTreeTableController.java index de0e0b10990ff4259be9887d04fec143e0936a56..f8645146e2f557ac626b66c5dedbff6f96e85631 100644 --- a/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyTreeTableController.java +++ b/src/main/java/org/olat/modules/taxonomy/ui/TaxonomyTreeTableController.java @@ -430,7 +430,7 @@ public class TaxonomyTreeTableController extends FormBasicController implements } if(levelsToDelete.isEmpty()) { - showWarning("warning.delete.level"); + showWarning("warning.atleastone.level"); } else { confirmDeleteCtrl = new DeleteTaxonomyLevelController(ureq, getWindowControl(), levelsToDelete, taxonomy); listenTo(confirmDeleteCtrl); @@ -443,19 +443,20 @@ public class TaxonomyTreeTableController extends FormBasicController implements } private void doConfirmDelete(UserRequest ureq, TaxonomyLevelRow row) { - if(taxonomyService.canDeleteTaxonomyLevel(row)) { - TaxonomyLevel taxonomyLevel = taxonomyService.getTaxonomyLevel(row); - List<TaxonomyLevel> levelToDelete = Collections.singletonList(taxonomyLevel); - confirmDeleteCtrl = new DeleteTaxonomyLevelController(ureq, getWindowControl(), levelToDelete, taxonomy); - listenTo(confirmDeleteCtrl); - - String title = translate("confirmation.delete.level.title"); - cmc = new CloseableModalController(getWindowControl(), "close", confirmDeleteCtrl.getInitialComponent(), true, title); - listenTo(cmc); - cmc.activate(); - } else { - showWarning("warning.delete.level"); + if(TaxonomyLevelManagedFlag.isManaged(row.getManagedFlags(), TaxonomyLevelManagedFlag.delete)) { + showWarning("warning.atleastone.level"); + return; } + + TaxonomyLevel taxonomyLevel = taxonomyService.getTaxonomyLevel(row); + List<TaxonomyLevel> levelToDelete = Collections.singletonList(taxonomyLevel); + confirmDeleteCtrl = new DeleteTaxonomyLevelController(ureq, getWindowControl(), levelToDelete, taxonomy); + listenTo(confirmDeleteCtrl); + + String title = translate("confirmation.delete.level.title"); + cmc = new CloseableModalController(getWindowControl(), "close", confirmDeleteCtrl.getInitialComponent(), true, title); + listenTo(cmc); + cmc.activate(); } private void doMove(UserRequest ureq) { diff --git a/src/main/java/org/olat/modules/taxonomy/ui/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/modules/taxonomy/ui/_i18n/LocalStrings_de.properties index 2466d58d20f7bb7cfb426f1842347871ae158a19..66bb6dbbb99cd9b41188c797bc4d3229a345bd0d 100644 --- a/src/main/java/org/olat/modules/taxonomy/ui/_i18n/LocalStrings_de.properties +++ b/src/main/java/org/olat/modules/taxonomy/ui/_i18n/LocalStrings_de.properties @@ -130,5 +130,5 @@ teach=Dozieren tools=$\:table.header.actions type.taxonomy.level=Ebenentyp zuweisen warning.atleastone.level=Sie m\u00FCssen mindestens eine Ebene w\u00E4hlen dass Sie editieren d\u00FCrfen. -warning.delete.level=Der Ebene darf nicht gel\u00F6scht werden. Er ist noch benutzt. +warning.delete.level=Die Ebene darf nicht gel\u00F6scht werden. Sie wird noch benutzt. warning.delete.level.type=Der Ebenentype wurde nicht gel\u00F6scht. Er ist noch benutzt.