Project

General

Profile

Revision 32b89663

ID32b8966302f46f7bdd0e0b3f9a4d1724bb57a9ce
Parent 599c51cd
Child c4b3991f

Added by Patrick Plitzner over 1 year ago

ref #7674 Move aggregation service method to data set service

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/longrunningService/LongRunningTasksServiceImpl.java
16 16
import org.springframework.stereotype.Service;
17 17
import org.springframework.transaction.annotation.Transactional;
18 18

  
19
import eu.etaxonomy.cdm.api.service.IDescriptionService;
19
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
20 20
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
21 21
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
22 22
import eu.etaxonomy.cdm.api.service.UpdateResult;
......
38 38
    ITaxonNodeService taxonNodeService;
39 39

  
40 40
    @Autowired
41
    IDescriptionService descriptionService;
41
    IDescriptiveDataSetService descriptiveDataSetService;
42 42

  
43 43
    @Autowired
44 44
    IProgressMonitorService progressMonitorService;
......
49 49
        RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
50 50
            @Override
51 51
            public Serializable doRun(IRemotingProgressMonitor monitor) {
52
                UpdateResult updateResult = descriptionService.aggregateTaxonDescription(taxonNodeUuid, descriptiveDataSetUuid, monitor);
52
                UpdateResult updateResult = descriptiveDataSetService.aggregateTaxonDescription(taxonNodeUuid, descriptiveDataSetUuid, monitor);
53 53
                for(Exception e : updateResult.getExceptions()) {
54 54
                    monitor.addReport(e.getMessage());
55 55
                }
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/DescriptionServiceImpl.java
12 12
import java.util.ArrayList;
13 13
import java.util.Arrays;
14 14
import java.util.Collection;
15
import java.util.HashMap;
16 15
import java.util.HashSet;
17 16
import java.util.Iterator;
18 17
import java.util.List;
......
31 30
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
32 31
import eu.etaxonomy.cdm.api.utility.DescriptionUtility;
33 32
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
34
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
35 33
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
36 34
import eu.etaxonomy.cdm.model.common.Annotation;
37 35
import eu.etaxonomy.cdm.model.common.AnnotationType;
38 36
import eu.etaxonomy.cdm.model.common.CdmBase;
39 37
import eu.etaxonomy.cdm.model.common.DefinedTerm;
40
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
41 38
import eu.etaxonomy.cdm.model.common.Language;
42 39
import eu.etaxonomy.cdm.model.common.Marker;
43 40
import eu.etaxonomy.cdm.model.common.MarkerType;
44
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
45 41
import eu.etaxonomy.cdm.model.common.TermVocabulary;
46
import eu.etaxonomy.cdm.model.description.CategoricalData;
47
import eu.etaxonomy.cdm.model.description.Character;
48 42
import eu.etaxonomy.cdm.model.description.DescriptionBase;
49 43
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
50 44
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
......
52 46
import eu.etaxonomy.cdm.model.description.Feature;
53 47
import eu.etaxonomy.cdm.model.description.FeatureTree;
54 48
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
55
import eu.etaxonomy.cdm.model.description.QuantitativeData;
56 49
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
57
import eu.etaxonomy.cdm.model.description.StateData;
58
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
59 50
import eu.etaxonomy.cdm.model.description.TaxonDescription;
60 51
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
61 52
import eu.etaxonomy.cdm.model.description.TextData;
......
65 56
import eu.etaxonomy.cdm.model.name.TaxonName;
66 57
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
67 58
import eu.etaxonomy.cdm.model.taxon.Taxon;
68
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
69
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
70 59
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
71 60
import eu.etaxonomy.cdm.persistence.dao.common.ITermVocabularyDao;
72 61
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
......
857 846

  
858 847
    }
859 848

  
860
    @Override
861
    @Transactional(readOnly=false)
