-// $Id$\r
-/**\r
-* Copyright (C) 2009 EDIT\r
-* European Distributed Institute of Taxonomy\r
-* http://www.e-taxonomy.eu\r
-*\r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/\r
-package eu.etaxonomy.cdm.api.service;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import java.util.UUID;\r
-\r
-import org.springframework.beans.factory.annotation.Autowired;\r
-import org.springframework.stereotype.Service;\r
-import org.springframework.transaction.annotation.Transactional;\r
-\r
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;\r
-import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;\r
-import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyNodeDao;\r
-\r
-/**\r
- * @author a.kohlbecker\r
- * @date 24.03.2011\r
- *\r
- */\r
-@Service\r
-@Transactional(readOnly = false)\r
-public class PolytomousKeyNodeServiceImpl extends VersionableServiceBase<PolytomousKeyNode, IPolytomousKeyNodeDao> implements IPolytomousKeyNodeService {\r
-\r
-\r
- @Override\r
- @Autowired\r
- protected void setDao(IPolytomousKeyNodeDao dao) {\r
- this.dao = dao;\r
- }\r
-\r
- @Override\r
- public DeleteResult delete(UUID nodeUUID, boolean deleteChildren){\r
- DeleteResult result = new DeleteResult();\r
- PolytomousKeyNode node = dao.findByUuid(nodeUUID);\r
- node = (PolytomousKeyNode)HibernateProxyHelper.deproxy(node);\r
- if(node == null) {\r
- return null;\r
- }\r
- List<PolytomousKeyNode> children = new ArrayList<PolytomousKeyNode>();\r
- for (PolytomousKeyNode child: node.getChildren()){\r
- children.add(child);\r
- }\r
- PolytomousKeyNode parent = node.getParent();\r
- parent = HibernateProxyHelper.deproxy(parent, PolytomousKeyNode.class);\r
-\r
- if(!deleteChildren){\r
-\r
- for (PolytomousKeyNode child: children){\r
- if (!child.equals(node)){\r
- parent.addChild(child);\r
- dao.update(child);\r
- result.addUpdatedObject(child);\r
- }\r
-\r
- }\r
-\r
-\r
- dao.update(node);\r
- result.addUpdatedObject(node);\r
- }\r
- if (parent!= null){\r
- parent.removeChild(node);\r
- dao.update(parent);\r
- }\r
- if (node.getKey().getRoot().equals(node)){\r
- node.getKey().setRoot(null);\r
- }\r
- if (node.getTaxon() != null){\r
- node.removeTaxon();\r
- }\r
- if (dao.delete(node) == null){\r
- result.setAbort();\r
- }\r
- dao.saveOrUpdate(parent);\r
- result.addUpdatedObject(parent);\r
- return result;\r
-\r
- }\r
-\r
-}\r
+/**
+* Copyright (C) 2009 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.api.service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.description.PolytomousKey;
+import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
+import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyDao;
+import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyNodeDao;
+
+/**
+ * @author a.kohlbecker
+ * @since 24.03.2011
+ */
+@Service
+@Transactional(readOnly = false)
+public class PolytomousKeyNodeServiceImpl
+ extends VersionableServiceBase<PolytomousKeyNode, IPolytomousKeyNodeDao>
+ implements IPolytomousKeyNodeService {
+
+ @Override
+ @Autowired
+ protected void setDao(IPolytomousKeyNodeDao dao) {
+ this.dao = dao;
+ }
+
+ @Autowired
+ protected IPolytomousKeyDao keyDao;
+
+ @Override
+ public DeleteResult delete(UUID nodeUUID, boolean deleteChildren){
+ DeleteResult result = new DeleteResult();
+ PolytomousKeyNode node = dao.findByUuid(nodeUUID);
+ node = HibernateProxyHelper.deproxy(node);
+ if (node == null) {
+ // result.addException(new Exception("The polytomouskey node was already deleted."));
+ return result;
+ }
+ List<PolytomousKeyNode> children = new ArrayList<>();
+
+ for (PolytomousKeyNode child: node.getChildren()){
+ children.add(child);
+ }
+ PolytomousKeyNode parent = node.getParent();
+ parent = HibernateProxyHelper.deproxy(parent);
+ PolytomousKey key = null;
+ if (parent == null){
+ key = node.getKey();
+ key = HibernateProxyHelper.deproxy(key);
+ }
+
+ if(!deleteChildren){
+ for (PolytomousKeyNode child: children){
+ if (!child.equals(node)){
+ parent.addChild(child);
+ dao.saveOrUpdate(child);
+ result.addUpdatedObject(child);
+ }
+ }
+
+ dao.saveOrUpdate(node);
+ result.addUpdatedObject(node);
+ }
+ if (parent!= null){
+ parent.removeChild(node);
+ dao.saveOrUpdate(parent);
+ }
+ if (node.getKey() != null && key != null){
+ key.setRoot(null);
+ }
+ if (node.getTaxon() != null){
+ node.removeTaxon();
+ }
+ if (dao.delete(node) == null){
+ result.setAbort();
+ }else{
+ result.addDeletedObject(node);
+ }
+ if (parent != null){
+ dao.saveOrUpdate(parent);
+ result.addUpdatedObject(parent);
+ } else{
+ keyDao.saveOrUpdate(key);
+ result.addUpdatedObject(key);
+ }
+
+ return result;
+ }
+}
\ No newline at end of file