Project

General

Profile

Download (12.3 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
    private Set<Representation> vocRepresentations = null;
49
    private String vocRepresentation_L10n = null;
50
    private String vocRepresentation_L10n_abbreviatedLabel = null;
51
    private Collection<UUID> media = null;
52
    private NamedAreaLevel level = null;
53

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

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

    
71
    static public TermDto fromTerm(DefinedTermBase term) {
72
        return fromTerm(term, null, false);
73
    }
74

    
75
    static public TermDto fromTerm(DefinedTermBase term, boolean initializeToTop) {
76
        return fromTerm(term, null, initializeToTop);
77
    }
78

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

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

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

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

    
137
    public void setVocRepresentation_L10n(String vocRepresentation_L10n) {
138
        this.vocRepresentation_L10n = vocRepresentation_L10n;
139
    }
140

    
141
    public String getVocRepresentation_L10n() {
142
        return vocRepresentation_L10n;
143
    }
144

    
145
    public void setVocRepresentation_L10n_abbreviatedLabel(String vocRepresentation_L10n_abbreviatedLabel) {
146
        this.vocRepresentation_L10n_abbreviatedLabel = vocRepresentation_L10n_abbreviatedLabel;
147
    }
148

    
149
    public String getVocRepresentation_L10n_abbreviatedLabel() {
150
        return vocRepresentation_L10n_abbreviatedLabel;
151
    }
152

    
153
    protected void addVocRepresentation(Representation vocRepresentation){
154
        this.vocRepresentations.add(vocRepresentation);
155
    }
156

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
233

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

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

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

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

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

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

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

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

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

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

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

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

    
348
                dtoMap.put(uuid, termDto);
349
                dtos.add(termDto);
350
            }
351
        }
352
        return dtos;
353
    }
354
}
(20-20/24)