Project

General

Profile

Download (86.6 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.DNASampleDTO;
52
import eu.etaxonomy.cdm.api.service.dto.DerivateDTO;
53
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO;
54
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO.ContigFile;
55
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO.Link;
56
import eu.etaxonomy.cdm.api.service.dto.DerivateDataDTO.MolecularData;
57
import eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO;
58
import eu.etaxonomy.cdm.api.service.dto.PreservedSpecimenDTO;
59
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
60
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
61
import eu.etaxonomy.cdm.api.service.pager.Pager;
62
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
63
import eu.etaxonomy.cdm.api.service.search.ILuceneIndexToolProvider;
64
import eu.etaxonomy.cdm.api.service.search.ISearchResultBuilder;
65
import eu.etaxonomy.cdm.api.service.search.LuceneParseException;
66
import eu.etaxonomy.cdm.api.service.search.LuceneSearch;
67
import eu.etaxonomy.cdm.api.service.search.QueryFactory;
68
import eu.etaxonomy.cdm.api.service.search.SearchResult;
69
import eu.etaxonomy.cdm.api.service.search.SearchResultBuilder;
70
import eu.etaxonomy.cdm.api.service.util.TaxonRelationshipEdge;
71
import eu.etaxonomy.cdm.common.CdmUtils;
72
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
73
import eu.etaxonomy.cdm.format.CdmFormatterFactory;
74
import eu.etaxonomy.cdm.format.ICdmFormatter.FormatKey;
75
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
76
import eu.etaxonomy.cdm.model.CdmBaseType;
77
import eu.etaxonomy.cdm.model.agent.AgentBase;
78
import eu.etaxonomy.cdm.model.common.CdmBase;
79
import eu.etaxonomy.cdm.model.common.Language;
80
import eu.etaxonomy.cdm.model.description.CategoricalData;
81
import eu.etaxonomy.cdm.model.description.DescriptionBase;
82
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
83
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
84
import eu.etaxonomy.cdm.model.description.QuantitativeData;
85
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
86
import eu.etaxonomy.cdm.model.description.TaxonDescription;
87
import eu.etaxonomy.cdm.model.location.Country;
88
import eu.etaxonomy.cdm.model.location.NamedArea;
89
import eu.etaxonomy.cdm.model.location.Point;
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.model.term.DefinedTerm;
112
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
113
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
114
import eu.etaxonomy.cdm.persistence.dao.initializer.AbstractBeanInitializer;
115
import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;
116
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
117
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
118
import eu.etaxonomy.cdm.persistence.query.AssignmentStatus;
119
import eu.etaxonomy.cdm.persistence.query.OrderHint;
120
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
121
import eu.etaxonomy.cdm.strategy.cache.common.IdentifiableEntityDefaultCacheStrategy;
122

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

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

    
133
    @Autowired
134
    private IDefinedTermDao definedTermDao;
135

    
136
    @Autowired
137
    private IDescriptionService descriptionService;
138

    
139
    @Autowired
140
    private INameService nameService;
141

    
142
    @Autowired
143
    private IEventBaseService eventService;
144

    
145
    @Autowired
146
    private ITaxonService taxonService;
147

    
148
    @Autowired
149
    private ISequenceService sequenceService;
150

    
151
    @Autowired
152
    private AbstractBeanInitializer beanInitializer;
153

    
154
    @Autowired
155
    private ILuceneIndexToolProvider luceneIndexToolProvider;
156

    
157
    private static final String SEPARATOR_STRING = ", ";
158

    
159
    private static final List<String> DERIVED_UNIT_INIT_STRATEGY =  Arrays.asList(new String []{
160
            "derivedFrom.derivatives",
161
            "derivedFrom.originals",
162
            "specimenTypeDesignations.*",
163
            "specimenTypeDesignations.citation.*",
164
            "specimenTypeDesignations.homotypicalGroup.*",
165
            "specimenTypeDesignations.typifiedNames",
166
            "collection.$"
167
    });
168

    
169
    public OccurrenceServiceImpl() {
170
        logger.debug("Load OccurrenceService Bean");
171
    }
172

    
173

    
174
    @Override
175
    @Transactional(readOnly = false)
176
    public void updateCaches(Class<? extends SpecimenOrObservationBase> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<SpecimenOrObservationBase> cacheStrategy, IProgressMonitor monitor) {
177
        if (clazz == null) {
178
            clazz = SpecimenOrObservationBase.class;
179
        }
180
        super.updateCachesImpl(clazz, stepSize, cacheStrategy, monitor);
181
    }
182

    
183
    /**
184
     * FIXME Candidate for harmonization
185
     * move to termService
186
     */
187
    @Override
188
    public Country getCountryByIso(String iso639) {
189
        return this.definedTermDao.getCountryByIso(iso639);
190

    
191
    }
192

    
193
    /**
194
     * FIXME Candidate for harmonization
195
     * move to termService
196
     */
197
    @Override
198
    public List<Country> getCountryByName(String name) {
199
        List<? extends DefinedTermBase> terms = this.definedTermDao.findByTitleWithRestrictions(Country.class, name, null, null, null, null, null, null);
200
        List<Country> countries = new ArrayList<>();
201
        for (int i = 0; i < terms.size(); i++) {
202
            countries.add((Country) terms.get(i));
203
        }
204
        return countries;
205
    }
206

    
207
    @Override
208
    @Autowired
209
    protected void setDao(IOccurrenceDao dao) {
210
        this.dao = dao;
211
    }
212

    
213
    @Override
214
    public Pager<DerivationEvent> getDerivationEvents(SpecimenOrObservationBase occurence, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
215
        long numberOfResults = dao.countDerivationEvents(occurence);
216

    
217
        List<DerivationEvent> results = new ArrayList<>();
218
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
219
            results = dao.getDerivationEvents(occurence, pageSize, pageNumber, propertyPaths);
220
        }
221

    
222
        return new DefaultPagerImpl<DerivationEvent>(pageNumber, numberOfResults, pageSize, results);
223
    }
224

    
225
    @Override
226
    public long countDeterminations(SpecimenOrObservationBase occurence, TaxonBase taxonbase) {
227
        return dao.countDeterminations(occurence, taxonbase);
228
    }
229

    
230
    @Override
231
    public Pager<DeterminationEvent> getDeterminations(SpecimenOrObservationBase occurrence, TaxonBase taxonBase,
232
            Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
233
        long numberOfResults = dao.countDeterminations(occurrence, taxonBase);
234

    
235
        List<DeterminationEvent> results = new ArrayList<>();
236
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
237
            results = dao.getDeterminations(occurrence, taxonBase, pageSize, pageNumber, propertyPaths);
238
        }
239

    
240
        return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
241
    }
242

    
243
    @Override
244
    public Pager<Media> getMedia(SpecimenOrObservationBase occurence, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
245
        long numberOfResults = dao.countMedia(occurence);
246

    
247
        List<Media> results = new ArrayList<>();
248
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
249
            results = dao.getMedia(occurence, pageSize, pageNumber, propertyPaths);
250
        }
251

    
252
        return new DefaultPagerImpl<Media>(pageNumber, numberOfResults, pageSize, results);
253
    }
254

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

    
291
    @Override
292
    public Pager<SpecimenOrObservationBase> list(Class<? extends SpecimenOrObservationBase> type, TaxonName determinedAs,
293
            Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
294
        long numberOfResults = dao.count(type, determinedAs);
295
        @SuppressWarnings("rawtypes")
296
        List<SpecimenOrObservationBase> results = new ArrayList<>();
297
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
298
            results = dao.list(type, determinedAs, pageSize, pageNumber, orderHints, propertyPaths);
299
        }
300
        return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
301
    }
302

    
303
    @Override
304
    public Pager<SpecimenOrObservationBase> list(Class<? extends SpecimenOrObservationBase> type, TaxonBase determinedAs,
305
            Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
306
        long numberOfResults = dao.count(type, determinedAs);
307
        @SuppressWarnings("rawtypes")
308
        List<SpecimenOrObservationBase> results = new ArrayList<>();
309
        if(numberOfResults > 0) { // no point checking again  //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
310
            results = dao.list(type, determinedAs, pageSize, pageNumber, orderHints, propertyPaths);
311
        }
312
        return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
313
    }
314

    
315
    @Override
316
    public List<UuidAndTitleCache<DerivedUnit>> getDerivedUnitUuidAndTitleCache(Integer limit, String pattern) {
317
        return dao.getDerivedUnitUuidAndTitleCache(limit, pattern);
318
    }
319

    
320
    @Override
321
    public List<UuidAndTitleCache<FieldUnit>> getFieldUnitUuidAndTitleCache() {
322
        return dao.getFieldUnitUuidAndTitleCache();
323
    }
324

    
325
    @Override
326
    public DerivedUnitFacade getDerivedUnitFacade(DerivedUnit derivedUnit, List<String> propertyPaths) throws DerivedUnitFacadeNotSupportedException {
327
        derivedUnit = (DerivedUnit) dao.load(derivedUnit.getUuid(), null);
328
        DerivedUnitFacadeConfigurator config = DerivedUnitFacadeConfigurator.NewInstance();
329
        config.setThrowExceptionForNonSpecimenPreservationMethodRequest(false);
330
        DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(derivedUnit, config);
331
        beanInitializer.initialize(derivedUnitFacade, propertyPaths);
332
        return derivedUnitFacade;
333
    }
334

    
335
    @Override
