Search in sources :

Example 51 with Criteria

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

the class FunctionTest method testDistinctTwoFieldsUsingGroupBy.

/**
 * To obtain distinct values this test uses the following group-by clause
 * select table_name, field_name
 * from valid_field_value
 * group by table_name, field_name
 * order by table_name, field_name;
 */
public void testDistinctTwoFieldsUsingGroupBy() {
    try {
        Criteria c = new Criteria();
        c.setTable(ValidFieldValueMeta.getName());
        c.addGroupBy(ValidFieldValueMeta.TABLE_NAME, "tableName");
        c.addGroupBy(ValidFieldValueMeta.FIELD_NAME, "fieldName");
        c.addOrderBy(ValidFieldValueMeta.TABLE_NAME);
        c.addOrderBy(ValidFieldValueMeta.FIELD_NAME);
        Collection col = m_uow.query(c);
        assertEquals("The GroupBy query should have returned 2 records", 2, col.size());
        Map m = null;
        Iterator i = col.iterator();
        m = (Map) i.next();
        assertEquals("tableName", "ZZ_JUT_ITEM", m.get("tableName"));
        assertEquals("fieldName", "KEY_REF", m.get("fieldName"));
        m = (Map) i.next();
        assertEquals("tableName", "ZZ_JUT_ITEM", m.get("tableName"));
        assertEquals("fieldName", "STATUS", m.get("fieldName"));
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : Iterator(java.util.Iterator) Collection(java.util.Collection) Criteria(org.jaffa.persistence.Criteria) Map(java.util.Map)

Example 52 with Criteria

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

the class FunctionTest method testMin.

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

Example 53 with Criteria

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

the class LobTest method testUpdateClob.

/**
 * Creates ZZ_TEST_PART_REMARKS_PICTURE records having clob and blob elements.
 * It then retrieves and updates the rows. Finally it checks if the data has been updated.
 */
public void testUpdateClob() {
    try {
        // create 3 records
        PartRemarksPicture partRemarksPicture = null;
        partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
        partRemarksPicture.updatePart("Z-TESTPART-01");
        partRemarksPicture.updateRemarks(CLOB_FIELD + "01");
        partRemarksPicture.updatePicture((BLOB_FIELD + "01").getBytes());
        m_uow.add(partRemarksPicture);
        partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
        partRemarksPicture.updatePart("Z-TESTPART-02");
        partRemarksPicture.updateRemarks(CLOB_FIELD + "02");
        partRemarksPicture.updatePicture((BLOB_FIELD + "02").getBytes());
        m_uow.add(partRemarksPicture);
        partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
        partRemarksPicture.updatePart("Z-TESTPART-03");
        partRemarksPicture.updateRemarks(CLOB_FIELD + "03");
        partRemarksPicture.updatePicture((BLOB_FIELD + "03").getBytes());
        m_uow.add(partRemarksPicture);
        m_uow.commit();
        // now check if they have been added
        m_uow = new UOW();
        Criteria c = new Criteria();
        c.setTable(PartRemarksPictureMeta.getName());
        c.addCriteria(PartRemarksPictureMeta.PART, Criteria.RELATIONAL_BEGINS_WITH, "Z-");
        c.addOrderBy(PartRemarksPictureMeta.PART, Criteria.ORDER_BY_ASC);
        Collection col = m_uow.query(c);
        // fetch in all the records
        for (Iterator i = col.iterator(); i.hasNext(); ) i.next();
        assertEquals(3, col.size());
        PartRemarksPicture[] partRemarksPictures = (PartRemarksPicture[]) col.toArray(new PartRemarksPicture[0]);
        assertEquals("Z-TESTPART-01", partRemarksPictures[0].getPart());
        assertEquals(CLOB_FIELD + "01", partRemarksPictures[0].getRemarks());
        assertTrue(Arrays.equals((BLOB_FIELD + "01").getBytes(), partRemarksPictures[0].getPicture()));
        assertEquals("Z-TESTPART-02", partRemarksPictures[1].getPart());
        assertEquals(CLOB_FIELD + "02", partRemarksPictures[1].getRemarks());
        assertTrue(Arrays.equals((BLOB_FIELD + "02").getBytes(), partRemarksPictures[1].getPicture()));
        assertEquals("Z-TESTPART-03", partRemarksPictures[2].getPart());
        assertEquals(CLOB_FIELD + "03", partRemarksPictures[2].getRemarks());
        assertTrue(Arrays.equals((BLOB_FIELD + "03").getBytes(), partRemarksPictures[2].getPicture()));
        // now update the records
        partRemarksPictures[0].updateRemarks(null);
        partRemarksPictures[1].updateRemarks("Z-UPDATEDREMARKS-022");
        m_uow.update(partRemarksPictures[0]);
        m_uow.update(partRemarksPictures[1]);
        m_uow.commit();
        // now check if the updates were successful
        m_uow = new UOW();
        c = new Criteria();
        c.setTable(PartRemarksPictureMeta.getName());
        c.addCriteria(PartRemarksPictureMeta.PART, Criteria.RELATIONAL_BEGINS_WITH, "Z-");
        c.addOrderBy(PartRemarksPictureMeta.PART, Criteria.ORDER_BY_ASC);
        col = m_uow.query(c);
        // fetch in all the records
        for (Iterator i = col.iterator(); i.hasNext(); ) i.next();
        assertEquals(3, col.size());
        partRemarksPictures = (PartRemarksPicture[]) col.toArray(new PartRemarksPicture[0]);
        assertNull(partRemarksPictures[0].getRemarks());
        assertEquals("Z-UPDATEDREMARKS-022", partRemarksPictures[1].getRemarks());
        assertEquals(CLOB_FIELD + "03", partRemarksPictures[2].getRemarks());
        // now delete the records
        m_uow.delete(partRemarksPictures[0]);
        m_uow.delete(partRemarksPictures[1]);
        m_uow.delete(partRemarksPictures[2]);
        m_uow.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 54 with Criteria

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

the class LobTest method testAddClobAndBlob.

/**
 * Creates ZZ_TEST_PART_REMARKS_PICTURE records having clob and blob elements. It then checks if the data has been added.
 */
public void testAddClobAndBlob() {
    try {
        // create 3 records
        PartRemarksPicture partRemarksPicture = null;
        partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
        partRemarksPicture.updatePart("Z-TESTPART-01");
        partRemarksPicture.updateRemarks(CLOB_FIELD + "01");
        partRemarksPicture.updatePicture((BLOB_FIELD + "01").getBytes());
        m_uow.add(partRemarksPicture);
        partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
        partRemarksPicture.updatePart("Z-TESTPART-02");
        partRemarksPicture.updateRemarks(CLOB_FIELD + "02");
        partRemarksPicture.updatePicture((BLOB_FIELD + "02").getBytes());
        m_uow.add(partRemarksPicture);
        partRemarksPicture = (PartRemarksPicture) m_uow.newPersistentInstance(PartRemarksPicture.class);
        partRemarksPicture.updatePart("Z-TESTPART-03");
        partRemarksPicture.updateRemarks(CLOB_FIELD + "03");
        partRemarksPicture.updatePicture((BLOB_FIELD + "03").getBytes());
        m_uow.add(partRemarksPicture);
        m_uow.commit();
        // now check if they have been added
        m_uow = new UOW();
        Criteria c = new Criteria();
        c.setTable(PartRemarksPictureMeta.getName());
        c.addCriteria(PartRemarksPictureMeta.PART, Criteria.RELATIONAL_BEGINS_WITH, "Z-");
        c.addOrderBy(PartRemarksPictureMeta.PART, Criteria.ORDER_BY_ASC);
        Collection col = m_uow.query(c);
        // fetch in all the records
        for (Iterator i = col.iterator(); i.hasNext(); ) i.next();
        assertEquals(3, col.size());
        PartRemarksPicture[] partRemarksPictures = (PartRemarksPicture[]) col.toArray(new PartRemarksPicture[0]);
        assertEquals("Z-TESTPART-01", partRemarksPictures[0].getPart());
        assertEquals(CLOB_FIELD + "01", partRemarksPictures[0].getRemarks());
        assertTrue(Arrays.equals((BLOB_FIELD + "01").getBytes(), partRemarksPictures[0].getPicture()));
        assertEquals("Z-TESTPART-02", partRemarksPictures[1].getPart());
        assertEquals(CLOB_FIELD + "02", partRemarksPictures[1].getRemarks());
        assertTrue(Arrays.equals((BLOB_FIELD + "02").getBytes(), partRemarksPictures[1].getPicture()));
        assertEquals("Z-TESTPART-03", partRemarksPictures[2].getPart());
        assertEquals(CLOB_FIELD + "03", partRemarksPictures[2].getRemarks());
        assertTrue(Arrays.equals((BLOB_FIELD + "03").getBytes(), partRemarksPictures[2].getPicture()));
        // now delete the records
        m_uow.delete(partRemarksPictures[0]);
        m_uow.delete(partRemarksPictures[1]);
        m_uow.delete(partRemarksPictures[2]);
        m_uow.commit();
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 55 with Criteria

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

the class ObjectCacheTest method testDisableCaching.

/**
 * Tests if caching can be disabled.
 */
public void testDisableCaching() {
    UOW uow = null;
    final String prefix = "Z-CACHE-";
    final int count = 5;
    final int pad = ("" + count).length();
    try {
        // Disable object caching
        ContextManagerFactory.instance().setThreadContext(null);
        ContextManagerFactory.instance().setProperty("jaffa.persistence.jdbcengine.disableObjectCache", "true");
        uow = new UOW();
        // Create CategoryOfInstrument records
        createCategoryOfInstrument(uow, prefix, count);
        // Read the CategoryOfInstrument records by non-key query
        CategoryOfInstrument[] round1 = new CategoryOfInstrument[count];
        Criteria c = new Criteria();
        c.setTable(CategoryOfInstrumentMeta.getName());
        c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
        c.addOrderBy(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT);
        Iterator itr = uow.query(c).iterator();
        for (int i = 0; i < count; i++) {
            String expected = prefix + StringHelper.pad(i + 1, pad);
            assertTrue("CategoryOfInstrument '" + expected + "' not found", itr.hasNext());
            CategoryOfInstrument obj = (CategoryOfInstrument) itr.next();
            assertEquals(expected, obj.getCategoryInstrument());
            round1[i] = obj;
        }
        assertTrue("No more CategoryOfInstrument records should have been retrieved", !itr.hasNext());
        // Read the CategoryOfInstrument records again by non-key query
        // the 2 sets of objects should not have the same identity
        CategoryOfInstrument[] round2 = new CategoryOfInstrument[count];
        c = new Criteria();
        c.setTable(CategoryOfInstrumentMeta.getName());
        c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
        c.addOrderBy(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT);
        itr = uow.query(c).iterator();
        for (int i = 0; i < count; i++) {
            String expected = prefix + StringHelper.pad(i + 1, pad);
            assertTrue("CategoryOfInstrument '" + expected + "' not found", itr.hasNext());
            CategoryOfInstrument obj = (CategoryOfInstrument) itr.next();
            assertEquals(expected, obj.getCategoryInstrument());
            round2[i] = obj;
        }
        assertTrue("No more CategoryOfInstrument records should have been retrieved", !itr.hasNext());
        for (int i = 0; i < count; i++) {
            assertTrue(round1[i].equals(round2[i]));
            assertTrue(round1[i] != round2[i]);
        }
        // Now retrieve the next set of objects by their primary-key
        // the 2 sets of objects should again not have the same identity
        CategoryOfInstrument[] round3 = new CategoryOfInstrument[count];
        for (int i = 0; i < count; i++) {
            String key = prefix + StringHelper.pad(i + 1, pad);
            CategoryOfInstrument obj = CategoryOfInstrument.findByPK(uow, key);
            round3[i] = obj;
        }
        for (int i = 0; i < count; i++) {
            assertTrue(round2[i].equals(round3[i]));
            assertTrue(round2[i] != round3[i]);
        }
        // Delete CategoryOfInstrument records
        deleteCategoryOfInstrument(uow, prefix);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    } finally {
        try {
            if (uow != null)
                uow.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
}
Also used : Iterator(java.util.Iterator) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

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