Project

General

Profile

Revision 041e26e2

ID041e26e260df6566ae0737678e20ef06c5caa276
Parent 25f9ee23
Child fca09e81

Added by Patrick Plitzner over 3 years ago

ref #7674 Create default taxon description when adding specimens

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/DescriptiveDataSetService.java
11 11
import java.util.Optional;
12 12
import java.util.Set;
13 13
import java.util.UUID;
14
import java.util.stream.Collectors;
14 15

  
15 16
import org.apache.log4j.Logger;
16 17
import org.springframework.beans.factory.annotation.Autowired;
......
26 27
import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
27 28
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28 29
import eu.etaxonomy.cdm.model.common.Language;
30
import eu.etaxonomy.cdm.model.common.MarkerType;
29 31
import eu.etaxonomy.cdm.model.description.CategoricalData;
30 32
import eu.etaxonomy.cdm.model.description.DescriptionBase;
31 33
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
......
125 127
                rowWrapper = createTaxonRowWrapper(HibernateProxyHelper.deproxy(description, TaxonDescription.class), descriptiveDataSet);
126 128
            }
127 129
            else if (HibernateProxyHelper.isInstanceOf(description, SpecimenDescription.class)){
128
                rowWrapper = createSpecimenRowWrapper(HibernateProxyHelper.deproxy(description, SpecimenDescription.class), descriptiveDataSet);
130
                rowWrapper = createSpecimenRowWrapper(HibernateProxyHelper.deproxy(description, SpecimenDescription.class), descriptiveDataSet, false);
129 131
            }
130 132
            if(rowWrapper!=null){
131 133
                wrappers.add(rowWrapper);
......
183 185
    }
184 186

  
185 187
    @Override
186
    public SpecimenRowWrapperDTO createSpecimenRowWrapper(SpecimenDescription description, DescriptiveDataSet descriptiveDataSet){
188
    public SpecimenRowWrapperDTO createSpecimenRowWrapper(SpecimenDescription description, DescriptiveDataSet descriptiveDataSet,
189
            boolean createDefaultTaxonDescription){
187 190
	    SpecimenOrObservationBase specimen = description.getDescribedSpecimenOrObservation();
188 191
	    TaxonNode taxonNode = null;
189 192
        FieldUnit fieldUnit = null;
190 193
        String identifier = null;
191 194
        NamedArea country = null;
192 195
        //supplemental information
193
        if(specimen!=null){
194
            //get taxon node
195
            Set<TaxonNode> taxonSubtreeFilter = descriptiveDataSet.getTaxonSubtreeFilter();
196
            for (TaxonNode node : taxonSubtreeFilter) {
197
                //check for node
198
                node = taxonNodeService.load(node.getId(), Arrays.asList("taxon"));
199
                taxonNode = findTaxonNodeForDescription(node, specimen);
200
                if(taxonNode!=null){
201
                    break;
202
                }
203
                else{
204
                    //check for child nodes
205
                    List<TaxonNode> allChildren = taxonNodeService.loadChildNodesOfTaxonNode(node, Arrays.asList("taxon"), true, true, null);
206
                    for (TaxonNode child : allChildren) {
207
                        taxonNode = findTaxonNodeForDescription(child, specimen);
208
                        if(taxonNode!=null){
209
                            break;
210
                        }
211
                    }
212
                }
213
            }
214
            if(taxonNode==null){
215
                return null;
216
            }
217
            Collection<FieldUnit> fieldUnits = occurrenceService.findFieldUnits(specimen.getUuid(),
218
                    Arrays.asList(new String[]{
219
                            "gatheringEvent",
220
                            "gatheringEvent.country"
221
                            }));
222
            if(fieldUnits.size()!=1){
223
                logger.error("More than one or no field unit found for specimen"); //$NON-NLS-1$
224
                return null;
196
        //get taxon node
197
        Set<TaxonNode> taxonSubtreeFilter = descriptiveDataSet.getTaxonSubtreeFilter();
198
        for (TaxonNode node : taxonSubtreeFilter) {
199
            //check for node
200
            node = taxonNodeService.load(node.getId(), Arrays.asList("taxon"));
201
            taxonNode = findTaxonNodeForDescription(node, specimen);
202
            if(taxonNode!=null){
203
                break;
225 204
            }
226 205
            else{
227
                fieldUnit = fieldUnits.iterator().next();
228
            }
229
            if(specimen instanceof DerivedUnit){
230
                identifier = occurrenceService.getMostSignificantIdentifier(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class));
231
            }
232
            if(fieldUnit!=null && fieldUnit.getGatheringEvent()!=null){
233
                country = fieldUnit.getGatheringEvent().getCountry();
206
                //check for child nodes
207
                List<TaxonNode> allChildren = taxonNodeService.loadChildNodesOfTaxonNode(node, Arrays.asList("taxon"), true, true, null);
208
                for (TaxonNode child : allChildren) {
209
                    taxonNode = findTaxonNodeForDescription(child, specimen);
210
                    if(taxonNode!=null){
211
                        break;
212
                    }
213
                }
234 214
            }
235 215
        }
236
        return new SpecimenRowWrapperDTO(description, taxonNode, fieldUnit, identifier, country);
216
        if(taxonNode==null){
217
            return null;
218
        }
219
        //taxon node was found
220

  
221
        //get field unit
222
        Collection<FieldUnit> fieldUnits = occurrenceService.findFieldUnits(specimen.getUuid(),
223
                Arrays.asList(new String[]{
224
                        "gatheringEvent",
225
                        "gatheringEvent.country"
226
                }));
227
        if(fieldUnits.size()!=1){
228
            logger.error("More than one or no field unit found for specimen"); //$NON-NLS-1$
229
            return null;
230
        }
231
        else{
232
            fieldUnit = fieldUnits.iterator().next();
233
        }
234
        //get identifier
235
        if(specimen instanceof DerivedUnit){
236
            identifier = occurrenceService.getMostSignificantIdentifier(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class));
237
        }
238
        //get country
239
        if(fieldUnit!=null && fieldUnit.getGatheringEvent()!=null){
240
            country = fieldUnit.getGatheringEvent().getCountry();
241
        }
242
        //get default taxon description
243
        TaxonDescription defaultTaxonDescription = findDefaultTaxonDescription(descriptiveDataSet.getUuid(),
244
                taxonNode.getUuid(), createDefaultTaxonDescription);
245
        TaxonRowWrapperDTO taxonRowWrapper = defaultTaxonDescription != null
246
                ? createTaxonRowWrapper(defaultTaxonDescription, descriptiveDataSet) : null;
247
        return new SpecimenRowWrapperDTO(description, taxonNode, fieldUnit, identifier, country, taxonRowWrapper);
237 248
	}
238 249

  
239 250
    @Override
......
247 258
    }
