Project

General

Profile

« Previous | Next » 

Revision a50e3be7

Added by Andreas Kohlbecker over 6 years ago

ref #6719 introducing TypeDesignationWorkingSet with string representation

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationConverter.java
58 58
    private Collection<TypeDesignationBase> typeDesignations;
59 59

  
60 60
    /**
61
     * Groups the EntityReferences for each of the TypeDesignatinos by the according TypeDesignationStatus.
61
     * Groups the EntityReferences for each of the TypeDesignations by the according TypeDesignationStatus.
62 62
     * The TypeDesignationStatusBase keys are already ordered by the term order defined in the vocabulary.
63 63
     */
64
    private LinkedHashMap<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> orderedByTypesByBaseEntity;
64
    private LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> orderedByTypesByBaseEntity;
65

  
65 66

  
66 67
    private EntityReference typifiedName;
67 68

  
......
74 75
     */
75 76
    public TypeDesignationConverter(Collection<TypeDesignationBase> typeDesignations) throws RegistrationValidationException {
76 77
        this.typeDesignations = typeDesignations;
77
        Map<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> byBaseEntityByTypeStatus = new HashMap<>();
78
        Map<TypedEntityReference, TypeDesignationWorkingSet> byBaseEntityByTypeStatus = new HashMap<>();
78 79
        typeDesignations.forEach(td -> mapTypeDesignation(byBaseEntityByTypeStatus, td));
79 80
        orderedByTypesByBaseEntity = orderByTypeByBaseEntity(byBaseEntityByTypeStatus);
80 81
        this.typifiedName = findTypifiedName();
81 82
    }
