Project

General

Profile

« Previous | Next » 

Revision 4e0052e3

Added by Andreas Kohlbecker almost 7 years ago

ref #6719 refactoring and better test

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationConverter.java
44 44
    private final String separator = ", ";
45 45

  
46 46
    private Collection<TypeDesignationBase> typeDesignations;
47
    private Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedStringsByType;
47

  
48
    /**
49
     * Groups the EntityReferences for each of the TypeDesignatinos by the according TypeDesignationStatus.
50
     * The TypeDesignationStatusBase keys are already ordered by the term order defined in the vocabulary.
51
     */
52
    private LinkedHashMap<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedStringsByOrderedTypes = new LinkedHashMap<>();
53

  
54
    /**
55
     * Groups the EntityReferences for each of the TypeDesignatinos by the label representations of the
56
     * according TypeDesignationStatus. The labels are ordered by the term order defined in the vocabulary.
57
     */
48 58
    private LinkedHashMap<String, Collection<EntityReference>> orderedRepresentations = new LinkedHashMap<>();
59

  
49 60
    private EntityReference typifiedName;
50 61

  
51 62
    private String finalString = null;
......
59 70
     */
60 71
    public TypeDesignationConverter(Collection<TypeDesignationBase> typeDesignations) throws RegistrationValidationException {
61 72
        this.typeDesignations = typeDesignations;
62
        orderedStringsByType = new HashMap<>();
63
        typeDesignations.forEach(td -> putString(td.getTypeStatus(), new EntityReference(td.getId(), stringify(td))));
73
        Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedStringsByType = new HashMap<>();
74
        typeDesignations.forEach(td -> putString(orderedStringsByType, td.getTypeStatus(), new EntityReference(td.getId(), stringify(td))));
75
        orderedStringsByOrderedTypes = orderedByType(orderedStringsByType);
64 76
        orderedRepresentations = buildOrderedRepresentations();
65 77
        this.typifiedName = findTypifiedName();
66 78
    }
67 79

  
68
    private LinkedHashMap buildOrderedRepresentations(){
80
    private LinkedHashMap<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedByType(Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedStringsByType){
69 81

  
70 82
        // 1. order by SpecimenType, NameType
71 83

  
......
83 95
        });
84 96
        // NameTypes.........
85 97

  
86
        keyList.forEach(key -> orderedRepresentations.put(getTypeDesignationStytusLabel(key), orderedStringsByType.get(key)));
98
        keyList.forEach(key -> orderedStringsByOrderedTypes.put(key, orderedStringsByType.get(key)));
99
        return orderedStringsByOrderedTypes;
100
    }
101

  
102
    private LinkedHashMap<String, Collection<EntityReference>> buildOrderedRepresentations(){
103

  
104
        orderedStringsByOrderedTypes.keySet().forEach(
105
                key -> orderedRepresentations.put(
106
                        getTypeDesignationStytusLabel(key),
107
                        orderedStringsByOrderedTypes.get(key))
108
                );
87 109
        return orderedRepresentations;
88 110
    }
89 111

  
......
270 292
        return sb.toString();
271 293
    }
272 294

  
273
    private void putString(TypeDesignationStatusBase<?> status, EntityReference idAndString){
295
    private void putString(Map<TypeDesignationStatusBase<?>, Collection<EntityReference>> orderedStringsByType, TypeDesignationStatusBase<?> status, EntityReference idAndString){
274 296
        // the cdm orderd term bases are ordered invers, fixing this for here
275 297
        if(status == null){
276 298
            status = SpecimenTypeDesignationStatus.TYPE();
src/test/java/eu/etaxonomy/cdm/vaadin/util/converter/TypeDesignationConverterTest.java
8 8
*/
9 9
package eu.etaxonomy.cdm.vaadin.util.converter;
10 10

  
11
import static org.junit.Assert.assertEquals;
11 12
import static org.junit.Assert.assertNotNull;
12 13

  
13 14
import java.util.ArrayList;
15
import java.util.Iterator;
14 16
import java.util.List;
15 17

  
16 18
import org.apache.log4j.Logger;
......
54 56

  
55 57
        typifiedName.addTypeDesignation(ntd, false);
56 58

  
57
        SpecimenTypeDesignation std = SpecimenTypeDesignation.NewInstance();
58
        DerivedUnit specimen = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
59
        specimen.setTitleCache("OHA", true);
60
        std.setTypeSpecimen(specimen);
61
        std.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
59
        SpecimenTypeDesignation std_HT = SpecimenTypeDesignation.NewInstance();
60
        DerivedUnit specimen_HT = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
61
        specimen_HT.setTitleCache("OHA", true);
62
        std_HT.setTypeSpecimen(specimen_HT);
63
        std_HT.setTypeStatus(SpecimenTypeDesignationStatus.HOLOTYPE());
64
        typifiedName.addTypeDesignation(std_HT, false);
62 65

  
63
        typifiedName.addTypeDesignation(std, false);
66
        SpecimenTypeDesignation std_IT = SpecimenTypeDesignation.NewInstance();
67
        DerivedUnit specimen_IT = DerivedUnit.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
68
        specimen_IT.setTitleCache("BER", true);
69
        std_IT.setTypeSpecimen(specimen_IT);
70
        std_IT.setTypeStatus(SpecimenTypeDesignationStatus.ISOTYPE());
71
        typifiedName.addTypeDesignation(std_IT, false);
64 72

  
65 73
        List<TypeDesignationBase> tds = new ArrayList<>();
66 74
        tds.add(ntd);
67
        tds.add(std);
75
        tds.add(std_IT);
76
        tds.add(std_HT);
68 77

  
69
        String result = new TypeDesignationConverter(tds).buildString().print();
78
        TypeDesignationConverter typeDesignationConverter = new TypeDesignationConverter(tds);
79
        String result = typeDesignationConverter.buildString().print();
70 80
        Logger.getLogger(this.getClass()).debug(result);
71 81
        assertNotNull(result);
82
        Iterator<String> keyIt = typeDesignationConverter.getOrderedTypeDesignationRepresentations().keySet().iterator();
83
        assertEquals("Holotype", keyIt.next());
84
        assertEquals("Isotype", keyIt.next());
72 85
    }
73 86

  
74 87
}

Also available in: Unified diff