336
    public List<DerivedUnitFacade> listDerivedUnitFacades(
337
            DescriptionBase description, List<String> propertyPaths) {
338

    
339
        List<DerivedUnitFacade> derivedUnitFacadeList = new ArrayList<>();
340
        IndividualsAssociation tempIndividualsAssociation;
341
        SpecimenOrObservationBase tempSpecimenOrObservationBase;
342
        List<IndividualsAssociation> elements = descriptionService.listDescriptionElements(description, null, IndividualsAssociation.class, null, 0, Arrays.asList(new String []{"associatedSpecimenOrObservation"}));
343
        for (IndividualsAssociation element : elements) {
344
            tempIndividualsAssociation = HibernateProxyHelper.deproxy(element, IndividualsAssociation.class);
345
            if (tempIndividualsAssociation.getAssociatedSpecimenOrObservation() != null) {
346
                tempSpecimenOrObservationBase = HibernateProxyHelper.deproxy(tempIndividualsAssociation.getAssociatedSpecimenOrObservation(), SpecimenOrObservationBase.class);
347
                if (tempSpecimenOrObservationBase.isInstanceOf(DerivedUnit.class)) {
348
                    try {
349
                        derivedUnitFacadeList.add(DerivedUnitFacade.NewInstance(HibernateProxyHelper.deproxy(tempSpecimenOrObservationBase, DerivedUnit.class)));
350
                    } catch (DerivedUnitFacadeNotSupportedException e) {
351
                        logger.warn(tempIndividualsAssociation.getAssociatedSpecimenOrObservation().getTitleCache() + " : " + e.getMessage());
352
                    }
353
                }
354
            }
355
        }
356

    
357
        beanInitializer.initializeAll(derivedUnitFacadeList, propertyPaths);
358

    
359
        return derivedUnitFacadeList;
360
    }
361

    
362

    
363
    @Override
364
    public <T extends SpecimenOrObservationBase> List<T> listByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includeRelationships,
365
            Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
366

    
367
        return pageByAssociatedTaxon(type, includeRelationships, associatedTaxon, maxDepth, pageSize, pageNumber, orderHints, propertyPaths).getRecords();
368
    }
369

    
370
    @Override
371
    public Collection<SpecimenNodeWrapper> listUuidAndTitleCacheByAssociatedTaxon(List<UUID> taxonNodeUuids,
372
            Integer limit, Integer start) {
373
        return dao.listUuidAndTitleCacheByAssociatedTaxon(taxonNodeUuids, limit, start);
374
        }
375

    
376
    @Override
377
    public Collection<SpecimenOrObservationBase> listFieldUnitsByAssociatedTaxon(Taxon associatedTaxon, List<OrderHint> orderHints, List<String> propertyPaths) {
378
        return pageFieldUnitsByAssociatedTaxon(null, associatedTaxon, null, null, null, null, propertyPaths).getRecords();
379
    }
380

    
381
    @Override
382
    public Pager<SpecimenOrObservationBase> pageFieldUnitsByAssociatedTaxon(Set<TaxonRelationshipEdge> includeRelationships,
383
            Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
384
            List<String> propertyPaths) {
385

    
386
        if (!getSession().contains(associatedTaxon)) {
387
            associatedTaxon = (Taxon) taxonService.load(associatedTaxon.getUuid());
388
        }
389

    
390
        // gather the IDs of all relevant field units
391
        Set<UUID> fieldUnitUuids = new HashSet<>();
392
        List<SpecimenOrObservationBase> records = listByAssociatedTaxon(null, includeRelationships, associatedTaxon, maxDepth, null, null, orderHints, propertyPaths);
393
        for (SpecimenOrObservationBase<?> specimen : records) {
394
            for (FieldUnit fieldUnit : findFieldUnits(specimen.getUuid(), null)) {
395
                fieldUnitUuids.add(fieldUnit.getUuid());
396
            }
397
        }
398
        //dao.list() does the paging of the field units. Passing the field units directly to the Pager would not work
399
        List<SpecimenOrObservationBase> fieldUnits = dao.list(fieldUnitUuids, pageSize, pageNumber, orderHints, propertyPaths);
400
        return new DefaultPagerImpl<>(pageNumber, fieldUnitUuids.size(), pageSize, fieldUnits);
401
    }
402

    
403
    @Override
404
    public FieldUnitDTO assembleFieldUnitDTO(FieldUnit fieldUnit) {
405

    
406
        if (!getSession().contains(fieldUnit)) {
407
            fieldUnit = (FieldUnit) load(fieldUnit.getUuid());
408
        }
409

    
410
        FieldUnitDTO fieldUnitDTO = new FieldUnitDTO(fieldUnit);
411

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

    
442
        // Herbaria map
443
        Map<eu.etaxonomy.cdm.model.occurrence.Collection, Integer> collectionToCountMap = new HashMap<>();
444
        // List of accession numbers for citation
445
        List<String> preservedSpecimenAccessionNumbers = new ArrayList<>();
446

    
447
        // assemble preserved specimen DTOs
448
        Set<DerivationEvent> derivationEvents = fieldUnit.getDerivationEvents();
449
        for (DerivationEvent derivationEvent : derivationEvents) {
450
            Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
451
            for (DerivedUnit derivedUnit : derivatives) {
452
                if(!derivedUnit.isPublish()){
453
                    continue;
454
                }
455
                // collect accession numbers for citation
456
                String identifier = getMostSignificantIdentifier(derivedUnit);
457
                // collect collections for herbaria column
458
                eu.etaxonomy.cdm.model.occurrence.Collection collection = derivedUnit.getCollection();
459
                if (collection != null) {
460
                    //combine collection with identifier
461
                    if (identifier != null) {
462
                        if(collection.getCode()!=null){
463
                            identifier = (collection.getCode()!=null?collection.getCode():"[no collection]")+" "+identifier;
464
                        }
465
                        preservedSpecimenAccessionNumbers.add(identifier);
466
                    }
467

    
468
                    Integer herbariumCount = collectionToCountMap.get(collection);
469
                    if (herbariumCount == null) {
470
                        herbariumCount = 0;
471
                    }
472
                    collectionToCountMap.put(collection, herbariumCount + 1);
473
                }
474
                if (derivedUnit.getRecordBasis().equals(SpecimenOrObservationType.PreservedSpecimen)) {
475
                    PreservedSpecimenDTO preservedSpecimenDTO = assemblePreservedSpecimenDTO(derivedUnit, fieldUnitDTO);
476
                    fieldUnitDTO.addDerivate(preservedSpecimenDTO);
477
                    fieldUnitDTO.setHasCharacterData(fieldUnitDTO.isHasCharacterData() || preservedSpecimenDTO.isHasCharacterData());
478
                    fieldUnitDTO.setHasDetailImage(fieldUnitDTO.isHasDetailImage() || preservedSpecimenDTO.isHasDetailImage());
479
                    fieldUnitDTO.setHasDna(fieldUnitDTO.isHasDna() || preservedSpecimenDTO.isHasDna());
480
                    fieldUnitDTO.setHasSpecimenScan(fieldUnitDTO.isHasSpecimenScan() || preservedSpecimenDTO.isHasSpecimenScan());
481
                }
482
            }
483
        }
484
        // assemble derivate data DTO
485
        assembleDerivateDataDTO(fieldUnitDTO, fieldUnit);
486

    
487
        // assemble citation
488
        String citation = fieldUnit.getTitleCache();
489
        if((CdmUtils.isBlank(citation) || citation.equals(IdentifiableEntityDefaultCacheStrategy.TITLE_CACHE_GENERATION_NOT_IMPLEMENTED))
490
                && !fieldUnit.isProtectedTitleCache()){
491
            fieldUnit.setTitleCache(null);
492
            citation = fieldUnit.getTitleCache();
493
        }
494
        if (!preservedSpecimenAccessionNumbers.isEmpty()) {
495
            citation += " (";
496
            for (String accessionNumber : preservedSpecimenAccessionNumbers) {
497
                if (!accessionNumber.isEmpty()) {
498
                    citation += accessionNumber + SEPARATOR_STRING;
499
                }
500
            }
501
            citation = removeTail(citation, SEPARATOR_STRING);
502
            citation += ")";
503
        }
504
        fieldUnitDTO.setCitation(citation);
505

    
506
        // assemble herbaria string
507
        String herbariaString = "";
508
        for (Entry<eu.etaxonomy.cdm.model.occurrence.Collection, Integer> e : collectionToCountMap.entrySet()) {
509
            eu.etaxonomy.cdm.model.occurrence.Collection collection = e.getKey();
510
            if (collection.getCode() != null) {
511
                herbariaString += collection.getCode();
512
            }
513
            if (e.getValue() > 1) {
514
                herbariaString += "(" + e.getValue() + ")";
515
            }
516
            herbariaString += SEPARATOR_STRING;
517
        }
518
        herbariaString = removeTail(herbariaString, SEPARATOR_STRING);
519
        fieldUnitDTO.setCollection(herbariaString);
520

    
521
        return fieldUnitDTO;
522
    }
523

    
524
    @Override
525
    public PreservedSpecimenDTO assemblePreservedSpecimenDTO(DerivedUnit derivedUnit) {
526
        return assemblePreservedSpecimenDTO(derivedUnit, null);
527
    }
528

    
529
    @Override
530
    public String getMostSignificantIdentifier(DerivedUnit derivedUnit) {
531
        if (derivedUnit.getAccessionNumber() != null && !derivedUnit.getAccessionNumber().isEmpty()) {
532
            return derivedUnit.getAccessionNumber();
533
        }
534
        else if(derivedUnit.getBarcode()!=null && !derivedUnit.getBarcode().isEmpty()){
535
            return derivedUnit.getBarcode();
536
        }
537
        else if(derivedUnit.getCatalogNumber()!=null && !derivedUnit.getCatalogNumber().isEmpty()){
538
            return derivedUnit.getCatalogNumber();
539
        }
540
        return null;
541
    }
