Project

General

Profile

Download (78.1 KB) Statistics
| Branch: | Tag: | Revision:
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.net.URI;
14
import java.net.URISyntaxException;
15
import java.util.ArrayList;
16
import java.util.Arrays;
17
import java.util.Collection;
18
import java.util.Collections;
19
import java.util.HashMap;
20
import java.util.HashSet;
21
import java.util.Iterator;
22
import java.util.List;
23
import java.util.Map;
24
import java.util.Map.Entry;
25
import java.util.Set;
26
import java.util.UUID;
27

    
28
import org.apache.log4j.Logger;
29
import org.apache.lucene.queryparser.classic.ParseException;
30
import org.apache.lucene.search.BooleanClause.Occur;
31
import org.apache.lucene.search.BooleanQuery.Builder;
32
import org.apache.lucene.search.SortField;
33
import org.apache.lucene.search.grouping.TopGroups;
34
import org.apache.lucene.util.BytesRef;
35
import org.hibernate.TransientObjectException;
36
import org.hibernate.search.spatial.impl.Rectangle;
37
import org.joda.time.Partial;
38
import org.springframework.beans.factory.annotation.Autowired;
39
import org.springframework.dao.DataRetrievalFailureException;
40
import org.springframework.stereotype.Service;
41
import org.springframework.transaction.annotation.Transactional;
42

    
43
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
44
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeConfigurator;
45
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
46
import eu.etaxonomy.cdm.api.service.UpdateResult.Status;
47
import eu.etaxonomy.cdm.api.service.config.DeleteConfiguratorBase;
48
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
49
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
50
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
51
import eu.etaxonomy.cdm.api.service.dto.DerivateDTO;
52
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO;
53
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO.ContigFile;
54
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO.Link;
55
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO.MolecularData;
56
import eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO;
57
import eu.etaxonomy.cdm.api.service.dto.PreservedSpecimenDTO;
58
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
59
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
60
import eu.etaxonomy.cdm.api.service.pager.Pager;
61
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
62
import eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider;
63
import eu.etaxonomy.cdm.api.service.search.ISearchResultBuilder;
64
import eu.etaxonomy.cdm.api.service.search.LuceneParseException;
65
import eu.etaxonomy.cdm.api.service.search.LuceneSearch;
66
import eu.etaxonomy.cdm.api.service.search.QueryFactory;
67
import eu.etaxonomy.cdm.api.service.search.SearchResult;
68
import eu.etaxonomy.cdm.api.service.search.SearchResultBuilder;
69
import eu.etaxonomy.cdm.api.service.util.TaxonRelationshipEdge;
70
import eu.etaxonomy.cdm.common.CdmUtils;
71
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
72
import eu.etaxonomy.cdm.format.CdmFormatterFactory;
73
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
74
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
75
import eu.etaxonomy.cdm.model.CdmBaseType;
76
import eu.etaxonomy.cdm.model.agent.AgentBase;
77
import eu.etaxonomy.cdm.model.common.CdmBase;
78
import eu.etaxonomy.cdm.model.common.DefinedTerm;
79
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
80
import eu.etaxonomy.cdm.model.common.Language;
81
import eu.etaxonomy.cdm.model.description.CategoricalData;
82
import eu.etaxonomy.cdm.model.description.DescriptionBase;
83
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
84
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
85
import eu.etaxonomy.cdm.model.description.QuantitativeData;
86
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
87
import eu.etaxonomy.cdm.model.description.TaxonDescription;
88
import eu.etaxonomy.cdm.model.location.Country;
89
import eu.etaxonomy.cdm.model.location.NamedArea;
90
import eu.etaxonomy.cdm.model.media.Media;
91
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
92
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
93
import eu.etaxonomy.cdm.model.media.MediaUtils;
94
import eu.etaxonomy.cdm.model.molecular.AmplificationResult;
95
import eu.etaxonomy.cdm.model.molecular.DnaSample;
96
import eu.etaxonomy.cdm.model.molecular.Sequence;
97
import eu.etaxonomy.cdm.model.molecular.SingleRead;
98
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
99
import eu.etaxonomy.cdm.model.name.TaxonName;
100
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
101
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
102
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
103
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
104
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
105
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
106
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
107
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
108
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
109
import eu.etaxonomy.cdm.model.taxon.Taxon;
110
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
111
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
112
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer;
113
import eu.etaxonomy.cdm.persistence.dao.molecular.ISingleReadDao;
114
import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;
115
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
116
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
117
import eu.etaxonomy.cdm.persistence.query.AssignmentStatus;
118
import eu.etaxonomy.cdm.persistence.query.OrderHint;
119
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
120
import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
121

    
122
/**
123
 * @author a.babadshanjan
124
 * @since 01.09.2008
125
 */
126
@Service
127
@Transactional(readOnly = true)
128
public class OccurrenceServiceImpl extends IdentifiableServiceBase<SpecimenOrObservationBase, IOccurrenceDao> implements IOccurrenceService {
129

    
130
    static private final Logger logger = Logger.getLogger(OccurrenceServiceImpl.class);
131

    
132
    @Autowired
133
    private IDefinedTermDao definedTermDao;
134

    
135
    @Autowired
136
    private IDescriptionService descriptionService;
137

    
138
    @Autowired
139
    private INameService nameService;
140

    
141
    @Autowired
142
    private IEventBaseService eventService;
143

    
144
    @Autowired
145
    private ITaxonService taxonService;
146

    
147
    @Autowired
148
    private ISequenceService sequenceService;
149

    
150
    @Autowired
151
    private ISingleReadDao singleReadDao;
152

    
153
    @Autowired
154
    private AbstractBeanInitializer beanInitializer;
155

    
156
    @Autowired
157
    private ILuceneIndexToolProvider luceneIndexToolProvider;
158

    
159
    private static final String SEPARATOR_STRING = ", ";
160

    
161
    public OccurrenceServiceImpl() {
162
        logger.debug("Load OccurrenceService Bean");
163
    }
164

    
165

    
166
    @Override
167
    @Transactional(readOnly = false)
168
    public void updateTitleCache(Class<? extends SpecimenOrObservationBase> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<SpecimenOrObservationBase> cacheStrategy, IProgressMonitor monitor) {
169
        if (clazz == null) {
170
            clazz = SpecimenOrObservationBase.class;
171
        }
172
        super.updateTitleCacheImpl(clazz, stepSize, cacheStrategy, monitor);
173
    }
174

    
175
    /**
176
     * FIXME Candidate for harmonization
177
     * move to termService
178
     */
179
    @Override
180
    public Country getCountryByIso(String iso639) {
181
        return this.definedTermDao.getCountryByIso(iso639);
182

    
183
    }
184

    
185
    /**
186
     * FIXME Candidate for harmonization
187
     * move to termService
188
     */
189
    @Override
190
    public List<Country> getCountryByName(String name) {
191
        List<? extends DefinedTermBase> terms = this.definedTermDao.findByTitle(Country.class, name, null, null, null, null, null, null);
192
        List<Country> countries = new ArrayList<>();
193
        for (int i = 0; i < terms.size(); i++) {
194
            countries.add((Country) terms.get(i));
195
        }
196
        return countries;
197
    }
198

    
199
    @Override
200
    @Autowired
201
    protected void setDao(IOccurrenceDao dao) {
202
        this.dao = dao;
203
    }
204

    
205
    @Override
206
    public Pager<DerivationEvent> getDerivationEvents(SpecimenOrObservationBase occurence, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
207
        Integer numberOfResults = dao.countDerivationEvents(occurence);
208

    
209
        List<DerivationEvent> results = new ArrayList<>();
210
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
211
            results = dao.getDerivationEvents(occurence, pageSize, pageNumber, propertyPaths);
212
        }
213

    
214
        return new DefaultPagerImpl<DerivationEvent>(pageNumber, numberOfResults, pageSize, results);
215
    }
216

    
217
    @Override
218
    public int countDeterminations(SpecimenOrObservationBase occurence, TaxonBase taxonbase) {
219
        return dao.countDeterminations(occurence, taxonbase);
220
    }
221

    
222
    @Override
223
    public Pager<DeterminationEvent> getDeterminations(SpecimenOrObservationBase occurrence, TaxonBase taxonBase, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
224
        Integer numberOfResults = dao.countDeterminations(occurrence, taxonBase);
225

    
226
        List<DeterminationEvent> results = new ArrayList<>();
227
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
228
            results = dao.getDeterminations(occurrence, taxonBase, pageSize, pageNumber, propertyPaths);
229
        }
230

    
231
        return new DefaultPagerImpl<DeterminationEvent>(pageNumber, numberOfResults, pageSize, results);
232
    }
233

    
234
    @Override
235
    public Pager<Media> getMedia(SpecimenOrObservationBase occurence, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
236
        Integer numberOfResults = dao.countMedia(occurence);
237

    
238
        List<Media> results = new ArrayList<>();
239
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
240
            results = dao.getMedia(occurence, pageSize, pageNumber, propertyPaths);
241
        }
242

    
243
        return new DefaultPagerImpl<Media>(pageNumber, numberOfResults, pageSize, results);
244
    }
245

    
246
    @Override
