001: /*
002: * Coefficient - facilitates project based collaboration
003: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004: * PO Box 395
005: * Pretoria 0001, RSA
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package za.org.coefficient.util.ejb;
021:
022: import net.sf.hibernate.HibernateException;
023: import net.sf.hibernate.util.HibernateUtil;
024:
025: import za.org.coefficient.authentication.CoefficientUser;
026: import za.org.coefficient.authentication.Role;
027: import za.org.coefficient.core.AttributeElement;
028: import za.org.coefficient.core.Constants;
029: import za.org.coefficient.core.Project;
030: import za.org.coefficient.core.ProjectAttribute;
031: import za.org.coefficient.core.ProjectAttributeData;
032: import za.org.coefficient.util.common.InvokerFactory;
033: import za.org.coefficient.util.ejb.SecurityUtil;
034:
035: import javax.naming.InitialContext;
036: import javax.transaction.UserTransaction;
037: import javax.transaction.Status;
038:
039: import java.util.List;
040: import java.util.Locale;
041: import java.util.TimeZone;
042:
043: /**
044: * This is a utility class that loads default core data if it does
045: * not exist.
046: */
047: public class DataLoaderUtil {
048:
049: //~ Static fields/initializers =============================================
050:
051: // prepopulation user data
052: private static final String WORKFLOW_USERNAME = "workflow_user";
053:
054: // prepopulation attribute data
055: private static String prepopLanguagesName = "Human Language";
056: private static String[][] prepopLanguagesElements = { { "Language",
057: "text,size=20" } };
058: private static String[] prepopLanguages = { "Afrikaans", "English",
059: "French", "German", "Italian", "Shona", "Spanish", "Xhosa",
060: "Zulu" };
061: private static String prepopProgrammingLanguagesName = "Programming Language";
062: private static String[][] prepopProgrammingLanguagesElements = { {
063: "Language", "text,size=10" } };
064: private static String[] prepopProgrammingLanguages = { "C", "C++",
065: "Java", "Pascal", "Ada", "Delphi", "Fortran", "Cobol" };
066: private static String prepopAudiencesName = "Audience Type";
067: private static String[][] prepopAudiencesElements = { {
068: "Intended Audience", "text,size=15" } };
069: private static String[] prepopAudiences = { "Student", "Teacher",
070: "Professor", "Hobbyist", "Toddler", "Teenager", "Male",
071: "Female", "Programmer", "Parent", "Scientist" };
072: private static String prepopLicensesName = "License";
073: private static String[][] prepopLicensesElements = {
074: { "License", "text,size=20" }, { "Name", "text,size=40" },
075: { "URL", "text,size=60" } };
076: private static String[] prepopLicenses = { "GPL",
077: "GNU General Public License",
078: "http://www.gnu.org/copyleft/gpl.html",
079:
080: "LGPL", "GNU Lesser General Public License",
081: "http://www.gnu.org/copyleft/lesser.html",
082:
083: "Apache", "Apache Sotware License",
084: "http://www.apache.org/LICENSE.txt",
085:
086: "FreeBSD", "FreeBSD Copyright",
087: "http://www.freebsd.org/copyright/freebsd-license.html",
088:
089: "OPL", "OpenContent License",
090: "http://www.opencontent.org/opl.shtml",
091:
092: "GFDL", "GNU Free Documentation License",
093: "http://www.gnu.org/copyleft/fdl.html" };
094: private static String prepopStatusName = "Status";
095: private static String[][] prepopStatusElements = { { "status",
096: "text,size=30" } };
097: private static String[] prepopStatus = { "New project",
098: "Under development", "Alpha testing", "Beta testing",
099: "Available" };
100:
101: public void initializeDataIfNeeded() {
102: try {
103: UserTransaction txn = null;
104:
105: // there must be a Transaction available in order to associate a
106: // HibernateSession with one
107: if (!HibernateUtil.isInTransaction()
108: && HibernateUtil.usingDataSource()) {
109:
110: // Make sure that if we are in an environment that is using
111: // transactions that we start a transaction for the data
112: // loading process.
113: InitialContext context = new InitialContext();
114: txn = (UserTransaction) context
115: .lookup(Constants.JTA_USER_TRANSACTION_JNDI_NAME);
116: txn.begin();
117: context.close();
118: }
119:
120: // Do all the data loading
121: createDefaultRolesIfNeeded();
122: createDefaultUsersIfNeeded();
123: createAttributeDataIfNeeded();
124:
125: // Make sure that if we are in an environment that is using
126: // transactions that we close the transaction correctly.
127: if (HibernateUtil.usingDataSource() && txn != null) {
128: if (txn.getStatus() == Status.STATUS_ACTIVE) {
129: txn.commit();
130: } else if (txn.getStatus() == Status.STATUS_MARKED_ROLLBACK) {
131: txn.rollback();
132: }
133: }
134: } catch (Exception e) {
135: e.printStackTrace();
136: System.err.println("<< problem loading the initial data");
137: }
138: }
139:
140: protected void createDefaultRolesIfNeeded() {
141: try {
142: List roles = (List) InvokerFactory.getInvoker()
143: .invokeMethodOnService("Role", "getAllRoles",
144: new Object[0]);
145:
146: if (roles == null || roles.size() == 0) {
147: // create the user roles
148: // highest user role
149: Role site_admin = new Role(
150: SecurityUtil.SITE_ADMIN_ROLE_VAL,
151: SecurityUtil.SITE_ADMIN_ROLE_DESC);
152: Role site_moderator = new Role(
153: SecurityUtil.SITE_MODERATOR_ROLE_VAL,
154: SecurityUtil.SITE_MODERATOR_ROLE_DESC);
155: Role champion = new Role(
156: SecurityUtil.PROJECT_CHAMPION_ROLE_VAL,
157: SecurityUtil.PROJECT_CHAMPION_ROLE_DESC);
158: Role project_member = new Role(
159: SecurityUtil.PROJECT_MEMBER_ROLE_VAL,
160: SecurityUtil.PROJECT_MEMBER_ROLE_DESC);
161:
162: // lowest non-guest role
163: Role site_member = new Role(
164: SecurityUtil.SITE_MEMBER_ROLE_VAL,
165: SecurityUtil.SITE_MEMBER_ROLE_DESC);
166: Role guest = new Role(SecurityUtil.GUEST_ROLE_VAL,
167: SecurityUtil.GUEST_ROLE_DESC);
168: try {
169: HibernateUtil.save(site_admin);
170: HibernateUtil.save(site_moderator);
171: HibernateUtil.save(champion);
172: HibernateUtil.save(project_member);
173: HibernateUtil.save(site_member);
174: HibernateUtil.save(guest);
175: } catch (HibernateException he) {
176: // This sucks, I guess we have no roles
177: he.printStackTrace();
178: }
179: }
180: } catch (Exception e) {
181: e.printStackTrace();
182: }
183: }
184:
185: protected void createAttributeDataIfNeeded() {
186: try {
187: List attributes = (List) InvokerFactory.getInvoker()
188: .invokeMethodOnModule("AttributeDataManagement",
189: "getExistingAttributes", new Object[0]);
190:
191: if (attributes.size() == 0) {
192: ProjectAttribute attribute = new ProjectAttribute();
193: AttributeElement element = new AttributeElement();
194: ProjectAttributeData pad;
195: int i;
196:
197: prepopulateAttributes(prepopLanguagesName,
198: prepopLanguagesElements, prepopLanguages);
199: prepopulateAttributes(prepopProgrammingLanguagesName,
200: prepopProgrammingLanguagesElements,
201: prepopProgrammingLanguages);
202: prepopulateAttributes(prepopAudiencesName,
203: prepopAudiencesElements, prepopAudiences);
204: prepopulateAttributes(prepopLicensesName,
205: prepopLicensesElements, prepopLicenses);
206: prepopulateAttributes(prepopStatusName,
207: prepopStatusElements, prepopStatus);
208: }
209: } catch (Exception e) {
210: e.printStackTrace();
211: }
212: }
213:
214: protected void createDefaultUsersIfNeeded() {
215: try {
216: List users = (List) InvokerFactory.getInvoker()
217: .invokeMethodOnModule("UserAdmin", "getAllUsers",
218: new Object[0]);
219:
220: if (users == null || users.size() == 0) {
221: // Create a site admin
222: CoefficientUser user = new CoefficientUser();
223: user.setUserName("admin");
224: user.setFullName("Site Administrator");
225: user.setPassword(new String(SecurityUtil
226: .md5AsHexString("admin")));
227: user.setEmail("admin@coefficient.org.za");
228: user.setLanguage(Locale.US);
229: user.setTimeZone(TimeZone.getDefault());
230: user.setActive(true);
231: Role site_admin = SecurityUtil
232: .getRoleForDescription(SecurityUtil.SITE_ADMIN_ROLE_DESC);
233: user.setSystemRole(site_admin);
234:
235: // create a workflow admin
236: CoefficientUser workflowUser = new CoefficientUser();
237: workflowUser.setUserName(WORKFLOW_USERNAME);
238: workflowUser.setFullName("Workflow User");
239: workflowUser
240: .setPassword(new String(
241: SecurityUtil
242: .md5AsHexString("fasdferefewfewfef3444656447567523dfgghy45353534!!##$@@!")));
243: workflowUser
244: .setEmail("workflowUser@coefficient.org.za");
245: workflowUser.setLanguage(Locale.US);
246: workflowUser.setTimeZone(TimeZone.getDefault());
247: workflowUser.setActive(false);
248: workflowUser.setSystemRole(site_admin);
249: try {
250: HibernateUtil.save(user);
251: HibernateUtil.save(workflowUser);
252: } catch (HibernateException he) {
253: // This sucks, I guess we have no users
254: he.printStackTrace();
255: }
256: }
257: } catch (Exception e) {
258: e.printStackTrace();
259: }
260: }
261:
262: protected void prepopulateAttributes(String prepopName,
263: String[][] prepopElements, String[] prepopData) {
264: ProjectAttribute attribute;
265: AttributeElement element;
266: ProjectAttributeData pad;
267:
268: int i;
269: int j;
270:
271: try {
272: attribute = new ProjectAttribute();
273: attribute.setName(prepopName);
274: for (i = 0; i < prepopElements.length; i++) {
275: element = new AttributeElement();
276: element.setName(prepopElements[i][0]);
277: element.setDescription(prepopElements[i][1]);
278: attribute.getElements().add(element);
279: }
280:
281: HibernateUtil.saveOrUpdate(attribute);
282:
283: j = 0;
284: for (i = 0; i < prepopData.length; i++) {
285: pad = new ProjectAttributeData();
286: pad.setElementId(((AttributeElement) attribute
287: .getElements().get(j)).getId());
288: pad.setAttributeId(attribute.getId());
289: pad.setDescription(prepopData[i]);
290: HibernateUtil.saveOrUpdate(pad);
291: j = (j + 1) % prepopElements.length;
292: }
293: } catch (Throwable t) {
294: System.out.println("***************" + t);
295: }
296: }
297: }
|