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