862
    public UpdateResult aggregateTaxonDescription(UUID taxonNodeUuid, UUID descriptiveDataSetUuid,
863
            IRemotingProgressMonitor monitor){
864
        UpdateResult result = new UpdateResult();
865

  
866
        TaxonNode node = taxonNodeDao.load(taxonNodeUuid);
867
        Taxon taxon = HibernateProxyHelper.deproxy(taxonDao.load(node.getTaxon().getUuid()), Taxon.class);
868
        result.setCdmEntity(taxon);
869

  
870
        //get all "computed" descriptions from all sub nodes
871
        List<TaxonNode> childNodes = taxonNodeDao.listChildrenOf(node, null, null, true, false, null);
872
        List<TaxonDescription> computedDescriptions = new ArrayList<>();
873

  
874
        childNodes.stream().map(childNode -> childNode.getTaxon())
875
                .forEach(childTaxon -> childTaxon.getDescriptions().stream()
876
                        // filter out non-computed descriptions
877
                        .filter(description -> description.getMarkers().stream()
878
                                .anyMatch(marker -> marker.getMarkerType().equals(MarkerType.COMPUTED())))
879
                        // add them to the list
880
                        .forEach(computedDescription -> computedDescriptions.add(computedDescription)));
881

  
882
        UpdateResult aggregateDescription = aggregateDescription(taxon, computedDescriptions,
883
                "[Taxon Descriptions]"+taxon.getTitleCache(), descriptiveDataSetUuid);
884
        result.includeResult(aggregateDescription);
885
        result.setCdmEntity(aggregateDescription.getCdmEntity());
886
        aggregateDescription.setCdmEntity(null);
887
        return result;
888
    }
889

  
890
    @Override
891
    @Transactional(readOnly=false)
892
    public UpdateResult aggregateDescription(UUID taxonUuid, List<UUID> descriptionUuids, String descriptionTitle
893
            , UUID descriptiveDataSetUuid) {
894
        UpdateResult result = new UpdateResult();
895

  
896
        TaxonBase taxonBase = taxonDao.load(taxonUuid);
897
        if(!(taxonBase instanceof Taxon)){
898
            result.addException(new ClassCastException("The given taxonUUID does not belong to a taxon"));
899
            result.setError();
900
            return result;
901
        }
902
        Taxon taxon = (Taxon)taxonBase;
903

  
904
        List<DescriptionBase> descriptions = load(descriptionUuids, null);
905

  
906
        UpdateResult aggregateDescriptionResult = aggregateDescription(taxon, descriptions, descriptionTitle, descriptiveDataSetUuid);
907
        result.setCdmEntity(aggregateDescriptionResult.getCdmEntity());
908
        aggregateDescriptionResult.setCdmEntity(null);
909
        result.includeResult(aggregateDescriptionResult);
910
        return result;
911
    }
912

  
913
    @SuppressWarnings("unchecked")
914
    private UpdateResult aggregateDescription(Taxon taxon, List<? extends DescriptionBase> descriptions, String descriptionTitle
915
            , UUID descriptiveDataSetUuid) {
916
        UpdateResult result = new UpdateResult();
917
        Map<Character, List<DescriptionElementBase>> featureToElementMap = new HashMap<>();
918

  
919
        DescriptiveDataSet dataSet = descriptiveDataSetDao.load(descriptiveDataSetUuid);
920
        if(dataSet==null){
921
            result.addException(new IllegalArgumentException("Could not find data set for uuid "+descriptiveDataSetUuid));
922
            result.setAbort();
923
            return result;
924
        }
925

  
926
        //extract all character description elements
927
        descriptions.forEach(description->{
928
            description.getElements()
929
            .stream()
930
            //filter out elements that do not have a Characters as Feature
931
            .filter(element->HibernateProxyHelper.isInstanceOf(((DescriptionElementBase)element).getFeature(), Character.class))
932
            .forEach(ele->{
933
                DescriptionElementBase descriptionElement = (DescriptionElementBase)ele;
934
                List<DescriptionElementBase> list = featureToElementMap.get(descriptionElement.getFeature());
935
                if(list==null){
936
                    list = new ArrayList<>();
937
                }
938
                list.add(descriptionElement);
939
                featureToElementMap.put(HibernateProxyHelper.deproxy(descriptionElement.getFeature(), Character.class), list);
940
            });
941
        });
942

  
943
        TaxonDescription description = TaxonDescription.NewInstance(taxon);
944
        description.setTitleCache("[Aggregation] "+descriptionTitle, true);
945
        description.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
946
        IdentifiableSource source = IdentifiableSource.NewInstance(OriginalSourceType.Aggregation);
947
        description.addSource(source);
948
        description.addDescriptiveDataSet(dataSet);
949

  
950
        featureToElementMap.forEach((feature, elements)->{
951
            //aggregate categorical data
952
            if(feature.isSupportsCategoricalData()){
953
                CategoricalData aggregate = CategoricalData.NewInstance(feature);
954
                elements.stream()
955
                .filter(element->element instanceof CategoricalData)
956
                .forEach(categoricalData->((CategoricalData)categoricalData).getStateData()
957
                        .forEach(stateData->aggregate.addStateData((StateData) stateData.clone())));
958
                description.addElement(aggregate);
959
            }
960
            //aggregate quantitative data
961
            else if(feature.isSupportsQuantitativeData()){
962
                QuantitativeData aggregate = QuantitativeData.NewInstance(feature);
963
                elements.stream()
964
                .filter(element->element instanceof QuantitativeData)
965
                .forEach(categoricalData->((QuantitativeData)categoricalData).getStatisticalValues()
966
                        .forEach(statisticalValue->aggregate.addStatisticalValue((StatisticalMeasurementValue) statisticalValue.clone())));
967
                description.addElement(aggregate);
968
            }
969
        });
970
        result.addUpdatedObject(taxon);
971
        result.setCdmEntity(description);
972
        return result;
973
    }