542

    
543
    public PreservedSpecimenDTO assemblePreservedSpecimenDTO(DerivedUnit derivedUnit, FieldUnitDTO fieldUnitDTO) {
544
        if (!getSession().contains(derivedUnit)) {
545
            derivedUnit = (DerivedUnit) load(derivedUnit.getUuid());
546
        }
547
        PreservedSpecimenDTO preservedSpecimenDTO = new PreservedSpecimenDTO(derivedUnit);
548

    
549
        //specimen identifier
550
        FormatKey collectionKey = FormatKey.COLLECTION_CODE;
551
        String specimenIdentifier = CdmFormatterFactory.format(derivedUnit, collectionKey);
552
        if (CdmUtils.isBlank(specimenIdentifier)) {
553
            collectionKey = FormatKey.COLLECTION_NAME;
554
        }
555
        specimenIdentifier = CdmFormatterFactory.format(derivedUnit, new FormatKey[] {
556
                collectionKey, FormatKey.SPACE,
557
                FormatKey.MOST_SIGNIFICANT_IDENTIFIER, FormatKey.SPACE });
558
        if(CdmUtils.isBlank(specimenIdentifier)){
559
            specimenIdentifier = derivedUnit.getTitleCache();
560
        }
561
        if(CdmUtils.isBlank(specimenIdentifier)){
562
            specimenIdentifier = derivedUnit.getUuid().toString();
563
        }
564
        preservedSpecimenDTO.setAccessionNumber(specimenIdentifier);
565

    
566

    
567
        //preferred stable URI
568
        preservedSpecimenDTO.setPreferredStableUri(derivedUnit.getPreferredStableUri());
569

    
570
        // citation
571
        Collection<FieldUnit> fieldUnits = getFieldUnits(derivedUnit, null);
572
        if (fieldUnits.size() == 1) {
573
            preservedSpecimenDTO.setCitation(fieldUnits.iterator().next().getTitleCache());
574
        }
575
        else{
576
            preservedSpecimenDTO.setCitation("No Citation available. This specimen either has no or multiple field units.");
577
        }
578

    
579
        // character state data
580
        Collection<DescriptionElementBase> characterDataForSpecimen = getCharacterDataForSpecimen(derivedUnit);
581
        if (!characterDataForSpecimen.isEmpty()) {
582
            if (fieldUnitDTO != null) {
583
                fieldUnitDTO.setHasCharacterData(true);
584
            }
585
        }
586
        for (DescriptionElementBase descriptionElementBase : characterDataForSpecimen) {
587
            String character = descriptionElementBase.getFeature().getLabel();
588
            ArrayList<Language> languages = new ArrayList<>(Collections.singleton(Language.DEFAULT()));
589
            if (descriptionElementBase instanceof QuantitativeData) {
590
                QuantitativeData quantitativeData = (QuantitativeData) descriptionElementBase;
591
                DefaultQuantitativeDescriptionBuilder builder = new DefaultQuantitativeDescriptionBuilder();
592
                String state = builder.build(quantitativeData, languages).getText(Language.DEFAULT());
593
                preservedSpecimenDTO.addCharacterData(character, state);
594
            }
595
            else if(descriptionElementBase instanceof CategoricalData){
596
                CategoricalData categoricalData = (CategoricalData) descriptionElementBase;
597
                DefaultCategoricalDescriptionBuilder builder = new DefaultCategoricalDescriptionBuilder();
598
                String state = builder.build(categoricalData, languages).getText(Language.DEFAULT());
599
                preservedSpecimenDTO.addCharacterData(character, state);
600
            }
601
        }
602
        // check type designations
603
        Collection<SpecimenTypeDesignation> specimenTypeDesignations = listTypeDesignations(derivedUnit, null, null, null, null);
604
        for (SpecimenTypeDesignation specimenTypeDesignation : specimenTypeDesignations) {
605
            if (fieldUnitDTO != null) {
606
                fieldUnitDTO.setHasType(true);
607
            }
608
            TypeDesignationStatusBase<?> typeStatus = specimenTypeDesignation.getTypeStatus();
609
            Set<TaxonName> typifiedNames = specimenTypeDesignation.getTypifiedNames();
610
            List<String> typedTaxaNames = new ArrayList<>();
611
            for (TaxonName taxonName : typifiedNames) {
612
                typedTaxaNames.add(taxonName.getTitleCache());
613
            }
614
            preservedSpecimenDTO.addTypes(typeStatus!=null?typeStatus.getLabel():"", typedTaxaNames);
615
        }
616

    
617
        // individuals associations
618
        Collection<IndividualsAssociation> individualsAssociations = listIndividualsAssociations(derivedUnit, null, null, null, null);
619
        for (IndividualsAssociation individualsAssociation : individualsAssociations) {
620
            if (individualsAssociation.getInDescription() != null) {
621
                if (individualsAssociation.getInDescription().isInstanceOf(TaxonDescription.class)) {
622
                    TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(individualsAssociation.getInDescription(), TaxonDescription.class);
623
                    Taxon taxon = taxonDescription.getTaxon();
624
                    if (taxon != null) {
625
                        preservedSpecimenDTO.addAssociatedTaxon(taxon);
626
                    }
627
                }
628
            }
629
        }
630
        // assemble sub derivates
631
        preservedSpecimenDTO.setDerivateDataDTO(assembleDerivateDataDTO(preservedSpecimenDTO, derivedUnit));
632
        return preservedSpecimenDTO;
633
    }
634

    
635
    private DerivateDataDTO assembleDerivateDataDTO(DerivateDTO derivateDTO, SpecimenOrObservationBase<?> specimenOrObservation) {
636
        DerivateDataDTO derivateDataDTO = new DerivateDataDTO();
637
        Collection<DerivedUnit> childDerivates = getDerivedUnitsFor(specimenOrObservation);
638
        for (DerivedUnit childDerivate : childDerivates) {
639
            // assemble molecular data
640
            //pattern: DNAMarker [contig1, primer1_1, primer1_2, ...][contig2, primer2_1, ...]...
641
            if (childDerivate.isInstanceOf(DnaSample.class)) {
642
                if (childDerivate.getRecordBasis() == SpecimenOrObservationType.TissueSample) {
643
                    // TODO implement TissueSample assembly for web service
644
                }
645
                if (childDerivate.getRecordBasis() == SpecimenOrObservationType.DnaSample) {
646

    
647
                    DnaSample dna = HibernateProxyHelper.deproxy(childDerivate, DnaSample.class);
648
                    if (!dna.getSequences().isEmpty()) {
649
                        derivateDTO.setHasDna(true);
650
                    }
651
                    for (Sequence sequence : dna.getSequences()) {
652
                        URI boldUri = null;
653
                        try {
654
                            boldUri = sequence.getBoldUri();
655
                        } catch (URISyntaxException e1) {
656
                            logger.error("Could not create BOLD URI", e1);
657
                        }
658
                        final DefinedTerm dnaMarker = sequence.getDnaMarker();
659
                        Link providerLink = null;
660
                        if(boldUri!=null && dnaMarker!=null){
661
                        	providerLink = new DerivateDataDTO.Link(boldUri, dnaMarker.getLabel());
662
                        }
663
                        MolecularData molecularData = derivateDataDTO.addProviderLink(providerLink);
664

    
665
                        //contig file
666
                        ContigFile contigFile = null;
667
                        if (sequence.getContigFile() != null) {
668
                            MediaRepresentationPart contigMediaRepresentationPart = MediaUtils.getFirstMediaRepresentationPart(sequence.getContigFile());
669
                            if (contigMediaRepresentationPart != null) {
670
                                contigFile = molecularData.addContigFile(new Link(contigMediaRepresentationPart.getUri(), "contig"));
671
                            }
672
                        }
673
                        else{
674
                        	contigFile = molecularData.addContigFile(null);
675
                        }
676
                        // primer files
677
                        if (sequence.getSingleReads() != null) {
678
                            int readCount = 1;
679
                            for (SingleRead singleRead : sequence.getSingleReads()) {
680
                                MediaRepresentationPart pherogramMediaRepresentationPart = MediaUtils.getFirstMediaRepresentationPart(singleRead.getPherogram());
681
                                if (pherogramMediaRepresentationPart != null) {
682
                                    contigFile.addPrimerLink(pherogramMediaRepresentationPart.getUri(), "read"+readCount++);
683
                                }
684
                            }
685
                        }
686
                    }
687
                }
688
            }
689
            // assemble media data
690
            else if (childDerivate.isInstanceOf(MediaSpecimen.class)) {
691
                MediaSpecimen media = HibernateProxyHelper.deproxy(childDerivate, MediaSpecimen.class);
692

    
693
                URI mediaUri = getMediaUri(media);
694
                if (media.getKindOfUnit() != null) {
695
                    // specimen scan
696
                    if (media.getKindOfUnit().getUuid().equals(DefinedTerm.uuidSpecimenScan)) {
697
                        derivateDataDTO.addSpecimenScanUuid(media.getMediaSpecimen().getUuid());
698
                        derivateDTO.setHasSpecimenScan(true);
699
                        String imageLinkText = "scan";
700
                        if (derivateDTO instanceof PreservedSpecimenDTO && ((PreservedSpecimenDTO) derivateDTO).getAccessionNumber() != null) {
701
                            imageLinkText = ((PreservedSpecimenDTO) derivateDTO).getAccessionNumber();
702
                        }
703
                        derivateDataDTO.addSpecimenScan(mediaUri, imageLinkText);
704
                    }
705
                    // detail image
706
                    else if (media.getKindOfUnit().getUuid().equals(DefinedTerm.uuidDetailImage)) {
707
                        derivateDataDTO.addDetailImageUuid(media.getMediaSpecimen().getUuid());
708
                        derivateDTO.setHasDetailImage(true);
709
                        String motif = "detail image";
710
                        if (media.getMediaSpecimen()!=null){
711
                        	if(CdmUtils.isNotBlank(media.getMediaSpecimen().getTitleCache())) {
712
                        		motif = media.getMediaSpecimen().getTitleCache();
713
                        	}
714
                        }
715
                        derivateDataDTO.addDetailImage(mediaUri, motif);
716
                    }
717
                }
718
            }
719
        }
720
        return derivateDataDTO;
721
    }
722

    
723
    private String removeTail(String string, final String tail) {
724
        if (string.endsWith(tail)) {
725
            string = string.substring(0, string.length() - tail.length());
726
        }
727
        return string;
728
    }
729

    
730
    private URI getMediaUri(MediaSpecimen mediaSpecimen) {
731
        URI mediaUri = null;
732
        Collection<MediaRepresentation> mediaRepresentations = mediaSpecimen.getMediaSpecimen().getRepresentations();
733
        if (mediaRepresentations != null && !mediaRepresentations.isEmpty()) {
734
            Collection<MediaRepresentationPart> mediaRepresentationParts = mediaRepresentations.iterator().next().getParts();
735
            if (mediaRepresentationParts != null && !mediaRepresentationParts.isEmpty()) {
736
                MediaRepresentationPart part = mediaRepresentationParts.iterator().next();
737
                if (part.getUri() != null) {
738
                    mediaUri = part.getUri();
739
                }
740
            }
741
        }
742
        return mediaUri;
743
    }
