Project

General

Profile

Download (12.4 KB) Statistics
| Branch: | Tag: | Revision:
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.model.location.NamedArea;
22
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
23
import eu.etaxonomy.cdm.model.media.Media;
24
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
25
import eu.etaxonomy.cdm.model.term.OrderedTermBase;
26
import eu.etaxonomy.cdm.model.term.Representation;
27
import eu.etaxonomy.cdm.model.term.TermType;
28
import eu.etaxonomy.cdm.model.term.TermVocabulary;
29

    
30
/**
31
 * @author andreas
32
 * @since Mar 25, 2015
33
 */
34
public class TermDto extends AbstractTermDto{
35

    
36
    private static final long serialVersionUID = 5627308906985438034L;
37

    
38
    private UUID kindOfUuid = null;
39
    private UUID partOfUuid = null;
40
    private UUID vocabularyUuid = null;
41
    private TermDto kindOfDto = null;
42
    private TermDto partOfDto = null;
43
    private TermVocabularyDto vocabularyDto = null;
44
    private Integer orderIndex = null;
45
    private String idInVocabulary = null;
46
    private Collection<TermDto> includes;
47
    private Collection<TermDto> generalizationOf;
48
//    this should be handled in vocabularyDto
49
//    private Set<Representation> vocRepresentations = null;
50
//    private String vocRepresentation_L10n = null;
51
//    private String vocRepresentation_L10n_abbreviatedLabel = null;
52
    private Collection<UUID> media = null;
53
    private NamedAreaLevel level = null;
54

    
55
//    private TermDto(UUID uuid, Set<Representation> representations, TermType termType, UUID partOfUuid, UUID kindOfUuid,
56
//            UUID vocabularyUuid, Integer orderIndex, String idInVocabulary, String titleCache) {
57
//        this(uuid, representations, termType, partOfUuid, kindOfUuid, vocabularyUuid, orderIndex, idInVocabulary, titleCache);
58
//    }
59

    
60
    protected TermDto(UUID uuid, Set<Representation> representations, TermType termType, UUID partOfUuid, UUID kindOfUuid,
61
            UUID vocabularyUuid, Integer orderIndex, String idInVocabulary, String titleCache) {
62
        super(uuid, representations, titleCache);
63
        this.partOfUuid = partOfUuid;
64
        this.kindOfUuid = kindOfUuid;
65
        this.vocabularyUuid = vocabularyUuid;
66
        this.orderIndex = orderIndex;
67
        this.idInVocabulary = idInVocabulary;
68
//        this.vocRepresentations = vocRepresentations;
69
        setTermType(termType);
70
    }
71

    
72
    static public TermDto fromTerm(DefinedTermBase<?> term) {
73
        return fromTerm(term, null, false);
74
    }
75

    
76
    static public TermDto fromTerm(DefinedTermBase<?> term, boolean initializeToTop) {
77
        return fromTerm(term, null, initializeToTop);
78
    }
79

    
80
    static public TermDto fromTerm(DefinedTermBase<?> term, Set<Representation> representations) {
81
        return fromTerm(term, representations, false);
82
    }
83

    
84
    static public TermDto fromTerm(DefinedTermBase<?> term, Set<Representation> representations, boolean initializeToTop) {
85
        DefinedTermBase<?> partOf = term.getPartOf();
86
        DefinedTermBase<?> kindOf = term.getKindOf();
87
        TermVocabulary<?> vocabulary = term.getVocabulary();
88

    
89
        TermDto dto = new TermDto(
90
                        term.getUuid(),
91
                        representations!=null?representations:term.getRepresentations(),
92
                        term.getTermType(),
93
                        (partOf!=null?partOf.getUuid():null),
94
                        (kindOf!=null?kindOf.getUuid():null),
95
                        (vocabulary!=null?vocabulary.getUuid():null),
96
                        (term instanceof OrderedTermBase)?((OrderedTermBase<?>) term).getOrderIndex():null,
97
                         term.getIdInVocabulary(), term.getTitleCache());
98
        dto.setUri(term.getUri());
99
        if(initializeToTop){
100
            if(partOf!=null){
101
                dto.setPartOfDto(fromTerm(partOf, initializeToTop));
102
            }
103
            if(kindOf!=null){
104
                dto.setKindOfDto(fromTerm(kindOf, initializeToTop));
105
            }
106
        }
107
        if (vocabulary != null){
108
            dto.setVocabularyDto(new TermVocabularyDto(vocabulary.getUuid(), vocabulary.getRepresentations(), term.getTermType(), vocabulary.getTitleCache(), vocabulary.isAllowDuplicates(), vocabulary.isOrderRelevant(), vocabulary.isFlat()));
109
        }
110
        if(term.getMedia()!=null){
111
            Collection<UUID> mediaUuids = new HashSet<>();
112
            Set<Media> media = term.getMedia();
113
            for (Media medium : media) {
114
                mediaUuids.add(medium.getUuid());
115
            }
116
            dto.setMedia(mediaUuids);
117
        }
118
        if (term instanceof NamedArea && ((NamedArea)term).getLevel() != null){
119
            dto.setLevel(((NamedArea)term).getLevel());
120
        }
121
        return dto;
122
    }
123

    
124
    @Override
125
    public void localize(ITermRepresentation_L10n representation_L10n) {
126
//        if(vocRepresentations!=null){
127
//            representation_L10n.localize(vocRepresentations);
128
//            if (representation_L10n.getLabel() != null) {
129
//                setVocRepresentation_L10n(representation_L10n.getLabel());
130
//            }
131
//            if (representation_L10n.getAbbreviatedLabel() != null) {
132
//                setVocRepresentation_L10n_abbreviatedLabel(representation_L10n.getAbbreviatedLabel());
133
//            }
134
//        }
135
        super.localize(representation_L10n);
136
    }
137

    
138
//    public void setVocRepresentation_L10n(String vocRepresentation_L10n) {
139
//        this.vocRepresentation_L10n = vocRepresentation_L10n;
140
//    }
141
//
142
    public String getVocRepresentation_L10n() {
143
        return vocabularyDto.getRepresentation_L10n();
144
    }
145
//
146
//    public void setVocRepresentation_L10n_abbreviatedLabel(String vocRepresentation_L10n_abbreviatedLabel) {
147
//        this.vocRepresentation_L10n_abbreviatedLabel = vocRepresentation_L10n_abbreviatedLabel;
148
//    }
149
//
150
    public String getVocRepresentation_L10n_abbreviatedLabel() {
151
        return vocabularyDto.getRepresentation_L10n_abbreviatedLabel();
152
    }
153
//
154
//    protected void addVocRepresentation(Representation vocRepresentation){
155
//        this.vocRepresentations.add(vocRepresentation);
156
//    }
157

    
158
    public void setPartOfDto(TermDto partOfDto) {
159
        this.partOfDto = partOfDto;
160
    }
161

    
162
    public TermDto getPartOfDto() {
163
        return partOfDto;
164
    }
165

    
166
    public void setKindOfDto(TermDto kindOfDto) {
167
        this.kindOfDto = kindOfDto;
168
    }
169

    
170
    public TermDto getKindOfDto() {
171
        return kindOfDto;
172
    }
173

    
174
    public void setVocabularyDto(TermVocabularyDto vocabularyDto) {
175
        this.vocabularyDto = vocabularyDto;
176
    }
177

    
178
    public TermVocabularyDto getVocabularyDto() {
179
        return vocabularyDto;
180
    }
181

    
182
    public UUID getVocabularyUuid() {
183
        return vocabularyUuid;
184
    }
185

    
186
    public void setVocabularyUuid(UUID vocabularyUuid) {
187
        this.vocabularyUuid = vocabularyUuid;
188
    }
189

    
190
    public UUID getPartOfUuid() {
191
        return partOfUuid;
192
    }
193

    
194
    public UUID getKindOfUuid() {
195
        return kindOfUuid;
196
    }
197

    
198
    public Integer getOrderIndex() {
199
        return orderIndex;
200
    }
201

    
202
    public void setOrderIndex(Integer orderIndex) {
203
        this.orderIndex = orderIndex;
204
    }
205

    
206
    public String getIdInVocabulary() {
207
        return idInVocabulary;
208
    }
209

    
210
    public void setIdInVocabulary(String idInVocabulary) {
211
        this.idInVocabulary = idInVocabulary;
212
    }
213

    
214
    public Collection<TermDto> getIncludes() {
215
        return includes;
216
    }
217

    
218
    public void setIncludes(Collection<TermDto> includes) {
219
        this.includes = includes;
220
    }
221

    
222
    public Collection<TermDto> getGeneralizationOf() {
223
        return generalizationOf;
224
    }
225

    
226
    public void setGeneralizationOf(Collection<TermDto> generalizationOf) {
227
        this.generalizationOf = generalizationOf;
228
    }
229

    
230
    public static String getTermDtoSelect(){
231
        return getTermDtoSelect("DefinedTermBase");
232
    }
233

    
234

    
235
    public Collection<UUID> getMedia() {
236
        return media;
237
    }
238

    
239
    public void setMedia(Collection<UUID> media) {
240
        this.media = media;
241
    }
242

    
243
    protected void addMedia(UUID mediaUuid){
244
        this.media.add(mediaUuid);
245
    }
246

    
247
    public void setLevel(NamedAreaLevel level) {
248
        this.level = level;
249
    }
250

    
251
    public NamedAreaLevel getLevel(){
252
        return level;
253
    }
254

    
255
    public static String getTermDtoSelect(String fromTable){
256
        String[] result = createSqlParts(fromTable);
257

    
258
        return result[0]+result[1]+result[2];
259
    }
260

    
261
    private static String[] createSqlParts(String fromTable) {
262
        String sqlSelectString = ""
263
                + "select a.uuid, "
264
                + "r, "
265
                + "p.uuid, "
266
                + "k.uuid, "
267
                + "v.uuid, "
268
                + "a.orderIndex, "
269
                + "a.idInVocabulary, "
270
//                + "voc_rep,  "
271
                + "a.termType,  "
272
                + "a.uri,  "
273
                + "m,  "
274
                + "a.titleCache ";
275
        String sqlFromString =   " FROM "+fromTable+" as a ";
276

    
277
        String sqlJoinString =  "LEFT JOIN a.partOf as p "
278
                + "LEFT JOIN a.kindOf as k "
279
                + "LEFT JOIN a.media as m "
280
                + "LEFT JOIN a.representations AS r "
281
                + "LEFT JOIN a.vocabulary as v "
282
//                + "LEFT JOIN v.representations as voc_rep "
283
                ;
284

    
285
        String[] result = new String[3];
286
        result[0] = sqlSelectString;
287
        result[1] = sqlFromString;
288
        result[2] = sqlJoinString;
289
        return result;
290
    }
291

    
292
    public static String getTermDtoSelectNamedArea(){
293
        String[] result = createSqlParts("NamedArea");
294
        return result[0]+  ", level  " + result[1] + result[2]+ "LEFT JOIN a.level as level ";
295
    }
296

    
297
    public static List<TermDto> termDtoListFrom(List<Object[]> results) {
298
        List<TermDto> dtos = new ArrayList<>(); // list to ensure order
299
        // map to handle multiple representations/media/vocRepresentation because of LEFT JOIN
300
        Map<UUID, TermDto> dtoMap = new HashMap<>(results.size());
301
        for (Object[] elements : results) {
302
            UUID uuid = (UUID)elements[0];
303
            if(dtoMap.containsKey(uuid)){
304
                // multiple results for one term -> multiple (voc) representation/media
305
                if(elements[1]!=null){
306
                    dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
307
                }
308
//                if(elements[7]!=null){
309
//                    dtoMap.get(uuid).addVocRepresentation((Representation)elements[7]);
310
//                }
311
                if(elements[9]!=null){
312
                    dtoMap.get(uuid).addMedia(((Media) elements[9]).getUuid());
313
                }
314
            } else {
315
                // term representation
316
                Set<Representation> representations = new HashSet<>();
317
                if(elements[1] instanceof Representation) {
318
                    representations = new HashSet<>(1);
319
                    representations.add((Representation)elements[1]);
320
                }
321
                // term media
322
                Set<UUID> mediaUuids = new HashSet<>();
323
                if(elements[9] instanceof Media) {
324
                    mediaUuids.add(((Media) elements[9]).getUuid());
325
                }
326
                // voc representation
327
//                Set<Representation> vocRepresentations = new HashSet<>();
328
//                if(elements[7] instanceof Representation) {
329
//                    vocRepresentations = new HashSet<>(7);
330
//                    vocRepresentations.add((Representation)elements[7]);
331
//                }
332
                TermDto termDto = new TermDto(
333
                        uuid,
334
                        representations,
335
                        (TermType)elements[7],
336
                        (UUID)elements[2],
337
                        (UUID)elements[3],
338
                        (UUID)elements[4],
339
                        (Integer)elements[5],
340
                        (String)elements[6],
341
//                        vocRepresentations,
342
                        (String)elements[10]);
343
                termDto.setUri((URI)elements[8]);
344
                termDto.setMedia(mediaUuids);
345
                if (elements.length>11 && elements[11] != null){
346
                    termDto.setLevel((NamedAreaLevel)elements[11]);
347
                }
348

    
349
                dtoMap.put(uuid, termDto);
350
                dtos.add(termDto);
351
            }
352
        }
353
        return dtos;
354
    }
355
}
(25-25/29)