1
|
/**
|
2
|
* Copyright (C) 2007 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.net.URI;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Collection;
|
15
|
import java.util.Enumeration;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.Iterator;
|
18
|
import java.util.List;
|
19
|
import java.util.Locale;
|
20
|
import java.util.Map;
|
21
|
import java.util.Set;
|
22
|
import java.util.UUID;
|
23
|
|
24
|
import org.apache.commons.lang.StringUtils;
|
25
|
import org.apache.log4j.Logger;
|
26
|
import org.springframework.beans.factory.annotation.Autowired;
|
27
|
import org.springframework.beans.factory.annotation.Qualifier;
|
28
|
import org.springframework.stereotype.Service;
|
29
|
import org.springframework.transaction.annotation.Transactional;
|
30
|
|
31
|
import eu.etaxonomy.cdm.api.service.UpdateResult.Status;
|
32
|
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
|
33
|
import eu.etaxonomy.cdm.api.service.config.TermDeletionConfigurator;
|
34
|
import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
|
35
|
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
|
36
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
37
|
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
|
38
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
39
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
40
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
41
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
42
|
import eu.etaxonomy.cdm.model.common.Language;
|
43
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
44
|
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
|
45
|
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
|
46
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
47
|
import eu.etaxonomy.cdm.model.common.Representation;
|
48
|
import eu.etaxonomy.cdm.model.common.TermBase;
|
49
|
import eu.etaxonomy.cdm.model.common.TermType;
|
50
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
51
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
52
|
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
|
53
|
import eu.etaxonomy.cdm.model.location.NamedAreaType;
|
54
|
import eu.etaxonomy.cdm.model.media.Media;
|
55
|
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
|
56
|
import eu.etaxonomy.cdm.persistence.dao.common.ILanguageStringBaseDao;
|
57
|
import eu.etaxonomy.cdm.persistence.dao.common.ILanguageStringDao;
|
58
|
import eu.etaxonomy.cdm.persistence.dao.common.IRepresentationDao;
|
59
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
60
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
61
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
62
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
63
|
|
64
|
@Service
|
65
|
@Transactional(readOnly = true)
|
66
|
public class TermServiceImpl extends IdentifiableServiceBase<DefinedTermBase,IDefinedTermDao> implements ITermService{
|
67
|
@SuppressWarnings("unused")
|
68
|
private static final Logger logger = Logger.getLogger(TermServiceImpl.class);
|
69
|
|
70
|
private ILanguageStringDao languageStringDao;
|
71
|
|
72
|
@Autowired
|
73
|
private IVocabularyService vocabularyService;
|
74
|
|
75
|
@Autowired
|
76
|
@Qualifier("langStrBaseDao")
|
77
|
private ILanguageStringBaseDao languageStringBaseDao;
|
78
|
private IRepresentationDao representationDao;
|
79
|
|
80
|
@Autowired
|
81
|
public void setLanguageStringDao(ILanguageStringDao languageStringDao) {
|
82
|
this.languageStringDao = languageStringDao;
|
83
|
}
|
84
|
|
85
|
@Autowired
|
86
|
public void setRepresentationDao(IRepresentationDao representationDao) {
|
87
|
this.representationDao = representationDao;
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
@Autowired
|
92
|
protected void setDao(IDefinedTermDao dao) {
|
93
|
this.dao = dao;
|
94
|
}
|
95
|
|
96
|
@Override
|
97
|
public <T extends DefinedTermBase> List<T> listByTermType(TermType termType, Integer limit, Integer start,
|
98
|
List<OrderHint> orderHints, List<String> propertyPaths) {
|
99
|
return dao.listByTermType(termType, limit, start, orderHints, propertyPaths);
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public DefinedTermBase getByUri(URI uri) {
|
104
|
return dao.findByUri(uri);
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public Language getLanguageByIso(String iso639) {
|
109
|
return dao.getLanguageByIso(iso639);
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public Language getLanguageByLabel(String label) {
|
114
|
return Language.getLanguageByLabel(label);
|
115
|
}
|
116
|
|
117
|
@Override
|
118
|
public List<Language> getLanguagesByLocale(Enumeration<Locale> locales){
|
119
|
return dao.getLanguagesByLocale(locales);
|
120
|
}
|
121
|
|
122
|
@Override
|
123
|
public <TERM extends DefinedTermBase> TERM findByIdInVocabulary(String id, UUID vocabularyUuid, Class<TERM> clazz) throws IllegalArgumentException {
|
124
|
List<TERM> list = dao.getDefinedTermByIdInVocabulary(id, vocabularyUuid, clazz, null, null);
|
125
|
if (list.isEmpty()){
|
126
|
return null;
|
127
|
}else if (list.size() == 1){
|
128
|
return list.get(0);
|
129
|
}else{
|
130
|
String message = "There is more then 1 (%d) term with the same id in vocabulary. This is forbidden. Check the state of your database.";
|
131
|
throw new IllegalStateException(String.format(message, list.size()));
|
132
|
}
|
133
|
}
|
134
|
|
135
|
|
136
|
@Override
|
137
|
public NamedArea getAreaByTdwgAbbreviation(String tdwgAbbreviation) {
|
138
|
if (StringUtils.isBlank(tdwgAbbreviation)){ //TDWG areas should always have a label
|
139
|
return null;
|
140
|
}
|
141
|
List<NamedArea> list = dao.getDefinedTermByIdInVocabulary(tdwgAbbreviation, NamedArea.uuidTdwgAreaVocabulary, NamedArea.class, null, null);
|
142
|
if (list.isEmpty()){
|
143
|
return null;
|
144
|
}else if (list.size() == 1){
|
145
|
return list.get(0);
|
146
|
}else{
|
147
|
String message = "There is more then 1 (%d) TDWG area with the same abbreviated label. This is forbidden. Check the state of your database.";
|
148
|
throw new IllegalStateException(String.format(message, list.size()));
|
149
|
}
|
150
|
}
|
151
|
|
152
|
@Override
|
153
|
public <T extends DefinedTermBase> Pager<T> getGeneralizationOf(T definedTerm, Integer pageSize, Integer pageNumber) {
|
154
|
long numberOfResults = dao.countGeneralizationOf(definedTerm);
|
155
|
|
156
|
List<T> results = new ArrayList<>();
|
157
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
158
|
results = dao.getGeneralizationOf(definedTerm, pageSize, pageNumber);
|
159
|
}
|
160
|
|
161
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
162
|
}
|
163
|
|
164
|
@Override
|
165
|
public <T extends DefinedTermBase> Pager<T> getIncludes(Collection<T> definedTerms, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
166
|
long numberOfResults = dao.countIncludes(definedTerms);
|
167
|
|
168
|
List<T> results = new ArrayList<>();
|
169
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
170
|
results = dao.getIncludes(definedTerms, pageSize, pageNumber,propertyPaths);
|
171
|
}
|
172
|
|
173
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public Pager<Media> getMedia(DefinedTermBase definedTerm, Integer pageSize, Integer pageNumber) {
|
178
|
long numberOfResults = dao.countMedia(definedTerm);
|
179
|
|
180
|
List<Media> results = new ArrayList<>();
|
181
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
182
|
results = dao.getMedia(definedTerm, pageSize, pageNumber);
|
183
|
}
|
184
|
|
185
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
186
|
}
|
187
|
|
188
|
@Override
|
189
|
public <T extends DefinedTermBase> Pager<T> getPartOf(Set<T> definedTerms,Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
190
|
long numberOfResults = dao.countPartOf(definedTerms);
|
191
|
|
192
|
List<T> results = new ArrayList<>();
|
193
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
194
|
results = dao.getPartOf(definedTerms, pageSize, pageNumber, propertyPaths);
|
195
|
}
|
196
|
|
197
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
198
|
}
|
199
|
|
200
|
@Override
|
201
|
public Pager<NamedArea> list(NamedAreaLevel level, NamedAreaType type, Integer pageSize, Integer pageNumber,
|
202
|
List<OrderHint> orderHints, List<String> propertyPaths) {
|
203
|
long numberOfResults = dao.count(level, type);
|
204
|
|
205
|
List<NamedArea> results = new ArrayList<>();
|
206
|
if (numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
207
|
results = dao.list(level, type, pageSize, pageNumber, orderHints, propertyPaths);
|
208
|
}
|
209
|
|
210
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public <T extends DefinedTermBase> Pager<T> findByRepresentationText(String label, Class<T> clazz, Integer pageSize, Integer pageNumber) {
|
215
|
long numberOfResults = dao.countDefinedTermByRepresentationText(label,clazz);
|
216
|
|
217
|
List<T> results = new ArrayList<>();
|
218
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
219
|
results = dao.getDefinedTermByRepresentationText(label, clazz, pageSize, pageNumber);
|
220
|
}
|
221
|
|
222
|
return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
|
223
|
}
|
224
|
|
225
|
@Override
|
226
|
public <T extends DefinedTermBase> Pager<T> findByRepresentationAbbreviation(String abbrev, Class<T> clazz, Integer pageSize, Integer pageNumber) {
|
227
|
long numberOfResults = dao.countDefinedTermByRepresentationAbbrev(abbrev,clazz);
|
228
|
|
229
|
List<T> results = new ArrayList<>();
|
230
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
231
|
results = dao.getDefinedTermByRepresentationAbbrev(abbrev, clazz, pageSize, pageNumber);
|
232
|
}
|
233
|
|
234
|
return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
|
235
|
}
|
236
|
|
237
|
@Override
|
238
|
public List<LanguageString> getAllLanguageStrings(int limit, int start) {
|
239
|
return languageStringDao.list(limit, start);
|
240
|
}
|
241
|
|
242
|
@Override
|
243
|
public List<Representation> getAllRepresentations(int limit, int start) {
|
244
|
return representationDao.list(limit,start);
|
245
|
}
|
246
|
|
247
|
@Override
|
248
|
public UUID saveLanguageData(LanguageStringBase languageData) {
|
249
|
return languageStringBaseDao.save(languageData).getUuid();
|
250
|
}
|
251
|
|
252
|
|
253
|
/** @deprecated use {@link #delete(DefinedTermBase, TermDeletionConfigurator)} instead
|
254
|
* to allow DeleteResult return type*/
|
255
|
@Override
|
256
|
@Deprecated
|
257
|
public DeleteResult delete(DefinedTermBase term){
|
258
|
DeleteResult result = new DeleteResult();
|
259
|
|
260
|
TermDeletionConfigurator defaultConfig = new TermDeletionConfigurator();
|
261
|
result = delete(term, defaultConfig);
|
262
|
return result;
|
263
|
}
|
264
|
|
265
|
@Override
|
266
|
@Deprecated
|
267
|
@Transactional(readOnly = false)
|
268
|
public DeleteResult delete(UUID termUuid){
|
269
|
DeleteResult result = new DeleteResult();
|
270
|
|
271
|
TermDeletionConfigurator defaultConfig = new TermDeletionConfigurator();
|
272
|
result = delete(dao.load(termUuid), defaultConfig);
|
273
|
return result;
|
274
|
}
|
275
|
|
276
|
@Override
|
277
|
public DeleteResult delete(DefinedTermBase term, TermDeletionConfigurator config){
|
278
|
if (config == null){
|
279
|
config = new TermDeletionConfigurator();
|
280
|
}
|
281
|
// boolean isInternal = config.isInternal();
|
282
|
|
283
|
Set<DefinedTermBase> termsToSave = new HashSet<DefinedTermBase>();
|
284
|
|
285
|
DeleteResult result = isDeletable(term.getUuid(), config);
|
286
|
//CdmBase.deproxy(dao.merge(term), DefinedTermBase.class);
|
287
|
try {
|
288
|
//generalization of
|
289
|
Set<DefinedTermBase> specificTerms = term.getGeneralizationOf();
|
290
|
if (specificTerms.size()>0){
|
291
|
if (config.isDeleteGeneralizationOfRelations()){
|
292
|
DefinedTermBase generalTerm = term.getKindOf();
|
293
|
for (DefinedTermBase specificTerm: specificTerms){
|
294
|
term.removeGeneralization(specificTerm);
|
295
|
if (generalTerm != null){
|
296
|
generalTerm.addGeneralizationOf(specificTerm);
|
297
|
termsToSave.add(generalTerm);
|
298
|
}
|
299
|
}
|
300
|
}else{
|
301
|
//TODO Exception type
|
302
|
String message = "This term has specifing terms. Move or delete specifiing terms prior to delete or change delete configuration.";
|
303
|
result.addRelatedObjects(specificTerms);
|
304
|
result.setAbort();
|
305
|
Exception ex = new DataChangeNoRollbackException(message);
|
306
|
result.addException(ex);
|
307
|
}
|
308
|
}
|
309
|
|
310
|
//kind of
|
311
|
DefinedTermBase generalTerm = term.getKindOf();
|
312
|
if (generalTerm != null){
|
313
|
if (config.isDeleteKindOfRelations()){
|
314
|
generalTerm.removeGeneralization(term);
|
315
|
}else{
|
316
|
//TODO Exception type
|
317
|
String message = "This term is kind of another term. Move or delete kind of relationship prior to delete or change delete configuration.";
|
318
|
result.addRelatedObject(generalTerm);
|
319
|
result.setAbort();
|
320
|
DataChangeNoRollbackException ex = new DataChangeNoRollbackException(message);
|
321
|
result.addException(ex);
|
322
|
throw ex;
|
323
|
}
|
324
|
}
|
325
|
|
326
|
//part of
|
327
|
DefinedTermBase parentTerm = term.getPartOf();
|
328
|
if (parentTerm != null){
|
329
|
if (! config.isDeletePartOfRelations()){
|
330
|
//TODO Exception type
|
331
|
String message = "This term is included in another term. Remove from parent term prior to delete or change delete configuration.";
|
332
|
result.addRelatedObject(parentTerm);
|
333
|
result.setAbort();
|
334
|
DataChangeNoRollbackException ex = new DataChangeNoRollbackException(message);
|
335
|
result.addException(ex);
|
336
|
}
|
337
|
}
|
338
|
|
339
|
|
340
|
//included in
|
341
|
Set<DefinedTermBase> includedTerms = term.getIncludes();
|
342
|
if (includedTerms.size()> 0){
|
343
|
// if (config.isDeleteIncludedTerms()){
|
344
|
// for (DefinedTermBase includedTerm: includedTerms){
|
345
|
// config.setCheck(true);
|
346
|
// DeleteResult includedResult = this.delete(includedTerm, config);
|
347
|
//// config.setCheck(isCheck);
|
348
|
// result.includeResult(includedResult);
|
349
|
// }
|
350
|
// }else
|
351
|
if (config.isDeleteIncludedRelations()){
|
352
|
DefinedTermBase parent = term.getPartOf();
|
353
|
for (DefinedTermBase includedTerm: includedTerms){
|
354
|
term.removeIncludes(includedTerm);
|
355
|
if (parent != null){
|
356
|
parent.addIncludes(includedTerm);
|
357
|
termsToSave.add(parent);
|
358
|
}
|
359
|
}
|
360
|
}else{
|
361
|
//TODO Exception type
|
362
|
String message = "This term includes other terms. Move or delete included terms prior to delete or change delete configuration.";
|
363
|
result.addRelatedObjects(includedTerms);
|
364
|
result.setAbort();
|
365
|
Exception ex = new DataChangeNoRollbackException(message);
|
366
|
result.addException(ex);
|
367
|
}
|
368
|
}
|
369
|
|
370
|
//part of
|
371
|
if (parentTerm != null){
|
372
|
if (config.isDeletePartOfRelations()){
|
373
|
parentTerm.removeIncludes(term);
|
374
|
termsToSave.add(parentTerm);
|
375
|
}else{
|
376
|
//handelede before "included in"
|
377
|
}
|
378
|
}
|
379
|
|
380
|
// relatedObjects;
|
381
|
|
382
|
|
383
|
if (result.isOk()){
|
384
|
TermVocabulary voc = term.getVocabulary();
|
385
|
if (voc!= null){
|
386
|
voc.removeTerm(term);
|
387
|
}
|
388
|
//TODO save voc
|
389
|
if (true /*!config.isInternal()*/){
|
390
|
dao.delete(term);
|
391
|
result.addDeletedObject(term);
|
392
|
dao.saveOrUpdateAll(termsToSave);
|
393
|
// for (DeleteResult.PersistPair persistPair : result.getObjectsToDelete()){
|
394
|
// persistPair.dao.delete(persistPair.objectToPersist);
|
395
|
// }
|
396
|
// for (DeleteResult.PersistPair persistPair : result.getObjectsToSave()){
|
397
|
// persistPair.dao.saveOrUpdate(persistPair.objectToPersist);
|
398
|
// }
|
399
|
|
400
|
}
|
401
|
}
|
402
|
} catch (DataChangeNoRollbackException e) {
|
403
|
result.setStatus(Status.ERROR);
|
404
|
}
|
405
|
return result;
|
406
|
}
|
407
|
|
408
|
@Override
|
409
|
@Transactional(readOnly = false)
|
410
|
public DeleteResult delete(UUID termUuid, TermDeletionConfigurator config){
|
411
|
return delete(dao.load(termUuid), config);
|
412
|
}
|
413
|
|
414
|
@Override
|
415
|
@Transactional(readOnly = false)
|
416
|
public void updateTitleCache(Class<? extends DefinedTermBase> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<DefinedTermBase> cacheStrategy, IProgressMonitor monitor) {
|
417
|
//TODO shouldnt this be TermBase instead of DefinedTermBase
|
418
|
if (clazz == null){
|
419
|
clazz = DefinedTermBase.class;
|
420
|
}
|
421
|
super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
|
422
|
}
|
423
|
|
424
|
@Override
|
425
|
public DeleteResult isDeletable(UUID termUuid, DeleteConfiguratorBase config){
|
426
|
DeleteResult result = new DeleteResult();
|
427
|
TermBase term = load(termUuid);
|
428
|
Set<CdmBase> references = commonService.getReferencingObjectsForDeletion(term);
|
429
|
if (references != null){
|
430
|
result.addRelatedObjects(references);
|
431
|
Iterator<CdmBase> iterator = references.iterator();
|
432
|
CdmBase ref;
|
433
|
while (iterator.hasNext()){
|
434
|
ref = iterator.next();
|
435
|
if (ref instanceof TermVocabulary){
|
436
|
result.getRelatedObjects().remove(ref);
|
437
|
}else{
|
438
|
|
439
|
String message = "An object of " + ref.getClass().getName() + " with ID " + ref.getId() + " is referencing the object" ;
|
440
|
result.addException(new ReferencedObjectUndeletableException(message));
|
441
|
result.setAbort();
|
442
|
}
|
443
|
|
444
|
}
|
445
|
}
|
446
|
return result;
|
447
|
}
|
448
|
|
449
|
@Override
|
450
|
@Transactional(readOnly = false)
|
451
|
public Map<UUID, Representation> saveOrUpdateRepresentations(Collection<Representation> representations){
|
452
|
return representationDao.saveOrUpdateAll(representations);
|
453
|
}
|
454
|
|
455
|
@Override
|
456
|
@Transactional(readOnly = true)
|
457
|
public List<UuidAndTitleCache<NamedArea>> getUuidAndTitleCache(List<TermVocabulary> vocs, Integer limit, String pattern, Language lang) {
|
458
|
List<NamedArea> areas = dao.getUuidAndTitleCache(vocs, limit, pattern);
|
459
|
|
460
|
List<UuidAndTitleCache<NamedArea>> result = new ArrayList();
|
461
|
UuidAndTitleCache<NamedArea> uuidAndTitleCache;
|
462
|
for (NamedArea area: areas){
|
463
|
uuidAndTitleCache = new UuidAndTitleCache<>(area.getUuid(), area.getId(), area.labelWithLevel(area, lang));
|
464
|
result.add(uuidAndTitleCache);
|
465
|
}
|
466
|
|
467
|
return result;
|
468
|
}
|
469
|
|
470
|
@Override
|
471
|
public TermDto getParentAsDto(TermDto childTerm) {
|
472
|
return dao.getParentAsDto(childTerm);
|
473
|
}
|
474
|
|
475
|
@Override
|
476
|
public Collection<TermDto> getIncludesAsDto(
|
477
|
TermDto parentTerm) {
|
478
|
return dao.getIncludesAsDto(parentTerm);
|
479
|
}
|
480
|
|
481
|
@Override
|
482
|
public Collection<TermDto> getKindOfsAsDto(
|
483
|
TermDto parentTerm) {
|
484
|
return dao.getKindOfsAsDto(parentTerm);
|
485
|
}
|
486
|
|
487
|
@Transactional(readOnly = false)
|
488
|
@Override
|
489
|
public void moveTerm(TermDto termDto, UUID parentUUID) {
|
490
|
moveTerm(termDto, parentUUID, null);
|
491
|
}
|
492
|
|
493
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
494
|
@Transactional(readOnly = false)
|
495
|
@Override
|
496
|
public void moveTerm(TermDto termDto, UUID parentUuid, TermMovePosition termMovePosition) {
|
497
|
boolean isKindOf = termDto.getKindOfUuid()!=null && termDto.getKindOfUuid().equals(parentUuid);
|
498
|
TermVocabulary vocabulary = HibernateProxyHelper.deproxy(vocabularyService.load(termDto.getVocabularyUuid()));
|
499
|
DefinedTermBase parent = HibernateProxyHelper.deproxy(dao.load(parentUuid));
|
500
|
if(parent==null){
|
501
|
//new parent is a vocabulary
|
502
|
TermVocabulary parentVocabulary = HibernateProxyHelper.deproxy(vocabularyService.load(parentUuid));
|
503
|
DefinedTermBase term = HibernateProxyHelper.deproxy(dao.load(termDto.getUuid()));
|
504
|
if(parentVocabulary!=null){
|
505
|
term.setKindOf(null);
|
506
|
term.setPartOf(null);
|
507
|
|
508
|
vocabulary.removeTerm(term);
|
509
|
parentVocabulary.addTerm(term);
|
510
|
}
|
511
|
vocabularyService.saveOrUpdate(parentVocabulary);
|
512
|
}
|
513
|
else {
|
514
|
DefinedTermBase term = HibernateProxyHelper.deproxy(dao.load(termDto.getUuid()));
|
515
|
//new parent is a term
|
516
|
if(parent.isInstanceOf(OrderedTermBase.class)
|
517
|
&& term.isInstanceOf(OrderedTermBase.class)
|
518
|
&& termMovePosition!=null
|
519
|
&& HibernateProxyHelper.deproxy(parent, OrderedTermBase.class).getVocabulary().isInstanceOf(OrderedTermVocabulary.class)) {
|
520
|
//new parent is an ordered term
|
521
|
OrderedTermBase orderedTerm = HibernateProxyHelper.deproxy(term, OrderedTermBase.class);
|
522
|
OrderedTermBase targetOrderedDefinedTerm = HibernateProxyHelper.deproxy(parent, OrderedTermBase.class);
|
523
|
OrderedTermVocabulary otVoc = HibernateProxyHelper.deproxy(targetOrderedDefinedTerm.getVocabulary(), OrderedTermVocabulary.class);
|
524
|
if(termMovePosition.equals(TermMovePosition.BEFORE)) {
|
525
|
orderedTerm.getVocabulary().removeTerm(orderedTerm);
|
526
|
otVoc.addTermAbove(orderedTerm, targetOrderedDefinedTerm);
|
527
|
if (targetOrderedDefinedTerm.getPartOf() != null){
|
528
|
targetOrderedDefinedTerm.getPartOf().addIncludes(orderedTerm);
|
529
|
}
|
530
|
}
|
531
|
else if(termMovePosition.equals(TermMovePosition.AFTER)) {
|
532
|
orderedTerm.getVocabulary().removeTerm(orderedTerm);
|
533
|
otVoc.addTermBelow(orderedTerm, targetOrderedDefinedTerm);
|
534
|
if (targetOrderedDefinedTerm.getPartOf() != null){
|
535
|
targetOrderedDefinedTerm.getPartOf().addIncludes(orderedTerm);
|
536
|
}
|
537
|
}
|
538
|
else if(termMovePosition.equals(TermMovePosition.ON)) {
|
539
|
orderedTerm.getVocabulary().removeTerm(orderedTerm);
|
540
|
targetOrderedDefinedTerm.addIncludes(orderedTerm);
|
541
|
targetOrderedDefinedTerm.getVocabulary().addTerm(orderedTerm);
|
542
|
}
|
543
|
}
|
544
|
else{
|
545
|
vocabulary.removeTerm(term);
|
546
|
if(isKindOf){
|
547
|
parent.addGeneralizationOf(term);
|
548
|
}
|
549
|
else{
|
550
|
parent.addIncludes(term);
|
551
|
}
|
552
|
parent.getVocabulary().addTerm(term);
|
553
|
}
|
554
|
vocabularyService.saveOrUpdate(parent.getVocabulary());
|
555
|
}
|
556
|
}
|
557
|
|
558
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
559
|
@Transactional(readOnly = false)
|
560
|
@Override
|
561
|
public void addNewTerm(TermType termType, UUID parentUUID, boolean isKindOf) {
|
562
|
DefinedTermBase term = termType.getEmptyDefinedTermBase();
|
563
|
dao.save(term);
|
564
|
DefinedTermBase parent = dao.load(parentUUID);
|
565
|
if(isKindOf){
|
566
|
parent.addGeneralizationOf(term);
|
567
|
}
|
568
|
else{
|
569
|
parent.addIncludes(term);
|
570
|
}
|
571
|
parent.getVocabulary().addTerm(term);
|
572
|
dao.saveOrUpdate(parent);
|
573
|
}
|
574
|
|
575
|
public enum TermMovePosition{
|
576
|
BEFORE,
|
577
|
AFTER,
|
578
|
ON
|
579
|
}
|
580
|
|
581
|
}
|