247
    public Pager<Media> getMediainHierarchy(SpecimenOrObservationBase rootOccurence, Integer pageSize,
248
            Integer pageNumber, List<String> propertyPaths) {
249
        List<Media> media = new ArrayList<>();
250
        //media specimens
251
        if(rootOccurence.isInstanceOf(MediaSpecimen.class)){
252
            MediaSpecimen mediaSpecimen = HibernateProxyHelper.deproxy(rootOccurence, MediaSpecimen.class);
253
            media.add(mediaSpecimen.getMediaSpecimen());
254
        }
255
        // pherograms & gelPhotos
256
        if (rootOccurence.isInstanceOf(DnaSample.class)) {
257
            DnaSample dnaSample = CdmBase.deproxy(rootOccurence, DnaSample.class);
258
            Set<Sequence> sequences = dnaSample.getSequences();
259
            //we do show only those gelPhotos which lead to a consensus sequence
260
            for (Sequence sequence : sequences) {
261
                Set<Media> dnaRelatedMedia = new HashSet<>();
262
                for (SingleRead singleRead : sequence.getSingleReads()){
263
                    AmplificationResult amplification = singleRead.getAmplificationResult();
264
                    dnaRelatedMedia.add(amplification.getGelPhoto());
265
                    dnaRelatedMedia.add(singleRead.getPherogram());
266
                    dnaRelatedMedia.remove(null);
267
                }
268
                media.addAll(dnaRelatedMedia);
269
            }
270
        }
271
        if(rootOccurence.isInstanceOf(DerivedUnit.class)){
272
            DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(rootOccurence, DerivedUnit.class);
273
            for (DerivationEvent derivationEvent : derivedUnit.getDerivationEvents()) {
274
                for (DerivedUnit childDerivative : derivationEvent.getDerivatives()) {
275
                    media.addAll(getMediainHierarchy(childDerivative, pageSize, pageNumber, propertyPaths).getRecords());
276
                }
277
            }
278
        }
279
        return new DefaultPagerImpl<Media>(pageNumber, media.size(), pageSize, media);
280
    }
281

    
282
    @Override
283
    public Pager<SpecimenOrObservationBase> list(Class<? extends SpecimenOrObservationBase> type, TaxonName determinedAs, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
284
        Integer numberOfResults = dao.count(type, determinedAs);
285
        List<SpecimenOrObservationBase> results = new ArrayList<>();
286
        pageNumber = pageNumber == null ? 0 : pageNumber;
287
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
288
            Integer start = pageSize == null ? 0 : pageSize * pageNumber;
289
            results = dao.list(type, determinedAs, pageSize, start, orderHints, propertyPaths);
290
        }
291
        return new DefaultPagerImpl<SpecimenOrObservationBase>(pageNumber, numberOfResults, pageSize, results);
292
    }
293

    
294
    @Override
295
    public Pager<SpecimenOrObservationBase> list(Class<? extends SpecimenOrObservationBase> type, TaxonBase determinedAs, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
296
        Integer numberOfResults = dao.count(type, determinedAs);
297
        List<SpecimenOrObservationBase> results = new ArrayList<>();
298
        pageNumber = pageNumber == null ? 0 : pageNumber;
299
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
300
            Integer start = pageSize == null ? 0 : pageSize * pageNumber;
301
            results = dao.list(type, determinedAs, pageSize, start, orderHints, propertyPaths);
302
        }
303
        return new DefaultPagerImpl<SpecimenOrObservationBase>(pageNumber, numberOfResults, pageSize, results);
304
    }
305

    
306
    @Override
307
    public List<UuidAndTitleCache<DerivedUnit>> getDerivedUnitUuidAndTitleCache(Integer limit, String pattern) {
308
        return dao.getDerivedUnitUuidAndTitleCache(limit, pattern);
309
    }
310

    
311
    @Override
312
    public List<UuidAndTitleCache<FieldUnit>> getFieldUnitUuidAndTitleCache() {
313
        return dao.getFieldUnitUuidAndTitleCache();
314
    }
315

    
316
    @Override
317
    public DerivedUnitFacade getDerivedUnitFacade(DerivedUnit derivedUnit, List<String> propertyPaths) throws DerivedUnitFacadeNotSupportedException {
318
        derivedUnit = (DerivedUnit) dao.load(derivedUnit.getUuid(), null);
319
        DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
320
        config.setThrowExceptionForNonSpecimenPreservationMethodRequest(false);
321
        DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(derivedUnit, config);
322
        beanInitializer.initialize(derivedUnitFacade, propertyPaths);
323
        return derivedUnitFacade;
324
    }
325

    
326
    @Override
327
    public List<DerivedUnitFacade> listDerivedUnitFacades(
328
            DescriptionBase description, List<String> propertyPaths) {
329

    
330
        List<DerivedUnitFacade> derivedUnitFacadeList = new ArrayList<>();
331
        IndividualsAssociation tempIndividualsAssociation;
332
        SpecimenOrObservationBase tempSpecimenOrObservationBase;
333
        List<IndividualsAssociation> elements = descriptionService.listDescriptionElements(description, null, IndividualsAssociation.class, null, 0, Arrays.asList(new String []{"associatedSpecimenOrObservation"}));
334
        for (IndividualsAssociation element : elements) {
335
            tempIndividualsAssociation = HibernateProxyHelper.deproxy(element, IndividualsAssociation.class);
336
            if (tempIndividualsAssociation.getAssociatedSpecimenOrObservation() != null) {
337
                tempSpecimenOrObservationBase = HibernateProxyHelper.deproxy(tempIndividualsAssociation.getAssociatedSpecimenOrObservation(), SpecimenOrObservationBase.class);
338
                if (tempSpecimenOrObservationBase.isInstanceOf(DerivedUnit.class)) {
339
                    try {
340
                        derivedUnitFacadeList.add(DerivedUnitFacade.NewInstance(HibernateProxyHelper.deproxy(tempSpecimenOrObservationBase, DerivedUnit.class)));
341
                    } catch (DerivedUnitFacadeNotSupportedException e) {
342
                        logger.warn(tempIndividualsAssociation.getAssociatedSpecimenOrObservation().getTitleCache() + " : " + e.getMessage());
343
                    }
344
                }
345
            }
346
        }
347

    
348
        beanInitializer.initializeAll(derivedUnitFacadeList, propertyPaths);
349

    
350
        return derivedUnitFacadeList;
351
    }
352

    
353

    
354
    @Override
355
    public <T extends SpecimenOrObservationBase> List<T> listByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includeRelationships,
356
            Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
357

    
358
        return pageByAssociatedTaxon(type, includeRelationships, associatedTaxon, maxDepth, pageSize, pageNumber, orderHints, propertyPaths).getRecords();
359
    }
360

    
361
    @Override
362
    public Collection<SpecimenNodeWrapper> listUuidAndTitleCacheByAssociatedTaxon(List<UUID> taxonNodeUuids,
363
            Integer limit, Integer start) {
364
        return dao.listUuidAndTitleCacheByAssociatedTaxon(taxonNodeUuids, limit, start);
365
        }
366

    
367
    @Override
368
    public Collection<SpecimenOrObservationBase> listFieldUnitsByAssociatedTaxon(Taxon associatedTaxon, List<OrderHint> orderHints, List<String> propertyPaths) {
369
        return pageFieldUnitsByAssociatedTaxon(null, associatedTaxon, null, null, null, null, propertyPaths).getRecords();
370
    }
371

    
372
    @Override
373
    public Pager<SpecimenOrObservationBase> pageFieldUnitsByAssociatedTaxon(Set<TaxonRelationshipEdge> includeRelationships,
374
            Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
375
            List<String> propertyPaths) {
376

    
377
        if (!getSession().contains(associatedTaxon)) {
378
            associatedTaxon = (Taxon) taxonService.load(associatedTaxon.getUuid());
379
        }
380

    
381
        // gather the IDs of all relevant field units
382
        Set<UUID> fieldUnitUuids = new HashSet<>();
383
        List<SpecimenOrObservationBase> records = listByAssociatedTaxon(null, includeRelationships, associatedTaxon, maxDepth, null, null, orderHints, propertyPaths);
384
        for (SpecimenOrObservationBase<?> specimen : records) {
385
            for (FieldUnit fieldUnit : getFieldUnits(specimen.getUuid(), null)) {
386
                fieldUnitUuids.add(fieldUnit.getUuid());
387
            }
388
        }
389
        //dao.list() does the paging of the field units. Passing the field units directly to the Pager would not work
390
        List<SpecimenOrObservationBase> fieldUnits = dao.list(fieldUnitUuids, pageSize, pageNumber, orderHints, propertyPaths);
391
        return new DefaultPagerImpl<SpecimenOrObservationBase>(pageNumber, fieldUnitUuids.size(), pageSize, fieldUnits);
392
    }
393

    
394
    @Override
