Project

General

Profile

Revision 5ee7595c

ID5ee7595cbf364b6d05ba7f07143a1a104cac2ab9
Parent 25a98c3f
Child 47992818

Added by Andreas Müller about 1 year ago

ref #8679 move aggregation modes to own class file

View differences:

cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/DescriptionListController.java
39 39
import eu.etaxonomy.cdm.api.service.IDescriptionService;
40 40
import eu.etaxonomy.cdm.api.service.ITermService;
41 41
import eu.etaxonomy.cdm.api.service.IVocabularyService;
42
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.AggregationMode;
42
import eu.etaxonomy.cdm.api.service.description.AggregationMode;
43 43
import eu.etaxonomy.cdm.api.service.description.DistributionAggregation;
44 44
import eu.etaxonomy.cdm.api.service.description.DistributionAggregationConfiguration;
45 45
import eu.etaxonomy.cdm.api.service.dto.DistributionInfoDTO;
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/description/AggregationMode.java
1
/**
2
* Copyright (C) 2019 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
package eu.etaxonomy.cdm.api.service.description;
10

  
11
import java.util.Arrays;
12
import java.util.List;
13

  
14
public enum AggregationMode {
15
        WithinTaxon,
16
        ToParent;
17
//        public boolean isByRank() {
18
//           return this==byRanks || this == byAreasAndRanks;
19
//        }
20
//        public boolean isByArea() {
21
//            return this==byAreas || this == byAreasAndRanks;
22
//         }
23

  
24

  
25
        public static List<AggregationMode> byAreasAndRanks(){
26
            return Arrays.asList(new AggregationMode[]{AggregationMode.WithinTaxon, AggregationMode.ToParent});
27
        }
28
        public static List<AggregationMode> byAreas(){
29
            return Arrays.asList(new AggregationMode[]{AggregationMode.WithinTaxon});
30
        }
31
        public static List<AggregationMode> byRanks(){
32
            return Arrays.asList(new AggregationMode[]{AggregationMode.ToParent});
33
        }
34

  
35
    }
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/description/AggregationSourceMode.java
1
/**
2
* Copyright (C) 2019 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
package eu.etaxonomy.cdm.api.service.description;
10

  
11
public enum AggregationSourceMode {
12
    NONE,
13
    ALL,
14
    ALL_SAMEVALUE,
15
    DESCRIPTION,
16
    TAXON;
17
 }
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/description/DescriptionAggregationBase.java
30 30
import eu.etaxonomy.cdm.api.service.ITaxonService;
31 31
import eu.etaxonomy.cdm.api.service.ITermService;
32 32
import eu.etaxonomy.cdm.api.service.UpdateResult;
33
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.AggregationMode;
34 33
import eu.etaxonomy.cdm.common.DynamicBatch;
35 34
import eu.etaxonomy.cdm.common.JvmLimitsException;
36 35
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/description/DescriptionAggregationConfigurationBase.java
29 29

  
30 30
    private TaxonNodeFilter taxonNodeFilter;
31 31

  
32
    private SourceMode toParentSourceMode = SourceMode.DESCRIPTION;
33
    private SourceMode withinTaxonSourceMode = SourceMode.ALL_SAMEVALUE;
32
    private AggregationSourceMode toParentSourceMode = AggregationSourceMode.DESCRIPTION;
33
    private AggregationSourceMode withinTaxonSourceMode = AggregationSourceMode.ALL_SAMEVALUE;
34 34

  
35 35
    private List<AggregationMode> aggregationModes;
36 36

  
......
46 46
    private EnumSet<OriginalSourceType> aggregatingSourceTypes = EnumSet.of(
47 47
            OriginalSourceType.PrimaryTaxonomicSource, OriginalSourceType.PrimaryMediaSource);
48 48

  
49
// ************************** ENUMS ******************************/
50

  
51
    public enum AggregationMode {
52
        WithinTaxon,
53
        ToParent;
54
//        public boolean isByRank() {
55
//           return this==byRanks || this == byAreasAndRanks;
56
//        }
57
//        public boolean isByArea() {
58
//            return this==byAreas || this == byAreasAndRanks;
59
//         }
60
        public static List<AggregationMode> byAreasAndRanks(){
61
            return Arrays.asList(new AggregationMode[]{AggregationMode.WithinTaxon, AggregationMode.ToParent});
62
        }
63
        public static List<AggregationMode> byAreas(){
64
            return Arrays.asList(new AggregationMode[]{AggregationMode.WithinTaxon});
65
        }
66
        public static List<AggregationMode> byRanks(){
67
            return Arrays.asList(new AggregationMode[]{AggregationMode.ToParent});
68
        }
69

  
70
    }
71

  
72
    public enum SourceMode {
73
        NONE,
74
        ALL,
75
        ALL_SAMEVALUE,
76
        DESCRIPTION,
77
        TAXON;
78
     }
