Merge branch 'develop' of ssh://dev.e-taxonomy.eu/var/git/cdmlib into develop
[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.HHH_9751_Util;
20 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
21 import eu.etaxonomy.cdm.model.description.PolytomousKey;
22 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
23 import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyDao;
24 import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyNodeDao;
25
26 /**
27 * @author a.kohlbecker
28 * @since 24.03.2011
29 *
30 */
31 @Service
32 @Transactional(readOnly = false)
33 public class PolytomousKeyNodeServiceImpl extends VersionableServiceBase<PolytomousKeyNode, IPolytomousKeyNodeDao> implements IPolytomousKeyNodeService {
34
35
36 @Override
37 @Autowired
38 protected void setDao(IPolytomousKeyNodeDao dao) {
39 this.dao = dao;
40 }
41
42 @Autowired
43 protected IPolytomousKeyDao keyDao;
44
45 @Override
46 public DeleteResult delete(UUID nodeUUID, boolean deleteChildren){
47 DeleteResult result = new DeleteResult();
48 PolytomousKeyNode node = dao.findByUuid(nodeUUID);
49 node = HibernateProxyHelper.deproxy(node);
50 if(node == null) {
51 // result.addException(new Exception("The polytomouskey node was already deleted."));;
52 return result;
53 }
54 List<PolytomousKeyNode> children = new ArrayList<PolytomousKeyNode>();
55
56 node.removeNullValueFromChildren();
57 for (PolytomousKeyNode child: node.getChildren()){
58 children.add(child);
59 }
60 PolytomousKeyNode parent = node.getParent();
61 parent = HibernateProxyHelper.deproxy(parent, PolytomousKeyNode.class);
62 PolytomousKey key = null;
63 if (parent == null){
64 key = node.getKey();
65 key = HibernateProxyHelper.deproxy(key, PolytomousKey.class);
66 }
67
68 if(!deleteChildren){
69
70 for (PolytomousKeyNode child: children){
71 if (!child.equals(node)){
72 parent.addChild(child);
73 dao.saveOrUpdate(child);
74 result.addUpdatedObject(child);
75 }
76
77 }
78
79
80 dao.saveOrUpdate(node);
81 result.addUpdatedObject(node);
82 }
83 if (parent!= null){
84 if (parent.getChildren().contains(null)){
85 List<PolytomousKeyNode> parentChildren = parent.getChildren();
86 HHH_9751_Util.removeAllNull(parentChildren);
87 }
88 parent.removeChild(node);
89 dao.saveOrUpdate(parent);
90 }
91 if (node.getKey() != null && key != null){
92 key.setRoot(null);
93 }
94 if (node.getTaxon() != null){
95 node.removeTaxon();
96 }
97 if (dao.delete(node) == null){
98 result.setAbort();
99 }else{
100 result.addDeletedObject(node);
101 }
102 if (parent != null){
103 dao.saveOrUpdate(parent);
104 result.addUpdatedObject(parent);
105 } else{
106 keyDao.saveOrUpdate(key);
107 result.addUpdatedObject(key);
108 }
109
110 return result;
111
112 }
113
114 }