744

    
745
    private Collection<DerivedUnit> getDerivedUnitsFor(SpecimenOrObservationBase<?> specimen) {
746
        Collection<DerivedUnit> derivedUnits = new ArrayList<>();
747
        for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
748
            for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
749
                derivedUnits.add(derivative);
750
                derivedUnits.addAll(getDerivedUnitsFor(derivative));
751
            }
752
        }
753
        return derivedUnits;
754
    }
755

    
756
    private Set<DerivateDTO> getDerivedUnitDTOsFor(DerivateDTO specimenDto, DerivedUnit specimen, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
757
        Set<DerivateDTO> derivedUnits = new HashSet<>();
758
//        load
759
        for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
760
            for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
761
                if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
762
                    PreservedSpecimenDTO dto;
763
                    if (derivative instanceof DnaSample) {
764
                        dto = new DNASampleDTO(derivative);
765
                    } else {
766
                        dto = new PreservedSpecimenDTO(derivative);
767
                    }
768
                    alreadyCollectedSpecimen.put(dto.getUuid(), dto);
769
                    dto.addAllDerivates(getDerivedUnitDTOsFor(dto, derivative, alreadyCollectedSpecimen));
770
                    derivedUnits.add(dto);
771
                }
772
            }
773
        }
774
        return derivedUnits;
775
    }
776

    
777
//    private Set<DerivateDTO> getDerivedUnitDTOsFor(DerivateDTO specimenDto, DerivedUnit specimen, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
778
//        Set<DerivateDTO> derivedUnits = new HashSet<>();
779
////        load
780
//        for (DerivationEvent derivationEvent : specimen.getDerivationEvents()) {
781
//            for (DerivedUnit derivative : derivationEvent.getDerivatives()) {
782
//                if (!alreadyCollectedSpecimen.containsKey(specimenDto.getUuid())){
783
//                    PreservedSpecimenDTO dto;
784
//                    if (derivative instanceof DnaSample){
785
//                        dto = DNASampleDTO.newInstance(derivative);
786
//                    }else{
787
//                        dto = PreservedSpecimenDTO.newInstance(derivative);
788
//                    }
789
//                    alreadyCollectedSpecimen.put(dto.getUuid(), dto);
790
//                    dto.addAllDerivates(getDerivedUnitDTOsFor(dto, derivative, alreadyCollectedSpecimen));
791
//                    derivedUnits.add(dto);
792
//                }
793
//            }
794
//        }
795
//        return derivedUnits;
796
//    }
797

    
798

    
799
    @SuppressWarnings("unchecked")
800
    @Override
801
    public <T extends SpecimenOrObservationBase> Pager<T> pageByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includedRelationships,
802
            Taxon associatedTaxon, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
803

    
804
        Set<Taxon> taxa = new HashSet<>();
805
        Set<Integer> occurrenceIds = new HashSet<>();
806
        List<T> occurrences = new ArrayList<>();
807
        boolean includeUnpublished = INCLUDE_UNPUBLISHED;
808

    
809
        // Integer limit = PagerUtils.limitFor(pageSize);
810
        // Integer start = PagerUtils.startFor(pageSize, pageNumber);
811

    
812
        if (!getSession().contains(associatedTaxon)) {
813
            associatedTaxon = (Taxon) taxonService.load(associatedTaxon.getUuid());
814
        }
815

    
816
        if (includedRelationships != null) {
817
            taxa = taxonService.listRelatedTaxa(associatedTaxon, includedRelationships, maxDepth, includeUnpublished, null, null, propertyPaths);
818
        }
819

    
820
        taxa.add(associatedTaxon);
821

    
822
        for (Taxon taxon : taxa) {
823
            List<T> perTaxonOccurrences = dao.listByAssociatedTaxon(type, taxon, null, null, orderHints, propertyPaths);
824
            for (SpecimenOrObservationBase<?> o : perTaxonOccurrences) {
825
                occurrenceIds.add(o.getId());
826
            }
827
        }
828
        occurrences = (List<T>) dao.loadList(occurrenceIds, propertyPaths);
829

    
830
        return new DefaultPagerImpl<T>(pageNumber, Long.valueOf(occurrences.size()), pageSize, occurrences);
831

    
832
    }
833

    
834
    @Override
835
    public <T extends SpecimenOrObservationBase> Pager<T> pageByAssociatedTaxon(Class<T> type, Set<TaxonRelationshipEdge> includeRelationships,
836
            String taxonUUID, Integer maxDepth, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
837

    
838
        UUID uuid = UUID.fromString(taxonUUID);
839
        Taxon tax = (Taxon) taxonService.load(uuid);
840
        // TODO REMOVE NULL STATEMENT
841
        type = null;
842
        return pageByAssociatedTaxon(type, includeRelationships, tax, maxDepth, pageSize, pageNumber, orderHints, propertyPaths);
843

    
844
    }
845

    
846
    @SuppressWarnings("unchecked")
847
    @Override
848
    public List<FieldUnitDTO> findFieldUnitDTOByAssociatedTaxon(Set<TaxonRelationshipEdge> includedRelationships,
849
            UUID associatedTaxonUuid) {
850

    
851
        Set<Taxon> taxa = new HashSet<>();
852
        Set<Integer> occurrenceIds = new HashSet<>();
853
        List<FieldUnitDTO> fieldUnitDTOs = new ArrayList<>();
854
        HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen = new HashMap<>();
855
        List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
856
        boolean includeUnpublished = INCLUDE_UNPUBLISHED;
857

    
858
        // Integer limit = PagerUtils.limitFor(pageSize);
859
        // Integer start = PagerUtils.startFor(pageSize, pageNumber);
860

    
861
        Taxon associatedTaxon = (Taxon) taxonService.load(associatedTaxonUuid);
862

    
863

    
864
        if (includedRelationships != null) {
865
            taxa = taxonService.listRelatedTaxa(associatedTaxon, includedRelationships, null, includeUnpublished, null, null, null);
866
        }
867

    
868
        taxa.add(associatedTaxon);
869

    
870
        for (Taxon taxon : taxa) {
871
            List<SpecimenOrObservationBase> perTaxonOccurrences = dao.listByAssociatedTaxon(null,taxon, null, null, null, DERIVED_UNIT_INIT_STRATEGY);
872
            for (SpecimenOrObservationBase<?> o : perTaxonOccurrences) {
873
                if (o.isInstanceOf(DerivedUnit.class)){
874
                    DerivedUnit derivedUnit;
875
                    DerivateDTO derivedUnitDTO;
876
                    if (o.isInstanceOf(DnaSample.class)) {
877
                         derivedUnit = HibernateProxyHelper.deproxy(o, DnaSample.class);
878
                        derivedUnitDTO = new DNASampleDTO(derivedUnit);
879
                    } else {
880
                        derivedUnit = HibernateProxyHelper.deproxy(o, DerivedUnit.class);
881
                        derivedUnitDTO = new PreservedSpecimenDTO(derivedUnit);
882
                    }
883
                    if (alreadyCollectedSpecimen.get(derivedUnitDTO.getUuid()) == null){
884
                        alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
885
                    }
886
                    derivedUnitDTO.addAllDerivates(getDerivedUnitDTOsFor(derivedUnitDTO, derivedUnit, alreadyCollectedSpecimen));
887
                    this.findFieldUnitDTO(derivedUnitDTO, fieldUnitDTOs, alreadyCollectedSpecimen);
888
                }
889
            }
890

    
891
        }
892

    
893
        return fieldUnitDTOs;
894

    
895
    }
896

    
897

    
898

    
899
    @Override
900
    public  List<DerivedUnit> findByAccessionNumber(
901
            String accessionNumberString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
902
            List<String> propertyPaths)  {
903

    
904
        List<DerivedUnit> records = new ArrayList<>();
905
        records = dao.findByGeneticAccessionNumber(accessionNumberString, propertyPaths);
906

    
907
        return records;
908

    
909
    }
910

    
911
    @Override
912
    public Pager<SearchResult<SpecimenOrObservationBase>> findByFullText(
913
            Class<? extends SpecimenOrObservationBase> clazz, String queryString, Rectangle boundingBox, List<Language> languages,
914
            boolean highlightFragments, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,
915
            List<String> propertyPaths) throws IOException, LuceneParseException {
916

    
917
        LuceneSearch luceneSearch = prepareByFullTextSearch(clazz, queryString, boundingBox, languages, highlightFragments);
918

    
919
        // --- execute search
920
        TopGroups<BytesRef> topDocsResultSet;
921
        try {
922
            topDocsResultSet = luceneSearch.executeSearch(pageSize, pageNumber);
923
        } catch (ParseException e) {
924
            LuceneParseException parseException = new LuceneParseException(e.getMessage());
925
            parseException.setStackTrace(e.getStackTrace());
926
            throw parseException;
927
        }
928

    
929
        Map<CdmBaseType, String> idFieldMap = new HashMap<>();
930
        idFieldMap.put(CdmBaseType.SPECIMEN_OR_OBSERVATIONBASE, "id");
931

    
932
        // --- initialize taxa, highlight matches ....
933
        ISearchResultBuilder searchResultBuilder = new SearchResultBuilder(luceneSearch, luceneSearch.getQuery());
934
        @SuppressWarnings("rawtypes")
935
        List<SearchResult<SpecimenOrObservationBase>> searchResults = searchResultBuilder.createResultSet(
936
                topDocsResultSet, luceneSearch.getHighlightFields(), dao, idFieldMap, propertyPaths);
937

    
938
        int totalHits = topDocsResultSet != null ? topDocsResultSet.totalGroupCount : 0;
939

    
940
        return new DefaultPagerImpl<>(pageNumber, Long.valueOf(totalHits), pageSize, searchResults);
941

    
942
    }
