Project

General

Profile

Revision a4ca6fce

IDa4ca6fce4f10fc9d9cd62e21457eb5e8e96cb59a
Parent 412b2eee
Child 2fc6f4c0

Added by Patrick Plitzner 12 months ago

ref #8679 Add sources to aggregated description

  • within taxon: specimen description
  • parent taxon: aggregated taxon description

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/description/StructuredDescriptionAggregation.java
9 9
package eu.etaxonomy.cdm.api.service.description;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.Arrays;
13 12
import java.util.HashMap;
14 13
import java.util.HashSet;
15 14
import java.util.List;
......
19 18
import java.util.stream.Collectors;
20 19

  
21 20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
22 22
import eu.etaxonomy.cdm.model.description.CategoricalData;
23
import eu.etaxonomy.cdm.model.description.DescriptionBase;
23 24
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
24 25
import eu.etaxonomy.cdm.model.description.DescriptionType;
25 26
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
......
32 33
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
33 34
import eu.etaxonomy.cdm.model.description.TaxonDescription;
34 35
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
36
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
35 37
import eu.etaxonomy.cdm.model.taxon.Taxon;
36 38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 39

  
......
50 52
public class StructuredDescriptionAggregation
51 53
        extends DescriptionAggregationBase<StructuredDescriptionAggregation, StructuredDescriptionAggregationConfiguration>{
52 54

  
53
    private static final List<String> DATASET_INIT_STRATEGY = Arrays.asList(new String[] {
54
            "descriptions", //$NON-NLS-1$
55
            "descriptions.sources", //$NON-NLS-1$
56
            "descriptions.descriptionElements", //$NON-NLS-1$
57
            "descriptions.descriptionElements.stateData", //$NON-NLS-1$
58
            "descriptions.descriptionElements.stateData.state", //$NON-NLS-1$
59
            "descriptions.descriptionElements.feature", //$NON-NLS-1$
60
            "descriptions.descriptiveDataSets", //$NON-NLS-1$
61
    });
62 55
    private DescriptiveDataSet dataSet;
63 56

  
64 57
    @Override
......
73 66
        // take start time for performance testing
74 67
        double start = System.currentTimeMillis();
75 68

  
76
        getResult().setCdmEntity(getDescriptiveDatasetService().load(getConfig().getDatasetUuid(), DATASET_INIT_STRATEGY));
69
        getResult().setCdmEntity(getDescriptiveDatasetService().load(getConfig().getDatasetUuid()));
77 70

  
78 71
        double end1 = System.currentTimeMillis();
79 72
        logger.info("Time elapsed for pre-accumulate() : " + (end1 - start) / (1000) + "s");
......
132 125

  
133 126
        replaceExistingDescriptionElements(targetDescription, structuredResultHolder.categoricalMap);
134 127
        replaceExistingDescriptionElements(targetDescription, structuredResultHolder.quantitativeMap);
128
        addAggregationSources(targetDescription, structuredResultHolder);
135 129

  
136 130
        if(!targetDescription.getElements().isEmpty()){
137 131
            dataSet.addDescription(targetDescription);
138 132
        }
139 133
    }
140 134

  
135
    private void addAggregationSources(TaxonDescription targetDescription,
136
            StructuredDescriptionResultHolder structuredResultHolder) {
137
        //FIXME Re-use sources if possible
138
        //Remove sources from description
139
        Set<IdentifiableSource> sourcesToRemove = targetDescription.getSources().stream()
140
                .filter(source->source.getType().equals(OriginalSourceType.Aggregation))
141
                .collect(Collectors.toSet());
142

  
143
        for (IdentifiableSource source : sourcesToRemove) {
144
            targetDescription.removeSource(source);
145
        }
146

  
147
        Set<DescriptionBase> sourceDescriptions = structuredResultHolder.sourceDescriptions;
148
        for (DescriptionBase descriptionBase : sourceDescriptions) {
149
            DescriptionBase sourceDescription = null;
150
            if(descriptionBase.isInstanceOf(SpecimenDescription.class)){
151
                DescriptionBase clone = (DescriptionBase)descriptionBase.clone();
152
                clone.removeDescriptiveDataSet(dataSet);
153
                clone.getTypes().add(DescriptionType.CLONE_FOR_SOURCE);
154
                SpecimenOrObservationBase specimen = CdmBase.deproxy(descriptionBase, SpecimenDescription.class).getDescribedSpecimenOrObservation();
155
                specimen.addDescription(CdmBase.deproxy(clone, SpecimenDescription.class));
156
                sourceDescription=clone;
157
            }
158
            else if(descriptionBase.isInstanceOf(TaxonDescription.class)){
159
                Taxon taxon = CdmBase.deproxy(descriptionBase, TaxonDescription.class).getTaxon();
160
                taxon.addDescription(CdmBase.deproxy(descriptionBase, TaxonDescription.class));
161
                sourceDescription=descriptionBase;
162
            }
163
            if(sourceDescription!=null){
164
                targetDescription.addAggregationSource(sourceDescription);
165
            }
166
        }
167
    }
