KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > standalone > HsqlShemaCreator


1 /*
2  * $Id: HsqlShemaCreator.java,v 1.5 2005/01/13 19:27:33 michelson Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com Koeln / Duesseldorf ,
5  * Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License as published by the Free Software
12  * Foundation; either version 2 of the License, or (at your option) any later
13  * version.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22  * Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.standalone;
27
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.Statement JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import net.sf.hibernate.Session;
34 import net.sf.hibernate.SessionFactory;
35 import net.sf.hibernate.Transaction;
36 import net.sf.hibernate.cfg.Configuration;
37 import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
38
39 import com.j2biz.blogunity.IConstants;
40 import com.j2biz.blogunity.pojo.SystemConfiguration;
41 import com.j2biz.blogunity.pojo.User;
42 import com.j2biz.blogunity.util.BlogUtils;
43
44 public class HsqlShemaCreator {
45
46     public static void main(String JavaDoc[] args) {
47         try {
48             export();
49         } catch (Exception JavaDoc e) {
50             e.printStackTrace();
51         }
52     }
53
54     public static void export() throws Exception JavaDoc {
55         Properties JavaDoc hibernateProps = new Properties JavaDoc();
56         hibernateProps.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
57         hibernateProps.setProperty("hibernate.connection.url",
58                 "jdbc:hsqldb:file:./standalone-merge/db/blogunitydb");
59         hibernateProps.setProperty("hibernate.connection.username", "sa");
60         hibernateProps.setProperty("hibernate.connection.password", "");
61         hibernateProps.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.HSQLDialect");
62         hibernateProps.setProperty("hibernate.show_sql", "false");
63
64         hibernateProps.setProperty("hibernate.cache.provider_class",
65                 "net.sf.hibernate.cache.EhCacheProvider");
66         hibernateProps.setProperty("hibernate.cache.use_query_cache", "true");
67
68         Configuration cfg = new Configuration();
69         cfg.setProperties(hibernateProps);
70         
71         for (int i = 0; i < IConstants.PERSISTENT_CLASSES.length; i++) {
72             cfg.addClass(IConstants.PERSISTENT_CLASSES[i]);
73         }
74         
75
76         System.out.println("Export shema...");
77
78         SchemaExport export = new SchemaExport(cfg, hibernateProps);
79         export.create(false, true);
80         System.out.println("Success.");
81
82         System.out.println("Building hibernate's session factory....");
83         SessionFactory sessionFactory = cfg.buildSessionFactory();
84         Session session = sessionFactory.openSession();
85
86         saveSystemConfiguration(session);
87         saveSystemAdministrator(session);
88
89         System.out.println("Execute flush...");
90         session.flush();
91
92         Connection JavaDoc conn = session.connection();
93         Statement JavaDoc stmt = conn.createStatement();
94         stmt.executeUpdate("SHUTDOWN");
95         stmt.close();
96         conn.close();
97
98         session.close();
99
100     }
101
102     /**
103      * @throws Exception
104      *
105      */

106     private static void saveSystemAdministrator(Session session) throws Exception JavaDoc {
107         // system administrator
108

109         System.out.print("Creating system administrator...");
110
111         String JavaDoc adminUser = "admin";
112         String JavaDoc adminPass = "admin";
113         String JavaDoc adminEmail = "admin@yoursite.com";
114
115         Transaction tx = session.beginTransaction();
116         User u = new User();
117
118         if (!BlogUtils.getInstance().isValidNickname(adminUser)) adminUser = "admin";
119
120         u.setNickname(adminUser);
121         u.setPassword(adminPass);
122
123         if (!BlogUtils.getInstance().isValidEmailAddress(adminEmail))
124                 adminEmail = "admin@site.com";
125         u.setEmail(adminEmail);
126         u.setFirstname("System");
127         u.setLastname("Administrator");
128         u.setAdministrator(true);
129         u.setActive(true);
130         u.setShowEmail(false);
131         session.save(u);
132
133         tx.commit();
134
135         System.out.println("Success.");
136     }
137
138     /**
139      *
140      */

141     private static void saveSystemConfiguration(Session session) throws Exception JavaDoc {
142         System.out.print("Creating system properties...");
143
144         String JavaDoc blogunityDataDir = "./webapp/WEB-INF/data";
145         String JavaDoc blogunityLogsDir = "./webapp/WEB-INF/logs";
146         String JavaDoc blogunityTempDir = "./webapp/WEB-INF/temp";
147
148         Transaction tx = session.beginTransaction();
149
150         Properties JavaDoc props = new Properties JavaDoc();
151         props.setProperty("resource.loader", "file, class");
152
153         // file resource loader
154
props.setProperty("file.resource.loader.path", blogunityDataDir);
155         props.setProperty("file.resource.loader.cache", "true");
156         props.setProperty("file.resource.loader.modificationCheckInterval", "2");
157         props.setProperty("velocimacro.library", "blogunity_macros.vm");
158
159         // classpath resource loader
160

161         props
162                 .setProperty("class.resource.loader.description",
163                         "Velocity Classpath Resource Loader");
164         props.setProperty("class.resource.loader.class",
165                 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
166
167         // velocity.log
168
props.setProperty("runtime.log", blogunityLogsDir + "/velocity.log");
169         props.setProperty("input.encoding", "UTF-8");
170         props.setProperty("output.encoding", "UTF-8");
171
172         SystemConfiguration config = new SystemConfiguration();
173         config.setAllowNewUsers(true);
174         config.setDataDir(blogunityDataDir);
175         config.setTempDir(blogunityTempDir);
176         // config.setRenderEngineClass("org.radeox.engine.BaseRenderEngine");
177
config.setValidateNewUsers(true);
178
179         ByteArrayOutputStream JavaDoc outx = new ByteArrayOutputStream JavaDoc();
180         props.store(outx, "Created by Blogunity Installer");
181
182         config.setVelocityProperties(new String JavaDoc(outx.toByteArray()));
183         session.save(config);
184         tx.commit();
185         System.out.println("Success.");
186     }
187
188 }
Popular Tags