Project

General

Profile

Download (75.9 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
 * @created 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, List<OrderHint> orderHints) {
364
        return dao.listUuidAndTitleCacheByAssociatedTaxon(taxonNodeUuids, limit, start, orderHints);
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, 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 Pager<SearchResult<SpecimenOrObservationBase>> findByFullText(
794
            Class<? extends SpecimenOrObservationBase> clazz, String queryString, Rectangle boundingBox, List<Language> languages,
795
            boolean highlightFragments, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
796
            List<String> propertyPaths) throws IOException, LuceneParseException {
797

    
798
        LuceneSearch luceneSearch = prepareByFullTextSearch(clazz, queryString, boundingBox, languages, highlightFragments);
799

    
800
        // --- execute search
801
        TopGroups<BytesRef> topDocsResultSet;
802
        try {
803
            topDocsResultSet = luceneSearch.executeSearch(pageSize, pageNumber);
804
        } catch (ParseException e) {
805
            LuceneParseException parseException = new LuceneParseException(e.getMessage());
806
            parseException.setStackTrace(e.getStackTrace());
807
            throw parseException;
808
        }
809

    
810
        Map<CdmBaseType, String> idFieldMap = new HashMap<>();
811
        idFieldMap.put(CdmBaseType.SPECIMEN_OR_OBSERVATIONBASE, "id");
812

    
813
        // --- initialize taxa, highlight matches ....
814
        ISearchResultBuilder searchResultBuilder = new SearchResultBuilder(luceneSearch, luceneSearch.getQuery());
815
        @SuppressWarnings("rawtypes")
816
        List<SearchResult<SpecimenOrObservationBase>> searchResults = searchResultBuilder.createResultSet(
817
                topDocsResultSet, luceneSearch.getHighlightFields(), dao, idFieldMap, propertyPaths);
818

    
819
        int totalHits = topDocsResultSet != null ? topDocsResultSet.totalGroupCount : 0;
820

    
821
        return new DefaultPagerImpl<SearchResult<SpecimenOrObservationBase>>(pageNumber, totalHits, pageSize,
822
                searchResults);
823

    
824
    }
825

    
826
    private LuceneSearch prepareByFullTextSearch(Class<? extends SpecimenOrObservationBase> clazz, String queryString, Rectangle bbox,
827
            List<Language> languages, boolean highlightFragments) {
828

    
829
        Builder finalQueryBuilder = new Builder();
830
        Builder textQueryBuilder = new Builder();
831

    
832
        LuceneSearch luceneSearch = new LuceneSearch(luceneIndexToolProvider, FieldUnit.class);
833
        QueryFactory queryFactory = luceneIndexToolProvider.newQueryFactoryFor(FieldUnit.class);
834

    
835
        // --- criteria
836
        luceneSearch.setCdmTypRestriction(clazz);
837
        if (queryString != null) {
838
            textQueryBuilder.add(queryFactory.newTermQuery("titleCache", queryString), Occur.SHOULD);
839
            finalQueryBuilder.add(textQueryBuilder.build(), Occur.MUST);
840
        }
841

    
842
        // --- spacial query
843
        if (bbox != null) {
844
            finalQueryBuilder.add(QueryFactory.buildSpatialQueryByRange(bbox, "gatheringEvent.exactLocation.point"), Occur.MUST);
845
        }
846

    
847
        luceneSearch.setQuery(finalQueryBuilder.build());
848

    
849
        // --- sorting
850
        SortField[] sortFields = new SortField[] { SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.Type.STRING, false) };
851
        luceneSearch.setSortFields(sortFields);
852

    
853
        if (highlightFragments) {
854
            luceneSearch.setHighlightFields(queryFactory.getTextFieldNamesAsArray());
855
        }
856
        return luceneSearch;
857
    }
858

    
859

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

    
865
        // FIXME: use HQL queries to increase performance
866
        SpecimenOrObservationBase<?> specimen = load(derivedUnitUuid, propertyPaths);
867
//        specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
868
        Collection<FieldUnit> fieldUnits = new ArrayList<>();
