[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 | */ |
---|
| 18 | package com.fmguler.ven.sample; |
---|
| 19 | |
---|
[30] | 20 | import com.fmguler.ven.Criteria; |
---|
[28] | 21 | import com.fmguler.ven.LiquibaseUtil; |
---|
[23] | 22 | import com.fmguler.ven.Ven; |
---|
[28] | 23 | import com.fmguler.ven.sample.domain.AnotherDomainObject; |
---|
[23] | 24 | import com.fmguler.ven.sample.domain.SomeDomainObject; |
---|
[25] | 25 | import java.util.Date; |
---|
[28] | 26 | import java.util.HashSet; |
---|
[29] | 27 | import java.util.Iterator; |
---|
[25] | 28 | import java.util.List; |
---|
[28] | 29 | import java.util.Set; |
---|
[23] | 30 | |
---|
| 31 | /** |
---|
| 32 | * Demonstrates sample usage of fmgVen. |
---|
| 33 | * @author Fatih Mehmet Güler |
---|
| 34 | */ |
---|
| 35 | public 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 | } |
---|