Merge branch 'release/5.44.0'
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / PolytomousKeyNodeServiceImpl.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.api.service;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.UUID;
14
15 import org.springframework.beans.factory.annotation.Autowired;
16 import org.springframework.stereotype.Service;
17 import org.springframework.transaction.annotation.Transactional;
18
19 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20 import eu.etaxonomy.cdm.model.description.PolytomousKey;
21 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
22 import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyDao;
23 import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyNodeDao;
24
25 /**
26 * @author a.kohlbecker
27 * @since 24.03.2011
28 */
29 @Service
30 @Transactional(readOnly = false)
31 public class PolytomousKeyNodeServiceImpl
32 extends VersionableServiceBase<PolytomousKeyNode, IPolytomousKeyNodeDao>
33 implements IPolytomousKeyNodeService {
34
35 @Override
36 @Autowired
37 protected void setDao(IPolytomousKeyNodeDao dao) {
38 this.dao = dao;
39 }
40
41 @Autowired
42 protected IPolytomousKeyDao keyDao;
43
44 @Override
45 public DeleteResult delete(UUID nodeUUID, boolean deleteChildren){
46 DeleteResult result = new DeleteResult();
47 PolytomousKeyNode node = dao.findByUuid(nodeUUID);
48 node = HibernateProxyHelper.deproxy(node);
49 if (node == null) {
50 // result.addException(new Exception("The polytomouskey node was already deleted."));
51 return result;
52 }
53 List<PolytomousKeyNode> children = new ArrayList<>();
54
55 for (PolytomousKeyNode child: node.getChildren()){
56 children.add(child);
57 }
58 PolytomousKeyNode parent = node.getParent();
59 parent = HibernateProxyHelper.deproxy(parent);
60 PolytomousKey key = null;
61 if (parent == null){
62 key = node.getKey();
63 key = HibernateProxyHelper.deproxy(key);
64 }
65
66 if(!deleteChildren){
67 for (PolytomousKeyNode child: children){
68 if (!child.equals(node)){
69 parent.addChild(child);
70 dao.saveOrUpdate(child);
71 result.addUpdatedObject(child);
72 }
73 }
74
75 dao.saveOrUpdate(node);
76 result.addUpdatedObject(node);
77 }
78 if (parent!= null){
79 parent.removeChild(node);
80 dao.saveOrUpdate(parent);
81 }
82 if (node.getKey() != null && key != null){
83 key.setRoot(null);
84 }
85 if (node.getTaxon() != null){
86 node.removeTaxon();
87 }
88 if (dao.delete(node) == null){
89 result.setAbort();
90 }else{
91 result.addDeletedObject(node);
92 }
93 if (parent != null){
94 dao.saveOrUpdate(parent);
95 result.addUpdatedObject(parent);
96 } else{
97 keyDao.saveOrUpdate(key);
98 result.addUpdatedObject(key);
99 }
100
101 return result;
102 }
103 }