1 64 65 70 package com.jcorporate.expresso.services.servlet; 71 72 import com.jcorporate.expresso.core.ExpressoSchema; 73 import com.jcorporate.expresso.core.db.DBException; 74 import com.jcorporate.expresso.core.dbobj.Schema; 75 import com.jcorporate.expresso.core.dbobj.SchemaFactory; 76 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 77 import com.jcorporate.expresso.core.jsdkapi.GenericSession; 78 import com.jcorporate.expresso.core.misc.ConfigManager; 79 import com.jcorporate.expresso.core.misc.ConfigurationException; 80 import com.jcorporate.expresso.core.misc.CurrentLogin; 81 import com.jcorporate.expresso.core.misc.StringUtil; 82 import com.jcorporate.expresso.core.security.User; 83 import com.jcorporate.expresso.core.servlet.DBServlet; 84 import com.jcorporate.expresso.core.servlet.ServletInstallLog; 85 import com.jcorporate.expresso.core.utility.DBTool; 86 import com.jcorporate.expresso.services.dbobj.SchemaList; 87 import com.jcorporate.expresso.services.html.Button; 88 import com.jcorporate.expresso.services.html.Cell; 89 import com.jcorporate.expresso.services.html.CheckBox; 90 import com.jcorporate.expresso.services.html.DropDown; 91 import com.jcorporate.expresso.services.html.Form; 92 import com.jcorporate.expresso.services.html.HiddenField; 93 import com.jcorporate.expresso.services.html.Page; 94 import com.jcorporate.expresso.services.html.Paragraph; 95 import com.jcorporate.expresso.services.html.Row; 96 import com.jcorporate.expresso.services.html.Table; 97 import com.jcorporate.expresso.services.html.Text; 98 import org.apache.log4j.Logger; 99 100 import javax.servlet.ServletConfig ; 101 import javax.servlet.ServletException ; 102 import javax.servlet.http.HttpServletRequest ; 103 import javax.servlet.http.HttpServletResponse ; 104 import java.io.IOException ; 105 import java.util.Enumeration ; 106 import java.util.Iterator ; 107 import java.util.List ; 108 import java.util.Vector ; 109 110 111 119 public class DBCreate 120 extends DBServlet { 121 private static Logger log = Logger.getLogger(DBCreate.class); 122 123 128 public void init(ServletConfig sc) 129 throws ServletException { 130 super.init(sc); 131 } 132 133 134 144 public void doGet(HttpServletRequest request, HttpServletResponse response) 145 throws ServletException , IOException { 146 147 try { 148 ExpressoSchema jc = (ExpressoSchema) ExpressoSchema.getInstance(); 149 150 if (jc == null) { 151 jc = new ExpressoSchema(); 152 } 153 154 boolean databaseCreated = isInitialDatabaseCreated(); 155 if (!databaseCreated) { 156 setSkipLogin(true); 157 } 158 159 super.doGet(request, response); 160 161 if (databaseCreated && !isAdmin(request, response)) { 162 throw new java.lang.SecurityException ("You must have Administrative rights to execute DBCreate"); 163 } 164 165 166 Page myPage = new Page("Initial Setup Options"); 167 Paragraph para = new Paragraph(new Text(getString(request, 168 "Initialize_Database"))); 169 para.setCSSClass("jc-pageheader"); myPage.add(para); 171 172 Form myForm = new Form(); 173 myPage.add(myForm); 174 175 Table formTable = new Table("formtable"); myForm.add(formTable); 177 myForm.setAction(getServletPrefix(request)); 178 179 Row oneRow = new Row("db row"); 180 Cell oneCell = new Cell(new Text(getString(request, 181 "Context/Database_"))); 182 oneCell.setCSSClass("jc-label"); 183 oneRow.add(oneCell); 184 185 DropDown dbContext = new DropDown("dbContext", "default"); 186 String oneConfigKey = null; 187 String oneDescrip = null; 188 189 for (Enumeration ie = ConfigManager.getAllConfigKeys(); 190 ie.hasMoreElements();) { 191 oneConfigKey = (String ) ie.nextElement(); 192 193 try { 194 oneDescrip = StringUtil.notNull(ConfigManager.getContext(oneConfigKey).getDescription()); 195 } catch (ConfigurationException ce) { 196 oneDescrip = ""; 197 } 198 if (oneDescrip.equals("")) { oneDescrip = oneConfigKey; 200 } 201 202 dbContext.addOption(oneConfigKey, oneDescrip); 203 } 204 205 oneRow.add(new Cell(dbContext)); 206 formTable.add(oneRow); 207 oneRow = new Row(); 208 formTable.add(oneRow); 209 210 Text t = new Text(getString(request, "Create_Tables_")); 211 t.setCSSClass("jc-label"); 212 oneRow.add(new Cell(t)); 213 oneRow.add(new Cell(new CheckBox("CreateTables", "Y", true))); 214 oneRow = new Row(); 215 formTable.add(oneRow); 216 217 t = new Text(getString(request, "Verify_Created_Tables_")); 218 t.setCSSClass("jc-label"); 219 oneRow.add(new Cell(t)); 220 oneRow.add(new Cell(new CheckBox("VerifyTables", "Y", true))); 221 oneRow = new Row(); 222 formTable.add(oneRow); 223 224 t = new Text(getString(request, "Setup_Default_Security_")); 225 t.setCSSClass("jc-label"); 226 oneRow.add(new Cell(t)); 227 oneRow.add(new Cell(new CheckBox("SetupSecurity", "Y", true))); 228 oneRow = new Row(); 229 formTable.add(oneRow); 230 t = new Text(getString(request, "Create_Default_Config_Valu")); 231 t.setCSSClass("jc-label"); 232 oneRow.add(new Cell(t)); 233 oneRow.add(new Cell(new CheckBox("ConfigValues", "Y", true))); 234 oneRow = new Row(); 235 formTable.add(oneRow); 236 t = new Text(getString(request, "Perform_additonal_Setup_")); 237 t.setCSSClass("jc-label"); 238 oneRow.add(new Cell(t)); 239 oneRow.add(new Cell(new CheckBox("OtherSetup", "Y", true))); 240 241 Table buttonTable = new Table("button table"); 242 buttonTable.setAlignment("center"); 243 buttonTable.add(new Row(new Cell(new Button("Run", 244 getString(request, 245 "Run"))))); 246 myForm.add(new HiddenField("back", 247 StringUtil.notNull(request.getParameter("back")))); 248 myForm.add(buttonTable); 249 myPage.display(request, response, getString(request, "charset")); 250 } catch (Exception de) { 251 showError(de, request, response); 252 } 253 } 254 255 256 266 public void doPost(HttpServletRequest request, 267 HttpServletResponse response) 268 throws ServletException , IOException { 269 boolean databaseCreated = isInitialDatabaseCreated(); 270 if (!databaseCreated) { 271 setSkipLogin(true); 272 } 273 274 super.doPost(request, response); 275 276 if (databaseCreated && !isAdmin(request, response)) { 277 throw new java.lang.SecurityException ("You must have Administrative rights to execute DBCreate"); 278 } 279 280 281 String createTables = StringUtil.notNull(request.getParameter("CreateTables")); 282 String validateTables = StringUtil.notNull(request.getParameter("VerifyTables")); 283 String setupSecurity = StringUtil.notNull(request.getParameter("SetupSecurity")); 284 String setupConfigValues = StringUtil.notNull(request.getParameter("ConfigValues")); 285 String otherSetup = StringUtil.notNull(request.getParameter("OtherSetup")); 286 String dbContext = StringUtil.notNull(request.getParameter("dbContext")); 287 response.setContentType("text/html; charset=iso-8859-1"); 288 java.io.PrintWriter out = response.getWriter(); 289 290 try { 291 ExpressoSchema jc = (ExpressoSchema) SchemaFactory.getInstance() 292 .getSchema(com.jcorporate.expresso 293 .core.ExpressoSchema.class.getName()); 294 295 if (jc == null) { 296 throw new ServletException ("Unable to construct Expresso Schema. Check logs for details"); 297 } 298 299 out.println("<html><head><title>Database Initialization</title></head><body class=\"jc-default\">" + 300 "<h1 align=\"center\">Database Initialization</h1>"); 301 302 Vector allSchemas = new Vector (5); 303 allSchemas.addElement(jc); 304 305 SchemaList sl = null; 306 SchemaList oneSchema = null; 307 boolean noOtherSchemas = true; 308 309 try { 310 sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 311 sl.setDataContext(dbContext); 312 sl.search(); 313 314 if (sl.getFoundCount() > 0) { 315 noOtherSchemas = false; 316 } 317 318 320 } catch (DBException de) { 321 noOtherSchemas = true; 322 } 323 if (!noOtherSchemas) { 324 for (Iterator e = sl.searchAndRetrieveList().iterator(); 325 e.hasNext();) { 326 oneSchema = (SchemaList) e.next(); 327 328 boolean schemaWarning = false; 329 330 Schema mySchema2 = SchemaFactory.getInstance() 331 .getSchema(oneSchema.getField("SchemaClass")); 332 333 if (mySchema2 == null) { 334 schemaWarning = true; 335 } else { 336 allSchemas.addElement(mySchema2); 337 } 338 339 if (schemaWarning) { 340 out.println("<p>Warning: Unable to process schema " + oneSchema.getField("SchemaClass") + 341 " - see log for details</p>"); 342 } 343 } 344 345 } 346 347 out.println("<p align=\"center\">" + getString(request, 348 "Processing_initializations") + "</p>"); 349 350 ServletInstallLog installLog = new ServletInstallLog(out); 351 352 if (createTables.equalsIgnoreCase("Y")) { 353 DBTool.createTables(installLog, allSchemas, dbContext); 354 } 355 356 if (validateTables.equalsIgnoreCase("Y")) { 357 List errors = DBTool.compareTables(installLog, allSchemas, dbContext); 358 359 for (Iterator iterator = errors.iterator(); iterator.hasNext();) { 360 String str = (String ) iterator.next(); 361 out.println("<p align=\"center\">" + str + "</p>"); 362 } 363 } 364 365 try { 366 if (setupSecurity.equalsIgnoreCase("Y")) { 370 DBTool.setupSecurity(installLog, allSchemas, dbContext); 371 } 372 373 if (setupConfigValues.equalsIgnoreCase("Y")) { 374 DBTool.populateTables(installLog, allSchemas, dbContext); 375 } 376 if (otherSetup.equalsIgnoreCase("Y")) { 377 DBTool.otherSetups(installLog, allSchemas, dbContext); 378 } 379 out.println("<p align=\"center\">" + getString(request, 380 "All_functions_complete") + "</p>"); 381 382 ConfigManager.initializeAllDBObjects(); 384 ConfigManager.mapOtherDBs(); 385 } catch (Exception e) { 386 387 out.println("<p align=\"center\"> PROBLEM with initialization of database: </p>"); 389 e.printStackTrace(out); 390 } 391 392 393 } catch (Exception de) { 394 log.error("Error performing setup", de); 395 showError(de, request, response); 396 } 397 } 398 399 400 406 protected boolean isInitialDatabaseCreated() { 407 try { 408 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 409 sl.setDataContext("default"); 410 sl.count(); 411 } catch (DBException dbe) { 412 return false; 413 } 414 return true; 415 } 416 417 425 protected boolean isAdmin(HttpServletRequest request, HttpServletResponse response) throws ServletException { 426 try { 427 CurrentLogin cl = (CurrentLogin) GenericSession.getAttribute(request, "CurrentLogin"); 428 if (cl == null) { 429 return false; 430 } 431 432 int uid = cl.getUid(); 433 String dbName = cl.getDBName(); 434 User u = new User(); 435 u.setUid(uid); 436 u.setDataContext(dbName); 437 try { 438 if (!u.find()) { 439 return false; 440 441 } 442 } catch (DBException ex1) { 443 log.error("Error performing find", ex1); 444 return false; 445 } 446 447 448 java.util.List groupList = u.getGroupsList(); 449 return groupList.contains(User.ADMIN_USER); 450 } catch (ClassCastException e) { 451 return false; 452 } catch (Exception ex) { 453 throw new ServletException ("Error checking isAdmin:", ex); 454 } 455 } 456 457 458 } 459 | Popular Tags |