Merge branch 'hotfix/3.11.1'
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / PolytomousKeyServiceImpl.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.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.api.service.pager.Pager;
21 import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
22 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
23 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
24 import eu.etaxonomy.cdm.model.description.PolytomousKey;
25 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
26 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27 import eu.etaxonomy.cdm.persistence.dao.description.IIdentificationKeyDao;
28 import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyDao;
29 import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyNodeDao;
30 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
31 import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
32
33 @Service
34 @Transactional(readOnly = false)
35 public class PolytomousKeyServiceImpl extends IdentifiableServiceBase<PolytomousKey, IPolytomousKeyDao> implements IPolytomousKeyService {
36
37 private IIdentificationKeyDao identificationKeyDao;
38 private ITaxonDao taxonDao;
39 private IPolytomousKeyNodeDao nodeDao;
40
41
42 @Override
43 @Autowired
44 protected void setDao(IPolytomousKeyDao dao) {
45 this.dao = dao;
46 }
47
48 @Autowired
49 protected void setDao(IIdentificationKeyDao identificationKeyDao) {
50 this.identificationKeyDao = identificationKeyDao;
51 }
52
53 @Autowired
54 protected void setDao(ITaxonDao taxonDao) {
55 this.taxonDao = taxonDao;
56 }
57
58 @Autowired
59 protected void setDao(IPolytomousKeyNodeDao nodeDao) {
60 this.nodeDao = nodeDao;
61 }
62
63
64 /* (non-Javadoc)
65 * @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
66 */
67 @Override
68 public void updateTitleCache(Class<? extends PolytomousKey> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<PolytomousKey> cacheStrategy, IProgressMonitor monitor) {
69 if (clazz == null){
70 clazz = PolytomousKey.class;
71 }
72 super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
73 }
74
75
76 /* (non-Javadoc)
77 * @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#loadWithNodes(java.util.UUID, java.util.List, java.util.List)
78 */
79 @Override
80 public PolytomousKey loadWithNodes(UUID uuid, List<String> propertyPaths, List<String> nodePaths) {
81
82 if(nodePaths == null){
83 nodePaths = new ArrayList<String>();
84 }
85 nodePaths.add("children");
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 PolytomousKey polytomousKey = load(uuid, rootPaths);
98 dao.loadNodes(polytomousKey.getRoot(),nodePaths);
99 return polytomousKey;
100 }
101
102 /**
103 * Returns the polytomous key specified by the given <code>uuid</code>.
104 *
105 * @see eu.etaxonomy.cdm.api.service.ServiceBase#load(java.util.UUID, java.util.List)
106 */
107 @Override
108 public PolytomousKey load(UUID uuid, List<String> propertyPaths) {
109 return super.load(uuid, propertyPaths);
110 }
111
112
113 /* (non-Javadoc)
114 * @see eu.etaxonomy.cdm.api.service.IPolytomousKeyService#findByTaxonomicScope(eu.etaxonomy.cdm.model.taxon.TaxonBase, java.lang.Class, java.lang.Integer, java.lang.Integer, java.util.List)
115 */
116 @Override
117 public Pager<PolytomousKey> findByTaxonomicScope(
118 TaxonBase taxon, Integer pageSize,
119 Integer pageNumber, List<String> propertyPaths, List<String> nodePaths) {
120
121 List<PolytomousKey> list = new ArrayList<PolytomousKey>();
122 Long numberOfResults = identificationKeyDao.countByTaxonomicScope(taxon.getUuid(), PolytomousKey.class);
123 if(AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)){
124 list = identificationKeyDao.findByTaxonomicScope(taxon.getUuid(), PolytomousKey.class, pageSize, pageNumber, propertyPaths);
125 }
126 if (nodePaths != null) {
127 for (PolytomousKey polytomousKey : list) {
128 dao.loadNodes(polytomousKey.getRoot(), nodePaths);
129 }
130 }
131 Pager<PolytomousKey> pager = new DefaultPagerImpl<PolytomousKey>(pageNumber, numberOfResults.intValue(), pageSize, list);
132
133 return pager;
134 }
135
136 @Override
137 public UpdateResult updateAllNodeNumberings(UUID polytomousKeyUuid) {
138 UpdateResult result = new UpdateResult();
139 PolytomousKey polytomousKey = dao.load(polytomousKeyUuid);
140 polytomousKey.getRoot().refreshNodeNumbering();
141 return result;
142 }
143
144 @Override
145 public UpdateResult updateAllNodeNumberings() {
146 UpdateResult result = new UpdateResult();
147 List<PolytomousKey> polytomousKeys = dao.list();
148 for(PolytomousKey polytomousKey : polytomousKeys) {
149 polytomousKey.getRoot().refreshNodeNumbering();
150 }
151 return result;
152 }
153
154 @Override
155 public DeleteResult delete(PolytomousKey key){
156 DeleteResult result = new DeleteResult();
157 PolytomousKeyNode root = key.getRoot();
158 key.setRoot(null);
159 try{
160 nodeDao.delete(root);
161 }catch(Exception e){
162 result.addException(e);
163 result.setAbort();
164 return result;
165 }
166 try{
167 dao.delete(key);
168 }catch(Exception e){
169 result.addException(e);
170 result.setAbort();
171 return result;
172 }
173 return result;
174 }
175
176 }