/**
   * addLink
   *
   * <p>Create a link between two items
   *
   * <pre>
   * Version	Date		Developer				Description
   * 0.1		26/04/2012	Genevieve Turner (GT)	Initial
   * 0.19		27/09/2012	Genevieve Turner (GT)	Updated to generate reverse links
   * </pre>
   *
   * @param fedoraObject The item to transform to a display
   * @param form Contains the parameters from the request
   * @return A response for the web page
   * @throws FedoraClientException
   */
  @Override
  public void addLink(FedoraObject fedoraObject, String linkType, String itemId)
      throws FedoraClientException {
    String link = GlobalProps.getProperty(GlobalProps.PROP_FEDORA_RELATEDURI);

    LinkTypeDAO linkTypeDAO = new LinkTypeDAOImpl();
    LinkType linkTypeRecord = linkTypeDAO.getByCode(linkType);
    if (null == linkTypeRecord) {
      throw new WebApplicationException(
          Response.status(400).entity("Invalid relation type").build());
    }

    FedoraReference reference = new FedoraReference();
    String referenceType = linkType;
    String referenceItem = itemId;
    reference.setPredicate_(link + referenceType);
    reference.setObject_(referenceItem);
    reference.setIsLiteral_(Boolean.FALSE);
    FedoraBroker.addRelationship(fedoraObject.getObject_id(), reference);

    if (referenceItem.startsWith("info:fedora/")) {
      String referenceItemID = referenceItem.substring(12);
      FedoraReference reverseReference = new FedoraReference();
      reverseReference.setPredicate_(link + linkTypeRecord.getReverse());
      reverseReference.setObject_("info:fedora/" + fedoraObject.getObject_id());
      reverseReference.setIsLiteral_(Boolean.FALSE);
      FedoraBroker.addRelationship(referenceItemID, reverseReference);
    }
  }
  @PrePersist
  protected void onCreate() {
    this.timestamp = new Date();

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(timestamp.getTime());
    calendar.add(Calendar.DATE, 30);
    this.expiry = calendar.getTime();
    this.accessCode = Math.round(Math.random() * (double) Long.MAX_VALUE);
    this.accessPassword =
        Util.generateRandomString(
            Integer.parseInt(GlobalProps.getProperty(GlobalProps.PROP_DROPBOX_PASSWORDLENGTH)));
  }
  /**
   * getLinks
   *
   * <p>Retrieves the links for a page.
   *
   * <pre>
   * Version	Date		Developer				Description
   * 0.3		26/04/2012	Genevieve Turner (GT)	Initial
   * 0.7		08/06/2012	Genevieve Turner (GT)	Updated to cater for change to post method in the riSearchService
   * 0.17		28/08/2012	Genevieve Turner (GT)	Added the display of reverse links
   * 0.19		28/09/2012	Genevieve Turner (GT)	Updated so reverse links are not displayed
   * 0.21		22/10/2012	Genevieve Turner (GT)	Made this method public
   * </pre>
   *
   * @param fedoraObject The object to retrieve the links for
   * @return The results of the query
   */
  @Override
  public List<Result> getLinks(FedoraObject fedoraObject) {
    SparqlQuery sparqlQuery = new SparqlQuery();

    sparqlQuery.addVar("?item");
    sparqlQuery.addVar("?title");
    sparqlQuery.addVar("?predicate");
    sparqlQuery.addVar("?type");

    sparqlQuery.addTriple(
        "<info:fedora/" + fedoraObject.getObject_id() + ">", "?predicate", "?item", Boolean.FALSE);
    // GT - 20120928 - Note this code is only commented out as it may be placed back in at a later
    // date.
    /*
    StringBuilder tripleString = new StringBuilder();
    tripleString.append("{ <info:fedora/");
    tripleString.append(fedoraObject.getObject_id());
    tripleString.append("> ?predicate ?item . } ");
    tripleString.append("UNION ");
    tripleString.append("{ ?item ?predicate <info:fedora/");
    tripleString.append(fedoraObject.getObject_id());
    tripleString.append("> } ");

    sparqlQuery.addTripleSet(tripleString.toString());
    */
    // Ensure that the linked to item is active (i.e. it hasn't been deleted)
    sparqlQuery.addTriple("?item", "<dc:title>", "?title", true);
    sparqlQuery.addTriple("?item", "<dc:type>", "?type", true);
    sparqlQuery.addTriple("?item", "<fedora-model:state>", "?state", true);
    String filterString =
        "regex(str(?predicate), '"
            + GlobalProps.getProperty(GlobalProps.PROP_FEDORA_RELATEDURI)
            + "', 'i')";
    sparqlQuery.addFilter(filterString, "");
    sparqlQuery.addFilter("!BOUND(?state) || regex(str(?state), 'Active')", "&&");
    ClientResponse respFromRiSearch = riSearchService.post("query", sparqlQuery.generateQuery());

    List<Result> resultList = getSparqlResultList(respFromRiSearch);
    LOGGER.debug("Number of related items found: {}", resultList.size());
    return resultList;
  }