869

    
870
        if (specimen.isInstanceOf(FieldUnit.class)) {
871
            fieldUnits.add(HibernateProxyHelper.deproxy(specimen, FieldUnit.class));
872
        }
873
        else if(specimen.isInstanceOf(DerivedUnit.class)){
874
            fieldUnits.addAll(getFieldUnits(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class), propertyPaths));
875
        }
876
        return fieldUnits;
877
    }
878

    
879
    private Collection<FieldUnit> getFieldUnits(DerivedUnit derivedUnit, List<String> propertyPaths) {
880
        Collection<FieldUnit> fieldUnits = new HashSet<>();
881
        Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
882
        if (originals != null && !originals.isEmpty()) {
883
            for (SpecimenOrObservationBase<?> original : originals) {
884
                if (original.isInstanceOf(FieldUnit.class)) {
885
                    fieldUnits.add((FieldUnit) load(original.getUuid(), propertyPaths));
886
                }
887
                else if(original.isInstanceOf(DerivedUnit.class)){
888
                    fieldUnits.addAll(getFieldUnits(HibernateProxyHelper.deproxy(original, DerivedUnit.class), propertyPaths));
889
                }
890
            }
891
        }
892
        return fieldUnits;
893
    }
894

    
895
    @Override
896
    @Transactional(readOnly = false)
897
    public UpdateResult moveSequence(DnaSample from, DnaSample to, Sequence sequence) {
898
        return moveSequence(from.getUuid(), to.getUuid(), sequence.getUuid());
899
    }
900

    
901
    @Override
902
    @Transactional(readOnly = false)
903
    public UpdateResult moveSequence(UUID fromUuid, UUID toUuid, UUID sequenceUuid) {
904
        // reload specimens to avoid session conflicts
905
        DnaSample from = (DnaSample) load(fromUuid);
906
        DnaSample to = (DnaSample) load(toUuid);
907
        Sequence sequence = sequenceService.load(sequenceUuid);
908

    
909
        if (from == null || to == null || sequence == null) {
910
            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" +
911
                    "Operation was move "+sequence+ " from "+from+" to "+to);
912
        }
913
        UpdateResult result = new UpdateResult();
914
        from.removeSequence(sequence);
915
        saveOrUpdate(from);
916
        to.addSequence(sequence);
917
        saveOrUpdate(to);
918
        result.setStatus(Status.OK);
919
        result.addUpdatedObject(from);
920
        result.addUpdatedObject(to);
921
        return result;
922
    }
923

    
924
    @Override
925
    @Transactional(readOnly = false)
926
    public boolean moveDerivate(SpecimenOrObservationBase<?> from, SpecimenOrObservationBase<?> to, DerivedUnit derivate) {
927
        return moveDerivate(from!=null?from.getUuid():null, to.getUuid(), derivate.getUuid()).isOk();
928
    }
929

    
930
    @Override
931
    @Transactional(readOnly = false)
932
    public UpdateResult moveDerivate(UUID specimenFromUuid, UUID specimenToUuid, UUID derivateUuid) {
933
        // reload specimens to avoid session conflicts
934
        SpecimenOrObservationBase<?> from = null;
935
        if(specimenFromUuid!=null){
936
            from = load(specimenFromUuid);
937
        }
938
        SpecimenOrObservationBase<?> to = load(specimenToUuid);
939
        DerivedUnit derivate = (DerivedUnit) load(derivateUuid);
940

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

    
981
            if(from!=null){
982
                saveOrUpdate(from);
983
            }
984
            saveOrUpdate(to);
985
            result.setStatus(Status.OK);
986
            result.addUpdatedObject(from);
987
            result.addUpdatedObject(to);
988
        } else {
989
            result.setStatus(Status.ERROR);
990
        }
991
        return result;
992
    }
993

    
994
    @Override
