001// Generated by delombok at Fri Sep 03 18:26:10 UTC 2021 002package com.kodexa.client; 003 004import com.fasterxml.jackson.annotation.JsonIgnore; 005import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 006import com.fasterxml.jackson.annotation.JsonInclude; 007import com.fasterxml.jackson.annotation.JsonProperty; 008import com.fasterxml.jackson.core.JsonProcessingException; 009import com.fasterxml.jackson.databind.ObjectMapper; 010import org.apache.commons.io.FileUtils; 011import org.apache.commons.lang3.tuple.ImmutablePair; 012import org.msgpack.jackson.dataformat.MessagePackFactory; 013import java.io.ByteArrayInputStream; 014import java.io.File; 015import java.io.IOException; 016import java.io.InputStream; 017import java.util.*; 018 019/** 020 * A Kodexa document is a representation of a set of content which combines content, metadata 021 * and features it is the core model for handling content 022 */ 023@JsonIgnoreProperties(ignoreUnknown = true) 024public class Document { 025 private static final ObjectMapper OBJECT_MAPPER; 026 private static final ObjectMapper OBJECT_MAPPER_MSGPACK; 027 public static final String CURRENT_VERSION = "4.0.1"; 028 029 static { 030 OBJECT_MAPPER = new ObjectMapper(); 031 OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); 032 OBJECT_MAPPER_MSGPACK = new ObjectMapper(new MessagePackFactory()); 033 OBJECT_MAPPER_MSGPACK.setSerializationInclusion(JsonInclude.Include.NON_NULL); 034 } 035 036 public Document() { 037 persistenceLayer = new SqlitePersistenceLayer(this); 038 } 039 040 public Document(InputStream kddbInputStream) { 041 persistenceLayer = new SqlitePersistenceLayer(kddbInputStream, this); 042 persistenceLayer.loadDocument(); 043 this.setVersion(CURRENT_VERSION); 044 } 045 046 public Document(File kddbFile) { 047 persistenceLayer = new SqlitePersistenceLayer(kddbFile, this); 048 persistenceLayer.loadDocument(); 049 this.setVersion(CURRENT_VERSION); 050 } 051 052 @JsonIgnore 053 private SqlitePersistenceLayer persistenceLayer; 054 055 public long getNodeCountByType(String type) { 056 return persistenceLayer.getNodeCountByType(type); 057 } 058 059 public List<ContentNode> getTaggedNodes() { 060 return persistenceLayer.getTaggedNodes(); 061 } 062 063 public ContentNode getNodeByUuid(String uuid) { 064 return persistenceLayer.getNodeByUuid(uuid); 065 } 066 067 public List<ContentNode> getTaggedNodeByTagUuid(String tagUuid) { 068 return persistenceLayer.getTaggedNodeByTagUuid(tagUuid); 069 } 070 071 private Map<String, Object> metadata = new HashMap<>(); 072 @JsonProperty("source") 073 private SourceMetadata source = new SourceMetadata(); 074 @JsonProperty("content_node") 075 private ContentNode contentNode; 076 private boolean virtual = false; 077 private List<String> mixins = new ArrayList<>(); 078 private List<String> labels = new ArrayList<>(); 079 private List<String> taxonomies = new ArrayList<>(); 080 private List<ContentClassification> classes = new ArrayList<>(); 081 private String uuid = UUID.randomUUID().toString(); 082 private String version = Document.CURRENT_VERSION; 083 084 /** 085 * Create a new instance of a Document from JSON string 086 * 087 * @param json String representation of the document JSON 088 * @return De-serialized Document 089 */ 090 @Deprecated 091 public static Document fromJson(String json) { 092 try { 093 return OBJECT_MAPPER.readValue(json, Document.class); 094 } catch (JsonProcessingException e) { 095 throw new KodexaException("Unable to convert to Document from JSON", e); 096 } 097 } 098 099 /** 100 * Create a Document from a message packed (kdx) representation 101 * 102 * @param bytes the bytes for the message packed document 103 * @return An instance of a Document 104 */ 105 @Deprecated 106 public static Document fromMsgPack(byte[] bytes) { 107 try { 108 return OBJECT_MAPPER_MSGPACK.readValue(new ByteArrayInputStream(bytes), Document.class); 109 } catch (IOException e) { 110 throw new KodexaException("Unable to convert to Document from message pack", e); 111 } 112 } 113 114 /** 115 * Create a Document from a message packed (kdx) representation 116 * 117 * @param is Input stream containing the document 118 * @return An instance of a Document 119 */ 120 @Deprecated 121 public static Document fromMsgPack(InputStream is) { 122 try { 123 return OBJECT_MAPPER_MSGPACK.readValue(is, Document.class); 124 } catch (IOException e) { 125 throw new KodexaException("Unable to convert to Document from message pack", e); 126 } 127 } 128 129 /** 130 * Create a Document from a message packed (kdx) representation 131 * 132 * @param file file containing the document 133 * @return An instance of a Document 134 */ 135 @Deprecated 136 public static Document fromMsgPack(File file) { 137 try { 138 return OBJECT_MAPPER_MSGPACK.readValue(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)), Document.class); 139 } catch (IOException e) { 140 throw new KodexaException("Unable to convert to Document from message pack", e); 141 } 142 } 143 144 public static Document fromText(String text) { 145 Document newDocument = new Document(); 146 newDocument.setContentNode(newDocument.createContentNode("text", text)); 147 newDocument.getMixins().add("text"); 148 return newDocument; 149 } 150 151 protected SqlitePersistenceLayer getPersistanceLayer() { 152 return persistenceLayer; 153 } 154 155 @SuppressWarnings("unchecked") 156 public static Document fromUrl(String url) { 157 Document newDocument = new Document(); 158 newDocument.getMetadata().put("connector", "url"); 159 newDocument.getMetadata().put("connector_options", new HashMap<String, Object>()); 160 ((Map) newDocument.getMetadata().get("connector_options")).put("url", url); 161 SourceMetadata sourceMetadata = new SourceMetadata(); 162 sourceMetadata.setConnector("url"); 163 sourceMetadata.setOriginalPath(url); 164 newDocument.setSource(sourceMetadata); 165 return newDocument; 166 } 167 168 public static Document fromKddb(File file) { 169 return new Document(file); 170 } 171 172 public static Document fromInputStream(InputStream inputStream) { 173 return new Document(inputStream); 174 } 175 176 public static Document fromBytes(byte[] bytes) { 177 return new Document(new ByteArrayInputStream(bytes)); 178 } 179 180 public byte[] toBytes() { 181 return persistenceLayer.toBytes(); 182 } 183 184 public ImmutablePair<InputStream, Long> toInputStream() { 185 return persistenceLayer.toInputStream(); 186 } 187 188 public void close() { 189 persistenceLayer.close(); 190 } 191 192 /** 193 * Add the given label to the document 194 * 195 * @param label the label to add 196 * @return the instance of the document 197 */ 198 public Document addLabel(String label) { 199 labels.add(label); 200 return this; 201 } 202 203 /** 204 * Remove the given label to the document 205 * 206 * @param label the label to remove 207 * @return the instance of the document 208 */ 209 public Document removeLabel(String label) { 210 labels.remove(label); 211 return this; 212 } 213 214 /** 215 * Convert the document to JSON 216 * 217 * @param pretty include spacing and new lines if true 218 * @return JSON representation of document 219 */ 220 @Deprecated 221 public String toJson(boolean pretty) { 222 try { 223 if (pretty) { 224 return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(this); 225 } else { 226 return OBJECT_MAPPER.writeValueAsString(this); 227 } 228 } catch (JsonProcessingException e) { 229 throw new KodexaException("Unable to convert Document to JSON", e); 230 } 231 } 232 233 /** 234 * Create a message pack representation of this document 235 * 236 * @return Byte array of the document packed 237 */ 238 @Deprecated 239 public byte[] toMsgPack() { 240 try { 241 return OBJECT_MAPPER_MSGPACK.writeValueAsBytes(this); 242 } catch (JsonProcessingException e) { 243 throw new KodexaException("Unable to write this document to message pack", e); 244 } 245 } 246 247 /** 248 * Create a JSON representation of this document 249 * 250 * @return String containing the JSON representation 251 */ 252 @Deprecated 253 public String toJson() { 254 return toJson(false); 255 } 256 257 public ContentNode createContentNode(String type, String content) { 258 ContentNode contentNode = new ContentNode(this); 259 contentNode.setType(type); 260 contentNode.setContent(content); 261 persistenceLayer.updateNode(contentNode); 262 return contentNode; 263 } 264 265 public void addMixin(String spatial) { 266 if (!this.mixins.contains(spatial)) this.mixins.add(spatial); 267 } 268 269 @java.lang.SuppressWarnings("all") 270 public SqlitePersistenceLayer getPersistenceLayer() { 271 return this.persistenceLayer; 272 } 273 274 @java.lang.SuppressWarnings("all") 275 public Map<String, Object> getMetadata() { 276 return this.metadata; 277 } 278 279 @java.lang.SuppressWarnings("all") 280 public SourceMetadata getSource() { 281 return this.source; 282 } 283 284 @java.lang.SuppressWarnings("all") 285 public ContentNode getContentNode() { 286 return this.contentNode; 287 } 288 289 @java.lang.SuppressWarnings("all") 290 public boolean isVirtual() { 291 return this.virtual; 292 } 293 294 @java.lang.SuppressWarnings("all") 295 public List<String> getMixins() { 296 return this.mixins; 297 } 298 299 @java.lang.SuppressWarnings("all") 300 public List<String> getLabels() { 301 return this.labels; 302 } 303 304 @java.lang.SuppressWarnings("all") 305 public List<String> getTaxonomies() { 306 return this.taxonomies; 307 } 308 309 @java.lang.SuppressWarnings("all") 310 public List<ContentClassification> getClasses() { 311 return this.classes; 312 } 313 314 @java.lang.SuppressWarnings("all") 315 public String getUuid() { 316 return this.uuid; 317 } 318 319 @java.lang.SuppressWarnings("all") 320 public String getVersion() { 321 return this.version; 322 } 323 324 @java.lang.SuppressWarnings("all") 325 public void setPersistenceLayer(final SqlitePersistenceLayer persistenceLayer) { 326 this.persistenceLayer = persistenceLayer; 327 } 328 329 @java.lang.SuppressWarnings("all") 330 public void setMetadata(final Map<String, Object> metadata) { 331 this.metadata = metadata; 332 } 333 334 @JsonProperty("source") 335 @java.lang.SuppressWarnings("all") 336 public void setSource(final SourceMetadata source) { 337 this.source = source; 338 } 339 340 @JsonProperty("content_node") 341 @java.lang.SuppressWarnings("all") 342 public void setContentNode(final ContentNode contentNode) { 343 this.contentNode = contentNode; 344 } 345 346 @java.lang.SuppressWarnings("all") 347 public void setVirtual(final boolean virtual) { 348 this.virtual = virtual; 349 } 350 351 @java.lang.SuppressWarnings("all") 352 public void setMixins(final List<String> mixins) { 353 this.mixins = mixins; 354 } 355 356 @java.lang.SuppressWarnings("all") 357 public void setLabels(final List<String> labels) { 358 this.labels = labels; 359 } 360 361 @java.lang.SuppressWarnings("all") 362 public void setTaxonomies(final List<String> taxonomies) { 363 this.taxonomies = taxonomies; 364 } 365 366 @java.lang.SuppressWarnings("all") 367 public void setClasses(final List<ContentClassification> classes) { 368 this.classes = classes; 369 } 370 371 @java.lang.SuppressWarnings("all") 372 public void setUuid(final String uuid) { 373 this.uuid = uuid; 374 } 375 376 @java.lang.SuppressWarnings("all") 377 public void setVersion(final String version) { 378 this.version = version; 379 } 380 381 @java.lang.Override 382 @java.lang.SuppressWarnings("all") 383 public boolean equals(final java.lang.Object o) { 384 if (o == this) return true; 385 if (!(o instanceof Document)) return false; 386 final Document other = (Document) o; 387 if (!other.canEqual((java.lang.Object) this)) return false; 388 final java.lang.Object this$persistenceLayer = this.getPersistenceLayer(); 389 final java.lang.Object other$persistenceLayer = other.getPersistenceLayer(); 390 if (this$persistenceLayer == null ? other$persistenceLayer != null : !this$persistenceLayer.equals(other$persistenceLayer)) return false; 391 final java.lang.Object this$metadata = this.getMetadata(); 392 final java.lang.Object other$metadata = other.getMetadata(); 393 if (this$metadata == null ? other$metadata != null : !this$metadata.equals(other$metadata)) return false; 394 final java.lang.Object this$source = this.getSource(); 395 final java.lang.Object other$source = other.getSource(); 396 if (this$source == null ? other$source != null : !this$source.equals(other$source)) return false; 397 final java.lang.Object this$contentNode = this.getContentNode(); 398 final java.lang.Object other$contentNode = other.getContentNode(); 399 if (this$contentNode == null ? other$contentNode != null : !this$contentNode.equals(other$contentNode)) return false; 400 if (this.isVirtual() != other.isVirtual()) return false; 401 final java.lang.Object this$mixins = this.getMixins(); 402 final java.lang.Object other$mixins = other.getMixins(); 403 if (this$mixins == null ? other$mixins != null : !this$mixins.equals(other$mixins)) return false; 404 final java.lang.Object this$labels = this.getLabels(); 405 final java.lang.Object other$labels = other.getLabels(); 406 if (this$labels == null ? other$labels != null : !this$labels.equals(other$labels)) return false; 407 final java.lang.Object this$taxonomies = this.getTaxonomies(); 408 final java.lang.Object other$taxonomies = other.getTaxonomies(); 409 if (this$taxonomies == null ? other$taxonomies != null : !this$taxonomies.equals(other$taxonomies)) return false; 410 final java.lang.Object this$classes = this.getClasses(); 411 final java.lang.Object other$classes = other.getClasses(); 412 if (this$classes == null ? other$classes != null : !this$classes.equals(other$classes)) return false; 413 final java.lang.Object this$uuid = this.getUuid(); 414 final java.lang.Object other$uuid = other.getUuid(); 415 if (this$uuid == null ? other$uuid != null : !this$uuid.equals(other$uuid)) return false; 416 final java.lang.Object this$version = this.getVersion(); 417 final java.lang.Object other$version = other.getVersion(); 418 if (this$version == null ? other$version != null : !this$version.equals(other$version)) return false; 419 return true; 420 } 421 422 @java.lang.SuppressWarnings("all") 423 protected boolean canEqual(final java.lang.Object other) { 424 return other instanceof Document; 425 } 426 427 @java.lang.Override 428 @java.lang.SuppressWarnings("all") 429 public int hashCode() { 430 final int PRIME = 59; 431 int result = 1; 432 final java.lang.Object $persistenceLayer = this.getPersistenceLayer(); 433 result = result * PRIME + ($persistenceLayer == null ? 43 : $persistenceLayer.hashCode()); 434 final java.lang.Object $metadata = this.getMetadata(); 435 result = result * PRIME + ($metadata == null ? 43 : $metadata.hashCode()); 436 final java.lang.Object $source = this.getSource(); 437 result = result * PRIME + ($source == null ? 43 : $source.hashCode()); 438 final java.lang.Object $contentNode = this.getContentNode(); 439 result = result * PRIME + ($contentNode == null ? 43 : $contentNode.hashCode()); 440 result = result * PRIME + (this.isVirtual() ? 79 : 97); 441 final java.lang.Object $mixins = this.getMixins(); 442 result = result * PRIME + ($mixins == null ? 43 : $mixins.hashCode()); 443 final java.lang.Object $labels = this.getLabels(); 444 result = result * PRIME + ($labels == null ? 43 : $labels.hashCode()); 445 final java.lang.Object $taxonomies = this.getTaxonomies(); 446 result = result * PRIME + ($taxonomies == null ? 43 : $taxonomies.hashCode()); 447 final java.lang.Object $classes = this.getClasses(); 448 result = result * PRIME + ($classes == null ? 43 : $classes.hashCode()); 449 final java.lang.Object $uuid = this.getUuid(); 450 result = result * PRIME + ($uuid == null ? 43 : $uuid.hashCode()); 451 final java.lang.Object $version = this.getVersion(); 452 result = result * PRIME + ($version == null ? 43 : $version.hashCode()); 453 return result; 454 } 455 456 @java.lang.Override 457 @java.lang.SuppressWarnings("all") 458 public java.lang.String toString() { 459 return "Document(persistenceLayer=" + this.getPersistenceLayer() + ", metadata=" + this.getMetadata() + ", source=" + this.getSource() + ", contentNode=" + this.getContentNode() + ", virtual=" + this.isVirtual() + ", mixins=" + this.getMixins() + ", labels=" + this.getLabels() + ", taxonomies=" + this.getTaxonomies() + ", classes=" + this.getClasses() + ", uuid=" + this.getUuid() + ", version=" + this.getVersion() + ")"; 460 } 461}