Search in sources :

Example 11 with DateTime

use of org.jaffa.datatypes.DateTime in project jaffa-framework by jaffa-projects.

the class FunctionTest method testGroupBy.

/**
 * Tests the group-by clause
 * select table_name, field_name, current_timestamp, count(*), max(legal_value), min(legal_value)
 * from valid_field_value
 * group by table_name, field_name
 * order by table_name, field_name;
 */
public void testGroupBy() {
    try {
        Criteria c = new Criteria();
        c.setTable(ValidFieldValueMeta.getName());
        c.addFunction(Criteria.FUNCTION_CURRENT_DATE_TIME, null, "sysdate");
        c.addFunction(Criteria.FUNCTION_COUNT, null, "count(*)");
        c.addFunction(Criteria.FUNCTION_MAX, ValidFieldValueMeta.LEGAL_VALUE, "max");
        c.addFunction(Criteria.FUNCTION_MIN, ValidFieldValueMeta.LEGAL_VALUE, "min");
        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"));
        assertEquals("count(*)", 3, ((Number) m.get("count(*)")).intValue());
        assertEquals("max", "Z-TEST-KEY-REF-3", m.get("max"));
        assertEquals("min", "Z-TEST-KEY-REF", m.get("min"));
        m = (Map) i.next();
        assertEquals("tableName", "ZZ_JUT_ITEM", m.get("tableName"));
        assertEquals("fieldName", "STATUS", m.get("fieldName"));
        assertEquals("count(*)", 8, ((Number) m.get("count(*)")).intValue());
        assertEquals("max", "Y", m.get("max"));
        assertEquals("min", "1", m.get("min"));
        DateTime dbValue = (DateTime) m.get("sysdate");
        DateTime localValue = new DateTime();
        assertEquals("Year on the database server does not match the current value", localValue.year(), dbValue.year());
        assertEquals("Month on the database server does not match the current value", localValue.month(), dbValue.month());
    } 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) DateTime(org.jaffa.datatypes.DateTime)

Example 12 with DateTime

use of org.jaffa.datatypes.DateTime 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 13 with DateTime

use of org.jaffa.datatypes.DateTime in project jaffa-framework by jaffa-projects.

the class QueryTest method testDateTimeQuery.

/**
 * Tests the sql:
 * Select * from item Where
 * created_datetime = to_date('2003-09-10 20:30:40', 'yyyy-MM-dd hh24:mi:ss')
 * or created_datetime = to_date('2003-09-10', 'yyyy-MM-dd')
 * or created_datetime is null
 * order by item_id
 * This should return 3 item records
 */
