Project

General

Profile

Revision b18e16cd

IDb18e16cde1453786198162b12ab44e890a6bc00f
Parent 0e072f9c
Child 1ff11053

Added by Andreas Kohlbecker over 2 years ago

ref #6169 TypeDesignationSetManager can handle empty sets and can add typeDesignations

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationSetManager.java
9 9
package eu.etaxonomy.cdm.vaadin.util.converter;
10 10

  
11 11
import java.util.ArrayList;
12
import java.util.Arrays;
12 13
import java.util.Collection;
13 14
import java.util.Collections;
14 15
import java.util.Comparator;
......
24 25
import eu.etaxonomy.cdm.model.common.CdmBase;
25 26
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
26 27
import eu.etaxonomy.cdm.model.common.TermVocabulary;
27
import eu.etaxonomy.cdm.model.common.VersionableEntity;
28 28
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
29 29
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
30 30
import eu.etaxonomy.cdm.model.name.TaxonName;
......
84 84
    private List<String> probelms = new ArrayList<>();
85 85

  
86 86
    /**
87
     * @param containgEntity
87 88
     * @param taxonName
88 89
     * @throws RegistrationValidationException
89 90
     *
90 91
     */
91
    public TypeDesignationSetManager(CdmBase containgEntity, Collection<TypeDesignationBase> typeDesignations) throws RegistrationValidationException {
92
    public TypeDesignationSetManager(Collection<TypeDesignationBase> typeDesignations) throws RegistrationValidationException {
92 93
        this.typeDesignations = typeDesignations;
94
        this.typifiedName = findTypifiedName();
95
        mapAndSort();
96
    }
97

  
98
    /**
99
     * @param typifiedName2
100
     */
101
    public TypeDesignationSetManager(TaxonName typifiedName) {
102
        this.typeDesignations = new ArrayList<>();
103
        this.typifiedName = new EntityReference(typifiedName.getId(), typifiedName.getTitleCache());
104
    }
105

  
106
    /**
107
     * Add one or more TypeDesignations to the manager. This causes re-grouping and re-ordering
108
     * of all managed TypeDesignations.
109
     *
110
     * @param containgEntity
111
     * @param typeDesignations
112
     */
113
    public void addTypeDesigations(CdmBase containgEntity, TypeDesignationBase ... typeDesignations){
114
       this.typeDesignations.addAll(Arrays.asList(typeDesignations));
115
       mapAndSort();
116
    }
117

  
118
    /**
119
     * Groups and orders all managed TypeDesignations.
120
     *
121
     * @param containgEntity
122
     */
123
    protected void mapAndSort() {
124
        finalString = null;
93 125
        Map<TypedEntityReference, TypeDesignationWorkingSet> byBaseEntityByTypeStatus = new HashMap<>();
94
        typeDesignations.forEach(td -> mapTypeDesignation(containgEntity, byBaseEntityByTypeStatus, td));
126
        this.typeDesignations.forEach(td -> mapTypeDesignation(byBaseEntityByTypeStatus, td));
95 127
        orderedByTypesByBaseEntity = orderByTypeByBaseEntity(byBaseEntityByTypeStatus);
96
        this.typifiedName = findTypifiedName();
97 128
    }
98 129

  
99 130

  
100
    private void mapTypeDesignation(CdmBase containgEntity, Map<TypedEntityReference, TypeDesignationWorkingSet> byBaseEntityByTypeStatus,
131
    /**
132
     *
133
     * @param containgEntity
134
     * @param byBaseEntityByTypeStatus
135
     * @param td
136
     */
137
    private void mapTypeDesignation(Map<TypedEntityReference, TypeDesignationWorkingSet> byBaseEntityByTypeStatus,
101 138
            TypeDesignationBase<?> td){
102 139

  
103 140
        TypeDesignationStatusBase<?> status = td.getTypeStatus();
......
110 147

  
111 148
            TypeDesignationWorkingSet typedesignationWorkingSet;
112 149
            if(!byBaseEntityByTypeStatus.containsKey(baseEntityReference)){
113
                TypedEntityReference containigEntityReference = new TypedEntityReference(containgEntity.getClass(), containgEntity.getId(), containgEntity.toString());
114
                byBaseEntityByTypeStatus.put(baseEntityReference, new TypeDesignationWorkingSet(containigEntityReference, baseEntity, baseEntityReference));
150
                byBaseEntityByTypeStatus.put(baseEntityReference, new TypeDesignationWorkingSet(baseEntity, baseEntityReference));
115 151
            }
116 152

  
117 153
            typedesignationWorkingSet = byBaseEntityByTypeStatus.get(baseEntityReference);
......
214 250
                }
215 251
            });
