use of org.nhindirect.monitor.dao.entity.Aggregation 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.entity.Aggregation 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.entity.Aggregation in project nhin-d by DirectProject.
the class ConcurrentJPAAggregationRepository method add.
/**
* {@inheritDoc}
* This specific implementation throws a Runtime exception on DAO errors. This implementation also checks for
* consistency/concurrency of the exchange. If the attempted exchange does match the latest and greatest
* exchange version, then an AggregationVersionException is wrapped by the runtime error. Routes using this
* repository should catch the AggregationVersionException and attempt to retry the exchange. If exception handling
* and redelivery is configured correctly, Camel should automatically reload the exchange from the latest version in the
* and attempt the aggregation process again.
*
*/
@Override
public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
try {
// serialize the exchange to a blob
final byte[] blob = codec.marshallExchange(camelContext, exchange).getData();
// get the current version of the exchange... if this is the first time the exchange with the
// given key is added, this should result in null
Integer currentEntityVersion = (Integer) exchange.getProperty(AGGREGATION_ENTITY_VERSON);
Aggregation agg = new Aggregation();
agg.setExchangeBlob(blob);
agg.setId(key);
agg.setVersion(currentEntityVersion == null ? 0 : currentEntityVersion);
// add/update the repository...
dao.addUpdateAggregation(agg);
// update the version on the exchange
exchange.setProperty(AGGREGATION_ENTITY_VERSON, agg.getVersion());
} catch (Exception e) {
// wrap exception in a runtime exception
throw new RuntimeException("Error adding to repository aggregation with key " + key, e);
}
// might support getting the older version in some later version
return null;
}
use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.
the class ConcurrentJPAAggregationRepository method remove.
/**
* {@inheritDoc}
* This specific implementation throws a Runtime exception on DAO errors. This implementation also checks for
* consistency/concurrency of the exchange. If the attempted removal of an exchange does match the latest and greatest
* exchange version, then an AggregationVersionException is wrapped by the runtime error. Routes using this
* repository should catch the AggregationVersionException and attempt to retry the exchange. If exception handling
* and redelivery is configured correctly, Camel should automatically reload the exchange from the latest version in the
* and attempt the aggregation and completion condition again.
*/
@Override
public void remove(CamelContext camelContext, String key, Exchange exchange) {
try {
// get the version of the exchange
Integer currentEntityVersion = (Integer) exchange.getProperty(AGGREGATION_ENTITY_VERSON);
// serialize the exchange to a byte array
final byte[] blob = codec.marshallExchange(camelContext, exchange).getData();
Aggregation agg = new Aggregation();
agg.setExchangeBlob(blob);
agg.setId(key);
agg.setVersion(currentEntityVersion == null ? 0 : currentEntityVersion);
// removed the exchange from the currently working set and move it to completed set
// for later confirmation
dao.removeAggregation(agg, exchange.getExchangeId());
} catch (Exception e) {
// wrap exception in a runtime exception
throw new RuntimeException("Error removing from repository aggregation with key " + key, e);
}
}
use of org.nhindirect.monitor.dao.entity.Aggregation 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