cleanup and javadoc
[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 */
30 @Service
31 @Transactional(readOnly = false)
32 public class PolytomousKeyNodeServiceImpl extends VersionableServiceBase<PolytomousKeyNode, IPolytomousKeyNodeDao> implements IPolytomousKeyNodeService {
33
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
68 for (PolytomousKeyNode child: children){
69 if (!child.equals(node)){
70 parent.addChild(child);
71 dao.saveOrUpdate(child);
72 result.addUpdatedObject(child);
73 }
74
75 }
76
77
78 dao.saveOrUpdate(node);
79 result.addUpdatedObject(node);
80 }
81 if (parent!= null){
82 parent.removeChild(node);
83 dao.saveOrUpdate(parent);
84 }
85 if (node.getKey() != null && key != null){
86 key.setRoot(null);
87 }
88 if (node.getTaxon() != null){
89 node.removeTaxon();
90 }
91 if (dao.delete(node) == null){
92 result.setAbort();
93 }else{
94 result.addDeletedObject(node);
95 }
96 if (parent != null){
97 dao.saveOrUpdate(parent);
98 result.addUpdatedObject(parent);
99 } else{
100 keyDao.saveOrUpdate(key);
101 result.addUpdatedObject(key);
102 }
103
104 return result;
105
106 }
107
108 }