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.persistence.dto;
|
10
|
|
11
|
import java.net.URI;
|
12
|
import java.util.ArrayList;
|
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.model.description.Character;
|
21
|
import eu.etaxonomy.cdm.model.term.Representation;
|
22
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
23
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
24
|
import eu.etaxonomy.cdm.model.term.TermType;
|
25
|
|
26
|
/**
|
27
|
* @author k.luther
|
28
|
* @since 07.01.2020
|
29
|
*/
|
30
|
public class TermTreeDto extends TermCollectionDto {
|
31
|
private static final long serialVersionUID = -7223363599985320531L;
|
32
|
|
33
|
private TermNodeDto root;
|
34
|
|
35
|
public static TermTreeDto fromTree(TermTree tree){
|
36
|
TermTreeDto dto = new TermTreeDto(tree.getUuid(), tree.getRepresentations(), tree.getTermType(), tree.getRoot(), tree.getTitleCache(), tree.isAllowDuplicates(), tree.isOrderRelevant(), tree.isFlat() );
|
37
|
return dto;
|
38
|
}
|
39
|
|
40
|
public TermTreeDto(UUID uuid, Set<Representation> representations, TermType termType, String titleCache, boolean isAllowDuplicates, boolean isOrderRelevant, boolean isFlat) {
|
41
|
super(uuid, representations, termType, titleCache, isAllowDuplicates, isOrderRelevant, isFlat);
|
42
|
}
|
43
|
|
44
|
public TermTreeDto(UUID uuid, Set<Representation> representations, TermType termType, TermNode root, String titleCache, boolean isAllowDuplicates, boolean isOrderRelevant, boolean isFlat) {
|
45
|
super(uuid, representations, termType, titleCache, isAllowDuplicates, isOrderRelevant, isFlat);
|
46
|
this.root = new TermNodeDto(null, 0, this, root.getUuid(), root.treeIndex(), root.getPath());
|
47
|
}
|
48
|
|
49
|
public TermNodeDto getRoot() {
|
50
|
return root;
|
51
|
}
|
52
|
|
53
|
public void setRoot(TermNodeDto root) {
|
54
|
this.root = root;
|
55
|
}
|
56
|
|
57
|
public boolean removeChild(TermNodeDto nodeDto){
|
58
|
return this.root.removeChild(nodeDto, true);
|
59
|
}
|
60
|
|
61
|
public static String getTermTreeDtoSelect(){
|
62
|
String[] result = createSqlParts();
|
63
|
|
64
|
return result[0]+result[1]+result[2];
|
65
|
}
|
66
|
|
67
|
private static String[] createSqlParts() {
|
68
|
String sqlSelectString = ""
|
69
|
+ "select a.uuid, "
|
70
|
+ "r, "
|
71
|
+ "a.termType, "
|
72
|
+ "a.uri, "
|
73
|
+ "root, "
|
74
|
+ "a.titleCache, "
|
75
|
+ "a.allowDuplicates, "
|
76
|
+ "a.orderRelevant, "
|
77
|
+ "a.isFlat ";
|
78
|
String sqlFromString = "from TermTree as a ";
|
79
|
|
80
|
String sqlJoinString = "LEFT JOIN a.root as root "
|
81
|
+ "LEFT JOIN a.representations AS r "
|
82
|
;
|
83
|
|
84
|
String[] result = new String[3];
|
85
|
result[0] = sqlSelectString;
|
86
|
result[1] = sqlFromString;
|
87
|
result[2] = sqlJoinString;
|
88
|
return result;
|
89
|
}
|
90
|
|
91
|
public static List<TermTreeDto> termTreeDtoListFrom(List<Object[]> results) {
|
92
|
List<TermTreeDto> dtos = new ArrayList<>(); // list to ensure order
|
93
|
// map to handle multiple representations/media/vocRepresentation because of LEFT JOIN
|
94
|
Map<UUID, TermTreeDto> dtoMap = new HashMap<>(results.size());
|
95
|
for (Object[] elements : results) {
|
96
|
UUID uuid = (UUID)elements[0];
|
97
|
if(dtoMap.containsKey(uuid)){
|
98
|
// multiple results for one term -> multiple (voc) representation/media
|
99
|
if(elements[1]!=null){
|
100
|
dtoMap.get(uuid).addRepresentation((Representation)elements[1]);
|
101
|
}
|
102
|
} else {
|
103
|
// term representation
|
104
|
Set<Representation> representations = new HashSet<>();
|
105
|
if(elements[1] instanceof Representation) {
|
106
|
representations = new HashSet<>(1);
|
107
|
representations.add((Representation)elements[1]);
|
108
|
}
|
109
|
|
110
|
TermTreeDto termTreeDto = new TermTreeDto(
|
111
|
uuid,
|
112
|
representations,
|
113
|
(TermType)elements[2],
|
114
|
(String)elements[5],
|
115
|
(boolean)elements[6],
|
116
|
(boolean)elements[7],
|
117
|
(boolean)elements[8]);
|
118
|
termTreeDto.setUri((URI)elements[3]);
|
119
|
if (termTreeDto.getTermType().equals(TermType.Character)){
|
120
|
termTreeDto.setRoot(CharacterNodeDto.fromTermNode((TermNode<Character>) elements[4], termTreeDto));
|
121
|
}else {
|
122
|
termTreeDto.setRoot(TermNodeDto.fromNode((TermNode)elements[4], termTreeDto));
|
123
|
}
|
124
|
|
125
|
dtoMap.put(uuid, termTreeDto);
|
126
|
dtos.add(termTreeDto);
|
127
|
}
|
128
|
}
|
129
|
return dtos;
|
130
|
}
|
131
|
}
|