Search in sources :

Example 41 with Criteria

use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.

the class CharFieldTest method testNotNullQuery.

/**
 * Tests the sql:
 * Select * from zz_jut_part_additional where field1 is not null and field1 != ' '
 * This should return 1 record
 */
public void testNotNullQuery() {
    UOW uow = null;
    try {
        uow = new UOW();
        // Create a new object
        PartAdditional object = (PartAdditional) uow.newPersistentInstance(PartAdditional.class);
        object.setPart(PART);
        // enter at least a blank string, since the field is mandatory
        object.setField1(" ");
        object.setField2("A VALUE");
        uow.add(object);
        uow.commit();
        // Retrieve records where field2 is not null. Should get 1 record
        uow = new UOW();
        Criteria c = new Criteria();
        c.setTable(PartAdditionalMeta.getName());
        c.addCriteria(PartAdditionalMeta.FIELD2, Criteria.RELATIONAL_IS_NOT_NULL);
        Collection col = uow.query(c);
        assertEquals("One record should have been retrieved", 1, col.size());
        object = (PartAdditional) col.iterator().next();
        assertEquals(PART, object.getPart());
        assertNull(object.getField1());
        assertEquals("A VALUE", object.getField2());
        assertNull(object.getField3());
        // Retrieve records where field1 is not null. Nothing should be retrieved
        uow = new UOW();
        c = new Criteria();
        c.setTable(PartAdditionalMeta.getName());
        c.addCriteria(PartAdditionalMeta.FIELD1, Criteria.RELATIONAL_IS_NOT_NULL);
        col = uow.query(c);
        assertEquals("No record should have been retrieved", 0, col.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    } finally {
        try {
            if (uow != null)
                uow.rollback();
        } catch (Exception e) {
        // do nothing
        }
        cleanupData(PART);
    }
}
Also used : PartAdditional(org.jaffa.persistence.domainobjects.PartAdditional) Collection(java.util.Collection) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 42 with Criteria

use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.

the class DeleteTest method testCheckRollbackAfterDelete.

/**
 * Deletes a record from SYCD using the following query:
 * delete from sycd WHERE condition='Z-TESTSYCD-03'
 * It then does a rollback. This is followed by a commit.
 * The test fails if the record is deleted.
 */
public void testCheckRollbackAfterDelete() {
    try {
        Criteria c = new Criteria();
        c.setTable(ConditionMeta.getName());
        c.addCriteria(ConditionMeta.CONDITION, "Z-TESTSYCD-03");
        Iterator i = m_uow.query(c).iterator();
        Condition sycd = (Condition) i.next();
        m_uow.delete(sycd);
        m_uow.rollback();
        // Now ensure the record isn't deleted
        m_uow = new UOW();
        c = new Criteria();
        c.setTable(ConditionMeta.getName());
        c.addCriteria(ConditionMeta.CONDITION, "Z-TESTSYCD-03");
        i = m_uow.query(c).iterator();
        if (!i.hasNext())
            fail("Record has been deleted 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 43 with Criteria

use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.

the class DeleteTest method testCheckRollbackAfterDeleteUsingProxy.

/**
 * Deletes a record from SYCD using the following query:
 * delete from sycd WHERE condition='Z-TESTSYCD-03'
 * It then does a rollback. This is followed by a commit.
 * The test fails if the record is deleted.
 */
public void testCheckRollbackAfterDeleteUsingProxy() {
    try {
        Criteria c = new Criteria();
        c.setTable(ICondition.class.getName());
        c.addCriteria(ICondition.CONDITION, "Z-TESTSYCD-03");
        Iterator i = m_uow.query(c).iterator();
        ICondition sycd = (ICondition) i.next();
        m_uow.delete(sycd);
        m_uow.rollback();
        // Now ensure the record isn't deleted
        m_uow = new UOW();
        c = new Criteria();
        c.setTable(ICondition.class.getName());
        c.addCriteria(ICondition.CONDITION, "Z-TESTSYCD-03");
        i = m_uow.query(c).iterator();
        if (!i.hasNext())
            fail("Record has been deleted 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 44 with Criteria

use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.

the class DeleteTest method testDeleteCondition.

/**
 * Executes the following query:
 * delete from sycd WHERE condition='Z-TESTSYCD-02'
 * It then ensures that the record was got deleted.
 */
public void testDeleteCondition() {
    try {
        Criteria c = new Criteria();
        c.setTable(ConditionMeta.getName());
        c.addCriteria(ConditionMeta.CONDITION, "Z-TESTSYCD-02");
        Iterator i = m_uow.query(c).iterator();
        Condition sycd = (Condition) i.next();
        String description = sycd.getDescription();
        m_uow.delete(sycd);
        m_uow.commit();
        // Now ensure that the record got deleted
        m_uow = new UOW();
        c = new Criteria();
        c.setTable(ConditionMeta.getName());
        c.addCriteria(ConditionMeta.CONDITION, "Z-TESTSYCD-02");
        i = m_uow.query(c).iterator();
        if (i.hasNext())
            fail();
        // Finally, recreate the record
        m_uow = new UOW();
        sycd = (Condition) m_uow.newPersistentInstance(Condition.class);
        sycd.setCondition("Z-TESTSYCD-02");
        sycd.setDescription(description);
        m_uow.add(sycd);
        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)

Example 45 with Criteria

use of org.jaffa.persistence.Criteria in project jaffa-framework by jaffa-projects.

the class FunctionTest method testMax.

/**
 * Tests the max() function
 * select max(CREATED_DATETIME) from item;
 */
public void testMax() {
    try {
        Criteria c = new Criteria();
        c.setTable(ItemMeta.getName());
        c.addFunction(Criteria.FUNCTION_MAX, ItemMeta.CREATED_DATETIME, "max");
        Map m = (Map) m_uow.query(c).iterator().next();
        assertEquals("max(CREATED_DATETIME)", new DateTime(2003, DateTime.SEPTEMBER, 10, 20, 30, 40, 0), m.get("max"));
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : Criteria(org.jaffa.persistence.Criteria) Map(java.util.Map) DateTime(org.jaffa.datatypes.DateTime)

Aggregations

Criteria (org.jaffa.persistence.Criteria)300 UOW (org.jaffa.persistence.UOW)136 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)124 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)66 Iterator (java.util.Iterator)53 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)39 TransactionCriteria (org.jaffa.transaction.apis.data.TransactionCriteria)32 TransactionFieldCriteria (org.jaffa.transaction.apis.data.TransactionFieldCriteria)32 OrderByField (org.jaffa.components.finder.OrderByField)28 Map (java.util.Map)23 ArrayList (java.util.ArrayList)17 DateTime (org.jaffa.datatypes.DateTime)16 FrameworkException (org.jaffa.exceptions.FrameworkException)14 ApplicationException (org.jaffa.exceptions.ApplicationException)13 DuplicateKeyException (org.jaffa.exceptions.DuplicateKeyException)13 Transaction (org.jaffa.transaction.domain.Transaction)13 Collection (java.util.Collection)12 LinkedHashMap (java.util.LinkedHashMap)11 IPersistent (org.jaffa.persistence.IPersistent)11 HashMap (java.util.HashMap)9