Search in sources :

Example 11 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class AggregationDAOImpl method addUpdateAggregation.

/**
	 * {@inheritDoc}
	 */
@Override
@Transactional(readOnly = false, rollbackFor = { AggregationDAOException.class })
public void addUpdateAggregation(Aggregation aggr) throws AggregationDAOException {
    try {
        // find the aggregation
        final Aggregation existingAggr = this.getAggregation(aggr.getId());
        // if its not there by the requested aggregation has a version > 1, then somethine is wrong
        if (existingAggr == null && aggr.getVersion() > 0)
            throw new AggregationVersionException("Aggregation not found but expected to exist due to non 0 version number");
        if (existingAggr != null) {
            // make sure the version on the existing aggregator matches ours
            if (existingAggr.getVersion() != aggr.getVersion())
                throw new AggregationVersionException("Version number of aggreation does not match what is in the store.");
            // lock the aggregation for update
            entityManager.lock(existingAggr, LockModeType.WRITE);
            existingAggr.setExchangeBlob(aggr.getExchangeBlob());
            entityManager.persist(existingAggr);
        } else {
            // initial add... set the version number to 1
            aggr.setVersion(aggr.getVersion() + 1);
            entityManager.persist(aggr);
        }
        // commit
        entityManager.flush();
    } catch (AggregationDAOException ae) {
        throw ae;
    } catch (OptimisticLockException ol) {
        throw new AggregationVersionException("Aggregation was updated by another thread or process before it could be committed.");
    } catch (Exception e) {
        throw new AggregationDAOException("Failed to add or update aggregation.", e);
    }
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 12 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_keyExists_assertAggregationCompletedFound.

@Test
public void testGetAggregationCompleted_nonEmptyRepository_keyExists_assertAggregationCompletedFound() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    assertNotNull(notifDao.getAggregation("12345"));
    final Aggregation remove = new Aggregation();
    remove.setExchangeBlob(new byte[] { 0, 3, 2 });
    remove.setId("12345");
    remove.setVersion(1);
    notifDao.removeAggregation(remove, "12345");
    final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
    assertNotNull(aggr);
    assertEquals("12345", aggr.getId());
    assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) Test(org.junit.Test)

Example 13 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_exchangeLocked_assertAggregationCompletedNotFound.

@Test
public void testGetAggregationCompleted_nonEmptyRepository_exchangeLocked_assertAggregationCompletedNotFound() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    assertNotNull(notifDao.getAggregation("12345"));
    final Aggregation remove = new Aggregation();
    remove.setExchangeBlob(new byte[] { 0, 3, 2 });
    remove.setId("12345");
    remove.setVersion(1);
    notifDao.removeAggregation(remove, "12345");
    final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
    assertNotNull(aggr);
    assertEquals("12345", aggr.getId());
    assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
    final AggregationCompleted lockedAggr = notifDao.getAggregationCompleted("12345", true);
    assertNull(lockedAggr);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) Test(org.junit.Test)

Example 14 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_exchangeLockExpired_assertAggregationCompletedFound.

@Test
public void testGetAggregationCompleted_nonEmptyRepository_exchangeLockExpired_assertAggregationCompletedFound() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    assertNotNull(notifDao.getAggregation("12345"));
    final Aggregation remove = new Aggregation();
    remove.setExchangeBlob(new byte[] { 0, 3, 2 });
    remove.setId("12345");
    remove.setVersion(1);
    notifDao.removeAggregation(remove, "12345");
    final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
    assertNotNull(aggr);
    assertEquals("12345", aggr.getId());
    assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
    Thread.sleep(4000);
    final AggregationCompleted lockedAggr = notifDao.getAggregationCompleted("12345", true);
    assertNotNull(lockedAggr);
    assertEquals("12345", lockedAggr.getId());
    assertTrue(Arrays.equals(insert.getExchangeBlob(), lockedAggr.getExchangeBlob()));
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) Test(org.junit.Test)

Example 15 with Aggregation

use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.

the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_keyDoesntExist_assertNoAggregation.

@Test
public void testGetAggregationCompleted_nonEmptyRepository_keyDoesntExist_assertNoAggregation() throws Exception {
    final Aggregation insert = new Aggregation();
    insert.setExchangeBlob(new byte[] { 0, 3, 2 });
    insert.setId("12345");
    insert.setVersion(0);
    notifDao.addUpdateAggregation(insert);
    assertNotNull(notifDao.getAggregation("12345"));
    final Aggregation remove = new Aggregation();
    remove.setExchangeBlob(new byte[] { 0, 3, 2 });
    remove.setId("12345");
    remove.setVersion(1);
    notifDao.removeAggregation(remove, "12345");
    final AggregationCompleted aggr = notifDao.getAggregationCompleted("doenst matter", true);
    assertNull(aggr);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) Test(org.junit.Test)

Aggregations

Aggregation (org.nhindirect.monitor.dao.entity.Aggregation)25 Test (org.junit.Test)20 AggregationDAOException (org.nhindirect.monitor.dao.AggregationDAOException)7 AggregationCompleted (org.nhindirect.monitor.dao.entity.AggregationCompleted)6 OptimisticLockException (javax.persistence.OptimisticLockException)3 AggregationVersionException (org.nhindirect.monitor.dao.AggregationVersionException)3 EntityManager (javax.persistence.EntityManager)2 AggregationDAOImpl (org.nhindirect.monitor.dao.impl.AggregationDAOImpl)2 Transactional (org.springframework.transaction.annotation.Transactional)2 Exchange (org.apache.camel.Exchange)1 Buffer (org.fusesource.hawtbuf.Buffer)1