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

Last change on this file 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
Line 
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
20import com.fmguler.ven.Criteria;
21import com.fmguler.ven.LiquibaseUtil;
22import com.fmguler.ven.Ven;
23import com.fmguler.ven.sample.domain.AnotherDomainObject;
24import com.fmguler.ven.sample.domain.SomeDomainObject;
25import java.util.Date;
26import java.util.HashSet;
27import java.util.Iterator;
28import java.util.List;
29import java.util.Set;
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) {
37        //build the sample database
38        LiquibaseUtil.buildDatabase();
39
40        //save an object
41        testSave();
42        //get an object
43        testGet();
44        //list the objects
45        testList();
46        //list the objects by some criteria string
47        testListByCriteriaString();
48        //list the objects by some typed criteria
49        testListByCriteriaObject();
50        //delete an object
51        testDelete();
52
53        //rollback the sample database to original state
54        LiquibaseUtil.rollbackDatabase("tag-init");
55    }
56
57    /**
58     * Test save an object
59     */
60    public static void testSave() {
61        System.out.println("******SAVE******");
62        Ven ven = getVen();
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() {
82        System.out.println("******DELETE******");
83        Ven ven = getVen();
84        ven.delete(2, SomeDomainObject.class);
85    }
86
87    /**
88     * Test get an object by primary key
89     */
90    public static void testGet() {
91        System.out.println("******GET******");
92        Ven ven = getVen();
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);
99        System.out.println(obj);
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);
105    }
106
107    /**
108     * Test list the collection of objects
109     */
110    public static void testList() {
111        System.out.println("******LIST******");
112        Ven ven = getVen();
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);
119
120        Iterator it = objList.iterator();
121        while (it.hasNext()) {
122            SomeDomainObject someDomainObject = (SomeDomainObject)it.next();
123            System.out.println(someDomainObject);
124        }
125    }
126
127    /**
128     * Test list the collection of objects by some criteria (string)
129     */
130    public static void testListByCriteriaString() {
131        Ven ven = getVen();
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                .orderDesc("SomeDomainObject.anotherDomainObjects.name"); //order by some attribute
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
184    //---------------------------------------------------------
185    private static Ven getVen() {
186        Ven ven = new Ven();
187        ven.setDataSource(LiquibaseUtil.getDataSource());
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.