View Javadoc
1   package com.kodexa.client;
2   
3   import com.fasterxml.jackson.annotation.JsonIgnore;
4   import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5   import com.fasterxml.jackson.annotation.JsonInclude;
6   import com.fasterxml.jackson.annotation.JsonProperty;
7   import com.fasterxml.jackson.core.JsonProcessingException;
8   import com.fasterxml.jackson.databind.ObjectMapper;
9   import lombok.Data;
10  import org.apache.commons.io.FileUtils;
11  import org.apache.commons.lang3.tuple.ImmutablePair;
12  import org.msgpack.jackson.dataformat.MessagePackFactory;
13  
14  import java.io.ByteArrayInputStream;
15  import java.io.File;
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.util.*;
19  
20  /**
21   * A Kodexa document is a representation of a set of content which combines content, metadata
22   * and features it is the core model for handling content
23   */
24  @Data
25  @JsonIgnoreProperties(ignoreUnknown = true)
26  public class Document {
27  
28      private final static ObjectMapper OBJECT_MAPPER;
29  
30      private final static ObjectMapper OBJECT_MAPPER_MSGPACK;
31  
32      public static final String CURRENT_VERSION = "4.0.1";
33  
34      static {
35          OBJECT_MAPPER = new ObjectMapper();
36          OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
37  
38          OBJECT_MAPPER_MSGPACK = new ObjectMapper(new MessagePackFactory());
39          OBJECT_MAPPER_MSGPACK.setSerializationInclusion(JsonInclude.Include.NON_NULL);
40      }
41  
42      public Document() {
43          persistenceLayer = new SqlitePersistenceLayer(this);
44      }
45  
46      public Document(InputStream kddbInputStream) {
47          persistenceLayer = new SqlitePersistenceLayer(kddbInputStream, this);
48          persistenceLayer.loadDocument();
49          this.setVersion(CURRENT_VERSION);
50      }
51  
52      public Document(File kddbFile) {
53          persistenceLayer = new SqlitePersistenceLayer(kddbFile, this);
54          persistenceLayer.loadDocument();
55          this.setVersion(CURRENT_VERSION);
56      }
57  
58      public List<ContentException> getContentExceptions() {
59          return persistenceLayer.getContentExceptions();
60      }
61  
62      @JsonIgnore
63      private SqlitePersistenceLayer persistenceLayer;
64  
65      public long getNodeCountByType(String type) {
66          return persistenceLayer.getNodeCountByType(type);
67      }
68  
69      public List<ContentNode> getTaggedNodes() {
70          return persistenceLayer.getTaggedNodes();
71      }
72  
73      public ContentNode getNodeByUuid(String uuid) {
74          return persistenceLayer.getNodeByUuid(uuid);
75      }
76  
77      public List<ContentNode> getTaggedNodeByTagUuid(String tagUuid) {
78          return persistenceLayer.getTaggedNodeByTagUuid(tagUuid);
79      }
80  
81      private Map<String, Object> metadata = new HashMap<>();
82  
83      @JsonProperty("source")
84      private SourceMetadataourceMetadata">SourceMetadata source = new SourceMetadata();
85  
86      @JsonProperty("content_node")
87      private ContentNode contentNode;
88  
89      private boolean virtual = false;
90  
91      private List<String> mixins = new ArrayList<>();
92  
93      private List<String> labels = new ArrayList<>();
94  
95      private List<String> taxonomies = new ArrayList<>();
96  
97      private List<ContentClassification> classes = new ArrayList<>();
98  
99      private String uuid = UUID.randomUUID()
100                               .toString();
101 
102     private String version = Document.CURRENT_VERSION;
103 
104     /**
105      * Create a new instance of a Document from JSON string
106      *
107      * @param json String representation of the document JSON
108      * @return De-serialized Document
109      */
110     @Deprecated
111     public static Document fromJson(String json) {
112         try {
113             return OBJECT_MAPPER.readValue(json, Document.class);
114         }
115         catch (JsonProcessingException e) {
116             throw new KodexaException("Unable to convert to Document from JSON", e);
117         }
118     }
119 
120     /**
121      * Create a Document from a message packed (kdx) representation
122      *
123      * @param bytes the bytes for the message packed document
124      * @return An instance of a Document
125      */
126     @Deprecated
127     public static Document fromMsgPack(byte[] bytes) {
128         try {
129             return OBJECT_MAPPER_MSGPACK.readValue(new ByteArrayInputStream(bytes), Document.class);
130         }
131         catch (IOException e) {
132             throw new KodexaException("Unable to convert to Document from message pack", e);
133         }
134     }
135 
136     /**
137      * Create a Document from a message packed (kdx) representation
138      *
139      * @param is Input stream containing the document
140      * @return An instance of a Document
141      */
142     @Deprecated
143     public static Document fromMsgPack(InputStream is) {
144         try {
145             return OBJECT_MAPPER_MSGPACK.readValue(is, Document.class);
146         }
147         catch (IOException e) {
148             throw new KodexaException("Unable to convert to Document from message pack", e);
149         }
150     }
151 
152     /**
153      * Create a Document from a message packed (kdx) representation
154      *
155      * @param file file containing the document
156      * @return An instance of a Document
157      */
158     @Deprecated
159     public static Document fromMsgPack(File file) {
160         try {
161             return OBJECT_MAPPER_MSGPACK.readValue(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)), Document.class);
162         }
163         catch (IOException e) {
164             throw new KodexaException("Unable to convert to Document from message pack", e);
165         }
166     }
167 
168     public static Document fromText(String text) {
169         Documentnt">Document newDocument = new Document();
170         newDocument.setContentNode(newDocument.createContentNode("text", text));
171         newDocument.getMixins()
172                    .add("text");
173         return newDocument;
174     }
175 
176     protected SqlitePersistenceLayer getPersistanceLayer() {
177         return persistenceLayer;
178     }
179 
180     @SuppressWarnings("unchecked")
181     public static Document fromUrl(String url) {
182         Documentnt">Document newDocument = new Document();
183         newDocument.getMetadata()
184                    .put("connector", "url");
185         newDocument.getMetadata()
186                    .put("connector_options", new HashMap<String, Object>());
187         ((Map) newDocument.getMetadata()
188                           .get("connector_options")).put("url", url);
189 
190         SourceMetadataadata">SourceMetadata sourceMetadata = new SourceMetadata();
191         sourceMetadata.setConnector("url");
192         sourceMetadata.setOriginalPath(url);
193 
194         newDocument.setSource(sourceMetadata);
195         return newDocument;
196     }
197 
198     public static Document fromKddb(File file) {
199         return new Document(file);
200     }
201 
202     public static Document fromInputStream(InputStream inputStream) {
203         return new Document(inputStream);
204     }
205 
206     public static Document fromBytes(byte[] bytes) {
207         return new Document(new ByteArrayInputStream(bytes));
208     }
209 
210     public byte[] toBytes() {
211         return persistenceLayer.toBytes();
212     }
213 
214     public ImmutablePair<InputStream, Long> toInputStream() {
215         return persistenceLayer.toInputStream();
216     }
217 
218     public void close() {
219         persistenceLayer.close();
220     }
221 
222     /**
223      * Add the given label to the document
224      *
225      * @param label the label to add
226      * @return the instance of the document
227      */
228     public Document addLabel(String label) {
229         labels.add(label);
230         return this;
231     }
232 
233     /**
234      * Remove the given label to the document
235      *
236      * @param label the label to remove
237      * @return the instance of the document
238      */
239     public Document removeLabel(String label) {
240         labels.remove(label);
241         return this;
242     }
243 
244     /**
245      * Convert the document to JSON
246      *
247      * @param pretty include spacing and new lines if true
248      * @return JSON representation of document
249      */
250     @Deprecated
251     public String toJson(boolean pretty) {
252         try {
253             if (pretty) {
254                 return OBJECT_MAPPER.writerWithDefaultPrettyPrinter()
255                                     .writeValueAsString(this);
256             }
257             else {
258                 return OBJECT_MAPPER.writeValueAsString(this);
259             }
260         }
261         catch (JsonProcessingException e) {
262             throw new KodexaException("Unable to convert Document to JSON", e);
263         }
264     }
265 
266     /**
267      * Create a message pack representation of this document
268      *
269      * @return Byte array of the document packed
270      */
271     @Deprecated
272     public byte[] toMsgPack() {
273         try {
274             return OBJECT_MAPPER_MSGPACK.writeValueAsBytes(this);
275         }
276         catch (JsonProcessingException e) {
277             throw new KodexaException("Unable to write this document to message pack", e);
278         }
279     }
280 
281     /**
282      * Create a JSON representation of this document
283      *
284      * @return String containing the JSON representation
285      */
286     @Deprecated
287     public String toJson() {
288         return toJson(false);
289     }
290 
291     public ContentNodel#ContentNode">ContentNode createContentNode(String type, String content, ContentNode parent, int index) {
292         ContentNodetNode">ContentNode contentNode = new ContentNode(this);
293         contentNode.setType(type);
294         contentNode.setContent(content);
295         contentNode.setIndex(index);
296         contentNode.setContentParts(List.of(content));
297 
298         if (parent != null) {
299             contentNode.setParent(parent);
300             contentNode.setParentId(Integer.valueOf(parent.getUuid()));
301         }
302 
303         persistenceLayer.updateNode(contentNode);
304         return contentNode;
305     }
306 
307     public ContentNode createContentNode(String type, String content) {
308         return createContentNode(type, content, null,0);
309     }
310 
311     public void addMixin(String spatial) {
312         if (!this.mixins.contains(spatial)) {
313             this.mixins.add(spatial);
314         }
315     }
316 
317     public int getNumberOfInsights() {
318         return persistenceLayer.getNumberOfInsights();
319     }
320 }