Project

General

Profile

Download (5.57 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2020 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.name;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Comparator;
14
import java.util.LinkedHashMap;
15
import java.util.List;
16

    
17
import eu.etaxonomy.cdm.compare.name.NullTypeDesignationStatus;
18
import eu.etaxonomy.cdm.model.common.VersionableEntity;
19
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
20
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
21
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
22
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
23
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
24
import eu.etaxonomy.cdm.ref.TypedEntityReference;
25

    
26
/**
27
 * TypeDesignations which refer to the same FieldUnit (SpecimenTypeDesignation) or TaxonName
28
 * (NameTypeDesignation) form a working set. The <code>TypeDesignationWorkingSet</code> internally
29
 * works with EnityReferences to the actual TypeDesignations.
30
 *
31
 * The EntityReferences for TypeDesignations are grouped by the according TypeDesignationStatus.
32
 * The TypeDesignationStatusBase keys can be ordered by the term order defined in the vocabulary.
33
 *
34
 * A workingset can be referenced by the <code>baseEntityReference</code>.
35
 *
36
 * @author a.kohlbecker
37
 * @since Mar 10, 2017
38
 */
39
public class TypeDesignationWorkingSet
40
        extends LinkedHashMap<TypeDesignationStatusBase<?>,
41
                              Collection<TypeDesignationDTO>> {
42

    
43
    private static final long serialVersionUID = -1329007606500890729L;
44

    
45
    public static final NullTypeDesignationStatus NULL_STATUS = NullTypeDesignationStatus.SINGLETON();
46

    
47
    private String label = null;
48

    
49
    //maybe removed in future as redundant with baseEntity
50
    private TypedEntityReference<? extends VersionableEntity> baseEntityReference;
51

    
52
    private VersionableEntity baseEntity;
53

    
54
    public enum TypeDesignationWorkingSetType {
55
        SPECIMEN_TYPE_DESIGNATION_WORKINGSET,
56
        NAME_TYPE_DESIGNATION_WORKINGSET;
57
        boolean isSpecimenType(){return this == SPECIMEN_TYPE_DESIGNATION_WORKINGSET;}
58
        boolean isNameType(){return this == NAME_TYPE_DESIGNATION_WORKINGSET;}
59
    }
60

    
61
// ********************************* CONSTRUCTOR **************************/
62

    
63
    public TypeDesignationWorkingSet(VersionableEntity baseEntity) {
64
        this.baseEntity = baseEntity;
65
        this.baseEntityReference = TypeDesignationSetManager.makeEntityReference(baseEntity);
66
    }
67

    
68
    public TypeDesignationWorkingSet(VersionableEntity baseEntity, TypedEntityReference<? extends VersionableEntity> baseEntityRef) {
69
        this.baseEntity = baseEntity;
70
        this.baseEntityReference = baseEntityRef;
71
    }
72

    
73
// ***********************************************************************/
74

    
75
    public VersionableEntity getBaseEntity() {
76
        return baseEntity;
77
    }
78

    
79
    public List<TypeDesignationDTO> getTypeDesignations() {
80
        List<TypeDesignationDTO> typeDesignations = new ArrayList<>();
81
        this.values().forEach(typeDesignationReferences -> typeDesignationReferences.forEach(td -> typeDesignations.add(td)));
82
        return typeDesignations;
83
    }
84

    
85
    public void insert(TypeDesignationStatusBase<?> status,
86
            TypeDesignationDTO typeDesignationEntityReference) {
87

    
88
        if(status == null){
89
            status = NULL_STATUS;
90
        }
91
        if(!this.containsKey(status)){
92
            this.put(status, new ArrayList<>());
93
        }
94
        this.get(status).add(typeDesignationEntityReference);
95
    }
96

    
97
    public String getLabel() {
98
        return label;
99
    }
100
    public void setRepresentation(String representation){
101
        this.label = representation;
102
    }
103

    
104
    /**
105
     * A reference to the entity which is the common base entity for all TypeDesignations in this workingset.
106
     * For a {@link SpecimenTypeDesignation} this is usually the {@link FieldUnit} if it is present. Otherwise it can also be
107
     * a {@link DerivedUnit} or something else depending on the specific use case.
108
     *
109
     * @return the baseEntityReference
110
     */
111
    public TypedEntityReference<? extends VersionableEntity> getBaseEntityReference() {
112
        return baseEntityReference;
113
    }
114

    
115
    public boolean isSpecimenWorkingSet() {
116
        return getWorkingsetType().isSpecimenType();
117
    }
118
    public boolean isNameWorkingSet() {
119
        return getWorkingsetType().isNameType();
120
    }
121

    
122
    private boolean isSpecimenTypeDesigationWorkingSet() {
123
        return SpecimenOrObservationBase.class.isAssignableFrom(baseEntity.getClass());
124
    }
125

    
126
    public TypeDesignationWorkingSetType getWorkingsetType() {
127
        return isSpecimenTypeDesigationWorkingSet() ? TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET : TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET;
128
    }
129

    
130
    /**
131
     * Uses the <code>comparator</code> to find the highest {@link TypeDesignationStatusBase} term and returns that.
132
     */
133
    public TypeDesignationStatusBase<?> highestTypeStatus(Comparator<TypeDesignationStatusBase<?>> comparator) {
134
        TypeDesignationStatusBase<?> highestTypeStatus = null;
135
        for(TypeDesignationStatusBase<?> status : keySet()) {
136
            if(comparator.compare(status, highestTypeStatus) < 0){
137
                highestTypeStatus = status;
138
            }
139
        }
140
        return highestTypeStatus;
141
    }
142

    
143
    @Override
144
    public String toString(){
145
        if(label != null){
146
            return label;
147
        } else {
148
            return super.toString();
149
        }
150
    }
151

    
152
}
(4-4/4)