use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.
the class ForeignKeyFieldValidator method validate.
/**
* The RulesEngine will invoke this method to perform the field validation.
* @throws ValidationException if any validation rule fails.
* @throws FrameworkException if any framework error occurs.
*/
public void validate() throws ValidationException, FrameworkException {
if (getValue() != null) {
UOW uow = getUow();
boolean localUow = (uow == null);
try {
if (localUow)
uow = new UOW();
Criteria c = new Criteria();
c.setTable(getDomainClass());
c.addCriteria(getProperty(), getValue());
Collection col = uow.query(c);
if (col.size() == 0) {
String str = "ForeignKey validation failed for the value '" + getValue() + "' against the domainClass/field - " + getDomainClass() + '/' + getProperty();
log.error(str);
throw new InvalidForeignKeyException(getLabelToken(), new Object[] { getLabelToken(getDomainClass()), getLabelToken(getDomainClass(), getProperty()) });
}
} finally {
if (localUow && uow != null)
uow.rollback();
}
}
}
use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.
the class GenericForeignKeyFieldValidator method validate.
/**
* The RulesEngine will invoke this method to perform the field validation.
* @throws ValidationException if any validation rule fails.
* @throws FrameworkException if any framework error occurs.
*/
public void validate() throws ValidationException, FrameworkException {
if (getValue() != null) {
UOW uow = getUow();
boolean localUow = (uow == null);
try {
if (localUow)
uow = new UOW();
Criteria c = new Criteria();
c.setTable(getDomainClassName());
c.addCriteria(getFieldNameForTable(), getTableName());
c.addCriteria(getFieldNameForField(), getFieldName());
c.addCriteria(getFieldNameForValue(), getValue().toString());
Collection col = uow.query(c);
if (col.size() == 0) {
// Invalid value. Display the list of valid values in the error message
StringBuffer validValues = new StringBuffer();
c = new Criteria();
c.setTable(getDomainClassName());
c.addCriteria(getFieldNameForTable(), getTableName());
c.addCriteria(getFieldNameForField(), getFieldName());
c.addOrderBy(getFieldNameForValue(), Criteria.ORDER_BY_ASC);
for (Iterator i = uow.query(c).iterator(); i.hasNext(); ) {
try {
Object value = BeanHelper.getField(i.next(), getFieldNameForValue());
if (validValues.length() > 0)
validValues.append(',');
validValues.append(value);
} catch (Exception e) {
// do nothing
}
}
String str = "Generic ForeignKey validation failed for the value '" + getValue() + "' against the table/field - " + getTableName() + '/' + getFieldName() + ". Valid values are " + validValues.toString();
log.error(str);
throw new InvalidGenericForeignKeyException(getLabelToken(), new Object[] { getTableName(), getFieldName(), validValues.toString() });
}
} finally {
if (localUow && uow != null)
uow.rollback();
}
}
}
use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.
the class AddTest method testAddDateTime.
/**
* Inserts a record into ITEM using the query:
* insert into ITEM(ITEM_ID, CREATED_DATETIME) values('Z-TESTITEM-04', to_date('2003-09-10 20:30:40', 'yyyy-MM-dd hh24:mi:ss')).
* It then checks if the record was added. Finally the record is deleted
*/
public void testAddDateTime() {
try {
String itemId = "Z-TESTITEM-04";
DateTime datetime = new DateTime(2003, DateTime.SEPTEMBER, 10, 20, 30, 40, 0);
Item obj = (Item) m_uow.newPersistentInstance(Item.class);
obj.updateItemId(itemId);
obj.updateCreatedDatetime(datetime);
m_uow.add(obj);
m_uow.commit();
// Now retrieve the added record & check if it was correctly added
m_uow = new UOW();
Criteria c = new Criteria();
c.setTable(ItemMeta.getName());
c.addCriteria(ItemMeta.ITEM_ID, itemId);
Iterator i = m_uow.query(c).iterator();
Item item = (Item) i.next();
assertEquals(itemId, item.getItemId());
assertEquals(datetime, item.getCreatedDatetime());
// Now delete the bugger
m_uow.delete(item);
m_uow.commit();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.
the class AddTest method testCheckRollbackAfterAdd.
/**
* Inserts a record into SYCI using the query:
* insert into SYCI(CATEGORY_INSTRUMENT, DESCRIPTION) values('Z-TESTCI-03', 'Z-TESTCIDESC-03').
* It then does a rollback. This is followed by a commit. The test fails if the record is added.
*/
public void testCheckRollbackAfterAdd() {
try {
CategoryOfInstrument obj = (CategoryOfInstrument) m_uow.newPersistentInstance(CategoryOfInstrument.class);
obj.updateCategoryInstrument("Z-TESTCI-03");
obj.updateDescription("Z-TESTCIDESC-03");
m_uow.add(obj);
m_uow.rollback();
// Now ensure the record isn't added
m_uow = new UOW();
Criteria c = new Criteria();
c.setTable(CategoryOfInstrumentMeta.getName());
c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, "Z-TESTCI-03");
Iterator i = m_uow.query(c).iterator();
if (i.hasNext()) {
// delete the record & fail the test
CategoryOfInstrument syci = (CategoryOfInstrument) i.next();
m_uow.delete(syci);
m_uow.commit();
fail("Record has been added even after a rollback");
}
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.jaffa.persistence.UOW in project jaffa-framework by jaffa-projects.
the class AddTest method testCreateCategoryOfInstrumentUsingProxy.
/**
* Inserts a record into SYCI using the query:
* insert into SYCI(CATEGORY_INSTRUMENT, DESCRIPTION) values('Z-TESTCI-02', 'Z-TESTCIDESC-02').
* It then checks if the record was added. Finally the record is deleted
*/
public void testCreateCategoryOfInstrumentUsingProxy() {
try {
ICategoryOfInstrument obj = (ICategoryOfInstrument) m_uow.newPersistentInstance(ICategoryOfInstrument.class);
obj.setCategoryInstrument("Z-TESTCI-02");
obj.setDescription("Z-TESTCIDESC-02");
obj.setSupportEquip(Boolean.FALSE);
m_uow.add(obj);
m_uow.commit();
// Now retrieve the added record & check if it was correctly added
m_uow = new UOW();
Criteria c = new Criteria();
c.setTable(ICategoryOfInstrument.class.getName());
c.addCriteria(ICategoryOfInstrument.CATEGORY_INSTRUMENT, "Z-TESTCI-02");
Iterator i = m_uow.query(c).iterator();
ICategoryOfInstrument syci = (ICategoryOfInstrument) i.next();
assertEquals("Z-TESTCI-02", syci.getCategoryInstrument());
assertEquals("Z-TESTCIDESC-02", syci.getDescription());
assertEquals(Boolean.FALSE, syci.getSupportEquip());
assertNull(syci.getCalculateMtbf());
// Now delete the bugger
m_uow.delete(syci);
m_uow.commit();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
Aggregations