974

  
975 849
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/DescriptiveDataSetService.java
26 26
import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitorThread;
27 27
import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
28 28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
29
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
29 30
import eu.etaxonomy.cdm.model.common.Language;
30 31
import eu.etaxonomy.cdm.model.common.Marker;
31 32
import eu.etaxonomy.cdm.model.common.MarkerType;
33
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
32 34
import eu.etaxonomy.cdm.model.description.CategoricalData;
35
import eu.etaxonomy.cdm.model.description.Character;
33 36
import eu.etaxonomy.cdm.model.description.DescriptionBase;
34 37
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
35 38
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
......
37 40
import eu.etaxonomy.cdm.model.description.Feature;
38 41
import eu.etaxonomy.cdm.model.description.QuantitativeData;
39 42
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
43
import eu.etaxonomy.cdm.model.description.StateData;
40 44
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
45
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
41 46
import eu.etaxonomy.cdm.model.description.TaxonDescription;
42 47
import eu.etaxonomy.cdm.model.description.TextData;
43 48
import eu.etaxonomy.cdm.model.location.NamedArea;
......
46 51
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
47 52
import eu.etaxonomy.cdm.model.taxon.Classification;
48 53
import eu.etaxonomy.cdm.model.taxon.Taxon;
54
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
49 55
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
50 56
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptiveDataSetDao;
57
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
51 58
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
52 59
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
53 60
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
......
73 80
    private ITaxonNodeService taxonNodeService;
74 81

  
75 82
    @Autowired
83
    private ITaxonNodeDao taxonNodeDao;
84

  
85
    @Autowired
76 86
    private IProgressMonitorService progressMonitorService;
77 87

  
78 88
	@Override
......
285 295
    }
286 296

  
287 297
    @Override
298
    @Transactional(readOnly=false)
299
    public UpdateResult aggregateTaxonDescription(UUID taxonNodeUuid, UUID descriptiveDataSetUuid,
300
            IRemotingProgressMonitor monitor){
301
        UpdateResult result = new UpdateResult();
302

  
303
        TaxonNode node = taxonNodeService.load(taxonNodeUuid);
304
        Taxon taxon = HibernateProxyHelper.deproxy(taxonService.load(node.getTaxon().getUuid()), Taxon.class);
305
        result.setCdmEntity(taxon);
306

  
307
        //get all "computed" descriptions from all sub nodes
308
        List<TaxonNode> childNodes = taxonNodeDao.listChildrenOf(node, null, null, true, false, null);
309
        List<TaxonDescription> computedDescriptions = new ArrayList<>();
310

  
311
        childNodes.stream().map(childNode -> childNode.getTaxon())
312
                .forEach(childTaxon -> childTaxon.getDescriptions().stream()
313
                        // filter out non-computed descriptions
314
                        .filter(description -> description.getMarkers().stream()
315
                                .anyMatch(marker -> marker.getMarkerType().equals(MarkerType.COMPUTED())))
316
                        // add them to the list
317
                        .forEach(computedDescription -> computedDescriptions.add(computedDescription)));
318

  
319
        UpdateResult aggregateDescription = aggregateDescription(taxon, computedDescriptions,
320
                "[Taxon Descriptions]"+taxon.getTitleCache(), descriptiveDataSetUuid);
321
        result.includeResult(aggregateDescription);
322
        result.setCdmEntity(aggregateDescription.getCdmEntity());
323
        aggregateDescription.setCdmEntity(null);
324
        return result;
325
    }
