Project

General

Profile

« Previous | Next » 

Revision 98768399

Added by Andreas Müller almost 2 years ago

cleanup and support for orderedBy and factory methods for TypeDesignationSetContainer

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/name/TypeDesignationSetContainer.java
17 17
import java.util.LinkedList;
18 18
import java.util.List;
19 19
import java.util.Map;
20
import java.util.Map.Entry;
21 20
import java.util.Optional;
22 21
import java.util.Set;
23 22
import java.util.UUID;
24 23

  
25 24
import eu.etaxonomy.cdm.api.service.exception.TypeDesignationSetException;
26
import eu.etaxonomy.cdm.api.service.name.TypeDesignationSet.TypeDesignationSetType;
27
import eu.etaxonomy.cdm.compare.name.NullTypeDesignationStatus;
28 25
import eu.etaxonomy.cdm.compare.name.TypeDesignationStatusComparator;
29 26
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30 27
import eu.etaxonomy.cdm.model.common.CdmBase;
......
66 63
        TYPE_NAME;
67 64
    }
68 65

  
69
    //not yet used
70
    enum ORDER_BY{
71
        TYPE_STATUS,
72
        BASE_ENTITY;
73
    }
74 66

  
75 67
    private NameTypeBaseEntityType nameTypeBaseEntityType = NameTypeBaseEntityType.NAME_TYPE_DESIGNATION;
76 68

  
......
78 70

  
79 71
    private TaxonName typifiedName;
80 72

  
81
    private ORDER_BY orderBy = ORDER_BY.TYPE_STATUS;
82

  
83
    /**
84
     * Sorts the base entities (TypedEntityReference) in the following order:
85
     *
86
     * 1. FieldUnits
87
     * 2. DerivedUnit (in case of missing FieldUnit we expect the base type to be DerivedUnit)
88
     * 3. NameType
89
     *
90
     * {@inheritDoc}
91
     */
92
    private Comparator<Entry<VersionableEntity,TypeDesignationSet>> entryComparator = (o1,o2)->{
93

  
94
         TypeDesignationSet ws1 = o1.getValue();
95
         TypeDesignationSet ws2 = o2.getValue();
96

  
97
         if (ws1.getWorkingsetType() != ws2.getWorkingsetType()){
98
             //first specimen types, then name types (very rare case anyway)
99
             return ws1.getWorkingsetType() == TypeDesignationSetType.NAME_TYPE_DESIGNATION_SET? 1:-1;
100
         }
101

  
102
         boolean hasStatus1 = !ws1.keySet().contains(null) && !ws1.keySet().contains(NullTypeDesignationStatus.SINGLETON());
103
         boolean hasStatus2 = !ws2.keySet().contains(null) && !ws2.keySet().contains(NullTypeDesignationStatus.SINGLETON());
104
         if (hasStatus1 != hasStatus2){
105
             //first without status as it is difficult to distinguish a non status from a "same" status record if the first record has a status and second has no status
106
             return hasStatus1? 1:-1;
107
         }
108

  
109
         //boolean hasStatus1 = ws1.getTypeDesignations(); //.stream().filter(td -> td.getSt);
110

  
111
         Class<?> type1 = o1.getKey().getClass();
112
         Class<?> type2 = o2.getKey().getClass();
113

  
114
         if(!type1.equals(type2)) {
115
             if(type1.equals(FieldUnit.class) || type2.equals(FieldUnit.class)){
116
                 // FieldUnits first
117
                 return type1.equals(FieldUnit.class) ? -1 : 1;
118
             } else {
119
                 // name types last (in case of missing FieldUnit we expect the base type to be DerivedUnit which comes into the middle)
120
                 return type2.equals(TaxonName.class) || type2.equals(NameTypeDesignation.class) ? -1 : 1;
121
             }
122
         } else {
123
             if (orderBy == ORDER_BY.TYPE_STATUS) {
124
                 @SuppressWarnings({ "unchecked", "rawtypes" })
125
                 Comparator<TypeDesignationStatusBase<?>> statusComparator = (Comparator)new TypeDesignationStatusComparator<>();
126
                 TypeDesignationStatusBase<?> status1 = ws1.highestTypeStatus(statusComparator);
127
                 TypeDesignationStatusBase<?> status2 = ws2.highestTypeStatus(statusComparator);
128
                 int comp = statusComparator.compare(status1, status2);
129
                 if (comp != 0) {
130
                     return comp;
131
                 }
132
             }
133

  
134
             String label1 = TypeDesignationSetFormatter.entityLabel(o1.getKey());
135
             String label2 = TypeDesignationSetFormatter.entityLabel(o2.getKey());
136
             return label1.compareTo(label2);
137
         }
138
     };