943

    
944
    private LuceneSearch prepareByFullTextSearch(Class<? extends SpecimenOrObservationBase> clazz, String queryString, Rectangle bbox,
945
            List<Language> languages, boolean highlightFragments) {
946

    
947
        Builder finalQueryBuilder = new Builder();
948
        Builder textQueryBuilder = new Builder();
949

    
950
        LuceneSearch luceneSearch = new LuceneSearch(luceneIndexToolProvider, FieldUnit.class);
951
        QueryFactory queryFactory = luceneIndexToolProvider.newQueryFactoryFor(FieldUnit.class);
952

    
953
        // --- criteria
954
        luceneSearch.setCdmTypRestriction(clazz);
955
        if (queryString != null) {
956
            textQueryBuilder.add(queryFactory.newTermQuery("titleCache", queryString), Occur.SHOULD);
957
            finalQueryBuilder.add(textQueryBuilder.build(), Occur.MUST);
958
        }
959

    
960
        // --- spacial query
961
        if (bbox != null) {
962
            finalQueryBuilder.add(QueryFactory.buildSpatialQueryByRange(bbox, "gatheringEvent.exactLocation.point"), Occur.MUST);
963
        }
964

    
965
        luceneSearch.setQuery(finalQueryBuilder.build());
966

    
967
        // --- sorting
968
        SortField[] sortFields = new SortField[] { SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.Type.STRING, false) };
969
        luceneSearch.setSortFields(sortFields);
970

    
971
        if (highlightFragments) {
972
            luceneSearch.setHighlightFields(queryFactory.getTextFieldNamesAsArray());
973
        }
974
        return luceneSearch;
975
    }
976

    
977

    
978
    @Override
979
    public Collection<FieldUnit> findFieldUnits(UUID derivedUnitUuid, List<String> propertyPaths) {
980
        //It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
981
        //from which this DerivedUnit was derived until all FieldUnits are found.
982

    
983
        // FIXME: use HQL queries to increase performance
984
        SpecimenOrObservationBase<?> specimen = load(derivedUnitUuid, propertyPaths);
985
//        specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
986
        Collection<FieldUnit> fieldUnits = new ArrayList<>();
987

    
988
        if (specimen.isInstanceOf(FieldUnit.class)) {
989
            fieldUnits.add(HibernateProxyHelper.deproxy(specimen, FieldUnit.class));
990
        }
991
        else if(specimen.isInstanceOf(DerivedUnit.class)){
992
            fieldUnits.addAll(getFieldUnits(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class), propertyPaths));
993
        }
994
        return fieldUnits;
995
    }
996

    
997
    private Collection<FieldUnit> getFieldUnits(DerivedUnit derivedUnit, List<String> propertyPaths) {
998
        Collection<FieldUnit> fieldUnits = new HashSet<>();
999
        Set<SpecimenOrObservationBase> originals = derivedUnit.getOriginals();
1000
        if (originals != null && !originals.isEmpty()) {
1001
            for (SpecimenOrObservationBase<?> original : originals) {
1002
                if (original.isInstanceOf(FieldUnit.class)) {
1003
                    fieldUnits.add((FieldUnit) load(original.getUuid(), propertyPaths));
1004
                }
1005
                else if(original.isInstanceOf(DerivedUnit.class)){
1006
                    fieldUnits.addAll(getFieldUnits(HibernateProxyHelper.deproxy(original, DerivedUnit.class), propertyPaths));
1007
                }
1008
            }
1009
        }
1010
        return fieldUnits;
1011
    }
1012

    
1013

    
1014

    
1015
    @Override
1016
    public FieldUnitDTO findFieldUnitDTO(DerivateDTO derivedUnitDTO, Collection<FieldUnitDTO> fieldUnits, HashMap<UUID, DerivateDTO> alreadyCollectedSpecimen) {
1017
        //It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
1018
        //from which this DerivedUnit was derived until all FieldUnits are found.
1019
        List<SpecimenOrObservationBase> specimens = new ArrayList<>();
1020
        List<String> propertyPaths = new ArrayList<>();
1021

    
1022
        propertyPaths.add("descriptions.elements.media.title");
1023
        propertyPaths.add("kindOfUnit");
1024

    
1025
        specimens = dao.findOriginalsForDerivedUnit(derivedUnitDTO.getUuid(), propertyPaths);
1026

    
1027
        if (specimens.size() > 1){
1028
            logger.debug("The derived unit with uuid " + derivedUnitDTO.getUuid() + "has more than one orginal");
1029
        }
1030
      //  for (SpecimenOrObservationBase specimen: specimens){
1031
        SpecimenOrObservationBase specimen = null;
1032
        if (specimens.size() > 0){
1033
            specimen = specimens.get(0);
1034
        }else{
1035
            return null;
1036
        }
1037
        FieldUnitDTO fieldUnitDto = null;
1038
        if (alreadyCollectedSpecimen.get(specimen.getUuid()) != null){
1039
            alreadyCollectedSpecimen.get(specimen.getUuid()).addDerivate(derivedUnitDTO);
1040
            if ( alreadyCollectedSpecimen.get(specimen.getUuid()) instanceof FieldUnitDTO){
1041
                ((FieldUnitDTO)alreadyCollectedSpecimen.get(specimen.getUuid())).getTaxonRelatedDerivedUnits().add(derivedUnitDTO.getUuid());
1042
            }
1043
        }else{
1044
            if (specimen.isInstanceOf(FieldUnit.class)){
1045
                fieldUnitDto = FieldUnitDTO.newInstance((FieldUnit)specimen);
1046
                fieldUnitDto.addDerivate(derivedUnitDTO);
1047
                fieldUnits.add(fieldUnitDto);
1048
            }else{
1049
                DerivateDTO originalDTO;
1050
                if (specimen instanceof DnaSample){
1051
                    originalDTO = new DNASampleDTO((DnaSample)specimen);
1052
                } else {
1053
                    originalDTO = new PreservedSpecimenDTO((DerivedUnit)specimen);
1054
                }
1055
                originalDTO.addDerivate(derivedUnitDTO);
1056
                fieldUnitDto = findFieldUnitDTO(originalDTO, fieldUnits, alreadyCollectedSpecimen);
1057
            }
1058

    
1059
        }
1060
      //  }
1061
        alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
1062
        if (fieldUnitDto != null){
1063
            fieldUnitDto.addTaxonRelatedDerivedUnits(derivedUnitDTO);
1064
        }
1065
        return fieldUnitDto;
1066

    
1067
    }
1068

    
1069
    @Override
1070
    @Transactional(readOnly = false)
1071
    public UpdateResult moveSequence(DnaSample from, DnaSample to, Sequence sequence) {
1072
        return moveSequence(from.getUuid(), to.getUuid(), sequence.getUuid());
1073
    }
1074

    
1075
    @Override
1076
    @Transactional(readOnly = false)
1077
    public UpdateResult moveSequence(UUID fromUuid, UUID toUuid, UUID sequenceUuid) {
1078
        // reload specimens to avoid session conflicts
1079
        DnaSample from = (DnaSample) load(fromUuid);
1080
        DnaSample to = (DnaSample) load(toUuid);
1081
        Sequence sequence = sequenceService.load(sequenceUuid);
1082

    
1083
        if (from == null || to == null || sequence == null) {
1084
            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" +
1085
                    "Operation was move "+sequence+ " from "+from+" to "+to);
1086
        }
1087
        UpdateResult result = new UpdateResult();
1088
        from.removeSequence(sequence);
1089
        saveOrUpdate(from);
1090
        to.addSequence(sequence);
1091
        saveOrUpdate(to);
1092
        result.setStatus(Status.OK);
1093
        result.addUpdatedObject(from);
1094
        result.addUpdatedObject(to);
1095
        return result;
1096
    }
1097

    
1098
    @Override
1099
    @Transactional(readOnly = false)
1100
    public boolean moveDerivate(SpecimenOrObservationBase<?> from, SpecimenOrObservationBase<?> to, DerivedUnit derivate) {
1101
        return moveDerivate(from!=null?from.getUuid():null, to.getUuid(), derivate.getUuid()).isOk();
1102
    }
1103

    
1104
    @Override
1105
    @Transactional(readOnly = false)
1106
    public UpdateResult moveDerivate(UUID specimenFromUuid, UUID specimenToUuid, UUID derivateUuid) {
1107
        // reload specimens to avoid session conflicts
1108
        SpecimenOrObservationBase<?> from = null;
1109
        if(specimenFromUuid!=null){
1110
            from = load(specimenFromUuid);
1111
        }
1112
        SpecimenOrObservationBase<?> to = load(specimenToUuid);
1113
        DerivedUnit derivate = (DerivedUnit) load(derivateUuid);
1114

    
1115
        if ((specimenFromUuid!=null && from == null) || to == null || derivate == null) {
1116
            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" +
1117
                    "Operation was move "+derivate+ " from "+from+" to "+to);
1118
        }
1119
        UpdateResult result = new UpdateResult();
1120
        SpecimenOrObservationType derivateType = derivate.getRecordBasis();
1121
        SpecimenOrObservationType toType = to.getRecordBasis();
1122
        // check if type is a sub derivate type
1123
        if(toType==SpecimenOrObservationType.FieldUnit //moving to FieldUnit always works
1124
                || derivateType==SpecimenOrObservationType.Media //moving media always works
1125
                || (derivateType.isKindOf(toType) && toType!=derivateType)){ //moving only to parent derivate type
1126
            if(from!=null){
1127
                // remove derivation event from parent specimen of dragged object
1128
                DerivationEvent eventToRemove = null;
1129
                for (DerivationEvent event : from.getDerivationEvents()) {
1130
                    if (event.getDerivatives().contains(derivate)) {
1131
                        eventToRemove = event;
1132
                        break;
1133
                    }
1134
                }
1135
                from.removeDerivationEvent(eventToRemove);
1136
                if(eventToRemove!=null){
1137
                    // add new derivation event to target and copy the event parameters of the old one
1138
                    DerivationEvent derivedFromNewOriginalEvent = DerivationEvent.NewSimpleInstance(to, derivate, null);
1139
                    derivedFromNewOriginalEvent.setActor(eventToRemove.getActor());
1140
                    derivedFromNewOriginalEvent.setDescription(eventToRemove.getDescription());
1141
                    derivedFromNewOriginalEvent.setInstitution(eventToRemove.getInstitution());
1142
                    derivedFromNewOriginalEvent.setTimeperiod(eventToRemove.getTimeperiod());
1143
                    derivedFromNewOriginalEvent.setType(eventToRemove.getType());
1144
                    to.addDerivationEvent(derivedFromNewOriginalEvent);
1145
                    derivate.setDerivedFrom(derivedFromNewOriginalEvent);
1146
                }
1147
            }
1148
            else{
1149
                //derivative had no parent before so we use empty derivation event
1150
                DerivationEvent derivedFromNewOriginalEvent = DerivationEvent.NewSimpleInstance(to, derivate, null);
1151
                to.addDerivationEvent(derivedFromNewOriginalEvent);
1152
                derivate.setDerivedFrom(derivedFromNewOriginalEvent);
1153
            }
1154

    
1155
            if(from!=null){
1156
                saveOrUpdate(from);
1157
            }
1158
            saveOrUpdate(to);
1159
            result.setStatus(Status.OK);
1160
            result.addUpdatedObject(from);
1161
            result.addUpdatedObject(to);
1162
        } else {
1163
            result.setStatus(Status.ERROR);
1164
        }
