Project

General

Profile

Download (8.35 KB) Statistics
| Branch: | Tag: | Revision:
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.io.Serializable;
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16
import java.util.UUID;
17

    
18
import org.springframework.util.Assert;
19

    
20
import eu.etaxonomy.cdm.model.description.FeatureState;
21
import eu.etaxonomy.cdm.model.term.TermNode;
22
import eu.etaxonomy.cdm.model.term.TermTree;
23
import eu.etaxonomy.cdm.model.term.TermType;
24

    
25
/**
26
 * @author k.luther
27
 * @since Mar 18, 2020
28
 */
29
public class TermNodeDto implements Serializable{
30

    
31
    private static final long serialVersionUID = 7568459208397248126L;
32

    
33
    private UUID parentUuid;
34
    private String treeIndex;
35
    private List<TermNodeDto> children;
36
    private Set<FeatureState> onlyApplicableIf = new HashSet<>();
37
    private Set<FeatureState> inapplicableIf = new HashSet<>();
38
    private UUID uuid;
39
    private TermDto term;
40
    private TermType type;
41
    private TermTreeDto tree;
42
    private String path;
43

    
44
    public static TermNodeDto fromNode(TermNode node, TermTreeDto treeDto){
45
        Assert.notNull(node, "Node should not be null");
46
        TermDto term = node.getTerm() != null?TermDto.fromTerm(node.getTerm()): null;
47
        if (treeDto != null){
48
            treeDto.addTerm(term);
49
        }
50
        TermNodeDto dto = new TermNodeDto(term, node.getParent() != null? node.getParent().getIndex(node): 0, treeDto != null? treeDto: TermTreeDto.fromTree((TermTree)node.getGraph()), node.getUuid(), node.treeIndex(), node.getPath());
51
        if (node.getParent() != null){
52
            dto.setParentUuid(node.getParent().getUuid());
53
        }
54

    
55
        List<TermNodeDto> children = new ArrayList<>();
56
        for (Object o: node.getChildNodes()){
57
            if (o instanceof TermNode){
58
                TermNode<?> child = (TermNode<?>)o;
59
                if (child != null){
60
                    if(child.getTerm() != null && child.getTerm().getTermType().equals(TermType.Character)){
61
                        children.add(CharacterNodeDto.fromTermNode((TermNode)child, treeDto));
62
                    }else{
63
                        children.add(TermNodeDto.fromNode(child, treeDto));
64
                    }
65

    
66
                }
67
            }
68
        }
69

    
70
        dto.setChildren(children);
71
        dto.setOnlyApplicableIf(node.getOnlyApplicableIf());
72
        dto.setInapplicableIf(node.getInapplicableIf());
73
        dto.setTermType(node.getTermType());
74
        return dto;
75
    }
76

    
77
    public TermNodeDto(TermDto termDto, TermNodeDto parent, int position, TermTreeDto treeDto, UUID uuid, String treeIndex, String path){
78
        this.uuid = uuid;
79
        if (parent != null){
80
            parentUuid = parent.getUuid();
81
        }
82
        this.treeIndex = treeIndex;
83
        term = termDto;
84
        type = termDto!= null? termDto.getTermType(): null;
85
        children = new ArrayList<>();
86
        if (parent != null){
87
            parent.getChildren().add(position, this);
88
        }
89
        tree = treeDto;
90
        if (tree != null){
91
            tree.addTerm(termDto);
92
        }
93
        this.path = path;
94
    }
95

    
96
    public TermNodeDto(TermDto termDto, UUID parentUuid, int position, TermTreeDto treeDto, UUID uuid, String treeIndex, String path){
97
        this.uuid = uuid;
98
        this.parentUuid = parentUuid;
99

    
100
        this.treeIndex = treeIndex;
101
        term = termDto;
102
        type = termDto!= null? termDto.getTermType(): null;
103
        children = new ArrayList<>();
104

    
105
        tree = treeDto;
106
        this.path = path;
107

    
108
    }
109

    
110
    public TermNodeDto(TermDto termDto, int position, TermTreeDto treeDto, UUID uuid, String treeIndex, String path){
111
        this.uuid = uuid;
112
        this.treeIndex = treeIndex;
113
        term = termDto;
114
        type = termDto!= null? termDto.getTermType(): null;
115
        children = new ArrayList<>();
116

    
117
        tree = treeDto;
118
        this.path = path;
119
    }
120

    
121
/*--------Getter and Setter ---------------*/
122

    
123
    public UUID getParentUuid() {
124
        return parentUuid;
125
    }
126

    
127
    public void setParentUuid(UUID parentUuid) {
128
        this.parentUuid = parentUuid;
129
    }
130

    
131
    public String getTreeIndex() {
132
        return treeIndex;
133
    }
134

    
135
    public void setTreeIndex(String treeIndex) {
136
        this.treeIndex = treeIndex;
137
    }
138

    
139
    public TermTreeDto getTree() {
140
        return tree;
141
    }
142

    
143
    public void setTree(TermTreeDto tree) {
144
        this.tree = tree;
145
    }
146

    
147
    public List<TermNodeDto> getChildren() {
148
        return children;
149
    }
150

    
151
    public void setChildren(List<TermNodeDto> children) {
152
        this.children = children;
153
    }
154

    
155
    public Set<FeatureState> getOnlyApplicableIf() {
156
        return onlyApplicableIf;
157
    }
158

    
159
    public void setOnlyApplicableIf(Set<FeatureState> onlyApplicableIf) {
160
        this.onlyApplicableIf = onlyApplicableIf;
161
    }
162

    
163
    public Set<FeatureState> getInapplicableIf() {
164
        return inapplicableIf;
165
    }
166

    
167
    public void setInapplicableIf(Set<FeatureState> inapplicableIf) {
168
        this.inapplicableIf = inapplicableIf;
169
    }
170

    
171
    public UUID getUuid() {
172
        return uuid;
173
    }
174

    
175
    public void setUuid(UUID uuid) {
176
        this.uuid = uuid;
177
    }
178

    
179
    public TermDto getTerm(){
180
        return term;
181
    }
182

    
183
    public void setTerm(TermDto termDto){
184
        term = termDto;
185
    }
186

    
187
    public TermType getType(){
188
        return type;
189
    }
190

    
191
    public void setTermType(TermType termType){
192
        type = termType;
193
    }
194

    
195
    public int getIndex(TermNodeDto nodeDto) {
196
        int index = 0;
197
        for (TermNodeDto child: children){
198
            if (child != null && child.getUuid() != null && nodeDto.getUuid() != null){
199
                if (child.getUuid().equals(nodeDto.getUuid())){
200
                    return index;
201
                }else if(child != null &&(child.getUuid() == null && nodeDto.getUuid() == null) && child.getTerm().getUuid().equals(nodeDto.getTerm().getUuid())){
202
                    return index;
203
                }
204
            }
205
            index++;
206
        }
207
        return -1;
208
   }
209

    
210
    public String getPath() {
211
        return path;
212
    }
213

    
214
    public void setPath(String path) {
215
        this.path = path;
216
    }
217

    
218
    public boolean removeChild(TermNodeDto nodeDto, boolean doRecursive){
219
       int index = this.getIndex(nodeDto);
220
       if (index > -1){
221
           this.getChildren().remove(index);
222
           this.tree.removeTerm(nodeDto.term);
223
           return true;
224
       }else if (doRecursive && this.getChildren() != null && !this.getChildren().isEmpty()){
225
           for (TermNodeDto child: children){
226
               boolean result = child.removeChild(nodeDto, doRecursive);
227
               if (result){
228
                   return true;
229
               }
230
           }
231
       }
232
       return false;
233
   }
234

    
235

    
236
   public static String getTermNodeDtoSelect(){
237
       String[] result = createSqlParts();
238

    
239
       return result[0]+result[1]+result[2];
240
   }
241

    
242
   private static String[] createSqlParts() {
243
       String sqlSelectString = ""
244
               + " SELECT a.uuid, "
245
               + " r, "
246
               + " a.termType,  "
247
               + " a.uri,  "
248
               + " root,  "
249
               + " a.titleCache, "
250
               + " a.allowDuplicates, "
251
               + " a.orderRelevant, "
252
               + " a.isFlat ";
253
       String sqlFromString = " FROM TermNode as a ";
254

    
255
       String sqlJoinString = " LEFT JOIN a.tree "
256
              + " LEFT JOIN a.representations AS r "
257
               ;
258

    
259
       String[] result = new String[3];
260
       result[0] = sqlSelectString;
261
       result[1] = sqlFromString;
262
       result[2] = sqlJoinString;
263
       return result;
264
   }
265

    
266
   private static String[] createSqlPartsWithTerm() {
267
       String sqlSelectString = ""
268
               + " SELECT a.uuid, "
269
               + " r, "
270
               + " a.term"
271
               + " a.termType,  "
272
               + " a.uri,  "
273
               + " root,  "
274
               + " a.titleCache, "
275
               + " a.allowDuplicates, "
276
               + " a.orderRelevant, "
277
               + " a.isFlat ";
278
       String sqlFromString = " FROM TermNode as a ";
279

    
280
       String sqlJoinString = " LEFT JOIN a.tree "
281
              + " LEFT JOIN a.representations AS r "
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
}
(26-26/29)