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
}
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);
}
}
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);
}
Aggregations