995
    public DeleteResult isDeletable(UUID specimenUuid, DeleteConfiguratorBase config) {
996
        DeleteResult deleteResult = new DeleteResult();
997
        SpecimenOrObservationBase specimen = this.load(specimenUuid);
998
        SpecimenDeleteConfigurator specimenDeleteConfigurator = (SpecimenDeleteConfigurator) config;
999

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

    
1085
    /**
1086
     * {@inheritDoc}
1087
     */
1088
    @Transactional(readOnly = false)
1089
    @Override
1090
    public DeleteResult delete(UUID specimenUuid, SpecimenDeleteConfigurator config) {
1091
        return delete(load(specimenUuid), config);
1092
    }
1093

    
1094

    
1095
    @Transactional(readOnly = false)
1096
    @Override
1097
    public DeleteResult delete(SpecimenOrObservationBase<?> specimen, SpecimenDeleteConfigurator config) {
1098
        specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
1099

    
1100
        DeleteResult deleteResult = isDeletable(specimen.getUuid(), config);
1101
        if (!deleteResult.isOk()) {
1102
            return deleteResult;
1103
        }
1104

    
1105
        if (config.isDeleteChildren()) {
1106
            Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
1107
            //clone to avoid concurrent modification
1108
            //can happen if the child is deleted and deleted its own derivedFrom event
1109
            Set<DerivationEvent> derivationEventsClone = new HashSet<>(derivationEvents);
1110
            for (DerivationEvent derivationEvent : derivationEventsClone) {
1111
                Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1112
                Iterator<DerivedUnit> it = derivatives.iterator();
1113
                Set<DerivedUnit> derivativesToDelete = new HashSet<>();
1114
                while (it.hasNext()) {
1115
                    DerivedUnit unit = it.next();
1116
                    derivativesToDelete.add(unit);
1117
                }
1118
                for (DerivedUnit unit:derivativesToDelete){
1119
                    deleteResult.includeResult(delete(unit, config));
1120
                }
1121
            }
1122
        }
1123

    
1124

    
1125

    
1126

    
1127
        // check related objects
1128
        Set<CdmBase> relatedObjects = deleteResult.getRelatedObjects();
1129

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

    
1207
        }
1208
        deleteResult.includeResult(delete(specimen));
1209

    
1210
        return deleteResult;
1211
    }
1212

    
1213
    @Override
1214
    public Collection<IndividualsAssociation> listIndividualsAssociations(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1215
        return dao.listIndividualsAssociations(specimen, limit, start, orderHints, propertyPaths);
1216
    }
1217

    
1218
    @Override
1219
    public Collection<TaxonBase<?>> listAssociatedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start,
1220
            List<OrderHint> orderHints, List<String> propertyPaths) {
1221
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1222

    
1223
        //individuals associations
1224
        associatedTaxa.addAll(listIndividualsAssociationTaxa(specimen, limit, start, orderHints, propertyPaths));
1225
        //type designation
1226
        if(specimen.isInstanceOf(DerivedUnit.class)){
1227
            associatedTaxa.addAll(listTypeDesignationTaxa(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class), limit, start, orderHints, propertyPaths));
1228
        }
1229
        //determinations
1230
        associatedTaxa.addAll(listDeterminedTaxa(specimen, limit, start, orderHints, propertyPaths));
1231

    
1232
        return associatedTaxa;
1233
    }
1234

    
1235

    
1236
    @Override
1237
    public Collection<TaxonBase<?>> listDeterminedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start,
1238
            List<OrderHint> orderHints, List<String> propertyPaths) {
1239
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1240
        for (DeterminationEvent determinationEvent : listDeterminationEvents(specimen, limit, start, orderHints, propertyPaths)) {
1241
            if(determinationEvent.getIdentifiedUnit().equals(specimen)){
1242
                if(determinationEvent.getTaxon()!=null){
1243
                    associatedTaxa.add(taxonService.load(determinationEvent.getTaxon().getUuid(), propertyPaths));
1244
                }
1245
                if(determinationEvent.getTaxonName()!=null){
1246
                    Collection<TaxonBase> taxonBases = determinationEvent.getTaxonName().getTaxonBases();
1247
                    for (TaxonBase taxonBase : taxonBases) {
1248
                        associatedTaxa.add(taxonService.load(taxonBase.getUuid(), propertyPaths));
1249
                    }
1250
                }
1251
            }
1252
        }
1253
        return associatedTaxa;
1254
    }
1255

    
1256
    @Override
