use of org.nhindirect.monitor.dao.entity.Aggregation 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.entity.Aggregation in project nhin-d by DirectProject.
the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_keyExists_assertAggregationCompletedFound.
@Test
public void testGetAggregationCompleted_nonEmptyRepository_keyExists_assertAggregationCompletedFound() throws Exception {
final Aggregation insert = new Aggregation();
insert.setExchangeBlob(new byte[] { 0, 3, 2 });
insert.setId("12345");
insert.setVersion(0);
notifDao.addUpdateAggregation(insert);
assertNotNull(notifDao.getAggregation("12345"));
final Aggregation remove = new Aggregation();
remove.setExchangeBlob(new byte[] { 0, 3, 2 });
remove.setId("12345");
remove.setVersion(1);
notifDao.removeAggregation(remove, "12345");
final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
assertNotNull(aggr);
assertEquals("12345", aggr.getId());
assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
}
use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.
the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_exchangeLocked_assertAggregationCompletedNotFound.
@Test
public void testGetAggregationCompleted_nonEmptyRepository_exchangeLocked_assertAggregationCompletedNotFound() throws Exception {
final Aggregation insert = new Aggregation();
insert.setExchangeBlob(new byte[] { 0, 3, 2 });
insert.setId("12345");
insert.setVersion(0);
notifDao.addUpdateAggregation(insert);
assertNotNull(notifDao.getAggregation("12345"));
final Aggregation remove = new Aggregation();
remove.setExchangeBlob(new byte[] { 0, 3, 2 });
remove.setId("12345");
remove.setVersion(1);
notifDao.removeAggregation(remove, "12345");
final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
assertNotNull(aggr);
assertEquals("12345", aggr.getId());
assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
final AggregationCompleted lockedAggr = notifDao.getAggregationCompleted("12345", true);
assertNull(lockedAggr);
}
use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.
the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_exchangeLockExpired_assertAggregationCompletedFound.
@Test
public void testGetAggregationCompleted_nonEmptyRepository_exchangeLockExpired_assertAggregationCompletedFound() throws Exception {
final Aggregation insert = new Aggregation();
insert.setExchangeBlob(new byte[] { 0, 3, 2 });
insert.setId("12345");
insert.setVersion(0);
notifDao.addUpdateAggregation(insert);
assertNotNull(notifDao.getAggregation("12345"));
final Aggregation remove = new Aggregation();
remove.setExchangeBlob(new byte[] { 0, 3, 2 });
remove.setId("12345");
remove.setVersion(1);
notifDao.removeAggregation(remove, "12345");
final AggregationCompleted aggr = notifDao.getAggregationCompleted("12345", true);
assertNotNull(aggr);
assertEquals("12345", aggr.getId());
assertTrue(Arrays.equals(insert.getExchangeBlob(), aggr.getExchangeBlob()));
Thread.sleep(4000);
final AggregationCompleted lockedAggr = notifDao.getAggregationCompleted("12345", true);
assertNotNull(lockedAggr);
assertEquals("12345", lockedAggr.getId());
assertTrue(Arrays.equals(insert.getExchangeBlob(), lockedAggr.getExchangeBlob()));
}
use of org.nhindirect.monitor.dao.entity.Aggregation in project nhin-d by DirectProject.
the class AggregationDAOImpl_getAggregationCompletedTest method testGetAggregationCompleted_nonEmptyRepository_keyDoesntExist_assertNoAggregation.
@Test
public void testGetAggregationCompleted_nonEmptyRepository_keyDoesntExist_assertNoAggregation() throws Exception {
final Aggregation insert = new Aggregation();
insert.setExchangeBlob(new byte[] { 0, 3, 2 });
insert.setId("12345");
insert.setVersion(0);
notifDao.addUpdateAggregation(insert);
assertNotNull(notifDao.getAggregation("12345"));
final Aggregation remove = new Aggregation();
remove.setExchangeBlob(new byte[] { 0, 3, 2 });
remove.setId("12345");
remove.setVersion(1);
notifDao.removeAggregation(remove, "12345");
final AggregationCompleted aggr = notifDao.getAggregationCompleted("doenst matter", true);
assertNull(aggr);
}
Aggregations