Project

General

Profile

Download (2.79 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.stereotype.Service;
18
import org.springframework.transaction.annotation.Transactional;
19

    
20
import eu.etaxonomy.cdm.hibernate.HHH_9751_Util;
21
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
23
import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyNodeDao;
24

    
25
/**
26
 * @author a.kohlbecker
27
 * @date 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
	@Override
42
	public DeleteResult delete(UUID nodeUUID, boolean deleteChildren){
43
        DeleteResult result = new DeleteResult();
44
        PolytomousKeyNode node = dao.findByUuid(nodeUUID);
45
        node = HibernateProxyHelper.deproxy(node);
46
        if(node == null) {
47
            return null;
48
        }
49
        List<PolytomousKeyNode> children = new ArrayList<PolytomousKeyNode>();
50
        for (PolytomousKeyNode child: node.getChildren()){
51
            children.add(child);
52
        }
53
        PolytomousKeyNode parent = node.getParent();
54
        parent = HibernateProxyHelper.deproxy(parent, PolytomousKeyNode.class);
55

    
56
        if(!deleteChildren){
57

    
58
            for (PolytomousKeyNode child: children){
59
                if (!child.equals(node)){
60
                    parent.addChild(child);
61
                    dao.saveOrUpdate(child);
62
                    result.addUpdatedObject(child);
63
                }
64

    
65
            }
66

    
67

    
68
            dao.saveOrUpdate(node);
69
            result.addUpdatedObject(node);
70
        }
71
        if (parent!= null){
72
            if (parent.getChildren().contains(null)){
73
                List<PolytomousKeyNode> parentChildren = parent.getChildren();
74
                HHH_9751_Util.removeAllNull(parentChildren);
75
            }
76
            parent.removeChild(node);
77
            dao.saveOrUpdate(parent);
78
        }
79
        if (node.getKey().getRoot().equals(node)){
80
            node.getKey().setRoot(null);
81
        }
82
        if (node.getTaxon() != null){
83
            node.removeTaxon();
84
        }
85
        if (dao.delete(node) == null){
86
            result.setAbort();
87
        }
88
        dao.saveOrUpdate(parent);
89
        result.addUpdatedObject(parent);
90
        return result;
91

    
92
    }
93

    
94
}
(79-79/97)