KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > test > SchemaCreator


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.test;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.dbobj.DBObject;
69 import com.jcorporate.expresso.core.dbobj.Schema;
70 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
71 import com.jcorporate.expresso.core.utility.DBTool;
72 import com.jcorporate.expresso.kernel.InstallLog;
73 import com.jcorporate.expresso.services.dbobj.SchemaList;
74 import org.apache.log4j.Logger;
75
76 import java.util.Enumeration JavaDoc;
77 import java.util.Vector JavaDoc;
78
79
80 /**
81  * Given a series of schema class names, this class will perform a "dbcreate"
82  * The big difference between this and other versions is that this will work
83  * on a blank database.
84  *
85  * @author Michael Rimov
86  */

87 public class SchemaCreator {
88     static Logger log = Logger.getLogger(SchemaCreator.class);
89     static String JavaDoc dataContext = null;;
90
91     /**
92      * Default Constructor
93      */

94     public SchemaCreator() {
95     }
96
97     public String JavaDoc getDataContext() {
98         return dataContext;
99     }
100
101     public void setDataContext(String JavaDoc newValue) {
102         dataContext = newValue;
103     }
104
105     private static String JavaDoc getMyContext() {
106         if (dataContext == null) {
107             return TestSystemInitializer.getTestContext();
108         } else {
109             return dataContext;
110         }
111     }
112
113
114     /**
115      * Iterates through all the tables. If they all exist, then we assume
116      * the schema is properly set up.
117      *
118      * @param oneSchema an instantiated Schema object to check to see if it is installed
119      * or not.
120      * @return false if there is an error on getting DBobject.count() on the
121      * database table.
122      * @throws DBException upon database communication error
123      */

124     public static boolean schemaExists(Schema oneSchema)
125             throws DBException {
126         DBObject oneMember;
127
128         for (Enumeration JavaDoc e = oneSchema.getMembers(); e.hasMoreElements();) {
129             oneMember = (DBObject) e.nextElement();
130             oneMember.setDataContext(getMyContext());
131             if (log.isDebugEnabled()) {
132                 log.debug("Checking table " + oneMember.getJDBCMetaData().getTargetTable());
133             }
134
135             try {
136                 oneMember.count();
137                 if (log.isDebugEnabled()) {
138                     log.debug("Table " + oneMember.getJDBCMetaData().getTargetTable() + " exists");
139                 }
140             } catch (Throwable JavaDoc de) {
141
142                 //Table didn't exist or there's a problem with the connection
143
return false;
144             }
145         }
146
147         SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
148         sl.setDataContext(dataContext);
149         sl.setField(SchemaList.FLD_SCHEMA_CLASS, oneSchema.getClass().getName());
150         if (!sl.find()) {
151             return false;
152         }
153
154         return true;
155     }
156
157     /**
158      * @param oneSchema
159      * @param installLog
160      * @return
161      */

162     public static synchronized boolean ensureSchemaExists(Schema oneSchema, InstallLog installLog)
163             throws DBException {
164         Vector JavaDoc toCreate = new Vector JavaDoc();
165
166         if (!schemaExists(oneSchema)) {
167             toCreate.add(oneSchema);
168         } else {
169
170             //Nothing to create.
171
return true;
172         }
173
174         DBTool.createTables(installLog, toCreate, getMyContext());
175
176         for (Enumeration JavaDoc e = toCreate.elements(); e.hasMoreElements();) {
177             Schema s = (Schema) e.nextElement();
178
179             if (!s.getClass().getName().equals("com.jcorporate.expresso.core.ExpressoSchema")) {
180                 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
181                 sl.setDataContext(getMyContext());
182                 sl.setField("SchemaClass", s.getClass().getName());
183
184                 if (!sl.find()) {
185                     sl.setField("Descrip", s.getDefaultDescription());
186                     sl.setField("ComponentCode",
187                             s.getDefaultComponentCode());
188                     sl.add();
189                 } else {
190                     log.info("Schema Class " + s.getClass().getName() +
191                             " already is listed as a registered schema");
192                 }
193             }
194         }
195
196         DBTool.setupSecurity(installLog, toCreate, getMyContext());
197         DBTool.populateTables(installLog, toCreate, getMyContext());
198         DBTool.setupConfig(installLog, toCreate, getMyContext());
199         DBTool.otherSetups(installLog, toCreate, getMyContext());
200
201         if (!oneSchema.getClass().getName().equals("com.jcorporate.expresso.core.ExpressoSchema")) {
202             SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT);
203             sl.setDataContext(getMyContext());
204             sl.setField("SchemaClass", oneSchema.getClass().getName());
205
206             if (!sl.find()) {
207                 sl.setField("Descrip", oneSchema.getDefaultDescription());
208                 sl.setField("ComponentCode",
209                         oneSchema.getDefaultComponentCode());
210                 sl.add();
211             }
212         }
213
214         return false;
215     }
216
217 }
Popular Tags