2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.util
.converter
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.Collections
;
14 import java
.util
.Comparator
;
15 import java
.util
.HashMap
;
16 import java
.util
.LinkedHashMap
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
21 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
22 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignation
;
23 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
24 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
25 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
26 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationBase
;
27 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationStatusBase
;
28 import eu
.etaxonomy
.cdm
.vaadin
.model
.EntityReference
;
29 import eu
.etaxonomy
.cdm
.vaadin
.view
.registration
.RegistrationValidationException
;
32 * Converts a collection of TypeDesignations, which should belong to the
33 * same name of course, into a string representation.
35 * Order of TypeDesignations in the resulting string:
36 * Type, Holotype, Lectotype, Epitypes
37 * @author a.kohlbecker
41 public class TypeDesignationConverter
{
44 private final String separator
= ", ";
46 private Collection
<TypeDesignationBase
> typeDesignations
;
47 private Map
<TypeDesignationStatusBase
<?
>, Collection
<EntityReference
>> orderedStringsByType
;
48 private LinkedHashMap
<String
, Collection
<EntityReference
>> orderedRepresentations
= new LinkedHashMap
<>();
49 private EntityReference typifiedName
;
51 private String finalString
= null;
57 * @throws RegistrationValidationException
60 public TypeDesignationConverter(Collection
<TypeDesignationBase
> typeDesignations
) throws RegistrationValidationException
{
61 this.typeDesignations
= typeDesignations
;
62 orderedStringsByType
= new HashMap
<>();
63 typeDesignations
.forEach(td
-> putString(td
.getTypeStatus(), new EntityReference(td
.getId(), stringify(td
))));
64 orderedRepresentations
= buildOrderedRepresentations();
65 this.typifiedName
= findTypifiedName();
68 private LinkedHashMap
buildOrderedRepresentations(){
70 List
<TypeDesignationStatusBase
<?
>> keyList
= new LinkedList
<>(orderedStringsByType
.keySet());
72 Collections
.sort(keyList
, new Comparator
<TypeDesignationStatusBase
>() {
74 public int compare(TypeDesignationStatusBase o1
, TypeDesignationStatusBase o2
) {
75 // fix inverted order of cdm terms by -1*
76 return -1 * o1
.compareTo(o2
);
80 keyList
.forEach(key
-> orderedRepresentations
.put(getTypeDesignationStytusLabel(key
), orderedStringsByType
.get(key
)));
81 return orderedRepresentations
;
84 public TypeDesignationConverter
buildString(){
86 StringBuilder sb
= new StringBuilder();
88 if(getTypifiedNameCache() != null){
89 sb
.append(getTypifiedNameCache()).append(": ");
92 List
<String
> keyList
= new LinkedList
<>(orderedRepresentations
.keySet());
95 keyList
.forEach(key
-> {
96 sb
.append(key
).append(": ");
97 orderedRepresentations
.get(key
).forEach(isAndString
-> {
98 sb
.append(isAndString
.getLabel());
100 sb
.append(separator
);
105 finalString
= sb
.toString();
109 public Map
<String
, Collection
<EntityReference
>> getOrderedTypeDesignationRepresentations() {
110 return orderedRepresentations
;
114 * FIXME use the validation framework validators and to store the validation problems!!!
117 * @throws RegistrationValidationException
119 private EntityReference
findTypifiedName() throws RegistrationValidationException
{
121 List
<String
> problems
= new ArrayList
<>();
123 TaxonName typifiedName
= null;
125 for(TypeDesignationBase
<?
> typeDesignation
: typeDesignations
){
126 typeDesignation
.getTypifiedNames();
127 if(typeDesignation
.getTypifiedNames().isEmpty()){
129 //TODO instead throw RegistrationValidationException()
130 problems
.add("Missing typifiedName in " + typeDesignation
.toString());
133 if(typeDesignation
.getTypifiedNames().size() > 1){
134 //TODO instead throw RegistrationValidationException()
135 problems
.add("Multiple typifiedName in " + typeDesignation
.toString());
138 if(typifiedName
== null){
140 typifiedName
= typeDesignation
.getTypifiedNames().iterator().next();
143 TaxonName otherTypifiedName
= typeDesignation
.getTypifiedNames().iterator().next();
144 if(typifiedName
.getId() != otherTypifiedName
.getId()){
145 //TODO instead throw RegistrationValidationException()
146 problems
.add("Multiple typifiedName in " + typeDesignation
.toString());
151 if(!problems
.isEmpty()){
152 // FIXME use the validation framework
153 throw new RegistrationValidationException("Inconsistent type designations", problems
);
156 if(typifiedName
!= null){
157 return new EntityReference(typifiedName
.getId(), typifiedName
.getTitleCache());
164 * @return the title cache of the typifying name or <code>null</code>
166 public String
getTypifiedNameCache() {
167 if(typifiedName
!= null){
168 return typifiedName
.getLabel();
174 * @return the title cache of the typifying name or <code>null</code>
176 public EntityReference
getTypifiedName() {
186 protected String
getTypeDesignationStytusLabel(TypeDesignationStatusBase
<?
> key
) {
188 if(key
.equals( SpecimenTypeDesignationStatus
.TYPE())){
191 typeLable
= key
.getPreferredRepresentation(Language
.DEFAULT()).getLabel();
200 private String
stringify(TypeDesignationBase td
) {
202 if(td
instanceof NameTypeDesignation
){
203 return stringify((NameTypeDesignation
)td
);
205 return stringify((SpecimenTypeDesignation
)td
);
214 protected String
stringify(NameTypeDesignation td
) {
216 StringBuffer sb
= new StringBuffer();
218 if(td
.getTypeName() != null){
219 sb
.append(td
.getTypeName().getTitleCache());
221 if(td
.getCitation() != null){
222 sb
.append(" ").append(td
.getCitation().getTitleCache());
223 if(td
.getCitationMicroReference() != null){
224 sb
.append(":").append(td
.getCitationMicroReference());
227 if(td
.isNotDesignated()){
228 sb
.append(" not designated");
230 if(td
.isRejectedType()){
231 sb
.append(" rejected");
233 if(td
.isConservedType()){
234 sb
.append(" conserved");
236 return sb
.toString();
243 private String
stringify(SpecimenTypeDesignation td
) {
244 StringBuffer sb
= new StringBuffer();
246 if(td
.getTypeSpecimen() != null){
247 String nameTitleCache
= td
.getTypeSpecimen().getTitleCache();
248 if(getTypifiedNameCache() != null){
249 nameTitleCache
= nameTitleCache
.replace(getTypifiedNameCache(), "");
251 sb
.append(nameTitleCache
);
254 if(td
.getCitation() != null){
255 sb
.append(" ").append(td
.getCitation().getTitleCache());
256 if(td
.getCitationMicroReference() != null){
257 sb
.append(" :").append(td
.getCitationMicroReference());
260 if(td
.isNotDesignated()){
261 sb
.append(" not designated");
264 return sb
.toString();
267 private void putString(TypeDesignationStatusBase
<?
> status
, EntityReference idAndString
){
268 // the cdm orderd term bases are ordered invers, fixing this for here
270 status
= SpecimenTypeDesignationStatus
.TYPE();
272 if(!orderedStringsByType
.containsKey(status
)){
273 orderedStringsByType
.put(status
, new ArrayList
<EntityReference
>());
275 orderedStringsByType
.get(status
).add(idAndString
);
278 public String
print(){