Search in sources :

Example 56 with Criteria

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

the class ObjectCacheTest method testCaching.

/**
 * Tests if the key-based retrieval of data brings up cached objects.
 */
public void testCaching() {
    UOW uow = null;
    final String prefix = "Z-CACHE-";
    final int count = 5;
    final int pad = ("" + count).length();
    try {
        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
        // This set of objects should match the previous set exactly
        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)

Example 57 with Criteria

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

the class ObjectCacheTest method deleteCategoryOfInstrument.

private void deleteCategoryOfInstrument(UOW uow, String prefix) throws Exception {
    Criteria c = new Criteria();
    c.setTable(CategoryOfInstrumentMeta.getName());
    c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
    for (Iterator i = uow.query(c).iterator(); i.hasNext(); ) uow.delete(i.next());
}
Also used : Iterator(java.util.Iterator) Criteria(org.jaffa.persistence.Criteria)

Example 58 with Criteria

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

the class PagingTest method deleteCategoryOfInstrument.

private void deleteCategoryOfInstrument(UOW uow, String prefix) throws Exception {
    Criteria c = new Criteria();
    c.setTable(CategoryOfInstrumentMeta.getName());
    c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
    for (Iterator i = uow.query(c).iterator(); i.hasNext(); ) uow.delete(i.next());
}
Also used : Iterator(java.util.Iterator) Criteria(org.jaffa.persistence.Criteria)

Example 59 with Criteria

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

the class PagingTest method testPaging.

/**
 * Creates 95 CategoryOfInstrument records and then reads them in batces of 25.
 */
public void testPaging() {
    // org.apache.log4j.BasicConfigurator.configure();
    UOW uow = null;
    final String prefix = "Z-PAGING-";
    final int count = 95;
    final int batch = 25;
    final int pad = ("" + count).length();
    try {
        uow = new UOW();
        // Create CategoryOfInstrument records
        createCategoryOfInstrument(uow, prefix, count);
        // Read CategoryOfInstrument records in batches
        int loops = count / batch;
        if (count % batch > 0)
            ++loops;
        for (int i = 0; i < loops; i++) {
            Criteria c = new Criteria();
            c.setTable(CategoryOfInstrumentMeta.getName());
            c.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, Criteria.RELATIONAL_BEGINS_WITH, prefix);
            c.addOrderBy(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT);
            c.setFirstResult(i * batch);
            c.setMaxResults(batch);
            Iterator itr = uow.query(c).iterator();
            int batchSize = (i * batch) + batch <= count ? batch : count - (i * batch);
            for (int j = 0; j < batchSize; j++) {
                String expected = prefix + StringHelper.pad((i * batch) + j + 1, pad);
                assertTrue("CategoryOfInstrument '" + expected + "' not found", itr.hasNext());
                CategoryOfInstrument obj = (CategoryOfInstrument) itr.next();
                assertEquals(expected, obj.getCategoryInstrument());
            }
            assertTrue("No more CategoryOfInstrument records should have been retrieved", !itr.hasNext());
        }
        // 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)

Example 60 with Criteria

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

the class PerformanceTest method deleteItems.

/**
 * Deletes Item records having item_id like 'itemIdBeginsWith%'.
 * This will commit the UOW and then acquire a new UOW.
 */
private void deleteItems(String itemIdBeginsWith) throws Exception {
    Criteria c = new Criteria();
    c.setTable(ItemMeta.getName());
    c.addCriteria(ItemMeta.ITEM_ID, Criteria.RELATIONAL_BEGINS_WITH, itemIdBeginsWith);
    for (Iterator itr = m_uow.query(c).iterator(); itr.hasNext(); ) {
        Item item = (Item) itr.next();
        m_uow.delete(item);
    }
    // commit the exiting UOW and create a new one
    m_uow.commit();
    m_uow = new UOW();
}
Also used : 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