395
    public FieldUnitDTO assembleFieldUnitDTO(FieldUnit fieldUnit, UUID associatedTaxonUuid) {
396

    
397
        if (!getSession().contains(fieldUnit)) {
398
            fieldUnit = (FieldUnit) load(fieldUnit.getUuid());
399
        }
400
        TaxonBase associatedTaxon = taxonService.load(associatedTaxonUuid);
401

    
402
        FieldUnitDTO fieldUnitDTO = new FieldUnitDTO();
403

    
404
        if (fieldUnit.getGatheringEvent() != null) {
405
            GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
406
            // Country
407
            NamedArea country = gatheringEvent.getCountry();
408
            fieldUnitDTO.setCountry(country != null ? country.getLabel() : null);
409
            // Collection
410
            AgentBase collector = gatheringEvent.getCollector();
411
            String fieldNumber = fieldUnit.getFieldNumber();
412
            String collectionString = "";
413
            if (collector != null || fieldNumber != null) {
414
                collectionString += collector != null ? collector : "";
415
                if (!collectionString.isEmpty()) {
416
                    collectionString += " ";
417
                }
418
                collectionString += (fieldNumber != null ? fieldNumber : "");
419
                collectionString.trim();
420
            }
421
            fieldUnitDTO.setCollection(collectionString);
422
            // Date
423
            Partial gatheringDate = gatheringEvent.getGatheringDate();
424
            String dateString = null;
425
            if (gatheringDate != null) {
426
                dateString = gatheringDate.toString();
427
            }
428
            else if(gatheringEvent.getTimeperiod()!=null && gatheringEvent.getTimeperiod().getFreeText()!=null){
429
                dateString = gatheringEvent.getTimeperiod().getFreeText();
430
            }
431
            fieldUnitDTO.setDate(dateString);
432
        }
433

    
434
        // Taxon Name
435
        fieldUnitDTO.setTaxonName(associatedTaxon.getName().getTitleCache());
436

    
437
        // Herbaria map
438
        Map<eu.etaxonomy.cdm.model.occurrence.Collection, Integer> collectionToCountMap = new HashMap<>();
439
        // List of accession numbers for citation
440
        List<String> preservedSpecimenAccessionNumbers = new ArrayList<>();
441

    
442
        // assemble preserved specimen DTOs
443
        Set<DerivationEvent> derivationEvents = fieldUnit.getDerivationEvents();
444
        for (DerivationEvent derivationEvent : derivationEvents) {
445
            Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
446
            for (DerivedUnit derivedUnit : derivatives) {
447
                if(!derivedUnit.isPublish()){
448
                    continue;
449
                }
450
                // collect accession numbers for citation
451
                String mostSignificantIdentifier = getMostSignificantIdentifier(derivedUnit);
452
                if (mostSignificantIdentifier != null) {
453
                    preservedSpecimenAccessionNumbers.add(mostSignificantIdentifier);
454
                }
455
                // collect collections for herbaria column
456
                if (derivedUnit.getCollection() != null) {
457
                    Integer herbariumCount = collectionToCountMap.get(derivedUnit.getCollection());
458
                    if (herbariumCount == null) {
459
                        herbariumCount = 0;
460
                    }
461
                    collectionToCountMap.put(derivedUnit.getCollection(), herbariumCount + 1);
462
                }
463
                if (derivedUnit.getRecordBasis().equals(SpecimenOrObservationType.PreservedSpecimen)) {
464
                    PreservedSpecimenDTO preservedSpecimenDTO = assemblePreservedSpecimenDTO(derivedUnit, fieldUnitDTO);
465
                    fieldUnitDTO.addPreservedSpecimenDTO(preservedSpecimenDTO);
466
                    fieldUnitDTO.setHasCharacterData(fieldUnitDTO.isHasCharacterData() || preservedSpecimenDTO.isHasCharacterData());
467
                    fieldUnitDTO.setHasDetailImage(fieldUnitDTO.isHasDetailImage() || preservedSpecimenDTO.isHasDetailImage());
468
                    fieldUnitDTO.setHasDna(fieldUnitDTO.isHasDna() || preservedSpecimenDTO.isHasDna());
469
                    fieldUnitDTO.setHasSpecimenScan(fieldUnitDTO.isHasSpecimenScan() || preservedSpecimenDTO.isHasSpecimenScan());
470
                }
471
            }
472
        }
473
        // assemble derivate data DTO
474
        assembleDerivateDataDTO(fieldUnitDTO, fieldUnit);
475

    
476
        // assemble citation
477
        String citation = fieldUnit.getTitleCache();
478
        if((CdmUtils.isBlank(citation) || citation.equals(IdentifiableEntityDefaultCacheStrategy.TITLE_CACHE_GENERATION_NOT_IMPLEMENTED))
479
                && !fieldUnit.isProtectedTitleCache()){
480
            fieldUnit.setTitleCache(null);
481
            citation = fieldUnit.getTitleCache();
482
        }
483
        if (!preservedSpecimenAccessionNumbers.isEmpty()) {
484
            citation += " (";
485
            for (String accessionNumber : preservedSpecimenAccessionNumbers) {
486
                if (!accessionNumber.isEmpty()) {
487
                    citation += accessionNumber + SEPARATOR_STRING;
488
                }
489
            }
490
            citation = removeTail(citation, SEPARATOR_STRING);
491
            citation += ")";
492
        }
493
        fieldUnitDTO.setCitation(citation);
494

    
495
        // assemble herbaria string
496
        String herbariaString = "";
497
        for (Entry<eu.etaxonomy.cdm.model.occurrence.Collection, Integer> e : collectionToCountMap.entrySet()) {
498
            eu.etaxonomy.cdm.model.occurrence.Collection collection = e.getKey();
499
            if (collection.getCode() != null) {
500
                herbariaString += collection.getCode();
501
            }
502
            if (e.getValue() > 1) {
503
                herbariaString += "(" + e.getValue() + ")";
504
            }
505
            herbariaString += SEPARATOR_STRING;
506
        }
507
        herbariaString = removeTail(herbariaString, SEPARATOR_STRING);
508
        fieldUnitDTO.setHerbarium(herbariaString);
509

    
510
        return fieldUnitDTO;
511
    }
512

    
513
    @Override
514
    public PreservedSpecimenDTO assemblePreservedSpecimenDTO(DerivedUnit derivedUnit) {
515
        return assemblePreservedSpecimenDTO(derivedUnit, null);
516
    }
517

    
518
    @Override
519
    public String getMostSignificantIdentifier(DerivedUnit derivedUnit) {
520
        if (derivedUnit.getAccessionNumber() != null && !derivedUnit.getAccessionNumber().isEmpty()) {
521
            return derivedUnit.getAccessionNumber();
522
        }
523
        else if(derivedUnit.getBarcode()!=null && !derivedUnit.getBarcode().isEmpty()){
524
            return derivedUnit.getBarcode();
525
        }
526
        else if(derivedUnit.getCatalogNumber()!=null && !derivedUnit.getCatalogNumber().isEmpty()){
527
            return derivedUnit.getCatalogNumber();
528
        }
529
        return null;
530
    }
531

    
532
    public PreservedSpecimenDTO assemblePreservedSpecimenDTO(DerivedUnit derivedUnit, FieldUnitDTO fieldUnitDTO) {
533
        if (!getSession().contains(derivedUnit)) {
534
            derivedUnit = (DerivedUnit) load(derivedUnit.getUuid());
535
        }
536
        PreservedSpecimenDTO preservedSpecimenDTO = new PreservedSpecimenDTO();
537

    
538
        //specimen identifier
539
        FormatKey collectionKey = FormatKey.COLLECTION_CODE;
540
        String specimenIdentifier = CdmFormatterFactory.format(derivedUnit, collectionKey);
541
        if (CdmUtils.isBlank(specimenIdentifier)) {
542
            collectionKey = FormatKey.COLLECTION_NAME;
543
        }
544
        specimenIdentifier = CdmFormatterFactory.format(derivedUnit, new FormatKey[] {
545
                collectionKey, FormatKey.SPACE,
546
                FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE });
547
        if(CdmUtils.isBlank(specimenIdentifier)){
548
            specimenIdentifier = derivedUnit.getUuid().toString();
549
        }
550
        preservedSpecimenDTO.setAccessionNumber(specimenIdentifier);
551
        preservedSpecimenDTO.setUuid(derivedUnit.getUuid().toString());
552

    
553
        //preferred stable URI
554
        preservedSpecimenDTO.setPreferredStableUri(derivedUnit.getPreferredStableUri());
555

    
556
        // citation
557
        Collection<FieldUnit> fieldUnits = getFieldUnits(derivedUnit, null);
558
        if (fieldUnits.size() == 1) {
559
            preservedSpecimenDTO.setCitation(fieldUnits.iterator().next().getTitleCache());
560
        }
561
        else{
562
            preservedSpecimenDTO.setCitation("No Citation available. This specimen either has no or multiple field units.");
563
        }
564

    
565
        // character state data
566
        Collection<DescriptionElementBase> characterDataForSpecimen = getCharacterDataForSpecimen(derivedUnit);
567
        if (!characterDataForSpecimen.isEmpty()) {
568
            if (fieldUnitDTO != null) {
569
                fieldUnitDTO.setHasCharacterData(true);
570
            }
571
        }
572
        for (DescriptionElementBase descriptionElementBase : characterDataForSpecimen) {
573
            String character = descriptionElementBase.getFeature().getLabel();
574
            ArrayList<Language> languages = new ArrayList<>(Collections.singleton(Language.DEFAULT()));
575
            if (descriptionElementBase instanceof QuantitativeData) {
576
                QuantitativeData quantitativeData = (QuantitativeData) descriptionElementBase;
577
                DefaultQuantitativeDescriptionBuilder builder = new DefaultQuantitativeDescriptionBuilder();
578
                String state = builder.build(quantitativeData, languages).getText(Language.DEFAULT());
579
                preservedSpecimenDTO.addCharacterData(character, state);
580
            }
581
            else if(descriptionElementBase instanceof CategoricalData){
582
                CategoricalData categoricalData = (CategoricalData) descriptionElementBase;
583
                DefaultCategoricalDescriptionBuilder builder = new DefaultCategoricalDescriptionBuilder();
584
                String state = builder.build(categoricalData, languages).getText(Language.DEFAULT());
585
                preservedSpecimenDTO.addCharacterData(character, state);
586
            }
587
        }
588
        // check type designations
589
        Collection<SpecimenTypeDesignation> specimenTypeDesignations = listTypeDesignations(derivedUnit, null, null, null, null);
590
        for (SpecimenTypeDesignation specimenTypeDesignation : specimenTypeDesignations) {
591
            if (fieldUnitDTO != null) {
592
                fieldUnitDTO.setHasType(true);
593
            }
594
            TypeDesignationStatusBase<?> typeStatus = specimenTypeDesignation.getTypeStatus();
595
            if (typeStatus != null) {
596
                List<String> typedTaxaNames = new ArrayList<>();
597
                String label = typeStatus.getLabel();
598
                Set<TaxonName> typifiedNames = specimenTypeDesignation.getTypifiedNames();
599
                for (TaxonName taxonName : typifiedNames) {
600
                    typedTaxaNames.add(taxonName.getFullTitleCache());
601
                }
602
                preservedSpecimenDTO.addTypes(label, typedTaxaNames);
603
            }
604
        }
605

    
606
        // individuals associations
607
        Collection<IndividualsAssociation> individualsAssociations = listIndividualsAssociations(derivedUnit, null, null, null, null);
608
        for (IndividualsAssociation individualsAssociation : individualsAssociations) {
609
            if (individualsAssociation.getInDescription() != null) {
610
                if (individualsAssociation.getInDescription().isInstanceOf(TaxonDescription.class)) {
611
                    TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(individualsAssociation.getInDescription(), TaxonDescription.class);
612
                    Taxon taxon = taxonDescription.getTaxon();
613
                    if (taxon != null) {
614
                        preservedSpecimenDTO.addAssociatedTaxon(taxon);
615
                    }
616
                }
617
            }
618
        }
619
        // assemble sub derivates
620
        preservedSpecimenDTO.setDerivateDataDTO(assembleDerivateDataDTO(preservedSpecimenDTO, derivedUnit));
621
        return preservedSpecimenDTO;
622
    }