1257
    public Collection<TaxonBase<?>> listTypeDesignationTaxa(DerivedUnit specimen, Integer limit, Integer start,
1258
            List<OrderHint> orderHints, List<String> propertyPaths) {
1259
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1260
        for (SpecimenTypeDesignation typeDesignation : listTypeDesignations(specimen, limit, start, orderHints, propertyPaths)) {
1261
            if(typeDesignation.getTypeSpecimen().equals(specimen)){
1262
                Set<TaxonName> typifiedNames = typeDesignation.getTypifiedNames();
1263
                for (TaxonName taxonName : typifiedNames) {
1264
                    Set<Taxon> taxa = taxonName.getTaxa();
1265
                    for (Taxon taxon : taxa) {
1266
                        associatedTaxa.add(taxonService.load(taxon.getUuid(), propertyPaths));
1267
                    }
1268
                }
1269
            }
1270
        }
1271
        return associatedTaxa;
1272
    }
1273

    
1274
    @Override
1275
    public Collection<TaxonBase<?>> listIndividualsAssociationTaxa(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start,
1276
            List<OrderHint> orderHints, List<String> propertyPaths) {
1277
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1278
        for (IndividualsAssociation individualsAssociation : listIndividualsAssociations(specimen, null, null, null, null)) {
1279
            if(individualsAssociation.getInDescription().isInstanceOf(TaxonDescription.class)){
1280
                TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(individualsAssociation.getInDescription(), TaxonDescription.class);
1281
                if(taxonDescription.getTaxon()!=null){
1282
                    associatedTaxa.add(taxonService.load(taxonDescription.getTaxon().getUuid(), propertyPaths));
1283
                }
1284
            }
1285
        }
1286
        return associatedTaxa;
1287
    }
1288

    
1289
    @Override
1290
    public Collection<DeterminationEvent> listDeterminationEvents(SpecimenOrObservationBase<?> specimen,
1291
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1292
        return dao.listDeterminationEvents(specimen, limit, start, orderHints, propertyPaths);
1293
    }
1294

    
1295
    @Override
1296
    public Map<DerivedUnit, Collection<SpecimenTypeDesignation>> listTypeDesignations(
1297
            Collection<DerivedUnit> specimens, Integer limit, Integer start,
1298
            List<OrderHint> orderHints, List<String> propertyPaths) {
1299
        Map<DerivedUnit, Collection<SpecimenTypeDesignation>> typeDesignationMap = new HashMap<>();
1300
        for (DerivedUnit specimen : specimens) {
1301
            Collection<SpecimenTypeDesignation> typeDesignations = listTypeDesignations(specimen, limit, start, orderHints, propertyPaths);
1302
            typeDesignationMap.put(specimen, typeDesignations);
1303
        }
1304
        return typeDesignationMap;
1305
    }
1306

    
1307
    @Override
1308
    public Collection<SpecimenTypeDesignation> listTypeDesignations(DerivedUnit specimen,
1309
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1310
        return dao.listTypeDesignations(specimen, limit, start, orderHints, propertyPaths);
1311
    }
1312

    
1313
    @Override
1314
    public Collection<DescriptionBase<?>> listDescriptionsWithDescriptionSpecimen(
1315
            SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints,
1316
            List<String> propertyPaths) {
1317
        return dao.listDescriptionsWithDescriptionSpecimen(specimen, limit, start, orderHints, propertyPaths);
1318
    }
1319

    
1320
    @Override
1321
    @Deprecated //this is not a service layer task so it may be removed in future versions
1322
    public Collection<DescriptionElementBase> getCharacterDataForSpecimen(SpecimenOrObservationBase<?> specimen) {
1323
        if (specimen != null) {
1324
            return specimen.characterData();
1325
        }else{
1326
            return new ArrayList<>();
1327
        }
1328
    }
1329

    
1330
    @Override
1331
    public Collection<DescriptionElementBase> getCharacterDataForSpecimen(UUID specimenUuid) {
1332
        SpecimenOrObservationBase<?> specimen = load(specimenUuid);
1333
        if (specimen != null) {
1334
            return getCharacterDataForSpecimen(specimen);
1335
        }
1336
        else{
1337
            throw new DataRetrievalFailureException("Specimen with the given uuid not found in the data base");
1338
        }
1339
    }
