+ @Override
+ public DeleteResult isDeletable(UUID termUuid, DeleteConfiguratorBase config){
+ TermDeletionConfigurator termConfig = null;
+ if(config instanceof TermDeletionConfigurator){
+ termConfig = (TermDeletionConfigurator) config;
+ }
+ DeleteResult result = new DeleteResult();
+ DefinedTermBase term = load(termUuid);
+ Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(term);
+
+ if(termConfig!=null){
+ //generalization of
+ Set<DefinedTermBase> specificTerms = term.getGeneralizationOf();
+ if (!specificTerms.isEmpty() && termConfig.isDeleteGeneralizationOfRelations()){
+ references.removeAll(specificTerms);
+ }
+ //kind of
+ DefinedTermBase generalTerm = term.getKindOf();
+ if (generalTerm != null && termConfig.isDeleteKindOfRelations()){
+ references.remove(generalTerm);
+ }
+ //part of
+ DefinedTermBase parentTerm = term.getPartOf();
+ if (parentTerm != null && termConfig.isDeletePartOfRelations()){
+ references.remove(parentTerm);
+ }
+ //included in
+ Set<DefinedTermBase> includedTerms = term.getIncludes();
+ if (!includedTerms.isEmpty() && termConfig.isDeleteIncludedRelations()){
+ references.removeAll(includedTerms);
+ }
+ }
+
+ //gather remaining referenced objects
+ for (CdmBase relatedObject : references) {
+ if(relatedObject instanceof TermVocabulary){
+ continue;
+ }
+ result.getRelatedObjects().add(relatedObject);
+ String message = "An object of " + relatedObject.getClass().getName() + " with ID " + relatedObject.getId() + " is referencing the object" ;
+ result.addException(new ReferencedObjectUndeletableException(message));
+ result.setAbort();
+ }
+ return result;
+ }
+
+ @Override
+ @Transactional(readOnly = false)
+ public Map<UUID, Representation> saveOrUpdateRepresentations(Collection<Representation> representations){
+ return representationDao.saveOrUpdateAll(representations);
+ }
+
+ @Override
+ @Transactional(readOnly = true)
+ public List<UuidAndTitleCache<NamedArea>> getUuidAndTitleCache(List<TermVocabulary> vocs, Integer limit, String pattern, Language lang) {
+ List<NamedArea> areas = dao.getUuidAndTitleCache(vocs, limit, pattern);
+
+ List<UuidAndTitleCache<NamedArea>> result = new ArrayList();
+ UuidAndTitleCache<NamedArea> uuidAndTitleCache;
+ for (NamedArea area: areas){
+ uuidAndTitleCache = new UuidAndTitleCache<>(area.getUuid(), area.getId(), area.labelWithLevel(area, lang));
+ result.add(uuidAndTitleCache);
+ }
+
+ return result;
+ }
+
+ @Override
+ public Collection<TermDto> getIncludesAsDto(
+ TermDto parentTerm) {
+ return dao.getIncludesAsDto(parentTerm);
+ }
+
+ @Override
+ public Collection<TermDto> getKindOfsAsDto(
+ TermDto parentTerm) {
+ return dao.getKindOfsAsDto(parentTerm);
+ }
+
+ @Transactional(readOnly = false)
+ @Override
+ public void moveTerm(TermDto termDto, UUID parentUUID) {
+ moveTerm(termDto, parentUUID, null);
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Transactional(readOnly = false)
+ @Override
+ public void moveTerm(TermDto termDto, UUID parentUuid, TermMovePosition termMovePosition) {
+ boolean isKindOf = termDto.getKindOfUuid()!=null && termDto.getKindOfUuid().equals(parentUuid);
+ TermVocabulary vocabulary = HibernateProxyHelper.deproxy(vocabularyService.load(termDto.getVocabularyUuid()));
+ DefinedTermBase parent = HibernateProxyHelper.deproxy(dao.load(parentUuid));
+ if(parent==null){
+ //new parent is a vocabulary
+ TermVocabulary parentVocabulary = HibernateProxyHelper.deproxy(vocabularyService.load(parentUuid));
+ DefinedTermBase term = HibernateProxyHelper.deproxy(dao.load(termDto.getUuid()));
+ if(parentVocabulary!=null){
+ term.setKindOf(null);
+ term.setPartOf(null);
+
+ vocabulary.removeTerm(term);
+ parentVocabulary.addTerm(term);
+ }
+ vocabularyService.saveOrUpdate(parentVocabulary);
+ }
+ else {
+ DefinedTermBase term = HibernateProxyHelper.deproxy(dao.load(termDto.getUuid()));
+ //new parent is a term
+ if(parent.isInstanceOf(OrderedTermBase.class)
+ && term.isInstanceOf(OrderedTermBase.class)
+ && termMovePosition!=null
+ && HibernateProxyHelper.deproxy(parent, OrderedTermBase.class).getVocabulary().isInstanceOf(OrderedTermVocabulary.class)) {
+ //new parent is an ordered term
+ OrderedTermBase orderedTerm = HibernateProxyHelper.deproxy(term, OrderedTermBase.class);
+ OrderedTermBase targetOrderedDefinedTerm = HibernateProxyHelper.deproxy(parent, OrderedTermBase.class);
+ OrderedTermVocabulary otVoc = HibernateProxyHelper.deproxy(targetOrderedDefinedTerm.getVocabulary(), OrderedTermVocabulary.class);
+ if(termMovePosition.equals(TermMovePosition.BEFORE)) {
+ orderedTerm.getVocabulary().removeTerm(orderedTerm);
+ otVoc.addTermAbove(orderedTerm, targetOrderedDefinedTerm);
+ if (targetOrderedDefinedTerm.getPartOf() != null){
+ targetOrderedDefinedTerm.getPartOf().addIncludes(orderedTerm);
+ }
+ }
+ else if(termMovePosition.equals(TermMovePosition.AFTER)) {
+ orderedTerm.getVocabulary().removeTerm(orderedTerm);
+ otVoc.addTermBelow(orderedTerm, targetOrderedDefinedTerm);
+ if (targetOrderedDefinedTerm.getPartOf() != null){
+ targetOrderedDefinedTerm.getPartOf().addIncludes(orderedTerm);
+ }
+ }
+ else if(termMovePosition.equals(TermMovePosition.ON)) {
+ orderedTerm.getVocabulary().removeTerm(orderedTerm);
+ targetOrderedDefinedTerm.addIncludes(orderedTerm);
+ targetOrderedDefinedTerm.getVocabulary().addTerm(orderedTerm);
+ }
+ }
+ else{
+ vocabulary.removeTerm(term);
+ if(isKindOf){
+ parent.addGeneralizationOf(term);
+ }
+ else{
+ parent.addIncludes(term);
+ }
+ parent.getVocabulary().addTerm(term);
+ }
+ vocabularyService.saveOrUpdate(parent.getVocabulary());
+ }
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Transactional(readOnly = false)
+ @Override
+ public TermDto addNewTerm(TermType termType, UUID parentUUID, boolean isKindOf) {
+ DefinedTermBase term = termType.getEmptyDefinedTermBase();
+ dao.save(term);
+ DefinedTermBase parent = dao.load(parentUUID);
+ if(isKindOf){
+ parent.addGeneralizationOf(term);
+ }
+ else{
+ parent.addIncludes(term);
+ }
+ parent.getVocabulary().addTerm(term);
+ dao.saveOrUpdate(parent);
+ return TermDto.fromTerm(term, true);
+ }
+
+ public enum TermMovePosition{
+ BEFORE,
+ AFTER,
+ ON
+ }
+