79

  
80 49
//******************* CONSTRUCTOR **********************/
81 50

  
82 51
    protected DescriptionAggregationConfigurationBase(TaxonNodeFilter filter,
......
137 106
        return this.aggregatingSourceTypes.remove(sourceTypeToRemove);
138 107
    }
139 108

  
140
    public SourceMode getToParentSourceMode() {
109
    public AggregationSourceMode getToParentSourceMode() {
141 110
        return toParentSourceMode;
142 111
    }
143
    public void setToParentSourceMode(SourceMode toParentSourceMode) {
112
    public void setToParentSourceMode(AggregationSourceMode toParentSourceMode) {
144 113
        this.toParentSourceMode = toParentSourceMode;
145 114
    }
146 115

  
147
    public SourceMode getWithinTaxonSourceMode() {
116
    public AggregationSourceMode getWithinTaxonSourceMode() {
148 117
        return withinTaxonSourceMode;
149 118
    }
150
    public void setWithinTaxonSourceMode(SourceMode withinTaxonSourceMode) {
119
    public void setWithinTaxonSourceMode(AggregationSourceMode withinTaxonSourceMode) {
151 120
        this.withinTaxonSourceMode = withinTaxonSourceMode;
152 121
    }
153 122
    public boolean isAdaptBatchSize() {
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/description/DistributionAggregation.java
23 23
import org.hibernate.search.Search;
24 24
import org.springframework.transaction.TransactionStatus;
25 25

  
26
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.SourceMode;
27 26
import eu.etaxonomy.cdm.model.common.CdmBase;
28 27
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
29 28
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
......
181 180
     * @param newStatus
182 181
     * @param additionalSourcesForWinningNewStatus Not in Use!
183 182
     *  In the case when <code>newStatus</code> is preferred over <code>accumulatedStatus</code> these Set of sources will be added to the sources of <code>b</code>
184
     * @param sourceMode
183
     * @param aggregationSourceMode
185 184
     * @return
186 185
     */
187 186
    private StatusAndSources choosePreferredOrMerge(StatusAndSources accumulatedStatus, StatusAndSources newStatus,
188
            Set<DescriptionElementSource> additionalSourcesForWinningNewStatus, SourceMode sourceMode){
187
            Set<DescriptionElementSource> additionalSourcesForWinningNewStatus, AggregationSourceMode aggregationSourceMode){
189 188

  
190 189
        if (newStatus == null || newStatus.status == null) {
191 190
            return accumulatedStatus;
......
209 208
            if(additionalSourcesForWinningNewStatus != null) {
210 209
                newStatus.addSources(additionalSourcesForWinningNewStatus);
211 210
            }
212
            if (sourceMode == SourceMode.ALL){
211
            if (aggregationSourceMode == AggregationSourceMode.ALL){
213 212
                newStatus.addSources(accumulatedStatus.sources);
214 213
            }
215 214
            return newStatus;
216 215
        } else {
217
            if (indexAcc == indexNew || sourceMode == SourceMode.ALL){
216
            if (indexAcc == indexNew || aggregationSourceMode == AggregationSourceMode.ALL){
218 217
                accumulatedStatus.addSources(newStatus.sources);
219 218
            }
220 219
            return accumulatedStatus;
......
310 309

  
311 310
            // accumulate all sub area status
312 311
            StatusAndSources accumulatedStatusAndSources = null;
313
            SourceMode sourceMode = getConfig().getWithinTaxonSourceMode();
312
            AggregationSourceMode aggregationSourceMode = getConfig().getWithinTaxonSourceMode();
314 313
            // TODO consider using the TermHierarchyLookup (only in local branch a.kohlbecker)
315 314
            Set<NamedArea> subAreas = getSubAreasFor(superArea);
316 315
            for(NamedArea subArea : subAreas){
......
330 329
                                || (getConfig().isIgnoreAbsentStatusByArea() && status.isAbsenceTerm())){
331 330
                            continue;
332 331
                        }
333
                        StatusAndSources subAreaStatusAndSources = new StatusAndSources(status, distribution, sourceMode);
334
                        accumulatedStatusAndSources = choosePreferredOrMerge(accumulatedStatusAndSources, subAreaStatusAndSources, null, sourceMode);
332
                        StatusAndSources subAreaStatusAndSources = new StatusAndSources(status, distribution, aggregationSourceMode);
333
                        accumulatedStatusAndSources = choosePreferredOrMerge(accumulatedStatusAndSources, subAreaStatusAndSources, null, aggregationSourceMode);
335 334
                    }
336 335
                }
337 336
            } // next sub area
338 337

  
339 338

  
340 339
            if (accumulatedStatusAndSources != null) {
341
                StatusAndSources preferedStatus = choosePreferredOrMerge(accumulatedStatusMap.get(superArea), accumulatedStatusAndSources, null, sourceMode);
340
                StatusAndSources preferedStatus = choosePreferredOrMerge(accumulatedStatusMap.get(superArea), accumulatedStatusAndSources, null, aggregationSourceMode);
342 341
                accumulatedStatusMap.put(superArea, preferedStatus);
343 342
            }
344 343

  
......
359 358
        private final PresenceAbsenceTerm status;
360 359
        private final Set<DescriptionElementSource> sources = new HashSet<>();
361 360

  
362
        public StatusAndSources(PresenceAbsenceTerm status, DescriptionElementBase deb, SourceMode sourceMode) {
361
        public StatusAndSources(PresenceAbsenceTerm status, DescriptionElementBase deb, AggregationSourceMode aggregationSourceMode) {
363 362
            this.status = status;
364
            if (sourceMode == SourceMode.NONE){
363
            if (aggregationSourceMode == AggregationSourceMode.NONE){
365 364
                return;
366
            }else if (sourceMode == SourceMode.DESCRIPTION){
365
            }else if (aggregationSourceMode == AggregationSourceMode.DESCRIPTION){
367 366
                sources.add(DescriptionElementSource.NewAggregationInstance(deb.getInDescription()));
368
            }else if (sourceMode == SourceMode.TAXON){
367
            }else if (aggregationSourceMode == AggregationSourceMode.TAXON){
369 368
                if (deb.getInDescription().isInstanceOf(TaxonDescription.class)){
370 369
                    TaxonDescription td = CdmBase.deproxy(deb.getInDescription(), TaxonDescription.class);
371 370
                    sources.add(DescriptionElementSource.NewAggregationInstance(td.getTaxon()));
372 371
                }else{
373 372
                    logger.warn("Description is not of type TaxonDescription. Adding source not possible");
374 373
                }
375
            }else if (sourceMode == SourceMode.ALL || sourceMode == SourceMode.ALL_SAMEVALUE){
374
            }else if (aggregationSourceMode == AggregationSourceMode.ALL || aggregationSourceMode == AggregationSourceMode.ALL_SAMEVALUE){
376 375
                addSourcesDeduplicated(this.sources, deb.getSources());
377 376
            }else{
378
                throw new RuntimeException("Unhandled source aggregation mode: " + sourceMode);
377
                throw new RuntimeException("Unhandled source aggregation mode: " + aggregationSourceMode);
379 378
            }
380 379
        }
381 380

  
......
430 429
                    }
431 430

  
432 431
                    NamedArea area = distribution.getArea();
433
                    SourceMode sourceMode = getConfig().getToParentSourceMode();
432
                    AggregationSourceMode aggregationSourceMode = getConfig().getToParentSourceMode();
434 433

  
435
                    StatusAndSources childStatusAndSources = new StatusAndSources(status, distribution, sourceMode);
434
                    StatusAndSources childStatusAndSources = new StatusAndSources(status, distribution, aggregationSourceMode);
436 435
                    StatusAndSources preferedStatus = choosePreferredOrMerge(accumulatedStatusMap.get(area),
437
                            childStatusAndSources, null, sourceMode );
436
                            childStatusAndSources, null, aggregationSourceMode );
438 437
                    accumulatedStatusMap.put(area, preferedStatus);
439 438
                }
440 439

  
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/description/DistributionAggregationTest.java
40 40
import eu.etaxonomy.cdm.api.service.IReferenceService;
41 41
import eu.etaxonomy.cdm.api.service.ITaxonService;
42 42
import eu.etaxonomy.cdm.api.service.ITermService;
43
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.AggregationMode;
44
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.SourceMode;
45 43
import eu.etaxonomy.cdm.common.CdmUtils;
46 44
import eu.etaxonomy.cdm.common.JvmLimitsException;
47 45
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
......
310 308
        TaxonNodeFilter filter = TaxonNodeFilter.NewInstance(null, null, null, null, null, lowerRank.getUuid(), upperRank.getUuid());
311 309
        DistributionAggregationConfiguration config = DistributionAggregationConfiguration.NewInstance(
312 310
                AggregationMode.byAreasAndRanks(), superAreas, filter, monitor);
313
        config.setToParentSourceMode(SourceMode.ALL_SAMEVALUE);
311
        config.setToParentSourceMode(AggregationSourceMode.ALL_SAMEVALUE);
314 312
        engine.invoke(config, repository);
315 313

  
316 314
        //test
......
463 461
        TaxonNodeFilter filter = TaxonNodeFilter.NewInstance(null, null, null, null, null, lowerRank.getUuid(), upperRank.getUuid());
464 462
        DistributionAggregationConfiguration config = DistributionAggregationConfiguration.NewInstance(
465 463
                AggregationMode.byAreasAndRanks(), superAreas, filter, monitor);
466
        config.setToParentSourceMode(SourceMode.ALL_SAMEVALUE);
464
        config.setToParentSourceMode(AggregationSourceMode.ALL_SAMEVALUE);
467 465
        engine.invoke(config, repository);
468 466

  
469 467
        Taxon lapsana_communis = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
......
518 516
        TaxonNodeFilter filter = TaxonNodeFilter.NewInstance(null, null, null, null, null, lowerRank.getUuid(), upperRank.getUuid());
519 517
        DistributionAggregationConfiguration config = DistributionAggregationConfiguration.NewInstance(
520 518
                AggregationMode.byAreasAndRanks(), superAreas, filter, monitor);
521
        config.setToParentSourceMode(SourceMode.ALL_SAMEVALUE);
519
        config.setToParentSourceMode(AggregationSourceMode.ALL_SAMEVALUE);
522 520
        engine.invoke(config, repository);
523 521

  
524 522
        Taxon lapsana_communis = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
......
598 596
    }
599 597

  
600 598
    /**
601
     * Test to check if {@link SourceMode#DESCRIPTION} works for toParentAggregation.
599
     * Test to check if {@link AggregationSourceMode#DESCRIPTION} works for toParentAggregation.
602 600
     */
603 601
    @Test
604 602
    @DataSets({
......
621 619
        TaxonNodeFilter filter = TaxonNodeFilter.NewInstance(null, null, null, null, null, lowerRank.getUuid(), upperRank.getUuid());
622 620
        DistributionAggregationConfiguration config = DistributionAggregationConfiguration.NewInstance(
623 621
                AggregationMode.byRanks(), superAreas, filter, monitor);
624
        config.setToParentSourceMode(SourceMode.DESCRIPTION);
622
        config.setToParentSourceMode(AggregationSourceMode.DESCRIPTION);
625 623
        engine.invoke(config, repository);
626 624

  
627 625
        Taxon lapsana_communis = (Taxon) taxonService.load(T_LAPSANA_COMMUNIS_UUID);
......
636 634
    }
637 635

  
638 636
    /**
639
     * Test to check if {@link SourceMode#DESCRIPTION} works for toParentAggregation.
637
     * Test to check if {@link AggregationSourceMode#DESCRIPTION} works for toParentAggregation.
640 638
     */
641 639
    @Test
642 640
    @DataSets({
......
660 658
        DistributionAggregationConfiguration config = DistributionAggregationConfiguration.NewInstance(
661 659
                AggregationMode.byAreasAndRanks(), superAreas, filter, monitor);
662 660
        config.setAggregatingSourceTypes(EnumSet.of(OriginalSourceType.PrimaryTaxonomicSource));
663
        config.setToParentSourceMode(SourceMode.ALL);
661
        config.setToParentSourceMode(AggregationSourceMode.ALL);
664 662
        engine.invoke(config, repository);
665 663

  
666 664
        //test
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/description/StructuredDescriptionAggregationTest.java
33 33
import eu.etaxonomy.cdm.api.service.ITermTreeService;
34 34
import eu.etaxonomy.cdm.api.service.IVocabularyService;
35 35
import eu.etaxonomy.cdm.api.service.UpdateResult;
36
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.AggregationMode;
37 36
import eu.etaxonomy.cdm.common.JvmLimitsException;
38 37
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
39 38
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
cdmlib-services/src/test/java/eu/etaxonomy/cdm/test/function/TestAggregations.java
16 16
import org.apache.log4j.Logger;
17 17

  
18 18
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
19
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.AggregationMode;
20
import eu.etaxonomy.cdm.api.service.description.DescriptionAggregationConfigurationBase.SourceMode;
21 19
import eu.etaxonomy.cdm.api.service.description.DistributionAggregationConfiguration;
20
import eu.etaxonomy.cdm.api.service.description.AggregationMode;
21
import eu.etaxonomy.cdm.api.service.description.AggregationSourceMode;
22 22
import eu.etaxonomy.cdm.api.service.pager.Pager;
23 23
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
24 24
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
......
127 127
		List<UUID> areaList = areaPager.getRecords().stream().map(p ->p.getUuid()).collect(Collectors.toList());
128 128
		DistributionAggregationConfiguration config = DistributionAggregationConfiguration
129 129
		        .NewInstance(modes, areaList, filter, monitor);
130
		config.setToParentSourceMode(SourceMode.NONE);
131
        config.setWithinTaxonSourceMode(SourceMode.NONE);
130
		config.setToParentSourceMode(AggregationSourceMode.NONE);
131
        config.setWithinTaxonSourceMode(AggregationSourceMode.NONE);
132 132

  
133 133
		try {
134 134
            config.getTaskInstance().invoke(config, appCtr);

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)