public void testDateTimeQuery() {
    try {
        Criteria c = new Criteria();
        c.addCriteria(ItemMeta.CREATED_DATETIME, new DateTime(2003, DateTime.SEPTEMBER, 10, 20, 30, 40, 0));
        c.addOrCriteria(ItemMeta.CREATED_DATETIME, new DateTime(2003, DateTime.SEPTEMBER, 10));
        c.addOrCriteria(ItemMeta.CREATED_DATETIME, Criteria.RELATIONAL_IS_NULL);
        c.setTable(ItemMeta.getName());
        c.addOrderBy(ItemMeta.ITEM_ID, Criteria.ORDER_BY_ASC);
        Iterator i = m_uow.query(c).iterator();
        Item item = (Item) i.next();
        assertEquals("Z-TESTITEM-01", item.getItemId());
        assertEquals(new DateTime(2003, DateTime.SEPTEMBER, 10, 20, 30, 40, 0), item.getCreatedDatetime());
        item = ((Item) i.next());
        assertEquals("Z-TESTITEM-02", item.getItemId());
        assertEquals(new DateTime(2003, DateTime.SEPTEMBER, 10), item.getCreatedDatetime());
        item = ((Item) i.next());
        assertNull(item.getCreatedDatetime());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DateTime(org.jaffa.datatypes.DateTime)

Example 14 with DateTime

use of org.jaffa.datatypes.DateTime in project jaffa-framework by jaffa-projects.

the class QueryTest method testBasicQueryUsingProxy.

/**
 * Tests the sql:
 * Select * from item where item_id like 'Z-TEST%'
 * and condition like '%TESTSYCD%'
 * and part = 'Z-TESTPART-01�
 * and sc is not null
 * and qty > 1 and qty < 3
 * and key_ref like '%TEST-KEY-REF'
 * order by item_id
 * This should return 3 item records
 */
public void testBasicQueryUsingProxy() {
    try {
        Criteria c = new Criteria();
        c.setTable(IItem.class.getName());
        c.addCriteria(IItem.ITEM_ID, Criteria.RELATIONAL_BEGINS_WITH, "Z-TEST");
        c.addCriteria(IItem.CONDITION, Criteria.RELATIONAL_LIKE, "TESTSYCD");
        c.addCriteria(IItem.PART, "Z-TESTPART-01");
        c.addCriteria(IItem.SC, Criteria.RELATIONAL_IS_NOT_NULL);
        c.addCriteria(IItem.QTY, Criteria.RELATIONAL_GREATER_THAN, new Long(1));
        c.addCriteria(IItem.QTY, Criteria.RELATIONAL_SMALLER_THAN, new Long(3));
        c.addCriteria(IItem.KEY_REF, Criteria.RELATIONAL_ENDS_WITH, "TEST-KEY-REF");
        c.addOrderBy(IItem.ITEM_ID, Criteria.ORDER_BY_ASC);
        // The following criteria points to an undefined mapping, and should be ignored and not cause any exception
        c.addCriteria("UnmappedField", "ZZZZ");
        Iterator i = m_uow.query(c).iterator();
        IItem item = null;
        item = ((IItem) i.next());
        assertEquals("Z-TESTITEM-01", item.getItemId());
        assertEquals("SOME SC", item.getSc());
        assertEquals("Z-TESTPART-01", item.getPart());
        assertEquals("Z-TESTPRIME-01", item.getPrime());
        assertEquals("Z-TESTSYCD-01", item.getCondition());
        assertEquals(2, item.getQty().intValue());
        assertEquals(new DateTime(2003, DateTime.SEPTEMBER, 10, 20, 30, 40, 0), item.getCreatedDatetime());
        item = ((IItem) i.next());
        assertEquals("Z-TESTITEM-02", item.getItemId());
        assertEquals("SOME SC", item.getSc());
        assertEquals("Z-TESTPART-01", item.getPart());
        assertEquals("Z-TESTPRIME-01", item.getPrime());
        assertEquals("Z-TESTSYCD-01", item.getCondition());
        assertEquals(2, item.getQty().intValue());
        assertEquals(new DateTime(2003, DateTime.SEPTEMBER, 10), item.getCreatedDatetime());
        item = ((IItem) i.next());
        assertEquals("Z-TESTITEM-03", item.getItemId());
        assertEquals("SOME SC", item.getSc());
        assertEquals("Z-TESTPART-01", item.getPart());
        assertEquals("Z-TESTPRIME-01", item.getPrime());
        assertEquals("Z-TESTSYCD-01", item.getCondition());
        assertEquals(2, item.getQty().intValue());
        assertNull(item.getCreatedDatetime());
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DateTime(org.jaffa.datatypes.DateTime)

Example 15 with DateTime

use of org.jaffa.datatypes.DateTime in project jaffa-framework by jaffa-projects.

the class UpdateTest method testUpdateDateTimeUsingProxy.

/**
 * Executes the following query:
 * update item set created_datetime=to_date('2003-10-10', 'yyyy-MM-dd') where item_id='Z-TESTITEM-01'
 * It then ensures that the record was properly updated and then resets it back to the original value
 */
public void testUpdateDateTimeUsingProxy() {
    try {
        Criteria c = new Criteria();
        c.setTable(IItem.class.getName());
        c.addCriteria(IItem.ITEM_ID, "Z-TESTITEM-01");
        Iterator i = m_uow.query(c).iterator();
        IItem item = (IItem) i.next();
        DateTime originalCreatedDatetime = item.getCreatedDatetime();
        DateTime newCreatedDatetime = new DateTime(2003, DateTime.OCTOBER, 10);
        item.setCreatedDatetime(newCreatedDatetime);
        m_uow.update(item);
        m_uow.commit();
        // Now ensure that the record got updated
        m_uow = new UOW();
        c = new Criteria();
        c.setTable(IItem.class.getName());
        c.addCriteria(IItem.ITEM_ID, "Z-TESTITEM-01");
        i = m_uow.query(c).iterator();
        item = (IItem) i.next();
        assertEquals(newCreatedDatetime, item.getCreatedDatetime());
        // Now reset to the original value
        item.setCreatedDatetime(originalCreatedDatetime);
        m_uow.update(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)

Aggregations

DateTime (org.jaffa.datatypes.DateTime)74 Criteria (org.jaffa.persistence.Criteria)16 UOW (org.jaffa.persistence.UOW)11 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)9 PreparedStatement (java.sql.PreparedStatement)8 ValidationException (org.jaffa.datatypes.ValidationException)8 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)8 Iterator (java.util.Iterator)7 Map (java.util.Map)5 Date (java.util.Date)4 FrameworkException (org.jaffa.exceptions.FrameworkException)4 LinkedHashMap (java.util.LinkedHashMap)3 JMSException (javax.jms.JMSException)3 MarshallException (org.directwebremoting.extend.MarshallException)3 CheckBoxModel (org.jaffa.presentation.portlet.widgets.model.CheckBoxModel)3 DateTimeModel (org.jaffa.presentation.portlet.widgets.model.DateTimeModel)3 DropDownModel (org.jaffa.presentation.portlet.widgets.model.DropDownModel)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Enumeration (java.util.Enumeration)2 LinkedList (java.util.LinkedList)2