248 259

  
249 260
    @Override
250
    public SpecimenDescription findDescriptionForDescriptiveDataSet(UUID descriptiveDataSetUuid, UUID specimenUuid){
261
    public TaxonDescription findDefaultTaxonDescription(UUID descriptiveDataSetUuid, UUID taxonNodeUuid, boolean create){
262
        DescriptiveDataSet dataSet = load(descriptiveDataSetUuid);
263
        TaxonNode taxonNode = taxonNodeService.load(taxonNodeUuid, Arrays.asList("taxon", "taxon.descriptions", "taxon.descriptions.markers"));
264
        Set<DescriptionBase> dataSetDescriptions = dataSet.getDescriptions();
265
        //filter out COMPUTED descriptions
266
        List<TaxonDescription> nonComputedDescriptions = taxonNode.getTaxon().getDescriptions().stream()
267
                .filter(desc -> desc.getMarkers().stream()
268
                        .anyMatch(marker -> marker.getMarkerType().equals(MarkerType.COMPUTED())))
269
                .collect(Collectors.toList());
270
        for (TaxonDescription taxonDescription : nonComputedDescriptions) {
271
            for (DescriptionBase description : dataSetDescriptions) {
272
                if(description.getUuid().equals(taxonDescription.getUuid())){
273
                    return taxonDescription;
274
                }
275
            }
276
        }
277
        if(!create){
278
            return null;
279
        }
280
        //description not yet added to dataset -> create a new one
281
        TaxonDescription newTaxonDescription = TaxonDescription.NewInstance(taxonNode.getTaxon());
282
        newTaxonDescription.setTitleCache("Dataset "+dataSet.getLabel()+": "+newTaxonDescription.generateTitle(), true); //$NON-NLS-2$
283
        dataSet.getDescriptiveSystem().getDistinctFeatures().forEach(wsFeature->{
284
            if(wsFeature.isSupportsCategoricalData()){
285
                newTaxonDescription.addElement(CategoricalData.NewInstance(wsFeature));
286
            }
287
            else if(wsFeature.isSupportsQuantitativeData()){
288
                newTaxonDescription.addElement(QuantitativeData.NewInstance(wsFeature));
289
            }
290
        });
291
        return newTaxonDescription;
292
    }
293

  
294
    @Override
295
    public SpecimenDescription findSpecimenDescription(UUID descriptiveDataSetUuid, UUID specimenUuid){
251 296
        DescriptiveDataSet dataSet = load(descriptiveDataSetUuid);
252 297
        SpecimenOrObservationBase specimen = occurrenceService.load(specimenUuid);
253 298

  
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IDescriptiveDataSetService.java
84 84
     * Creates a specimen row wrapper object for the given description
85 85
     * @param description the specimen description for which the wrapper should be created
86 86
     * @param descriptiveDataSet the data set it should be used in
87
     * @param if <code>true</code> a default taxon description will be created for the taxon
88
     * this wrapper belongs to
87 89
     * @return the created row wrapper
88 90
     */
89
    public SpecimenRowWrapperDTO createSpecimenRowWrapper(SpecimenDescription description, DescriptiveDataSet descriptiveDataSet);
91
    public SpecimenRowWrapperDTO createSpecimenRowWrapper(SpecimenDescription description, DescriptiveDataSet descriptiveDataSet,
92
            boolean createDefaultTaxonDescription);
90 93

  
91 94
    /**
92 95
     * Creates a taxon row wrapper object for the given description
......
98 101

  
99 102
    /**
100 103
     * Returns a {@link SpecimenDescription} for a given specimen with corresponding
101
     * features according to the {@link DescriptiveDataSet}.
102
     * @param descriptiveDataSetUuid
103
     * @param specimenUuid
104
     * @return
104
     * features according to the {@link DescriptiveDataSet}.<br>
105
     * If a description is found that matches all features of the data set this description
106
     * will be returned. A new one will be created otherwise.
107
     * @param descriptiveDataSetUuid the uuid of the dataset defining the features
108
     * @param specimenUuid the uuid of the specimen
109
     * @return either the found specimen description or a newly created one
105 110
     */
106
    public SpecimenDescription findDescriptionForDescriptiveDataSet(UUID descriptiveDataSetUuid, UUID specimenUuid);
111
    public SpecimenDescription findSpecimenDescription(UUID descriptiveDataSetUuid, UUID specimenUuid);
112

  
113
    /**
114
     * Returns a {@link TaxonDescription} for a given taxon node with corresponding
115
     * features according to the {@link DescriptiveDataSet}.<br>
116
     * If a description is found that matches all features of the data set this description
117
     * will be returned. Otherwise a new one will be created if specified.
118
     * @param descriptiveDataSetUuid the uuid of the dataset defining the features
119
     * @param taxonNodeUuid the uuid of the taxon node that links to the taxon
120
     * @param create if <code>true</code> a new description will be created
121
     * if none could be found
122
     * @return either the found taxon description or a newly created one
123
     */
124
    public TaxonDescription findDefaultTaxonDescription(UUID descriptiveDataSetUuid, UUID taxonNodeUuid, boolean create);
107 125

  
108 126
    /**
109 127
     * Loads all taxon nodes that match the filter set defined in the
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/dto/SpecimenRowWrapperDTO.java
24 24

  
25 25
    private static final long serialVersionUID = 5198447592554976471L;
26 26

  
27
    private TaxonRowWrapperDTO defaultTaxonDescription;
27 28
    private SpecimenOrObservationBase specimen;
28 29
    private FieldUnit fieldUnit;
29 30
    private String identifier;
30 31
    private NamedArea country;
31 32

  
32 33
    public SpecimenRowWrapperDTO(SpecimenDescription description, TaxonNode taxonNode, FieldUnit fieldUnit, String identifier,
33
                NamedArea country) {
34
                NamedArea country, TaxonRowWrapperDTO defaultTaxonDescription) {
34 35
        super(description, taxonNode);
35 36
        this.fieldUnit = fieldUnit;
36 37
        this.identifier = identifier;
37 38
        this.country = country;
39
        this.defaultTaxonDescription = defaultTaxonDescription;
38 40
        this.specimen = description.getDescribedSpecimenOrObservation();
39 41
    }
40 42

  
......
53 55
    public NamedArea getCountry() {
54 56
        return country;
55 57
    }
58

  
59
    public TaxonRowWrapperDTO getDefaultTaxonDescription() {
60
        return defaultTaxonDescription;
61
    }
56 62
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)