예제 #1
0
파일: TestMisc.java 프로젝트: evren/Empire
  @Test
  public void testEmpireUtil() throws Exception {
    SupportsRdfId aId = new SupportsRdfIdImpl();

    assertTrue(EmpireUtil.asResource(aId) == null);

    Resource aRes =
        EmpireUtil.asResource(new SupportsRdfIdImpl(new SupportsRdfId.BNodeKey("asdf")));
    assertTrue(aRes instanceof BNode);
    assertEquals(((BNode) aRes).getID(), "asdf");

    aId = EmpireUtil.asSupportsRdfId(java.net.URI.create("urn:foo"));
    assertTrue(aId.getRdfId() instanceof SupportsRdfId.URIKey);
    assertEquals(aId.getRdfId().value(), java.net.URI.create("urn:foo"));

    assertTrue(EmpireUtil.getNamedGraph("") == null);

    SupportsRdfId.RdfKey aKey = EmpireUtil.asPrimaryKey(new URL("http://example.org"));
    assertTrue(aKey instanceof SupportsRdfId.URIKey);
    assertEquals(aKey.value(), new URL("http://example.org").toURI());

    BNode aAnon = ValueFactoryImpl.getInstance().createBNode("foobar");
    aKey = EmpireUtil.asPrimaryKey(aAnon);
    assertTrue(aKey instanceof SupportsRdfId.BNodeKey);
    assertEquals(aKey.value(), "foobar");
  }
예제 #2
0
  /** @inheritDoc */
  @SuppressWarnings("unchecked")
  public <T> T merge(final T theT) {
    assertStateOk(theT);

    Graph aExistingData = null;

    if (theT instanceof EmpireGenerated) {
      aExistingData = ((EmpireGenerated) theT).getInstanceTriples();
    }

    if (aExistingData == null || aExistingData.isEmpty()) {
      try {
        aExistingData = assertContainsAndDescribe(theT);
      } catch (IllegalArgumentException e) {
        // it doesnt exist, so really, nothing to delete
        aExistingData = new GraphImpl();
      }
    }

    try {
      preUpdate(theT);

      Graph aData = RdfGenerator.asRdf(theT);

      boolean isTopOperation = (mOp == null);

      DataSourceOperation aOp = new DataSourceOperation();

      if (doesSupportNamedGraphs() && EmpireUtil.hasNamedGraphSpecified(theT)) {
        java.net.URI aGraphURI = EmpireUtil.getNamedGraph(theT);

        aOp.remove(aGraphURI, aExistingData);
        aOp.add(aGraphURI, aData);
      } else {
        aOp.remove(aExistingData);
        aOp.add(aData);
      }

      joinCurrentDataSourceOperation(aOp);

      // cascade the merge
      cascadeOperation(theT, new IsMergeCascade(), new MergeCascade());

      finishCurrentDataSourceOperation(isTopOperation);

      postUpdate(theT);

      return theT;
    } catch (DataSourceException ex) {
      throw new PersistenceException(ex);
    } catch (InvalidRdfException ex) {
      throw new IllegalStateException(ex);
    }
  }
예제 #3
0
  /** @inheritDoc */
  public void remove(final Object theObj) {
    assertStateOk(theObj);

    Graph aData = assertContainsAndDescribe(theObj);

    try {
      preRemove(theObj);

      boolean isTopOperation = (mOp == null);

      DataSourceOperation aOp = new DataSourceOperation();

      // we were transforming the current object to RDF and deleting that, but i dont think that's
      // the intended
      // behavior.  you want to delete everything about the object in the database, not the
      // properties specifically
      // on the thing being deleted -- there's an obvious case where there could be a delta between
      // them and you
      // don't delete everything.  so we'll do a describe on the object and delete everything we
      // know about it
      // i.e. everything where its in the subject position.

      // Graph aData = RdfGenerator.asRdf(theObj);
      // Graph aData = DataSourceUtil.describe(getDataSource(), theObj);

      if (doesSupportNamedGraphs() && EmpireUtil.hasNamedGraphSpecified(theObj)) {
        aOp.remove(EmpireUtil.getNamedGraph(theObj), aData);
      } else {
        aOp.remove(aData);
      }

      joinCurrentDataSourceOperation(aOp);

      cascadeOperation(theObj, new IsRemoveCascade(), new RemoveCascade());

      finishCurrentDataSourceOperation(isTopOperation);

      postRemove(theObj);
    } catch (DataSourceException ex) {
      throw new PersistenceException(ex);
    }
  }
예제 #4
0
  /** @inheritDoc */
  public void persist(final Object theObj) {
    assertStateOk(theObj);

    try {
      assertNotContains(theObj);
    } catch (Throwable e) {
      throw new EntityExistsException(e);
    }

    try {
      prePersist(theObj);

      boolean isTopOperation = (mOp == null);

      DataSourceOperation aOp = new DataSourceOperation();

      Graph aData = RdfGenerator.asRdf(theObj);

      if (doesSupportNamedGraphs() && EmpireUtil.hasNamedGraphSpecified(theObj)) {
        aOp.add(EmpireUtil.getNamedGraph(theObj), aData);
      } else {
        aOp.add(aData);
      }

      joinCurrentDataSourceOperation(aOp);

      cascadeOperation(theObj, new IsPersistCascade(), new MergeCascade());

      finishCurrentDataSourceOperation(isTopOperation);

      postPersist(theObj);
    } catch (InvalidRdfException ex) {
      throw new IllegalStateException(ex);
    } catch (DataSourceException ex) {
      throw new PersistenceException(ex);
    }
  }