Project

General

Profile

Download (5.33 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2017 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.taxeditor.editor.workingSet.matrix;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.HashMap;
14
import java.util.Map;
15
import java.util.Set;
16

    
17
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
18
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19
import eu.etaxonomy.cdm.model.description.CategoricalData;
20
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
21
import eu.etaxonomy.cdm.model.description.Feature;
22
import eu.etaxonomy.cdm.model.description.QuantitativeData;
23
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
24
import eu.etaxonomy.cdm.model.description.State;
25
import eu.etaxonomy.cdm.model.location.NamedArea;
26
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
27
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
28
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31
import eu.etaxonomy.taxeditor.model.MessagingUtils;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34
/**
35
 * Wrapper object representing one row in the character matrix
36
 * containing a specimen description and supplemental information
37
 * about the taxon and the specimen
38
 * @author pplitzner
39
 * @since Dec 14, 2017
40
 *
41
 */
42
public class RowWrapper {
43

    
44
    private SpecimenDescription description;
45

    
46
    private Taxon associatedTaxon;
47
    private FieldUnit fieldUnit;
48
    private String identifier;
49
    private NamedArea country;
50
    private Map<Feature, DescriptionElementBase> featureToElementMap = new HashMap<>();
51

    
52
    public RowWrapper(SpecimenDescription description) {
53
        this.description = description;
54

    
55
        IOccurrenceService occurrenceService = CdmStore.getService(IOccurrenceService.class);
56
        SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(description.getDescribedSpecimenOrObservation(), SpecimenOrObservationBase.class);
57
        //supplemental information
58
        if(specimen!=null){
59
            Collection<TaxonBase<?>> associatedTaxa = occurrenceService.listAssociatedTaxa(specimen, null, null, null, null);
60
            if(associatedTaxa!=null){
61
                associatedTaxon = (Taxon) associatedTaxa.iterator().next();
62
            }
63
            Collection<FieldUnit> fieldUnits = occurrenceService.getFieldUnits(specimen.getUuid());
64
            if(fieldUnits.size()!=1){
65
                MessagingUtils.error(RowWrapper.class, "More than one or no field unit found for specimen", null);
66
            }
67
            else{
68
                fieldUnit = fieldUnits.iterator().next();
69
            }
70
            if(specimen instanceof DerivedUnit){
71
                identifier = occurrenceService.getMostSignificantIdentifier(HibernateProxyHelper.deproxy(specimen, DerivedUnit.class));
72
            }
73
            if(fieldUnit!=null && fieldUnit.getGatheringEvent()!=null){
74
                country = fieldUnit.getGatheringEvent().getCountry();
75
            }
76
        }
77
        //description elements
78
        Set<DescriptionElementBase> elements = description.getElements();
79
        for (DescriptionElementBase descriptionElementBase : elements) {
80
            Feature feature = descriptionElementBase.getFeature();
81
            featureToElementMap.put(feature, descriptionElementBase);
82
        }
83
    }
84

    
85
    public QuantitativeData addQuantitativeData(Feature feature){
86
        QuantitativeData data = QuantitativeData.NewInstance(feature);
87
        description.addElement(data);
88
        featureToElementMap.put(feature, data);
89
        return data;
90
    }
91

    
92
    public CategoricalData addCategoricalData(Feature feature){
93
        CategoricalData data = CategoricalData.NewInstance(feature);
94
        description.addElement(data);
95
        featureToElementMap.put(feature, data);
96
        return data;
97
    }
98

    
99
    public SpecimenDescription getSpecimenDescription() {
100
        return description;
101
    }
102

    
103
    public Taxon getAssociatedTaxon() {
104
        return associatedTaxon;
105
    }
106

    
107
    public FieldUnit getFieldUnit() {
108
        return fieldUnit;
109
    }
110

    
111
    public String getIdentifier() {
112
        return identifier;
113
    }
114

    
115
    public NamedArea getCountry() {
116
        return country;
117
    }
118

    
119
    public Object getDataValueForFeature(Feature feature){
120
        DescriptionElementBase descriptionElementBase = featureToElementMap.get(feature);
121
        if(descriptionElementBase!=null && descriptionElementBase.isInstanceOf(CategoricalData.class)){
122
            CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
123
            return categoricalData.getStatesOnly();
124
        }
125
        return descriptionElementBase;
126
    }
127

    
128
    public void setDataValueForFeature(Feature feature, Object newValue){
129
        DescriptionElementBase descriptionElementBase = featureToElementMap.get(feature);
130
        if(descriptionElementBase!=null && descriptionElementBase.isInstanceOf(CategoricalData.class) && newValue instanceof Collection){
131
            CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
132
            categoricalData.setStateDataOnly(new ArrayList<>((Collection<State>) newValue));
133
        }
134
    }
135

    
136
}
(7-7/10)