1
|
/**
|
2
|
* Copyright (C) 2020 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.persistence.dto;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.EnumSet;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import eu.etaxonomy.cdm.common.URI;
|
21
|
import eu.etaxonomy.cdm.model.common.CdmClass;
|
22
|
import eu.etaxonomy.cdm.model.description.Character;
|
23
|
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
|
24
|
import eu.etaxonomy.cdm.model.description.State;
|
25
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
|
26
|
import eu.etaxonomy.cdm.model.media.Media;
|
27
|
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
28
|
import eu.etaxonomy.cdm.model.term.Representation;
|
29
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
30
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
31
|
import eu.etaxonomy.cdm.model.term.TermType;
|
32
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
33
|
/**
|
34
|
* @author k.luther
|
35
|
* @since Oct 6, 2020
|
36
|
*/
|
37
|
public class CharacterDto extends FeatureDto {
|
38
|
|
39
|
private static final long serialVersionUID = 1743178749047550590L;
|
40
|
|
41
|
private TermNodeDto structure;
|
42
|
private TermDto structureModifier;
|
43
|
private TermNodeDto property;
|
44
|
private TermDto propertyModifier;
|
45
|
private TermNodeDto ratioTo;
|
46
|
|
47
|
public CharacterDto(UUID uuid, Set<Representation> representations, UUID partOfUuid, UUID kindOfUuid,
|
48
|
UUID vocabularyUuid, Integer orderIndex, String idInVocabulary, //Set<Representation> vocRepresentations,
|
49
|
boolean isAvailableForTaxon, boolean isAvailableForTaxonName, boolean isAvailableForOccurrence,
|
50
|
String titleCache, TermNodeDto structure, TermDto structureModifier, TermNodeDto property,
|
51
|
TermDto propertyModifier, TermNodeDto ratioTo, boolean isSupportCategoricalData, boolean isSupportsQuantitativeData, Set<TermVocabularyDto> supportedCategoricalEnumerations, Set<TermVocabularyDto> recommendedModifierEnumeration, Set<TermDto> recommendedMeasurementUnits, Set<TermDto> recommendedStatisticalMeasures) {
|
52
|
super(uuid, representations, partOfUuid, kindOfUuid, vocabularyUuid, orderIndex, idInVocabulary,
|
53
|
isAvailableForTaxon, isAvailableForTaxonName, isAvailableForOccurrence, titleCache, isSupportCategoricalData, isSupportsQuantitativeData, supportedCategoricalEnumerations, recommendedModifierEnumeration, recommendedMeasurementUnits, recommendedStatisticalMeasures);
|
54
|
this.structure = structure;
|
55
|
this.structureModifier = structureModifier;
|
56
|
this.property = property;
|
57
|
this.propertyModifier = propertyModifier;
|
58
|
this.setRatioTo(ratioTo);
|
59
|
this.setTermType(TermType.Character);
|
60
|
}
|
61
|
|
62
|
public static CharacterDto fromCharacter(Character character) {
|
63
|
TermVocabulary voc = character.getVocabulary();
|
64
|
|
65
|
Set<TermVocabularyDto> recommendedModifierDtos = new HashSet<>();
|
66
|
for (TermVocabulary<DefinedTerm> modVoc: character.getRecommendedModifierEnumeration()){
|
67
|
recommendedModifierDtos.add(TermVocabularyDto.fromVocabulary(modVoc));
|
68
|
}
|
69
|
|
70
|
Set<TermDto> recommendedStatisticalMeasuresDtos = new HashSet<>();
|
71
|
for (StatisticalMeasure term: character.getRecommendedStatisticalMeasures()){
|
72
|
recommendedStatisticalMeasuresDtos.add(TermDto.fromTerm(term));
|
73
|
}
|
74
|
|
75
|
Set<TermVocabularyDto> supportedCategoricalDtos = new HashSet<>();
|
76
|
for (TermVocabulary<State> catVoc: character.getSupportedCategoricalEnumerations()){
|
77
|
supportedCategoricalDtos.add(TermVocabularyDto.fromVocabulary(catVoc));
|
78
|
}
|
79
|
|
80
|
Set<TermDto> recommendedMeasurementUnitsDtos = new HashSet<>();
|
81
|
for (MeasurementUnit term: character.getRecommendedMeasurementUnits()){
|
82
|
recommendedMeasurementUnitsDtos.add(TermDto.fromTerm(term));
|
83
|
}
|
84
|
CharacterDto dto = new CharacterDto(character.getUuid(), character.getRepresentations(), character.getPartOf() != null? character.getPartOf().getUuid(): null, character.getKindOf() != null? character.getKindOf().getUuid(): null,
|
85
|
voc != null? voc.getUuid(): null, null, character.getIdInVocabulary(), character.isAvailableForTaxon(), character.isAvailableForTaxonName(), character.isAvailableForOccurrence(), character.getTitleCache(),
|
86
|
character.getStructure() !=null? TermNodeDto.fromNode(character.getStructure(), null): null, character.getStructureModifier() != null? TermDto.fromTerm(character.getStructureModifier()): null,
|
87
|
character.getProperty() != null? TermNodeDto.fromNode(character.getProperty(), null): null, character.getPropertyModifier() != null? TermDto.fromTerm(character.getPropertyModifier()): null, character.getRatioToStructure() != null? TermNodeDto.fromNode(character.getRatioToStructure(), null): null,
|
88
|
character.isSupportsCategoricalData(), character.isSupportsQuantitativeData(),supportedCategoricalDtos, recommendedModifierDtos, recommendedMeasurementUnitsDtos, recommendedStatisticalMeasuresDtos);
|
89
|
|
90
|
return dto;
|
91
|
}
|
92
|
|
93
|
public TermNodeDto getStructure() {
|
94
|
return structure;
|
95
|
}
|
96
|
public void setStructure(TermNodeDto structure) {
|
97
|
this.structure = structure;
|
98
|
}
|
99
|
|
100
|
public TermNodeDto getRatioTo() {
|
101
|
return ratioTo;
|
102
|
}
|
103
|
|
104
|
public void setRatioTo(TermNodeDto ratioTo) {
|
105
|
this.ratioTo = ratioTo;
|
106
|
}
|
107
|
|
108
|
public TermDto getStructureModifier() {
|
109
|
return structureModifier;
|
110
|
}
|
111
|
public void setStructureModifier(TermDto structureModifier) {
|
112
|
this.structureModifier = structureModifier;
|
113
|
}
|
114
|
|
115
|
public TermNodeDto getProperty() {
|
116
|
return property;
|
117
|
}
|
118
|
public void setProperty(TermNodeDto property) {
|
119
|
this.property = property;
|
120
|
}
|
121
|
|
122
|
public TermDto getPropertyModifier() {
|
123
|
return propertyModifier;
|
124
|
}
|
125
|
public void setPropertyModifier(TermDto propertyModifier) {
|
126
|
this.propertyModifier = propertyModifier;
|
127
|
}
|
128
|
|
129
|
|
130
|
public static String getTermDtoSelect(){
|
131
|
String[] result = createSqlParts("Character");
|
132
|
|
133
|
return result[0]+result[1]+result[2];
|
134
|
}
|
135
|
|
136
|
private static String[] createSqlParts(String fromTable) {
|
137
|
String sqlSelectString = ""
|
138
|
+ "select a.uuid, "
|
139
|
+ "r, "
|
140
|
+ "p.uuid, "
|
141
|
+ "k.uuid, "
|
142
|
+ "v.uuid, "
|
143
|
// + "a.orderIndex, "
|
144
|
+ "a.idInVocabulary, "
|
145
|
// + "voc_rep, "
|
146
|
+ "a.termType, "
|
147
|
+ "a.uri, "
|
148
|
+ "m, "
|
149
|
+ "a.availableFor, "
|
150
|
+ "a.titleCache, "
|
151
|
+ "a.supportedDataTypes, "
|
152
|
+ "recommendedModifierEnumeration, "
|
153
|
+ "recommendedStatisticalMeasures, "
|
154
|
+ "supportedCategoricalEnumerations, "
|
155
|
+ "recommendedMeasurementUnits, "
|
156
|
+ "a.property, "
|
157
|
//// + "a.propertyModifier ";
|
158
|
+ "a.structure, "
|
159
|
// + "a.structureModifier";
|
160
|
+ "ratioToStructure ";
|
161
|
|
162
|
String sqlFromString = " from "+fromTable+" as a ";
|
163
|
|
164
|
String sqlJoinString = "LEFT JOIN a.partOf as p "
|
165
|
+ "LEFT JOIN a.kindOf as k "
|
166
|
+ "LEFT JOIN a.media as m "
|
167
|
+ "LEFT JOIN a.representations AS r "
|
168
|
+ "LEFT JOIN a.vocabulary as v "
|
169
|
+ "LEFT JOIN v.representations as voc_rep "
|
170
|
+ "LEFT JOIN a.recommendedModifierEnumeration as recommendedModifierEnumeration "
|
171
|
+ "LEFT JOIN a.recommendedStatisticalMeasures as recommendedStatisticalMeasures "
|
172
|
+ "LEFT JOIN a.supportedCategoricalEnumerations as supportedCategoricalEnumerations "
|
173
|
+ "LEFT JOIN a.recommendedMeasurementUnits as recommendedMeasurementUnits "
|
174
|
+ "LEFT JOIN a.ratioToStructure as ratioToStructure"
|
175
|
;
|
176
|
|
177
|
String[] result = new String[3];
|
178
|
result[0] = sqlSelectString;
|
179
|
result[1] = sqlFromString;
|
180
|
result[2] = sqlJoinString;
|
181
|
return result;
|
182
|
}
|
183
|
|
184
|
public static List<TermDto> termDtoListFrom(List<Object[]> results) {
|
185
|
List<TermDto> dtos = new ArrayList<>(); // list to ensure order
|
186
|
// map to handle multiple representations/media/vocRepresentation because of LEFT JOIN
|
187
|
Map<UUID, TermDto> dtoMap = new HashMap<>(results.size());
|
188
|
for (Object[] elements : results) {
|
189
|
UUID uuid = (UUID)elements[0];
|
190
|
if(dtoMap.containsKey(uuid)){
|
191
|
// multiple results for one term -> multiple (voc) representation/media
|
192
|
if(elements[1]!=null){
|
193
|
dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
|
194
|
}
|
195
|
// if(elements[6]!=null){
|
196
|
// dtoMap.get(uuid).addVocRepresentation((Representation)elements[6]);
|
197
|
// }
|
198
|
if(elements[8]!=null){
|
199
|
dtoMap.get(uuid).addMedia(((Media) elements[8]).getUuid());
|
200
|
}
|
201
|
} else {
|
202
|
// term representation
|
203
|
Set<Representation> representations = new HashSet<>();
|
204
|
if(elements[1] instanceof Representation) {
|
205
|
representations = new HashSet<Representation>(1);
|
206
|
representations.add((Representation)elements[1]);
|
207
|
}
|
208
|
// term media
|
209
|
Set<UUID> mediaUuids = new HashSet<>();
|
210
|
if(elements[8] instanceof Media) {
|
211
|
mediaUuids.add(((Media) elements[8]).getUuid());
|
212
|
}
|
213
|
// voc representation
|
214
|
// Set<Representation> vocRepresentations = new HashSet<>();
|
215
|
// if(elements[6] instanceof Representation) {
|
216
|
// vocRepresentations = new HashSet<Representation>(7);
|
217
|
// vocRepresentations.add((Representation)elements[6]);
|
218
|
// }
|
219
|
boolean isAvailableForTaxon = false;
|
220
|
boolean isAvailableForTaxonName = false;
|
221
|
boolean isAvailableForOccurrence = false;
|
222
|
|
223
|
EnumSet<CdmClass> availableForString = (EnumSet<CdmClass>)elements[9];
|
224
|
|
225
|
if (availableForString.contains(CdmClass.TAXON)){
|
226
|
isAvailableForTaxon = true;
|
227
|
}
|
228
|
if (availableForString.contains(CdmClass.TAXON_NAME)){
|
229
|
isAvailableForTaxonName = true;
|
230
|
}
|
231
|
if (availableForString.contains(CdmClass.OCCURRENCE)){
|
232
|
isAvailableForOccurrence = true;
|
233
|
}
|
234
|
boolean isSupportsCategoricalData = false;
|
235
|
boolean isSupportsQuantitativeData = false;
|
236
|
|
237
|
EnumSet<CdmClass> supportsString = (EnumSet<CdmClass>)elements[11];
|
238
|
|
239
|
if (supportsString.contains(CdmClass.CATEGORICAL_DATA)){
|
240
|
isSupportsCategoricalData = true;
|
241
|
}
|
242
|
if (supportsString.contains(CdmClass.QUANTITATIVE_DATA)){
|
243
|
isSupportsQuantitativeData = true;
|
244
|
}
|
245
|
|
246
|
Object o = elements[12];
|
247
|
Set<TermVocabularyDto> recommendedModifierDtos = new HashSet<>();
|
248
|
if (o instanceof TermVocabulary){
|
249
|
recommendedModifierDtos.add(TermVocabularyDto.fromVocabulary((TermVocabulary)o));
|
250
|
}else if (o instanceof Set){
|
251
|
Set<TermVocabulary<DefinedTerm>> recommendedModifierEnumeration = (Set<TermVocabulary<DefinedTerm>>) o;
|
252
|
if (recommendedModifierEnumeration != null){
|
253
|
for (TermVocabulary<DefinedTerm> voc: recommendedModifierEnumeration){
|
254
|
recommendedModifierDtos.add(TermVocabularyDto.fromVocabulary(voc));
|
255
|
}
|
256
|
}
|
257
|
}
|
258
|
|
259
|
|
260
|
|
261
|
o = elements[13];
|
262
|
Set<TermDto> recommendedStatisticalMeasuresDtos = new HashSet<>();
|
263
|
if (o instanceof StatisticalMeasure){
|
264
|
recommendedStatisticalMeasuresDtos.add(TermDto.fromTerm((StatisticalMeasure)o));
|
265
|
}else if (o instanceof Set){
|
266
|
Set<StatisticalMeasure> recommendedStatisticalMeasures = (Set<StatisticalMeasure>) o;
|
267
|
if (recommendedStatisticalMeasures != null) {
|
268
|
for (StatisticalMeasure term: recommendedStatisticalMeasures){
|
269
|
recommendedStatisticalMeasuresDtos.add(TermDto.fromTerm(term));
|
270
|
}
|
271
|
}
|
272
|
}
|
273
|
o = elements[14];
|
274
|
Set<TermVocabularyDto> supportedCategoricalDtos = new HashSet<>();
|
275
|
if (o instanceof TermVocabulary){
|
276
|
supportedCategoricalDtos.add(TermVocabularyDto.fromVocabulary((TermVocabulary)o));
|
277
|
}else if (o instanceof Set){
|
278
|
Set<TermVocabulary> supportedCategoricalEnumerations = (Set<TermVocabulary>)o;
|
279
|
for (TermVocabulary<State> voc: supportedCategoricalEnumerations){
|
280
|
supportedCategoricalDtos.add(TermVocabularyDto.fromVocabulary(voc));
|
281
|
}
|
282
|
}
|
283
|
|
284
|
|
285
|
o = elements[15];
|
286
|
Set<TermDto> recommendedMeasurementUnitsDtos = new HashSet<>();
|
287
|
if (o instanceof MeasurementUnit){
|
288
|
recommendedMeasurementUnitsDtos.add(TermDto.fromTerm((MeasurementUnit)o));
|
289
|
}else if (o instanceof Set){
|
290
|
Set<MeasurementUnit> recommendedMeasurementUnits = (Set<MeasurementUnit>) elements[15];
|
291
|
for (MeasurementUnit term: recommendedMeasurementUnits){
|
292
|
recommendedMeasurementUnitsDtos.add(TermDto.fromTerm(term));
|
293
|
}
|
294
|
}
|
295
|
|
296
|
o = elements[16];
|
297
|
TermNodeDto prop = null;
|
298
|
if (o instanceof TermNode){
|
299
|
prop = TermNodeDto.fromNode((TermNode) o, TermTreeDto.fromTree((TermTree) ((TermNode)o).getGraph()));
|
300
|
}
|
301
|
|
302
|
o = elements[17];
|
303
|
TermNodeDto structure = null;
|
304
|
if (o instanceof TermNode){
|
305
|
structure = TermNodeDto.fromNode((TermNode) o, TermTreeDto.fromTree((TermTree) ((TermNode)o).getGraph()));
|
306
|
}
|
307
|
|
308
|
o = elements[9];
|
309
|
TermNodeDto ratioTo = null;
|
310
|
if (o instanceof TermNode){
|
311
|
ratioTo = TermNodeDto.fromNode((TermNode) o, TermTreeDto.fromTree((TermTree) ((TermNode)o).getGraph()));
|
312
|
}
|
313
|
|
314
|
|
315
|
TermDto termDto = new CharacterDto(
|
316
|
uuid,
|
317
|
representations,
|
318
|
(UUID)elements[2],
|
319
|
(UUID)elements[3],
|
320
|
(UUID)elements[4],
|
321
|
null,
|
322
|
(String)elements[5],
|
323
|
// vocRepresentations,
|
324
|
isAvailableForTaxon,
|
325
|
isAvailableForTaxonName,
|
326
|
isAvailableForOccurrence,
|
327
|
(String)elements[10],structure, null, prop, null, ratioTo, isSupportsCategoricalData,
|
328
|
// structure, structureModifier, prop, null, isSupportsCategoricalData,
|
329
|
isSupportsQuantitativeData,
|
330
|
supportedCategoricalDtos,
|
331
|
recommendedModifierDtos,
|
332
|
recommendedMeasurementUnitsDtos,
|
333
|
recommendedStatisticalMeasuresDtos)
|
334
|
;
|
335
|
termDto.setUri((URI)elements[7]);
|
336
|
termDto.setMedia(mediaUuids);
|
337
|
|
338
|
|
339
|
dtoMap.put(uuid, termDto);
|
340
|
dtos.add(termDto);
|
341
|
}
|
342
|
}
|
343
|
return dtos;
|
344
|
}
|
345
|
|
346
|
|
347
|
|
348
|
}
|
349
|
|