Project

General

Profile

Download (9.12 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.net.URI;
12
import java.util.ArrayList;
13
import java.util.Collection;
14
import java.util.HashMap;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.UUID;
20

    
21
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
22
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
23
import eu.etaxonomy.cdm.model.common.Representation;
24
import eu.etaxonomy.cdm.model.common.TermType;
25
import eu.etaxonomy.cdm.model.common.TermVocabulary;
26

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

    
34
    private static final long serialVersionUID = 5627308906985438034L;
35

    
36
    private UUID kindOfUuid = null;
37
    private UUID partOfUuid = null;
38
    private UUID vocabularyUuid = null;
39
    private TermDto kindOfDto = null;
40
    private TermDto partOfDto = null;
41
    private TermVocabularyDto vocabularyDto = null;
42
    private Integer orderIndex = null;
43
    private String idInVocabulary = null;
44
    private Collection<TermDto> includes;
45
    private Collection<TermDto> generalizationOf;
46
    private Set<Representation> vocRepresentations = null;
47
    private String vocRepresentation_L10n = null;
48
    private String vocRepresentation_L10n_abbreviatedLabel = null;
49

    
50
    private TermDto(UUID uuid, Set<Representation> representations, UUID partOfUuid, UUID kindOfUuid,
51
            UUID vocabularyUuid, Integer orderIndex, String idInVocabulary) {
52
        this(uuid, representations, partOfUuid, kindOfUuid, vocabularyUuid, orderIndex, idInVocabulary, null);
53
    }
54

    
55
    private TermDto(UUID uuid, Set<Representation> representations, UUID partOfUuid, UUID kindOfUuid,
56
            UUID vocabularyUuid, Integer orderIndex, String idInVocabulary, Set<Representation> vocRepresentations) {
57
        super(uuid, representations);
58
        this.partOfUuid = partOfUuid;
59
        this.kindOfUuid = kindOfUuid;
60
        this.vocabularyUuid = vocabularyUuid;
61
        this.orderIndex = orderIndex;
62
        this.idInVocabulary = idInVocabulary;
63
        this.vocRepresentations = vocRepresentations;
64
    }
65

    
66
    static public TermDto fromTerm(DefinedTermBase term) {
67
        return fromTerm(term, null, false);
68
    }
69

    
70
    static public TermDto fromTerm(DefinedTermBase term, boolean initializeToTop) {
71
        return fromTerm(term, null, initializeToTop);
72
    }
73

    
74
    static public TermDto fromTerm(DefinedTermBase term, Set<Representation> representations) {
75
        return fromTerm(term, representations, false);
76
    }
77

    
78
    static public TermDto fromTerm(DefinedTermBase term, Set<Representation> representations, boolean initializeToTop) {
79
        DefinedTermBase partOf = term.getPartOf();
80
        DefinedTermBase kindOf = term.getKindOf();
81
        TermVocabulary vocabulary = term.getVocabulary();
82
        TermDto dto = new TermDto(
83
                term.getUuid(),
84
                representations!=null?representations:term.getRepresentations(),
85
                (partOf!=null?partOf.getUuid():null),
86
                (kindOf!=null?kindOf.getUuid():null),
87
                vocabulary.getUuid(),
88
                (term instanceof OrderedTermBase)?((OrderedTermBase) term).getOrderIndex():null,
89
                term.getIdInVocabulary());
90
        dto.setTermType(term.getTermType());
91
        dto.setUri(term.getUri());
92
        if(initializeToTop){
93
            if(partOf!=null){
94
                dto.setPartOfDto(fromTerm(partOf, initializeToTop));
95
            }
96
            if(kindOf!=null){
97
                dto.setKindOfDto(fromTerm(kindOf, initializeToTop));
98
            }
99
            dto.setVocabularyDto(new TermVocabularyDto(vocabulary.getUuid(), vocabulary.getRepresentations()));
100
        }
101
        return dto;
102
    }
103

    
104
    @Override
105
    public void localize(ITermRepresentation_L10n representation_L10n) {
106
        if(vocRepresentations!=null){
107
            representation_L10n.localize(vocRepresentations);
108
            if (representation_L10n.getLabel() != null) {
109
                setVocRepresentation_L10n(representation_L10n.getLabel());
110
            }
111
            if (representation_L10n.getAbbreviatedLabel() != null) {
112
                setVocRepresentation_L10n_abbreviatedLabel(representation_L10n.getAbbreviatedLabel());
113
            }
114
        }
115
        super.localize(representation_L10n);
116
    }
117

    
118
    public void setVocRepresentation_L10n(String vocRepresentation_L10n) {
119
        this.vocRepresentation_L10n = vocRepresentation_L10n;
120
    }
121

    
122
    public String getVocRepresentation_L10n() {
123
        return vocRepresentation_L10n;
124
    }
125

    
126
    public void setVocRepresentation_L10n_abbreviatedLabel(String vocRepresentation_L10n_abbreviatedLabel) {
127
        this.vocRepresentation_L10n_abbreviatedLabel = vocRepresentation_L10n_abbreviatedLabel;
128
    }
129

    
130
    public String getVocRepresentation_L10n_abbreviatedLabel() {
131
        return vocRepresentation_L10n_abbreviatedLabel;
132
    }
133

    
134
    public void setPartOfDto(TermDto partOfDto) {
135
        this.partOfDto = partOfDto;
136
    }
137

    
138
    public TermDto getPartOfDto() {
139
        return partOfDto;
140
    }
141

    
142
    public void setKindOfDto(TermDto kindOfDto) {
143
        this.kindOfDto = kindOfDto;
144
    }
145

    
146
    public TermDto getKindOfDto() {
147
        return kindOfDto;
148
    }
149

    
150
    public void setVocabularyDto(TermVocabularyDto vocabularyDto) {
151
        this.vocabularyDto = vocabularyDto;
152
    }
153

    
154
    public TermVocabularyDto getVocabularyDto() {
155
        return vocabularyDto;
156
    }
157

    
158
    public UUID getVocabularyUuid() {
159
        return vocabularyUuid;
160
    }
161

    
162
    public void setVocabularyUuid(UUID vocabularyUuid) {
163
        this.vocabularyUuid = vocabularyUuid;
164
    }
165

    
166
    public UUID getPartOfUuid() {
167
        return partOfUuid;
168
    }
169

    
170
    public UUID getKindOfUuid() {
171
        return kindOfUuid;
172
    }
173

    
174
    public Integer getOrderIndex() {
175
        return orderIndex;
176
    }
177

    
178
    public void setOrderIndex(Integer orderIndex) {
179
        this.orderIndex = orderIndex;
180
    }
181

    
182
    public String getIdInVocabulary() {
183
        return idInVocabulary;
184
    }
185

    
186
    public void setIdInVocabulary(String idInVocabulary) {
187
        this.idInVocabulary = idInVocabulary;
188
    }
189

    
190
    public Collection<TermDto> getIncludes() {
191
        return includes;
192
    }
193

    
194
    public void setIncludes(Collection<TermDto> includes) {
195
        this.includes = includes;
196
    }
197

    
198
    public Collection<TermDto> getGeneralizationOf() {
199
        return generalizationOf;
200
    }
201

    
202
    public void setGeneralizationOf(Collection<TermDto> generalizationOf) {
203
        this.generalizationOf = generalizationOf;
204
    }
205

    
206
    public static String getTermDtoSelect(){
207
        return getTermDtoSelect("DefinedTermBase");
208
    }
209

    
210
    public static String getTermDtoSelect(String fromTable){
211
        return ""
212
                + "select a.uuid, "
213
                + "r, "
214
                + "p.uuid, "
215
                + "k.uuid, "
216
                + "v.uuid, "
217
                + "a.orderIndex, "
218
                + "a.idInVocabulary, "
219
                + "voc_rep,  "
220
                + "a.termType,  "
221
                + "a.uri  "
222
                + "from "+fromTable+" as a "
223
                + "LEFT JOIN a.partOf as p "
224
                + "LEFT JOIN a.kindOf as k "
225
                + "LEFT JOIN a.representations AS r "
226
                + "LEFT JOIN a.vocabulary as v "
227
                + "LEFT JOIN v.representations as voc_rep ";
228
    }
229

    
230
    public static List<TermDto> termDtoListFrom(List<Object[]> results) {
231
        Map<UUID, TermDto> dtoMap = new HashMap<>(results.size());
232
        for (Object[] elements : results) {
233
            UUID uuid = (UUID)elements[0];
234
            if(dtoMap.containsKey(uuid)){
235
                dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
236
            } else {
237
                Set<Representation> representations = new HashSet<>();
238
                if(elements[1] instanceof Representation) {
239
                    representations = new HashSet<Representation>(1);
240
                    representations.add((Representation)elements[1]);
241
                } else {
242
                    representations = (Set<Representation>)elements[1];
243
                }
244
                Set<Representation> vocRepresentations = new HashSet<>();
245
                if(elements[7] instanceof Representation) {
246
                    vocRepresentations = new HashSet<Representation>(7);
247
                    vocRepresentations.add((Representation)elements[7]);
248
                } else {
249
                    vocRepresentations = (Set<Representation>)elements[7];
250
                }
251
                TermDto termDto = new TermDto(
252
                        uuid,
253
                        representations,
254
                        (UUID)elements[2],
255
                        (UUID)elements[3],
256
                        (UUID)elements[4],
257
                        (Integer)elements[5],
258
                        (String)elements[6],
259
                        vocRepresentations);
260
                termDto.setTermType((TermType)elements[8]);
261
                termDto.setUri((URI)elements[9]);
262
                dtoMap.put(uuid, termDto);
263
            }
264
        }
265
        return new ArrayList<>(dtoMap.values());
266
    }
267

    
268
}
(14-14/16)