1340

    
1341

    
1342
    @Override
1343
    public Integer countByTitle(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config){
1344
        if (config instanceof FindOccurrencesConfigurator) {
1345
            FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
1346
            Taxon taxon = null;
1347
            if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
1348
                TaxonBase taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
1349
                if(taxonBase.isInstanceOf(Taxon.class)){
1350
                    taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
1351
                }
1352
            }
1353
            TaxonName taxonName = null;
1354
            if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
1355
                taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
1356
            }
1357
            /*TODO: #6484 Neither isRetrieveIndirectlyAssociatedSpecimens() nor the AssignmentStatus
1358
             * is currently reflected in the HQL query. So using these in the count method will
1359
             * significantly slow down this method as we have to retreive the entities instead of
1360
             * the just the amount.
1361
             */
1362
            if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens() || !occurrenceConfig.getAssignmentStatus().equals(AssignmentStatus.ALL_SPECIMENS)){
1363
                List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
1364
                occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
1365
                        occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1366
                        occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1367
                        occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
1368
                occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
1369
                return occurrences.size();
1370
            }
1371

    
1372
            return dao.countOccurrences(occurrenceConfig.getClazz(),
1373
                    occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1374
                    occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1375
                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
1376
        }
1377
        else{
1378
            return dao.countByTitle(config.getTitleSearchString());
1379
        }
1380
    }
1381

    
1382
    @Override
1383
    public Pager<UuidAndTitleCache<SpecimenOrObservationBase>> findByTitleUuidAndTitleCache(
1384
            FindOccurrencesConfigurator config){
1385
        List<UuidAndTitleCache<SpecimenOrObservationBase>> occurrences = new ArrayList<>();
1386
        Taxon taxon = null;
1387
        if(config.getAssociatedTaxonUuid()!=null){
1388
            TaxonBase taxonBase = taxonService.load(config.getAssociatedTaxonUuid());
1389
            if(taxonBase.isInstanceOf(Taxon.class)){
1390
                taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
1391
            }
1392
        }
1393
        TaxonName taxonName = null;
1394
        if(config.getAssociatedTaxonNameUuid()!=null){
1395
            taxonName = nameService.load(config.getAssociatedTaxonNameUuid());
1396
        }
1397
        occurrences.addAll(dao.findOccurrencesUuidAndTitleCache(config.getClazz(),
1398
                config.getTitleSearchString(), config.getSignificantIdentifier(),
1399
                config.getSpecimenType(), taxon, taxonName, config.getMatchMode(), null, null,
1400
                config.getOrderHints()));
1401

    
1402
        return new DefaultPagerImpl<UuidAndTitleCache<SpecimenOrObservationBase>>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
1403
    }
1404

    
1405
    @Override
1406
    public Pager<SpecimenOrObservationBase> findByTitle(
1407
            IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config) {
1408
        if (config instanceof FindOccurrencesConfigurator) {
1409
            FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
1410
            List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
1411
            Taxon taxon = null;
1412
            if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
1413
                TaxonBase taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
1414
                if(taxonBase.isInstanceOf(Taxon.class)){
1415
                    taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
1416
                }
1417
            }
1418
            TaxonName taxonName = null;
1419
            if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
1420
                taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
1421
            }
1422
            occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
1423
                    occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1424
                    occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1425
                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
1426
            occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
1427

    
1428
            return new DefaultPagerImpl<SpecimenOrObservationBase>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
1429
        }
1430
        return super.findByTitle(config);
1431
    }
