View Javadoc

1   /*
2    * org.riverock.dbrevision - Database revision engine
3    * For more information about DbRevision, please visit project site
4    * http://www.riverock.org
5    *
6    * Copyright (C) 2006-2006, Riverock Software, All Rights Reserved.
7    *
8    * Riverock - The Open-source Java Development Community
9    * http://www.riverock.org
10   *
11   *
12   * This library is free software; you can redistribute it and/or
13   * modify it under the terms of the GNU Lesser General Public
14   * License as published by the Free Software Foundation; either
15   * version 2.1 of the License, or (at your option) any later version.
16   *
17   * This library is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25   */
26  package org.riverock.dbrevision.system;
27  
28  import java.io.InputStream;
29  
30  import org.apache.log4j.Logger;
31  
32  import org.riverock.dbrevision.annotation.schema.db.DbSchema;
33  import org.riverock.dbrevision.annotation.schema.db.DbSequence;
34  import org.riverock.dbrevision.annotation.schema.db.DbTable;
35  import org.riverock.dbrevision.annotation.schema.db.DbView;
36  import org.riverock.dbrevision.db.Database;
37  import org.riverock.dbrevision.db.DatabaseManager;
38  import org.riverock.dbrevision.db.DatabaseStructureManager;
39  import org.riverock.dbrevision.utils.Utils;
40  import org.riverock.dbrevision.exception.DbRevisionException;
41  
42  /**
43   * import data from input stream to DB
44   * 
45   * Author: mill
46   * Date: Nov 28, 2002
47   * Time: 3:10:19 PM
48   * <p/>
49   * $Id: DbStructureImport.java 1141 2006-12-14 14:43:29Z serg_main $
50   * <p/>
51   */
52  @SuppressWarnings({"UnusedAssignment"})
53  public class DbStructureImport {
54      private static Logger log = Logger.getLogger(DbStructureImport.class);
55  
56      public static void importStructure(Database database, InputStream stream, boolean isData ) {
57          log.debug("Unmarshal data from inputstream");
58          DbSchema millSchema;
59          try {
60              millSchema = Utils.getObjectFromXml(DbSchema.class, stream);
61          }
62          catch (Exception e) {
63              String es = "Error unmarshal DB structure from input stream";
64              log.error(es, e);
65              throw new DbRevisionException(es, e);
66          }
67          importStructure(database, millSchema, isData);
68      }
69  
70      public static void importStructure(Database database, DbSchema millSchema, boolean isData) {
71          for (DbTable table : millSchema.getTables()) {
72  
73              if (!DatabaseManager.isSkipTable(table.getName())) {
74                  try {
75                      log.debug("create table " + table.getName());
76                      database.createTable(table);
77                  }
78                  catch (Exception e) {
79                      String es = "Error create table ";
80                      log.debug(es + table.getName(), e);
81                      throw new DbRevisionException(es, e);
82                  }
83                  if (isData) {
84                      DatabaseStructureManager.setDataTable(database, table);
85                  }
86              }
87              else {
88                  log.debug("skip table " + table.getName());
89              }
90  
91          }
92  
93          for (DbView view : millSchema.getViews()) {
94              DatabaseManager.createWithReplaceAllView(database, millSchema);
95              try {
96                  log.debug("create view " + view.getName());
97                  database.createView(view);
98              }
99              catch (Exception e) {
100                 if (database.testExceptionViewExists(e)) {
101                     log.debug("view " + view.getName() + " already exists");
102                     log.debug("drop view " + view.getName());
103                     DatabaseStructureManager.dropView(database, view);
104                     log.debug("create view " + view.getName());
105                     try {
106                         database.createView(view);
107                     }
108                     catch (Exception e1) {
109                         String es = "Error create view - ";
110                         log.error(es, e1);
111                         throw new DbRevisionException(es, e1);
112                     }
113                 }
114                 else {
115                     String es = "Error create view";
116                     log.debug(es,e);
117                     throw new DbRevisionException(es, e);
118                 }
119             }
120         }
121         DatabaseManager.createWithReplaceAllView(database, millSchema);
122 
123         for (DbSequence seq : millSchema.getSequences()) {
124             try {
125                 log.debug("create sequence " + seq.getName());
126                 database.createSequence(seq);
127             }
128             catch (Exception e) {
129                 String es = "Error create sequence ";
130                 log.debug(es + seq.getName(), e);
131                 throw new DbRevisionException(es, e);
132             }
133         }
134     }
135 }