Project

General

Profile

Download (5.34 KB) Statistics
| Branch: | Tag: | Revision:
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

    
10
package eu.etaxonomy.cdm.api.service;
11

    
12
import java.util.ArrayList;
13
import java.util.Collection;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.UUID;
17

    
18
import javax.persistence.EntityNotFoundException;
19

    
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.stereotype.Service;
22
import org.springframework.transaction.annotation.Transactional;
23

    
24
import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
25
import eu.etaxonomy.cdm.api.service.config.NodeDeletionConfigurator.ChildHandling;
26
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
27
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28
import eu.etaxonomy.cdm.model.description.FeatureNode;
29
import eu.etaxonomy.cdm.model.description.FeatureTree;
30
import eu.etaxonomy.cdm.model.term.TermType;
31
import eu.etaxonomy.cdm.persistence.dao.description.IFeatureNodeDao;
32
import eu.etaxonomy.cdm.persistence.dao.description.IFeatureTreeDao;
33
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
34
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
35

    
36
@Service
37
@Transactional(readOnly = false)
38
public class FeatureTreeServiceImpl extends IdentifiableServiceBase<FeatureTree, IFeatureTreeDao> implements IFeatureTreeService {
39

    
40
    private IFeatureNodeDao featureNodeDao;
41

    
42
    @Autowired
43
    private IFeatureNodeService featureNodeService;
44

    
45
    @Override
46
    @Autowired
47
    protected void setDao(IFeatureTreeDao dao) {
48
        this.dao = dao;
49
    }
50

    
51
    @Autowired
52
    protected void setFeatureNodeDao(IFeatureNodeDao featureNodeDao) {
53
        this.featureNodeDao = featureNodeDao;
54
    }
55

    
56
    @Override
57
    @Transactional(readOnly = false)
58
    public void updateCaches(Class<? extends FeatureTree> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<FeatureTree> cacheStrategy, IProgressMonitor monitor) {
59
        if (clazz == null){
60
            clazz = FeatureTree.class;
61
        }
62
        super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
63
    }
64

    
65
    @Override
66
    public List<FeatureNode> getFeatureNodesAll() {
67
        return featureNodeDao.list();
68
    }
69

    
70
    @Override
71
    public Map<UUID, FeatureNode> saveFeatureNodesAll(Collection<FeatureNode> featureNodeCollection) {
72
        return featureNodeDao.saveAll(featureNodeCollection);
73
    }
74

    
75
    @Override
76
    public Map<UUID, FeatureNode> saveOrUpdateFeatureNodesAll(Collection<FeatureNode> featureNodeCollection) {
77
        return featureNodeDao.saveOrUpdateAll(featureNodeCollection);
78
    }
79

    
80
    @Override
81
    public FeatureTree loadWithNodes(UUID uuid, List<String> propertyPaths, List<String> nodePaths) {
82

    
83
        if(!nodePaths.contains("children")) {
84
            nodePaths.add("children");
85
        }
86

    
87
        List<String> rootPaths = new ArrayList<String>();
88
        rootPaths.add("root");
89
        for(String path : nodePaths) {
90
            rootPaths.add("root." + path);
91
        }
92

    
93
        if(propertyPaths != null) {
94
            rootPaths.addAll(propertyPaths);
95
        }
96

    
97
        FeatureTree featureTree = load(uuid, rootPaths);
98
        if(featureTree == null){
99
            throw new EntityNotFoundException("No FeatureTree entity found for " + uuid);
100
        }
101
        dao.deepLoadNodes(featureTree.getRoot().getChildNodes() ,nodePaths);
102
        return featureTree;
103
    }
104

    
105
    /**
106
     * Returns the featureTree specified by the given <code>uuid</code>.
107
     * The specified featureTree either can be one of those stored in the CDM database or can be the
108
     * DefaultFeatureTree (contains all Features in use).
109
     * The uuid of the DefaultFeatureTree is defined in {@link IFeatureTreeService#DefaultFeatureTreeUuid}.
110
     * The DefaultFeatureTree is also returned if no feature tree at all is stored in the cdm database.
111
     *
112
     * @see eu.etaxonomy.cdm.api.service.ServiceBase#load(java.util.UUID, java.util.List)
113
     */
114
    @Override
115
    public FeatureTree load(UUID uuid, List<String> propertyPaths) {
116
        return super.load(uuid, propertyPaths);
117
    }
118

    
119
    @Override
120
    public FeatureTree createTransientDefaultFeatureTree() {
121
        return load(IFeatureTreeDao.DefaultFeatureTreeUuid);
122
    }
123

    
124
    @Override
125
    public DeleteResult delete(UUID featureTreeUuid){
126
        DeleteResult result = new DeleteResult();
127
        FeatureTree tree = dao.load(featureTreeUuid);
128

    
129
        FeatureNode rootNode = HibernateProxyHelper.deproxy(tree.getRoot());
130
        FeatureNodeDeletionConfigurator config = new FeatureNodeDeletionConfigurator();
131
        config.setChildHandling(ChildHandling.DELETE);
132
        result =featureNodeService.deleteFeatureNode(rootNode.getUuid(), config);
133
        tree.removeRootNode();
134
        if (result.isOk()){
135
          dao.delete(tree);
136
          result.addDeletedObject(tree);
137
        }
138
        return result;
139

    
140
    }
141

    
142
    @Override
143
    public <S extends FeatureTree> List<UuidAndTitleCache<S>> getUuidAndTitleCacheByTermType(Class<S> clazz, TermType termType, Integer limit,
144
            String pattern) {
145
        return dao.getUuidAndTitleCacheByTermType(clazz, termType, limit, pattern);
146
    }
147
}
(24-24/103)