Changeset 30


Ignore:
Timestamp:
Apr 9, 2011, 12:44:03 PM (14 years ago)
Author:
fmguler
Message:

Fixes #3 - Criteria object is mostly implemented. An override of Ven.list() having criteria parameter is added. Old codebase only had string based criteria. In this commit, I am also adding typed criteria as a new feauture. With this new criteria system, user can create criteria object like;

new Criteria().eq(attr,value).like(attr,value).and().gt(attr, value).or().orderAsc(attr).orderDesc(attr).limit(limit, offset);

The orderings and the limit/offset values are not taken into account in the Ven (these are trivial, I will do them later).

Not thouroughly tested, because complex scenarios are only possible when applying to a real problem in a real application (which I am planning to do next).

Location:
trunk/fmgVen
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/fmgVen/src/com/fmguler/ven/Ven.java

    r29 r30  
    9797 
    9898    /** 
     99     * List the objects of the specified objectClass type, filtering according to some criteria. 
     100     * <p> 
     101     * By default none of the associations will be retrieved. 
     102     * To include the object associations (retrieve the object graph) joins should be specified, e.g. 
     103     * <code>SomeObject.anotherObject</code> 
     104     *  
     105     * @param objectClass the class of the objects to be retrieved 
     106     * @param joins the set of object graphs to be included with objects 
     107     * @param criteria to filter and order the result according to some criteria 
     108     * @return the list of objects including the specified associations filtered according to the specified criteria 
     109     */ 
     110    public List list(Class objectClass, Set joins, Criteria criteria) { 
     111        String query = generator.generateSelectQuery(objectClass, joins); 
     112        query += " where 1=1 " + criteria.criteriaStringToSQL() + " and " + criteria.criteriaToSQL(); 
     113 
     114        if (debug) System.out.println("Ven - SQL: " + query); 
     115 
     116        List result = mapper.list(query, criteria.getParameters(), objectClass); 
     117        return result; 
     118    } 
     119 
     120    /** 
    99121     * Save the object. If it has a non null (or non zero) "id" property it will be updated. 
    100122     * It will be inserted otherwise. 
  • trunk/fmgVen/test/com/fmguler/ven/sample/Sample.java

    r29 r30  
    1818package com.fmguler.ven.sample; 
    1919 
     20import com.fmguler.ven.Criteria; 
    2021import com.fmguler.ven.LiquibaseUtil; 
    2122import com.fmguler.ven.Ven; 
     
    4344        //list the objects 
    4445        testList(); 
     46        //list the objects by some criteria string 
     47        testListByCriteriaString(); 
     48        //list the objects by some typed criteria 
     49        testListByCriteriaObject(); 
    4550        //delete an object 
    4651        testDelete(); 
     
    112117        joins.add("SomeDomainObject.anotherDomainObject"); 
    113118        List objList = ven.list(SomeDomainObject.class, joins); 
    114          
     119 
    115120        Iterator it = objList.iterator(); 
    116121        while (it.hasNext()) { 
     
    121126 
    122127    /** 
    123      * Test list the collection of objects by some criteria 
     128     * Test list the collection of objects by some criteria (string) 
    124129     */ 
    125     public static void testListByCriteria() { 
     130    public static void testListByCriteriaString() { 
    126131        Ven ven = getVen(); 
    127         //List objList = ven.list(SomeDomainObject.class/*, criteria */); 
    128         //System.out.println(objList); 
     132 
     133        //these objects will be included 
     134        Set joins = new HashSet(); 
     135        joins.add("SomeDomainObject.anotherDomainObjects"); 
     136        joins.add("SomeDomainObject.anotherDomainObject"); 
     137 
     138        //the results will be filtered according to this criteria 
     139        Criteria criteria = new Criteria() //criteria object 
     140                //.param("and SomeDomainObject.name like :p1").param("p1", "s%") 
     141                .add("and SomeDomainObject.anotherDomainObjects.name like :p2").param("p2", "a%"); 
     142 
     143 
     144        //list with includes and criteria 
     145        List objList = ven.list(SomeDomainObject.class, joins, criteria); 
     146 
     147        //print the results 
     148        Iterator it = objList.iterator(); 
     149        while (it.hasNext()) { 
     150            SomeDomainObject someDomainObject = (SomeDomainObject)it.next(); 
     151            System.out.println(someDomainObject); 
     152        } 
     153    } 
     154 
     155    /** 
     156     * Test list the collection of objects by some criteria (object) 
     157     */ 
     158    public static void testListByCriteriaObject() { 
     159        Ven ven = getVen(); 
     160 
     161        //these objects will be included 
     162        Set joins = new HashSet(); 
     163        joins.add("SomeDomainObject.anotherDomainObjects"); 
     164        joins.add("SomeDomainObject.anotherDomainObject"); 
     165 
     166        //the results will be filtered according to this criteria 
     167        Criteria criteria = new Criteria() //criteria object 
     168                .like("SomeDomainObject.anotherDomainObjects.name", "a%") //attribute like value 
     169                .eq("SomeDomainObject.name", "sdo1") //attribute equals value 
     170                .and(); //connects previous criteria with and 
     171 
     172        //list with includes and criteria 
     173        List objList = ven.list(SomeDomainObject.class, joins, criteria); 
     174 
     175        //print the results 
     176        Iterator it = objList.iterator(); 
     177        while (it.hasNext()) { 
     178            SomeDomainObject someDomainObject = (SomeDomainObject)it.next(); 
     179            System.out.println(someDomainObject); 
     180        } 
    129181    } 
    130182 
Note: See TracChangeset for help on using the changeset viewer.