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.io.IOException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Arrays;
|
15
|
import java.util.Collection;
|
16
|
import java.util.EnumSet;
|
17
|
import java.util.HashMap;
|
18
|
import java.util.HashSet;
|
19
|
import java.util.Iterator;
|
20
|
import java.util.List;
|
21
|
import java.util.Map;
|
22
|
import java.util.Set;
|
23
|
import java.util.UUID;
|
24
|
|
25
|
import org.apache.log4j.Logger;
|
26
|
import org.apache.lucene.queryparser.classic.ParseException;
|
27
|
import org.apache.lucene.search.BooleanClause.Occur;
|
28
|
import org.apache.lucene.search.BooleanQuery.Builder;
|
29
|
import org.apache.lucene.search.SortField;
|
30
|
import org.apache.lucene.search.grouping.TopGroups;
|
31
|
import org.apache.lucene.util.BytesRef;
|
32
|
import org.hibernate.TransientObjectException;
|
33
|
import org.hibernate.search.spatial.impl.Rectangle;
|
34
|
import org.springframework.beans.factory.annotation.Autowired;
|
35
|
import org.springframework.dao.DataRetrievalFailureException;
|
36
|
import org.springframework.stereotype.Service;
|
37
|
import org.springframework.transaction.annotation.Transactional;
|
38
|
|
39
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
40
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
|
41
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
|
42
|
import eu.etaxonomy.cdm.api.service.UpdateResult.Status;
|
43
|
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
|
44
|
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
|
45
|
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
|
46
|
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
|
47
|
import eu.etaxonomy.cdm.api.service.dto.DNASampleDTO;
|
48
|
import eu.etaxonomy.cdm.api.service.dto.DerivedUnitDTO;
|
49
|
import eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO;
|
50
|
import eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO;
|
51
|
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
|
52
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
53
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
54
|
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
|
55
|
import eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider;
|
56
|
import eu.etaxonomy.cdm.api.service.search.ISearchResultBuilder;
|
57
|
import eu.etaxonomy.cdm.api.service.search.LuceneParseException;
|
58
|
import eu.etaxonomy.cdm.api.service.search.LuceneSearch;
|
59
|
import eu.etaxonomy.cdm.api.service.search.QueryFactory;
|
60
|
import eu.etaxonomy.cdm.api.service.search.SearchResult;
|
61
|
import eu.etaxonomy.cdm.api.service.search.SearchResultBuilder;
|
62
|
import eu.etaxonomy.cdm.api.service.util.TaxonRelationshipEdge;
|
63
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
64
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
65
|
import eu.etaxonomy.cdm.model.CdmBaseType;
|
66
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
67
|
import eu.etaxonomy.cdm.model.common.Language;
|
68
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
69
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
70
|
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
|
71
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
72
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
73
|
import eu.etaxonomy.cdm.model.location.Country;
|
74
|
import eu.etaxonomy.cdm.model.location.Point;
|
75
|
import eu.etaxonomy.cdm.model.media.Media;
|
76
|
import eu.etaxonomy.cdm.model.molecular.AmplificationResult;
|
77
|
import eu.etaxonomy.cdm.model.molecular.DnaSample;
|
78
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
79
|
import eu.etaxonomy.cdm.model.molecular.SingleRead;
|
80
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
81
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
82
|
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
|
83
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
84
|
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
|
85
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
86
|
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
|
87
|
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
|
88
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
89
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
90
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
91
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
92
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
93
|
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer;
|
94
|
import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;
|
95
|
import eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao;
|
96
|
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
|
97
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
98
|
import eu.etaxonomy.cdm.persistence.query.AssignmentStatus;
|
99
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
100
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
101
|
|
102
|
/**
|
103
|
* @author a.babadshanjan
|
104
|
* @since 01.09.2008
|
105
|
*/
|
106
|
@Service
|
107
|
@Transactional(readOnly = true)
|
108
|
public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObservationBase, IOccurrenceDao> implements IOccurrenceService {
|
109
|
|
110
|
static private final Logger logger = Logger.getLogger(OccurrenceServiceImpl.class);
|
111
|
|
112
|
@Autowired
|
113
|
private IDefinedTermDao definedTermDao;
|
114
|
|
115
|
@Autowired
|
116
|
private IDescriptionService descriptionService;
|
117
|
|
118
|
@Autowired
|
119
|
private INameService nameService;
|
120
|
|
121
|
@Autowired
|
122
|
private IEventBaseService eventService;
|
123
|
|
124
|
@Autowired
|
125
|
private ITaxonService taxonService;
|
126
|
|
127
|
@Autowired
|
128
|
private ISequenceService sequenceService;
|
129
|
|
130
|
@Autowired
|
131
|
private AbstractBeanInitializer beanInitializer;
|
132
|
|
133
|
@Autowired
|
134
|
private ILuceneIndexToolProvider luceneIndexToolProvider;
|
135
|
|
136
|
public OccurrenceServiceImpl() {
|
137
|
logger.debug("Load OccurrenceService Bean");
|
138
|
}
|
139
|
|
140
|
|
141
|
@Override
|
142
|
@Transactional(readOnly = false)
|
143
|
public UpdateResult updateCaches(Class<? extends SpecimenOrObservationBase> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<SpecimenOrObservationBase> cacheStrategy, IProgressMonitor monitor) {
|
144
|
if (clazz == null) {
|
145
|
clazz = SpecimenOrObservationBase.class;
|
146
|
}
|
147
|
return super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
|
148
|
}
|
149
|
|
150
|
/**
|
151
|
* FIXME Candidate for harmonization
|
152
|
* move to termService
|
153
|
*/
|
154
|
@Override
|
155
|
public Country getCountryByIso(String iso639) {
|
156
|
return this.definedTermDao.getCountryByIso(iso639);
|
157
|
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* FIXME Candidate for harmonization
|
162
|
* move to termService
|
163
|
*/
|
164
|
@Override
|
165
|
public List<Country> getCountryByName(String name) {
|
166
|
List<? extends DefinedTermBase> terms = this.definedTermDao.findByTitleWithRestrictions(Country.class, name, null, null, null, null, null, null);
|
167
|
List<Country> countries = new ArrayList<>();
|
168
|
for (int i = 0; i < terms.size(); i++) {
|
169
|
countries.add((Country) terms.get(i));
|
170
|
}
|
171
|
return countries;
|
172
|
}
|
173
|
|
174
|
@Override
|
175
|
@Autowired
|
176
|
protected void setDao(IOccurrenceDao dao) {
|
177
|
this.dao = dao;
|
178
|
}
|
179
|
|
180
|
@Override
|
181
|
public Pager<DerivationEvent> getDerivationEvents(SpecimenOrObservationBase occurence, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
182
|
long numberOfResults = dao.countDerivationEvents(occurence);
|
183
|
|
184
|
List<DerivationEvent> results = new ArrayList<>();
|
185
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
186
|
results = dao.getDerivationEvents(occurence, pageSize, pageNumber, propertyPaths);
|
187
|
}
|
188
|
|
189
|
return new DefaultPagerImpl<DerivationEvent>(pageNumber, numberOfResults, pageSize, results);
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
public long countDeterminations(SpecimenOrObservationBase occurence, TaxonBase taxonbase) {
|
194
|
return dao.countDeterminations(occurence, taxonbase);
|
195
|
}
|
196
|
|
197
|
@Override
|
198
|
public Pager<DeterminationEvent> getDeterminations(SpecimenOrObservationBase occurrence, TaxonBase taxonBase,
|
199
|
Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
200
|
long numberOfResults = dao.countDeterminations(occurrence, taxonBase);
|
201
|
|
202
|
List<DeterminationEvent> results = new ArrayList<>();
|
203
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
204
|
results = dao.getDeterminations(occurrence, taxonBase, pageSize, pageNumber, propertyPaths);
|
205
|
}
|
206
|
|
207
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
208
|
}
|
209
|
|
210
|
@Override
|
211
|
public Pager<Media> getMedia(SpecimenOrObservationBase occurence, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
212
|
long numberOfResults = dao.countMedia(occurence);
|
213
|
|
214
|
List<Media> results = new ArrayList<>();
|
215
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
216
|
results = dao.getMedia(occurence, pageSize, pageNumber, propertyPaths);
|
217
|
}
|
218
|
|
219
|
return new DefaultPagerImpl<Media>(pageNumber, numberOfResults, pageSize, results);
|
220
|
}
|
221
|
|
222
|
@Override
|
223
|
public Pager<Media> getMediainHierarchy(SpecimenOrObservationBase rootOccurence, Integer pageSize,
|
224
|
Integer pageNumber, List<String> propertyPaths) {
|
225
|
List<Media> media = new ArrayList<>();
|
226
|
//media specimens
|
227
|
if(rootOccurence.isInstanceOf(MediaSpecimen.class)){
|
228
|
MediaSpecimen mediaSpecimen = HibernateProxyHelper.deproxy(rootOccurence, MediaSpecimen.class);
|
229
|
media.add(mediaSpecimen.getMediaSpecimen());
|
230
|
}
|
231
|
// pherograms & gelPhotos
|
232
|
if (rootOccurence.isInstanceOf(DnaSample.class)) {
|
233
|
DnaSample dnaSample = CdmBase.deproxy(rootOccurence, DnaSample.class);
|
234
|
Set<Sequence> sequences = dnaSample.getSequences();
|
235
|
//we do show only those gelPhotos which lead to a consensus sequence
|
236
|
for (Sequence sequence : sequences) {
|
237
|
Set<Media> dnaRelatedMedia = new HashSet<>();
|
238
|
for (SingleRead singleRead : sequence.getSingleReads()){
|
239
|
AmplificationResult amplification = singleRead.getAmplificationResult();
|
240
|
dnaRelatedMedia.add(amplification.getGelPhoto());
|
241
|
dnaRelatedMedia.add(singleRead.getPherogram());
|
242
|
dnaRelatedMedia.remove(null);
|
243
|
}
|
244
|
media.addAll(dnaRelatedMedia);
|
245
|
}
|
246
|
}
|
247
|
if(rootOccurence.isInstanceOf(DerivedUnit.class)){
|
248
|
DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(rootOccurence, DerivedUnit.class);
|
249
|
for (DerivationEvent derivationEvent : derivedUnit.getDerivationEvents()) {
|
250
|
for (DerivedUnit childDerivative : derivationEvent.getDerivatives()) {
|
251
|
media.addAll(getMediainHierarchy(childDerivative, pageSize, pageNumber, propertyPaths).getRecords());
|
252
|
}
|
253
|
}
|
254
|
}
|
255
|
return new DefaultPagerImpl<Media>(pageNumber, Long.valueOf(media.size()), pageSize, media);
|
256
|
}
|
257
|
|
258
|
@Override
|
259
|
public Pager<SpecimenOrObservationBase> list(Class<? extends SpecimenOrObservationBase> type, TaxonName determinedAs,
|
260
|
Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
261
|
long numberOfResults = dao.count(type, determinedAs);
|
262
|
@SuppressWarnings("rawtypes")
|
263
|
List<SpecimenOrObservationBase> results = new ArrayList<>();
|
264
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
265
|
results = dao.list(type, determinedAs, pageSize, pageNumber, orderHints, propertyPaths);
|
266
|
}
|
267
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
268
|
}
|
269
|
|
270
|
@Override
|
271
|
public Pager<SpecimenOrObservationBase> list(Class<? extends SpecimenOrObservationBase> type, TaxonBase determinedAs,
|
272
|
Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
273
|
long numberOfResults = dao.count(type, determinedAs);
|
274
|
@SuppressWarnings("rawtypes")
|
275
|
List<SpecimenOrObservationBase> results = new ArrayList<>();
|
276
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
277
|
results = dao.list(type, determinedAs, pageSize, pageNumber, orderHints, propertyPaths);
|
278
|
}
|
279
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
280
|
}
|
281
|
|
282
|
@Override
|
283
|
public List<UuidAndTitleCache<DerivedUnit>> getDerivedUnitUuidAndTitleCache(Integer limit, String pattern) {
|
284
|
return dao.getDerivedUnitUuidAndTitleCache(limit, pattern);
|
285
|
}
|
286
|
|
287
|
@Override
|
288
|
public List<UuidAndTitleCache<FieldUnit>> getFieldUnitUuidAndTitleCache() {
|
289
|
return dao.getFieldUnitUuidAndTitleCache();
|
290
|
}
|
291
|
|
292
|
@Override
|
293
|
public DerivedUnitFacade getDerivedUnitFacade(DerivedUnit derivedUnit, List<String> derivedUnitFacadeInitStrategy) throws DerivedUnitFacadeNotSupportedException {
|
294
|
derivedUnit = (DerivedUnit) dao.load(derivedUnit.getUuid(), null);
|
295
|
DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
|
296
|
config.setThrowExceptionForNonSpecimenPreservationMethodRequest(false);
|
297
|
DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(derivedUnit, config);
|
298
|
beanInitializer.initialize(derivedUnitFacade, derivedUnitFacadeInitStrategy);
|
299
|
return derivedUnitFacade;
|
300
|
}
|
301
|
|
302
|
@Override
|
303
|
public List<DerivedUnitFacade> listDerivedUnitFacades(
|
304
|
DescriptionBase description, List<String> derivedUnitFacadeInitStrategy) {
|
305
|
|
306
|
List<DerivedUnitFacade> derivedUnitFacadeList = new ArrayList<>();
|
307
|
IndividualsAssociation tempIndividualsAssociation;
|
308
|
SpecimenOrObservationBase tempSpecimenOrObservationBase;
|
309
|
List<IndividualsAssociation> elements = descriptionService.listDescriptionElements(description, null, IndividualsAssociation.class, null, 0, Arrays.asList(new String []{"associatedSpecimenOrObservation"}));
|
310
|
for (IndividualsAssociation element : elements) {
|
311
|
tempIndividualsAssociation = HibernateProxyHelper.deproxy(element, IndividualsAssociation.class);
|
312
|
if (tempIndividualsAssociation.getAssociatedSpecimenOrObservation() != null) {
|
313
|
tempSpecimenOrObservationBase = HibernateProxyHelper.deproxy(tempIndividualsAssociation.getAssociatedSpecimenOrObservation(), SpecimenOrObservationBase.class);
|
314
|
if (tempSpecimenOrObservationBase.isInstanceOf(DerivedUnit.class)) {
|
315
|
try {
|
316
|
derivedUnitFacadeList.add(DerivedUnitFacade.NewInstance(HibernateProxyHelper.deproxy(tempSpecimenOrObservationBase, DerivedUnit.class)));
|
317
|
} catch (DerivedUnitFacadeNotSupportedException e) {
|
318
|
logger.warn(tempIndividualsAssociation.getAssociatedSpecimenOrObservation().getTitleCache() + " : " + e.getMessage());
|
319
|
}
|
320
|
}
|
321
|
}
|
322
|
}
|
323
|
|
324
|
beanInitializer.initializeAll(derivedUnitFacadeList, derivedUnitFacadeInitStrategy);
|
325
|
|
326
|
return derivedUnitFacadeList;
|
327
|
}
|
328
|
|
329
|
|
330
|
@Override
|
331
|
public <T extends SpecimenOrObservationBase> List<T> listByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includeRelationships,
|
332
|
Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
333
|
|
334
|
return pageByAssociatedTaxon(type, includeRelationships, associatedTaxon, maxDepth, pageSize, pageNumber, orderHints, propertyPaths).getRecords();
|
335
|
}
|
336
|
|
337
|
@Override
|
338
|
public Collection<SpecimenNodeWrapper> listUuidAndTitleCacheByAssociatedTaxon(List<UUID> taxonNodeUuids,
|
339
|
Integer limit, Integer start) {
|
340
|
return dao.listUuidAndTitleCacheByAssociatedTaxon(taxonNodeUuids, limit, start);
|
341
|
}
|
342
|
|
343
|
@Override
|
344
|
public Collection<SpecimenOrObservationBase> listFieldUnitsByAssociatedTaxon(Taxon associatedTaxon, List<OrderHint> orderHints, List<String> propertyPaths) {
|
345
|
return pageFieldUnitsByAssociatedTaxon(null, associatedTaxon, null, null, null, null, propertyPaths).getRecords();
|
346
|
}
|
347
|
|
348
|
@Override
|
349
|
public Pager<SpecimenOrObservationBase> pageFieldUnitsByAssociatedTaxon(Set<TaxonRelationshipEdge> includeRelationships,
|
350
|
Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
|
351
|
List<String> propertyPaths) {
|
352
|
|
353
|
if (!getSession().contains(associatedTaxon)) {
|
354
|
associatedTaxon = (Taxon) taxonService.load(associatedTaxon.getUuid());
|
355
|
}
|
356
|
|
357
|
// gather the IDs of all relevant field units
|
358
|
Set<UUID> fieldUnitUuids = new HashSet<>();
|
359
|
List<SpecimenOrObservationBase> records = listByAssociatedTaxon(null, includeRelationships, associatedTaxon, maxDepth, null, null, orderHints, propertyPaths);
|
360
|
for (SpecimenOrObservationBase<?> specimen : records) {
|
361
|
for (FieldUnit fieldUnit : findFieldUnits(specimen.getUuid(), null)) {
|
362
|
fieldUnitUuids.add(fieldUnit.getUuid());
|
363
|
}
|
364
|
}
|
365
|
//dao.list() does the paging of the field units. Passing the field units directly to the Pager would not work
|
366
|
List<SpecimenOrObservationBase> fieldUnits = dao.list(fieldUnitUuids, pageSize, pageNumber, orderHints, propertyPaths);
|
367
|
return new DefaultPagerImpl<>(pageNumber, fieldUnitUuids.size(), pageSize, fieldUnits);
|
368
|
}
|
369
|
|
370
|
@Override
|
371
|
public FieldUnitDTO assembleFieldUnitDTO(FieldUnit fieldUnit) {
|
372
|
|
373
|
if (!getSession().contains(fieldUnit)) {
|
374
|
fieldUnit = (FieldUnit) load(fieldUnit.getUuid());
|
375
|
}
|
376
|
// FIXME the filter for SpecimenOrObservationType.PreservedSpecimen has been preserved from the former implementation (see commit 07e3f63c7d and older)
|
377
|
// it is questionable if this filter makes sense for all use cases or if it is only a sensible default for the
|
378
|
// compressed specimen table in the cdm-dataportal (see #6816, #6870)
|
379
|
FieldUnitDTO fieldUnitDTO = FieldUnitDTO.fromEntity(fieldUnit, EnumSet.of(SpecimenOrObservationType.PreservedSpecimen));
|
380
|
return fieldUnitDTO;
|
381
|
}
|
382
|
|
383
|
|
384
|
@Override
|
385
|
@Deprecated
|
386
|
public String getMostSignificantIdentifier(DerivedUnit derivedUnit) {
|
387
|
return derivedUnit.getMostSignificantIdentifier();
|
388
|
}
|
389
|
|
390
|
@Override
|
391
|
@Transactional
|
392
|
public DerivedUnitDTO assembleDerivedUnitDTO(DerivedUnit derivedUnit) {
|
393
|
|
394
|
if (!getSession().contains(derivedUnit)) {
|
395
|
derivedUnit = (DerivedUnit) load(derivedUnit.getUuid());
|
396
|
}
|
397
|
Collection<IndividualsAssociation> individualsAssociations = listIndividualsAssociations(derivedUnit, null, null, null, null);
|
398
|
DerivedUnitDTO derivedUnitDTO = DerivedUnitDTO.fromEntity(derivedUnit, individualsAssociations);
|
399
|
return derivedUnitDTO;
|
400
|
}
|
401
|
|
402
|
private Set<SpecimenOrObservationBaseDTO> getDerivedUnitDTOsFor(SpecimenOrObservationBaseDTO specimenDto, DerivedUnit specimen, HashMap<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedSpecimen) {
|
403
|
Set<SpecimenOrObservationBaseDTO> derivedUnits = new HashSet<>();
|
404
|
// load
|
405
|
for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
|
406
|
for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
|
407
|
if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
|
408
|
DerivedUnitDTO dto;
|
409
|
if (derivative instanceof DnaSample) {
|
410
|
dto = new DNASampleDTO(derivative);
|
411
|
} else {
|
412
|
dto = new DerivedUnitDTO(derivative);
|
413
|
}
|
414
|
alreadyCollectedSpecimen.put(dto.getUuid(), dto);
|
415
|
dto.addAllDerivates(getDerivedUnitDTOsFor(dto, derivative, alreadyCollectedSpecimen));
|
416
|
derivedUnits.add(dto);
|
417
|
}
|
418
|
}
|
419
|
}
|
420
|
return derivedUnits;
|
421
|
}
|
422
|
|
423
|
// private Set<DerivateDTO> getDerivedUnitDTOsFor(DerivateDTO specimenDto, DerivedUnit specimen, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
|
424
|
// Set<DerivateDTO> derivedUnits = new HashSet<>();
|
425
|
//// load
|
426
|
// for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
|
427
|
// for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
|
428
|
// if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
|
429
|
// PreservedSpecimenDTO dto;
|
430
|
// if (derivative instanceof DnaSample){
|
431
|
// dto = DNASampleDTO.newInstance(derivative);
|
432
|
// }else{
|
433
|
// dto = PreservedSpecimenDTO.newInstance(derivative);
|
434
|
// }
|
435
|
// alreadyCollectedSpecimen.put(dto.getUuid(), dto);
|
436
|
// dto.addAllDerivates(getDerivedUnitDTOsFor(dto, derivative, alreadyCollectedSpecimen));
|
437
|
// derivedUnits.add(dto);
|
438
|
// }
|
439
|
// }
|
440
|
// }
|
441
|
// return derivedUnits;
|
442
|
// }
|
443
|
|
444
|
|
445
|
@SuppressWarnings("unchecked")
|
446
|
@Override
|
447
|
public <T extends SpecimenOrObservationBase> Pager<T> pageByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includedRelationships,
|
448
|
Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
449
|
|
450
|
Set<Taxon> taxa = new HashSet<>();
|
451
|
Set<Integer> occurrenceIds = new HashSet<>();
|
452
|
List<T> occurrences = new ArrayList<>();
|
453
|
boolean includeUnpublished = INCLUDE_UNPUBLISHED;
|
454
|
|
455
|
// Integer limit = PagerUtils.limitFor(pageSize);
|
456
|
// Integer start = PagerUtils.startFor(pageSize, pageNumber);
|
457
|
|
458
|
if (!getSession().contains(associatedTaxon)) {
|
459
|
associatedTaxon = (Taxon) taxonService.load(associatedTaxon.getUuid());
|
460
|
}
|
461
|
|
462
|
if (includedRelationships != null) {
|
463
|
taxa = taxonService.listRelatedTaxa(associatedTaxon, includedRelationships, maxDepth, includeUnpublished, null, null, propertyPaths);
|
464
|
}
|
465
|
|
466
|
taxa.add(associatedTaxon);
|
467
|
|
468
|
for (Taxon taxon : taxa) {
|
469
|
List<T> perTaxonOccurrences = dao.listByAssociatedTaxon(type, taxon, null, null, orderHints, propertyPaths);
|
470
|
for (SpecimenOrObservationBase<?> o : perTaxonOccurrences) {
|
471
|
occurrenceIds.add(o.getId());
|
472
|
}
|
473
|
}
|
474
|
occurrences = (List<T>) dao.loadList(occurrenceIds, null, propertyPaths);
|
475
|
|
476
|
return new DefaultPagerImpl<T>(pageNumber, Long.valueOf(occurrences.size()), pageSize, occurrences);
|
477
|
|
478
|
}
|
479
|
|
480
|
@Override
|
481
|
public <T extends SpecimenOrObservationBase> Pager<T> pageByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includeRelationships,
|
482
|
String taxonUUID, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
483
|
|
484
|
UUID uuid = UUID.fromString(taxonUUID);
|
485
|
Taxon tax = (Taxon) taxonService.load(uuid);
|
486
|
// TODO REMOVE NULL STATEMENT
|
487
|
type = null;
|
488
|
return pageByAssociatedTaxon(type, includeRelationships, tax, maxDepth, pageSize, pageNumber, orderHints, propertyPaths);
|
489
|
|
490
|
}
|
491
|
|
492
|
@Override
|
493
|
public List<FieldUnitDTO> findFieldUnitDTOByAssociatedTaxon(Set<TaxonRelationshipEdge> includedRelationships,
|
494
|
UUID associatedTaxonUuid, List<String> propertyPaths) {
|
495
|
|
496
|
Set<Taxon> taxa = new HashSet<>();
|
497
|
Set<Integer> occurrenceIds = new HashSet<>();
|
498
|
List<FieldUnitDTO> fieldUnitDTOs = new ArrayList<>();
|
499
|
HashMap<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedSpecimen = new HashMap<>();
|
500
|
List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
|
501
|
boolean includeUnpublished = INCLUDE_UNPUBLISHED;
|
502
|
|
503
|
// Integer limit = PagerUtils.limitFor(pageSize);
|
504
|
// Integer start = PagerUtils.startFor(pageSize, pageNumber);
|
505
|
|
506
|
Taxon associatedTaxon = (Taxon) taxonService.load(associatedTaxonUuid);
|
507
|
|
508
|
|
509
|
if (includedRelationships != null) {
|
510
|
taxa = taxonService.listRelatedTaxa(associatedTaxon, includedRelationships, null, includeUnpublished, null, null, null);
|
511
|
}
|
512
|
|
513
|
taxa.add(associatedTaxon);
|
514
|
|
515
|
for (Taxon taxon : taxa) {
|
516
|
List<SpecimenOrObservationBase> perTaxonOccurrences = dao.listByAssociatedTaxon(null,taxon, null, null, null, propertyPaths);
|
517
|
for (SpecimenOrObservationBase<?> o : perTaxonOccurrences) {
|
518
|
if (o.isInstanceOf(DerivedUnit.class)){
|
519
|
DerivedUnit derivedUnit;
|
520
|
SpecimenOrObservationBaseDTO derivedUnitDTO;
|
521
|
if (o.isInstanceOf(DnaSample.class)) {
|
522
|
derivedUnit = HibernateProxyHelper.deproxy(o, DnaSample.class);
|
523
|
derivedUnitDTO = new DNASampleDTO(derivedUnit);
|
524
|
} else {
|
525
|
derivedUnit = HibernateProxyHelper.deproxy(o, DerivedUnit.class);
|
526
|
derivedUnitDTO = new DerivedUnitDTO(derivedUnit);
|
527
|
}
|
528
|
if (alreadyCollectedSpecimen.get(derivedUnitDTO.getUuid()) == null){
|
529
|
alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
|
530
|
}
|
531
|
derivedUnitDTO.addAllDerivates(getDerivedUnitDTOsFor(derivedUnitDTO, derivedUnit, alreadyCollectedSpecimen));
|
532
|
this.findFieldUnitDTO(derivedUnitDTO, fieldUnitDTOs,
|
533
|
alreadyCollectedSpecimen);
|
534
|
}
|
535
|
}
|
536
|
|
537
|
}
|
538
|
|
539
|
return fieldUnitDTOs;
|
540
|
|
541
|
}
|
542
|
|
543
|
|
544
|
|
545
|
@Override
|
546
|
public FieldUnitDTO findByAccessionNumber(
|
547
|
String accessionNumberString, List<OrderHint> orderHints,
|
548
|
List<String> propertyPaths) {
|
549
|
|
550
|
DnaSample dnaSample = dao.findByGeneticAccessionNumber(accessionNumberString, propertyPaths);
|
551
|
SpecimenOrObservationBaseDTO derivedUnitDTO;
|
552
|
HashMap<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedSpecimen = new HashMap<>();
|
553
|
List<FieldUnitDTO> fieldUnitDTOs = new ArrayList<>();
|
554
|
if (dnaSample != null){
|
555
|
derivedUnitDTO = new DNASampleDTO(dnaSample);
|
556
|
alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
|
557
|
derivedUnitDTO.addAllDerivates(getDerivedUnitDTOsFor(derivedUnitDTO, dnaSample, alreadyCollectedSpecimen));
|
558
|
FieldUnitDTO fieldUnit = this.findFieldUnitDTO(derivedUnitDTO, fieldUnitDTOs,
|
559
|
alreadyCollectedSpecimen);
|
560
|
|
561
|
return fieldUnit;
|
562
|
}
|
563
|
return null;
|
564
|
|
565
|
}
|
566
|
|
567
|
@Override
|
568
|
public Pager<SearchResult<SpecimenOrObservationBase>> findByFullText(
|
569
|
Class<? extends SpecimenOrObservationBase> clazz, String queryString, Rectangle boundingBox, List<Language> languages,
|
570
|
boolean highlightFragments, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
|
571
|
List<String> propertyPaths) throws IOException, LuceneParseException {
|
572
|
|
573
|
LuceneSearch luceneSearch = prepareByFullTextSearch(clazz, queryString, boundingBox, languages, highlightFragments);
|
574
|
|
575
|
// --- execute search
|
576
|
TopGroups<BytesRef> topDocsResultSet;
|
577
|
try {
|
578
|
topDocsResultSet = luceneSearch.executeSearch(pageSize, pageNumber);
|
579
|
} catch (ParseException e) {
|
580
|
LuceneParseException parseException = new LuceneParseException(e.getMessage());
|
581
|
parseException.setStackTrace(e.getStackTrace());
|
582
|
throw parseException;
|
583
|
}
|
584
|
|
585
|
Map<CdmBaseType, String> idFieldMap = new HashMap<>();
|
586
|
idFieldMap.put(CdmBaseType.SPECIMEN_OR_OBSERVATIONBASE, "id");
|
587
|
|
588
|
// --- initialize taxa, highlight matches ....
|
589
|
ISearchResultBuilder searchResultBuilder = new SearchResultBuilder(luceneSearch, luceneSearch.getQuery());
|
590
|
@SuppressWarnings("rawtypes")
|
591
|
List<SearchResult<SpecimenOrObservationBase>> searchResults = searchResultBuilder.createResultSet(
|
592
|
topDocsResultSet, luceneSearch.getHighlightFields(), dao, idFieldMap, propertyPaths);
|
593
|
|
594
|
int totalHits = topDocsResultSet != null ? topDocsResultSet.totalGroupCount : 0;
|
595
|
|
596
|
return new DefaultPagerImpl<>(pageNumber, Long.valueOf(totalHits), pageSize, searchResults);
|
597
|
|
598
|
}
|
599
|
|
600
|
private LuceneSearch prepareByFullTextSearch(Class<? extends SpecimenOrObservationBase> clazz, String queryString, Rectangle bbox,
|
601
|
List<Language> languages, boolean highlightFragments) {
|
602
|
|
603
|
Builder finalQueryBuilder = new Builder();
|
604
|
Builder textQueryBuilder = new Builder();
|
605
|
|
606
|
LuceneSearch luceneSearch = new LuceneSearch(luceneIndexToolProvider, FieldUnit.class);
|
607
|
QueryFactory queryFactory = luceneIndexToolProvider.newQueryFactoryFor(FieldUnit.class);
|
608
|
|
609
|
// --- criteria
|
610
|
luceneSearch.setCdmTypRestriction(clazz);
|
611
|
if (queryString != null) {
|
612
|
textQueryBuilder.add(queryFactory.newTermQuery("titleCache", queryString), Occur.SHOULD);
|
613
|
finalQueryBuilder.add(textQueryBuilder.build(), Occur.MUST);
|
614
|
}
|
615
|
|
616
|
// --- spacial query
|
617
|
if (bbox != null) {
|
618
|
finalQueryBuilder.add(QueryFactory.buildSpatialQueryByRange(bbox, "gatheringEvent.exactLocation.point"), Occur.MUST);
|
619
|
}
|
620
|
|
621
|
luceneSearch.setQuery(finalQueryBuilder.build());
|
622
|
|
623
|
// --- sorting
|
624
|
SortField[] sortFields = new SortField[] { SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.Type.STRING, false) };
|
625
|
luceneSearch.setSortFields(sortFields);
|
626
|
|
627
|
if (highlightFragments) {
|
628
|
luceneSearch.setHighlightFields(queryFactory.getTextFieldNamesAsArray());
|
629
|
}
|
630
|
return luceneSearch;
|
631
|
}
|
632
|
|
633
|
|
634
|
@Override
|
635
|
@Transactional(readOnly=true)
|
636
|
public Collection<FieldUnit> findFieldUnits(UUID derivedUnitUuid, List<String> propertyPaths) {
|
637
|
//It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
|
638
|
//from which this DerivedUnit was derived until all FieldUnits are found.
|
639
|
|
640
|
// FIXME: use HQL queries to increase performance
|
641
|
|
642
|
SpecimenOrObservationBase<?> specimen = load(derivedUnitUuid, propertyPaths);
|
643
|
// specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
|
644
|
Collection<FieldUnit> fieldUnits = new ArrayList<>();
|
645
|
if (specimen == null){
|
646
|
return null;
|
647
|
}
|
648
|
if (specimen.isInstanceOf(FieldUnit.class)) {
|
649
|
fieldUnits.add(HibernateProxyHelper.deproxy(specimen, FieldUnit.class));
|
650
|
}
|
651
|
else if(specimen.isInstanceOf(DerivedUnit.class)){
|
652
|
fieldUnits.addAll(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class).collectFieldUnits());
|
653
|
}
|
654
|
|
655
|
fieldUnits = beanInitializer.initializeAll(fieldUnits, propertyPaths);
|
656
|
return fieldUnits;
|
657
|
}
|
658
|
|
659
|
|
660
|
@Override
|
661
|
@Transactional(readOnly=true)
|
662
|
public FieldUnitDTO findFieldUnitDTO(SpecimenOrObservationBaseDTO derivedUnitDTO, Collection<FieldUnitDTO> fieldUnits,
|
663
|
HashMap<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedSpecimen) {
|
664
|
//It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
|
665
|
//from which this DerivedUnit was derived until all FieldUnits are found.
|
666
|
List<SpecimenOrObservationBase> specimens = new ArrayList<>();
|
667
|
List<String> propertyPaths = new ArrayList<>();
|
668
|
|
669
|
// FIXME defining propertyPaths inside of transactional service method makes no sense. see also #9253
|
670
|
propertyPaths.add("descriptions.elements.media.title");
|
671
|
propertyPaths.add("kindOfUnit");
|
672
|
propertyPaths.add("derivedFrom");
|
673
|
|
674
|
specimens = dao.findOriginalsForDerivedUnit(derivedUnitDTO.getUuid(), propertyPaths);
|
675
|
|
676
|
if (specimens.size() > 1){
|
677
|
logger.debug("The derived unit with uuid " + derivedUnitDTO.getUuid() + "has more than one orginal");
|
678
|
}
|
679
|
// for (SpecimenOrObservationBase specimen: specimens){
|
680
|
SpecimenOrObservationBase specimen = null;
|
681
|
if (specimens.size() > 0){
|
682
|
specimen = specimens.get(0);
|
683
|
}else{
|
684
|
return null;
|
685
|
}
|
686
|
FieldUnitDTO fieldUnitDto = null;
|
687
|
if (alreadyCollectedSpecimen.get(specimen.getUuid()) != null){
|
688
|
alreadyCollectedSpecimen.get(specimen.getUuid()).addDerivate(derivedUnitDTO);
|
689
|
// if ( alreadyCollectedSpecimen.get(specimen.getUuid()) instanceof FieldUnitDTO){
|
690
|
// ((FieldUnitDTO)alreadyCollectedSpecimen.get(specimen.getUuid())).getTaxonRelatedDerivedUnits().add(derivedUnitDTO.getUuid());
|
691
|
// }
|
692
|
}else{
|
693
|
if (specimen.isInstanceOf(FieldUnit.class)){
|
694
|
fieldUnitDto = FieldUnitDTO.fromEntity((FieldUnit)specimen);
|
695
|
fieldUnitDto.addDerivate(derivedUnitDTO);
|
696
|
fieldUnits.add(fieldUnitDto);
|
697
|
}else{
|
698
|
SpecimenOrObservationBaseDTO originalDTO;
|
699
|
if (specimen instanceof DnaSample){
|
700
|
originalDTO = new DNASampleDTO((DnaSample)specimen);
|
701
|
} else {
|
702
|
originalDTO = new DerivedUnitDTO((DerivedUnit)specimen);
|
703
|
}
|
704
|
originalDTO.addDerivate(derivedUnitDTO);
|
705
|
fieldUnitDto = findFieldUnitDTO(originalDTO, fieldUnits,
|
706
|
alreadyCollectedSpecimen);
|
707
|
}
|
708
|
|
709
|
}
|
710
|
// }
|
711
|
alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
|
712
|
// if (fieldUnitDto != null){
|
713
|
// fieldUnitDto.addTaxonRelatedDerivedUnits(derivedUnitDTO);
|
714
|
// }
|
715
|
return fieldUnitDto;
|
716
|
|
717
|
}
|
718
|
|
719
|
@Override
|
720
|
@Transactional(readOnly=true)
|
721
|
public FieldUnitDTO loadFieldUnitDTO(UUID derivedUnitUuid) {
|
722
|
|
723
|
FieldUnitDTO fieldUnitDTO = null;
|
724
|
SpecimenOrObservationBaseDTO derivedUnitDTO = null;
|
725
|
|
726
|
Map<UUID, SpecimenOrObservationBaseDTO> cycleDetectionMap = new HashMap<>();
|
727
|
SpecimenOrObservationBase derivative = dao.load(derivedUnitUuid);
|
728
|
if(derivative != null){
|
729
|
if (derivative instanceof FieldUnit){
|
730
|
fieldUnitDTO = FieldUnitDTO.fromEntity((FieldUnit)derivative);
|
731
|
return fieldUnitDTO;
|
732
|
} else {
|
733
|
// must be a DerivedUnit otherwise
|
734
|
derivedUnitDTO = DerivedUnitDTO.fromEntity((DerivedUnit)derivative, null);
|
735
|
while(true){
|
736
|
|
737
|
Set<SpecimenOrObservationBaseDTO> originals = originalDTOs(derivedUnitDTO.getUuid());
|
738
|
|
739
|
if(originals.isEmpty()){
|
740
|
break;
|
741
|
}
|
742
|
if (originals.size() > 1){
|
743
|
logger.debug("The derived unit with uuid " + derivedUnitUuid + "has more than one orginal, ignoring all but the first one.");
|
744
|
}
|
745
|
|
746
|
SpecimenOrObservationBaseDTO originalDTO = originals.iterator().next();
|
747
|
|
748
|
// cycle detection and handling
|
749
|
if(cycleDetectionMap.containsKey(originalDTO.getUuid())){
|
750
|
// cycle detected!!!
|
751
|
try {
|
752
|
throw new Exception();
|
753
|
} catch(Exception e){
|
754
|
logger.error("Cycle in derivate graph detected at DerivedUnit with uuid=" + originalDTO.getUuid() , e);
|
755
|
}
|
756
|
// to solve the situation for the output we remove the derivate from the more distant graph node
|
757
|
// by removing it from the derivatives of its original
|
758
|
// but let the derivate to be added to the original which is closer to the FieldUnit (below at originalDTO.addDerivate(derivedUnitDTO);)
|
759
|
for(SpecimenOrObservationBaseDTO seenOriginal: cycleDetectionMap.values()){
|
760
|
for(SpecimenOrObservationBaseDTO derivateDTO : seenOriginal.getDerivates()){
|
761
|
if(derivateDTO.equals(originalDTO)){
|
762
|
seenOriginal.getDerivates().remove(originalDTO);
|
763
|
}
|
764
|
}
|
765
|
}
|
766
|
|
767
|
} else {
|
768
|
cycleDetectionMap.put(originalDTO.getUuid(), originalDTO);
|
769
|
}
|
770
|
|
771
|
|
772
|
if (originalDTO instanceof FieldUnitDTO){
|
773
|
fieldUnitDTO = (FieldUnitDTO)originalDTO;
|
774
|
if(derivedUnitDTO != null){
|
775
|
fieldUnitDTO.addDerivate(derivedUnitDTO);
|
776
|
}
|
777
|
break;
|
778
|
}else{
|
779
|
if (derivedUnitDTO == null){
|
780
|
derivedUnitDTO = originalDTO;
|
781
|
} else {
|
782
|
originalDTO.addDerivate(derivedUnitDTO);
|
783
|
derivedUnitDTO = originalDTO;
|
784
|
}
|
785
|
}
|
786
|
}
|
787
|
}
|
788
|
}
|
789
|
return fieldUnitDTO;
|
790
|
|
791
|
}
|
792
|
|
793
|
/**
|
794
|
* @param originalDTO
|
795
|
* @return
|
796
|
*/
|
797
|
private Set<SpecimenOrObservationBaseDTO> originalDTOs(UUID derivativeUuid) {
|
798
|
|
799
|
Set<SpecimenOrObservationBaseDTO> dtos = new HashSet<>();
|
800
|
List<SpecimenOrObservationBase> specimens = dao.findOriginalsForDerivedUnit(derivativeUuid, null);
|
801
|
for(SpecimenOrObservationBase sob : specimens){
|
802
|
if(sob instanceof FieldUnit) {
|
803
|
dtos.add(FieldUnitDTO.fromEntity((FieldUnit)sob));
|
804
|
} else {
|
805
|
dtos.add(DerivedUnitDTO.fromEntity((DerivedUnit)sob, null));
|
806
|
}
|
807
|
}
|
808
|
return dtos;
|
809
|
}
|
810
|
|
811
|
|
812
|
@Override
|
813
|
@Transactional(readOnly = false)
|
814
|
public UpdateResult moveSequence(DnaSample from, DnaSample to, Sequence sequence) {
|
815
|
return moveSequence(from.getUuid(), to.getUuid(), sequence.getUuid());
|
816
|
}
|
817
|
|
818
|
@Override
|
819
|
@Transactional(readOnly = false)
|
820
|
public UpdateResult moveSequence(UUID fromUuid, UUID toUuid, UUID sequenceUuid) {
|
821
|
// reload specimens to avoid session conflicts
|
822
|
DnaSample from = (DnaSample) load(fromUuid);
|
823
|
DnaSample to = (DnaSample) load(toUuid);
|
824
|
Sequence sequence = sequenceService.load(sequenceUuid);
|
825
|
|
826
|
if (from == null || to == null || sequence == null) {
|
827
|
throw new TransientObjectException("One of the CDM entities has not been saved to the data base yet. Moving only works for persisted/saved CDM entities.\n" +
|
828
|
"Operation was move "+sequence+ " from "+from+" to "+to);
|
829
|
}
|
830
|
UpdateResult result = new UpdateResult();
|
831
|
from.removeSequence(sequence);
|
832
|
saveOrUpdate(from);
|
833
|
to.addSequence(sequence);
|
834
|
saveOrUpdate(to);
|
835
|
result.setStatus(Status.OK);
|
836
|
result.addUpdatedObject(from);
|
837
|
result.addUpdatedObject(to);
|
838
|
return result;
|
839
|
}
|
840
|
|
841
|
@Override
|
842
|
@Transactional(readOnly = false)
|
843
|
public boolean moveDerivate(SpecimenOrObservationBase<?> from, SpecimenOrObservationBase<?> to, DerivedUnit derivate) {
|
844
|
return moveDerivate(from!=null?from.getUuid():null, to.getUuid(), derivate.getUuid()).isOk();
|
845
|
}
|
846
|
|
847
|
@Override
|
848
|
@Transactional(readOnly = false)
|
849
|
public UpdateResult moveDerivate(UUID specimenFromUuid, UUID specimenToUuid, UUID derivateUuid) {
|
850
|
// reload specimens to avoid session conflicts
|
851
|
SpecimenOrObservationBase<?> from = null;
|
852
|
if(specimenFromUuid!=null){
|
853
|
from = load(specimenFromUuid);
|
854
|
}
|
855
|
SpecimenOrObservationBase<?> to = load(specimenToUuid);
|
856
|
DerivedUnit derivate = (DerivedUnit) load(derivateUuid);
|
857
|
|
858
|
if ((specimenFromUuid!=null && from == null) || to == null || derivate == null) {
|
859
|
throw new TransientObjectException("One of the CDM entities has not been saved to the data base yet. Moving only works for persisted/saved CDM entities.\n" +
|
860
|
"Operation was move "+derivate+ " from "+from+" to "+to);
|
861
|
}
|
862
|
UpdateResult result = new UpdateResult();
|
863
|
SpecimenOrObservationType derivateType = derivate.getRecordBasis();
|
864
|
SpecimenOrObservationType toType = to.getRecordBasis();
|
865
|
// check if type is a sub derivate type
|
866
|
if(toType==SpecimenOrObservationType.FieldUnit //moving to FieldUnit always works
|
867
|
|| derivateType==SpecimenOrObservationType.Media //moving media always works
|
868
|
|| (derivateType.isKindOf(toType) && toType!=derivateType)){ //moving only to parent derivate type
|
869
|
if(from!=null){
|
870
|
// remove derivation event from parent specimen of dragged object
|
871
|
DerivationEvent eventToRemove = null;
|
872
|
for (DerivationEvent event : from.getDerivationEvents()) {
|
873
|
if (event.getDerivatives().contains(derivate)) {
|
874
|
eventToRemove = event;
|
875
|
break;
|
876
|
}
|
877
|
}
|
878
|
from.removeDerivationEvent(eventToRemove);
|
879
|
if(eventToRemove!=null){
|
880
|
// add new derivation event to target and copy the event parameters of the old one
|
881
|
DerivationEvent derivedFromNewOriginalEvent = DerivationEvent.NewSimpleInstance(to, derivate, null);
|
882
|
derivedFromNewOriginalEvent.setActor(eventToRemove.getActor());
|
883
|
derivedFromNewOriginalEvent.setDescription(eventToRemove.getDescription());
|
884
|
derivedFromNewOriginalEvent.setInstitution(eventToRemove.getInstitution());
|
885
|
derivedFromNewOriginalEvent.setTimeperiod(eventToRemove.getTimeperiod());
|
886
|
derivedFromNewOriginalEvent.setType(eventToRemove.getType());
|
887
|
to.addDerivationEvent(derivedFromNewOriginalEvent);
|
888
|
derivate.setDerivedFrom(derivedFromNewOriginalEvent);
|
889
|
}
|
890
|
}
|
891
|
else{
|
892
|
//derivative had no parent before so we use empty derivation event
|
893
|
DerivationEvent derivedFromNewOriginalEvent = DerivationEvent.NewSimpleInstance(to, derivate, null);
|
894
|
to.addDerivationEvent(derivedFromNewOriginalEvent);
|
895
|
derivate.setDerivedFrom(derivedFromNewOriginalEvent);
|
896
|
}
|
897
|
|
898
|
if(from!=null){
|
899
|
saveOrUpdate(from);
|
900
|
}
|
901
|
saveOrUpdate(to);
|
902
|
result.setStatus(Status.OK);
|
903
|
result.addUpdatedObject(from);
|
904
|
result.addUpdatedObject(to);
|
905
|
} else {
|
906
|
result.setStatus(Status.ERROR);
|
907
|
}
|
908
|
return result;
|
909
|
}
|
910
|
|
911
|
@Override
|
912
|
public DeleteResult isDeletable(UUID specimenUuid, DeleteConfiguratorBase config) {
|
913
|
DeleteResult deleteResult = new DeleteResult();
|
914
|
SpecimenOrObservationBase specimen = this.load(specimenUuid);
|
915
|
SpecimenDeleteConfigurator specimenDeleteConfigurator = (SpecimenDeleteConfigurator) config;
|
916
|
|
917
|
// check elements found by super method
|
918
|
Set<CdmBase> relatedObjects = super.isDeletable(specimenUuid, config).getRelatedObjects();
|
919
|
for (CdmBase cdmBase : relatedObjects) {
|
920
|
// check for type designation
|
921
|
if (cdmBase.isInstanceOf(SpecimenTypeDesignation.class) && !specimenDeleteConfigurator.isDeleteFromTypeDesignation()) {
|
922
|
deleteResult.setAbort();
|
923
|
deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is a type specimen."));
|
924
|
deleteResult.addRelatedObject(cdmBase);
|
925
|
break;
|
926
|
}
|
927
|
// check for IndividualsAssociations
|
928
|
else if (cdmBase.isInstanceOf(IndividualsAssociation.class) && !specimenDeleteConfigurator.isDeleteFromIndividualsAssociation()) {
|
929
|
deleteResult.setAbort();
|
930
|
deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is still associated via IndividualsAssociations"));
|
931
|
deleteResult.addRelatedObject(cdmBase);
|
932
|
break;
|
933
|
}
|
934
|
// check for taxon description
|
935
|
else if(cdmBase.isInstanceOf(TaxonDescription.class)
|
936
|
&& HibernateProxyHelper.deproxy(cdmBase, TaxonDescription.class).getDescribedSpecimenOrObservation().equals(specimen)
|
937
|
&& !specimenDeleteConfigurator.isDeleteFromDescription()){
|
938
|
deleteResult.setAbort();
|
939
|
deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is still used as \"Described Specimen\" in a taxon description."));
|
940
|
deleteResult.addRelatedObject(cdmBase);
|
941
|
break;
|
942
|
}
|
943
|
// check for children and parents (derivation events)
|
944
|
else if (cdmBase.isInstanceOf(DerivationEvent.class)) {
|
945
|
DerivationEvent derivationEvent = HibernateProxyHelper.deproxy(cdmBase, DerivationEvent.class);
|
946
|
// check if derivation event is empty
|
947
|
if (!derivationEvent.getDerivatives().isEmpty() && derivationEvent.getOriginals().contains(specimen)) {
|
948
|
// if derivationEvent is the childEvent and contains derivations
|
949
|
// if (derivationEvent.getDerivatives().contains(specimen)) {
|
950
|
// //if it is the parent event the specimen is still deletable
|
951
|
// continue;
|
952
|
// }
|
953
|
if(!specimenDeleteConfigurator.isDeleteChildren()){
|
954
|
//if children should not be deleted then it is undeletable
|
955
|
deleteResult.setAbort();
|
956
|
deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation still has child derivatives."));
|
957
|
deleteResult.addRelatedObject(cdmBase);
|
958
|
break;
|
959
|
}
|
960
|
else{
|
961
|
// check all children if they can be deleted
|
962
|
Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
|
963
|
DeleteResult childResult = new DeleteResult();
|
964
|
for (DerivedUnit derivedUnit : derivatives) {
|
965
|
childResult.includeResult(isDeletable(derivedUnit.getUuid(), specimenDeleteConfigurator));
|
966
|
}
|
967
|
if (!childResult.isOk()) {
|
968
|
deleteResult.setAbort();
|
969
|
deleteResult.includeResult(childResult);
|
970
|
deleteResult.addRelatedObject(cdmBase);
|
971
|
break;
|
972
|
}
|
973
|
}
|
974
|
}
|
975
|
}
|
976
|
// check for amplification
|
977
|
else if (cdmBase.isInstanceOf(AmplificationResult.class)
|
978
|
&& !specimenDeleteConfigurator.isDeleteMolecularData()
|
979
|
&& !specimenDeleteConfigurator.isDeleteChildren()) {
|
980
|
deleteResult.setAbort();
|
981
|
deleteResult.addException(new ReferencedObjectUndeletableException("DnaSample is used in amplification results."));
|
982
|
deleteResult.addRelatedObject(cdmBase);
|
983
|
break;
|
984
|
}
|
985
|
// check for sequence
|
986
|
else if (cdmBase.isInstanceOf(Sequence.class)
|
987
|
&& !specimenDeleteConfigurator.isDeleteMolecularData()
|
988
|
&& !specimenDeleteConfigurator.isDeleteChildren()) {
|
989
|
deleteResult.setAbort();
|
990
|
deleteResult.addException(new ReferencedObjectUndeletableException("DnaSample is used in sequences."));
|
991
|
deleteResult.addRelatedObject(cdmBase);
|
992
|
break;
|
993
|
}
|
994
|
}
|
995
|
if (deleteResult.isOk()) {
|
996
|
//add all related object if deletion is OK so they can be handled by the delete() method
|
997
|
deleteResult.addRelatedObjects(relatedObjects);
|
998
|
}
|
999
|
return deleteResult;
|
1000
|
}
|
1001
|
|
1002
|
/**
|
1003
|
* {@inheritDoc}
|
1004
|
*/
|
1005
|
@Transactional(readOnly = false)
|
1006
|
@Override
|
1007
|
public DeleteResult delete(UUID specimenUuid, SpecimenDeleteConfigurator config) {
|
1008
|
return delete(load(specimenUuid), config);
|
1009
|
}
|
1010
|
|
1011
|
|
1012
|
@Transactional(readOnly = false)
|
1013
|
@Override
|
1014
|
public DeleteResult delete(SpecimenOrObservationBase<?> specimen, SpecimenDeleteConfigurator config) {
|
1015
|
specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
|
1016
|
|
1017
|
DeleteResult deleteResult = isDeletable(specimen.getUuid(), config);
|
1018
|
if (!deleteResult.isOk()) {
|
1019
|
return deleteResult;
|
1020
|
}
|
1021
|
|
1022
|
if (config.isDeleteChildren()) {
|
1023
|
Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
|
1024
|
//clone to avoid concurrent modification
|
1025
|
//can happen if the child is deleted and deleted its own derivedFrom event
|
1026
|
Set<DerivationEvent> derivationEventsClone = new HashSet<>(derivationEvents);
|
1027
|
for (DerivationEvent derivationEvent : derivationEventsClone) {
|
1028
|
Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
|
1029
|
Iterator<DerivedUnit> it = derivatives.iterator();
|
1030
|
Set<DerivedUnit> derivativesToDelete = new HashSet<>();
|
1031
|
while (it.hasNext()) {
|
1032
|
DerivedUnit unit = it.next();
|
1033
|
derivativesToDelete.add(unit);
|
1034
|
}
|
1035
|
for (DerivedUnit unit:derivativesToDelete){
|
1036
|
deleteResult.includeResult(delete(unit, config));
|
1037
|
}
|
1038
|
}
|
1039
|
}
|
1040
|
|
1041
|
|
1042
|
|
1043
|
|
1044
|
// check related objects
|
1045
|
Set<CdmBase> relatedObjects = deleteResult.getRelatedObjects();
|
1046
|
|
1047
|
for (CdmBase relatedObject : relatedObjects) {
|
1048
|
// check for TypeDesignations
|
1049
|
if (relatedObject.isInstanceOf(SpecimenTypeDesignation.class)) {
|
1050
|
SpecimenTypeDesignation designation = HibernateProxyHelper.deproxy(relatedObject, SpecimenTypeDesignation.class);
|
1051
|
designation.setTypeSpecimen(null);
|
1052
|
List<TaxonName> typifiedNames = new ArrayList<>();
|
1053
|
typifiedNames.addAll(designation.getTypifiedNames());
|
1054
|
for (TaxonName taxonName : typifiedNames) {
|
1055
|
taxonName.removeTypeDesignation(designation);
|
1056
|
}
|
1057
|
}
|
1058
|
// delete IndividualsAssociation
|
1059
|
if (relatedObject.isInstanceOf(IndividualsAssociation.class)) {
|
1060
|
IndividualsAssociation association = HibernateProxyHelper.deproxy(relatedObject, IndividualsAssociation.class);
|
1061
|
association.setAssociatedSpecimenOrObservation(null);
|
1062
|
association.getInDescription().removeElement(association);
|
1063
|
}
|
1064
|
// check for "described specimen" (deprecated)
|
1065
|
if (relatedObject.isInstanceOf(TaxonDescription.class)) {
|
1066
|
TaxonDescription description = HibernateProxyHelper.deproxy(relatedObject, TaxonDescription.class);
|
1067
|
description.setDescribedSpecimenOrObservation(null);
|
1068
|
}
|
1069
|
// check for specimen description
|
1070
|
if (relatedObject.isInstanceOf(SpecimenDescription.class)) {
|
1071
|
SpecimenDescription specimenDescription = HibernateProxyHelper.deproxy(relatedObject, SpecimenDescription.class);
|
1072
|
specimenDescription.setDescribedSpecimenOrObservation(null);
|
1073
|
// check if description is a description of the given specimen
|
1074
|
if (specimen.getDescriptions().contains(specimenDescription)) {
|
1075
|
specimen.removeDescription(specimenDescription);
|
1076
|
}
|
1077
|
DeleteResult descriptionDelete = descriptionService.isDeletable(specimenDescription.getUuid(), null);
|
1078
|
if (descriptionDelete.isOk()){
|
1079
|
deleteResult.includeResult(descriptionService.delete(specimenDescription));
|
1080
|
}
|
1081
|
}
|
1082
|
// check for amplification
|
1083
|
if (relatedObject.isInstanceOf(AmplificationResult.class)) {
|
1084
|
AmplificationResult amplificationResult = HibernateProxyHelper.deproxy(relatedObject, AmplificationResult.class);
|
1085
|
amplificationResult.getDnaSample().removeAmplificationResult(amplificationResult);
|
1086
|
}
|
1087
|
// check for sequence
|
1088
|
if (relatedObject.isInstanceOf(Sequence.class)) {
|
1089
|
Sequence sequence = HibernateProxyHelper.deproxy(relatedObject, Sequence.class);
|
1090
|
sequence.getDnaSample().removeSequence(sequence);
|
1091
|
}
|
1092
|
// check for children and parents (derivation events)
|
1093
|
if (relatedObject.isInstanceOf(DerivationEvent.class)) {
|
1094
|
DerivationEvent derivationEvent = HibernateProxyHelper.deproxy(relatedObject, DerivationEvent.class);
|
1095
|
// parent derivation event (derivedFrom)
|
1096
|
if (derivationEvent.getDerivatives().contains(specimen) && specimen.isInstanceOf(DerivedUnit.class)) {
|
1097
|
derivationEvent.removeDerivative(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class));
|
1098
|
if (derivationEvent.getDerivatives().isEmpty()) {
|
1099
|
Set<SpecimenOrObservationBase> originals = derivationEvent.getOriginals();
|
1100
|
for (SpecimenOrObservationBase specimenOrObservationBase : originals) {
|
1101
|
specimenOrObservationBase.removeDerivationEvent(derivationEvent);
|
1102
|
deleteResult.addUpdatedObject(specimenOrObservationBase);
|
1103
|
}
|
1104
|
// if derivationEvent has no derivates anymore, delete it
|
1105
|
deleteResult.includeResult(eventService.delete(derivationEvent));
|
1106
|
}
|
1107
|
}
|
1108
|
else{
|
1109
|
//child derivation events should not occur since we delete the hierarchy from bottom to top
|
1110
|
}
|
1111
|
}
|
1112
|
}
|
1113
|
if (specimen instanceof FieldUnit){
|
1114
|
FieldUnit fieldUnit = HibernateProxyHelper.deproxy(specimen, FieldUnit.class);
|
1115
|
GatheringEvent event = fieldUnit.getGatheringEvent();
|
1116
|
fieldUnit.setGatheringEvent(null);
|
1117
|
if (event != null){
|
1118
|
DeleteResult result = eventService.isDeletable(event.getUuid(), null);
|
1119
|
if (result.isOk()){
|
1120
|
deleteResult.includeResult( eventService.delete(event));
|
1121
|
}
|
1122
|
}
|
1123
|
|
1124
|
}
|
1125
|
deleteResult.includeResult(delete(specimen));
|
1126
|
|
1127
|
return deleteResult;
|
1128
|
}
|
1129
|
|
1130
|
@Override
|
1131
|
public Collection<IndividualsAssociation> listIndividualsAssociations(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1132
|
return dao.listIndividualsAssociations(specimen, limit, start, orderHints, propertyPaths);
|
1133
|
}
|
1134
|
|
1135
|
/**
|
1136
|
* {@inheritDoc}
|
1137
|
*/
|
1138
|
@Override
|
1139
|
public Collection<TaxonBase<?>> listAssociatedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
|
1140
|
Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1141
|
return listAssociatedTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
|
1142
|
}
|
1143
|
@Override
|
1144
|
public Collection<TaxonBase<?>> listAssociatedTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished,
|
1145
|
Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1146
|
Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
|
1147
|
|
1148
|
//individuals associations
|
1149
|
associatedTaxa.addAll(listIndividualsAssociationTaxa(specimen, includeUnpublished, limit, start, orderHints, propertyPaths));
|
1150
|
//type designation
|
1151
|
if(specimen.isInstanceOf(DerivedUnit.class)){
|
1152
|
associatedTaxa.addAll(listTypeDesignationTaxa(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class),
|
1153
|
includeUnpublished, limit, start, orderHints, propertyPaths));
|
1154
|
}
|
1155
|
//determinations
|
1156
|
associatedTaxa.addAll(listDeterminedTaxa(specimen, includeUnpublished, limit, start, orderHints, propertyPaths));
|
1157
|
|
1158
|
return associatedTaxa;
|
1159
|
}
|
1160
|
|
1161
|
|
1162
|
|
1163
|
/**
|
1164
|
* {@inheritDoc}
|
1165
|
*/
|
1166
|
@Override
|
1167
|
public Collection<TaxonBase<?>> listDeterminedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
|
1168
|
Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1169
|
return listDeterminedTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
|
1170
|
}
|
1171
|
@Override
|
1172
|
public Collection<TaxonBase<?>> listDeterminedTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished, Integer limit, Integer start,
|
1173
|
List<OrderHint> orderHints, List<String> propertyPaths) {
|
1174
|
Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
|
1175
|
for (DeterminationEvent determinationEvent : listDeterminationEvents(specimen, limit, start, orderHints, propertyPaths)) {
|
1176
|
if(determinationEvent.getIdentifiedUnit().equals(specimen)){
|
1177
|
if(determinationEvent.getTaxon()!=null){
|
1178
|
associatedTaxa.add(taxonService.load(determinationEvent.getTaxon().getUuid(), includeUnpublished, propertyPaths));
|
1179
|
}
|
1180
|
if(determinationEvent.getTaxonName()!=null){
|
1181
|
Collection<TaxonBase> taxonBases = determinationEvent.getTaxonName().getTaxonBases();
|
1182
|
for (TaxonBase taxonBase : taxonBases) {
|
1183
|
associatedTaxa.add(taxonService.load(taxonBase.getUuid(), includeUnpublished, propertyPaths));
|
1184
|
}
|
1185
|
}
|
1186
|
}
|
1187
|
}
|
1188
|
return associatedTaxa;
|
1189
|
}
|
1190
|
|
1191
|
/**
|
1192
|
* {@inheritDoc}
|
1193
|
*/
|
1194
|
@Override
|
1195
|
public Collection<TaxonBase<?>> listTypeDesignationTaxa(DerivedUnit specimen, Integer limit, Integer start,
|
1196
|
List<OrderHint> orderHints, List<String> propertyPaths) {
|
1197
|
return listTypeDesignationTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
|
1198
|
}
|
1199
|
@Override
|
1200
|
public Collection<TaxonBase<?>> listTypeDesignationTaxa(DerivedUnit specimen, boolean includeUnpublished,
|
1201
|
Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1202
|
Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
|
1203
|
for (SpecimenTypeDesignation typeDesignation : listTypeDesignations(specimen, limit, start, orderHints, propertyPaths)) {
|
1204
|
if(typeDesignation.getTypeSpecimen().equals(specimen)){
|
1205
|
Set<TaxonName> typifiedNames = typeDesignation.getTypifiedNames();
|
1206
|
for (TaxonName taxonName : typifiedNames) {
|
1207
|
Set<Taxon> taxa = taxonName.getTaxa();
|
1208
|
for (Taxon taxon : taxa) {
|
1209
|
associatedTaxa.add(taxonService.load(taxon.getUuid(), includeUnpublished, propertyPaths));
|
1210
|
}
|
1211
|
}
|
1212
|
}
|
1213
|
}
|
1214
|
return associatedTaxa;
|
1215
|
}
|
1216
|
|
1217
|
/**
|
1218
|
* {@inheritDoc}
|
1219
|
*/
|
1220
|
@Override
|
1221
|
public Collection<TaxonBase<?>> listIndividualsAssociationTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
|
1222
|
Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1223
|
return listIndividualsAssociationTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
|
1224
|
}
|
1225
|
|
1226
|
@Override
|
1227
|
public Collection<TaxonBase<?>> listIndividualsAssociationTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished,
|
1228
|
Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1229
|
Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
|
1230
|
for (IndividualsAssociation individualsAssociation : listIndividualsAssociations(specimen, null, null, null, null)) {
|
1231
|
if(individualsAssociation.getInDescription().isInstanceOf(TaxonDescription.class)){
|
1232
|
TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(individualsAssociation.getInDescription(), TaxonDescription.class);
|
1233
|
if(taxonDescription.getTaxon()!=null){
|
1234
|
associatedTaxa.add(taxonService.load(taxonDescription.getTaxon().getUuid(), includeUnpublished, propertyPaths));
|
1235
|
}
|
1236
|
}
|
1237
|
}
|
1238
|
return associatedTaxa;
|
1239
|
}
|
1240
|
|
1241
|
@Override
|
1242
|
public Collection<DeterminationEvent> listDeterminationEvents(SpecimenOrObservationBase<?> specimen,
|
1243
|
Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1244
|
return dao.listDeterminationEvents(specimen, limit, start, orderHints, propertyPaths);
|
1245
|
}
|
1246
|
|
1247
|
@Override
|
1248
|
public Map<DerivedUnit, Collection<SpecimenTypeDesignation>> listTypeDesignations(
|
1249
|
Collection<DerivedUnit> specimens, Integer limit, Integer start,
|
1250
|
List<OrderHint> orderHints, List<String> propertyPaths) {
|
1251
|
Map<DerivedUnit, Collection<SpecimenTypeDesignation>> typeDesignationMap = new HashMap<>();
|
1252
|
for (DerivedUnit specimen : specimens) {
|
1253
|
Collection<SpecimenTypeDesignation> typeDesignations = listTypeDesignations(specimen, limit, start, orderHints, propertyPaths);
|
1254
|
typeDesignationMap.put(specimen, typeDesignations);
|
1255
|
}
|
1256
|
return typeDesignationMap;
|
1257
|
}
|
1258
|
|
1259
|
@Override
|
1260
|
public Collection<SpecimenTypeDesignation> listTypeDesignations(DerivedUnit specimen,
|
1261
|
Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
|
1262
|
return dao.listTypeDesignations(specimen, limit, start, orderHints, propertyPaths);
|
1263
|
}
|
1264
|
|
1265
|
@Override
|
1266
|
public Collection<DescriptionBase<?>> listDescriptionsWithDescriptionSpecimen(
|
1267
|
SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints,
|
1268
|
List<String> propertyPaths) {
|
1269
|
return dao.listDescriptionsWithDescriptionSpecimen(specimen, limit, start, orderHints, propertyPaths);
|
1270
|
}
|
1271
|
|
1272
|
@Override
|
1273
|
public Collection<DescriptionElementBase> getCharacterDataForSpecimen(UUID specimenUuid) {
|
1274
|
SpecimenOrObservationBase<?> specimen = load(specimenUuid);
|
1275
|
if (specimen != null) {
|
1276
|
return specimen.characterData();
|
1277
|
}
|
1278
|
else{
|
1279
|
throw new DataRetrievalFailureException("Specimen with the given uuid not found in the data base");
|
1280
|
}
|
1281
|
}
|
1282
|
|
1283
|
@Override
|
1284
|
public long countByTitle(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config){
|
1285
|
if (config instanceof FindOccurrencesConfigurator) {
|
1286
|
FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
|
1287
|
Taxon taxon = null;
|
1288
|
if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
|
1289
|
TaxonBase<?> taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
|
1290
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
1291
|
taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
|
1292
|
}
|
1293
|
}
|
1294
|
TaxonName taxonName = null;
|
1295
|
if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
|
1296
|
taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
|
1297
|
}
|
1298
|
/*TODO: #6484 Neither isRetrieveIndirectlyAssociatedSpecimens() nor the AssignmentStatus
|
1299
|
* is currently reflected in the HQL query. So using these in the count method will
|
1300
|
* significantly slow down this method as we have to retrieve the entities instead of
|
1301
|
* just the amount.
|
1302
|
*/
|
1303
|
if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens() || !occurrenceConfig.getAssignmentStatus().equals(AssignmentStatus.ALL_SPECIMENS)){
|
1304
|
List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
|
1305
|
occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
|
1306
|
occurrenceConfig.getTitleSearchStringSqlized(), occurrenceConfig.getSignificantIdentifier(),
|
1307
|
occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
|
1308
|
occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
|
1309
|
occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
|
1310
|
return occurrences.size();
|
1311
|
}
|
1312
|
|
1313
|
return dao.countOccurrences(occurrenceConfig.getClazz(),
|
1314
|
occurrenceConfig.getTitleSearchStringSqlized(), occurrenceConfig.getSignificantIdentifier(),
|
1315
|
occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
|
1316
|
occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
|
1317
|
}
|
1318
|
else{
|
1319
|
return super.countByTitle(config);
|
1320
|
}
|
1321
|
}
|
1322
|
|
1323
|
@Override
|
1324
|
public Pager<UuidAndTitleCache<SpecimenOrObservationBase>> findByTitleUuidAndTitleCache(
|
1325
|
FindOccurrencesConfigurator config){
|
1326
|
List<UuidAndTitleCache<SpecimenOrObservationBase>> occurrences = new ArrayList<>();
|
1327
|
Taxon taxon = null;
|
1328
|
if(config.getAssociatedTaxonUuid()!=null){
|
1329
|
TaxonBase<?> taxonBase = taxonService.load(config.getAssociatedTaxonUuid());
|
1330
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
1331
|
taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
1332
|
}
|
1333
|
}
|
1334
|
TaxonName taxonName = null;
|
1335
|
if(config.getAssociatedTaxonNameUuid()!=null){
|
1336
|
taxonName = nameService.load(config.getAssociatedTaxonNameUuid());
|
1337
|
}
|
1338
|
occurrences.addAll(dao.findOccurrencesUuidAndTitleCache(config.getClazz(),
|
1339
|
config.getTitleSearchString(), config.getSignificantIdentifier(),
|
1340
|
config.getSpecimenType(), taxon, taxonName, config.getMatchMode(), null, null,
|
1341
|
config.getOrderHints()));
|
1342
|
|
1343
|
return new DefaultPagerImpl<>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
|
1344
|
}
|
1345
|
|
1346
|
@Override
|
1347
|
public List<DerivedUnitDTO> findByTitleDerivedUnitDTO(FindOccurrencesConfigurator config) {
|
1348
|
Taxon taxon = null;
|
1349
|
if(config.getAssociatedTaxonUuid()!=null){
|
1350
|
TaxonBase<?> taxonBase = taxonService.load(config.getAssociatedTaxonUuid());
|
1351
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
1352
|
taxon = CdmBase.deproxy(taxonBase, Taxon.class);
|
1353
|
}
|
1354
|
}
|
1355
|
TaxonName taxonName = null;
|
1356
|
if(config.getAssociatedTaxonNameUuid()!=null){
|
1357
|
taxonName = nameService.load(config.getAssociatedTaxonNameUuid());
|
1358
|
}
|
1359
|
List<DerivedUnit> occurrences = new ArrayList<>();
|
1360
|
occurrences.addAll(dao.findOccurrences(DerivedUnit.class,
|
1361
|
config.getTitleSearchString(), config.getSignificantIdentifier(),
|
1362
|
config.getSpecimenType(), taxon, taxonName, config.getMatchMode(), null, null,
|
1363
|
config.getOrderHints(), null));
|
1364
|
|
1365
|
List<DerivedUnitDTO> dtos = new ArrayList<>();
|
1366
|
occurrences.forEach(derivedUnit->dtos.add(assembleDerivedUnitDTO(derivedUnit)));
|
1367
|
return dtos;
|
1368
|
}
|
1369
|
|
1370
|
@Override
|
1371
|
public <S extends SpecimenOrObservationBase> Pager<S> findByTitle(
|
1372
|
IIdentifiableEntityServiceConfigurator<S> config) {
|
1373
|
if (config instanceof FindOccurrencesConfigurator) {
|
1374
|
FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
|
1375
|
List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
|
1376
|
Taxon taxon = null;
|
1377
|
if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
|
1378
|
TaxonBase<?> taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
|
1379
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
1380
|
taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
|
1381
|
}
|
1382
|
}
|
1383
|
TaxonName taxonName = null;
|
1384
|
if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
|
1385
|
taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
|
1386
|
}
|
1387
|
List<? extends SpecimenOrObservationBase> foundOccurrences = dao.findOccurrences(occurrenceConfig.getClazz(),
|
1388
|
occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
|
1389
|
occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
|
1390
|
occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
|
1391
|
occurrences.addAll(foundOccurrences);
|
1392
|
occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
|
1393
|
|
1394
|
return new DefaultPagerImpl<>(config.getPageNumber(), occurrences.size(), config.getPageSize(), (List<S>)occurrences);
|
1395
|
}
|
1396
|
return super.findByTitle(config);
|
1397
|
}
|
1398
|
|
1399
|
private List<SpecimenOrObservationBase> filterOccurencesByAssignmentAndHierarchy(
|
1400
|
FindOccurrencesConfigurator occurrenceConfig, List<SpecimenOrObservationBase> occurrences, Taxon taxon,
|
1401
|
TaxonName taxonName) {
|
1402
|
//filter out (un-)assigned specimens
|
1403
|
if(taxon==null && taxonName==null){
|
1404
|
AssignmentStatus assignmentStatus = occurrenceConfig.getAssignmentStatus();
|
1405
|
List<SpecimenOrObservationBase> specimenWithAssociations = new ArrayList<>();
|
1406
|
if(!assignmentStatus.equals(AssignmentStatus.ALL_SPECIMENS)){
|
1407
|
for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
|
1408
|
boolean includeUnpublished = true; //TODO not sure if this is correct, maybe we have to propagate publish flag to higher methods.
|
1409
|
Collection<TaxonBase<?>> associatedTaxa = listAssociatedTaxa(specimenOrObservationBase,
|
1410
|
includeUnpublished, null, null, null, null);
|
1411
|
if(!associatedTaxa.isEmpty()){
|
1412
|
specimenWithAssociations.add(specimenOrObservationBase);
|
1413
|
}
|
1414
|
}
|
1415
|
}
|
1416
|
if(assignmentStatus.equals(AssignmentStatus.UNASSIGNED_SPECIMENS)){
|
1417
|
occurrences.removeAll(specimenWithAssociations);
|
1418
|
}
|
1419
|
if(assignmentStatus.equals(AssignmentStatus.ASSIGNED_SPECIMENS)){
|
1420
|
occurrences = new ArrayList<>(specimenWithAssociations);
|
1421
|
}
|
1422
|
}
|
1423
|
// indirectly associated specimens
|
1424
|
if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens()){
|
1425
|
List<SpecimenOrObservationBase> indirectlyAssociatedOccurrences = new ArrayList<>(occurrences);
|
1426
|
for (SpecimenOrObservationBase<?> specimen : occurrences) {
|
1427
|
List<SpecimenOrObservationBase<?>> allHierarchyDerivates = getAllHierarchyDerivatives(specimen);
|
1428
|
for (SpecimenOrObservationBase<?> specimenOrObservationBase : allHierarchyDerivates) {
|
1429
|
if(!occurrences.contains(specimenOrObservationBase)){
|
1430
|
indirectlyAssociatedOccurrences.add(specimenOrObservationBase);
|
1431
|
}
|
1432
|
}
|
1433
|
}
|
1434
|
occurrences = indirectlyAssociatedOccurrences;
|
1435
|
}
|
1436
|
return occurrences;
|
1437
|
}
|
1438
|
|
1439
|
@Override
|
1440
|
public List<SpecimenOrObservationBase<?>> getAllHierarchyDerivatives(SpecimenOrObservationBase<?> specimen){
|
1441
|
List<SpecimenOrObservationBase<?>> allHierarchyDerivatives = new ArrayList<>();
|
1442
|
Collection<FieldUnit> fieldUnits = findFieldUnits(specimen.getUuid(), null);
|
1443
|
if(fieldUnits.isEmpty()){
|
1444
|
allHierarchyDerivatives.add(specimen);
|
1445
|
allHierarchyDerivatives.addAll(getAllChildDerivatives(specimen));
|
1446
|
}
|
1447
|
else{
|
1448
|
for (FieldUnit fieldUnit : fieldUnits) {
|
1449
|
allHierarchyDerivatives.add(fieldUnit);
|
1450
|
allHierarchyDerivatives.addAll(getAllChildDerivatives(fieldUnit));
|
1451
|
}
|
1452
|
}
|
1453
|
return allHierarchyDerivatives;
|
1454
|
}
|
1455
|
|
1456
|
@Override
|
1457
|
public List<DerivedUnit> getAllChildDerivatives(UUID specimenUuid){
|
1458
|
return getAllChildDerivatives(load(specimenUuid));
|
1459
|
}
|
1460
|
|
1461
|
@Override
|
1462
|
public List<DerivedUnit> getAllChildDerivatives(SpecimenOrObservationBase<?> specimen){
|
1463
|
if (specimen == null){
|
1464
|
return null;
|
1465
|
}
|
1466
|
List<DerivedUnit> childDerivate = new ArrayList<>();
|
1467
|
Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
|
1468
|
for (DerivationEvent derivationEvent : derivationEvents) {
|
1469
|
Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
|
1470
|
for (DerivedUnit derivedUnit : derivatives) {
|
1471
|
childDerivate.add(derivedUnit);
|
1472
|
childDerivate.addAll(getAllChildDerivatives(derivedUnit.getUuid()));
|
1473
|
}
|
1474
|
}
|
1475
|
return childDerivate;
|
1476
|
}
|
1477
|
|
1478
|
@Override
|
1479
|
public long countOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config){
|
1480
|
return countByTitle(config);
|
1481
|
}
|
1482
|
|
1483
|
/**
|
1484
|
* {@inheritDoc}
|
1485
|
*/
|
1486
|
@Override
|
1487
|
public List<FieldUnit> findFieldUnitsForGatheringEvent(UUID gatheringEventUuid) {
|
1488
|
return dao.findFieldUnitsForGatheringEvent(gatheringEventUuid, null, null, null, null);
|
1489
|
}
|
1490
|
|
1491
|
|
1492
|
/**
|
1493
|
* {@inheritDoc}
|
1494
|
*/
|
1495
|
@Override
|
1496
|
public List<Point> findPointsForFieldUnitList(List<UUID> fieldUnitUuids) {
|
1497
|
|
1498
|
return dao.findPointsForFieldUnitList(fieldUnitUuids);
|
1499
|
}
|
1500
|
}
|