Project

General

Profile

Download (9.17 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<FeatureStateDto> onlyApplicableIf = new HashSet<>();
37
    private Set<FeatureStateDto> 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

    
51
        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());
52
        if (node.getParent() != null){
53
            dto.setParentUuid(node.getParent().getUuid());
54
        }
55
        
56
        List<TermNodeDto> children = new ArrayList<>();
57
        for (Object o: node.getChildNodes()){
58
            if (o instanceof TermNode){
59
                TermNode<?> child = (TermNode<?>)o;
60
                if (child != null){
61
                    if(child.getTerm() != null && child.getTerm().getTermType().equals(TermType.Character)){
62
                        children.add(CharacterNodeDto.fromTermNode((TermNode)child, treeDto));
63
                    }else{
64
                        children.add(TermNodeDto.fromNode(child, treeDto));
65
                    }
66

    
67
                }
68
            }
69
        }
70

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

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

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

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

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

    
109
    }
110

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

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

    
122
/*--------Getter and Setter ---------------*/
123

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

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

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

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

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

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

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

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

    
156
    public Set<FeatureStateDto> getOnlyApplicableIf() {
157
        return onlyApplicableIf;
158
    }
159

    
160
    public void setOnlyApplicableIfDto(Set<FeatureStateDto> onlyApplicableIf) {
161
        this.onlyApplicableIf = onlyApplicableIf;
162
    }
163
    public void setOnlyApplicableIf(Set<FeatureState> onlyApplicableIf) {
164
        if (this.onlyApplicableIf == null){
165
            this.onlyApplicableIf = new HashSet<>();
166
        }
167
        for (FeatureState state: onlyApplicableIf){
168
            this.onlyApplicableIf.add(new FeatureStateDto(state.getUuid(), FeatureDto.fromFeature(state.getFeature()), TermDto.fromTerm(state.getState())));
169
        }
170

    
171
    }
172

    
173
    public Set<FeatureStateDto> getInapplicableIf() {
174
        return inapplicableIf;
175
    }
176

    
177
    public void setInapplicableIfDto(Set<FeatureStateDto> inapplicableIf) {
178
        this.inapplicableIf = inapplicableIf;
179
    }
180

    
181
    public void setInapplicableIf(Set<FeatureState> inApplicableIf) {
182
        if (this.inapplicableIf == null){
183
            this.inapplicableIf = new HashSet<>();
184
        }
185
        for (FeatureState state: inApplicableIf){
186
            this.inapplicableIf.add( new FeatureStateDto(state.getUuid(),FeatureDto.fromFeature(state.getFeature()), TermDto.fromTerm(state.getState())));
187
        }
188

    
189
    }
190

    
191
    public UUID getUuid() {
192
        return uuid;
193
    }
194

    
195
    public void setUuid(UUID uuid) {
196
        this.uuid = uuid;
197
    }
198

    
199
    public TermDto getTerm(){
200
        return term;
201
    }
202

    
203
    public void setTerm(TermDto termDto){
204
        term = termDto;
205
    }
206

    
207
    public TermType getType(){
208
        return type;
209
    }
210

    
211
    public void setTermType(TermType termType){
212
        type = termType;
213
    }
214

    
215
    public int getIndex(TermNodeDto nodeDto) {
216
        int index = 0;
217
        for (TermNodeDto child: children){
218
            if (child != null && child.getUuid() != null && nodeDto.getUuid() != null){
219
                if (child.getUuid().equals(nodeDto.getUuid())){
220
                    return index;
221
                }else if(child != null &&(child.getUuid() == null && nodeDto.getUuid() == null) && child.getTerm().getUuid().equals(nodeDto.getTerm().getUuid())){
222
                    return index;
223
                }
224
            }
225
            index++;
226
        }
227
        return -1;
228
   }
229

    
230
    public String getPath() {
231
        return path;
232
    }
233

    
234
    public void setPath(String path) {
235
        this.path = path;
236
    }
237

    
238
    public boolean removeChild(TermNodeDto nodeDto, boolean doRecursive){
239
       int index = this.getIndex(nodeDto);
240
       if (index > -1){
241
           this.getChildren().remove(index);
242
           this.tree.removeTerm(nodeDto.term);
243
           return true;
244
       }else if (doRecursive && this.getChildren() != null && !this.getChildren().isEmpty()){
245
           for (TermNodeDto child: children){
246
               boolean result = child.removeChild(nodeDto, doRecursive);
247
               if (result){
248
                   return true;
249
               }
250
           }
251
       }
252
       return false;
253
   }
254

    
255

    
256
   public static String getTermNodeDtoSelect(){
257
       String[] result = createSqlParts();
258

    
259
       return result[0]+result[1]+result[2];
260
   }
261

    
262
   private static String[] createSqlParts() {
263
       String sqlSelectString = ""
264
               + " SELECT a.uuid, "
265
               + " r, "
266
               + " a.termType,  "
267
               + " a.uri,  "
268
               + " root,  "
269
               + " a.titleCache, "
270
               + " a.allowDuplicates, "
271
               + " a.orderRelevant, "
272
               + " a.isFlat ";
273
       String sqlFromString = " FROM TermNode as a ";
274

    
275
       String sqlJoinString = " LEFT JOIN a.tree "
276
              + " LEFT JOIN a.representations AS r "
277
               ;
278

    
279
       String[] result = new String[3];
280
       result[0] = sqlSelectString;
281
       result[1] = sqlFromString;
282
       result[2] = sqlJoinString;
283
       return result;
284
   }
285

    
286
   private static String[] createSqlPartsWithTerm() {
287
       String sqlSelectString = ""
288
               + " SELECT a.uuid, "
289
               + " r, "
290
               + " a.term"
291
               + " a.termType,  "
292
               + " a.uri,  "
293
               + " root,  "
294
               + " a.titleCache, "
295
               + " a.allowDuplicates, "
296
               + " a.orderRelevant, "
297
               + " a.isFlat ";
298
       String sqlFromString = " FROM TermNode as a ";
299

    
300
       String sqlJoinString = " LEFT JOIN a.tree "
301
              + " LEFT JOIN a.representations AS r "
302
               ;
303

    
304
       String[] result = new String[3];
305
       result[0] = sqlSelectString;
306
       result[1] = sqlFromString;
307
       result[2] = sqlJoinString;
308
       return result;
309
   }
310
}
(28-28/30)