326

  
327
    @Override
328
    @Transactional(readOnly=false)
329
    public UpdateResult aggregateDescription(UUID taxonUuid, List<UUID> descriptionUuids, String descriptionTitle
330
            , UUID descriptiveDataSetUuid) {
331
        UpdateResult result = new UpdateResult();
332

  
333
        TaxonBase taxonBase = taxonService.load(taxonUuid);
334
        if(!(taxonBase instanceof Taxon)){
335
            result.addException(new ClassCastException("The given taxonUUID does not belong to a taxon"));
336
            result.setError();
337
            return result;
338
        }
339
        Taxon taxon = (Taxon)taxonBase;
340

  
341
        List<DescriptionBase> descriptions = descriptionService.load(descriptionUuids, null);
342

  
343
        UpdateResult aggregateDescriptionResult = aggregateDescription(taxon, descriptions, descriptionTitle, descriptiveDataSetUuid);
344
        result.setCdmEntity(aggregateDescriptionResult.getCdmEntity());
345
        aggregateDescriptionResult.setCdmEntity(null);
346
        result.includeResult(aggregateDescriptionResult);
347
        return result;
348
    }
349

  
350
    @SuppressWarnings("unchecked")
351
    private UpdateResult aggregateDescription(Taxon taxon, List<? extends DescriptionBase> descriptions, String descriptionTitle
352
            , UUID descriptiveDataSetUuid) {
353
        UpdateResult result = new UpdateResult();
354
        Map<Character, List<DescriptionElementBase>> featureToElementMap = new HashMap<>();
355

  
356
        DescriptiveDataSet dataSet = load(descriptiveDataSetUuid);
357
        if(dataSet==null){
358
            result.addException(new IllegalArgumentException("Could not find data set for uuid "+descriptiveDataSetUuid));
359
            result.setAbort();
360
            return result;
361
        }
362

  
363
        //extract all character description elements
364
        descriptions.forEach(description->{
365
            description.getElements()
366
            .stream()
367
            //filter out elements that do not have a Characters as Feature
368
            .filter(element->HibernateProxyHelper.isInstanceOf(((DescriptionElementBase)element).getFeature(), Character.class))
369
            .forEach(ele->{
370
                DescriptionElementBase descriptionElement = (DescriptionElementBase)ele;
371
                List<DescriptionElementBase> list = featureToElementMap.get(descriptionElement.getFeature());
372
                if(list==null){
373
                    list = new ArrayList<>();
374
                }
375
                list.add(descriptionElement);
376
                featureToElementMap.put(HibernateProxyHelper.deproxy(descriptionElement.getFeature(), Character.class), list);
377
            });
378
        });
379

  
380
        TaxonDescription description = TaxonDescription.NewInstance(taxon);
381
        description.setTitleCache("[Aggregation] "+descriptionTitle, true);
382
        description.addMarker(Marker.NewInstance(MarkerType.COMPUTED(), true));
383
        IdentifiableSource source = IdentifiableSource.NewInstance(OriginalSourceType.Aggregation);
384
        description.addSource(source);
385
        description.addDescriptiveDataSet(dataSet);
386

  
387
        featureToElementMap.forEach((feature, elements)->{
388
            //aggregate categorical data
389
            if(feature.isSupportsCategoricalData()){
390
                CategoricalData aggregate = CategoricalData.NewInstance(feature);
391
                elements.stream()
392
                .filter(element->element instanceof CategoricalData)
393
                .forEach(categoricalData->((CategoricalData)categoricalData).getStateData()
394
                        .forEach(stateData->aggregate.addStateData((StateData) stateData.clone())));
395
                description.addElement(aggregate);
396
            }
397
            //aggregate quantitative data
398
            else if(feature.isSupportsQuantitativeData()){
399
                QuantitativeData aggregate = QuantitativeData.NewInstance(feature);
400
                elements.stream()
401
                .filter(element->element instanceof QuantitativeData)
402
                .forEach(categoricalData->((QuantitativeData)categoricalData).getStatisticalValues()
403
                        .forEach(statisticalValue->aggregate.addStatisticalValue((StatisticalMeasurementValue) statisticalValue.clone())));
404
                description.addElement(aggregate);
405
            }
406
        });
407
        result.addUpdatedObject(taxon);
408
        result.setCdmEntity(description);
409
        return result;
410
    }
