1 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 ; 36 import javax.transaction.UserTransaction ; 37 import javax.transaction.Status ; 38 39 import java.util.List ; 40 import java.util.Locale ; 41 import java.util.TimeZone ; 42 43 47 public class DataLoaderUtil { 48 49 51 private static final String WORKFLOW_USERNAME = "workflow_user"; 53 54 private static String prepopLanguagesName = "Human Language"; 56 private static String [][] prepopLanguagesElements = 57 { 58 { "Language", "text,size=20" } 59 }; 60 private static String [] prepopLanguages = 61 { 62 "Afrikaans", "English", "French", "German", "Italian", "Shona", 63 "Spanish", "Xhosa", "Zulu" 64 }; 65 private static String prepopProgrammingLanguagesName = 66 "Programming Language"; 67 private static String [][] prepopProgrammingLanguagesElements = 68 { 69 { "Language", "text,size=10" } 70 }; 71 private static String [] prepopProgrammingLanguages = 72 { "C", "C++", "Java", "Pascal", "Ada", "Delphi", "Fortran", "Cobol" }; 73 private static String prepopAudiencesName = "Audience Type"; 74 private static String [][] prepopAudiencesElements = 75 { 76 { "Intended Audience", "text,size=15" } 77 }; 78 private static String [] prepopAudiences = 79 { 80 "Student", "Teacher", "Professor", "Hobbyist", "Toddler", "Teenager", 81 "Male", "Female", "Programmer", "Parent", "Scientist" 82 }; 83 private static String prepopLicensesName = "License"; 84 private static String [][] prepopLicensesElements = 85 { 86 { "License", "text,size=20" }, 87 { "Name", "text,size=40" }, 88 { "URL", "text,size=60" } 89 }; 90 private static String [] 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 prepopStatusName = "Status"; 109 private static String [][] prepopStatusElements = 110 { 111 { "status", "text,size=30" } 112 }; 113 private static String [] prepopStatus = 114 { 115 "New project", "Under development", "Alpha testing", "Beta testing", 116 "Available" 117 }; 118 119 120 public void initializeDataIfNeeded() { 121 try { 122 UserTransaction txn = null; 123 124 if (!HibernateUtil.isInTransaction() && 127 HibernateUtil.usingDataSource()) { 128 129 InitialContext context = new InitialContext (); 133 txn = (UserTransaction )context 134 .lookup(Constants.JTA_USER_TRANSACTION_JNDI_NAME); 135 txn.begin(); 136 context.close(); 137 } 138 139 createDefaultRolesIfNeeded(); 141 createDefaultUsersIfNeeded(); 142 createAttributeDataIfNeeded(); 143 144 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 e) { 155 e.printStackTrace(); 156 System.err.println("<< problem loading the initial data"); 157 } 158 } 159 160 protected void createDefaultRolesIfNeeded() { 161 try { 162 List roles = (List )InvokerFactory.getInvoker() 163 .invokeMethodOnService("Role", "getAllRoles", new Object [0]); 164 165 if (roles == null || roles.size() == 0) { 166 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 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 he.printStackTrace(); 198 } 199 } 200 } catch (Exception e) { 201 e.printStackTrace(); 202 } 203 } 204 205 protected void createAttributeDataIfNeeded() { 206 try { 207 List attributes = (List )InvokerFactory.getInvoker() 208 .invokeMethodOnModule("AttributeDataManagement", 209 "getExistingAttributes", new Object [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 e) { 232 e.printStackTrace(); 233 } 234 } 235 236 protected void createDefaultUsersIfNeeded() { 237 try { 238 List users = (List )InvokerFactory.getInvoker() 239 .invokeMethodOnModule("UserAdmin", "getAllUsers", new Object [0]); 240 241 if (users == null || users.size() == 0) { 242 CoefficientUser user = new CoefficientUser(); 244 user.setUserName("admin"); 245 user.setFullName("Site Administrator"); 246 user.setPassword(new String (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 CoefficientUser workflowUser = new CoefficientUser(); 257 workflowUser.setUserName(WORKFLOW_USERNAME); 258 workflowUser.setFullName("Workflow User"); 259 workflowUser.setPassword(new String (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 he.printStackTrace(); 271 } 272 } 273 } catch (Exception e) { 274 e.printStackTrace(); 275 } 276 } 277 278 protected void prepopulateAttributes(String prepopName, 279 String [][] prepopElements, 280 String [] 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 t) { 312 System.out.println("***************" + t); 313 } 314 } 315 } 316 | Popular Tags |