216 252
            // new LinkedHashMap for the ordered TypeDesignationStatusBase keys
217
            TypeDesignationWorkingSet orderedStringsByOrderedTypes = new TypeDesignationWorkingSet(typeDesignationWorkingSet.getContainigEntityReference(),
253
            TypeDesignationWorkingSet orderedStringsByOrderedTypes = new TypeDesignationWorkingSet(
218 254
                    typeDesignationWorkingSet.getBaseEntity(),
219 255
                    baseEntityRef);
220 256
            orderedStringsByOrderedTypes.setWorkingSetId(typeDesignationWorkingSet.workingSetId); // preserve original workingSetId
......
247 283
            }
248 284

  
249 285
            int typeCount = 0;
250
            for(TypedEntityReference baseEntityRef : orderedByTypesByBaseEntity.keySet()) {
251
                StringBuilder sb = new StringBuilder();
252
                if(typeCount++ > 0){
253
                    sb.append(TYPE_SEPARATOR);
254
                }
255
                boolean isNameTypeDesignation = false;
256
                if(SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType())){
257
                    sb.append("Type: ");
258
                } else {
259
                    sb.append("NameType: ");
260
                    isNameTypeDesignation = true;
261
                }
262
                if(!baseEntityRef.getLabel().isEmpty()){
263
                    sb.append(baseEntityRef.getLabel()).append(" ");
264
                }
265
                TypeDesignationWorkingSet typeDesignationWorkingSet = orderedByTypesByBaseEntity.get(baseEntityRef);
266
                if(!isNameTypeDesignation ){
267
                    sb.append("(");
268
                }
269
                int typeStatusCount = 0;
