Project

General

Profile

Download (8.56 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
    UUID parentUuid;
34
    String treeIndex;
35
    List<TermNodeDto> children;
36
    Set<FeatureState> onlyApplicableIf = new HashSet<>();
37
    Set<FeatureState> inapplicableIf = new HashSet<>();
38
    UUID uuid;
39
    TermDto term;
40
    TermType type;
41
    TermTreeDto tree;
42
    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
//        TermTreeDto tree = node.getGraph() != null? TermTreeDto.fromTree(HibernateProxyHelper.deproxy(node.getGraph(), TermTree.class)):null;
48
        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());
49
//        uuid = node.getUuid();
50
        if (node.getParent() != null){
51
            dto.setParentUuid(node.getParent().getUuid());
52
        }
53
//        TermTree termTree = HibernateProxyHelper.deproxy(node.getGraph(), TermTree.class);
54
//        tree = TermTreeDto.fromTree(termTree);
55
//        treeIndex = node.treeIndex();
56
        List<TermNodeDto> children = new ArrayList();
57
        for (Object o: node.getChildNodes()){
58
            if (o instanceof TermNode){
59
                TermNode child = (TermNode)o;
60

    
61
                if (child != null){
62
                    if(child.getTerm().getTermType().equals(TermType.Character)){
63
                        children.add(CharacterNodeDto.fromTermNode(child, treeDto));
64
                    }else{
65
                        children.add(TermNodeDto.fromNode(child, treeDto));
66
                    }
67
                }
68
            }
69
        }
70
        dto.setChildren(children);
71
        dto.setOnlyApplicableIf(node.getOnlyApplicableIf());
72
        dto.setInapplicableIf(node.getInapplicableIf());
73
//        if (node.getTerm() != null){
74
//            term = TermDto.fromTerm(node.getTerm());
75
//        }
76
        dto.setTermType(node.getTermType());
77
//        path = node.getPath();
78
        return dto;
79
    }
80

    
81

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

    
100
    }
101

    
102
    public TermNodeDto(TermDto termDto, UUID parentUuid, int position, TermTreeDto treeDto, UUID uuid, String treeIndex, String path){
103
        this.uuid = uuid;
104
        this.parentUuid = parentUuid;
105

    
106
        this.treeIndex = treeIndex;
107
        term = termDto;
108
        type = termDto!= null? termDto.getTermType(): null;
109
        children = new ArrayList<>();
110
//        if (parent != null){
111
//            parent.getChildren().add(position, this);
112
//        }
113
        tree = treeDto;
114
        this.path = path;
115

    
116
    }
117

    
118
    public TermNodeDto(TermDto termDto, int position, TermTreeDto treeDto, UUID uuid, String treeIndex, String path){
119
        this.uuid = uuid;
120
        this.treeIndex = treeIndex;
121
        term = termDto;
122
        type = termDto!= null? termDto.getTermType(): null;
123
        children = new ArrayList<>();
124

    
125
        tree = treeDto;
126
        this.path = path;
127

    
128
    }
129

    
130
/*--------Getter and Setter ---------------*/
131

    
132
    public UUID getParentUuid() {
133
        return parentUuid;
134
    }
135

    
136
    public void setParentUuid(UUID parentUuid) {
137
        this.parentUuid = parentUuid;
138
    }
139

    
140
    public String getTreeIndex() {
141
        return treeIndex;
142
    }
143

    
144
    public void setTreeIndex(String treeIndex) {
145
        this.treeIndex = treeIndex;
146
    }
147

    
148
    public TermTreeDto getTree() {
149
        return tree;
150
    }
151

    
152
    public void setTree(TermTreeDto tree) {
153
        this.tree = tree;
154
    }
155

    
156
    public List<TermNodeDto> getChildren() {
157
        return children;
158
    }
159

    
160
    public void setChildren(List<TermNodeDto> children) {
161
        this.children = children;
162
    }
163

    
164
    public Set<FeatureState> getOnlyApplicableIf() {
165
        return onlyApplicableIf;
166
    }
167

    
168
    public void setOnlyApplicableIf(Set<FeatureState> onlyApplicableIf) {
169
        this.onlyApplicableIf = onlyApplicableIf;
170
    }
171

    
172
    public Set<FeatureState> getInapplicableIf() {
173
        return inapplicableIf;
174
    }
175

    
176
    public void setInapplicableIf(Set<FeatureState> inapplicableIf) {
177
        this.inapplicableIf = inapplicableIf;
178
    }
179

    
180
    public UUID getUuid() {
181
        return uuid;
182
    }
183

    
184
    public void setUuid(UUID uuid) {
185
        this.uuid = uuid;
186
    }
187

    
188
    public TermDto getTerm(){
189
        return term;
190
    }
191

    
192
    public void setTerm(TermDto termDto){
193
        term = termDto;
194
    }
195

    
196
    public TermType getType(){
197
        return type;
198
    }
199

    
200
    public void setTermType(TermType termType){
201
        type = termType;
202
    }
203

    
204
    public int getIndex(TermNodeDto nodeDto) {
205
        int index = 0;
206
        for (TermNodeDto child: children){
207
            if (child != null){
208
                if (child.getUuid().equals(nodeDto.getUuid())){
209
                    return index;
210
                }
211
            }
212
            index++;
213
        }
214
        return -1;
215
   }
216

    
217
   /**
218
     * @return the path
219
     */
220
    public String getPath() {
221
        return path;
222
    }
223

    
224

    
225
    /**
226
     * @param path the path to set
227
     */
228
    public void setPath(String path) {
229
        this.path = path;
230
    }
231

    
232

    
233
public boolean removeChild(TermNodeDto nodeDto, boolean doRecursive){
234
       int index = this.getIndex(nodeDto);
235
       if (index > -1){
236
           this.getChildren().remove(index);
237
           return true;
238
       }else if (doRecursive && this.getChildren() != null && !this.getChildren().isEmpty()){
239
           for (TermNodeDto child: children){
240
               boolean result = child.removeChild(nodeDto, doRecursive);
241
               if (result){
242
                   return true;
243
               }
244
           }
245
       }
246
       return false;
247
   }
248

    
249

    
250
   public static String getTermNodeDtoSelect(){
251
       String[] result = createSqlParts();
252

    
253
       return result[0]+result[1]+result[2];
254
   }
255

    
256
   /**
257
    * @param fromTable
258
    * @return
259
    */
260
   private static String[] createSqlParts() {
261
       String sqlSelectString = ""
262
               + "select a.uuid, "
263
               + "r, "
264
               + "a.termType,  "
265
               + "a.uri,  "
266
               + "root,  "
267
               + "a.titleCache, "
268
               + "a.allowDuplicates, "
269
               + "a.orderRelevant, "
270
               + "a.isFlat ";
271
       String sqlFromString =   "from TermNode as a ";
272

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

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

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

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

    
302
       String[] result = new String[3];
303
       result[0] = sqlSelectString;
304
       result[1] = sqlFromString;
305
       result[2] = sqlJoinString;
306
       return result;
307
   }
308
}
(21-21/24)