73
    private Comparator<TypeDesignationSet> typeDesignationSetComparator = TypeDesignationSetComparator.INSTANCE();
139 74

  
140 75
    /**
141 76
     * Groups the EntityReferences for each of the TypeDesignations by the according TypeDesignationStatus.
......
145 80

  
146 81
    private List<String> problems = new ArrayList<>();
147 82

  
83
// **************************** FACTORY ***************************************/
84

  
85
    public static TypeDesignationSetContainer NewDefaultInstance(@SuppressWarnings("rawtypes") Collection<TypeDesignationBase> typeDesignations)
86
            throws TypeDesignationSetException{
87
        return new TypeDesignationSetContainer(typeDesignations);
88
    }
89

  
90
    public static TypeDesignationSetContainer NewInstance(@SuppressWarnings("rawtypes") Collection<TypeDesignationBase> typeDesignations,
91
            TypeDesignationSetComparator.ORDER_BY orderBy)
92
            throws TypeDesignationSetException{
93
        TypeDesignationSetContainer result = new TypeDesignationSetContainer(typeDesignations);
94
        result.typeDesignationSetComparator = new TypeDesignationSetComparator(orderBy);
95
        return result;
96
    }
97

  
148 98
// **************************** CONSTRUCTOR ***********************************/
149 99

  
150
    public TypeDesignationSetContainer(@SuppressWarnings("rawtypes") Collection<TypeDesignationBase> typeDesignations)
100
    private TypeDesignationSetContainer(@SuppressWarnings("rawtypes") Collection<TypeDesignationBase> typeDesignations)
151 101
            throws TypeDesignationSetException{
152 102
    	this(typeDesignations, null);
153 103
    }
......
310 260
            Map<VersionableEntity,TypeDesignationSet> stringsByTypeByBaseEntity){
311 261

  
312 262
       // order the FieldUnit TypeName keys
313
       Set<Entry<VersionableEntity,TypeDesignationSet>> entrySet
314
               = stringsByTypeByBaseEntity.entrySet();
315
       LinkedList<Entry<VersionableEntity,TypeDesignationSet>> baseEntityKeyList
263
       Collection<TypeDesignationSet> entrySet
264
               = stringsByTypeByBaseEntity.values();
265
       LinkedList<TypeDesignationSet> baseEntityKeyList
316 266
               = new LinkedList<>(entrySet);
317
       Collections.sort(baseEntityKeyList, entryComparator);
267
       Collections.sort(baseEntityKeyList, typeDesignationSetComparator);
318 268

  
319 269
       // new LinkedHashMap for the ordered FieldUnitOrTypeName keys
320 270
       LinkedHashMap<VersionableEntity,TypeDesignationSet> stringsOrderedbyBaseEntityOrderdByType
321 271
           = new LinkedHashMap<>(stringsByTypeByBaseEntity.size());
322 272

  
323
       for(Entry<VersionableEntity,TypeDesignationSet> entry : baseEntityKeyList){
324
           VersionableEntity baseEntity = entry.getKey();
273
       for(TypeDesignationSet entry : baseEntityKeyList){
274
           VersionableEntity baseEntity = entry.getBaseEntity();
325 275
           TypeDesignationSet typeDesignationSet = stringsByTypeByBaseEntity.get(baseEntity);
326 276
           // order the TypeDesignationStatusBase keys
327 277
            List<TypeDesignationStatusBase<?>> keyList = new LinkedList<>(typeDesignationSet.keySet());

Also available in: Unified diff