Search in sources :

Example 1 with AggregationVersionException

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

the class AggregationDAOImpl method removeAggregation.

/**
	 * {@inheritDoc}
	 */
@Override
@Transactional(readOnly = false, rollbackFor = { AggregationDAOException.class })
public void removeAggregation(Aggregation agg, String exchangeId) throws AggregationDAOException {
    try {
        // find the aggregation
        final Aggregation existingAgg = this.getAggregation(agg.getId());
        if (existingAgg != null) {
            // check the version number for consistency
            if (existingAgg.getVersion() != agg.getVersion())
                throw new AggregationVersionException("Version number of aggreation does not match what is in the store.");
            // lock for removal
            entityManager.lock(existingAgg, LockModeType.WRITE);
            entityManager.remove(existingAgg);
        } else
            throw new AggregationDAOException("Aggregation does not exist is store.");
        // add to the completed repository
        final AggregationCompleted completed = new AggregationCompleted();
        completed.setExchangeBlob(existingAgg.getExchangeBlob());
        completed.setId(exchangeId);
        completed.setVersion(1);
        entityManager.persist(completed);
        // commit
        entityManager.flush();
    } catch (AggregationDAOException ae) {
        throw ae;
    }///CLOVER:OFF
     catch (OptimisticLockException ol) {
        throw new AggregationVersionException("Aggregation was removed by another thread or process before it could be committed.");
    } catch (Exception e) {
        throw new AggregationDAOException("Failed to remove aggregation.", e);
    }
///CLOVER:ON
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) AggregationCompleted(org.nhindirect.monitor.dao.entity.AggregationCompleted) 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 2 with AggregationVersionException

use of org.nhindirect.monitor.dao.AggregationVersionException 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 3 with AggregationVersionException

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

the class AggregationDAOImpl_addUpdateAggregationTest method testAddUpdateAggregationTest_optomisticLockException_assertAggregationVersionException.

@Test
public void testAddUpdateAggregationTest_optomisticLockException_assertAggregationVersionException() throws Exception {
    EntityManager mgr = mock(EntityManager.class);
    doThrow(new OptimisticLockException()).when(mgr).persist(any());
    final AggregationDAOImpl dao = new AggregationDAOImpl();
    dao.setEntityManager(mgr);
    boolean exceptionOccured = false;
    try {
        final Aggregation insert = new Aggregation();
        insert.setExchangeBlob(new byte[] { 0, 3, 2 });
        insert.setId("12345");
        insert.setVersion(0);
        dao.addUpdateAggregation(insert);
    } catch (AggregationVersionException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationVersionException(org.nhindirect.monitor.dao.AggregationVersionException) EntityManager(javax.persistence.EntityManager) OptimisticLockException(javax.persistence.OptimisticLockException) AggregationDAOImpl(org.nhindirect.monitor.dao.impl.AggregationDAOImpl) Test(org.junit.Test)

Aggregations

OptimisticLockException (javax.persistence.OptimisticLockException)3 AggregationVersionException (org.nhindirect.monitor.dao.AggregationVersionException)3 Aggregation (org.nhindirect.monitor.dao.entity.Aggregation)3 AggregationDAOException (org.nhindirect.monitor.dao.AggregationDAOException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 EntityManager (javax.persistence.EntityManager)1 Test (org.junit.Test)1 AggregationCompleted (org.nhindirect.monitor.dao.entity.AggregationCompleted)1 AggregationDAOImpl (org.nhindirect.monitor.dao.impl.AggregationDAOImpl)1