KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > util > ejb > DataLoaderUtil


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.util.ejb;
21
22 import net.sf.hibernate.HibernateException;
23 import net.sf.hibernate.util.HibernateUtil;
24
25 import za.org.coefficient.authentication.CoefficientUser;
26 import za.org.coefficient.authentication.Role;
27 import za.org.coefficient.core.AttributeElement;
28 import za.org.coefficient.core.Constants;
29 import za.org.coefficient.core.Project;
30 import za.org.coefficient.core.ProjectAttribute;
31 import za.org.coefficient.core.ProjectAttributeData;
32 import za.org.coefficient.util.common.InvokerFactory;
33 import za.org.coefficient.util.ejb.SecurityUtil;
34
35 import javax.naming.InitialContext JavaDoc;
36 import javax.transaction.UserTransaction JavaDoc;
37 import javax.transaction.Status JavaDoc;
38
39 import java.util.List JavaDoc;
40 import java.util.Locale JavaDoc;
41 import java.util.TimeZone JavaDoc;
42
43 /**
44  * This is a utility class that loads default core data if it does
45  * not exist.
46  */

47 public class DataLoaderUtil {
48
49     //~ Static fields/initializers =============================================
50

51     // prepopulation user data
52
private static final String JavaDoc WORKFLOW_USERNAME = "workflow_user";
53
54     // prepopulation attribute data
55
private static String JavaDoc prepopLanguagesName = "Human Language";
56     private static String JavaDoc[][] prepopLanguagesElements =
57     {
58         { "Language", "text,size=20" }
59     };
60     private static String JavaDoc[] prepopLanguages =
61     {
62         "Afrikaans", "English", "French", "German", "Italian", "Shona",
63         "Spanish", "Xhosa", "Zulu"
64     };
65     private static String JavaDoc prepopProgrammingLanguagesName =
66         "Programming Language";
67     private static String JavaDoc[][] prepopProgrammingLanguagesElements =
68     {
69         { "Language", "text,size=10" }
70     };
71     private static String JavaDoc[] prepopProgrammingLanguages =
72     { "C", "C++", "Java", "Pascal", "Ada", "Delphi", "Fortran", "Cobol" };
73     private static String JavaDoc prepopAudiencesName = "Audience Type";
74     private static String JavaDoc[][] prepopAudiencesElements =
75     {
76         { "Intended Audience", "text,size=15" }
77     };
78     private static String JavaDoc[] prepopAudiences =
79     {
80         "Student", "Teacher", "Professor", "Hobbyist", "Toddler", "Teenager",
81         "Male", "Female", "Programmer", "Parent", "Scientist"
82     };
83     private static String JavaDoc prepopLicensesName = "License";
84     private static String JavaDoc[][] prepopLicensesElements =
85     {
86         { "License", "text,size=20" },
87         { "Name", "text,size=40" },
88         { "URL", "text,size=60" }
89     };
90     private static String JavaDoc[] prepopLicenses =
91     {
92         "GPL", "GNU General Public License",
93         "http://www.gnu.org/copyleft/gpl.html",
94         
95         "LGPL", "GNU Lesser General Public License",
96         "http://www.gnu.org/copyleft/lesser.html",
97         
98         "Apache", "Apache Sotware License", "http://www.apache.org/LICENSE.txt",
99         
100         "FreeBSD", "FreeBSD Copyright",
101         "http://www.freebsd.org/copyright/freebsd-license.html",
102         
103         "OPL", "OpenContent License", "http://www.opencontent.org/opl.shtml",
104         
105         "GFDL", "GNU Free Documentation License",
106         "http://www.gnu.org/copyleft/fdl.html"
107     };
108     private static String JavaDoc prepopStatusName = "Status";
109     private static String JavaDoc[][] prepopStatusElements =
110     {
111         { "status", "text,size=30" }
112     };
113     private static String JavaDoc[] prepopStatus =
114     {
115         "New project", "Under development", "Alpha testing", "Beta testing",
116         "Available"
117     };
118
119
120     public void initializeDataIfNeeded() {
121         try {
122             UserTransaction JavaDoc txn = null;
123             
124             // there must be a Transaction available in order to associate a
125
// HibernateSession with one
126
if (!HibernateUtil.isInTransaction() &&
127                 HibernateUtil.usingDataSource()) {
128
129                 // Make sure that if we are in an environment that is using
130
// transactions that we start a transaction for the data
131
// loading process.
132
InitialContext JavaDoc context = new InitialContext JavaDoc();
133                 txn = (UserTransaction JavaDoc)context
134                     .lookup(Constants.JTA_USER_TRANSACTION_JNDI_NAME);
135                 txn.begin();
136                 context.close();
137             }
138
139             // Do all the data loading
140
createDefaultRolesIfNeeded();
141             createDefaultUsersIfNeeded();
142             createAttributeDataIfNeeded();
143
144             // Make sure that if we are in an environment that is using
145
// transactions that we close the transaction correctly.
146
if(HibernateUtil.usingDataSource() && txn != null) {
147                 if (txn.getStatus() == Status.STATUS_ACTIVE) {
148                     txn.commit();
149                 }
150                 else if (txn.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
151                     txn.rollback();
152                 }
153             }
154         } catch (Exception JavaDoc e) {
155             e.printStackTrace();
156             System.err.println("<< problem loading the initial data");
157         }
158     }
159
160     protected void createDefaultRolesIfNeeded() {
161         try {
162             List JavaDoc roles = (List JavaDoc)InvokerFactory.getInvoker()
163                 .invokeMethodOnService("Role", "getAllRoles", new Object JavaDoc[0]);
164             
165             if (roles == null || roles.size() == 0) {
166                 // create the user roles
167
// highest user role
168
Role site_admin =
169                     new Role(SecurityUtil.SITE_ADMIN_ROLE_VAL,
170                              SecurityUtil.SITE_ADMIN_ROLE_DESC);
171                 Role site_moderator =
172                     new Role(SecurityUtil.SITE_MODERATOR_ROLE_VAL,
173                              SecurityUtil.SITE_MODERATOR_ROLE_DESC);
174                 Role champion =
175                     new Role(SecurityUtil.PROJECT_CHAMPION_ROLE_VAL,
176                              SecurityUtil.PROJECT_CHAMPION_ROLE_DESC);
177                 Role project_member =
178                     new Role(SecurityUtil.PROJECT_MEMBER_ROLE_VAL,
179                              SecurityUtil.PROJECT_MEMBER_ROLE_DESC);
180                 
181                 // lowest non-guest role
182
Role site_member =
183                     new Role(SecurityUtil.SITE_MEMBER_ROLE_VAL,
184                              SecurityUtil.SITE_MEMBER_ROLE_DESC);
185                 Role guest =
186                     new Role(SecurityUtil.GUEST_ROLE_VAL,
187                              SecurityUtil.GUEST_ROLE_DESC);
188                 try {
189                     HibernateUtil.save(site_admin);
190                     HibernateUtil.save(site_moderator);
191                     HibernateUtil.save(champion);
192                     HibernateUtil.save(project_member);
193                     HibernateUtil.save(site_member);
194                     HibernateUtil.save(guest);
195                 } catch (HibernateException he) {
196                     // This sucks, I guess we have no roles
197
he.printStackTrace();
198                 }
199             }
200         } catch (Exception JavaDoc e) {
201             e.printStackTrace();
202         }
203     }
204
205     protected void createAttributeDataIfNeeded() {
206         try {
207             List JavaDoc attributes = (List JavaDoc)InvokerFactory.getInvoker()
208                 .invokeMethodOnModule("AttributeDataManagement",
209                                       "getExistingAttributes", new Object JavaDoc[0]);
210             
211             if (attributes.size() == 0) {
212                 ProjectAttribute attribute = new ProjectAttribute();
213                 AttributeElement element = new AttributeElement();
214                 ProjectAttributeData pad;
215                 int i;
216                 
217                 prepopulateAttributes(prepopLanguagesName,
218                                       prepopLanguagesElements,
219                                       prepopLanguages);
220                 prepopulateAttributes(prepopProgrammingLanguagesName,
221                                       prepopProgrammingLanguagesElements,
222                                       prepopProgrammingLanguages);
223                 prepopulateAttributes(prepopAudiencesName,
224                                       prepopAudiencesElements,
225                                       prepopAudiences);
226                 prepopulateAttributes(prepopLicensesName,
227                                       prepopLicensesElements, prepopLicenses);
228                 prepopulateAttributes(prepopStatusName,
229                                       prepopStatusElements, prepopStatus);
230             }
231         } catch (Exception JavaDoc e) {
232             e.printStackTrace();
233         }
234     }
235         
236     protected void createDefaultUsersIfNeeded() {
237         try {
238             List JavaDoc users = (List JavaDoc)InvokerFactory.getInvoker()
239                 .invokeMethodOnModule("UserAdmin", "getAllUsers", new Object JavaDoc[0]);
240             
241             if (users == null || users.size() == 0) {
242                 // Create a site admin
243
CoefficientUser user = new CoefficientUser();
244                 user.setUserName("admin");
245                 user.setFullName("Site Administrator");
246                 user.setPassword(new String JavaDoc(SecurityUtil.md5AsHexString("admin")));
247                 user.setEmail("admin@coefficient.org.za");
248                 user.setLanguage(Locale.US);
249                 user.setTimeZone(TimeZone.getDefault());
250                 user.setActive(true);
251                 Role site_admin =
252                     SecurityUtil.getRoleForDescription(SecurityUtil.SITE_ADMIN_ROLE_DESC);
253                 user.setSystemRole(site_admin);
254                 
255                 // create a workflow admin
256
CoefficientUser workflowUser = new CoefficientUser();
257                 workflowUser.setUserName(WORKFLOW_USERNAME);
258                 workflowUser.setFullName("Workflow User");
259                 workflowUser.setPassword(new String JavaDoc(SecurityUtil.md5AsHexString("fasdferefewfewfef3444656447567523dfgghy45353534!!##$@@!")));
260                 workflowUser.setEmail("workflowUser@coefficient.org.za");
261                 workflowUser.setLanguage(Locale.US);
262                 workflowUser.setTimeZone(TimeZone.getDefault());
263                 workflowUser.setActive(false);
264                 workflowUser.setSystemRole(site_admin);
265                 try {
266                     HibernateUtil.save(user);
267                     HibernateUtil.save(workflowUser);
268                 } catch (HibernateException he) {
269                     // This sucks, I guess we have no users
270
he.printStackTrace();
271                 }
272             }
273         } catch (Exception JavaDoc e) {
274             e.printStackTrace();
275         }
276     }
277     
278     protected void prepopulateAttributes(String JavaDoc prepopName,
279                                          String JavaDoc[][] prepopElements,
280                                          String JavaDoc[] prepopData)
281     {
282         ProjectAttribute attribute;
283         AttributeElement element;
284         ProjectAttributeData pad;
285         
286         int i;
287         int j;
288         
289         try {
290             attribute = new ProjectAttribute();
291             attribute.setName(prepopName);
292             for (i = 0; i < prepopElements.length; i++) {
293                 element = new AttributeElement();
294                 element.setName(prepopElements[i][0]);
295                 element.setDescription(prepopElements[i][1]);
296                 attribute.getElements().add(element);
297             }
298
299             HibernateUtil.saveOrUpdate(attribute);
300
301             j = 0;
302             for (i = 0; i < prepopData.length; i++) {
303                 pad = new ProjectAttributeData();
304                 pad.setElementId(((AttributeElement) attribute.getElements()
305                                   .get(j)).getId());
306                 pad.setAttributeId(attribute.getId());
307                 pad.setDescription(prepopData[i]);
308                 HibernateUtil.saveOrUpdate(pad);
309                 j = (j + 1) % prepopElements.length;
310             }
311         } catch (Throwable JavaDoc t) {
312             System.out.println("***************" + t);
313         }
314     }
315 }
316
Popular Tags