| 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 | |
|---|
| 20 | import com.fmguler.ven.LiquibaseUtil; |
|---|
| 21 | import com.fmguler.ven.Ven; |
|---|
| 22 | import com.fmguler.ven.sample.domain.AnotherDomainObject; |
|---|
| 23 | import com.fmguler.ven.sample.domain.SomeDomainObject; |
|---|
| 24 | import java.util.Date; |
|---|
| 25 | import java.util.HashSet; |
|---|
| 26 | import java.util.Iterator; |
|---|
| 27 | import java.util.List; |
|---|
| 28 | import java.util.Set; |
|---|
| 29 | |
|---|
| 30 | /** |
|---|
| 31 | * Demonstrates sample usage of fmgVen. |
|---|
| 32 | * @author Fatih Mehmet Güler |
|---|
| 33 | */ |
|---|
| 34 | public class Sample { |
|---|
| 35 | public static void main(String[] args) { |
|---|
| 36 | //build the sample database |
|---|
| 37 | LiquibaseUtil.buildDatabase(); |
|---|
| 38 | |
|---|
| 39 | //save an object |
|---|
| 40 | testSave(); |
|---|
| 41 | //get an object |
|---|
| 42 | testGet(); |
|---|
| 43 | //list the objects |
|---|
| 44 | testList(); |
|---|
| 45 | //delete an object |
|---|
| 46 | testDelete(); |
|---|
| 47 | |
|---|
| 48 | //rollback the sample database to original state |
|---|
| 49 | LiquibaseUtil.rollbackDatabase("tag-init"); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Test save an object |
|---|
| 54 | */ |
|---|
| 55 | public static void testSave() { |
|---|
| 56 | System.out.println("******SAVE******"); |
|---|
| 57 | Ven ven = getVen(); |
|---|
| 58 | |
|---|
| 59 | //insert |
|---|
| 60 | SomeDomainObject obj = new SomeDomainObject(); |
|---|
| 61 | obj.setName("name"); |
|---|
| 62 | obj.setDescription("desc"); |
|---|
| 63 | obj.setDate(new Date()); |
|---|
| 64 | ven.save(obj); |
|---|
| 65 | System.out.println(obj); |
|---|
| 66 | |
|---|
| 67 | //update |
|---|
| 68 | obj.setName("name update"); |
|---|
| 69 | ven.save(obj); |
|---|
| 70 | System.out.println(obj); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Test delete an object |
|---|
| 75 | */ |
|---|
| 76 | public static void testDelete() { |
|---|
| 77 | System.out.println("******DELETE******"); |
|---|
| 78 | Ven ven = getVen(); |
|---|
| 79 | ven.delete(2, SomeDomainObject.class); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Test get an object by primary key |
|---|
| 84 | */ |
|---|
| 85 | public static void testGet() { |
|---|
| 86 | System.out.println("******GET******"); |
|---|
| 87 | Ven ven = getVen(); |
|---|
| 88 | |
|---|
| 89 | //get with includes |
|---|
| 90 | Set joins = new HashSet(); |
|---|
| 91 | joins.add("SomeDomainObject.anotherDomainObjects"); |
|---|
| 92 | joins.add("SomeDomainObject.anotherDomainObject"); |
|---|
| 93 | SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class, joins); |
|---|
| 94 | System.out.println(obj); |
|---|
| 95 | |
|---|
| 96 | Set joins2 = new HashSet(); |
|---|
| 97 | joins2.add("AnotherDomainObject.someDomainObject"); |
|---|
| 98 | AnotherDomainObject obj2 = (AnotherDomainObject)ven.get(1, AnotherDomainObject.class, joins2); |
|---|
| 99 | System.out.println(obj2); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Test list the collection of objects |
|---|
| 104 | */ |
|---|
| 105 | public static void testList() { |
|---|
| 106 | System.out.println("******LIST******"); |
|---|
| 107 | Ven ven = getVen(); |
|---|
| 108 | |
|---|
| 109 | //list with includes |
|---|
| 110 | Set joins = new HashSet(); |
|---|
| 111 | joins.add("SomeDomainObject.anotherDomainObjects"); |
|---|
| 112 | joins.add("SomeDomainObject.anotherDomainObject"); |
|---|
| 113 | List objList = ven.list(SomeDomainObject.class, joins); |
|---|
| 114 | |
|---|
| 115 | Iterator it = objList.iterator(); |
|---|
| 116 | while (it.hasNext()) { |
|---|
| 117 | SomeDomainObject someDomainObject = (SomeDomainObject)it.next(); |
|---|
| 118 | System.out.println(someDomainObject); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * Test list the collection of objects by some criteria |
|---|
| 124 | */ |
|---|
| 125 | public static void testListByCriteria() { |
|---|
| 126 | Ven ven = getVen(); |
|---|
| 127 | //List objList = ven.list(SomeDomainObject.class/*, criteria */); |
|---|
| 128 | //System.out.println(objList); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | //--------------------------------------------------------- |
|---|
| 132 | private static Ven getVen() { |
|---|
| 133 | Ven ven = new Ven(); |
|---|
| 134 | ven.setDataSource(LiquibaseUtil.getDataSource()); |
|---|
| 135 | ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package"); |
|---|
| 136 | return ven; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|