623

    
624
    private DerivateDataDTO assembleDerivateDataDTO(DerivateDTO derivateDTO, SpecimenOrObservationBase<?> specimenOrObservation) {
625
        DerivateDataDTO derivateDataDTO = new DerivateDataDTO();
626
        Collection<DerivedUnit> childDerivates = getDerivedUnitsFor(specimenOrObservation);
627
        for (DerivedUnit childDerivate : childDerivates) {
628
            // assemble molecular data
629
            //pattern: DNAMarker [contig1, primer1_1, primer1_2, ...][contig2, primer2_1, ...]...
630
            if (childDerivate.isInstanceOf(DnaSample.class)) {
631
                if (childDerivate.getRecordBasis() == SpecimenOrObservationType.TissueSample) {
632
                    // TODO implement TissueSample assembly for web service
633
                }
634
                if (childDerivate.getRecordBasis() == SpecimenOrObservationType.DnaSample) {
635

    
636
                    DnaSample dna = HibernateProxyHelper.deproxy(childDerivate, DnaSample.class);
637
                    if (!dna.getSequences().isEmpty()) {
638
                        derivateDTO.setHasDna(true);
639
                    }
640
                    for (Sequence sequence : dna.getSequences()) {
641
                        URI boldUri = null;
642
                        try {
643
                            boldUri = sequence.getBoldUri();
644
                        } catch (URISyntaxException e1) {
645
                            logger.error("Could not create BOLD URI", e1);
646
                        }
647
                        final DefinedTerm dnaMarker = sequence.getDnaMarker();
648
                        Link providerLink = null;
649
                        if(boldUri!=null && dnaMarker!=null){
650
                        	providerLink = new DerivateDataDTO.Link(boldUri, dnaMarker.getLabel());
651
                        }
652
                        MolecularData molecularData = derivateDataDTO.addProviderLink(providerLink);
653

    
654
                        //contig file
655
                        ContigFile contigFile = null;
656
                        if (sequence.getContigFile() != null) {
657
                            MediaRepresentationPart contigMediaRepresentationPart = MediaUtils.getFirstMediaRepresentationPart(sequence.getContigFile());
658
                            if (contigMediaRepresentationPart != null) {
659
                                contigFile = molecularData.addContigFile(new Link(contigMediaRepresentationPart.getUri(), "contig"));
660
                            }
661
                        }
662
                        else{
663
                        	contigFile = molecularData.addContigFile(null);
664
                        }
665
                        // primer files
666
                        if (sequence.getSingleReads() != null) {
667
                            int readCount = 1;
668
                            for (SingleRead singleRead : sequence.getSingleReads()) {
669
                                MediaRepresentationPart pherogramMediaRepresentationPart = MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram());
670
                                if (pherogramMediaRepresentationPart != null) {
671
                                    contigFile.addPrimerLink(pherogramMediaRepresentationPart.getUri(), "read"+readCount++);
672
                                }
673
                            }
674
                        }
675
                    }
676
                }
677
            }
678
            // assemble media data
679
            else if (childDerivate.isInstanceOf(MediaSpecimen.class)) {
680
                MediaSpecimen media = HibernateProxyHelper.deproxy(childDerivate, MediaSpecimen.class);
681

    
682
                URI mediaUri = getMediaUri(media);
683
                if (media.getKindOfUnit() != null) {
684
                    // specimen scan
685
                    if (media.getKindOfUnit().getUuid().equals(DefinedTerm.uuidSpecimenScan)) {
686
                        derivateDataDTO.addSpecimenScanUuid(media.getMediaSpecimen().getUuid());
687
                        derivateDTO.setHasSpecimenScan(true);
688
                        String imageLinkText = "scan";
689
                        if (derivateDTO instanceof PreservedSpecimenDTO && ((PreservedSpecimenDTO) derivateDTO).getAccessionNumber() != null) {
690
                            imageLinkText = ((PreservedSpecimenDTO) derivateDTO).getAccessionNumber();
691
                        }
692
                        derivateDataDTO.addSpecimenScan(mediaUri, imageLinkText);
693
                    }
694
                    // detail image
695
                    else if (media.getKindOfUnit().getUuid().equals(DefinedTerm.uuidDetailImage)) {
696
                        derivateDataDTO.addDetailImageUuid(media.getMediaSpecimen().getUuid());
697
                        derivateDTO.setHasDetailImage(true);
698
                        String motif = "detail image";
699
                        if (media.getMediaSpecimen()!=null){
700
                        	if(CdmUtils.isNotBlank(media.getMediaSpecimen().getTitleCache())) {
701
                        		motif = media.getMediaSpecimen().getTitleCache();
702
                        	}
703
                        }
704
                        derivateDataDTO.addDetailImage(mediaUri, motif);
705
                    }
706
                }
707
            }
708
        }
709
        return derivateDataDTO;
710
    }
711

    
712
    private String removeTail(String string, final String tail) {
713
        if (string.endsWith(tail)) {
714
            string = string.substring(0, string.length() - tail.length());
715
        }
716
        return string;
717
    }
718

    
719
    private URI getMediaUri(MediaSpecimen mediaSpecimen) {
720
        URI mediaUri = null;
721
        Collection<MediaRepresentation> mediaRepresentations = mediaSpecimen.getMediaSpecimen().getRepresentations();
722
        if (mediaRepresentations != null && !mediaRepresentations.isEmpty()) {
723
            Collection<MediaRepresentationPart> mediaRepresentationParts = mediaRepresentations.iterator().next().getParts();
724
            if (mediaRepresentationParts != null && !mediaRepresentationParts.isEmpty()) {
725
                MediaRepresentationPart part = mediaRepresentationParts.iterator().next();
726
                if (part.getUri() != null) {
727
                    mediaUri = part.getUri();
728
                }
729
            }
730
        }
731
        return mediaUri;
732
    }
733

    
734
    private Collection<DerivedUnit> getDerivedUnitsFor(SpecimenOrObservationBase<?> specimen) {
735
        Collection<DerivedUnit> derivedUnits = new ArrayList<>();
736
        for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
737
            for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
738
                derivedUnits.add(derivative);
739
                derivedUnits.addAll(getDerivedUnitsFor(derivative));
740
            }
741
        }
742
        return derivedUnits;
743
    }
744

    
745

    
746
    @SuppressWarnings("unchecked")
747
    @Override
748
    public <T extends SpecimenOrObservationBase> Pager<T> pageByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includeRelationships,
749
            Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
750

    
751
        Set<Taxon> taxa = new HashSet<>();
752
        Set<Integer> occurrenceIds = new HashSet<>();
753
        List<T> occurrences = new ArrayList<>();
754

    
755
        // Integer limit = PagerUtils.limitFor(pageSize);
756
        // Integer start = PagerUtils.startFor(pageSize, pageNumber);
757

    
758
        if (!getSession().contains(associatedTaxon)) {
759
            associatedTaxon = (Taxon) taxonService.load(associatedTaxon.getUuid());
760
        }
761

    
762
        if (includeRelationships != null) {
763
            taxa = taxonService.listRelatedTaxa(associatedTaxon, includeRelationships, maxDepth, null, null, propertyPaths);
764
        }
765

    
766
        taxa.add(associatedTaxon);
767

    
768
        for (Taxon taxon : taxa) {
769
            List<T> perTaxonOccurrences = dao.listByAssociatedTaxon(type, taxon, null, null, orderHints, propertyPaths);
770
            for (SpecimenOrObservationBase<?> o : perTaxonOccurrences) {
771
                occurrenceIds.add(o.getId());
772
            }
773
        }
774
        occurrences = (List<T>) dao.loadList(occurrenceIds, propertyPaths);
