Project

General

Profile

Download (2.8 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.HibernateProxyHelper;
21
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
22
import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyNodeDao;
23

    
24
/**
25
 * @author a.kohlbecker
26
 * @date 24.03.2011
27
 *
28
 */
29
@Service
30
@Transactional(readOnly = false)
31
public class PolytomousKeyNodeServiceImpl  extends VersionableServiceBase<PolytomousKeyNode, IPolytomousKeyNodeDao> implements IPolytomousKeyNodeService {
32

    
33

    
34
	@Override
35
    @Autowired
36
	protected void setDao(IPolytomousKeyNodeDao dao) {
37
		this.dao = dao;
38
	}
39

    
40
	@Override
41
	public DeleteResult delete(UUID nodeUUID, boolean deleteChildren){
42
        DeleteResult result = new DeleteResult();
43
        PolytomousKeyNode node = dao.findByUuid(nodeUUID);
44
        node = HibernateProxyHelper.deproxy(node);
45
        if(node == null) {
46
            return null;
47
        }
48
        List<PolytomousKeyNode> children = new ArrayList<PolytomousKeyNode>();
49
        for (PolytomousKeyNode child: node.getChildren()){
50
            children.add(child);
51
        }
52
        PolytomousKeyNode parent = node.getParent();
53
        parent = HibernateProxyHelper.deproxy(parent, PolytomousKeyNode.class);
54

    
55
        if(!deleteChildren){
56

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

    
64
            }
65

    
66

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

    
93
    }
94

    
95
}
(79-79/97)