예제 #1
0
  /** @inheritDoc */
  public <T> T find(final Class<T> theClass, final Object theObj) {
    assertOpen();

    try {
      AnnotationChecker.assertValid(theClass);
    } catch (EmpireException e) {
      throw new IllegalArgumentException(e);
    }

    try {
      if (DataSourceUtil.exists(getDataSource(), EmpireUtil.asPrimaryKey(theObj))) {
        T aT = RdfGenerator.fromRdf(theClass, EmpireUtil.asPrimaryKey(theObj), getDataSource());

        postLoad(aT);

        return aT;
      } else {
        return null;
      }
    } catch (InvalidRdfException e) {
      throw new IllegalArgumentException(
          "Type is not valid, or object with key is not a valid Rdf Entity.", e);
    } catch (DataSourceException e) {
      throw new PersistenceException(e);
    }
  }
예제 #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 refresh(Object theObj) {
    assertStateOk(theObj);

    assertContains(theObj);

    Object aDbObj = find(theObj.getClass(), EmpireUtil.asSupportsRdfId(theObj).getRdfId());

    Collection<AccessibleObject> aAccessors = new HashSet<AccessibleObject>();

    aAccessors.addAll(getAnnotatedFields(aDbObj.getClass()));
    aAccessors.addAll(getAnnotatedGetters(aDbObj.getClass(), true));

    if (theObj instanceof EmpireGenerated) {
      ((EmpireGenerated) theObj).setAllTriples(((EmpireGenerated) aDbObj).getAllTriples());
      ((EmpireGenerated) theObj)
          .setInstanceTriples(((EmpireGenerated) aDbObj).getInstanceTriples());
    }

    try {
      for (AccessibleObject aAccess : aAccessors) {
        Object aValue = safeGet(aAccess, aDbObj);

        AccessibleObject aSetter = asSetter(aDbObj.getClass(), aAccess);

        safeSet(aSetter, theObj, aValue);
      }
    } catch (InvocationTargetException e) {
      throw new PersistenceException(e);
    }
  }
예제 #4
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);
    }
  }
예제 #5
0
    /**
     * Verify that all the objects to be added/removed were completed successfully.
     *
     * @throws PersistenceException if an add or remove failed for any reason
     */
    private void verify() {
      for (Object aObj : mVerifyRemove) {
        if (contains(aObj)) {
          throw new PersistenceException(
              "Remove failed for object: "
                  + aObj.getClass()
                  + " -> "
                  + EmpireUtil.asSupportsRdfId(aObj).getRdfId());
        }
      }

      for (Object aObj : mVerifyAdd) {
        if (!contains(aObj)) {
          throw new PersistenceException(
              "Addition failed for object: "
                  + aObj.getClass()
                  + " -> "
                  + EmpireUtil.asSupportsRdfId(aObj).getRdfId());
        }
      }
    }
예제 #6
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");
  }
예제 #7
0
파일: TestMisc.java 프로젝트: evren/Empire
  @Test
  public void testTimesTwo() throws InvalidRdfException {
    TestDoubleImpl obj = new TestDoubleImpl();

    Graph g = RdfGenerator.asRdf(obj);

    assertEquals(
        1,
        new ExtGraph(g)
            .getValues(
                EmpireUtil.asResource(obj),
                ValueFactoryImpl.getInstance().createURI(PrefixMapping.GLOBAL.uri("test:foo")))
            .size());
  }
예제 #8
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);
    }
  }