1432

    
1433
    private List<SpecimenOrObservationBase> filterOccurencesByAssignmentAndHierarchy(
1434
            FindOccurrencesConfigurator occurrenceConfig, List<SpecimenOrObservationBase> occurrences, Taxon taxon,
1435
            TaxonName taxonName) {
1436
        //filter out (un-)assigned specimens
1437
        if(taxon==null && taxonName==null){
1438
            AssignmentStatus assignmentStatus = occurrenceConfig.getAssignmentStatus();
1439
            List<SpecimenOrObservationBase<?>> specimenWithAssociations = new ArrayList<>();
1440
            if(!assignmentStatus.equals(AssignmentStatus.ALL_SPECIMENS)){
1441
                for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
1442
                    Collection<TaxonBase<?>> associatedTaxa = listAssociatedTaxa(specimenOrObservationBase, null, null, null, null);
1443
                    if(!associatedTaxa.isEmpty()){
1444
                        specimenWithAssociations.add(specimenOrObservationBase);
1445
                    }
1446
                }
1447
            }
1448
            if(assignmentStatus.equals(AssignmentStatus.UNASSIGNED_SPECIMENS)){
1449
                occurrences.removeAll(specimenWithAssociations);
1450
            }
1451
            if(assignmentStatus.equals(AssignmentStatus.ASSIGNED_SPECIMENS)){
1452
                occurrences = new ArrayList<>(specimenWithAssociations);
1453
            }
1454
        }
1455
        // indirectly associated specimens
1456
        if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens()){
1457
            List<SpecimenOrObservationBase> indirectlyAssociatedOccurrences = new ArrayList<>(occurrences);
1458
            for (SpecimenOrObservationBase specimen : occurrences) {
1459
                List<SpecimenOrObservationBase<?>> allHierarchyDerivates = getAllHierarchyDerivatives(specimen);
1460
                for (SpecimenOrObservationBase<?> specimenOrObservationBase : allHierarchyDerivates) {
1461
                    if(!occurrences.contains(specimenOrObservationBase)){
1462
                        indirectlyAssociatedOccurrences.add(specimenOrObservationBase);
1463
                    }
1464
                }
1465
            }
1466
            occurrences = indirectlyAssociatedOccurrences;
1467
        }
1468
        return occurrences;
1469
    }
1470

    
1471
    @Override
1472
    public List<SpecimenOrObservationBase<?>> getAllHierarchyDerivatives(SpecimenOrObservationBase<?> specimen){
1473
        List<SpecimenOrObservationBase<?>> allHierarchyDerivatives = new ArrayList<>();
1474
        Collection<FieldUnit> fieldUnits = getFieldUnits(specimen.getUuid(), null);
1475
        if(fieldUnits.isEmpty()){
1476
            allHierarchyDerivatives.add(specimen);
1477
            allHierarchyDerivatives.addAll(getAllChildDerivatives(specimen));
1478
        }
1479
        else{
1480
            for (FieldUnit fieldUnit : fieldUnits) {
1481
                allHierarchyDerivatives.add(fieldUnit);
1482
                allHierarchyDerivatives.addAll(getAllChildDerivatives(fieldUnit));
1483
            }
1484
        }
1485
        return allHierarchyDerivatives;
1486
    }
1487

    
1488
    @Override
1489
    public List<DerivedUnit> getAllChildDerivatives(UUID specimenUuid){
1490
        return getAllChildDerivatives(load(specimenUuid));
1491
    }
1492

    
1493
    @Override
1494
    public List<DerivedUnit> getAllChildDerivatives(SpecimenOrObservationBase<?> specimen){
1495
        if (specimen == null){
1496
            return null;
1497
        }
1498
        List<DerivedUnit> childDerivate = new ArrayList<>();
1499
        Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
1500
        for (DerivationEvent derivationEvent : derivationEvents) {
1501
            Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1502
            for (DerivedUnit derivedUnit : derivatives) {
1503
                childDerivate.add(derivedUnit);
1504
                childDerivate.addAll(getAllChildDerivatives(derivedUnit.getUuid()));
1505
            }
1506
        }
1507
        return childDerivate;
1508
    }
1509

    
1510
    @Override
1511
    public int countOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config){
1512
        return countByTitle(config);
1513
    }
1514

    
1515
    /**
1516
     * {@inheritDoc}
1517
     */
1518
    @Override
1519
    public List<FieldUnit> getFieldUnitsForGatheringEvent(UUID gatheringEventUuid) {
1520
        return dao.getFieldUnitsForGatheringEvent(gatheringEventUuid, null, null, null, null);
1521
    }
1522

    
1523
}
(81-81/105)