Class Linker

java.lang.Object
com.erudika.para.core.Linker
All Implemented Interfaces:
Linkable, ParaObject, Votable, Serializable

public final class Linker extends Object implements ParaObject, Serializable
This class represents a many-to-many relationship (link) between two objects.
Author:
Alex Bogdanovski [[email protected]]
See Also:
  • Constructor Details

    • Linker

      public Linker()
      No-args constructor.
    • Linker

      public Linker(String type1, String type2, String id1, String id2)
      A link. The names of the objects are compared and sorted alphabetically.
      Parameters:
      type1 - the type of the first object
      type2 - the type of the second object
      id1 - the id of the first object
      id2 - the id of the second object
  • Method Details

    • getId2

      public String getId2()
      Returns the id of the second object in the link.
      Returns:
      the id
    • setId2

      public void setId2(String id2)
      Sets the id of the second object in the link.
      Parameters:
      id2 - a new id
    • getId1

      public String getId1()
      Returns the id of the first object in the link.
      Returns:
      the id
    • setId1

      public void setId1(String id1)
      Sets the id of the first object in the link.
      Parameters:
      id1 - a new id
    • getType1

      public String getType1()
      Returns the type of the first object in the link.
      Returns:
      the type
    • setType1

      public void setType1(String type1)
      Sets the type of the first object in the link.
      Parameters:
      type1 - the type
    • getType2

      public String getType2()
      Returns the type of the second object in the link.
      Returns:
      the type
    • setType2

      public void setType2(String type2)
      Sets the type of the second object in the link.
      Parameters:
      type2 - second type
    • getMetadata

      public String getMetadata()
      Returns the additional information about the link.
      Returns:
      some info
    • setMetadata

      public void setMetadata(String metadata)
      Sets additional information about the link.
      Parameters:
      metadata - some info
    • isFirst

      public boolean isFirst(String type2)
      Checks if the position of a given object is first or second.
      Parameters:
      type2 - the given class of object
      Returns:
      true if the object's type is equal to getType1()
    • getIdFieldNameFor

      public String getIdFieldNameFor(String type)
      Returns "id1" or "id2" depending on the alphabetical order of type.
      Parameters:
      type - the type
      Returns:
      id1 or id2
    • getNstd

      public List<Map<String,Object>> getNstd()
      Get the nested objects that are linked by this link.
      Returns:
      a 2-element array of objects or null
    • setNstd

      public void setNstd(List<Map<String,Object>> nstd)
      Sets the nested array of objects that are linked by this link.
      Parameters:
      nstd - an array of 2 objects only
    • addNestedObject

      public void addNestedObject(ParaObject obj)
      Add an object to nest inside the linker object. Used for joining queries when searching objects in a many-to-many relationship.
      Parameters:
      obj - object
    • getId

      public String getId()
      Returns the object ID.
      Specified by:
      getId in interface ParaObject
      Returns:
      the id
    • setId

      public void setId(String id)
      Sets the object ID.
      Specified by:
      setId in interface ParaObject
      Parameters:
      id - the id
    • getType

      public String getType()
      Returns the object type.
      Specified by:
      getType in interface ParaObject
      Returns:
      the type
    • setType

      public void setType(String type)
      Sets the object type.
      Specified by:
      setType in interface ParaObject
      Parameters:
      type - the type
    • getAppid

      public String getAppid()
      Returns the app identifier.
      Specified by:
      getAppid in interface ParaObject
      Returns:
      the appid
    • setAppid

      public void setAppid(String appid)
      Sets the app identifier.
      Specified by:
      setAppid in interface ParaObject
      Parameters:
      appid - the appid
    • getObjectURI

      public String getObjectURI()
      Returns the object URI.
      Specified by:
      getObjectURI in interface ParaObject
      Returns:
      the URI
      See Also:
    • getTags

      public List<String> getTags()
      Returns the tags.
      Specified by:
      getTags in interface ParaObject
      Returns:
      the tags
      See Also:
    • setTags

      public void setTags(List<String> tags)
      Sets the tags.
      Specified by:
      setTags in interface ParaObject
      Parameters:
      tags - the tags
    • getStored

      public Boolean getStored()
      Returns true if the object is stored in the database.
      Specified by:
      getStored in interface ParaObject
      Returns:
      true if stored
    • setStored

      public void setStored(Boolean stored)
      Sets the stored flag.
      Specified by:
      setStored in interface ParaObject
      Parameters:
      stored - the stored flag
    • getIndexed

      public Boolean getIndexed()
      Returns true if the object is indexed.
      Specified by:
      getIndexed in interface ParaObject
      Returns:
      true if indexed
    • setIndexed

      public void setIndexed(Boolean indexed)
      Sets the indexed flag.
      Specified by:
      setIndexed in interface ParaObject
      Parameters:
      indexed - the indexed flag
    • getCached

      public Boolean getCached()
      Returns true if the object is cached.
      Specified by:
      getCached in interface ParaObject
      Returns:
      true if cached
    • setCached

      public void setCached(Boolean cached)
      Sets the cached flag.
      Specified by:
      setCached in interface ParaObject
      Parameters:
      cached - the cached flag
    • getTimestamp

      public Long getTimestamp()
      Returns the timestamp.
      Specified by:
      getTimestamp in interface ParaObject
      Returns:
      the timestamp
    • setTimestamp

      public void setTimestamp(Long timestamp)
      Sets the timestamp.
      Specified by:
      setTimestamp in interface ParaObject
      Parameters:
      timestamp - the timestamp
    • getCreatorid

      public String getCreatorid()
      Returns the creator ID.
      Specified by:
      getCreatorid in interface ParaObject
      Returns:
      the creatorid
    • setCreatorid

      public void setCreatorid(String creatorid)
      Sets the creator ID.
      Specified by:
      setCreatorid in interface ParaObject
      Parameters:
      creatorid - the creatorid
    • getName

      public String getName()
      Returns the object name.
      Specified by:
      getName in interface ParaObject
      Returns:
      the name
    • setName

      public void setName(String name)
      Sets the object name.
      Specified by:
      setName in interface ParaObject
      Parameters:
      name - the name
    • getPlural

      public String getPlural()
      Returns the plural form of the object type.
      Specified by:
      getPlural in interface ParaObject
      Returns:
      the plural name
    • getParentid

      public String getParentid()
      Returns the parent ID.
      Specified by:
      getParentid in interface ParaObject
      Returns:
      the parentid
    • setParentid

      public void setParentid(String parentid)
      Sets the parent ID.
      Specified by:
      setParentid in interface ParaObject
      Parameters:
      parentid - the parentid
    • getUpdated

      public Long getUpdated()
      Returns the updated timestamp.
      Specified by:
      getUpdated in interface ParaObject
      Returns:
      the updated timestamp
    • setUpdated

      public void setUpdated(Long updated)
      Sets the updated timestamp.
      Specified by:
      setUpdated in interface ParaObject
      Parameters:
      updated - the updated timestamp
    • create

      public String create()
      Persists the object to the database.
      Specified by:
      create in interface ParaObject
      Returns:
      the object ID
      See Also:
    • update

      public void update()
      Updates the object in the database.
      Specified by:
      update in interface ParaObject
      See Also:
    • delete

      public void delete()
      Deletes the object from the database.
      Specified by:
      delete in interface ParaObject
      See Also:
    • exists

      public boolean exists()
      Returns true if the object exists in the database.
      Specified by:
      exists in interface ParaObject
      Returns:
      true if exists
      See Also:
    • voteUp

      public boolean voteUp(String userid)
      Votes up for the object.
      Specified by:
      voteUp in interface Votable
      Parameters:
      userid - the voter ID
      Returns:
      true if successful
    • voteDown

      public boolean voteDown(String userid)
      Votes down for the object.
      Specified by:
      voteDown in interface Votable
      Parameters:
      userid - the voter ID
      Returns:
      true if successful
    • getVotes

      public Integer getVotes()
      Returns the number of votes.
      Specified by:
      getVotes in interface Votable
      Returns:
      the votes
    • setVotes

      public void setVotes(Integer votes)
      Sets the number of votes.
      Specified by:
      setVotes in interface Votable
      Parameters:
      votes - the votes
    • getVersion

      public Long getVersion()
      Returns the version of the object.
      Specified by:
      getVersion in interface ParaObject
      Returns:
      the version
    • setVersion

      public void setVersion(Long version)
      Sets the version of the object.
      Specified by:
      setVersion in interface ParaObject
      Parameters:
      version - the version
    • countLinks

      public Long countLinks(String type2)
      Counts the number of links to other objects of a certain type.
      Specified by:
      countLinks in interface Linkable
      Parameters:
      type2 - the other type
      Returns:
      the number of links
    • getLinks

      public List<Linker> getLinks(String type2, Pager... pager)
      Returns a list of links to other objects of a certain type.
      Specified by:
      getLinks in interface Linkable
      Parameters:
      type2 - the other type
      pager - a pager
      Returns:
      a list of links
    • getLinkedObjects

      public <P extends ParaObject> List<P> getLinkedObjects(String type, Pager... pager)
      Returns a list of linked objects of a certain type.
      Specified by:
      getLinkedObjects in interface Linkable
      Type Parameters:
      P - the type of linked objects
      Parameters:
      type - the other type
      pager - a pager
      Returns:
      a list of linked objects
    • findLinkedObjects

      public <P extends ParaObject> List<P> findLinkedObjects(String type, String field, String query, Pager... pager)
      Finds linked objects of a certain type that match a specific field and query.
      Specified by:
      findLinkedObjects in interface Linkable
      Type Parameters:
      P - the type of linked objects
      Parameters:
      type - the other type
      field - the field to search in
      query - the search query
      pager - a pager
      Returns:
      a list of linked objects
    • isLinked

      public boolean isLinked(String type2, String id2)
      Returns true if the object is linked to another object of a certain type and ID.
      Specified by:
      isLinked in interface Linkable
      Parameters:
      type2 - the other type
      id2 - the other ID
      Returns:
      true if linked
    • isLinked

      public boolean isLinked(ParaObject toObj)
      Returns true if the object is linked to another object.
      Specified by:
      isLinked in interface Linkable
      Parameters:
      toObj - the other object
      Returns:
      true if linked
    • link

      public String link(String id2)
      Links the object to another object of a certain type and ID.
      Specified by:
      link in interface Linkable
      Parameters:
      id2 - the other ID
      Returns:
      the link ID
    • unlink

      public void unlink(String type, String id2)
      Unlinks the object from another object of a certain type and ID.
      Specified by:
      unlink in interface Linkable
      Parameters:
      type - the other type
      id2 - the other ID
    • unlinkAll

      public void unlinkAll()
      Unlinks the object from all other objects.
      Specified by:
      unlinkAll in interface Linkable
    • countChildren

      public Long countChildren(String type)
      Counts the number of children of a certain type.
      Specified by:
      countChildren in interface Linkable
      Parameters:
      type - the child type
      Returns:
      the number of children
    • getChildren

      public <P extends ParaObject> List<P> getChildren(String type, Pager... pager)
      Returns a list of children of a certain type.
      Specified by:
      getChildren in interface Linkable
      Type Parameters:
      P - the type of children
      Parameters:
      type - the child type
      pager - a pager
      Returns:
      a list of children
    • getChildren

      public <P extends ParaObject> List<P> getChildren(String type, String field, String term, Pager... pager)
      Returns a list of children of a certain type that match a specific field and term.
      Specified by:
      getChildren in interface Linkable
      Type Parameters:
      P - the type of children
      Parameters:
      type - the child type
      field - the field to search in
      term - the search term
      pager - a pager
      Returns:
      a list of children
    • findChildren

      public <P extends ParaObject> List<P> findChildren(String type, String query, Pager... pager)
      Finds children of a certain type that match a specific query.
      Specified by:
      findChildren in interface Linkable
      Type Parameters:
      P - the type of children
      Parameters:
      type - the child type
      query - the search query
      pager - a pager
      Returns:
      a list of children
    • deleteChildren

      public void deleteChildren(String type)
      Deletes all children of a certain type.
      Specified by:
      deleteChildren in interface Linkable
      Parameters:
      type - the child type
    • hashCode

      public int hashCode()
      Returns the hash code for this object.
      Overrides:
      hashCode in class Object
      Returns:
      hash code
    • equals

      public boolean equals(Object obj)
      Returns true if this object is equal to another.
      Overrides:
      equals in class Object
      Parameters:
      obj - the other object
      Returns:
      true if equal
    • toString

      public String toString()
      Returns a JSON string representation of this object.
      Overrides:
      toString in class Object
      Returns:
      JSON string