Exemple #1
0
 @Override
 public void remove(List<Statement> statements) {
   Model graph = null;
   GraphConnection graphConnection = null;
   try {
     graphConnection = openGraph();
     graph = graphConnection.getGraph();
     graph.enterCriticalSection(Lock.WRITE);
     for (Statement nuxStmt : statements) {
       com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStmt.getSubject());
       com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStmt.getPredicate());
       com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStmt.getObject());
       Triple jenaTriple = Triple.create(subject, predicate, object);
       com.hp.hpl.jena.rdf.model.Statement jenaStmt = graph.asStatement(jenaTriple);
       graph.remove(jenaStmt);
       // remove properties
       RSIterator it = graph.listReifiedStatements(jenaStmt);
       while (it.hasNext()) {
         ReifiedStatement rs = it.nextRS();
         rs.removeProperties();
       }
       // remove quadlets
       graph.removeAllReifications(jenaStmt);
       // graph.removeReification(reifiedStmt);
     }
   } finally {
     if (graph != null) {
       graph.leaveCriticalSection();
     }
     if (graphConnection != null) {
       graphConnection.close();
     }
   }
 }
Exemple #2
0
 @Override
 public List<Node> getPredicates(Node subject, Node object) {
   Model graph = null;
   GraphConnection graphConnection = null;
   try {
     graphConnection = openGraph();
     graph = graphConnection.getGraph();
     graph.enterCriticalSection(Lock.READ);
     SimpleSelector selector = getJenaSelector(graph, new StatementImpl(subject, null, object));
     StmtIterator it = graph.listStatements(selector);
     List<Statement> statements = getNXRelationsStatements(graph, it.toList());
     List<Node> res = new ArrayList<Node>();
     for (Statement stmt : statements) {
       Node predicate = stmt.getPredicate();
       if (!res.contains(predicate)) {
         // remove duplicates
         res.add(predicate);
       }
     }
     return res;
   } finally {
     if (graph != null) {
       graph.leaveCriticalSection();
     }
     if (graphConnection != null) {
       graphConnection.close();
     }
   }
 }
  @Override
  public void deleteRelation(CoreSession session, Statement stmt, boolean includeStatementsInEvents)
      throws ClientException {

    // notifications
    Map<String, Serializable> options = new HashMap<String, Serializable>();

    // Find relative document
    DocumentModel eventDocument = null;
    if (stmt.getSubject() instanceof QNameResource) {
      eventDocument =
          (DocumentModel)
              getRelationManager()
                  .getResourceRepresentation(
                      RelationConstants.DOCUMENT_NAMESPACE,
                      (QNameResource) stmt.getSubject(),
                      null);
    } else if (stmt.getObject() instanceof QNameResource) {
      eventDocument =
          (DocumentModel)
              getRelationManager()
                  .getResourceRepresentation(
                      RelationConstants.DOCUMENT_NAMESPACE, (QNameResource) stmt.getObject(), null);
    }

    // Complete event info and send first event
    if (eventDocument != null) {
      String currentLifeCycleState = eventDocument.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, eventDocument, options, null, session);
    }

    // remove statement
    getRelationManager().getGraphByName(RelationConstants.GRAPH_NAME).remove(stmt);

    if (eventDocument != null) {
      // after notification
      notifyEvent(RelationEvents.AFTER_RELATION_REMOVAL, eventDocument, options, null, session);
    }
  }
Exemple #4
0
  @Override
  public void add(List<Statement> statements) {
    Model graph = null;
    GraphConnection graphConnection = null;
    try {
      graphConnection = openGraph();
      graph = graphConnection.getGraph();
      graph.enterCriticalSection(Lock.WRITE);
      for (Statement nuxStmt : statements) {
        com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStmt.getSubject());
        com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStmt.getPredicate());
        com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStmt.getObject());
        Triple jenaTriple = Triple.create(subject, predicate, object);
        com.hp.hpl.jena.rdf.model.Statement jenaStmt = graph.asStatement(jenaTriple);

        // properties
        Map<Resource, Node[]> properties = nuxStmt.getProperties();
        if (properties == null || properties.isEmpty()) {
          // no properties
          graph.add(jenaStmt);
        } else {
          List<com.hp.hpl.jena.rdf.model.Statement> stmts =
              new ArrayList<com.hp.hpl.jena.rdf.model.Statement>();
          stmts.add(jenaStmt);
          // create reified statement if it does not exist
          com.hp.hpl.jena.graph.Node reifiedStmt = graph.getAnyReifiedStatement(jenaStmt).asNode();
          for (Map.Entry<Resource, Node[]> property : properties.entrySet()) {
            com.hp.hpl.jena.graph.Node prop = getJenaNode(property.getKey());
            for (Node node : property.getValue()) {
              com.hp.hpl.jena.graph.Node value = getJenaNode(node);
              Triple propTriple = Triple.create(reifiedStmt, prop, value);
              stmts.add(graph.asStatement(propTriple));
            }
          }
          graph.add(stmts);
        }
      }
    } finally {
      if (graph != null) {
        graph.leaveCriticalSection();
      }
      if (graphConnection != null) {
        graphConnection.close();
      }
    }
  }