270
                for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()) {
271
                    if(typeStatusCount++  > 0){
272
                        sb.append(TYPE_STATUS_SEPARATOR);
286
            if(orderedByTypesByBaseEntity != null){
287
                for(TypedEntityReference baseEntityRef : orderedByTypesByBaseEntity.keySet()) {
288
                    StringBuilder sb = new StringBuilder();
289
                    if(typeCount++ > 0){
290
                        sb.append(TYPE_SEPARATOR);
273 291
                    }
274
                    boolean isPlural = typeDesignationWorkingSet.get(typeStatus).size() > 1;
275
                    if(!typeStatus.equals(NULL_STATUS)) {
276
                        sb.append(typeStatus.getLabel());
277
                        if(isPlural){
278
                            sb.append("s: ");
279
                        } else {
280
                            sb.append(", ");
281
                        }
292
                    boolean isNameTypeDesignation = false;
293
                    if(SpecimenOrObservationBase.class.isAssignableFrom(baseEntityRef.getType())){
294
                        sb.append("Type: ");
295
                    } else {
296
                        sb.append("NameType: ");
297
                        isNameTypeDesignation = true;
298
                    }
299
                    if(!baseEntityRef.getLabel().isEmpty()){
300
                        sb.append(baseEntityRef.getLabel()).append(" ");
301
                    }
302
                    TypeDesignationWorkingSet typeDesignationWorkingSet = orderedByTypesByBaseEntity.get(baseEntityRef);
303
                    if(!isNameTypeDesignation ){
304
                        sb.append("(");
282 305
                    }
283
                    int typeDesignationCount = 0;
284
                    for(EntityReference typeDesignationEntityReference : typeDesignationWorkingSet.get(typeStatus)) {
285
                        if(typeDesignationCount++  > 0){
286
                            sb.append(TYPE_DESIGNATION_SEPARATOR);
306
                    int typeStatusCount = 0;
307
                    for(TypeDesignationStatusBase<?> typeStatus : typeDesignationWorkingSet.keySet()) {
308
                        if(typeStatusCount++  > 0){
309
                            sb.append(TYPE_STATUS_SEPARATOR);
310
                        }
311
                        boolean isPlural = typeDesignationWorkingSet.get(typeStatus).size() > 1;
312
                        if(!typeStatus.equals(NULL_STATUS)) {
313
                            sb.append(typeStatus.getLabel());
314
                            if(isPlural){
315
                                sb.append("s: ");
316
                            } else {
317
                                sb.append(", ");
318
                            }
319
                        }
320
                        int typeDesignationCount = 0;
321
                        for(EntityReference typeDesignationEntityReference : typeDesignationWorkingSet.get(typeStatus)) {
322
                            if(typeDesignationCount++  > 0){
323
                                sb.append(TYPE_DESIGNATION_SEPARATOR);
324
                            }
325
                            sb.append(typeDesignationEntityReference.getLabel());
287 326
                        }
288
                        sb.append(typeDesignationEntityReference.getLabel());
289 327
                    }
328
                    if(!isNameTypeDesignation ){
329
                        sb.append(")");
330
                    }
331
                    typeDesignationWorkingSet.setRepresentation(sb.toString());
332
                    finalString += typeDesignationWorkingSet.getRepresentation();
290 333
                }
291
                if(!isNameTypeDesignation ){
292
                    sb.append(")");
293
                }
294
                typeDesignationWorkingSet.setRepresentation(sb.toString());
295
                finalString += typeDesignationWorkingSet.getRepresentation();
296 334
            }
297

  
298 335
        }
299 336
        return this;
300 337
    }
......
516 553
    }
517 554

  
518 555
    public String print() {
519
        return finalString;
556
        return finalString.trim();
520 557
    }
521 558

  
522 559
    /**
......
529 566

  
530 567
        String workingSetRepresentation = null;
531 568

  
532
        TypedEntityReference<?> containigEntityReference;
533

  
534 569
        TypedEntityReference<IdentifiableEntity<?>> baseEntityReference;
535 570

  
536 571
        IdentifiableEntity<?> baseEntity;
......
542 577
        /**
543 578
         * @param baseEntityReference
544 579
         */
545
        public TypeDesignationWorkingSet(TypedEntityReference<? extends VersionableEntity> containigEntityReference, IdentifiableEntity<?> baseEntity, TypedEntityReference<IdentifiableEntity<?>> baseEntityReference) {
546
            this.containigEntityReference = containigEntityReference;
580
        public TypeDesignationWorkingSet(IdentifiableEntity<?> baseEntity, TypedEntityReference<IdentifiableEntity<?>> baseEntityReference) {
547 581
            this.baseEntity = baseEntity;
548 582
            this.baseEntityReference = baseEntityReference;
549 583
        }
......
609 643
            return baseEntityReference;
610 644
        }
611 645

  
612
        /**
613
         * A reference to the entity which contains the TypeDesignations bundled in this working set.
614
         * This can be for example a {@link TaxonName} or a {@link Registration} entity.
615
         *
616
         * @return the baseEntityReference
617
         */
618
        public TypedEntityReference getContainigEntityReference() {
619
            return containigEntityReference;
620
        }
621

  
622 646
        @Override
623 647
        public String toString(){
624 648
            if(workingSetRepresentation != null){
src/test/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationSetManagerTest.java
19 19
import java.util.Map;
20 20

  
21 21
import org.apache.log4j.Logger;
22
import org.junit.Before;
22 23
import org.junit.Test;
23 24

  
24 25
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
......
49 50
public class TypeDesignationSetManagerTest extends CdmVaadinBaseTest{
50 51

  
51 52

  
52
    @Test
53
    public void test1() throws RegistrationValidationException{
54

  
53
    private NameTypeDesignation ntd;
54
    private SpecimenTypeDesignation std_IT;
55
    private SpecimenTypeDesignation std_HT;
56
    private SpecimenTypeDesignation std_IT_2;
57
    private SpecimenTypeDesignation std_IT_3;
55 58

  
56
        TaxonName typifiedName = TaxonNameFactory.NewBacterialInstance(Rank.SPECIES());
57
        typifiedName.setTitleCache("Prionus coriatius L.", true);
59
    @Before
60
    public void init(){
58 61

  
59
        NameTypeDesignation ntd = NameTypeDesignation.NewInstance();
62
        ntd = NameTypeDesignation.NewInstance();
60 63
        ntd.setId(1);
61 64
        TaxonName typeName = TaxonNameFactory.NewBacterialInstance(Rank.SPECIES());
62 65
        typeName.setTitleCache("Prionus L.", true);
63 66
        ntd.setTypeName(typeName);
64 67
        Reference citation = ReferenceFactory.newGeneric();
65
        citation.setTitleCache("Species Platarum", true);
68
        citation.setTitleCache("Species Plantarum", true);
66 69
        ntd.setCitation(citation);
67
        typifiedName.addTypeDesignation(ntd, false);
68 70

  
69 71
        FieldUnit fu_1 = FieldUnit.NewInstance();
70 72
        fu_1.setId(1);
......
74 76
        fu_2.setId(2);
75 77
        fu_2.setTitleCache("Dreamland, near Kissingen, A.Kohlbecker 66211, 2017", true);
76 78

  
77
        SpecimenTypeDesignation std_HT = SpecimenTypeDesignation.NewInstance();
79
        std_HT = SpecimenTypeDesignation.NewInstance();
78 80
        std_HT.setId(1);
79 81
        DerivedUnit specimen_HT = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
80 82
        specimen_HT.setTitleCache("OHA", true);
......
84 86
        specimen_HT.getOriginals().add(fu_1);
85 87
        std_HT.setTypeSpecimen(specimen_HT);
86 88
        std_HT.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
87
        typifiedName.addTypeDesignation(std_HT, false);
88 89

  
89
        SpecimenTypeDesignation std_IT = SpecimenTypeDesignation.NewInstance();
90
        std_IT = SpecimenTypeDesignation.NewInstance();
90 91
        std_IT.setId(2);
91 92
        DerivedUnit specimen_IT = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
92 93
        specimen_IT.setTitleCache("BER", true);
......
95 96
        derivationEvent_2.addDerivative(specimen_IT);
96 97
        std_IT.setTypeSpecimen(specimen_IT);
97 98
        std_IT.setTypeStatus(SpecimenTypeDesignationStatus.ISOTYPE());
98
        typifiedName.addTypeDesignation(std_IT, false);
99 99

  
100
        SpecimenTypeDesignation std_IT_2 = SpecimenTypeDesignation.NewInstance();
100
        std_IT_2 = SpecimenTypeDesignation.NewInstance();
101 101
        std_IT_2.setId(3);
102 102
        DerivedUnit specimen_IT_2 = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
103 103
        specimen_IT_2.setTitleCache("KEW", true);
......
106 106
        derivationEvent_3.addDerivative(specimen_IT_2);
107 107
        std_IT_2.setTypeSpecimen(specimen_IT_2);
108 108
        std_IT_2.setTypeStatus(SpecimenTypeDesignationStatus.ISOTYPE());
109
        typifiedName.addTypeDesignation(std_IT_2, false);
110 109

  
111
        SpecimenTypeDesignation std_IT_3 = SpecimenTypeDesignation.NewInstance();
110
        std_IT_3 = SpecimenTypeDesignation.NewInstance();
112 111
        std_IT_3.setId(4);
113 112
        DerivedUnit specimen_IT_3 = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
114 113
        specimen_IT_3.setTitleCache("M", true);
115 114
        std_IT_3.setTypeSpecimen(specimen_IT_3);
116 115
        std_IT_3.setTypeStatus(SpecimenTypeDesignationStatus.ISOTYPE());
117
        typifiedName.addTypeDesignation(std_IT_3, false);
116
    }
117

  
118
    @Test
119
    public void test1() throws RegistrationValidationException{
118 120

  
119 121
        List<TypeDesignationBase> tds = new ArrayList<>();
120 122
        tds.add(ntd);
......
123 125
        tds.add(std_IT_2);
124 126
        tds.add(std_IT_3);
125 127

  
126
        TypeDesignationSetManager typeDesignationManager = new TypeDesignationSetManager(typifiedName, tds);
128
        TaxonName typifiedName = TaxonNameFactory.NewBacterialInstance(Rank.SPECIES());
129
        typifiedName.setTitleCache("Prionus coriatius L.", true);
130

  
131
        typifiedName.addTypeDesignation(ntd, false);
132
        typifiedName.addTypeDesignation(std_HT, false);
133
        typifiedName.addTypeDesignation(std_IT, false);
134
        typifiedName.addTypeDesignation(std_IT_2, false);
135
        typifiedName.addTypeDesignation(std_IT_3, false);
136

  
137
        TypeDesignationSetManager typeDesignationManager = new TypeDesignationSetManager(tds);
127 138
        String result = typeDesignationManager.buildString().print();
128
        System.err.println(result);
129 139

  
130 140
        Logger.getLogger(this.getClass()).debug(result);
131 141
        assertNotNull(result);
132 142
        assertEquals(
133
                "Prionus coriatius L. Type: Testland, near Bughausen, A.Kohlbecker 81989, 2017 (Holotype, OHA; Isotypes: BER, KEW); Type: (Isotype, M); NameType: Prionus L. Species Platarum"
143
                "Prionus coriatius L. Type: Testland, near Bughausen, A.Kohlbecker 81989, 2017 (Holotype, OHA; Isotypes: BER, KEW); Type: (Isotype, M); NameType: Prionus L. Species Plantarum"
134 144
                , result
135 145
                );
136 146

  
......
142 152
        assertEquals("Isotype", keyIt.next().getLabel());
143 153
    }
144 154

  
155
    @Test
156
    public void test2() throws RegistrationValidationException{
157

  
158
        TaxonName typifiedName = TaxonNameFactory.NewBacterialInstance(Rank.SPECIES());
159
        typifiedName.setTitleCache("Prionus coriatius L.", true);
160

  
161
        TypeDesignationSetManager typeDesignationManager = new TypeDesignationSetManager(typifiedName);
162
        String result = typeDesignationManager.buildString().print();
163
        Logger.getLogger(this.getClass()).debug(result);
164
        assertNotNull(result);
165
        assertEquals(
166
                "Prionus coriatius L."
167
                , result
168
                );
169

  
170
        typifiedName.addTypeDesignation(ntd, false);
171
        typeDesignationManager.addTypeDesigations(null, ntd);
172

  
173
        assertEquals(
174
                "Prionus coriatius L. NameType: Prionus L. Species Plantarum"
175
                , typeDesignationManager.buildString().print()
176
                );
177

  
178
        typifiedName.addTypeDesignation(std_HT, false);
179
        typeDesignationManager.addTypeDesigations(null, std_HT);
180

  
181
        assertEquals(
182
                "Prionus coriatius L. Type: Testland, near Bughausen, A.Kohlbecker 81989, 2017 (Holotype, OHA); NameType: Prionus L. Species Plantarum"
183
                , typeDesignationManager.buildString().print()
184
                );
185

  
186
    }
145 187
}
188

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)