82 83

  
83 84

  
84
    private void mapTypeDesignation(Map<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> byBaseEntityByTypeStatus,
85
    private void mapTypeDesignation(Map<TypedEntityReference, TypeDesignationWorkingSet> byBaseEntityByTypeStatus,
85 86
            TypeDesignationBase<?> td){
86 87

  
87 88
        TypeDesignationStatusBase<?> status = td.getTypeStatus();
......
90 91

  
91 92
        EntityReference typeDesignationEntityReference = new EntityReference(td.getId(), stringify(td));
92 93

  
93
        Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> stringsOrderdByType;
94
        TypeDesignationWorkingSet typedesignationWorkingSet;
94 95
        if(!byBaseEntityByTypeStatus.containsKey(baseEntityReference)){
95
            byBaseEntityByTypeStatus.put(baseEntityReference, new LinkedHashMap<>());
96
            byBaseEntityByTypeStatus.put(baseEntityReference, new TypeDesignationWorkingSet());
96 97
        }
97 98

  
98
        stringsOrderdByType = byBaseEntityByTypeStatus.get(baseEntityReference);
99

  
100
        // the cdm ordered term bases are ordered inverse, fixing this for here
101
        if(status == null){
102
            status = SpecimenTypeDesignationStatus.TYPE();
103
        }
104
        if(!stringsOrderdByType.containsKey(status)){
105
            stringsOrderdByType.put(status, new ArrayList<EntityReference>());
106
        }
107
        stringsOrderdByType.get(status).add(typeDesignationEntityReference);
99
        typedesignationWorkingSet = byBaseEntityByTypeStatus.get(baseEntityReference);
100
        typedesignationWorkingSet.insert(status, typeDesignationEntityReference);
108 101
    }
109 102

  
110 103
    /**
......
140 133
    }
141 134

  
142 135

  
143
    private LinkedHashMap<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> orderByTypeByBaseEntity(
144
            Map<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> stringsByTypeByBaseEntity){
136
    private LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> orderByTypeByBaseEntity(
137
            Map<TypedEntityReference, TypeDesignationWorkingSet> stringsByTypeByBaseEntity){
145 138

  
146 139
       // order the FieldUnit TypeName keys
147 140
       List<TypedEntityReference> baseEntityKeyList = new LinkedList<>(stringsByTypeByBaseEntity.keySet());
......
155 148
        }});
156 149

  
157 150
       // new LinkedHashMap for the ordered FieldUnitOrTypeName keys
158
       LinkedHashMap<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> stringsOrderedbyBaseEntityOrderdByType = new LinkedHashMap<>(stringsByTypeByBaseEntity.size());
151
       LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> stringsOrderedbyBaseEntityOrderdByType = new LinkedHashMap<>(stringsByTypeByBaseEntity.size());
159 152

  
160 153
       for(TypedEntityReference baseEntityRef : baseEntityKeyList){
161 154

  
162
           Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> stringsByType = stringsByTypeByBaseEntity.get(baseEntityRef);
155
           TypeDesignationWorkingSet typeDesignationWorkingSet = stringsByTypeByBaseEntity.get(baseEntityRef);
163 156
           // order the TypeDesignationStatusBase keys
164
            List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(stringsByType.keySet());
157
            List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(typeDesignationWorkingSet.keySet());
165 158
            Collections.sort(keyList, new Comparator<TypeDesignationStatusBase>() {
166 159
                @Override
167 160
                public int compare(TypeDesignationStatusBase o1, TypeDesignationStatusBase o2) {
......
170 163
                }
171 164
            });
172 165
            // new LinkedHashMap for the ordered TypeDesignationStatusBase keys
173
            LinkedHashMap<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedStringsByOrderedTypes = new LinkedHashMap<>();
174
            keyList.forEach(key -> orderedStringsByOrderedTypes.put(key, stringsByType.get(key)));
166
            TypeDesignationWorkingSet orderedStringsByOrderedTypes = new TypeDesignationWorkingSet();
167
            keyList.forEach(key -> orderedStringsByOrderedTypes.put(key, typeDesignationWorkingSet.get(key)));
175 168
            stringsOrderedbyBaseEntityOrderdByType.put(baseEntityRef, orderedStringsByOrderedTypes);
176 169
       }
177 170

  
......
193 186
    public TypeDesignationConverter buildString(){
194 187

  
195 188
        if(finalString == null){
196
            StringBuilder sb = new StringBuilder();
197 189

  
190
            finalString = "";
198 191
            if(getTypifiedNameCache() != null){
199
                sb.append(getTypifiedNameCache()).append(" ");
192
                finalString += getTypifiedNameCache() + " ";
200 193
            }
201 194

  
202 195
            int typeCount = 0;
203 196
            for(TypedEntityReference baseEntityRef : orderedByTypesByBaseEntity.keySet()) {
197
                StringBuilder sb = new StringBuilder();
204 198
                if(typeCount++ > 0){
205 199
                    sb.append(TYPE_SEPARATOR);
206 200
                }
......
214 208
                if(!baseEntityRef.getLabel().isEmpty()){
215 209
                    sb.append(baseEntityRef.getLabel()).append(" ");
216 210
                }
217
                Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedRepresentations = orderedByTypesByBaseEntity.get(baseEntityRef);
211
                TypeDesignationWorkingSet typeDesignationWorkingSet = orderedByTypesByBaseEntity.get(baseEntityRef);
218 212
                if(!isNameTypeDesignation ){
219 213
                    sb.append("(");
220 214
                }
221 215
                int typeStatusCount = 0;
222
                for(TypeDesignationStatusBase<?> typeStatus : orderedRepresentations.keySet()) {
216
                for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()) {
223 217
                    if(typeStatusCount++  > 0){
224 218
                        sb.append(TYPE_STATUS_SEPARATOR);
225 219
                    }
226
                    boolean isPlural = orderedRepresentations.get(typeStatus).size() > 1;
220
                    boolean isPlural = typeDesignationWorkingSet.get(typeStatus).size() > 1;
227 221
                    sb.append(typeStatus.getLabel());
228 222
                    if(isPlural){
229 223
                        sb.append("s: ");
......
231 225
                        sb.append(", ");
232 226
                    }
233 227
                    int typeDesignationCount = 0;
234
                    for(EntityReference typeDesignationEntityReference : orderedRepresentations.get(typeStatus)) {
228
                    for(EntityReference typeDesignationEntityReference : typeDesignationWorkingSet.get(typeStatus)) {
235 229
                        if(typeDesignationCount++  > 0){
236 230
                            sb.append(TYPE_DESIGNATION_SEPARATOR);
237 231
                        }
238 232
                        sb.append(typeDesignationEntityReference.getLabel());
239
                    };
240
                };
233
                    }
234
                }
241 235
                if(!isNameTypeDesignation ){
242 236
                    sb.append(")");
243 237
                }
244
            };
238
                typeDesignationWorkingSet.setRepresentation(sb.toString());
239
                finalString += typeDesignationWorkingSet.getRepresentation();
240
            }
245 241

  
246
            finalString  = sb.toString();
247 242
        }
248 243
        return this;
249 244
    }
......
323 318
        return typeDesignations;
324 319
    }
325 320

  
326
    public LinkedHashMap<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> getOrderedTypeDesignations() {
321
    public LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> getOrderedTypeDesignations() {
327 322
        return orderedByTypesByBaseEntity;
328 323
    }
329 324

  
......
465 460
        return null;
466 461
    }
467 462

  
468

  
469 463
    public String print() {
470 464
        return finalString;
471 465
    }
472 466

  
473
/**
474
 *
475
 * @author a.kohlbecker
476
 * @since Jun 12, 2017
477
 *
478
 */
479
    public class FieldUnitOrTypeName {
467
    /**
468
     * Groups the EntityReferences for TypeDesignations by the according TypeDesignationStatus.
469
     * The TypeDesignationStatusBase keys can be ordered by the term order defined in the vocabulary.
470
     */
471
    public class TypeDesignationWorkingSet extends LinkedHashMap<TypeDesignationStatusBase<?>, Collection<EntityReference>> {
480 472

  
481
        FieldUnit fieldUnit = null;
473
        String workingSetRepresentation = null;
482 474

  
483
        TaxonName typeName = null;
484 475

  
485
        /**
486
         * @param fieldUnit
487
         * @param typeName
488
         */
489
        private FieldUnitOrTypeName(FieldUnit fieldUnit, TaxonName typeName) {
490
            this.fieldUnit = fieldUnit;
491
            this.typeName = typeName;
492
            if(fieldUnit != null && typeName != null){
493
                throw new RuntimeException("FieldUnitOrTypeName must not contain two non null fields");
494
            }
476
        private static final long serialVersionUID = -1329007606500890729L;
495 477

  
496
            if(fieldUnit == null && typeName == null){
497
                throw new NullPointerException("FieldUnitOrTypeName must not contain two null fields");
498
            }
499
        }
500

  
501
        /**
502
         * @return the fieldUnit
503
         */
504
        public FieldUnit getFieldUnit() {
505
            return fieldUnit;
478
        public List<EntityReference> getTypeDesignations() {
479
            List<EntityReference> typeDesignations = new ArrayList<>();
480
            this.values().forEach(typeDesignationReferences -> typeDesignationReferences.forEach(td -> typeDesignations.add(td)));
481
            return typeDesignations;
506 482
        }
507 483

  
508 484
        /**
509
         * @return the typeName
485
         * @param status
486
         * @param typeDesignationEntityReference
510 487
         */
511
        public TaxonName getTypeName() {
512
            return typeName;
513
        }
488
        public void insert(TypeDesignationStatusBase<?> status, EntityReference typeDesignationEntityReference) {
514 489

  
515
        /**
516
         * @return the fieldUnit
517
         */
518
        public boolean isFieldUnit() {
519
            return fieldUnit != null;
520
        }
490
            if(status == null){
491
                status = SpecimenTypeDesignationStatus.TYPE();
492
            }
493
            if(!containsKey(status)){
494
                put(status, new ArrayList<EntityReference>());
495
            }
496
            get(status).add(typeDesignationEntityReference);
521 497

  
522
        /**
523
         * @return the typeName
524
         */
525
        public boolean isTypeName() {
526
            return typeName != null;
527 498
        }
528 499

  
529
        public int getEntitiyId() {
530
            if(isFieldUnit()){
531
                return fieldUnit.getId();
532
            } else {
533
                return typeName.getId();
534
            }
500
        public String getRepresentation() {
501
            return workingSetRepresentation;
535 502
        }
536 503

  
537
        /**
538
         * @return
539
         */
540
        public String getTypeLabel() {
541
            if(isFieldUnit()){
542
                return "Type";
543
            } else {
544
                return "NameType";
545
            }
504
        public void setRepresentation(String representation){
505
            this.workingSetRepresentation = representation;
546 506
        }
547 507

  
548
        /**
549
         * @return
550
         */
551
        public String getTitleCache() {
552
            if(isFieldUnit()){
553
                return fieldUnit.getTitleCache();
508
        @Override
509
        public String toString(){
510
            if(workingSetRepresentation != null){
511
                return workingSetRepresentation;
554 512
            } else {
555
                return typeName.getTitleCache();
513
                return super.toString();
556 514
            }
557 515
        }
558 516

  
559
        public boolean matches(FieldUnit fieldUnit, TaxonName typeName){
560
            boolean fuMatch = this.fieldUnit == null && fieldUnit == null || this.fieldUnit.equals(fieldUnit);
561
            boolean nameMatch = this.typeName == null && typeName == null || this.typeName.equals(typeName);
562
            return fuMatch && nameMatch;
563
        }
564

  
565

  
566

  
567

  
568 517
    }
518

  
569 519
}
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/RegistrationDTO.java
13 13
import java.util.HashSet;
14 14
import java.util.LinkedHashMap;
15 15
import java.util.List;
16
import java.util.Map;
17 16
import java.util.Set;
18 17
import java.util.UUID;
19 18

  
......
25 24
import eu.etaxonomy.cdm.model.name.Registration;
26 25
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
27 26
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
28
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
29 27
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
30 28
import eu.etaxonomy.cdm.model.reference.IReference;
31 29
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
32 30
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
33 31
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationConverter;
32
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationConverter.TypeDesignationWorkingSet;
34 33

  
35 34
public class RegistrationDTO{
36 35

  
......
233 232
        return name;
234 233
    }
235 234

  
236
    public LinkedHashMap<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> getOrderdTypeDesignationEntitiyReferences() {
235
    public LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> getOrderdTypeDesignationEntitiyReferences() {
237 236
        return typeDesignationConverter != null ? typeDesignationConverter.getOrderedTypeDesignations() : null;
238 237
    }
239 238

  
src/test/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationConverterTest.java
38 38
import eu.etaxonomy.cdm.vaadin.CdmVaadinBaseTest;
39 39
import eu.etaxonomy.cdm.vaadin.model.EntityReference;
40 40
import eu.etaxonomy.cdm.vaadin.model.TypedEntityReference;
41
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationConverter.TypeDesignationWorkingSet;
41 42
import eu.etaxonomy.cdm.vaadin.view.registration.RegistrationValidationException;
42 43

  
43 44
/**
......
127 128

  
128 129
        Logger.getLogger(this.getClass()).debug(result);
129 130
        assertNotNull(result);
131
        assertEquals(
132
                "Prionus coriatius L. Type: Testland, near Bughausen, A.Kohlbecker 81989, 2017 (Holotype, OHA; Isotypes: BER, KEW); NameType: Unknown type category, Prionus L. Species Platarum; Type: (Isotype, M)"
133
                , result
134
                );
130 135

  
131
        LinkedHashMap<TypedEntityReference, Map<TypeDesignationStatusBase<?>, Collection<EntityReference>>> orderedTypeDesignations =
136
        LinkedHashMap<TypedEntityReference, TypeDesignationWorkingSet> orderedTypeDesignations =
132 137
                typeDesignationConverter.getOrderedTypeDesignations();
133 138
        Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> byStatusMap = orderedTypeDesignations.values().iterator().next();
134 139
        Iterator<TypeDesignationStatusBase<?>> keyIt = byStatusMap.keySet().iterator();

Also available in: Unified diff