Search in sources :

Example 21 with UOW

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();
        }
    }
}
Also used : InvalidForeignKeyException(org.jaffa.datatypes.exceptions.InvalidForeignKeyException) Collection(java.util.Collection) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 22 with UOW

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();
        }
    }
}
Also used : InvalidGenericForeignKeyException(org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW) InvalidGenericForeignKeyException(org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException) ValidationException(org.jaffa.datatypes.ValidationException) FrameworkException(org.jaffa.exceptions.FrameworkException)

Example 23 with UOW

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();
    }
}
Also used : Iterator(java.util.Iterator) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW) DateTime(org.jaffa.datatypes.DateTime)

Example 24 with UOW

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();
    }
}
Also used : Iterator(java.util.Iterator) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 25 with UOW

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();
    }
}
Also used : Iterator(java.util.Iterator) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Aggregations

UOW (org.jaffa.persistence.UOW)259 Criteria (org.jaffa.persistence.Criteria)138 FrameworkException (org.jaffa.exceptions.FrameworkException)99 ApplicationException (org.jaffa.exceptions.ApplicationException)88 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)87 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)82 Iterator (java.util.Iterator)47 TransactionCriteria (org.jaffa.transaction.apis.data.TransactionCriteria)33 TransactionFieldCriteria (org.jaffa.transaction.apis.data.TransactionFieldCriteria)33 ArrayList (java.util.ArrayList)19 Transaction (org.jaffa.transaction.domain.Transaction)19 Map (java.util.Map)16 LinkedHashMap (java.util.LinkedHashMap)13 HashMap (java.util.HashMap)12 DateTime (org.jaffa.datatypes.DateTime)11 FormTemplate (org.jaffa.modules.printing.domain.FormTemplate)10 IPersistent (org.jaffa.persistence.IPersistent)10 Method (java.lang.reflect.Method)9 Collection (java.util.Collection)8 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)8