/**
   * Return outgoing relations for a given DocumentModel Same as super.getOutgoingStatementsInfo(),
   * except for document argument (@see
   * org.nuxeo.ecm.platform.relations.web.listener.ejb.RelationActionsBean::getOutgoingStatementsInfo())
   *
   * @param currentDoc
   * @return
   * @throws ClientException
   */
  public List<StatementInfo> getOutgoingStatementsInfo(DocumentModel currentDoc)
      throws ClientException {

    if (log.isDebugEnabled()) log.debug("getOutgoingStatementsInfo :: currentDoc=" + currentDoc);

    if (outgoingStatementsInfo != null) {
      return outgoingStatementsInfo;
    }

    Resource docResource = getDocumentResource(currentDoc);
    if (docResource == null) {
      outgoingStatements = Collections.emptyList();
      outgoingStatementsInfo = Collections.emptyList();
    } else {
      Statement pattern = new StatementImpl(docResource, null, null);
      outgoingStatements = relationManager.getStatements(RelationConstants.GRAPH_NAME, pattern);
      // add old statements, BBB
      Resource oldDocResource = getOldDocumentResource(currentDoc);
      Statement oldPattern = new StatementImpl(oldDocResource, null, null);
      outgoingStatements.addAll(
          relationManager.getStatements(RelationConstants.GRAPH_NAME, oldPattern));
      outgoingStatementsInfo = getStatementsInfo(outgoingStatements);
      // sort by modification date, reverse
      Comparator<StatementInfo> comp = Collections.reverseOrder(new StatementInfoComparator());
      Collections.sort(outgoingStatementsInfo, comp);
    }
    return outgoingStatementsInfo;
  }
 public QNameResource getDocumentResource(DocumentModel document) throws ClientException {
   QNameResource documentResource = null;
   if (document != null) {
     documentResource =
         (QNameResource)
             relationManager.getResource(RelationConstants.DOCUMENT_NAMESPACE, document, null);
   }
   return documentResource;
 }
 public DocumentModel getDocumentModel(Node node) throws ClientException {
   if (node.isQNameResource()) {
     QNameResource resource = (QNameResource) node;
     Map<String, Serializable> context = new HashMap<String, Serializable>();
     context.put(ResourceAdapter.CORE_SESSION_ID_CONTEXT_KEY, documentManager.getSessionId());
     Object o =
         relationManager.getResourceRepresentation(resource.getNamespace(), resource, context);
     if (o instanceof DocumentModel) {
       return (DocumentModel) o;
     }
   }
   return null;
 }
 public List<StatementInfo> getIncomingStatementsInfo() throws ClientException {
   if (incomingStatementsInfo != null) {
     return incomingStatementsInfo;
   }
   DocumentModel currentDoc = getCurrentDocument();
   Resource docResource = getDocumentResource(currentDoc);
   if (docResource == null) {
     incomingStatements = Collections.emptyList();
     incomingStatementsInfo = Collections.emptyList();
   } else {
     Statement pattern = new StatementImpl(null, null, docResource);
     incomingStatements = relationManager.getStatements(RelationConstants.GRAPH_NAME, pattern);
     // add old statements, BBB
     Resource oldDocResource = getOldDocumentResource(currentDoc);
     Statement oldPattern = new StatementImpl(null, null, oldDocResource);
     incomingStatements.addAll(
         relationManager.getStatements(RelationConstants.GRAPH_NAME, oldPattern));
     incomingStatementsInfo = getStatementsInfo(incomingStatements);
     // sort by modification date, reverse
     Comparator<StatementInfo> comp = Collections.reverseOrder(new StatementInfoComparator());
     Collections.sort(incomingStatementsInfo, comp);
   }
   return incomingStatementsInfo;
 }
  public String deleteStatement(StatementInfo stmtInfo, DocumentModel source)
      throws ClientException {
    if (stmtInfo != null
        && outgoingStatementsInfo != null
        && outgoingStatementsInfo.contains(stmtInfo)) {
      Statement stmt = stmtInfo.getStatement();
      // notifications
      Map<String, Serializable> options = new HashMap<String, Serializable>();
      // DocumentModel source = getCurrentDocument();
      String currentLifeCycleState = source.getCurrentLifeCycleState();
      options.put(CoreEventConstants.DOC_LIFE_CYCLE, currentLifeCycleState);
      options.put(RelationEvents.GRAPH_NAME_EVENT_KEY, RelationConstants.GRAPH_NAME);
      if (includeStatementsInEvents) {
        putStatements(options, stmt);
      }

      // before notification
      notifyEvent(RelationEvents.BEFORE_RELATION_REMOVAL, source, options, null);

      // remove statement
      List<Statement> stmts = new ArrayList<Statement>();
      stmts.add(stmt);
      relationManager.remove(RelationConstants.GRAPH_NAME, stmts);

      // after notification
      notifyEvent(RelationEvents.AFTER_RELATION_REMOVAL, source, options, null);

      // make sure statements will be recomputed
      resetStatements();

      facesMessages.add(
          FacesMessage.SEVERITY_INFO,
          resourcesAccessor.getMessages().get("label.relation.deleted"));
    }
    return "document_relations";
  }
  protected int getCommentGrahNodesNumber() throws Exception {
    RelationManager rm = Framework.getService(RelationManager.class);

    List<Statement> statementList = rm.getGraphByName("documentComments").getStatements();
    return statementList.size();
  }
  // MODIFORI
  public String addStatement(DocumentModel currentDoc) throws ClientException {
    Resource documentResource = getDocumentResource(currentDoc);
    if (documentResource == null) {
      throw new ClientException("Document resource could not be retrieved");
    }

    Resource predicate = new ResourceImpl(predicateUri);
    Node object = null;
    if (objectType.equals("literal")) {
      objectLiteralValue = objectLiteralValue.trim();
      object = new LiteralImpl(objectLiteralValue);
    } else if (objectType.equals("uri")) {
      objectUri = objectUri.trim();
      object = new ResourceImpl(objectUri);
    } else if (objectType.equals("document")) {
      objectDocumentUid = objectDocumentUid.trim();
      String repositoryName = navigationContext.getCurrentServerLocation().getName();
      String localName = repositoryName + "/" + objectDocumentUid;
      object = new QNameResourceImpl(RelationConstants.DOCUMENT_NAMESPACE, localName);
    }

    // if outgoing statement is null
    if (outgoingStatements == null) {
      getOutgoingStatementsInfo(currentDoc);
    }

    // create new statement
    Statement stmt = new StatementImpl(documentResource, predicate, object);
    if (!outgoingStatements.contains(stmt)) {
      // add statement to the graph
      List<Statement> stmts = new ArrayList<Statement>();
      String eventComment = null;
      if (comment != null) {
        comment = comment.trim();
        if (comment.length() > 0) {
          stmt.addProperty(RelationConstants.COMMENT, new LiteralImpl(comment));
          eventComment = comment;
        }
      }
      Literal now = RelationDate.getLiteralDate(new Date());
      stmt.addProperty(RelationConstants.CREATION_DATE, now);
      stmt.addProperty(RelationConstants.MODIFICATION_DATE, now);
      if (currentUser != null) {
        stmt.addProperty(RelationConstants.AUTHOR, new LiteralImpl(currentUser.getName()));
      }

      stmts.add(stmt);

      // notifications

      Map<String, Serializable> options = new HashMap<String, Serializable>();
      String currentLifeCycleState = currentDoc.getCurrentLifeCycleState();
      options.put(CoreEventConstants.DOC_LIFE_CYCLE, currentLifeCycleState);
      if (includeStatementsInEvents) {
        putStatements(options, stmt);
      }
      options.put(RelationEvents.GRAPH_NAME_EVENT_KEY, RelationConstants.GRAPH_NAME);

      // before notification
      notifyEvent(RelationEvents.BEFORE_RELATION_CREATION, currentDoc, options, eventComment);

      // add statement
      relationManager.add(RelationConstants.GRAPH_NAME, stmts);

      // XXX AT: try to refetch it from the graph so that resources are
      // transformed into qname resources: useful for indexing
      if (includeStatementsInEvents) {
        putStatements(options, relationManager.getStatements(RelationConstants.GRAPH_NAME, stmt));
      }

      // after notification
      notifyEvent(RelationEvents.AFTER_RELATION_CREATION, currentDoc, options, eventComment);

      // make sure statements will be recomputed
      resetStatements();

      // commenté pour ne pas affiché Relation créée qui ne parle pas à l'utilisateur
      // facesMessages.add(FacesMessage.SEVERITY_INFO,
      // resourcesAccessor.getMessages().get("label.relation.created"));
      resetCreateFormValues();
    } else {
      facesMessages.add(
          FacesMessage.SEVERITY_WARN,
          resourcesAccessor.getMessages().get("label.relation.already.exists"));
    }
    return "document_relations";
  }