Project

General

Profile

Download (5.71 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
import java.util.Set;
17

    
18
import eu.etaxonomy.cdm.compare.name.NullTypeDesignationStatus;
19
import eu.etaxonomy.cdm.model.common.VersionableEntity;
20
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
21
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
22

    
23
/**
24
 * TypeDesignations which refer to the same base entity (e.g. FieldUnit for SpecimenTypeDesignations
25
 * or TaxonName/NameTypeDesignation for NameTypeDesignation form a type designation set.
26
 * The <code>TypeDesignationSet</code> internally stores the entity base
27
 * and an ordered map that maps type status (TypeDesignationStatusBase) to the type designations
28
 * in the <code>TypeDesignationSet</code>.
29
 *
30
 * The TypeDesignationStatusBase keys can be ordered by the term order defined in the vocabulary.
31
 *
32
 * A typeDesignationSet can be referenced by the <code>baseEntity</code>.
33
 *
34
 * @author a.kohlbecker
35
 * @author a.mueller
36
 * @since Mar 10, 2017
37
 */
38
public class TypeDesignationSet {
39

    
40
    public static final NullTypeDesignationStatus NULL_STATUS = NullTypeDesignationStatus.SINGLETON();
41

    
42
    //TODO needed?
43
    private String label = null;
44

    
45
    private VersionableEntity baseEntity;
46

    
47
    private LinkedHashMap<TypeDesignationStatusBase<?>,Collection<TypeDesignationDTO>> designationByStatusMap = new LinkedHashMap<>();
48

    
49
    public enum TypeDesignationSetType {
50
        SPECIMEN_TYPE_DESIGNATION_SET,
51
        NAME_TYPE_DESIGNATION_SET;
52
        boolean isSpecimenType(){return this == SPECIMEN_TYPE_DESIGNATION_SET;}
53
        boolean isNameType(){return this == NAME_TYPE_DESIGNATION_SET;}
54
    }
55

    
56
// ********************************* CONSTRUCTOR **************************/
57

    
58
    public TypeDesignationSet(VersionableEntity baseEntity) {
59
        this.baseEntity = baseEntity;
60
    }
61

    
62
// ***********************************************************************/
63

    
64
    public VersionableEntity getBaseEntity() {
65
        return baseEntity;
66
    }
67

    
68
    public List<TypeDesignationDTO> getTypeDesignations() {
69
        List<TypeDesignationDTO> typeDesignations = new ArrayList<>();
70
        designationByStatusMap.values().forEach(typeDesignationDtos -> typeDesignationDtos.forEach(td -> typeDesignations.add(td)));
71
        return typeDesignations;
72
    }
73

    
74
    public Set<TypeDesignationStatusBase<?>> keySet() {
75
        return designationByStatusMap.keySet();
76
    }
77

    
78
    public Collection<TypeDesignationDTO> get(TypeDesignationStatusBase<?> typeStatus) {
79
        return designationByStatusMap.get(typeStatus);
80
    }
81

    
82
    public void insert(TypeDesignationStatusBase<?> status,
83
            TypeDesignationDTO<?> typeDesignationDto) {
84

    
85
        if(status == null){
86
            status = NULL_STATUS;
87
        }
88
        if(!designationByStatusMap.containsKey(status)){
89
            designationByStatusMap.put(status, new ArrayList<>());
90
        }
91
        designationByStatusMap.get(status).add(typeDesignationDto);
92
    }
93

    
94
    public Collection<TypeDesignationDTO> put(TypeDesignationStatusBase<?> status,
95
            Collection<TypeDesignationDTO> typeDesignationDtos) {
96
        if(status == null){
97
            status = NULL_STATUS;
98
        }
99
        return designationByStatusMap.put(status, typeDesignationDtos);
100
    }
101

    
102

    
103
    public String getLabel() {
104
        return label;
105
    }
106
    public void setRepresentation(String representation){
107
        this.label = representation;
108
    }
109

    
110
//TODO remove if not needed anymore
111
//    /**
112
//     * A reference to the entity which is the common base entity for all TypeDesignations in this workingset.
113
//     * For a {@link SpecimenTypeDesignation} this is usually the {@link FieldUnit} if it is present. Otherwise it can also be
114
//     * a {@link DerivedUnit} or something else depending on the specific use case.
115
//     *
116
//     * @return the baseEntityReference
117
//     */
118
//    public TypedEntityReference<? extends VersionableEntity> getBaseEntityReference() {
119
//        return baseEntityReference;
120
//    }
121

    
122
    public boolean isSpecimenWorkingSet() {
123
        return getWorkingsetType().isSpecimenType();
124
    }
125
    public boolean isNameWorkingSet() {
126
        return getWorkingsetType().isNameType();
127
    }
128

    
129
    private boolean isSpecimenTypeDesigationWorkingSet() {
130
        return SpecimenOrObservationBase.class.isAssignableFrom(baseEntity.getClass());
131
    }
132

    
133
    public TypeDesignationSetType getWorkingsetType() {
134
        return isSpecimenTypeDesigationWorkingSet() ? TypeDesignationSetType.SPECIMEN_TYPE_DESIGNATION_SET : TypeDesignationSetType.NAME_TYPE_DESIGNATION_SET;
135
    }
136

    
137
    /**
138
     * Uses the <code>comparator</code> to find the highest {@link TypeDesignationStatusBase} term and returns that.
139
     */
140
    public TypeDesignationStatusBase<?> highestTypeStatus(Comparator<TypeDesignationStatusBase<?>> comparator) {
141
        TypeDesignationStatusBase<?> highestTypeStatus = null;
142
        boolean isFirst = true;
143
        for(TypeDesignationStatusBase<?> status : designationByStatusMap.keySet()) {
144
            if(isFirst || comparator.compare(status, highestTypeStatus) < 0){
145
                highestTypeStatus = status;
146
                isFirst = false;
147
            }
148
        }
149
        return highestTypeStatus;
150
    }
151

    
152
// **************************** toString() ************************************
153

    
154
    @Override
155
    public String toString(){
156
        if(label != null){
157
            return label;
158
        } else {
159
            return designationByStatusMap.toString();
160
        }
161
    }
162
}
(2-2/4)