1165
        return result;
1166
    }
1167

    
1168
    @Override
1169
    public DeleteResult isDeletable(UUID specimenUuid, DeleteConfiguratorBase config) {
1170
        DeleteResult deleteResult = new DeleteResult();
1171
        SpecimenOrObservationBase specimen = this.load(specimenUuid);
1172
        SpecimenDeleteConfigurator specimenDeleteConfigurator = (SpecimenDeleteConfigurator) config;
1173

    
1174
        // check elements found by super method
1175
        Set<CdmBase> relatedObjects = super.isDeletable(specimenUuid, config).getRelatedObjects();
1176
        for (CdmBase cdmBase : relatedObjects) {
1177
            // check for type designation
1178
            if (cdmBase.isInstanceOf(SpecimenTypeDesignation.class) && !specimenDeleteConfigurator.isDeleteFromTypeDesignation()) {
1179
                deleteResult.setAbort();
1180
                deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is a type specimen."));
1181
                deleteResult.addRelatedObject(cdmBase);
1182
                break;
1183
            }
1184
            // check for IndividualsAssociations
1185
            else if (cdmBase.isInstanceOf(IndividualsAssociation.class) && !specimenDeleteConfigurator.isDeleteFromIndividualsAssociation()) {
1186
                deleteResult.setAbort();
1187
                deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is still associated via IndividualsAssociations"));
1188
                deleteResult.addRelatedObject(cdmBase);
1189
                break;
1190
            }
1191
            // check for taxon description
1192
            else if(cdmBase.isInstanceOf(TaxonDescription.class)
1193
                    && HibernateProxyHelper.deproxy(cdmBase, TaxonDescription.class).getDescribedSpecimenOrObservation().equals(specimen)
1194
                    && !specimenDeleteConfigurator.isDeleteFromDescription()){
1195
                deleteResult.setAbort();
1196
                deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation is still used as \"Described Specimen\" in a taxon description."));
1197
                deleteResult.addRelatedObject(cdmBase);
1198
                break;
1199
            }
1200
            // check for children and parents (derivation events)
1201
            else if (cdmBase.isInstanceOf(DerivationEvent.class)) {
1202
                DerivationEvent derivationEvent = HibernateProxyHelper.deproxy(cdmBase, DerivationEvent.class);
1203
                // check if derivation event is empty
1204
                if (!derivationEvent.getDerivatives().isEmpty() && derivationEvent.getOriginals().contains(specimen)) {
1205
                    // if derivationEvent is the childEvent and contains derivations
1206
//                    if (derivationEvent.getDerivatives().contains(specimen)) {
1207
//                        //if it is the parent event the specimen is still deletable
1208
//                        continue;
1209
//                    }
1210
                    if(!specimenDeleteConfigurator.isDeleteChildren()){
1211
                        //if children should not be deleted then it is undeletable
1212
                        deleteResult.setAbort();
1213
                        deleteResult.addException(new ReferencedObjectUndeletableException("Specimen or obeservation still has child derivatives."));
1214
                        deleteResult.addRelatedObject(cdmBase);
1215
                        break;
1216
                    }
1217
                    else{
1218
                        // check all children if they can be deleted
1219
                        Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1220
                        DeleteResult childResult = new DeleteResult();
1221
                        for (DerivedUnit derivedUnit : derivatives) {
1222
                            childResult.includeResult(isDeletable(derivedUnit.getUuid(), specimenDeleteConfigurator));
1223
                        }
1224
                        if (!childResult.isOk()) {
1225
                            deleteResult.setAbort();
1226
                            deleteResult.includeResult(childResult);
1227
                            deleteResult.addRelatedObject(cdmBase);
1228
                            break;
1229
                        }
1230
                    }
1231
                }
1232
            }
1233
            // check for amplification
1234
            else if (cdmBase.isInstanceOf(AmplificationResult.class)
1235
                    && !specimenDeleteConfigurator.isDeleteMolecularData()
1236
                    && !specimenDeleteConfigurator.isDeleteChildren()) {
1237
                deleteResult.setAbort();
1238
                deleteResult.addException(new ReferencedObjectUndeletableException("DnaSample is used in amplification results."));
1239
                deleteResult.addRelatedObject(cdmBase);
1240
                break;
1241
            }
1242
            // check for sequence
1243
            else if (cdmBase.isInstanceOf(Sequence.class)
1244
                    && !specimenDeleteConfigurator.isDeleteMolecularData()
1245
                    && !specimenDeleteConfigurator.isDeleteChildren()) {
1246
                deleteResult.setAbort();
1247
                deleteResult.addException(new ReferencedObjectUndeletableException("DnaSample is used in sequences."));
1248
                deleteResult.addRelatedObject(cdmBase);
1249
                break;
1250
            }
1251
        }
1252
        if (deleteResult.isOk()) {
1253
            //add all related object if deletion is OK so they can be handled by the delete() method
1254
            deleteResult.addRelatedObjects(relatedObjects);
1255
        }
1256
        return deleteResult;
1257
    }
1258

    
1259
    /**
1260
     * {@inheritDoc}
1261
     */
1262
    @Transactional(readOnly = false)
1263
    @Override
1264
    public DeleteResult delete(UUID specimenUuid, SpecimenDeleteConfigurator config) {
1265
        return delete(load(specimenUuid), config);
1266
    }
1267

    
1268

    
1269
    @Transactional(readOnly = false)
1270
    @Override
1271
    public DeleteResult delete(SpecimenOrObservationBase<?> specimen, SpecimenDeleteConfigurator config) {
1272
        specimen = HibernateProxyHelper.deproxy(specimen, SpecimenOrObservationBase.class);
1273

    
1274
        DeleteResult deleteResult = isDeletable(specimen.getUuid(), config);
1275
        if (!deleteResult.isOk()) {
1276
            return deleteResult;
1277
        }
1278

    
1279
        if (config.isDeleteChildren()) {
1280
            Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
1281
            //clone to avoid concurrent modification
1282
            //can happen if the child is deleted and deleted its own derivedFrom event
1283
            Set<DerivationEvent> derivationEventsClone = new HashSet<>(derivationEvents);
1284
            for (DerivationEvent derivationEvent : derivationEventsClone) {
1285
                Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1286
                Iterator<DerivedUnit> it = derivatives.iterator();
1287
                Set<DerivedUnit> derivativesToDelete = new HashSet<>();
1288
                while (it.hasNext()) {
1289
                    DerivedUnit unit = it.next();
1290
                    derivativesToDelete.add(unit);
1291
                }
1292
                for (DerivedUnit unit:derivativesToDelete){
1293
                    deleteResult.includeResult(delete(unit, config));
1294
                }
1295
            }
1296
        }
1297

    
1298

    
1299

    
1300

    
1301
        // check related objects
1302
        Set<CdmBase> relatedObjects = deleteResult.getRelatedObjects();
1303

    
1304
        for (CdmBase relatedObject : relatedObjects) {
1305
            // check for TypeDesignations
1306
            if (relatedObject.isInstanceOf(SpecimenTypeDesignation.class)) {
1307
                SpecimenTypeDesignation designation = HibernateProxyHelper.deproxy(relatedObject, SpecimenTypeDesignation.class);
1308
                designation.setTypeSpecimen(null);
1309
                List<TaxonName> typifiedNames = new ArrayList<>();
1310
                typifiedNames.addAll(designation.getTypifiedNames());
1311
                for (TaxonName taxonName : typifiedNames) {
1312
                    taxonName.removeTypeDesignation(designation);
1313
                }
1314
            }
1315
            // delete IndividualsAssociation
1316
            if (relatedObject.isInstanceOf(IndividualsAssociation.class)) {
1317
                IndividualsAssociation association = HibernateProxyHelper.deproxy(relatedObject, IndividualsAssociation.class);
1318
                association.setAssociatedSpecimenOrObservation(null);
1319
                association.getInDescription().removeElement(association);
1320
            }
1321
            // check for "described specimen" (deprecated)
1322
            if (relatedObject.isInstanceOf(TaxonDescription.class)) {
1323
                TaxonDescription description = HibernateProxyHelper.deproxy(relatedObject, TaxonDescription.class);
1324
                description.setDescribedSpecimenOrObservation(null);
1325
            }
1326
            // check for specimen description
1327
            if (relatedObject.isInstanceOf(SpecimenDescription.class)) {
1328
                SpecimenDescription specimenDescription = HibernateProxyHelper.deproxy(relatedObject, SpecimenDescription.class);
1329
                specimenDescription.setDescribedSpecimenOrObservation(null);
1330
                // check if description is a description of the given specimen
1331
                if (specimen.getDescriptions().contains(specimenDescription)) {
1332
                    specimen.removeDescription(specimenDescription);
1333
                }
1334
                DeleteResult descriptionDelete = descriptionService.isDeletable(specimenDescription.getUuid(), null);
1335
                if (descriptionDelete.isOk()){
1336
                    deleteResult.includeResult(descriptionService.delete(specimenDescription));
1337
                }
1338
            }
1339
            // check for amplification
1340
            if (relatedObject.isInstanceOf(AmplificationResult.class)) {
1341
                AmplificationResult amplificationResult = HibernateProxyHelper.deproxy(relatedObject, AmplificationResult.class);
1342
                amplificationResult.getDnaSample().removeAmplificationResult(amplificationResult);
1343
            }
1344
            // check for sequence
1345
            if (relatedObject.isInstanceOf(Sequence.class)) {
1346
                Sequence sequence = HibernateProxyHelper.deproxy(relatedObject, Sequence.class);
1347
                sequence.getDnaSample().removeSequence(sequence);
1348
            }
1349
            // check for children and parents (derivation events)
1350
            if (relatedObject.isInstanceOf(DerivationEvent.class)) {
1351
                DerivationEvent derivationEvent = HibernateProxyHelper.deproxy(relatedObject, DerivationEvent.class);
1352
                // parent derivation event (derivedFrom)
1353
                if (derivationEvent.getDerivatives().contains(specimen) && specimen.isInstanceOf(DerivedUnit.class)) {
1354
                    derivationEvent.removeDerivative(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class));
1355
                    if (derivationEvent.getDerivatives().isEmpty()) {
1356
                        Set<SpecimenOrObservationBase> originals = derivationEvent.getOriginals();
1357
                        for (SpecimenOrObservationBase specimenOrObservationBase : originals) {
1358
                            specimenOrObservationBase.removeDerivationEvent(derivationEvent);
1359
                            deleteResult.addUpdatedObject(specimenOrObservationBase);
1360
                        }
1361
                        // if derivationEvent has no derivates anymore, delete it
1362
                        deleteResult.includeResult(eventService.delete(derivationEvent));
1363
                    }
1364
                }
1365
                else{
1366
                    //child derivation events should not occur since we delete the hierarchy from bottom to top
1367
                }
1368
            }
1369
        }
