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.Ven; |
---|
21 | import com.fmguler.ven.sample.domain.SomeDomainObject; |
---|
22 | import java.sql.SQLException; |
---|
23 | import java.util.Date; |
---|
24 | import java.util.List; |
---|
25 | import java.util.Locale; |
---|
26 | import javax.sql.DataSource; |
---|
27 | import liquibase.FileSystemFileOpener; |
---|
28 | import liquibase.exception.JDBCException; |
---|
29 | import liquibase.exception.LiquibaseException; |
---|
30 | import org.springframework.jdbc.datasource.DriverManagerDataSource; |
---|
31 | import liquibase.Liquibase; |
---|
32 | import liquibase.database.Database; |
---|
33 | import liquibase.database.DatabaseFactory; |
---|
34 | |
---|
35 | /** |
---|
36 | * Demonstrates sample usage of fmgVen. |
---|
37 | * @author Fatih Mehmet Güler |
---|
38 | */ |
---|
39 | public class Sample { |
---|
40 | public static void main(String[] args) { |
---|
41 | //build the sample database |
---|
42 | buildDatabase(); |
---|
43 | |
---|
44 | //save an object |
---|
45 | testSave(); |
---|
46 | //delete an object |
---|
47 | testDelete(); |
---|
48 | |
---|
49 | //rollback the sample database to original state |
---|
50 | rollbackDatabase(); |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * Test save an object |
---|
55 | */ |
---|
56 | public static void testSave() { |
---|
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 | Ven ven = getVen(); |
---|
78 | ven.delete(1, SomeDomainObject.class); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Test get an object by primary key |
---|
83 | */ |
---|
84 | public static void testGet() { |
---|
85 | Ven ven = getVen(); |
---|
86 | SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class); |
---|
87 | System.out.println(obj); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Test list the collection of objects |
---|
92 | */ |
---|
93 | public static void testList() { |
---|
94 | Ven ven = getVen(); |
---|
95 | List objList = ven.list(SomeDomainObject.class); |
---|
96 | System.out.println(objList); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Test list the collection of objects by some criteria |
---|
101 | */ |
---|
102 | public static void testListByCriteria() { |
---|
103 | Ven ven = getVen(); |
---|
104 | List objList = ven.list(SomeDomainObject.class/*, criteria */); |
---|
105 | System.out.println(objList); |
---|
106 | } |
---|
107 | |
---|
108 | //--------------------------------------------------------- |
---|
109 | private static Ven getVen() { |
---|
110 | Ven ven = new Ven(); |
---|
111 | ven.setDataSource(getDataSource()); |
---|
112 | ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package"); |
---|
113 | return ven; |
---|
114 | } |
---|
115 | |
---|
116 | private static DataSource getDataSource() { |
---|
117 | DriverManagerDataSource ds = new DriverManagerDataSource(); |
---|
118 | ds.setDriverClassName("org.postgresql.Driver"); |
---|
119 | ds.setUsername("postgres"); |
---|
120 | ds.setPassword("qwerty"); |
---|
121 | ds.setUrl("jdbc:postgresql://127.0.0.1:5432/ven-test"); |
---|
122 | return ds; |
---|
123 | } |
---|
124 | |
---|
125 | private static void buildDatabase() { |
---|
126 | try { |
---|
127 | Locale currLocale = Locale.getDefault(); |
---|
128 | Locale.setDefault(Locale.ENGLISH); |
---|
129 | Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection()); |
---|
130 | Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database); |
---|
131 | liquibase.update(""); |
---|
132 | Locale.setDefault(currLocale); |
---|
133 | } catch (SQLException ex) { |
---|
134 | ex.printStackTrace(); |
---|
135 | } catch (JDBCException ex) { |
---|
136 | ex.printStackTrace(); |
---|
137 | } catch (LiquibaseException ex) { |
---|
138 | ex.printStackTrace(); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | private static void rollbackDatabase() { |
---|
143 | try { |
---|
144 | Locale currLocale = Locale.getDefault(); |
---|
145 | Locale.setDefault(Locale.ENGLISH); |
---|
146 | Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection()); |
---|
147 | Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database); |
---|
148 | liquibase.rollback("tag-single-table", ""); |
---|
149 | Locale.setDefault(currLocale); |
---|
150 | } catch (SQLException ex) { |
---|
151 | ex.printStackTrace(); |
---|
152 | } catch (JDBCException ex) { |
---|
153 | ex.printStackTrace(); |
---|
154 | } catch (LiquibaseException ex) { |
---|
155 | ex.printStackTrace(); |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|