775

    
776
        return new DefaultPagerImpl<T>(pageNumber, Long.valueOf(occurrenceIds.size()), pageSize, occurrences);
777

    
778
    }
779

    
780
    @Override
781
    public <T extends SpecimenOrObservationBase> Pager<T> pageByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includeRelationships,
782
            String taxonUUID, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
783

    
784
        UUID uuid = UUID.fromString(taxonUUID);
785
        Taxon tax = (Taxon) taxonService.load(uuid);
786
        // TODO REMOVE NULL STATEMENT
787
        type = null;
788
        return pageByAssociatedTaxon(type, includeRelationships, tax, maxDepth, pageSize, pageNumber, orderHints, propertyPaths);
789

    
790
    }
791

    
792
    @Override
793
    public  List<DerivedUnit> findByAccessionNumber(
794
            String accessionNumberString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
795
            List<String> propertyPaths)  {
796

    
797
        List<DerivedUnit> records = new ArrayList<>();
798
        records = dao.getByGeneticAccessionNumber(accessionNumberString, propertyPaths);
799

    
800
        return records;
801

    
802
    }
803

    
804
    @Override
805
    public Pager<SearchResult<SpecimenOrObservationBase>> findByFullText(
806
            Class<? extends SpecimenOrObservationBase> clazz, String queryString, Rectangle boundingBox, List<Language> languages,
807
            boolean highlightFragments, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
808
            List<String> propertyPaths) throws IOException, LuceneParseException {
809

    
810
        LuceneSearch luceneSearch = prepareByFullTextSearch(clazz, queryString, boundingBox, languages, highlightFragments);
811

    
812
        // --- execute search
813
        TopGroups<BytesRef> topDocsResultSet;
814
        try {
815
            topDocsResultSet = luceneSearch.executeSearch(pageSize, pageNumber);
816
        } catch (ParseException e) {
817
            LuceneParseException parseException = new LuceneParseException(e.getMessage());
818
            parseException.setStackTrace(e.getStackTrace());
819
            throw parseException;
820
        }
821

    
822
        Map<CdmBaseType, String> idFieldMap = new HashMap<>();
823
        idFieldMap.put(CdmBaseType.SPECIMEN_OR_OBSERVATIONBASE, "id");
824

    
825
        // --- initialize taxa, highlight matches ....
826
        ISearchResultBuilder searchResultBuilder = new SearchResultBuilder(luceneSearch, luceneSearch.getQuery());
827
        @SuppressWarnings("rawtypes")
828
        List<SearchResult<SpecimenOrObservationBase>> searchResults = searchResultBuilder.createResultSet(
829
                topDocsResultSet, luceneSearch.getHighlightFields(), dao, idFieldMap, propertyPaths);
830

    
831
        int totalHits = topDocsResultSet != null ? topDocsResultSet.totalGroupCount : 0;
832

    
833
        return new DefaultPagerImpl<SearchResult<SpecimenOrObservationBase>>(pageNumber, totalHits, pageSize,
834
                searchResults);
835

    
836
    }
837

    
838
    private LuceneSearch prepareByFullTextSearch(Class<? extends SpecimenOrObservationBase> clazz, String queryString, Rectangle bbox,
839
            List<Language> languages, boolean highlightFragments) {
840

    
841
        Builder finalQueryBuilder = new Builder();
842
        Builder textQueryBuilder = new Builder();
843

    
844
        LuceneSearch luceneSearch = new LuceneSearch(luceneIndexToolProvider, FieldUnit.class);
845
        QueryFactory queryFactory = luceneIndexToolProvider.newQueryFactoryFor(FieldUnit.class);
846

    
847
        // --- criteria
848
        luceneSearch.setCdmTypRestriction(clazz);
849
        if (queryString != null) {
850
            textQueryBuilder.add(queryFactory.newTermQuery("titleCache", queryString), Occur.SHOULD);
851
            finalQueryBuilder.add(textQueryBuilder.build(), Occur.MUST);
852
        }
853

    
854
        // --- spacial query
855
        if (bbox != null) {
856
            finalQueryBuilder.add(QueryFactory.buildSpatialQueryByRange(bbox, "gatheringEvent.exactLocation.point"), Occur.MUST);
857
        }
858

    
859
        luceneSearch.setQuery(finalQueryBuilder.build());
860

    
861
        // --- sorting
862
        SortField[] sortFields = new SortField[] { SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.Type.STRING, false) };
863
        luceneSearch.setSortFields(sortFields);
864

    
865
        if (highlightFragments) {
866
            luceneSearch.setHighlightFields(queryFactory.getTextFieldNamesAsArray());
867
        }
868
        return luceneSearch;
869
    }
870

    
871

    
872
    @Override
873
    public Collection<FieldUnit> getFieldUnits(UUID derivedUnitUuid, List<String> propertyPaths) {
874
        //It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
875
        //from which this DerivedUnit was derived until all FieldUnits are found.
876

    
877
        // FIXME: use HQL queries to increase performance
878
        SpecimenOrObservationBase<?> specimen = load(derivedUnitUuid, propertyPaths);
879
//        specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
880
        Collection<FieldUnit> fieldUnits = new ArrayList<>();
881

    
882
        if (specimen.isInstanceOf(FieldUnit.class)) {
883
            fieldUnits.add(HibernateProxyHelper.deproxy(specimen, FieldUnit.class));
884
        }
885
        else if(specimen.isInstanceOf(DerivedUnit.class)){
886
            fieldUnits.addAll(getFieldUnits(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class), propertyPaths));
887
        }
888
        return fieldUnits;
889
    }
890

    
891
    private Collection<FieldUnit> getFieldUnits(DerivedUnit derivedUnit, List<String> propertyPaths) {
892
        Collection<FieldUnit> fieldUnits = new HashSet<>();
893
        Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
894
        if (originals != null && !originals.isEmpty()) {
895
            for (SpecimenOrObservationBase<?> original : originals) {
896
                if (original.isInstanceOf(FieldUnit.class)) {
897
                    fieldUnits.add((FieldUnit) load(original.getUuid(), propertyPaths));
898
                }
899
                else if(original.isInstanceOf(DerivedUnit.class)){
900
                    fieldUnits.addAll(getFieldUnits(HibernateProxyHelper.deproxy(original, DerivedUnit.class), propertyPaths));
901
                }
902
            }
903
        }
904
        return fieldUnits;
905
    }
906

    
907
    @Override
908
    @Transactional(readOnly = false)
909
    public UpdateResult moveSequence(DnaSample from, DnaSample to, Sequence sequence) {
910
        return moveSequence(from.getUuid(), to.getUuid(), sequence.getUuid());
911
    }
912

    
913
    @Override
914
    @Transactional(readOnly = false)
915
    public UpdateResult moveSequence(UUID fromUuid, UUID toUuid, UUID sequenceUuid) {
916
        // reload specimens to avoid session conflicts
917
        DnaSample from = (DnaSample) load(fromUuid);
918
        DnaSample to = (DnaSample) load(toUuid);
919
        Sequence sequence = sequenceService.load(sequenceUuid);
920

    
921
        if (from == null || to == null || sequence == null) {
922
            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" +
923
                    "Operation was move "+sequence+ " from "+from+" to "+to);
924
        }
925
        UpdateResult result = new UpdateResult();
926
        from.removeSequence(sequence);
927
        saveOrUpdate(from);
928
        to.addSequence(sequence);
929
        saveOrUpdate(to);
930
        result.setStatus(Status.OK);
931
        result.addUpdatedObject(from);
932
        result.addUpdatedObject(to);
933
        return result;
934
    }
935

    
936
    @Override
937
    @Transactional(readOnly = false)
938
    public boolean moveDerivate(SpecimenOrObservationBase<?> from, SpecimenOrObservationBase<?> to, DerivedUnit derivate) {
939
        return moveDerivate(from!=null?from.getUuid():null, to.getUuid(), derivate.getUuid()).isOk();
940
    }
941

    
942
    @Override
943
    @Transactional(readOnly = false)
944
    public UpdateResult moveDerivate(UUID specimenFromUuid, UUID specimenToUuid, UUID derivateUuid) {
945
        // reload specimens to avoid session conflicts
946
        SpecimenOrObservationBase<?> from = null;
947
        if(specimenFromUuid!=null){
948
            from = load(specimenFromUuid);
949
        }
950
        SpecimenOrObservationBase<?> to = load(specimenToUuid);
951
        DerivedUnit derivate = (DerivedUnit) load(derivateUuid);
952

    
953
        if ((specimenFromUuid!=null && from == null) || to == null || derivate == null) {
954
            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" +
955
                    "Operation was move "+derivate+ " from "+from+" to "+to);
956
        }
957
        UpdateResult result = new UpdateResult();
958
        SpecimenOrObservationType derivateType = derivate.getRecordBasis();
959
        SpecimenOrObservationType toType = to.getRecordBasis();
960
        // check if type is a sub derivate type
