View Javadoc
1   package com.kodexa.client;
2   
3   
4   import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5   import com.fasterxml.jackson.annotation.JsonProperty;
6   import lombok.Getter;
7   import lombok.Setter;
8   
9   import java.io.Serializable;
10  import java.util.ArrayList;
11  import java.util.List;
12  import java.util.stream.Collectors;
13  
14  /**
15   * A node in a content tree
16   */
17  @Getter
18  @Setter
19  @JsonIgnoreProperties(ignoreUnknown = true)
20  public class ContentNode implements Serializable {
21  
22      private int index;
23  
24      private ContentNode parent;
25  
26      private Integer parentId;
27  
28      private String uuid;
29  
30      @JsonProperty("node_type")
31      private String type;
32  
33      private String content;
34  
35      @JsonProperty("content_parts")
36      private List<Object> contentParts;
37  
38      private List<ContentFeature> features = new ArrayList<>();
39  
40      private final Document document;
41  
42      public ContentNode(Document document) {
43          this.document = document;
44      }
45  
46      /**
47       * Returns all the contents from this node and all its children
48       *
49       * @param separator
50       * @return
51       */
52      public String getAllContent(String separator) {
53          List<String> allContents = new ArrayList<>();
54          allContents.add(this.content);
55          for (ContentNode child : this.getChildren()) {
56              allContents.add(child.getAllContent(separator));
57          }
58          return String.join(separator, allContents);
59      }
60  
61      public ContentNode getParent() {
62          if (parent != null || parentId == null) {
63              return parent;
64          }
65  
66          parent = document.getPersistanceLayer().getNodeByUuid(String.valueOf(parentId));
67          return parent;
68      }
69  
70      public List<ContentNode> getChildren() {
71          return document.getPersistanceLayer()
72                  .getChildNodes(this);
73      }
74  
75      public ContentFeature addFeature(String featureType, String featureName) {
76          ContentFeatureature">ContentFeature contentFeature = new ContentFeature();
77          contentFeature.setFeatureType(featureType);
78          contentFeature.setName(featureName);
79          getFeatures().add(contentFeature);
80  
81          document.getPersistanceLayer()
82                  .updateNode(this);
83          return contentFeature;
84      }
85  
86      public ContentFeaturecom/kodexa/client/ContentFeature.html#ContentFeature">ContentFeature addFeature(ContentFeature feature) {
87          getFeatures().add(feature);
88          document.getPersistanceLayer()
89                  .updateNode(this);
90          return feature;
91      }
92  
93  
94      public void removeFeature(ContentFeature feature) {
95          setFeatures(getFeatures().stream()
96                  .filter(f -> !(f.getFeatureType()
97                          .equals(feature.getFeatureType()) && f.getName()
98                          .equals(feature.getName())))
99                  .collect(Collectors.toList()));
100         document.getPersistanceLayer()
101                 .updateNode(this);
102     }
103 
104 }