Search in sources :

Example 11 with AggregationDAOException

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

the class AggregationDAOImpl method purgeAll.

/**
	 * {@inheritDoc}
	 */
@Override
@Transactional(readOnly = false)
public void purgeAll() throws AggregationDAOException {
    try {
        // purge using simple delete statements
        Query delete = entityManager.createQuery("DELETE FROM Aggregation agg where agg.version > -1");
        delete.executeUpdate();
        delete = entityManager.createQuery("DELETE FROM AggregationCompleted agg where agg.version > -1");
        delete.executeUpdate();
        entityManager.flush();
    }///CLOVER:OFF
     catch (Exception e) {
        throw new AggregationDAOException("Failed to purge all aggregation information.", e);
    }
///CLOVER:ON
}
Also used : Query(javax.persistence.Query) 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 AggregationDAOException

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

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

the class AggregationDAOImpl_getAggregationCompletedKeysTest method testGetAggregationCompletedKeys_entityManagerException_assertNoAggregation.

@Test
public void testGetAggregationCompletedKeys_entityManagerException_assertNoAggregation() throws Exception {
    EntityManager mgr = mock(EntityManager.class);
    doThrow(new RuntimeException()).when(mgr).createQuery((String) any());
    final AggregationDAOImpl dao = new AggregationDAOImpl();
    dao.setEntityManager(mgr);
    boolean exceptionOccured = false;
    try {
        dao.getAggregationCompletedKeys();
    } catch (AggregationDAOException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : EntityManager(javax.persistence.EntityManager) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) AggregationDAOImpl(org.nhindirect.monitor.dao.impl.AggregationDAOImpl) Test(org.junit.Test)

Example 14 with AggregationDAOException

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

the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_entityManagerException_assertException.

@Test
public void testGetAggregationCompleted_entityManagerException_assertException() throws Exception {
    EntityManager mgr = mock(EntityManager.class);
    doThrow(new RuntimeException()).when(mgr).find((Class<?>) any(), any());
    final AggregationDAOImpl dao = new AggregationDAOImpl();
    dao.setEntityManager(mgr);
    boolean exceptionOccured = false;
    try {
        dao.getAggregationCompleted("12345", true);
    } catch (AggregationDAOException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : EntityManager(javax.persistence.EntityManager) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) AggregationDAOImpl(org.nhindirect.monitor.dao.impl.AggregationDAOImpl) Test(org.junit.Test)

Example 15 with AggregationDAOException

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

the class AggregationDAOImpl_removeAggregationTest method testRemoveAggregation_emptyRepository_assertExcpetion.

@Test
public void testRemoveAggregation_emptyRepository_assertExcpetion() throws Exception {
    final Aggregation remove = new Aggregation();
    remove.setExchangeBlob(new byte[] { 0, 3, 2 });
    remove.setId("12345");
    remove.setVersion(0);
    boolean exceptionOccured = false;
    try {
        notifDao.removeAggregation(remove, "12345");
    } catch (AggregationDAOException e) {
        exceptionOccured = true;
    }
    assertTrue(exceptionOccured);
}
Also used : Aggregation(org.nhindirect.monitor.dao.entity.Aggregation) AggregationDAOException(org.nhindirect.monitor.dao.AggregationDAOException) Test(org.junit.Test)

Aggregations

AggregationDAOException (org.nhindirect.monitor.dao.AggregationDAOException)17 Test (org.junit.Test)10 OptimisticLockException (javax.persistence.OptimisticLockException)7 AggregationVersionException (org.nhindirect.monitor.dao.AggregationVersionException)7 Aggregation (org.nhindirect.monitor.dao.entity.Aggregation)7 Transactional (org.springframework.transaction.annotation.Transactional)7 EntityManager (javax.persistence.EntityManager)6 AggregationDAOImpl (org.nhindirect.monitor.dao.impl.AggregationDAOImpl)6 Query (javax.persistence.Query)3 AggregationCompleted (org.nhindirect.monitor.dao.entity.AggregationCompleted)3 Calendar (java.util.Calendar)1