1370
        if (specimen instanceof FieldUnit){
1371
            FieldUnit fieldUnit = HibernateProxyHelper.deproxy(specimen, FieldUnit.class);
1372
            GatheringEvent event = fieldUnit.getGatheringEvent();
1373
            fieldUnit.setGatheringEvent(null);
1374
            if (event != null){
1375
                DeleteResult result = eventService.isDeletable(event.getUuid(), null);
1376
                if (result.isOk()){
1377
                    deleteResult.includeResult( eventService.delete(event));
1378
                }
1379
            }
1380

    
1381
        }
1382
        deleteResult.includeResult(delete(specimen));
1383

    
1384
        return deleteResult;
1385
    }
1386

    
1387
    @Override
1388
    public Collection<IndividualsAssociation> listIndividualsAssociations(SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1389
        return dao.listIndividualsAssociations(specimen, limit, start, orderHints, propertyPaths);
1390
    }
1391

    
1392
    /**
1393
     * {@inheritDoc}
1394
     */
1395
    @Override
1396
    public Collection<TaxonBase<?>> listAssociatedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
1397
            Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1398
        return listAssociatedTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
1399
    }
1400
    @Override
1401
    public Collection<TaxonBase<?>> listAssociatedTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished,
1402
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1403
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1404

    
1405
        //individuals associations
1406
        associatedTaxa.addAll(listIndividualsAssociationTaxa(specimen, includeUnpublished, limit, start, orderHints, propertyPaths));
1407
        //type designation
1408
        if(specimen.isInstanceOf(DerivedUnit.class)){
1409
            associatedTaxa.addAll(listTypeDesignationTaxa(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class),
1410
                  includeUnpublished, limit, start, orderHints, propertyPaths));
1411
        }
1412
        //determinations
1413
        associatedTaxa.addAll(listDeterminedTaxa(specimen, includeUnpublished, limit, start, orderHints, propertyPaths));
1414

    
1415
        return associatedTaxa;
1416
    }
1417

    
1418

    
1419

    
1420
    /**
1421
     * {@inheritDoc}
1422
     */
1423
    @Override
1424
    public Collection<TaxonBase<?>> listDeterminedTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
1425
            Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1426
        return listDeterminedTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
1427
    }
1428
    @Override
1429
    public Collection<TaxonBase<?>> listDeterminedTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished, Integer limit, Integer start,
1430
            List<OrderHint> orderHints, List<String> propertyPaths) {
1431
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1432
        for (DeterminationEvent determinationEvent : listDeterminationEvents(specimen, limit, start, orderHints, propertyPaths)) {
1433
            if(determinationEvent.getIdentifiedUnit().equals(specimen)){
1434
                if(determinationEvent.getTaxon()!=null){
1435
                    associatedTaxa.add(taxonService.load(determinationEvent.getTaxon().getUuid(), includeUnpublished, propertyPaths));
1436
                }
1437
                if(determinationEvent.getTaxonName()!=null){
1438
                    Collection<TaxonBase> taxonBases = determinationEvent.getTaxonName().getTaxonBases();
1439
                    for (TaxonBase taxonBase : taxonBases) {
1440
                        associatedTaxa.add(taxonService.load(taxonBase.getUuid(), includeUnpublished, propertyPaths));
1441
                    }
1442
                }
1443
            }
1444
        }
1445
        return associatedTaxa;
1446
    }
1447

    
1448
    /**
1449
     * {@inheritDoc}
1450
     */
1451
    @Override
1452
    public Collection<TaxonBase<?>> listTypeDesignationTaxa(DerivedUnit specimen, Integer limit, Integer start,
1453
            List<OrderHint> orderHints, List<String> propertyPaths) {
1454
        return listTypeDesignationTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
1455
    }
1456
    @Override
1457
    public Collection<TaxonBase<?>> listTypeDesignationTaxa(DerivedUnit specimen, boolean includeUnpublished,
1458
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1459
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1460
        for (SpecimenTypeDesignation typeDesignation : listTypeDesignations(specimen, limit, start, orderHints, propertyPaths)) {
1461
            if(typeDesignation.getTypeSpecimen().equals(specimen)){
1462
                Set<TaxonName> typifiedNames = typeDesignation.getTypifiedNames();
1463
                for (TaxonName taxonName : typifiedNames) {
1464
                    Set<Taxon> taxa = taxonName.getTaxa();
1465
                    for (Taxon taxon : taxa) {
1466
                        associatedTaxa.add(taxonService.load(taxon.getUuid(), includeUnpublished, propertyPaths));
1467
                    }
1468
                }
1469
            }
1470
        }
1471
        return associatedTaxa;
1472
    }
1473

    
1474
    /**
1475
     * {@inheritDoc}
1476
     */
1477
    @Override
1478
    public Collection<TaxonBase<?>> listIndividualsAssociationTaxa(SpecimenOrObservationBase<?> specimen, Integer limit,
1479
            Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1480
        return listIndividualsAssociationTaxa(specimen, INCLUDE_UNPUBLISHED, limit, start, orderHints, propertyPaths);
1481
    }
1482

    
1483
    @Override
1484
    public Collection<TaxonBase<?>> listIndividualsAssociationTaxa(SpecimenOrObservationBase<?> specimen, boolean includeUnpublished,
1485
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1486
        Collection<TaxonBase<?>> associatedTaxa = new HashSet<>();
1487
        for (IndividualsAssociation individualsAssociation : listIndividualsAssociations(specimen, null, null, null, null)) {
1488
            if(individualsAssociation.getInDescription().isInstanceOf(TaxonDescription.class)){
1489
                TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(individualsAssociation.getInDescription(), TaxonDescription.class);
1490
                if(taxonDescription.getTaxon()!=null){
1491
                    associatedTaxa.add(taxonService.load(taxonDescription.getTaxon().getUuid(), includeUnpublished, propertyPaths));
1492
                }
1493
            }
1494
        }
1495
        return associatedTaxa;
1496
    }
1497

    
1498
    @Override
1499
    public Collection<DeterminationEvent> listDeterminationEvents(SpecimenOrObservationBase<?> specimen,
1500
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1501
        return dao.listDeterminationEvents(specimen, limit, start, orderHints, propertyPaths);
1502
    }
1503

    
1504
    @Override
1505
    public Map<DerivedUnit, Collection<SpecimenTypeDesignation>> listTypeDesignations(
1506
            Collection<DerivedUnit> specimens, Integer limit, Integer start,
1507
            List<OrderHint> orderHints, List<String> propertyPaths) {
1508
        Map<DerivedUnit, Collection<SpecimenTypeDesignation>> typeDesignationMap = new HashMap<>();
1509
        for (DerivedUnit specimen : specimens) {
1510
            Collection<SpecimenTypeDesignation> typeDesignations = listTypeDesignations(specimen, limit, start, orderHints, propertyPaths);
1511
            typeDesignationMap.put(specimen, typeDesignations);
1512
        }
1513
        return typeDesignationMap;
1514
    }
1515

    
1516
    @Override
1517
    public Collection<SpecimenTypeDesignation> listTypeDesignations(DerivedUnit specimen,
1518
            Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
1519
        return dao.listTypeDesignations(specimen, limit, start, orderHints, propertyPaths);
1520
    }
1521

    
1522
    @Override
1523
    public Collection<DescriptionBase<?>> listDescriptionsWithDescriptionSpecimen(
1524
            SpecimenOrObservationBase<?> specimen, Integer limit, Integer start, List<OrderHint> orderHints,
1525
            List<String> propertyPaths) {
1526
        return dao.listDescriptionsWithDescriptionSpecimen(specimen, limit, start, orderHints, propertyPaths);
1527
    }
1528

    
1529
    @Override
1530
    @Deprecated //this is not a service layer task so it may be removed in future versions
1531
    public Collection<DescriptionElementBase> getCharacterDataForSpecimen(SpecimenOrObservationBase<?> specimen) {
1532
        if (specimen != null) {
1533
            return specimen.characterData();
1534
        }else{
1535
            return new ArrayList<>();
1536
        }
1537
    }
1538

    
1539
    @Override
1540
    public Collection<DescriptionElementBase> getCharacterDataForSpecimen(UUID specimenUuid) {
1541
        SpecimenOrObservationBase<?> specimen = load(specimenUuid);
1542
        if (specimen != null) {
1543
            return getCharacterDataForSpecimen(specimen);
1544
        }
1545
        else{
1546
            throw new DataRetrievalFailureException("Specimen with the given uuid not found in the data base");
1547
        }
1548
    }
1549

    
1550

    
1551
    @Override
1552
    public long countByTitle(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config){
1553
        if (config instanceof FindOccurrencesConfigurator) {
1554
            FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
1555
            Taxon taxon = null;
1556
            if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
1557
                TaxonBase<?> taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
1558
                if(taxonBase.isInstanceOf(Taxon.class)){
1559
                    taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
1560
                }
1561
            }
1562
            TaxonName taxonName = null;
1563
            if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
1564
                taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
1565
            }