961
        if(toType==SpecimenOrObservationType.FieldUnit //moving to FieldUnit always works
962
                || derivateType==SpecimenOrObservationType.Media //moving media always works
963
                || (derivateType.isKindOf(toType) && toType!=derivateType)){ //moving only to parent derivate type
964
            if(from!=null){
965
                // remove derivation event from parent specimen of dragged object
966
                DerivationEvent eventToRemove = null;
967
                for (DerivationEvent event : from.getDerivationEvents()) {
968
                    if (event.getDerivatives().contains(derivate)) {
969
                        eventToRemove = event;
970
                        break;
971
                    }
972
                }
973
                from.removeDerivationEvent(eventToRemove);
974
                if(eventToRemove!=null){
975
                    // add new derivation event to target and copy the event parameters of the old one
976
                    DerivationEvent derivedFromNewOriginalEvent = DerivationEvent.NewSimpleInstance(to, derivate, null);
977
                    derivedFromNewOriginalEvent.setActor(eventToRemove.getActor());
978
                    derivedFromNewOriginalEvent.setDescription(eventToRemove.getDescription());
979
                    derivedFromNewOriginalEvent.setInstitution(eventToRemove.getInstitution());
980
                    derivedFromNewOriginalEvent.setTimeperiod(eventToRemove.getTimeperiod());
981
                    derivedFromNewOriginalEvent.setType(eventToRemove.getType());
982
                    to.addDerivationEvent(derivedFromNewOriginalEvent);
983
                    derivate.setDerivedFrom(derivedFromNewOriginalEvent);
984
                }
985
            }
986
            else{
987
                //derivative had no parent before so we use empty derivation event
988
                DerivationEvent derivedFromNewOriginalEvent = DerivationEvent.NewSimpleInstance(to, derivate, null);
989
                to.addDerivationEvent(derivedFromNewOriginalEvent);
990
                derivate.setDerivedFrom(derivedFromNewOriginalEvent);
991
            }
992

    
993
            if(from!=null){
994
                saveOrUpdate(from);
995
            }
996
            saveOrUpdate(to);
997
            result.setStatus(Status.OK);
998
            result.addUpdatedObject(from);
999
            result.addUpdatedObject(to);
1000
        } else {
1001
            result.setStatus(Status.ERROR);
1002
        }
1003
        return result;
1004
    }
1005

    
1006
    @Override
1007
    public DeleteResult isDeletable(UUID specimenUuid, DeleteConfiguratorBase config) {
1008
        DeleteResult deleteResult = new DeleteResult();
1009
        SpecimenOrObservationBase specimen = this.load(specimenUuid);
1010
        SpecimenDeleteConfigurator specimenDeleteConfigurator = (SpecimenDeleteConfigurator) config;
1011

    
1012
        // check elements found by super method
1013
        Set<CdmBase> relatedObjects = super.isDeletable(specimenUuid, config).getRelatedObjects();
1014
        for (CdmBase cdmBase : relatedObjects) {
1015
            // check for type designation
1016
            if (cdmBase.isInstanceOf(SpecimenTypeDesignation.class) && !specimenDeleteConfigurator.isDeleteFromTypeDesignation()) {
1017
                deleteResult.setAbort();
1018
                deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is a type specimen."));
1019
                deleteResult.addRelatedObject(cdmBase);
1020
                break;
1021
            }
1022
            // check for IndividualsAssociations
1023
            else if (cdmBase.isInstanceOf(IndividualsAssociation.class) && !specimenDeleteConfigurator.isDeleteFromIndividualsAssociation()) {
1024
                deleteResult.setAbort();
1025
                deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is still associated via IndividualsAssociations"));
1026
                deleteResult.addRelatedObject(cdmBase);
1027
                break;
1028
            }
1029
            // check for taxon description
1030
            else if(cdmBase.isInstanceOf(TaxonDescription.class)
1031
                    && HibernateProxyHelper.deproxy(cdmBase, TaxonDescription.class).getDescribedSpecimenOrObservation().equals(specimen)
1032
                    && !specimenDeleteConfigurator.isDeleteFromDescription()){
1033
                deleteResult.setAbort();
1034
                deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is still used as \"Described Specimen\" in a taxon description."));
1035
                deleteResult.addRelatedObject(cdmBase);
1036
                break;
1037
            }
1038
            // check for children and parents (derivation events)
1039
            else if (cdmBase.isInstanceOf(DerivationEvent.class)) {
1040
                DerivationEvent derivationEvent = HibernateProxyHelper.deproxy(cdmBase, DerivationEvent.class);
1041
                // check if derivation event is empty
1042
                if (!derivationEvent.getDerivatives().isEmpty() && derivationEvent.getOriginals().contains(specimen)) {
1043
                    // if derivationEvent is the childEvent and contains derivations
1044
//                    if (derivationEvent.getDerivatives().contains(specimen)) {
1045
//                        //if it is the parent event the specimen is still deletable
1046
//                        continue;
1047
//                    }
1048
                    if(!specimenDeleteConfigurator.isDeleteChildren()){
1049
                        //if children should not be deleted then it is undeletable
1050
                        deleteResult.setAbort();
1051
                        deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation still has child derivatives."));
1052
                        deleteResult.addRelatedObject(cdmBase);
1053
                        break;
1054
                    }
1055
                    else{
1056
                        // check all children if they can be deleted
1057
                        Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1058
                        DeleteResult childResult = new DeleteResult();
1059
                        for (DerivedUnit derivedUnit : derivatives) {
1060
                            childResult.includeResult(isDeletable(derivedUnit.getUuid(), specimenDeleteConfigurator));
1061
                        }
1062
                        if (!childResult.isOk()) {
1063
                            deleteResult.setAbort();
1064
                            deleteResult.includeResult(childResult);
1065
                            deleteResult.addRelatedObject(cdmBase);
1066
                            break;
1067
                        }
1068
                    }
1069
                }
1070
            }
1071
            // check for amplification
1072
            else if (cdmBase.isInstanceOf(AmplificationResult.class)
1073
                    && !specimenDeleteConfigurator.isDeleteMolecularData()
1074
                    && !specimenDeleteConfigurator.isDeleteChildren()) {
1075
                deleteResult.setAbort();
1076
                deleteResult.addException(new ReferencedObjectUndeletableException("DnaSample is used in amplification results."));
1077
                deleteResult.addRelatedObject(cdmBase);
1078
                break;
1079
            }
1080
            // check for sequence
1081
            else if (cdmBase.isInstanceOf(Sequence.class)
1082
                    && !specimenDeleteConfigurator.isDeleteMolecularData()
1083
                    && !specimenDeleteConfigurator.isDeleteChildren()) {
1084
                deleteResult.setAbort();
1085
                deleteResult.addException(new ReferencedObjectUndeletableException("DnaSample is used in sequences."));
1086
                deleteResult.addRelatedObject(cdmBase);
1087
                break;
1088
            }
1089
        }
1090
        if (deleteResult.isOk()) {
1091
            //add all related object if deletion is OK so they can be handled by the delete() method
1092
            deleteResult.addRelatedObjects(relatedObjects);
1093
        }
1094
        return deleteResult;
1095
    }
1096

    
1097
    /**
1098
     * {@inheritDoc}
1099
     */
1100
    @Transactional(readOnly = false)
1101
    @Override
1102
    public DeleteResult delete(UUID specimenUuid, SpecimenDeleteConfigurator config) {
1103
        return delete(load(specimenUuid), config);
1104
    }
1105

    
1106

    
1107
    @Transactional(readOnly = false)
1108
    @Override
