use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.
the class AggregationDAOImpl method getAggregationCompletedKeys.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
@Transactional(readOnly = true)
public List<String> getAggregationCompletedKeys() throws AggregationDAOException {
final String query = "SELECT id from AggregationCompleted agg";
Collection<String> rs;
List<String> retVal = null;
;
try {
// get the list using a simple select
final Query select = entityManager.createQuery(query);
rs = (Collection<String>) select.getResultList();
// no keys... return an empty set
if (rs == null || rs.size() == 0)
return Collections.emptyList();
// put the list into a linked list
retVal = new LinkedList<String>(rs);
} catch (Exception e) {
throw new AggregationDAOException("Failed to get aggregation completed keys", e);
}
return retVal;
}
use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.
the class AggregationDAOImpl method getAggregationCompleted.
/**
* {@inheritDoc}
*/
@Override
@Transactional(readOnly = false, rollbackFor = { AggregationDAOException.class })
public AggregationCompleted getAggregationCompleted(String id, boolean lock) throws AggregationDAOException {
validateState();
try {
if (!lock) {
// it's not locked, so just find the completed aggregation and return it
return entityManager.find(AggregationCompleted.class, id);
} else {
/* this is essentially a recovery of a failed completed exchange
* camel runs this on start up and incremental intervals... it is
* essential that two application or route instances do not try to play
* back the same exchange at the same time, or else duplicate exchanges may be
* routed.... the recovery needs to be locked
*/
// first check to see if the recovery is available for locking
final AggregationCompleted entity = entityManager.find(AggregationCompleted.class, id);
if (entity == null)
return null;
// if the recovery lock time has not passed, then we can't recover
if (entity.getRecoveryLockedUntilDtTm() != null && entity.getRecoveryLockedUntilDtTm().after(Calendar.getInstance(Locale.getDefault())))
return null;
// now lock the row and update lock time and the count
entityManager.lock(entity, LockModeType.WRITE);
final Calendar newRecoveryLockTime = Calendar.getInstance(Locale.getDefault());
newRecoveryLockTime.add(Calendar.SECOND, recoveredEntityLockInterval);
entity.setRecoveryLockedUntilDtTm(newRecoveryLockTime);
// persist the time new lock time and increment the update count
entityManager.persist(entity);
entityManager.flush();
return entity;
}
} catch (OptimisticLockException ol) {
// someone else locked the row... return null
return null;
} catch (Exception e) {
throw new AggregationDAOException(e);
}
}
use of org.nhindirect.monitor.dao.AggregationDAOException 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
}
use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.
the class AggregationDAOImpl_addUpdateAggregationTest method testAddUpdateAggregationTest_entityManagerException_assertNoAggregation.
@Test
public void testAddUpdateAggregationTest_entityManagerException_assertNoAggregation() throws Exception {
EntityManager mgr = mock(EntityManager.class);
doThrow(new RuntimeException()).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 (AggregationDAOException e) {
exceptionOccured = true;
}
assertTrue(exceptionOccured);
}
use of org.nhindirect.monitor.dao.AggregationDAOException in project nhin-d by DirectProject.
the class AggregationDAOImpl_addUpdateAggregationTest method testAddUpdateAggregationTest_updateIncorrectVersion_assertExceptionAndRollback.
@Test
public void testAddUpdateAggregationTest_updateIncorrectVersion_assertExceptionAndRollback() throws Exception {
final Aggregation insert = new Aggregation();
insert.setExchangeBlob(new byte[] { 0, 3, 2 });
insert.setId("12345");
insert.setVersion(0);
notifDao.addUpdateAggregation(insert);
final Aggregation insert2 = new Aggregation();
insert2.setExchangeBlob(new byte[] { 0, 3, 2, 4, 1, 32 });
insert2.setVersion(3);
insert2.setId("12345");
boolean exceptionOccured = false;
try {
notifDao.addUpdateAggregation(insert2);
} catch (AggregationDAOException e) {
exceptionOccured = true;
}
assertTrue(exceptionOccured);
final Aggregation aggr = notifDao.getAggregation("12345");
assertNotNull(aggr);
assertEquals(insert, aggr);
}
Aggregations