[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 | |
---|
| 20 | import com.fmguler.ven.Ven; |
---|
| 21 | import com.fmguler.ven.sample.domain.SomeDomainObject; |
---|
[24] | 22 | import java.sql.SQLException; |
---|
| 23 | import java.util.Locale; |
---|
[23] | 24 | import javax.sql.DataSource; |
---|
[24] | 25 | import liquibase.FileSystemFileOpener; |
---|
| 26 | import liquibase.exception.JDBCException; |
---|
| 27 | import liquibase.exception.LiquibaseException; |
---|
[23] | 28 | import org.springframework.jdbc.datasource.DriverManagerDataSource; |
---|
[24] | 29 | import liquibase.Liquibase; |
---|
| 30 | import liquibase.database.Database; |
---|
| 31 | import liquibase.database.DatabaseFactory; |
---|
[23] | 32 | |
---|
| 33 | /** |
---|
| 34 | * Demonstrates sample usage of fmgVen. |
---|
| 35 | * @author Fatih Mehmet Güler |
---|
| 36 | */ |
---|
| 37 | public class Sample { |
---|
| 38 | public static void main(String[] args) { |
---|
[24] | 39 | buildDatabase(); |
---|
[23] | 40 | test1(); |
---|
[24] | 41 | //rollbackDatabase(); |
---|
[23] | 42 | } |
---|
| 43 | |
---|
| 44 | public static void test1() { |
---|
| 45 | Ven ven = getVen(); |
---|
| 46 | SomeDomainObject obj = (SomeDomainObject)ven.get(1, SomeDomainObject.class); |
---|
| 47 | System.out.println(obj); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | //--------------------------------------------------------- |
---|
| 51 | private static Ven getVen() { |
---|
| 52 | Ven ven = new Ven(); |
---|
| 53 | ven.setDataSource(getDataSource()); |
---|
| 54 | ven.addDomainPackage("com.fmguler.ven.sample.domain").addDomainPackage("another.package"); |
---|
| 55 | return ven; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | private static DataSource getDataSource() { |
---|
| 59 | DriverManagerDataSource ds = new DriverManagerDataSource(); |
---|
| 60 | ds.setDriverClassName("org.postgresql.Driver"); |
---|
| 61 | ds.setUsername("postgres"); |
---|
| 62 | ds.setPassword("qwerty"); |
---|
[24] | 63 | ds.setUrl("jdbc:postgresql://127.0.0.1:5432/ven-test"); |
---|
[23] | 64 | return ds; |
---|
| 65 | } |
---|
[24] | 66 | |
---|
| 67 | private static void buildDatabase() { |
---|
| 68 | try { |
---|
| 69 | Locale currLocale = Locale.getDefault(); |
---|
| 70 | Locale.setDefault(Locale.ENGLISH); |
---|
| 71 | Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection()); |
---|
| 72 | Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database); |
---|
| 73 | liquibase.update(""); |
---|
| 74 | Locale.setDefault(currLocale); |
---|
| 75 | } catch (SQLException ex) { |
---|
| 76 | ex.printStackTrace(); |
---|
| 77 | } catch (JDBCException ex) { |
---|
| 78 | ex.printStackTrace(); |
---|
| 79 | } catch (LiquibaseException ex) { |
---|
| 80 | ex.printStackTrace(); |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | private static void rollbackDatabase() { |
---|
| 85 | try { |
---|
| 86 | Locale currLocale = Locale.getDefault(); |
---|
| 87 | Locale.setDefault(Locale.ENGLISH); |
---|
| 88 | Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(getDataSource().getConnection()); |
---|
| 89 | Liquibase liquibase = new Liquibase("etc/test-db/test-db-changelog.xml", new FileSystemFileOpener(), database); |
---|
| 90 | //liquibase.rollback(51, ""); |
---|
| 91 | liquibase.rollback("0", ""); |
---|
| 92 | Locale.setDefault(currLocale); |
---|
| 93 | } catch (SQLException ex) { |
---|
| 94 | ex.printStackTrace(); |
---|
| 95 | } catch (JDBCException ex) { |
---|
| 96 | ex.printStackTrace(); |
---|
| 97 | } catch (LiquibaseException ex) { |
---|
| 98 | ex.printStackTrace(); |
---|
| 99 | } |
---|
| 100 | } |
---|
[23] | 101 | } |
---|