1109
    public DeleteResult delete(SpecimenOrObservationBase<?> specimen, SpecimenDeleteConfigurator config) {
1110
        specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
1111

    
1112
        DeleteResult deleteResult = isDeletable(specimen.getUuid(), config);
1113
        if (!deleteResult.isOk()) {
1114
            return deleteResult;
1115
        }
1116

    
1117
        if (config.isDeleteChildren()) {
1118
            Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
1119
            //clone to avoid concurrent modification
1120
            //can happen if the child is deleted and deleted its own derivedFrom event
1121
            Set<DerivationEvent> derivationEventsClone = new HashSet<>(derivationEvents);
1122
            for (DerivationEvent derivationEvent : derivationEventsClone) {
1123
                Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1124
                Iterator<DerivedUnit> it = derivatives.iterator();
1125
                Set<DerivedUnit> derivativesToDelete = new HashSet<>();
1126
                while (it.hasNext()) {
1127
                    DerivedUnit unit = it.next();
1128
                    derivativesToDelete.add(unit);
1129
                }
1130
                for (DerivedUnit unit:derivativesToDelete){
1131
                    deleteResult.includeResult(delete(unit, config));
1132
                }
1133
            }
1134
        }
1135

    
1136

    
1137

    
1138

    
1139
        // check related objects
1140
        Set<CdmBase> relatedObjects = deleteResult.getRelatedObjects();
1141

    
1142
        for (CdmBase relatedObject : relatedObjects) {
1143
            // check for TypeDesignations
1144
            if (relatedObject.isInstanceOf(SpecimenTypeDesignation.class)) {
1145
                SpecimenTypeDesignation designation = HibernateProxyHelper.deproxy(relatedObject, SpecimenTypeDesignation.class);
1146
                designation.setTypeSpecimen(null);
1147
                List<TaxonName> typifiedNames = new ArrayList<>();
1148
                typifiedNames.addAll(designation.getTypifiedNames());
1149
                for (TaxonName taxonName : typifiedNames) {
1150
                    taxonName.removeTypeDesignation(designation);
1151
                }
1152
            }
1153
            // delete IndividualsAssociation
1154
            if (relatedObject.isInstanceOf(IndividualsAssociation.class)) {
1155
                IndividualsAssociation association = HibernateProxyHelper.deproxy(relatedObject, IndividualsAssociation.class);
1156
                association.setAssociatedSpecimenOrObservation(null);
1157
                association.getInDescription().removeElement(association);
1158
            }
1159
            // check for "described specimen" (deprecated)
1160
            if (relatedObject.isInstanceOf(TaxonDescription.class)) {
1161
                TaxonDescription description = HibernateProxyHelper.deproxy(relatedObject, TaxonDescription.class);
1162
                description.setDescribedSpecimenOrObservation(null);
1163
            }
1164
            // check for specimen description
1165
            if (relatedObject.isInstanceOf(SpecimenDescription.class)) {
1166
                SpecimenDescription specimenDescription = HibernateProxyHelper.deproxy(relatedObject, SpecimenDescription.class);
1167
                specimenDescription.setDescribedSpecimenOrObservation(null);
1168
                // check if description is a description of the given specimen
1169
                if (specimen.getDescriptions().contains(specimenDescription)) {
1170
                    specimen.removeDescription(specimenDescription);
1171
                }
1172
                DeleteResult descriptionDelete = descriptionService.isDeletable(specimenDescription.getUuid(), null);
1173
                if (descriptionDelete.isOk()){
1174
                    deleteResult.includeResult(descriptionService.delete(specimenDescription));
1175
                }
1176
            }
1177
            // check for amplification
1178
            if (relatedObject.isInstanceOf(AmplificationResult.class)) {
1179
                AmplificationResult amplificationResult = HibernateProxyHelper.deproxy(relatedObject, AmplificationResult.class);
1180
                amplificationResult.getDnaSample().removeAmplificationResult(amplificationResult);
1181
            }
1182
            // check for sequence
1183
            if (relatedObject.isInstanceOf(Sequence.class)) {
1184
                Sequence sequence = HibernateProxyHelper.deproxy(relatedObject, Sequence.class);
1185
                sequence.getDnaSample().removeSequence(sequence);
1186
            }
1187
            // check for children and parents (derivation events)
1188
            if (relatedObject.isInstanceOf(DerivationEvent.class)) {
1189
                DerivationEvent derivationEvent = HibernateProxyHelper.deproxy(relatedObject, DerivationEvent.class);
1190
                // parent derivation event (derivedFrom)
1191
                if (derivationEvent.getDerivatives().contains(specimen) && specimen.isInstanceOf(DerivedUnit.class)) {
1192
                    derivationEvent.removeDerivative(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class));
1193
                    if (derivationEvent.getDerivatives().isEmpty()) {
1194
                        Set<SpecimenOrObservationBase> originals = derivationEvent.getOriginals();
1195
                        for (SpecimenOrObservationBase specimenOrObservationBase : originals) {
1196
                            specimenOrObservationBase.removeDerivationEvent(derivationEvent);
1197
                            deleteResult.addUpdatedObject(specimenOrObservationBase);
1198
                        }
1199
                        // if derivationEvent has no derivates anymore, delete it
1200
                        deleteResult.includeResult(eventService.delete(derivationEvent));
1201
                    }
1202
                }
1203
                else{
1204
                    //child derivation events should not occur since we delete the hierarchy from bottom to top
1205
                }
1206
            }
1207
        }
1208
        if (specimen instanceof FieldUnit){
1209
            FieldUnit fieldUnit = HibernateProxyHelper.deproxy(specimen, FieldUnit.class);
1210
            GatheringEvent event = fieldUnit.getGatheringEvent();
1211
            fieldUnit.setGatheringEvent(null);
1212
            if (event != null){
1213
                DeleteResult result = eventService.isDeletable(event.getUuid(), null);
1214
                if (result.isOk()){
1215
                    deleteResult.includeResult( eventService.delete(event));
1216
                }
1217
            }
1218

    
1219
        }
1220
        deleteResult.includeResult(delete(specimen));
1221

    
1222
        return deleteResult;
1223
    }
1224

    
1225
    @Override
1226
    public Collection<IndividualsAssociation> listIndividualsAssociations(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1227
        return dao.listIndividualsAssociations(specimen, limit, start, orderHints, propertyPaths);
1228
    }
1229

    
1230
    /**
1231
     * {@inheritDoc}
1232
     */
1233
    @Override
1234
    public Collection<TaxonBase<?>> listAssociatedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
1235
            Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1236
        return listAssociatedTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
1237
    }
1238
    @Override
1239
    public Collection<TaxonBase<?>> listAssociatedTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished,
1240
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1241
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1242

    
1243
        //individuals associations
1244
        associatedTaxa.addAll(listIndividualsAssociationTaxa(specimen, includeUnpublished, limit, start, orderHints, propertyPaths));
1245
        //type designation
1246
        if(specimen.isInstanceOf(DerivedUnit.class)){
1247
            associatedTaxa.addAll(listTypeDesignationTaxa(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class),
1248
                  includeUnpublished, limit, start, orderHints, propertyPaths));
1249
        }
1250
        //determinations
1251
        associatedTaxa.addAll(listDeterminedTaxa(specimen, includeUnpublished, limit, start, orderHints, propertyPaths));
1252

    
1253
        return associatedTaxa;
1254
    }
1255

    
1256

    
1257

    
1258
    /**
1259
     * {@inheritDoc}
1260
     */
1261
    @Override
1262
    public Collection<TaxonBase<?>> listDeterminedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
1263
            Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1264
        return listDeterminedTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
1265
    }
1266
    @Override
1267
    public Collection<TaxonBase<?>> listDeterminedTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished, Integer limit, Integer start,
1268
            List<OrderHint> orderHints, List<String> propertyPaths) {
1269
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1270
        for (DeterminationEvent determinationEvent : listDeterminationEvents(specimen, limit, start, orderHints, propertyPaths)) {
1271
            if(determinationEvent.getIdentifiedUnit().equals(specimen)){
1272
                if(determinationEvent.getTaxon()!=null){
1273
                    associatedTaxa.add(taxonService.load(determinationEvent.getTaxon().getUuid(), includeUnpublished, propertyPaths));
1274
                }
1275
                if(determinationEvent.getTaxonName()!=null){
1276
                    Collection<TaxonBase> taxonBases = determinationEvent.getTaxonName().getTaxonBases();
1277
                    for (TaxonBase taxonBase : taxonBases) {
1278
                        associatedTaxa.add(taxonService.load(taxonBase.getUuid(), includeUnpublished, propertyPaths));
1279
                    }
1280
                }
1281
            }
1282
        }
1283
        return associatedTaxa;
1284
    }
1285

    
1286
    /**
1287
     * {@inheritDoc}
1288
     */
1289
    @Override
1290
    public Collection<TaxonBase<?>> listTypeDesignationTaxa(DerivedUnit specimen, Integer limit, Integer start,
1291
            List<OrderHint> orderHints, List<String> propertyPaths) {
1292
        return listTypeDesignationTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
1293
    }
1294
    @Override
1295
    public Collection<TaxonBase<?>> listTypeDesignationTaxa(DerivedUnit specimen, boolean includeUnpublished,
1296
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1297
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1298
        for (SpecimenTypeDesignation typeDesignation : listTypeDesignations(specimen, limit, start, orderHints, propertyPaths)) {
1299
            if(typeDesignation.getTypeSpecimen().equals(specimen)){
1300
                Set<TaxonName> typifiedNames = typeDesignation.getTypifiedNames();
1301
                for (TaxonName taxonName : typifiedNames) {
1302
                    Set<Taxon> taxa = taxonName.getTaxa();
1303
                    for (Taxon taxon : taxa) {
1304
                        associatedTaxa.add(taxonService.load(taxon.getUuid(), includeUnpublished, propertyPaths));
1305
                    }
1306
                }
1307
            }
1308
        }
1309
        return associatedTaxa;
1310
    }
1311

    
1312
    /**
1313
     * {@inheritDoc}
1314
     */
1315
    @Override
1316
    public Collection<TaxonBase<?>> listIndividualsAssociationTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
1317
            Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1318
        return listIndividualsAssociationTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
1319
    }
1320

    
1321
    @Override
1322
    public Collection<TaxonBase<?>> listIndividualsAssociationTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished,
1323
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1324
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1325
        for (IndividualsAssociation individualsAssociation : listIndividualsAssociations(specimen, null, null, null, null)) {
1326
            if(individualsAssociation.getInDescription().isInstanceOf(TaxonDescription.class)){
1327
                TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(individualsAssociation.getInDescription(), TaxonDescription.class);
1328
                if(taxonDescription.getTaxon()!=null){
1329
                    associatedTaxa.add(taxonService.load(taxonDescription.getTaxon().getUuid(), includeUnpublished, propertyPaths));
1330
                }
1331
            }
1332
        }
1333
        return associatedTaxa;
1334
    }
1335

    
1336
    @Override
1337
    public Collection<DeterminationEvent> listDeterminationEvents(SpecimenOrObservationBase<?> specimen,
1338
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1339
        return dao.listDeterminationEvents(specimen, limit, start, orderHints, propertyPaths);
1340
    }
1341

    
1342
    @Override
1343
    public Map<DerivedUnit, Collection<SpecimenTypeDesignation>> listTypeDesignations(
1344
            Collection<DerivedUnit> specimens, Integer limit, Integer start,
1345
            List<OrderHint> orderHints, List<String> propertyPaths) {
1346
        Map<DerivedUnit, Collection<SpecimenTypeDesignation>> typeDesignationMap = new HashMap<>();
1347
        for (DerivedUnit specimen : specimens) {
1348
            Collection<SpecimenTypeDesignation> typeDesignations = listTypeDesignations(specimen, limit, start, orderHints, propertyPaths);
1349
            typeDesignationMap.put(specimen, typeDesignations);
1350
        }
1351
        return typeDesignationMap;
1352
    }
1353

    
1354
    @Override
1355
    public Collection<SpecimenTypeDesignation> listTypeDesignations(DerivedUnit specimen,
1356
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1357
        return dao.listTypeDesignations(specimen, limit, start, orderHints, propertyPaths);
1358
    }
