source: trunk/fmgVen/test/com/fmguler/ven/sample/Sample.java @ 37

Last change on this file since 37 was 37, checked in by fmguler, 12 years ago

Refs #7 - Implemented orderAsc and orderDesc methods of Criteria. Have been testing these for a while, no problem so far. Added BigDecimal to db classes (Numeric db type). If the column name is "order" it is escaped while insert/update. (This should be done for all db keywords). Fixed missing mapping of one to many assc. (lists) of many to one (object) assc (obj.obj.list).

File size: 6.4 KB
RevLine 
[23]1/*
2 *  fmgVen - A Convention over Configuration Java ORM Tool
3 *  Copyright 2010 Fatih Mehmet Güler
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *  under the License.
17 */
18package com.fmguler.ven.sample;
19
[30]20import com.fmguler.ven.Criteria;
[28]21import com.fmguler.ven.LiquibaseUtil;
[23]22import com.fmguler.ven.Ven;
[28]23import com.fmguler.ven.sample.domain.AnotherDomainObject;
[23]24import com.fmguler.ven.sample.domain.SomeDomainObject;
[25]25import java.util.Date;
[28]26import java.util.HashSet;
[29]27import java.util.Iterator;
[25]28import java.util.List;
[28]29import java.util.Set;
[23]30
31/**
32 * Demonstrates sample usage of fmgVen.
33 * @author Fatih Mehmet Güler
34 */
35public class Sample {
36    public static void main(String[] args) {
[27]37        //build the sample database
[28]38        LiquibaseUtil.buildDatabase();
[27]39
40        //save an object
[26]41        testSave();
[28]42        //get an object
43        testGet();
[29]44        //list the objects
45        testList();
[30]46        //list the objects by some criteria string
47        testListByCriteriaString();
48        //list the objects by some typed criteria
49        testListByCriteriaObject();
[27]50        //delete an object
51        testDelete();
52
53        //rollback the sample database to original state
[28]54        LiquibaseUtil.rollbackDatabase("tag-init");
[23]55    }
56
[25]57    /**
58     * Test save an object
59     */
60    public static void testSave() {
[29]61        System.out.println("******SAVE******");
[23]62        Ven ven = getVen();
[25]63
64        //insert
65        SomeDomainObject obj = new SomeDomainObject();
66        obj.setName("name");
67        obj.setDescription("desc");
68        obj.setDate(new Date());
69        ven.save(obj);
70        System.out.println(obj);
71
72        //update
73        obj.setName("name update");
74        ven.save(obj);
75        System.out.println(obj);
76    }
77
78    /**
79     * Test delete an object
80     */
81    public static void testDelete() {
[29]82        System.out.println("******DELETE******");
[25]83        Ven ven = getVen();
[28]84        ven.delete(2, SomeDomainObject.class);
[25]85    }
86
87    /**
88     * Test get an object by primary key
89     */
90    public static void testGet() {
[29]91        System.out.println("******GET******");
[25]92        Ven ven = getVen();
[28]93
94        //get with includes
95        Set joins = new HashSet();
96        joins.add("SomeDomainObject.anotherDomainObjects");
97        joins.add("SomeDomainObject.anotherDomainObject");
98        SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class, joins);
[23]99        System.out.println(obj);
[28]100
101        Set joins2 = new HashSet();
102        joins2.add("AnotherDomainObject.someDomainObject");
103        AnotherDomainObject obj2 = (AnotherDomainObject)ven.get(1, AnotherDomainObject.class, joins2);
104        System.out.println(obj2);
[23]105    }
106
[25]107    /**
108     * Test list the collection of objects
109     */
110    public static void testList() {
[29]111        System.out.println("******LIST******");
[25]112        Ven ven = getVen();
[29]113
114        //list with includes
115        Set joins = new HashSet();
116        joins.add("SomeDomainObject.anotherDomainObjects");
117        joins.add("SomeDomainObject.anotherDomainObject");
118        List objList = ven.list(SomeDomainObject.class, joins);
[30]119
[29]120        Iterator it = objList.iterator();
121        while (it.hasNext()) {
122            SomeDomainObject someDomainObject = (SomeDomainObject)it.next();
123            System.out.println(someDomainObject);
124        }
[25]125    }
126
127    /**
[30]128     * Test list the collection of objects by some criteria (string)
[25]129     */
[30]130    public static void testListByCriteriaString() {
[25]131        Ven ven = getVen();
[30]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        }
[25]153    }
154
[30]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
[37]170                .and() //connects previous criteria with and
171                .orderDesc("SomeDomainObject.anotherDomainObjects.name"); //order by some attribute
[30]172
173        //list with includes and criteria
174        List objList = ven.list(SomeDomainObject.class, joins, criteria);
175
176        //print the results
177        Iterator it = objList.iterator();
178        while (it.hasNext()) {
179            SomeDomainObject someDomainObject = (SomeDomainObject)it.next();
180            System.out.println(someDomainObject);
181        }
182    }
183
[23]184    //---------------------------------------------------------
185    private static Ven getVen() {
186        Ven ven = new Ven();
[28]187        ven.setDataSource(LiquibaseUtil.getDataSource());
[23]188        ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package");
189        return ven;
190    }
191}
Note: See TracBrowser for help on using the repository browser.