1
|
/**
|
2
|
* Copyright (C) 2015 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.Collection;
|
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.hibernate.HibernateProxyHelper;
|
22
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
23
|
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
|
24
|
import eu.etaxonomy.cdm.model.media.Media;
|
25
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
26
|
import eu.etaxonomy.cdm.model.term.OrderedTermBase;
|
27
|
import eu.etaxonomy.cdm.model.term.Representation;
|
28
|
import eu.etaxonomy.cdm.model.term.TermType;
|
29
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
30
|
|
31
|
/**
|
32
|
* @author andreas
|
33
|
* @since Mar 25, 2015
|
34
|
*/
|
35
|
public class TermDto extends AbstractTermDto{
|
36
|
|
37
|
private static final long serialVersionUID = 5627308906985438034L;
|
38
|
|
39
|
private UUID kindOfUuid = null;
|
40
|
private UUID partOfUuid = null;
|
41
|
private UUID vocabularyUuid = null;
|
42
|
private TermDto kindOfDto = null;
|
43
|
private TermDto partOfDto = null;
|
44
|
private TermVocabularyDto vocabularyDto = null;
|
45
|
private Integer orderIndex = null;
|
46
|
private String idInVocabulary = null;
|
47
|
private Collection<TermDto> includes;
|
48
|
private Collection<TermDto> generalizationOf;
|
49
|
// this should be handled in vocabularyDto
|
50
|
// private Set<Representation> vocRepresentations = null;
|
51
|
// private String vocRepresentation_L10n = null;
|
52
|
// private String vocRepresentation_L10n_abbreviatedLabel = null;
|
53
|
private Collection<UUID> media = null;
|
54
|
private NamedAreaLevel level = null;
|
55
|
|
56
|
// private TermDto(UUID uuid, Set<Representation> representations, TermType termType, UUID partOfUuid, UUID kindOfUuid,
|
57
|
// UUID vocabularyUuid, Integer orderIndex, String idInVocabulary, String titleCache) {
|
58
|
// this(uuid, representations, termType, partOfUuid, kindOfUuid, vocabularyUuid, orderIndex, idInVocabulary, titleCache);
|
59
|
// }
|
60
|
|
61
|
public TermDto(UUID uuid, Set<Representation> representations, TermType termType, UUID partOfUuid, UUID kindOfUuid,
|
62
|
UUID vocabularyUuid, Integer orderIndex, String idInVocabulary, String titleCache) {
|
63
|
super(uuid, representations, titleCache);
|
64
|
this.partOfUuid = partOfUuid;
|
65
|
this.kindOfUuid = kindOfUuid;
|
66
|
this.vocabularyUuid = vocabularyUuid;
|
67
|
this.orderIndex = orderIndex;
|
68
|
this.idInVocabulary = idInVocabulary;
|
69
|
// this.vocabularyDto = vocDto;
|
70
|
setTermType(termType);
|
71
|
}
|
72
|
|
73
|
static public TermDto fromTerm(DefinedTermBase<?> term) {
|
74
|
return fromTerm(term, null, false);
|
75
|
}
|
76
|
|
77
|
static public TermDto fromTerm(DefinedTermBase<?> term, boolean initializeToTop) {
|
78
|
return fromTerm(term, null, initializeToTop);
|
79
|
}
|
80
|
|
81
|
static public TermDto fromTerm(DefinedTermBase<?> term, Set<Representation> representations) {
|
82
|
return fromTerm(term, representations, false);
|
83
|
}
|
84
|
|
85
|
static public TermDto fromTerm(DefinedTermBase<?> term, Set<Representation> representations, boolean initializeToTop) {
|
86
|
if (term == null){
|
87
|
return null;
|
88
|
}
|
89
|
DefinedTermBase<?> partOf = term.getPartOf();
|
90
|
DefinedTermBase<?> kindOf = term.getKindOf();
|
91
|
TermVocabulary<?> vocabulary = term.getVocabulary();
|
92
|
if (representations == null){
|
93
|
term = HibernateProxyHelper.deproxy(term, DefinedTermBase.class);
|
94
|
representations = term.getRepresentations();
|
95
|
}
|
96
|
|
97
|
TermDto dto = new TermDto(
|
98
|
term.getUuid(),
|
99
|
representations!=null?representations:term.getRepresentations(),
|
100
|
term.getTermType(),
|
101
|
(partOf!=null?partOf.getUuid():null),
|
102
|
(kindOf!=null?kindOf.getUuid():null),
|
103
|
(vocabulary!=null?vocabulary.getUuid():null),
|
104
|
(term instanceof OrderedTermBase)?((OrderedTermBase<?>) term).getOrderIndex():null,
|
105
|
term.getIdInVocabulary(),
|
106
|
term.getTitleCache());
|
107
|
dto.setUri(term.getUri());
|
108
|
if(initializeToTop){
|
109
|
if(partOf!=null){
|
110
|
dto.setPartOfDto(fromTerm(partOf, initializeToTop));
|
111
|
}
|
112
|
if(kindOf!=null){
|
113
|
dto.setKindOfDto(fromTerm(kindOf, initializeToTop));
|
114
|
}
|
115
|
}
|
116
|
if (vocabulary != null){
|
117
|
dto.setVocabularyDto(new TermVocabularyDto(vocabulary.getUuid(), vocabulary.getRepresentations(), term.getTermType(), vocabulary.getTitleCache(), vocabulary.isAllowDuplicates(), vocabulary.isOrderRelevant(), vocabulary.isFlat()));
|
118
|
}
|
119
|
if(term.getMedia()!=null){
|
120
|
Collection<UUID> mediaUuids = new HashSet<>();
|
121
|
Set<Media> media = term.getMedia();
|
122
|
|
123
|
for (Media medium : media) {
|
124
|
mediaUuids.add(medium.getUuid());
|
125
|
}
|
126
|
dto.setMedia(mediaUuids);
|
127
|
|
128
|
}
|
129
|
if (term instanceof NamedArea && ((NamedArea)term).getLevel() != null){
|
130
|
dto.setLevel(((NamedArea)term).getLevel());
|
131
|
}
|
132
|
return dto;
|
133
|
}
|
134
|
|
135
|
@Override
|
136
|
public void localize(ITermRepresentation_L10n representation_L10n) {
|
137
|
// if(vocRepresentations!=null){
|
138
|
// representation_L10n.localize(vocRepresentations);
|
139
|
// if (representation_L10n.getLabel() != null) {
|
140
|
// setVocRepresentation_L10n(representation_L10n.getLabel());
|
141
|
// }
|
142
|
// if (representation_L10n.getAbbreviatedLabel() != null) {
|
143
|
// setVocRepresentation_L10n_abbreviatedLabel(representation_L10n.getAbbreviatedLabel());
|
144
|
// }
|
145
|
// }
|
146
|
super.localize(representation_L10n);
|
147
|
}
|
148
|
|
149
|
// public void setVocRepresentation_L10n(String vocRepresentation_L10n) {
|
150
|
// this.vocRepresentation_L10n = vocRepresentation_L10n;
|
151
|
// }
|
152
|
//
|
153
|
public String getVocRepresentation_L10n() {
|
154
|
return vocabularyDto == null ? null : vocabularyDto.getRepresentation_L10n();
|
155
|
}
|
156
|
//
|
157
|
// public void setVocRepresentation_L10n_abbreviatedLabel(String vocRepresentation_L10n_abbreviatedLabel) {
|
158
|
// this.vocRepresentation_L10n_abbreviatedLabel = vocRepresentation_L10n_abbreviatedLabel;
|
159
|
// }
|
160
|
//
|
161
|
public String getVocRepresentation_L10n_abbreviatedLabel() {
|
162
|
return vocabularyDto == null ? null : vocabularyDto.getRepresentation_L10n_abbreviatedLabel();
|
163
|
}
|
164
|
//
|
165
|
// protected void addVocRepresentation(Representation vocRepresentation){
|
166
|
// this.vocRepresentations.add(vocRepresentation);
|
167
|
// }
|
168
|
|
169
|
public void setPartOfDto(TermDto partOfDto) {
|
170
|
this.partOfDto = partOfDto;
|
171
|
}
|
172
|
|
173
|
public TermDto getPartOfDto() {
|
174
|
return partOfDto;
|
175
|
}
|
176
|
|
177
|
public void setKindOfDto(TermDto kindOfDto) {
|
178
|
this.kindOfDto = kindOfDto;
|
179
|
}
|
180
|
|
181
|
public TermDto getKindOfDto() {
|
182
|
return kindOfDto;
|
183
|
}
|
184
|
|
185
|
public void setVocabularyDto(TermVocabularyDto vocabularyDto) {
|
186
|
this.vocabularyDto = vocabularyDto;
|
187
|
}
|
188
|
|
189
|
public TermVocabularyDto getVocabularyDto() {
|
190
|
return vocabularyDto;
|
191
|
}
|
192
|
|
193
|
public UUID getVocabularyUuid() {
|
194
|
return vocabularyUuid;
|
195
|
}
|
196
|
|
197
|
public void setVocabularyUuid(UUID vocabularyUuid) {
|
198
|
this.vocabularyUuid = vocabularyUuid;
|
199
|
}
|
200
|
|
201
|
public UUID getPartOfUuid() {
|
202
|
return partOfUuid;
|
203
|
}
|
204
|
|
205
|
public UUID getKindOfUuid() {
|
206
|
return kindOfUuid;
|
207
|
}
|
208
|
|
209
|
public Integer getOrderIndex() {
|
210
|
return orderIndex;
|
211
|
}
|
212
|
|
213
|
public void setOrderIndex(Integer orderIndex) {
|
214
|
this.orderIndex = orderIndex;
|
215
|
}
|
216
|
|
217
|
public String getIdInVocabulary() {
|
218
|
return idInVocabulary;
|
219
|
}
|
220
|
|
221
|
public void setIdInVocabulary(String idInVocabulary) {
|
222
|
this.idInVocabulary = idInVocabulary;
|
223
|
}
|
224
|
|
225
|
public Collection<TermDto> getIncludes() {
|
226
|
return includes;
|
227
|
}
|
228
|
|
229
|
public void setIncludes(Collection<TermDto> includes) {
|
230
|
this.includes = includes;
|
231
|
}
|
232
|
|
233
|
public Collection<TermDto> getGeneralizationOf() {
|
234
|
return generalizationOf;
|
235
|
}
|
236
|
|
237
|
public void setGeneralizationOf(Collection<TermDto> generalizationOf) {
|
238
|
this.generalizationOf = generalizationOf;
|
239
|
}
|
240
|
|
241
|
public static String getTermDtoSelect(){
|
242
|
return getTermDtoSelect("DefinedTermBase");
|
243
|
}
|
244
|
|
245
|
|
246
|
public Collection<UUID> getMedia() {
|
247
|
return media;
|
248
|
}
|
249
|
|
250
|
public void setMedia(Collection<UUID> media) {
|
251
|
this.media = media;
|
252
|
}
|
253
|
|
254
|
protected void addMedia(UUID mediaUuid){
|
255
|
this.media.add(mediaUuid);
|
256
|
}
|
257
|
|
258
|
public void setLevel(NamedAreaLevel level) {
|
259
|
this.level = level;
|
260
|
}
|
261
|
|
262
|
public NamedAreaLevel getLevel(){
|
263
|
return level;
|
264
|
}
|
265
|
|
266
|
public static String getTermDtoSelect(String fromTable){
|
267
|
String[] result = createSqlParts(fromTable);
|
268
|
|
269
|
return result[0]+result[1]+result[2];
|
270
|
}
|
271
|
|
272
|
private static String[] createSqlParts(String fromTable) {
|
273
|
String sqlSelectString = ""
|
274
|
+ "select a.uuid, "
|
275
|
+ "r, "
|
276
|
+ "p.uuid, "
|
277
|
+ "k.uuid, "
|
278
|
+ "v.uuid, "
|
279
|
+ "a.orderIndex, "
|
280
|
+ "a.idInVocabulary, "
|
281
|
+ "a.termType, "
|
282
|
+ "a.uri, "
|
283
|
+ "m, "
|
284
|
+ "a.titleCache ";
|
285
|
|
286
|
String sqlFromString = " FROM "+fromTable+" as a ";
|
287
|
|
288
|
String sqlJoinString = "LEFT JOIN a.partOf as p "
|
289
|
+ "LEFT JOIN a.kindOf as k "
|
290
|
+ "LEFT JOIN a.media as m "
|
291
|
+ "LEFT JOIN a.representations AS r "
|
292
|
+ "LEFT JOIN a.vocabulary as v "
|
293
|
|
294
|
;
|
295
|
|
296
|
String[] result = new String[3];
|
297
|
result[0] = sqlSelectString;
|
298
|
result[1] = sqlFromString;
|
299
|
result[2] = sqlJoinString;
|
300
|
return result;
|
301
|
}
|
302
|
|
303
|
public static String getTermDtoSelectNamedArea(){
|
304
|
String[] result = createSqlParts("NamedArea");
|
305
|
return result[0]+ ", level " + result[1] + result[2]+ "LEFT JOIN a.level as level ";
|
306
|
}
|
307
|
|
308
|
public static List<TermDto> termDtoListFrom(List<Object[]> results) {
|
309
|
List<TermDto> dtos = new ArrayList<>(); // list to ensure order
|
310
|
// map to handle multiple representations/media/vocRepresentation because of LEFT JOIN
|
311
|
Map<UUID, TermDto> dtoMap = new HashMap<>(results.size());
|
312
|
for (Object[] elements : results) {
|
313
|
UUID uuid = (UUID)elements[0];
|
314
|
if(dtoMap.containsKey(uuid)){
|
315
|
// multiple results for one term -> multiple (voc) representation/media
|
316
|
if(elements[1]!=null){
|
317
|
dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
|
318
|
}
|
319
|
|
320
|
if(elements[9]!=null){
|
321
|
dtoMap.get(uuid).addMedia(((Media) elements[9]).getUuid());
|
322
|
}
|
323
|
} else {
|
324
|
// term representation
|
325
|
Set<Representation> representations = new HashSet<>();
|
326
|
if(elements[1] instanceof Representation) {
|
327
|
representations = new HashSet<>(1);
|
328
|
representations.add((Representation)elements[1]);
|
329
|
}
|
330
|
// term media
|
331
|
Set<UUID> mediaUuids = new HashSet<>();
|
332
|
if(elements[9] instanceof Media) {
|
333
|
mediaUuids.add(((Media) elements[9]).getUuid());
|
334
|
}
|
335
|
|
336
|
TermDto termDto = new TermDto(
|
337
|
uuid,
|
338
|
representations,
|
339
|
(TermType)elements[7],
|
340
|
(UUID)elements[2],
|
341
|
(UUID)elements[3],
|
342
|
(UUID)elements[4],
|
343
|
(Integer)elements[5],
|
344
|
(String)elements[6],
|
345
|
(String)elements[10]);
|
346
|
termDto.setUri((URI)elements[8]);
|
347
|
termDto.setMedia(mediaUuids);
|
348
|
if (elements.length>11 && elements[11] != null){
|
349
|
termDto.setLevel((NamedAreaLevel)elements[11]);
|
350
|
}
|
351
|
|
352
|
dtoMap.put(uuid, termDto);
|
353
|
dtos.add(termDto);
|
354
|
}
|
355
|
}
|
356
|
return dtos;
|
357
|
}
|
358
|
}
|