168

  
141 169
    private void replaceExistingDescriptionElements(TaxonDescription targetDescription,
142 170
            Map<Feature, ? extends DescriptionElementBase> elementMap) {
143 171
        for (Entry<Feature, ? extends DescriptionElementBase> entry : elementMap.entrySet()) {
......
162 190

  
163 191
    @Override
164 192
    protected void initTransaction() {
165
        dataSet = getDescriptiveDatasetService().load(getConfig().getDatasetUuid(), DATASET_INIT_STRATEGY);
193
        dataSet = getDescriptiveDatasetService().load(getConfig().getDatasetUuid());
166 194
    }
167 195

  
168 196
    @Override
......
178 206
            ResultHolder resultHolder,
179 207
            Set<TaxonDescription> excludedDescriptions) {
180 208
        StructuredDescriptionResultHolder descriptiveResultHolder = (StructuredDescriptionResultHolder)resultHolder;
181
        Set<TaxonDescription> childTaxonDescriptions = getChildTaxonDescriptions(taxonNode, dataSet);
182
        for (TaxonDescription desc:childTaxonDescriptions){
183
            for (DescriptionElementBase deb:desc.getElements()){
184
                if (deb.isCharacterData()){
185
                    if (deb.isInstanceOf(CategoricalData.class)){
186
                        addToCategorical(CdmBase.deproxy(deb, CategoricalData.class), descriptiveResultHolder);
187
                    }else if (deb.isInstanceOf(QuantitativeData.class)){
188
                        addToQuantitative(CdmBase.deproxy(deb, QuantitativeData.class), descriptiveResultHolder);
189
                    }
190
                }
191
            }
192
        }
209
        addDescriptionElement(descriptiveResultHolder, getChildTaxonDescriptions(taxonNode, dataSet));
193 210
    }
194 211

  
195 212
    @Override
......
197 214
            ResultHolder resultHolder,
198 215
            Set<TaxonDescription> excludedDescriptions) {
199 216
        StructuredDescriptionResultHolder descriptiveResultHolder = (StructuredDescriptionResultHolder)resultHolder;
200
        Set<SpecimenDescription> specimenDescriptions = getSpecimenDescriptions(taxon, dataSet);
201
        for (SpecimenDescription desc:specimenDescriptions){
202
            for (DescriptionElementBase deb:desc.getElements()){
217
        addDescriptionElement(descriptiveResultHolder, getSpecimenDescriptions(taxon, dataSet));
218
    }
219

  
220
    private void addDescriptionElement(StructuredDescriptionResultHolder descriptiveResultHolder,
221
            Set<? extends DescriptionBase> descriptions) {
222
        boolean descriptionWasUsed = false;
223
        for (DescriptionBase desc:descriptions){
224
            for (DescriptionElementBase deb:(Set<DescriptionElementBase>)desc.getElements()){
203 225
                if (hasCharacterData(deb)){
204 226
                    if (deb.isInstanceOf(CategoricalData.class)){
205 227
                        addToCategorical(CdmBase.deproxy(deb, CategoricalData.class), descriptiveResultHolder);
228
                        descriptionWasUsed = true;
206 229
                    }else if (deb.isInstanceOf(QuantitativeData.class)){
207 230
                        addToQuantitative(CdmBase.deproxy(deb, QuantitativeData.class), descriptiveResultHolder);
231
                        descriptionWasUsed = true;
208 232
                    }
209 233
                }
210 234
            }
235
            if(descriptionWasUsed){
236
                descriptiveResultHolder.sourceDescriptions.add(desc);
237
            }
211 238
        }
212 239
    }
213 240

  
......
271 298
    private class StructuredDescriptionResultHolder implements ResultHolder{
272 299
        Map<Feature, CategoricalData> categoricalMap = new HashMap<>();
273 300
        Map<Feature, QuantitativeData> quantitativeMap = new HashMap<>();
301
        Set<DescriptionBase> sourceDescriptions = new HashSet<>();
274 302
    }
275 303

  
276 304
    /*

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)