Project

General

Profile

Download (5.48 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.description.WorkingSet;
26
import eu.etaxonomy.cdm.model.location.NamedArea;
27
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
28
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
29
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
import eu.etaxonomy.taxeditor.store.CdmStore;
35

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

    
46
    private SpecimenDescription description;
47

    
48
    private TaxonNode taxonNode;
49
    private FieldUnit fieldUnit;
50
    private String identifier;
51
    private NamedArea country;
52
    private Map<Feature, DescriptionElementBase> featureToElementMap = new HashMap<>();
53

    
54
    public RowWrapper(SpecimenDescription description, WorkingSet workingSet) {
55
        this.description = description;
56

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

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

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

    
102
    public SpecimenDescription getSpecimenDescription() {
103
        return description;
104
    }
105

    
106
    public TaxonNode getTaxonNode() {
107
        return taxonNode;
108
    }
109

    
110
    public FieldUnit getFieldUnit() {
111
        return fieldUnit;
112
    }
113

    
114
    public String getIdentifier() {
115
        return identifier;
116
    }
117

    
118
    public NamedArea getCountry() {
119
        return country;
120
    }
121

    
122
    public Object getDataValueForFeature(Feature feature){
123
        DescriptionElementBase descriptionElementBase = featureToElementMap.get(feature);
124
        return descriptionElementBase;
125
    }
126

    
127
    public void setDataValueForFeature(Feature feature, Object newValue){
128
        /* Only CategoricalData is handled here because for QuantitativeData the value
129
         * is set in the ModifyListener of the swt.Text in the CellEditor
130
         * for each StatisticalMeasure. So no need to set it again here.
131
         */
132
        DescriptionElementBase descriptionElementBase = featureToElementMap.get(feature);
133
        if(descriptionElementBase!=null && descriptionElementBase.isInstanceOf(CategoricalData.class) && newValue instanceof Collection){
134
            CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
135
            categoricalData.setStateDataOnly(new ArrayList<>((Collection<State>) newValue));
136
        }
137
    }
138

    
139
}
(8-8/11)