Merge branch 'release/5.0.0'
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / FeatureTreeServiceImpl.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
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.persistence.dao.description.IFeatureNodeDao;
31 import eu.etaxonomy.cdm.persistence.dao.description.IFeatureTreeDao;
32 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
33
34 @Service
35 @Transactional(readOnly = false)
36 public class FeatureTreeServiceImpl extends IdentifiableServiceBase<FeatureTree, IFeatureTreeDao> implements IFeatureTreeService {
37
38 private IFeatureNodeDao featureNodeDao;
39
40 @Autowired
41 private IVocabularyService vocabularyService;
42
43 @Autowired
44 private IFeatureNodeService featureNodeService;
45
46 @Override
47 @Autowired
48 protected void setDao(IFeatureTreeDao dao) {
49 this.dao = dao;
50 }
51
52 @Autowired
53 protected void setFeatureNodeDao(IFeatureNodeDao featureNodeDao) {
54 this.featureNodeDao = featureNodeDao;
55 }
56
57
58 /* (non-Javadoc)
59 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
60 */
61 @Override
62 @Transactional(readOnly = false)
63 public void updateTitleCache(Class<? extends FeatureTree> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<FeatureTree> cacheStrategy, IProgressMonitor monitor) {
64 if (clazz == null){
65 clazz = FeatureTree.class;
66 }
67 super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
68 }
69
70
71 /* (non-Javadoc)
72 * @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#getFeatureNodesAll()
73 */
74 @Override
75 public List<FeatureNode> getFeatureNodesAll() {
76 return featureNodeDao.list();
77 }
78
79 /*
80 * (non-Javadoc)
81 * @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#saveFeatureNodesAll(java.util.Collection)
82 */
83 @Override
84 public Map<UUID, FeatureNode> saveFeatureNodesAll(Collection<FeatureNode> featureNodeCollection) {
85 return featureNodeDao.saveAll(featureNodeCollection);
86 }
87
88 /* (non-Javadoc)
89 * @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#saveOrUpdateFeatureNodesAll(java.util.Collection)
90 */
91 @Override
92 public Map<UUID, FeatureNode> saveOrUpdateFeatureNodesAll(Collection<FeatureNode> featureNodeCollection) {
93 return featureNodeDao.saveOrUpdateAll(featureNodeCollection);
94 }
95
96 /* (non-Javadoc)
97 * @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#loadWithNodes(java.util.UUID, java.util.List, java.util.List)
98 */
99 @Override
100 public FeatureTree loadWithNodes(UUID uuid, List<String> propertyPaths, List<String> nodePaths) {
101
102 if(!nodePaths.contains("children")) {
103 nodePaths.add("children");
104 }
105
106 List<String> rootPaths = new ArrayList<String>();
107 rootPaths.add("root");
108 for(String path : nodePaths) {
109 rootPaths.add("root." + path);
110 }
111
112 if(propertyPaths != null) {
113 rootPaths.addAll(propertyPaths);
114 }
115
116 FeatureTree featureTree = load(uuid, rootPaths);
117 if(featureTree == null){
118 throw new EntityNotFoundException("No FeatureTree entity found for " + uuid);
119 }
120 dao.deepLoadNodes(featureTree.getRoot().getChildNodes() ,nodePaths);
121 return featureTree;
122 }
123
124 /**
125 * Returns the featureTree specified by the given <code>uuid</code>.
126 * The specified featureTree either can be one of those stored in the CDM database or can be the
127 * DefaultFeatureTree (contains all Features in use).
128 * The uuid of the DefaultFeatureTree is defined in {@link IFeatureTreeService#DefaultFeatureTreeUuid}.
129 * The DefaultFeatureTree is also returned if no feature tree at all is stored in the cdm database.
130 *
131 * @see eu.etaxonomy.cdm.api.service.ServiceBase#load(java.util.UUID, java.util.List)
132 */
133 @Override
134 public FeatureTree load(UUID uuid, List<String> propertyPaths) {
135 return super.load(uuid, propertyPaths);
136 }
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#createTransientDefaultFeatureTree()
140 */
141 @Override
142 public FeatureTree createTransientDefaultFeatureTree() {
143 return load(IFeatureTreeDao.DefaultFeatureTreeUuid);
144 }
145
146 @Override
147 public DeleteResult delete(UUID featureTreeUuid){
148 DeleteResult result = new DeleteResult();
149 FeatureTree tree = dao.load(featureTreeUuid);
150
151 FeatureNode rootNode = HibernateProxyHelper.deproxy(tree.getRoot(), FeatureNode.class);
152 FeatureNodeDeletionConfigurator config = new FeatureNodeDeletionConfigurator();
153 config.setChildHandling(ChildHandling.DELETE);
154 result =featureNodeService.deleteFeatureNode(rootNode.getUuid(), config);
155 tree.setRoot(null);
156 if (result.isOk()){
157 dao.delete(tree);
158 result.addDeletedObject(tree);
159 }
160 return result;
161
162 }
163
164
165
166 }