Project

General

Profile

Download (12.2 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.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
    public Collection<UUID> getMedia() {
246
        return media;
247
    }
248

    
249
    public void setMedia(Collection<UUID> media) {
250
        this.media = media;
251
    }
252

    
253
    protected void addMedia(UUID mediaUuid){
254
        this.media.add(mediaUuid);
255
    }
256

    
257
    public void setLevel(NamedAreaLevel level) {
258
        this.level = level;
259
    }
260

    
261
    public NamedAreaLevel getLevel(){
262
        return level;
263
    }
264

    
265
    public static String getTermDtoSelect(String fromTable){
266
        String[] result = createSqlParts(fromTable);
267

    
268
        return result[0]+result[1]+result[2];
269
    }
270

    
271
    private static String[] createSqlParts(String fromTable) {
272
        String sqlSelectString = ""
273
                + "select a.uuid, "
274
                + "r, "
275
                + "p.uuid, "
276
                + "k.uuid, "
277
                + "v.uuid, "
278
                + "a.orderIndex, "
279
                + "a.idInVocabulary, "
280
                + "a.termType,  "
281
                + "a.uri,  "
282
                + "m,  "
283
                + "a.titleCache ";
284

    
285
        String sqlFromString =   " FROM "+fromTable+" as a ";
286

    
287
        String sqlJoinString =  "LEFT JOIN a.partOf as p "
288
                + "LEFT JOIN a.kindOf as k "
289
                + "LEFT JOIN a.media as m "
290
                + "LEFT JOIN a.representations AS r "
291
                + "LEFT JOIN a.vocabulary as v "
292

    
293
                ;
294

    
295
        String[] result = new String[3];
296
        result[0] = sqlSelectString;
297
        result[1] = sqlFromString;
298
        result[2] = sqlJoinString;
299
        return result;
300
    }
301

    
302
    public static String getTermDtoSelectNamedArea(){
303
        String[] result = createSqlParts("NamedArea");
304
        return result[0]+  ", level  " + result[1] + result[2]+ "LEFT JOIN a.level as level ";
305
    }
306

    
307
    public static List<TermDto> termDtoListFrom(List<Object[]> results) {
308
        List<TermDto> dtos = new ArrayList<>(); // list to ensure order
309
        // map to handle multiple representations/media/vocRepresentation because of LEFT JOIN
310
        Map<UUID, TermDto> dtoMap = new HashMap<>(results.size());
311
        for (Object[] elements : results) {
312
            UUID uuid = (UUID)elements[0];
313
            if(dtoMap.containsKey(uuid)){
314
                // multiple results for one term -> multiple (voc) representation/media
315
                if(elements[1]!=null){
316
                    dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
317
                }
318

    
319
                if(elements[9]!=null){
320
                    dtoMap.get(uuid).addMedia(((Media) elements[9]).getUuid());
321
                }
322
            } else {
323
                // term representation
324
                Set<Representation> representations = new HashSet<>();
325
                if(elements[1] instanceof Representation) {
326
                    representations = new HashSet<>(1);
327
                    representations.add((Representation)elements[1]);
328
                }
329
                // term media
330
                Set<UUID> mediaUuids = new HashSet<>();
331
                if(elements[9] instanceof Media) {
332
                    mediaUuids.add(((Media) elements[9]).getUuid());
333
                }
334

    
335
                TermDto termDto = new TermDto(
336
                        uuid,
337
                        representations,
338
                        (TermType)elements[7],
339
                        (UUID)elements[2],
340
                        (UUID)elements[3],
341
                        (UUID)elements[4],
342
                        (Integer)elements[5],
343
                        (String)elements[6],
344
                        (String)elements[10]);
345
                termDto.setUri((URI)elements[8]);
346
                termDto.setMedia(mediaUuids);
347
                if (elements.length>11 && elements[11] != null){
348
                    termDto.setLevel((NamedAreaLevel)elements[11]);
349
                }
350

    
351
                dtoMap.put(uuid, termDto);
352
                dtos.add(termDto);
353
            }
354
        }
355
        return dtos;
356
    }
357
}
(27-27/30)