1 package projectmanagement.presentation.projects; 2 3 import projectmanagement.presentation.*; 4 import projectmanagement.spec.*; 5 6 import projectmanagement.spec.project.*; 7 import projectmanagement.spec.customer.*; 8 import com.lutris.appserver.server.httpPresentation.*; 9 import org.enhydra.xml.xmlc.XMLObject; 10 11 import org.w3c.dom.*; 12 import org.w3c.dom.html.*; 13 14 20 public class Edit extends BasePO { 21 22 25 private static String CUSTOMER = "cboCustomer"; 26 private static String NAME = "txtName"; 27 private static String MONEY_PER_HOUR = "txtMoneyPerHour"; 28 29 private static String PROJECT_ID = "projectID"; 30 private static String CONTEXT_PAGE = "Context.po"; 31 private static String ADD = "add"; 32 private static String DELETE = "delete"; 33 private static String MODIFY = "modify"; 34 private static String DETAILS = "showDetailsPage"; 35 private static String EVENT = "event"; 36 37 40 protected int getRequiredAuthLevel() { 41 51 return 2; 52 } 53 54 57 public XMLObject handleDefault() 58 throws HttpPresentationException { 59 return showModifyPage(null,false); 60 } 61 62 68 public XMLObject handleShowAddPage() 69 throws HttpPresentationException { 70 return showAddPage(null); 71 } 72 73 79 public XMLObject handleShowDetailsPage () 80 throws HttpPresentationException { 81 return showModifyPage(null,true); 82 } 83 84 90 public XMLObject handleModify() 91 throws HttpPresentationException { 92 String projectID = this.getComms().request.getParameter(PROJECT_ID); 93 Project project=null; 94 95 try { 97 ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl"); 98 project = projectManager.findProjectByID(projectID); 99 100 } catch(Exception ex) { 101 this.getSessionData().setUserMessage("Please choose a valid project to edit"); 102 throw new ClientPageRedirectException(CONTEXT_PAGE); 103 } 104 105 try { 106 saveProject(project); 107 throw new ClientPageRedirectException(CONTEXT_PAGE); 108 } catch(Exception ex) { 109 return showModifyPage("To add this project, you must fill out fields properly!",false); 110 } 111 } 112 113 119 public XMLObject handleAdd() 120 throws HttpPresentationException { 121 try { 122 ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl"); 123 Project project = projectManager.getProject(); 124 125 saveProject(project); 126 throw new ClientPageRedirectException(CONTEXT_PAGE); 127 132 133 } catch(NullPointerException ex) { 134 return showAddPage("You cannot add project while runing ProjectManagement_pres"); 135 } catch(Exception ex) { 136 return showAddPage("To add this project, you must fill out fields properly!"); 137 } 138 } 139 140 146 public XMLObject handleDelete() 147 throws HttpPresentationException, ProjectManagementPresentationException { 148 String projectID = this.getComms().request.getParameter(PROJECT_ID); 149 150 try { 151 ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl"); 152 Project project = projectManager.findProjectByID(projectID); 153 154 String projectName = project.getName(); 155 project.delete(); 156 this.getSessionData().setUserMessage("The project, " + projectName + ", was deleted"); 157 } catch(Exception ex) { 160 this.getSessionData().setUserMessage("Please choose a valid project to delete"); 161 } 162 throw new ClientPageRedirectException(CONTEXT_PAGE); 165 } 166 167 176 public XMLObject showModifyPage(String errorMsg,boolean disabled) 177 throws HttpPresentationException, ProjectManagementPresentationException { 178 179 String customer = this.getComms().request.getParameter(CUSTOMER); 180 String name = this.getComms().request.getParameter(NAME); 181 String moneyPerHour = this.getComms().request.getParameter(MONEY_PER_HOUR); 182 183 String projectID = this.getComms().request.getParameter(PROJECT_ID); 184 EditHTML page = new EditHTML(); 186 187 Project project = null; 188 189 try { 190 ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl"); 191 project = projectManager.findProjectByID(projectID); 192 197 198 } catch(NullPointerException ex) { 199 page.setTextErrorText("This is a default HTML page"); 200 return page; 201 } catch(ProjectManagementException ex) { 203 this.getSessionData().setUserMessage("Please choose a valid project to edit"); 204 throw new ClientPageRedirectException(CONTEXT_PAGE); 205 } 206 207 try { 208 page.getElementProjectID().setValue(project.getHandle()); 211 212 HTMLSelectElement sel=page.getElementCboCustomer(); 213 214 if (null == customer || customer.length() == 0) { 215 customer=project.getCustomer().getHandle(); 216 } 217 218 fillSelection(page,sel,customer); 219 sel.setDisabled(disabled); 220 221 HTMLInputElement el=page.getElementTxtName(); 222 el.setDisabled(disabled); 223 if (null != name && name.length() != 0) { 224 el.setValue(name); 225 } else { 226 el.setValue(project.getName()); 227 } 228 229 el=page.getElementTxtMoneyPerHour(); 230 el.setDisabled(disabled); 231 if (null != moneyPerHour && moneyPerHour.length() != 0) { 232 el.setValue(moneyPerHour); 233 } else { 234 el.setValue(String.valueOf(project.getMoneyPerHour())); 235 } 236 237 el=page.getElementBtnSave(); 238 el.setDisabled(disabled); 239 240 if (null == errorMsg) { 241 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText()); 242 } else { 243 page.setTextErrorText(errorMsg); 244 } 245 } catch(ProjectManagementException ex) { 246 throw new ProjectManagementPresentationException("Error populating page for project editing", ex); 247 } 248 249 page.getElementEvent().setValue(MODIFY); 250 return page; 251 } 252 253 260 public XMLObject showAddPage(String errorMsg) 261 throws HttpPresentationException, ProjectManagementPresentationException { 262 263 String customer = this.getComms().request.getParameter(CUSTOMER); 264 String name = this.getComms().request.getParameter(NAME); 265 String moneyPerHour = this.getComms().request.getParameter(MONEY_PER_HOUR); 266 267 EditHTML page = new EditHTML(); 269 270 HTMLSelectElement sel=page.getElementCboCustomer(); 271 272 fillSelection(page,sel,customer); 273 274 HTMLInputElement el=page.getElementTxtName(); 275 if (null != name) { 276 el.setValue(name); 277 } else { 278 el.setValue(""); 279 } 280 281 el=page.getElementTxtMoneyPerHour(); 282 if (null != moneyPerHour) { 283 el.setValue(moneyPerHour); 284 } else { 285 el.setValue(""); 286 } 287 288 if (null == errorMsg) { 289 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText()); 290 } else { 291 page.setTextErrorText(errorMsg); 292 } 293 294 return page; 295 } 296 297 304 protected void saveProject(Project theProject) 305 throws HttpPresentationException { 306 307 String customer = this.getComms().request.getParameter(CUSTOMER); 308 String name = this.getComms().request.getParameter(NAME); 309 String moneyPerHour = this.getComms().request.getParameter(MONEY_PER_HOUR); 310 311 if (isNullField(customer) || isNullField(name) || isNullField(moneyPerHour)) { 312 throw new ProjectManagementPresentationException("Error adding project",new Exception ()); 313 } 314 315 try { 316 CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl"); 317 318 319 theProject.setCustomer(customerManager.findCustomerByID(customer)); 320 theProject.setName(name); 321 theProject.setMoneyPerHour(Double.parseDouble(moneyPerHour)); 322 323 theProject.save(); 324 } catch(Exception ex) { 325 throw new ProjectManagementPresentationException("Error adding project", ex); 326 } 327 } 328 329 private void fillSelection (EditHTML page,HTMLSelectElement cboCustomers, 330 String selectedCustomerID) throws ProjectManagementPresentationException { 331 HTMLOptionElement optCustomer = page.getElementOptCustomer(); 333 334 try { 335 336 CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl"); 337 Customer[] customers = customerManager.getAllCustomers(); 338 339 optCustomer.removeChild(optCustomer.getFirstChild()); 341 342 if (customers!=null) { 343 for (int i=0; i<customers.length; i++) { 344 Customer c=customers[i]; 345 HTMLOptionElement clonedOption = (HTMLOptionElement)optCustomer.cloneNode(true); 353 clonedOption.setValue(c.getHandle()); 354 Node optionTextNode = clonedOption.getOwnerDocument(). 355 createTextNode(c.getCompanyName()); 356 clonedOption.appendChild(optionTextNode); 357 if (c.getHandle().equals(selectedCustomerID)) { 358 clonedOption.setAttribute("selected","selected"); 359 } 360 cboCustomers.appendChild(clonedOption); 363 } 367 } 368 cboCustomers.removeChild(optCustomer); 369 } catch(NullPointerException ex) { 370 } catch (Exception ex) { 371 this.writeDebugMsg("Error populating list of customers: " + ex); 372 throw new ProjectManagementPresentationException("Error populating customer list: ", ex); 373 } 374 375 376 } 377 } 378 379 | Popular Tags |