1566
            /*TODO: #6484 Neither isRetrieveIndirectlyAssociatedSpecimens() nor the AssignmentStatus
1567
             * is currently reflected in the HQL query. So using these in the count method will
1568
             * significantly slow down this method as we have to retreive the entities instead of
1569
             * the just the amount.
1570
             */
1571
            if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens() || !occurrenceConfig.getAssignmentStatus().equals(AssignmentStatus.ALL_SPECIMENS)){
1572
                List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
1573
                occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
1574
                        occurrenceConfig.getTitleSearchStringSqlized(), occurrenceConfig.getSignificantIdentifier(),
1575
                        occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1576
                        occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
1577
                occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
1578
                return occurrences.size();
1579
            }
1580

    
1581
            return dao.countOccurrences(occurrenceConfig.getClazz(),
1582
                    occurrenceConfig.getTitleSearchStringSqlized(), occurrenceConfig.getSignificantIdentifier(),
1583
                    occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1584
                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
1585
        }
1586
        else{
1587
            return super.countByTitle(config);
1588
        }
1589
    }
1590

    
1591
    @Override
1592
    public Pager<UuidAndTitleCache<SpecimenOrObservationBase>> findByTitleUuidAndTitleCache(
1593
            FindOccurrencesConfigurator config){
1594
        List<UuidAndTitleCache<SpecimenOrObservationBase>> occurrences = new ArrayList<>();
1595
        Taxon taxon = null;
1596
        if(config.getAssociatedTaxonUuid()!=null){
1597
            TaxonBase<?> taxonBase = taxonService.load(config.getAssociatedTaxonUuid());
1598
            if(taxonBase.isInstanceOf(Taxon.class)){
1599
                taxon = CdmBase.deproxy(taxonBase, Taxon.class);
1600
            }
1601
        }
1602
        TaxonName taxonName = null;
1603
        if(config.getAssociatedTaxonNameUuid()!=null){
1604
            taxonName = nameService.load(config.getAssociatedTaxonNameUuid());
1605
        }
1606
        occurrences.addAll(dao.findOccurrencesUuidAndTitleCache(config.getClazz(),
1607
                config.getTitleSearchString(), config.getSignificantIdentifier(),
1608
                config.getSpecimenType(), taxon, taxonName, config.getMatchMode(), null, null,
1609
                config.getOrderHints()));
1610

    
1611
        return new DefaultPagerImpl<>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
1612
    }
1613

    
1614
    @Override
1615
    public List<PreservedSpecimenDTO> findByTitlePreservedSpecimenDTO(FindOccurrencesConfigurator config) {
1616
        Taxon taxon = null;
1617
        if(config.getAssociatedTaxonUuid()!=null){
1618
            TaxonBase<?> taxonBase = taxonService.load(config.getAssociatedTaxonUuid());
1619
            if(taxonBase.isInstanceOf(Taxon.class)){
1620
                taxon = CdmBase.deproxy(taxonBase, Taxon.class);
1621
            }
1622
        }
1623
        TaxonName taxonName = null;
1624
        if(config.getAssociatedTaxonNameUuid()!=null){
1625
            taxonName = nameService.load(config.getAssociatedTaxonNameUuid());
1626
        }
1627
        List<DerivedUnit> occurrences = new ArrayList<>();
1628
        occurrences.addAll(dao.findOccurrences(DerivedUnit.class,
1629
                config.getTitleSearchString(), config.getSignificantIdentifier(),
1630
                config.getSpecimenType(), taxon, taxonName, config.getMatchMode(), null, null,
1631
                config.getOrderHints(), null));
1632

    
1633
        List<PreservedSpecimenDTO> dtos = new ArrayList<>();
1634
        occurrences.forEach(derivedUnit->dtos.add(assemblePreservedSpecimenDTO(derivedUnit)));
1635
        return dtos;
1636
    }
1637

    
1638
    @Override
1639
    public <S extends SpecimenOrObservationBase> Pager<S> findByTitle(
1640
            IIdentifiableEntityServiceConfigurator<S> config) {
1641
        if (config instanceof FindOccurrencesConfigurator) {
1642
            FindOccurrencesConfigurator occurrenceConfig = (FindOccurrencesConfigurator) config;
1643
            List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
1644
            Taxon taxon = null;
1645
            if(occurrenceConfig.getAssociatedTaxonUuid()!=null){
1646
                TaxonBase<?> taxonBase = taxonService.load(occurrenceConfig.getAssociatedTaxonUuid());
1647
                if(taxonBase.isInstanceOf(Taxon.class)){
1648
                    taxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
1649
                }
1650
            }
1651
            TaxonName taxonName = null;
1652
            if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
1653
                taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
1654
            }
1655
            List<? extends SpecimenOrObservationBase> foundOccurrences = dao.findOccurrences(occurrenceConfig.getClazz(),
1656
                    occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1657
                    occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1658
                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths());
1659
            occurrences.addAll(foundOccurrences);
1660
            occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
1661

    
1662
            return new DefaultPagerImpl<>(config.getPageNumber(), occurrences.size(), config.getPageSize(), (List<S>)occurrences);
1663
        }
1664
        return super.findByTitle(config);
1665
    }
1666

    
1667
    private List<SpecimenOrObservationBase> filterOccurencesByAssignmentAndHierarchy(
1668
            FindOccurrencesConfigurator occurrenceConfig, List<SpecimenOrObservationBase> occurrences, Taxon taxon,
1669
            TaxonName taxonName) {
1670
        //filter out (un-)assigned specimens
1671
        if(taxon==null && taxonName==null){
1672
            AssignmentStatus assignmentStatus = occurrenceConfig.getAssignmentStatus();
1673
            List<SpecimenOrObservationBase> specimenWithAssociations = new ArrayList<>();
1674
            if(!assignmentStatus.equals(AssignmentStatus.ALL_SPECIMENS)){
1675
                for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
1676
                    boolean includeUnpublished = true;  //TODO not sure if this is correct, maybe we have to propagate publish flag to higher methods.
1677
                    Collection<TaxonBase<?>> associatedTaxa = listAssociatedTaxa(specimenOrObservationBase,
1678
                            includeUnpublished, null, null, null, null);
1679
                    if(!associatedTaxa.isEmpty()){
1680
                        specimenWithAssociations.add(specimenOrObservationBase);
1681
                    }
1682
                }
1683
            }
1684
            if(assignmentStatus.equals(AssignmentStatus.UNASSIGNED_SPECIMENS)){
1685
                occurrences.removeAll(specimenWithAssociations);
1686
            }
1687
            if(assignmentStatus.equals(AssignmentStatus.ASSIGNED_SPECIMENS)){
1688
                occurrences = new ArrayList<>(specimenWithAssociations);
1689
            }
1690
        }
1691
        // indirectly associated specimens
1692
        if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens()){
1693
            List<SpecimenOrObservationBase> indirectlyAssociatedOccurrences = new ArrayList<>(occurrences);
1694
            for (SpecimenOrObservationBase<?> specimen : occurrences) {
1695
                List<SpecimenOrObservationBase<?>> allHierarchyDerivates = getAllHierarchyDerivatives(specimen);
1696
                for (SpecimenOrObservationBase<?> specimenOrObservationBase : allHierarchyDerivates) {
1697
                    if(!occurrences.contains(specimenOrObservationBase)){
1698
                        indirectlyAssociatedOccurrences.add(specimenOrObservationBase);
1699
                    }
1700
                }
1701
            }
1702
            occurrences = indirectlyAssociatedOccurrences;
1703
        }
1704
        return occurrences;
1705
    }
1706

    
1707
    @Override
1708
    public List<SpecimenOrObservationBase<?>> getAllHierarchyDerivatives(SpecimenOrObservationBase<?> specimen){
1709
        List<SpecimenOrObservationBase<?>> allHierarchyDerivatives = new ArrayList<>();
1710
        Collection<FieldUnit> fieldUnits = findFieldUnits(specimen.getUuid(), null);
1711
        if(fieldUnits.isEmpty()){
1712
            allHierarchyDerivatives.add(specimen);
1713
            allHierarchyDerivatives.addAll(getAllChildDerivatives(specimen));
1714
        }
1715
        else{
1716
            for (FieldUnit fieldUnit : fieldUnits) {
1717
                allHierarchyDerivatives.add(fieldUnit);
1718
                allHierarchyDerivatives.addAll(getAllChildDerivatives(fieldUnit));
1719
            }
1720
        }
1721
        return allHierarchyDerivatives;
1722
    }
1723

    
1724
    @Override
1725
    public List<DerivedUnit> getAllChildDerivatives(UUID specimenUuid){
1726
        return getAllChildDerivatives(load(specimenUuid));
1727
    }
1728

    
1729
    @Override
1730
    public List<DerivedUnit> getAllChildDerivatives(SpecimenOrObservationBase<?> specimen){
1731
        if (specimen == null){
1732
            return null;
1733
        }
1734
        List<DerivedUnit> childDerivate = new ArrayList<>();
1735
        Set<DerivationEvent> derivationEvents = specimen.getDerivationEvents();
1736
        for (DerivationEvent derivationEvent : derivationEvents) {
1737
            Set<DerivedUnit> derivatives = derivationEvent.getDerivatives();
1738
            for (DerivedUnit derivedUnit : derivatives) {
1739
                childDerivate.add(derivedUnit);
1740
                childDerivate.addAll(getAllChildDerivatives(derivedUnit.getUuid()));
1741
            }
1742
        }
1743
        return childDerivate;
1744
    }
1745

    
1746
    @Override
1747
    public long countOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> config){
1748
        return countByTitle(config);
1749
    }
1750

    
1751
    /**
1752
     * {@inheritDoc}
1753
     */
1754
    @Override
1755
    public List<FieldUnit> findFieldUnitsForGatheringEvent(UUID gatheringEventUuid) {
1756
        return dao.findFieldUnitsForGatheringEvent(gatheringEventUuid, null, null, null, null);
1757
    }
1758

    
1759

    
1760
    /**
1761
     * {@inheritDoc}
1762
     */
1763
    @Override
1764
    public List<Point> findPointsForFieldUnitList(List<UUID> fieldUnitUuids) {
1765

    
1766
        return dao.findPointsForFieldUnitList(fieldUnitUuids);
1767
    }
1768
}
(83-83/103)