1
|
/**
|
2
|
* Copyright (C) 2020 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.api.service.dto;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.EnumSet;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
17
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
18
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
19
|
import eu.etaxonomy.cdm.model.description.DescriptionType;
|
20
|
import eu.etaxonomy.cdm.model.description.Feature;
|
21
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
22
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
23
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
24
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
26
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
28
|
import eu.etaxonomy.cdm.persistence.dto.FeatureDto;
|
29
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
30
|
|
31
|
/**
|
32
|
* @author k.luther
|
33
|
* @since May 15, 2020
|
34
|
*/
|
35
|
public class DescriptionBaseDto extends EntityDTO<DescriptionBase>{
|
36
|
|
37
|
private static final long serialVersionUID = -1578895619195062502L;
|
38
|
|
39
|
private UuidAndTitleCache<Taxon> taxonUuidAndTitleCache;
|
40
|
private UuidAndTitleCache<SpecimenOrObservationBase> specimenDto;
|
41
|
private int id;
|
42
|
|
43
|
private UuidAndTitleCache<TaxonName> nameUuidAndTitleCache;
|
44
|
|
45
|
private List<DescriptionElementDto> elements = new ArrayList<>();
|
46
|
|
47
|
private EnumSet<DescriptionType> types = EnumSet.noneOf(DescriptionType.class);
|
48
|
|
49
|
|
50
|
public DescriptionBaseDto(UUID uuid, String titleCache, UuidAndTitleCache<Taxon> taxonUuidAndTitleCache, UuidAndTitleCache<SpecimenOrObservationBase> specimenDto, UuidAndTitleCache<TaxonName> nameUuidAndTitleCache, Integer id, List<DescriptionElementDto> elements, EnumSet<DescriptionType> types){
|
51
|
super(uuid, titleCache);
|
52
|
this.taxonUuidAndTitleCache = taxonUuidAndTitleCache;
|
53
|
this.specimenDto = specimenDto;
|
54
|
this.nameUuidAndTitleCache = nameUuidAndTitleCache;
|
55
|
this.id = id;
|
56
|
if (elements != null){
|
57
|
this.elements = elements;
|
58
|
}
|
59
|
this.types = types;
|
60
|
|
61
|
|
62
|
}
|
63
|
|
64
|
public DescriptionBaseDto(UuidAndTitleCache<SpecimenOrObservationBase> specimen){
|
65
|
super(SpecimenDescription.NewInstance());
|
66
|
specimenDto = specimen;
|
67
|
// if(specimen instanceof FieldUnit) {
|
68
|
// specimenDto = FieldUnitDTO.fromEntity((FieldUnit)specimen);
|
69
|
// } else {
|
70
|
// specimenDto = DerivedUnitDTO.fromEntity((DerivedUnit)specimen);
|
71
|
// }
|
72
|
}
|
73
|
|
74
|
public static String getDescriptionBaseDtoSelect(){
|
75
|
String[] result = createSqlParts();
|
76
|
|
77
|
return result[0]+result[1]+result[2] + result[3];
|
78
|
}
|
79
|
|
80
|
|
81
|
private static String[] createSqlParts() {
|
82
|
String sqlSelectString = ""
|
83
|
+ "select a.uuid, "
|
84
|
+ "a.id, "
|
85
|
+ "a.titleCache, "
|
86
|
//taxon
|
87
|
+ "t.uuid, "
|
88
|
+ "t.id, "
|
89
|
+ "t.titleCache, "
|
90
|
//specimen
|
91
|
+ "s.uuid, "
|
92
|
+ "s.id, "
|
93
|
+ "s.titleCache, "
|
94
|
//types
|
95
|
+ "a.types ";
|
96
|
|
97
|
String sqlFromString = " FROM DescriptionBase as a ";
|
98
|
|
99
|
String sqlJoinString = " LEFT JOIN a.taxon as t "
|
100
|
+ " LEFT JOIN a.describedSpecimenOrObservation as s ";
|
101
|
|
102
|
String sqlWhereString = " WHERE a.uuid = :uuid";
|
103
|
|
104
|
String[] result = new String[4];
|
105
|
result[0] = sqlSelectString;
|
106
|
result[1] = sqlFromString;
|
107
|
result[2] = sqlJoinString;
|
108
|
result[3] = sqlWhereString;
|
109
|
return result;
|
110
|
}
|
111
|
|
112
|
public static String getDescriptionBaseDtoForTaxonSelect(UUID uuidTaxon ){
|
113
|
String[] result = createSqlPartsForTaxon(uuidTaxon);
|
114
|
|
115
|
return result[0]+result[1]+result[2];
|
116
|
}
|
117
|
|
118
|
private static String[] createSqlPartsForTaxon(UUID uuidTaxon) {
|
119
|
|
120
|
|
121
|
|
122
|
String sqlSelectString = ""
|
123
|
+ "select a.uuid, "
|
124
|
+ "a.id, "
|
125
|
+ "a.titleCache, "
|
126
|
//taxon
|
127
|
+ "t.uuid, "
|
128
|
+ "t.id, "
|
129
|
+ "t.titleCache, "
|
130
|
|
131
|
//types
|
132
|
+ "a.types ";
|
133
|
|
134
|
String sqlFromString = " FROM DescriptionBase as a ";
|
135
|
|
136
|
String sqlJoinString = " LEFT JOIN a.taxon as t ";
|
137
|
|
138
|
String sqlWhereString = " WHERE a.taxon.uuid like "+ uuidTaxon.toString();
|
139
|
|
140
|
String[] result = new String[4];
|
141
|
result[0] = sqlSelectString;
|
142
|
result[1] = sqlFromString;
|
143
|
result[2] = sqlJoinString;
|
144
|
result[3] = sqlWhereString;
|
145
|
return result;
|
146
|
}
|
147
|
|
148
|
|
149
|
|
150
|
// public DescriptionBaseDto(SpecimenOrObservationBase specimen){
|
151
|
// this(specimen, null, false, false);
|
152
|
// }
|
153
|
|
154
|
|
155
|
public UUID getDescriptionUuid() {
|
156
|
return getCdmEntity().getUuid();
|
157
|
}
|
158
|
|
159
|
public String getTitleCache(){
|
160
|
return getCdmEntity().getTitleCache();
|
161
|
}
|
162
|
|
163
|
public UuidAndTitleCache<Taxon> getTaxonDto() {
|
164
|
return taxonUuidAndTitleCache;
|
165
|
}
|
166
|
|
167
|
public UuidAndTitleCache<SpecimenOrObservationBase> getSpecimenDto() {
|
168
|
return specimenDto;
|
169
|
}
|
170
|
|
171
|
public UuidAndTitleCache<TaxonName> getNameDto() {
|
172
|
return nameUuidAndTitleCache;
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* @return
|
177
|
*/
|
178
|
public List<DescriptionElementDto> getElements() {
|
179
|
return elements;
|
180
|
}
|
181
|
|
182
|
public void addElement(DescriptionElementDto element){
|
183
|
if (elements == null){
|
184
|
elements = new ArrayList<>();
|
185
|
}
|
186
|
elements.add(element);
|
187
|
}
|
188
|
|
189
|
public EnumSet<DescriptionType> getTypes() {
|
190
|
return types;
|
191
|
}
|
192
|
|
193
|
public void setTypes(EnumSet<DescriptionType> types) {
|
194
|
this.types = types;
|
195
|
}
|
196
|
|
197
|
public int getId() {
|
198
|
return id;
|
199
|
}
|
200
|
|
201
|
public void setId(int id) {
|
202
|
this.id = id;
|
203
|
}
|
204
|
|
205
|
public static DescriptionBaseDto fromDescription(DescriptionBase desc) {
|
206
|
UuidAndTitleCache<Taxon> taxonUuidAndTitleCache = null;
|
207
|
UuidAndTitleCache<SpecimenOrObservationBase> specimenDto = null;
|
208
|
UuidAndTitleCache<TaxonName> nameUuidAndTitleCache = null;
|
209
|
if (desc instanceof TaxonDescription){
|
210
|
Taxon taxon = HibernateProxyHelper.deproxy(((TaxonDescription)desc).getTaxon(), Taxon.class);
|
211
|
if (taxon != null){
|
212
|
taxonUuidAndTitleCache = new UuidAndTitleCache<Taxon>(taxon.getUuid(), taxon.getId(), taxon.getTitleCache());
|
213
|
}
|
214
|
}
|
215
|
if (desc instanceof SpecimenDescription){
|
216
|
SpecimenDescription specimenDesc = HibernateProxyHelper.deproxy(desc, SpecimenDescription.class);
|
217
|
SpecimenOrObservationBase specimen = specimenDesc.getDescribedSpecimenOrObservation();
|
218
|
specimenDto = new UuidAndTitleCache<>(specimen.getUuid(), specimen.getId(), specimen.getTitleCache());
|
219
|
|
220
|
// if (specimen != null){
|
221
|
// if (specimen instanceof FieldUnit){
|
222
|
// specimenDto = FieldUnitDTO.fromEntity((FieldUnit)specimen);
|
223
|
// }else{
|
224
|
// specimenDto = DerivedUnitDTO.fromEntity((DerivedUnit)specimen);
|
225
|
// }
|
226
|
// }
|
227
|
}
|
228
|
if (desc instanceof TaxonNameDescription){
|
229
|
TaxonNameDescription nameDesc = HibernateProxyHelper.deproxy(desc, TaxonNameDescription.class);
|
230
|
TaxonName name = nameDesc.getTaxonName();
|
231
|
if (name != null){
|
232
|
nameUuidAndTitleCache = new UuidAndTitleCache<TaxonName>(name.getUuid(), name.getId(), name.getTitleCache());
|
233
|
}
|
234
|
}
|
235
|
|
236
|
List<DescriptionElementDto> elements = new ArrayList<>();
|
237
|
for (Object element: desc.getElements()){
|
238
|
if (element instanceof CategoricalData){
|
239
|
Feature feature = ((CategoricalData) element).getFeature();
|
240
|
FeatureDto featureDto = FeatureDto.fromFeature(feature);
|
241
|
CategoricalDataDto dto = CategoricalDataDto.fromCategoricalData((CategoricalData)element);
|
242
|
elements.add(dto);
|
243
|
}
|
244
|
if (element instanceof QuantitativeData){
|
245
|
Feature feature = ((QuantitativeData) element).getFeature();
|
246
|
FeatureDto featureDto = FeatureDto.fromFeature(feature);
|
247
|
QuantitativeDataDto dto = QuantitativeDataDto.fromQuantitativeData((QuantitativeData)element);
|
248
|
elements.add(dto);
|
249
|
}
|
250
|
}
|
251
|
|
252
|
|
253
|
DescriptionBaseDto dto = new DescriptionBaseDto(desc.getUuid(), desc.getTitleCache(), taxonUuidAndTitleCache, specimenDto, nameUuidAndTitleCache, desc.getId(), elements, desc.getTypes());
|
254
|
return dto;
|
255
|
}
|
256
|
|
257
|
/**
|
258
|
* @param result
|
259
|
* @return
|
260
|
*/
|
261
|
public static List<DescriptionBaseDto> descriptionBaseDtoListFrom(List<Object[]> result) {
|
262
|
List<DescriptionBaseDto> dtoResult = new ArrayList<>();
|
263
|
DescriptionBaseDto dto;
|
264
|
for (Object[] o: result){
|
265
|
UuidAndTitleCache<Taxon> taxonUuidAndTitleCache = null;
|
266
|
UuidAndTitleCache<SpecimenOrObservationBase> specimenUuidAndTitleCache = null;
|
267
|
UuidAndTitleCache<TaxonName> nameUuidAndTitleCache = null;
|
268
|
EnumSet<DescriptionType> type = null;
|
269
|
if (o[3] != null){
|
270
|
taxonUuidAndTitleCache = new UuidAndTitleCache<>((UUID)o[3], (Integer)o[4], (String)o[5]);
|
271
|
}
|
272
|
if (o[6] != null && o[6] instanceof UUID){
|
273
|
specimenUuidAndTitleCache = new UuidAndTitleCache<>((UUID)o[6], (Integer)o[7], (String)o[8]);
|
274
|
}else if (o[6] instanceof EnumSet<?>){
|
275
|
type = (EnumSet<DescriptionType>)o[6];
|
276
|
}else if (o.length >7 && o[9] instanceof EnumSet<?>){
|
277
|
type = (EnumSet<DescriptionType>)o[9];
|
278
|
}
|
279
|
// if (o[9] != null){
|
280
|
// nameUuidAndTitleCache = new UuidAndTitleCache<>((UUID)o[9], (Integer)o[10], (String)o[11]);
|
281
|
// }
|
282
|
dto = new DescriptionBaseDto((UUID)o[0], (String)o[2], taxonUuidAndTitleCache, specimenUuidAndTitleCache, nameUuidAndTitleCache, (Integer)o[1], null, type);
|
283
|
dtoResult.add(dto);
|
284
|
}
|
285
|
|
286
|
return dtoResult;
|
287
|
|
288
|
}
|
289
|
|
290
|
|
291
|
|
292
|
|
293
|
|
294
|
|
295
|
|
296
|
|
297
|
|
298
|
|
299
|
}
|