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.Iterator;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.stereotype.Service;
|
20
|
import org.springframework.transaction.annotation.Transactional;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
|
23
|
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
|
24
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
25
|
import eu.etaxonomy.cdm.api.service.pager.impl.AbstractPagerImpl;
|
26
|
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
|
27
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
28
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
29
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
30
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
31
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
32
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
33
|
import eu.etaxonomy.cdm.persistence.dao.description.IIdentificationKeyDao;
|
34
|
import eu.etaxonomy.cdm.persistence.dao.description.IPolytomousKeyDao;
|
35
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
36
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
37
|
|
38
|
@Service
|
39
|
@Transactional(readOnly = false)
|
40
|
public class PolytomousKeyServiceImpl extends IdentifiableServiceBase<PolytomousKey, IPolytomousKeyDao> implements IPolytomousKeyService {
|
41
|
|
42
|
private IIdentificationKeyDao identificationKeyDao;
|
43
|
private ITaxonDao taxonDao;
|
44
|
@Autowired
|
45
|
private IPolytomousKeyNodeService nodeService;
|
46
|
|
47
|
|
48
|
@Override
|
49
|
@Autowired
|
50
|
protected void setDao(IPolytomousKeyDao dao) {
|
51
|
this.dao = dao;
|
52
|
}
|
53
|
|
54
|
@Autowired
|
55
|
protected void setDao(IIdentificationKeyDao identificationKeyDao) {
|
56
|
this.identificationKeyDao = identificationKeyDao;
|
57
|
}
|
58
|
|
59
|
@Autowired
|
60
|
protected void setDao(ITaxonDao taxonDao) {
|
61
|
this.taxonDao = taxonDao;
|
62
|
}
|
63
|
|
64
|
|
65
|
|
66
|
|
67
|
/* (non-Javadoc)
|
68
|
* @see eu.etaxonomy.cdm.api.service.IIdentifiableEntityService#updateTitleCache(java.lang.Integer, eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy)
|
69
|
*/
|
70
|
@Override
|
71
|
public void updateTitleCache(Class<? extends PolytomousKey> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<PolytomousKey> cacheStrategy, IProgressMonitor monitor) {
|
72
|
if (clazz == null){
|
73
|
clazz = PolytomousKey.class;
|
74
|
}
|
75
|
super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
|
76
|
}
|
77
|
|
78
|
|
79
|
/* (non-Javadoc)
|
80
|
* @see eu.etaxonomy.cdm.api.service.IFeatureTreeService#loadWithNodes(java.util.UUID, java.util.List, java.util.List)
|
81
|
*/
|
82
|
@Override
|
83
|
public PolytomousKey loadWithNodes(UUID uuid, List<String> propertyPaths, List<String> nodePaths) {
|
84
|
|
85
|
if(nodePaths == null){
|
86
|
nodePaths = new ArrayList<String>();
|
87
|
}
|
88
|
nodePaths.add("children");
|
89
|
|
90
|
List<String> rootPaths = new ArrayList<String>();
|
91
|
rootPaths.add("root");
|
92
|
for(String path : nodePaths) {
|
93
|
rootPaths.add("root." + path);
|
94
|
}
|
95
|
|
96
|
if(propertyPaths != null) {
|
97
|
rootPaths.addAll(propertyPaths);
|
98
|
}
|
99
|
|
100
|
PolytomousKey polytomousKey = load(uuid, rootPaths);
|
101
|
dao.loadNodes(polytomousKey.getRoot(),nodePaths);
|
102
|
return polytomousKey;
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Returns the polytomous key specified by the given <code>uuid</code>.
|
107
|
*
|
108
|
* @see eu.etaxonomy.cdm.api.service.ServiceBase#load(java.util.UUID, java.util.List)
|
109
|
*/
|
110
|
@Override
|
111
|
public PolytomousKey load(UUID uuid, List<String> propertyPaths) {
|
112
|
return super.load(uuid, propertyPaths);
|
113
|
}
|
114
|
|
115
|
|
116
|
/* (non-Javadoc)
|
117
|
* @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)
|
118
|
*/
|
119
|
@Override
|
120
|
public Pager<PolytomousKey> findByTaxonomicScope(
|
121
|
TaxonBase taxon, Integer pageSize,
|
122
|
Integer pageNumber, List<String> propertyPaths, List<String> nodePaths) {
|
123
|
|
124
|
List<PolytomousKey> list = new ArrayList<PolytomousKey>();
|
125
|
Long numberOfResults = identificationKeyDao.countByTaxonomicScope(taxon.getUuid(), PolytomousKey.class);
|
126
|
if(AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)){
|
127
|
list = identificationKeyDao.findByTaxonomicScope(taxon.getUuid(), PolytomousKey.class, pageSize, pageNumber, propertyPaths);
|
128
|
}
|
129
|
if (nodePaths != null) {
|
130
|
for (PolytomousKey polytomousKey : list) {
|
131
|
dao.loadNodes(polytomousKey.getRoot(), nodePaths);
|
132
|
}
|
133
|
}
|
134
|
Pager<PolytomousKey> pager = new DefaultPagerImpl<PolytomousKey>(pageNumber, numberOfResults, pageSize, list);
|
135
|
|
136
|
return pager;
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public UpdateResult updateAllNodeNumberings(UUID polytomousKeyUuid) {
|
141
|
UpdateResult result = new UpdateResult();
|
142
|
PolytomousKey polytomousKey = dao.load(polytomousKeyUuid);
|
143
|
polytomousKey.getRoot().refreshNodeNumbering();
|
144
|
return result;
|
145
|
}
|
146
|
|
147
|
@Override
|
148
|
public UpdateResult updateAllNodeNumberings() {
|
149
|
UpdateResult result = new UpdateResult();
|
150
|
List<PolytomousKey> polytomousKeys = dao.list();
|
151
|
for(PolytomousKey polytomousKey : polytomousKeys) {
|
152
|
polytomousKey.getRoot().refreshNodeNumbering();
|
153
|
}
|
154
|
return result;
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public DeleteResult delete(PolytomousKey key){
|
159
|
//DeleteResult result = new DeleteResult();
|
160
|
PolytomousKeyNode root = key.getRoot();
|
161
|
|
162
|
|
163
|
DeleteResult result = isDeletable(key.getUuid(), null);
|
164
|
DeleteResult resultRoot = new DeleteResult();
|
165
|
if (result.isOk()){
|
166
|
try{
|
167
|
if (root != null){
|
168
|
// root.setKey(null);
|
169
|
resultRoot = nodeService.delete(root.getUuid(), true);
|
170
|
|
171
|
}
|
172
|
}catch(Exception e){
|
173
|
result.addException(e);
|
174
|
result.setAbort();
|
175
|
return result;
|
176
|
}
|
177
|
try{
|
178
|
if (resultRoot.isOk()){
|
179
|
dao.delete(key);
|
180
|
result.addDeletedObject(key);
|
181
|
result.includeResult(resultRoot);
|
182
|
}
|
183
|
}catch(Exception e){
|
184
|
result.addException(e);
|
185
|
result.setAbort();
|
186
|
return result;
|
187
|
}
|
188
|
}
|
189
|
return result;
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
public DeleteResult isDeletable(UUID keyUuid, DeleteConfiguratorBase config){
|
194
|
DeleteResult result = new DeleteResult();
|
195
|
PolytomousKey key = this.load(keyUuid);
|
196
|
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(key);
|
197
|
if (references != null){
|
198
|
Iterator<CdmBase> iterator = references.iterator();
|
199
|
CdmBase ref;
|
200
|
while (iterator.hasNext()){
|
201
|
ref = iterator.next();
|
202
|
if ((ref instanceof PolytomousKeyNode) ){
|
203
|
PolytomousKeyNode node = HibernateProxyHelper.deproxy(ref, PolytomousKeyNode.class);
|
204
|
if (!node.getKey().equals(key)){
|
205
|
String message = "The key is a subkey of " + node.getKey() + ", referenced in node with id: " + node.getId() + ". Please remove the subkey reference first and then delete the key. " ;
|
206
|
result.addException(new ReferencedObjectUndeletableException(message));
|
207
|
result.setAbort();
|
208
|
result.addRelatedObject(ref);
|
209
|
}
|
210
|
}
|
211
|
}
|
212
|
}
|
213
|
return result;
|
214
|
}
|
215
|
|
216
|
}
|