Search in sources :

Example 16 with Office

use of org.jpox.samples.models.company.Office in project tests by datanucleus.

the class JDOQLBasicTest method testAnalysisRollup.

/**
 * Tests the Analsys.rollup() expression
 */
public void testAnalysisRollup() {
    RDBMSStoreManager srm = (RDBMSStoreManager) storeMgr;
    if (!srm.getDatastoreAdapter().supportsOption(DatastoreAdapter.ANALYSIS_METHODS)) {
        // Not supported so it passed :-)
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist some simple objects (uses datastore id, or composite application id depending on suite)
        tx.begin();
        Office o1 = new Office(1, "Green", "Big spacious office");
        Calendar cal1 = Calendar.getInstance();
        cal1.set(2004, 1, 1);
        o1.setDate(cal1.getTime());
        Office o2 = new Office(2, "Blue", "Pokey office at the back of the building");
        Calendar cal2 = Calendar.getInstance();
        cal2.set(2005, 1, 1);
        o2.setDate(cal2.getTime());
        Office o3 = new Office(1, "Yellow", "Massive open plan office");
        Calendar cal3 = Calendar.getInstance();
        cal3.set(2005, 1, 1);
        o3.setDate(cal3.getTime());
        pm.newQuery(Office.class).deletePersistentAll();
        pm.makePersistent(o1);
        pm.makePersistent(o2);
        pm.makePersistent(o3);
        tx.commit();
        Object[] officeIds = new Object[3];
        officeIds[0] = pm.getObjectId(o1);
        officeIds[1] = pm.getObjectId(o2);
        officeIds[2] = pm.getObjectId(o3);
        // TODO This throws an exception about "JDOQL query has result clause PrimaryExpression{roomName} but this is invalid (see JDO spec 14.6.10). When specified with grouping should be aggregate, or grouping expression"
        tx.begin();
        Query q = pm.newQuery(Office.class);
        q.setGrouping("Analysis.rollup({roomName})");
        q.setOrdering("roomName ascending");
        q.setResult("roomName,count(floor)");
        Collection c = (Collection) q.execute();
        assertEquals(4, c.size());
        Iterator it = c.iterator();
        Object[] obj = ((Object[]) it.next());
        assertEquals("Blue", obj[0]);
        assertEquals(1, ((Long) obj[1]).longValue());
        obj = ((Object[]) it.next());
        assertEquals(1, ((Long) obj[1]).longValue());
        assertEquals("Green", obj[0]);
        obj = ((Object[]) it.next());
        assertEquals(1, ((Long) obj[1]).longValue());
        assertEquals("Yellow", obj[0]);
        assertEquals(1, ((Long) obj[1]).longValue());
        obj = ((Object[]) it.next());
        assertNull(obj[0]);
        assertEquals(3, ((Long) obj[1]).longValue());
        q = pm.newQuery(Office.class);
        q.setGrouping("Analysis.rollup({date})");
        q.setOrdering("date ascending");
        q.setResult("date,count(floor)");
        c = (Collection) q.execute();
        assertEquals(3, c.size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        // Clean out our data
        clean(Office.class);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Calendar(java.util.Calendar) Iterator(java.util.Iterator) Collection(java.util.Collection) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

Office (org.jpox.samples.models.company.Office)16 PersistenceManager (javax.jdo.PersistenceManager)15 Transaction (javax.jdo.Transaction)15 Query (javax.jdo.Query)14 Collection (java.util.Collection)8 JDOUserException (javax.jdo.JDOUserException)8 List (java.util.List)6 JDOException (javax.jdo.JDOException)6 Department (org.jpox.samples.models.company.Department)6 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)6 ArrayList (java.util.ArrayList)5 Iterator (java.util.Iterator)4 ListIterator (java.util.ListIterator)3 Manager (org.jpox.samples.models.company.Manager)3 NoSuchElementException (java.util.NoSuchElementException)2 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)2 Calendar (java.util.Calendar)1 JDODataStoreException (javax.jdo.JDODataStoreException)1