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.SpecimenTypeDesignation;
|
21
|
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
|
22
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
23
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
24
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
25
|
import eu.etaxonomy.cdm.ref.TypedEntityReference;
|
26
|
|
27
|
/**
|
28
|
* TypeDesignations which refer to the same FieldUnit (SpecimenTypeDesignation) or TaxonName
|
29
|
* (NameTypeDesignation) form a working set. The <code>TypeDesignationWorkingSet</code> internally
|
30
|
* works with EnityReferences to the actual TypeDesignations.
|
31
|
*
|
32
|
* The EntityReferences for TypeDesignations are grouped by the according TypeDesignationStatus.
|
33
|
* The TypeDesignationStatusBase keys can be ordered by the term order defined in the vocabulary.
|
34
|
*
|
35
|
* A workingset can be referenced by the <code>baseEntityReference</code>.
|
36
|
*
|
37
|
* @author a.kohlbecker
|
38
|
* @since Mar 10, 2017
|
39
|
*/
|
40
|
public class TypeDesignationWorkingSet {
|
41
|
|
42
|
public static final NullTypeDesignationStatus NULL_STATUS = NullTypeDesignationStatus.SINGLETON();
|
43
|
|
44
|
private String label = null;
|
45
|
|
46
|
//maybe removed in future as redundant with baseEntity
|
47
|
private TypedEntityReference<? extends VersionableEntity> baseEntityReference;
|
48
|
|
49
|
private LinkedHashMap<TypeDesignationStatusBase<?>,Collection<TypeDesignationDTO>> designationByStatusMap = new LinkedHashMap<>();
|
50
|
|
51
|
private VersionableEntity baseEntity;
|
52
|
|
53
|
public enum TypeDesignationWorkingSetType {
|
54
|
SPECIMEN_TYPE_DESIGNATION_WORKINGSET,
|
55
|
NAME_TYPE_DESIGNATION_WORKINGSET;
|
56
|
boolean isSpecimenType(){return this == SPECIMEN_TYPE_DESIGNATION_WORKINGSET;}
|
57
|
boolean isNameType(){return this == NAME_TYPE_DESIGNATION_WORKINGSET;}
|
58
|
}
|
59
|
|
60
|
// ********************************* CONSTRUCTOR **************************/
|
61
|
|
62
|
public TypeDesignationWorkingSet(VersionableEntity baseEntity) {
|
63
|
this.baseEntity = baseEntity;
|
64
|
this.baseEntityReference = TypeDesignationSetManager.makeEntityReference(baseEntity);
|
65
|
}
|
66
|
|
67
|
public TypeDesignationWorkingSet(VersionableEntity baseEntity, TypedEntityReference<? extends VersionableEntity> baseEntityRef) {
|
68
|
this.baseEntity = baseEntity;
|
69
|
this.baseEntityReference = baseEntityRef;
|
70
|
}
|
71
|
|
72
|
// ***********************************************************************/
|
73
|
|
74
|
public VersionableEntity getBaseEntity() {
|
75
|
return baseEntity;
|
76
|
}
|
77
|
|
78
|
public List<TypeDesignationDTO> getTypeDesignations() {
|
79
|
List<TypeDesignationDTO> typeDesignations = new ArrayList<>();
|
80
|
designationByStatusMap.values().forEach(typeDesignationDtos -> typeDesignationDtos.forEach(td -> typeDesignations.add(td)));
|
81
|
return typeDesignations;
|
82
|
}
|
83
|
|
84
|
public Set<TypeDesignationStatusBase<?>> keySet() {
|
85
|
return designationByStatusMap.keySet();
|
86
|
}
|
87
|
|
88
|
public Collection<TypeDesignationDTO> get(TypeDesignationStatusBase<?> typeStatus) {
|
89
|
return designationByStatusMap.get(typeStatus);
|
90
|
}
|
91
|
|
92
|
public void insert(TypeDesignationStatusBase<?> status,
|
93
|
TypeDesignationDTO<?> typeDesignationDto) {
|
94
|
|
95
|
if(status == null){
|
96
|
status = NULL_STATUS;
|
97
|
}
|
98
|
if(!designationByStatusMap.containsKey(status)){
|
99
|
designationByStatusMap.put(status, new ArrayList<>());
|
100
|
}
|
101
|
designationByStatusMap.get(status).add(typeDesignationDto);
|
102
|
}
|
103
|
|
104
|
public Collection<TypeDesignationDTO> put(TypeDesignationStatusBase<?> status,
|
105
|
Collection<TypeDesignationDTO> typeDesignationDtos) {
|
106
|
if(status == null){
|
107
|
status = NULL_STATUS;
|
108
|
}
|
109
|
return designationByStatusMap.put(status, typeDesignationDtos);
|
110
|
}
|
111
|
|
112
|
|
113
|
public String getLabel() {
|
114
|
return label;
|
115
|
}
|
116
|
public void setRepresentation(String representation){
|
117
|
this.label = representation;
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* A reference to the entity which is the common base entity for all TypeDesignations in this workingset.
|
122
|
* For a {@link SpecimenTypeDesignation} this is usually the {@link FieldUnit} if it is present. Otherwise it can also be
|
123
|
* a {@link DerivedUnit} or something else depending on the specific use case.
|
124
|
*
|
125
|
* @return the baseEntityReference
|
126
|
*/
|
127
|
public TypedEntityReference<? extends VersionableEntity> getBaseEntityReference() {
|
128
|
return baseEntityReference;
|
129
|
}
|
130
|
|
131
|
public boolean isSpecimenWorkingSet() {
|
132
|
return getWorkingsetType().isSpecimenType();
|
133
|
}
|
134
|
public boolean isNameWorkingSet() {
|
135
|
return getWorkingsetType().isNameType();
|
136
|
}
|
137
|
|
138
|
private boolean isSpecimenTypeDesigationWorkingSet() {
|
139
|
return SpecimenOrObservationBase.class.isAssignableFrom(baseEntity.getClass());
|
140
|
}
|
141
|
|
142
|
public TypeDesignationWorkingSetType getWorkingsetType() {
|
143
|
return isSpecimenTypeDesigationWorkingSet() ? TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET : TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET;
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* Uses the <code>comparator</code> to find the highest {@link TypeDesignationStatusBase} term and returns that.
|
148
|
*/
|
149
|
public TypeDesignationStatusBase<?> highestTypeStatus(Comparator<TypeDesignationStatusBase<?>> comparator) {
|
150
|
TypeDesignationStatusBase<?> highestTypeStatus = null;
|
151
|
for(TypeDesignationStatusBase<?> status : designationByStatusMap.keySet()) {
|
152
|
if(comparator.compare(status, highestTypeStatus) < 0){
|
153
|
highestTypeStatus = status;
|
154
|
}
|
155
|
}
|
156
|
return highestTypeStatus;
|
157
|
}
|
158
|
|
159
|
@Override
|
160
|
public String toString(){
|
161
|
if(label != null){
|
162
|
return label;
|
163
|
} else {
|
164
|
return super.toString();
|
165
|
}
|
166
|
}
|
167
|
|
168
|
}
|