1359

    
1360
    @Override
1361
    public Collection<DescriptionBase<?>> listDescriptionsWithDescriptionSpecimen(
1362
            SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints,
1363
            List<String> propertyPaths) {
1364
        return dao.listDescriptionsWithDescriptionSpecimen(specimen, limit, start, orderHints, propertyPaths);
1365
    }
1366

    
1367
    @Override
1368
    @Deprecated //this is not a service layer task so it may be removed in future versions
1369
    public Collection<DescriptionElementBase> getCharacterDataForSpecimen(SpecimenOrObservationBase<?> specimen) {
1370
        if (specimen != null) {
1371
            return specimen.characterData();
1372
        }else{
1373
            return new ArrayList<>();
1374
        }
1375
    }
1376

    
1377
    @Override
1378
    public Collection<DescriptionElementBase> getCharacterDataForSpecimen(UUID specimenUuid) {
1379
        SpecimenOrObservationBase<?> specimen = load(specimenUuid);
1380
        if (specimen != null) {
1381
            return getCharacterDataForSpecimen(specimen);
1382
        }
1383
        else{
1384
            throw new DataRetrievalFailureException("Specimen with the given uuid not found in the data base");
1385
        }
1386
    }
1387

    
1388

    
1389
    @Override
1390
    public Integer countByTitle(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config){
1391
        if (config instanceof FindOccurrencesConfigurator) {
1392
            FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
1393
            Taxon taxon = null;
1394
            if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
1395
                TaxonBase taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
1396
                if(taxonBase.isInstanceOf(Taxon.class)){
1397
                    taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
1398
                }
1399
            }
1400
            TaxonName taxonName = null;
1401
            if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
1402
                taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
1403
            }
1404
            /*TODO: #6484 Neither isRetrieveIndirectlyAssociatedSpecimens() nor the AssignmentStatus
1405
             * is currently reflected in the HQL query. So using these in the count method will
1406
             * significantly slow down this method as we have to retreive the entities instead of
1407
             * the just the amount.
1408
             */
1409
            if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens() || !occurrenceConfig.getAssignmentStatus().equals(AssignmentStatus.ALL_SPECIMENS)){
1410
                List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
1411
                occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
1412
                        occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1413
                        occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1414
                        occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
1415
                occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
1416
                return occurrences.size();
1417
            }
1418

    
1419
            return dao.countOccurrences(occurrenceConfig.getClazz(),
1420
                    occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1421
                    occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1422
                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
1423
        }
1424
        else{
1425
            return dao.countByTitle(config.getTitleSearchString());
1426
        }
1427
    }
1428

    
1429
    @Override
1430
    public Pager<UuidAndTitleCache<SpecimenOrObservationBase>> findByTitleUuidAndTitleCache(
1431
            FindOccurrencesConfigurator config){
1432
        List<UuidAndTitleCache<SpecimenOrObservationBase>> occurrences = new ArrayList<>();
1433
        Taxon taxon = null;
1434
        if(config.getAssociatedTaxonUuid()!=null){
1435
            TaxonBase taxonBase = taxonService.load(config.getAssociatedTaxonUuid());
1436
            if(taxonBase.isInstanceOf(Taxon.class)){
1437
                taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
1438
            }
1439
        }
1440
        TaxonName taxonName = null;
1441
        if(config.getAssociatedTaxonNameUuid()!=null){
1442
            taxonName = nameService.load(config.getAssociatedTaxonNameUuid());
1443
        }
1444
        occurrences.addAll(dao.findOccurrencesUuidAndTitleCache(config.getClazz(),
1445
                config.getTitleSearchString(), config.getSignificantIdentifier(),
1446
                config.getSpecimenType(), taxon, taxonName, config.getMatchMode(), null, null,
1447
                config.getOrderHints()));
1448

    
1449
        return new DefaultPagerImpl<UuidAndTitleCache<SpecimenOrObservationBase>>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
1450
    }
1451

    
1452
    @Override
1453
    public Pager<SpecimenOrObservationBase> findByTitle(
1454
            IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config) {
1455
        if (config instanceof FindOccurrencesConfigurator) {
1456
            FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
1457
            List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
1458
            Taxon taxon = null;
1459
            if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
1460
                TaxonBase taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
1461
                if(taxonBase.isInstanceOf(Taxon.class)){
1462
                    taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
1463
                }
1464
            }
1465
            TaxonName taxonName = null;
1466
            if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
1467
                taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
1468
            }
1469
            occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
1470
                    occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1471
                    occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1472
                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
1473
            occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
1474

    
1475
            return new DefaultPagerImpl<SpecimenOrObservationBase>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
1476
        }
1477
        return super.findByTitle(config);
1478
    }
1479

    
1480
    private List<SpecimenOrObservationBase> filterOccurencesByAssignmentAndHierarchy(
1481
            FindOccurrencesConfigurator occurrenceConfig, List<SpecimenOrObservationBase> occurrences, Taxon taxon,
1482
            TaxonName taxonName) {
1483
        //filter out (un-)assigned specimens
1484
        if(taxon==null && taxonName==null){
1485
            AssignmentStatus assignmentStatus = occurrenceConfig.getAssignmentStatus();
1486
            List<SpecimenOrObservationBase<?>> specimenWithAssociations = new ArrayList<>();
1487
            if(!assignmentStatus.equals(AssignmentStatus.ALL_SPECIMENS)){
1488
                for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
1489
                    boolean includeUnpublished = true;  //TODO not sure if this is correct, maybe we have to propagate publish flag to higher methods.
1490
                    Collection<TaxonBase<?>> associatedTaxa = listAssociatedTaxa(specimenOrObservationBase,
1491
                            includeUnpublished, null, null, null, null);
1492
                    if(!associatedTaxa.isEmpty()){
1493
                        specimenWithAssociations.add(specimenOrObservationBase);
1494
                    }
1495
                }
1496
            }
1497
            if(assignmentStatus.equals(AssignmentStatus.UNASSIGNED_SPECIMENS)){
1498
                occurrences.removeAll(specimenWithAssociations);
1499
            }
1500
            if(assignmentStatus.equals(AssignmentStatus.ASSIGNED_SPECIMENS)){
1501
                occurrences = new ArrayList<>(specimenWithAssociations);
1502
            }
1503
        }
1504
        // indirectly associated specimens
1505
        if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens()){
1506
            List<SpecimenOrObservationBase> indirectlyAssociatedOccurrences = new ArrayList<>(occurrences);
1507
            for (SpecimenOrObservationBase specimen : occurrences) {
1508
                List<SpecimenOrObservationBase<?>> allHierarchyDerivates = getAllHierarchyDerivatives(specimen);
1509
                for (SpecimenOrObservationBase<?> specimenOrObservationBase : allHierarchyDerivates) {
1510
                    if(!occurrences.contains(specimenOrObservationBase)){
1511
                        indirectlyAssociatedOccurrences.add(specimenOrObservationBase);
1512
                    }
1513
                }
1514
            }
1515
            occurrences = indirectlyAssociatedOccurrences;
1516
        }
1517
        return occurrences;
1518
    }
1519

    
1520
    @Override
1521
    public List<SpecimenOrObservationBase<?>> getAllHierarchyDerivatives(SpecimenOrObservationBase<?> specimen){
1522
        List<SpecimenOrObservationBase<?>> allHierarchyDerivatives = new ArrayList<>();
1523
        Collection<FieldUnit> fieldUnits = getFieldUnits(specimen.getUuid(), null);
1524
        if(fieldUnits.isEmpty()){
1525
            allHierarchyDerivatives.add(specimen);
1526
            allHierarchyDerivatives.addAll(getAllChildDerivatives(specimen));
1527
        }
1528
        else{
1529
            for (FieldUnit fieldUnit : fieldUnits) {
1530
                allHierarchyDerivatives.add(fieldUnit);
1531
                allHierarchyDerivatives.addAll(getAllChildDerivatives(fieldUnit));
1532
            }
1533
        }
1534
        return allHierarchyDerivatives;
1535
    }
1536

    
1537
    @Override
1538
    public List<DerivedUnit> getAllChildDerivatives(UUID specimenUuid){
1539
        return getAllChildDerivatives(load(specimenUuid));
1540
    }
1541

    
1542
    @Override
1543
    public List<DerivedUnit> getAllChildDerivatives(SpecimenOrObservationBase<?> specimen){
1544
        if (specimen == null){
1545
            return null;
1546
        }
1547
        List<DerivedUnit> childDerivate = new ArrayList<>();
1548
        Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
1549
        for (DerivationEvent derivationEvent : derivationEvents) {
1550
            Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1551
            for (DerivedUnit derivedUnit : derivatives) {
1552
                childDerivate.add(derivedUnit);
1553
                childDerivate.addAll(getAllChildDerivatives(derivedUnit.getUuid()));
1554
            }
1555
        }
1556
        return childDerivate;
1557
    }
1558

    
1559
    @Override
1560
    public int countOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config){
1561
        return countByTitle(config);
1562
    }
1563

    
1564
    /**
1565
     * {@inheritDoc}
1566
     */
1567
    @Override
1568
    public List<FieldUnit> getFieldUnitsForGatheringEvent(UUID gatheringEventUuid) {
1569
        return dao.getFieldUnitsForGatheringEvent(gatheringEventUuid, null, null, null, null);
1570
    }
1571
}
(83-83/106)