Class BoxDocument

java.lang.Object
edu.byu.hbll.box.BoxDocument

public class BoxDocument extends Object
Represents a document in box including metadata and dependencies. This object contains an internal document in the form of a ObjectNode. This internal document is the data of this object and all other fields are metadata.
Author:
Charles Draper
  • Constructor Details

    • BoxDocument

      protected BoxDocument()
      Creates a new empty document.
    • BoxDocument

      public BoxDocument(String id)
      Creates a new document initially in an UNPROCESSED state.
      Parameters:
      id - the unique id of the document
    • BoxDocument

      public BoxDocument(String id, BoxDocument.Status status)
      Creates a new document with the given state.
      Parameters:
      id - the unique id of the document
      status - the status of the document
    • BoxDocument

      public BoxDocument(String id, com.fasterxml.jackson.databind.node.ObjectNode document)
      Creates a new document initially in a READY state with the given internal document.
      Parameters:
      id - the unique id of the document
      document - the processed document
    • BoxDocument

      public BoxDocument(BoxDocument boxDocument)
      Copy Constructor.
      Parameters:
      boxDocument - the box document to copy
  • Method Details

    • toJson

      public com.fasterxml.jackson.databind.node.ObjectNode toJson()
      Serializes to an ObjectNode.
      Returns:
      a json representation of this document
    • toJson

      public com.fasterxml.jackson.databind.node.ObjectNode toJson(Collection<String> fields)
      Returns the ObjectNode representation of this document with the given field projection applied. Calling this method with an empty projection returns the entire document.

      Note: projection follows MongoDB's projection pattern (https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/). The projection is only applied to child nodes of objects. Arrays or nested arrays are iterated over and projection picks up again when objects are found. Primitive values that are not part of a terminal projection are removed.

      Parameters:
      fields - collection of dot-notated fields representing the projection
      Returns:
      the node representation of this document with projection applied.
    • parse

      public static BoxDocument parse(InputStream json)
      Creates a new box document from the given json.
      Parameters:
      json - the json to parse
      Returns:
      the parsed box document
    • parse

      public static BoxDocument parse(String json)
      Creates a new box document from the given json.
      Parameters:
      json - the json to parse
      Returns:
      the parsed box document
    • parse

      public static BoxDocument parse(com.fasterxml.jackson.databind.node.ObjectNode json)
      Creates a new box document from the given json.
      Parameters:
      json - the json to parse
      Returns:
      the parsed box document
    • hash

      public byte[] hash()
      Hashes the document using SHA-256 including metadata, but excluding all volatile metadata fields (ie, cursor, modified, and processed).
      Returns:
      the digest
    • hasDifferentDependencies

      public boolean hasDifferentDependencies(BoxDocument o)
      Whether or not this document's dependencies differ from the give document's.
      Parameters:
      o - the document to compare
      Returns:
      whether or not the dependencies differ
    • isProcessed

      public boolean isProcessed()
      Whether or not this document's processing has completed. That is whether the document is in a READY or DELETED state.
      Returns:
      if in a ready or deleted state
    • isUnProcessed

      @Deprecated public boolean isUnProcessed()
      Deprecated.
      use isUnprocessed() instead
      Whether or not this document is in the UNPROCESSED state.
      Returns:
      if in the UNPROCESSED state
    • isUnprocessed

      public boolean isUnprocessed()
      Whether or not this document is in the UNPROCESSED state.
      Returns:
      if in the UNPROCESSED state
    • isDeleted

      public boolean isDeleted()
      Whether or not this document is in the DELETED state.
      Returns:
      if in the DELETED state
    • isError

      public boolean isError()
      Whether or not this document is in the ERROR state.
      Returns:
      if in the ERROR state
    • isReady

      public boolean isReady()
      Whether or not this document is in the READY state.
      Returns:
      if in the READY state
    • matches

      public boolean matches(Collection<Facet> facets)
      Determines if this document matches (or should be included in a result set) given the supplied facets. In order to match, the document must have at least one facet from each facet group represented in the supplied facets. If the supplied facet list is empty, the document will match.
      Parameters:
      facets - the facets to test the document against
      Returns:
      whether or not this document matches the given facets
    • setAsDeleted

      public BoxDocument setAsDeleted()
      Explicitly sets the status to deleted.
      Returns:
      this
    • setAsUnprocessed

      public BoxDocument setAsUnprocessed()
      Explicitly sets the status to unprocessed.
      Returns:
      this
    • setAsReady

      public BoxDocument setAsReady()
      Explicitly sets the status to ready.
      Returns:
      this
    • setAsError

      public BoxDocument setAsError()
      Explicitly sets the status to error.
      Returns:
      this
    • setAsError

      public BoxDocument setAsError(String message)
      Explicitly sets the status to error and includes an error message.
      Parameters:
      message - the error message
      Returns:
      this
    • addDependency

      public BoxDocument addDependency(String sourceName, String id)
      Adds a dependency for this document.
      Parameters:
      sourceName - the sourceName of the dependency to add.
      id - the id of the dependency to add.
      Returns:
      this
    • addDependencies

      public BoxDocument addDependencies(DocumentId... dependencies)
      Adds dependencies for this document.
      Parameters:
      dependencies - the dependencies to add.
      Returns:
      this
    • addDependencies

      public BoxDocument addDependencies(Collection<? extends DocumentId> dependencies)
      Adds dependencies for this document.
      Parameters:
      dependencies - the dependencies to add.
      Returns:
      this
    • setDependencies

      public BoxDocument setDependencies(Collection<? extends DocumentId> dependencies)
      Clears dependencies and then adds these dependencies for this document.
      Parameters:
      dependencies - the dependencies to add.
      Returns:
      this
    • addFacet

      public BoxDocument addFacet(String name, String value)
      Adds a facet to this document.
      Parameters:
      name - name of the facet group
      value - value of the facet
      Returns:
      this
    • addFacets

      public BoxDocument addFacets(String name, String... values)
      Adds multiple facets to the document.
      Parameters:
      name - name of the facet group
      values - value(s) of the facet
      Returns:
      this
    • addFacets

      public BoxDocument addFacets(String name, Collection<String> values)
      Adds multiple facets to the document.
      Parameters:
      name - name of the facet group
      values - values of the facet
      Returns:
      this
    • addFacets

      public BoxDocument addFacets(Facet... facets)
      Adds multiple facets to the document.
      Parameters:
      facets - the facets to add
      Returns:
      this
    • addFacets

      public BoxDocument addFacets(Collection<? extends Facet> facets)
      Adds multiple facets to the document.
      Parameters:
      facets - the facets to add
      Returns:
      this
    • setFacets

      public BoxDocument setFacets(Collection<? extends Facet> facets)
      Clears existing facets and then adds these facets to the document.
      Parameters:
      facets - the facets to add
      Returns:
      this
    • addFacetsByQuery

      public BoxDocument addFacetsByQuery(String name, String path)
      Adds a facet by querying the internal document using the given path.
      Parameters:
      name - name of facet
      path - path to field value in dot notation
      Returns:
      this
    • addFacetsByQuery

      public BoxDocument addFacetsByQuery(Map<String,Set<String>> paths)
      Adds facets by querying the internal document using the given paths.
      Parameters:
      paths - the paths to the field values in dot notation, key is name of facet, value is set of paths
      Returns:
      this
    • getId

      public String getId()
      Returns the unique id of the document.
      Returns:
      the unique id of the document
    • setId

      public BoxDocument setId(String id)
      Set the unique id of the document.
      Parameters:
      id - the id of the document
      Returns:
      this
    • getDocument

      public com.fasterxml.jackson.databind.node.ObjectNode getDocument()
      Returns the internal document. Initialized to an empty object.
      Returns:
      the internal document
    • setDocument

      public BoxDocument setDocument(com.fasterxml.jackson.databind.node.ObjectNode document)
      Sets the internal document and modifies the status to READY if state not explicitly set elsewhere.
      Parameters:
      document - the document to set
      Returns:
      this
    • withDocument

      public com.fasterxml.jackson.databind.node.ObjectNode withDocument()
      Sets the status to READY if state not explicitly set elsewhere and returns the internal document. The internal documents is initially empty if not already set elsewhere.
      Returns:
      internal document
    • getCursor

      public Optional<Long> getCursor()
      Returns the cursor.
      Returns:
      the cursor
    • setCursor

      public BoxDocument setCursor(Long cursor)
      Sets the cursor.

      When processing a document, this should only be updated if the box document actually changed since last time. This should generally be left blank because Box will make that determination and set it appropriately when saving. If it's not blank, Box will honor the set value.

      Parameters:
      cursor - the cursor to set
      Returns:
      this
    • getModified

      public Optional<Instant> getModified()
      Returns the modified date.
      Returns:
      when the document was last modified
    • setModified

      public BoxDocument setModified(Instant modified)
      Sets the modified date.

      When processing a document, this should only be updated if the box document actually changed since last time. This should generally be left blank because Box will make that determination and set it appropriately when saving. If it's not blank, Box will honor the set value.

      Parameters:
      modified - the modified to set
      Returns:
      this
    • getProcessed

      public Optional<Instant> getProcessed()
      Returns the processed date.
      Returns:
      the processed
    • setProcessed

      public BoxDocument setProcessed(Instant processed)
      Sets the processed date.

      When processing a document, this can be left blank because Box will set it appropriately when saving. If it's not blank, Box will honor the set value.

      Parameters:
      processed - the processed to set
      Returns:
      this
    • getMessage

      public Optional<String> getMessage()
      Return the error message.
      Returns:
      the error
    • setMessage

      public BoxDocument setMessage(String message)
      Sets the message if an error occurred.
      Parameters:
      message - the message to set if an error occurred
      Returns:
      this
    • getFacets

      public Set<Facet> getFacets()
      Returns the facets.
      Returns:
      the facets
    • getFacetValues

      public Set<String> getFacetValues(String facetName)
      Returns all values for the given facet. An empty set is returned if no facet of the given name exists.
      Parameters:
      facetName - the facet name
      Returns:
      the facet values
    • getFacetValue

      public Optional<String> getFacetValue(String facetName)
      Returns the first value for the given facet.
      Parameters:
      facetName - the facet name
      Returns:
      the first facet value
    • getDependencies

      public Set<DocumentId> getDependencies()
      Returns the dependencies.
      Returns:
      the dependencies
    • getGroupId

      public Optional<String> getGroupId()
      Returns the groupId.
      Returns:
      the groupId
    • setGroupId

      public BoxDocument setGroupId(String groupId)
      Sets this document's group (used for orphan cleanup).
      Parameters:
      groupId - the groupId to set
      Returns:
      this
    • getStatus

      public BoxDocument.Status getStatus()
      Returns the status.
      Returns:
      the status
    • setStatus

      public BoxDocument setStatus(BoxDocument.Status status)
      Explicitly sets the status.
      Parameters:
      status - the status to set
      Returns:
      this
    • clearFacets

      public BoxDocument clearFacets()
      Clears the set of facets.
      Returns:
      this
    • clearDependencies

      public BoxDocument clearDependencies()
      Clears the set of dependencies.
      Returns:
      this
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)

      This method follows the same comparison Box uses internally to determine if the document has been modified. All fields are compared except for the volatile fields cursor, modified, and processed. The two documents must serialize the same in order for them to be considered equal.

      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(Collection<String> fields)
      Returns the string representation of this document with the given field projection applied. Calling this method with an empty projection returns the entire document.

      Note: projection follows MongoDB's projection pattern (https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/). The projection is only applied to child nodes of objects. Arrays or nested arrays are iterated over and projection picks up again when objects are found. Primitive values that are not part of a terminal projection are removed.

      Parameters:
      fields - collection of dot-notated fields representing the projection
      Returns:
      the string representation of this document with projection applied.