411

  
412
    @Override
288 413
    public TaxonRowWrapperDTO createTaxonDescription(UUID dataSetUuid, UUID taxonNodeUuid, MarkerType markerType, boolean markerFlag){
289 414
        DescriptiveDataSet dataSet = load(dataSetUuid);
290 415
        TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid, Arrays.asList("taxon"));
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IDescriptionService.java
17 17

  
18 18
import eu.etaxonomy.cdm.api.service.pager.Pager;
19 19
import eu.etaxonomy.cdm.api.utility.DescriptionUtility;
20
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
21 20
import eu.etaxonomy.cdm.model.common.Annotation;
22 21
import eu.etaxonomy.cdm.model.common.DefinedTerm;
23 22
import eu.etaxonomy.cdm.model.common.Language;
......
609 608
     */
610 609
    public UpdateResult moveTaxonDescription(UUID descriptionUuid, UUID targetTaxonUuid);
611 610

  
612
    /**
613
     * Aggregates the character data of the given descriptions and creates a new description
614
     * for the given taxon with the aggregated data.
615
     * @param taxonUuid the UUID of the taxon where the new description with the
616
     * aggregated data will be stored
617
     * @param descriptionUuids the uuid of the descriptions that are aggregated
618
     * @param descriptionTitle the new title of the description
619
     * @param descriptiveDataSetUuid the uuid of the descriptive data set to which the
620
     * aggregated description will be added to
621
     * @return the result of the operation
622
     */
623
    public UpdateResult aggregateDescription(UUID taxonUuid, List<UUID> descriptionUuids, String descriptionTitle
624
            , UUID descriptiveDataSetUuid);
625

  
626
    /**
627
     * Aggregates all {@link TaxonDescription}s of all sub nodes that have a "computed" marker
628
     * @param taxonNodeUuid the parent taxon node
629
     * @param descriptiveDataSetUuid the uuid of the descriptive data set to which the
630
     * aggregated description will be added to
631
     * @param monitor the progress monitor
632
     * @return the update result
633
     */
634
    public UpdateResult aggregateTaxonDescription(UUID taxonNodeUuid, UUID descriptiveDataSetUuid,
635
            IRemotingProgressMonitor monitor);
636 611
}
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IDescriptiveDataSetService.java
10 10
import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
11 11
import eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO;
12 12
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
13
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
13 14
import eu.etaxonomy.cdm.model.common.MarkerType;
14 15
import eu.etaxonomy.cdm.model.description.DescriptionBase;
15 16
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
......
143 144
     *
144 145
     */
145 146
    public List<TaxonNode> loadFilteredTaxonNodes(DescriptiveDataSet descriptiveDataSet, List<String> propertyPaths);
147

  
148
    /**
149
     * Aggregates the character data of the given descriptions and creates a new description
150
     * for the given taxon with the aggregated data.
151
     * @param taxonUuid the UUID of the taxon where the new description with the
152
     * aggregated data will be stored
153
     * @param descriptionUuids the uuid of the descriptions that are aggregated
154
     * @param descriptionTitle the new title of the description
155
     * @param descriptiveDataSetUuid the uuid of the descriptive data set to which the
156
     * aggregated description will be added to
157
     * @return the result of the operation
158
     */
159
    public UpdateResult aggregateDescription(UUID taxonUuid, List<UUID> descriptionUuids, String descriptionTitle
160
            , UUID descriptiveDataSetUuid);
161

  
162
    /**
163
     * Aggregates all {@link TaxonDescription}s of all sub nodes that have a "computed" marker
164
     * @param taxonNodeUuid the parent taxon node
165
     * @param descriptiveDataSetUuid the uuid of the descriptive data set to which the
166
     * aggregated description will be added to
167
     * @param monitor the progress monitor
168
     * @return the update result
169
     */
170
    public UpdateResult aggregateTaxonDescription(UUID taxonNodeUuid, UUID descriptiveDataSetUuid,
171
            IRemotingProgressMonitor monitor);
146 172
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)