Exemple #5
0
 /**
  * Gets Jena statement selector corresponding to the NXRelations statement.
  *
  * @param graph the jena graph
  * @param nuxStatement NXRelations statement
  * @return jena statement selector
  */
 private static SimpleSelector getJenaSelector(Model graph, Statement nuxStatement) {
   com.hp.hpl.jena.rdf.model.Resource subjResource = null;
   com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStatement.getSubject());
   if (subject != null && subject.isURI()) {
     subjResource = graph.getResource(subject.getURI());
   }
   Property predProp = null;
   com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStatement.getPredicate());
   if (predicate != null && predicate.isURI()) {
     predProp = graph.getProperty(predicate.getURI());
   }
   com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStatement.getObject());
   RDFNode objRDF = null;
   if (object != null) {
     objRDF = graph.asRDFNode(object);
   }
   return new SimpleSelector(subjResource, predProp, objRDF);
 }
 protected List<StatementInfo> getStatementsInfo(List<Statement> statements)
     throws ClientException {
   if (statements == null) {
     return null;
   }
   List<StatementInfo> infoList = new ArrayList<StatementInfo>();
   for (Statement statement : statements) {
     Subject subject = statement.getSubject();
     // TODO: filter on doc visibility (?)
     NodeInfo subjectInfo = new NodeInfoImpl(subject, getDocumentModel(subject), true);
     Resource predicate = statement.getPredicate();
     Node object = statement.getObject();
     NodeInfo objectInfo = new NodeInfoImpl(object, getDocumentModel(object), true);
     StatementInfo info =
         new StatementInfoImpl(statement, subjectInfo, new NodeInfoImpl(predicate), objectInfo);
     infoList.add(info);
   }
   return infoList;
 }
Exemple #7
0
  /**
   * Gets NXRelations statement corresponding to the Jena statement.
   *
   * <p>Reified statements may be retrieved from the Jena graph and set as properties on NXRelations
   * statements.
   *
   * @param graph the jena graph
   * @param jenaStatement jena statement
   * @return NXRelations statement
   */
  private Statement getNXRelationsStatement(
      Model graph, com.hp.hpl.jena.rdf.model.Statement jenaStatement) {
    Node subject = getNXRelationsNode(jenaStatement.getSubject().asNode());
    Node predicate = getNXRelationsNode(jenaStatement.getPredicate().asNode());
    Node object = getNXRelationsNode(jenaStatement.getObject().asNode());
    Statement statement = new StatementImpl(subject, predicate, object);

    // take care of properties
    if (graph.isReified(jenaStatement)) {
      com.hp.hpl.jena.rdf.model.Resource reifiedStmt = graph.getAnyReifiedStatement(jenaStatement);
      StmtIterator it = reifiedStmt.listProperties();
      while (it.hasNext()) {
        com.hp.hpl.jena.rdf.model.Statement stmt = it.nextStatement();
        Node nuxNode = getNXRelationsNode(stmt.getPredicate().asNode());
        // ugly cast as a Resource
        Node value = getNXRelationsNode(stmt.getObject().asNode());
        statement.addProperty((Resource) nuxNode, value);
      }
    }

    return statement;
  }
  @Override
  public void addRelation(
      CoreSession session,
      DocumentModel from,
      Node toResource,
      String predicate,
      boolean inverse,
      boolean includeStatementsInEvents,
      String comment)
      throws ClientException {
    Graph graph = getRelationManager().getGraphByName(RelationConstants.GRAPH_NAME);
    QNameResource fromResource = getNodeFromDocumentModel(from);

    Resource predicateResource = new ResourceImpl(predicate);
    Statement stmt = null;
    List<Statement> statements = null;
    if (inverse) {
      stmt = new StatementImpl(toResource, predicateResource, fromResource);
      statements = graph.getStatements(toResource, predicateResource, fromResource);
      if (statements != null && statements.size() > 0) {
        throw new RelationAlreadyExistsException();
      }
    } else {
      stmt = new StatementImpl(fromResource, predicateResource, toResource);
      statements = graph.getStatements(fromResource, predicateResource, toResource);
      if (statements != null && statements.size() > 0) {
        throw new RelationAlreadyExistsException();
      }
    }

    // Comment ?
    if (!StringUtils.isEmpty(comment)) {
      stmt.addProperty(RelationConstants.COMMENT, new LiteralImpl(comment));
    }
    Literal now = RelationDate.getLiteralDate(new Date());
    if (stmt.getProperties(RelationConstants.CREATION_DATE) == null) {
      stmt.addProperty(RelationConstants.CREATION_DATE, now);
    }
    if (stmt.getProperties(RelationConstants.MODIFICATION_DATE) == null) {
      stmt.addProperty(RelationConstants.MODIFICATION_DATE, now);
    }

    if (session.getPrincipal() != null && stmt.getProperty(RelationConstants.AUTHOR) != null) {
      stmt.addProperty(RelationConstants.AUTHOR, new LiteralImpl(session.getPrincipal().getName()));
    }

    // notifications

    Map<String, Serializable> options = new HashMap<String, Serializable>();
    String currentLifeCycleState = from.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, from, options, comment, session);

    // add statement
    graph.add(stmt);

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

    // after notification
    notifyEvent(RelationEvents.AFTER_RELATION_CREATION, from, options, comment, session);
  }
  // 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";
  }