1 package projectmanagement.presentation.payrates; 2 3 import projectmanagement.presentation.*; 4 import projectmanagement.spec.*; 5 import projectmanagement.spec.timewage.*; 6 import projectmanagement.spec.project.*; 7 import projectmanagement.spec.employee.*; 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 import java.sql.Date ; 15 import java.util.*; 16 17 23 public class Edit extends BasePO { 24 25 28 private static String EMPLOYEE = "cboEmployee"; 29 private static String PROJECT = "cboProject"; 30 private static String PAY_RATE = "txtPayRate"; 31 private static String VALID_FROM_YYYY = "txtValidFromYYYY"; 32 private static String VALID_FROM_MM = "txtValidFromMM"; 33 private static String VALID_FROM_DD = "txtValidFromDD"; 34 35 private static String PAY_RATE_ID = "payRateID"; 36 private static String CONTEXT_PAGE = "Context.po"; 37 private static String ADD = "add"; 38 private static String DELETE = "delete"; 39 private static String MODIFY = "modify"; 40 private static String DETAILS = "showDetailsPage"; 41 private static String EVENT = "event"; 42 43 46 protected int getRequiredAuthLevel() { 47 return 2; 48 } 49 50 53 public XMLObject handleDefault() 54 throws HttpPresentationException { 55 return showModifyPage(null,false); 56 } 57 58 64 public XMLObject handleShowAddPage() 65 throws HttpPresentationException { 66 return showAddPage(null); 67 } 68 69 75 public XMLObject handleShowDetailsPage () 76 throws HttpPresentationException { 77 return showModifyPage(null,true); 78 } 79 80 86 public XMLObject handleModify() 87 throws HttpPresentationException { 88 String payRateID = this.getComms().request.getParameter(PAY_RATE_ID); 89 PayRate payRate=null; 90 91 try { 93 PayRateManager payRateManager = PayRateManagerFactory. 94 getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl"); 95 payRate = payRateManager.findPayRateByID(payRateID); 96 97 } catch(Exception ex) { 98 this.getSessionData().setUserMessage("Please choose a valid pay rate to edit"); 99 throw new ClientPageRedirectException(CONTEXT_PAGE); 100 } 101 102 try { 103 savePayRate(payRate); 104 throw new ClientPageRedirectException(CONTEXT_PAGE); 105 } catch(Exception ex) { 106 return showModifyPage("To modify this pay rate, you must fill out fields properly!",false); 107 } 108 } 109 110 116 public XMLObject handleAdd() 117 throws HttpPresentationException { 118 try { 119 PayRateManager payRateManager = PayRateManagerFactory. 120 getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl"); 121 PayRate payRate = payRateManager.getPayRate(); 122 123 savePayRate(payRate); 124 throw new ClientPageRedirectException(CONTEXT_PAGE); 125 130 131 } catch(NullPointerException ex) { 132 return showAddPage("You cannot add pay rate while runing ProjectManagement_pres"); 133 } catch(Exception ex) { 134 return showAddPage("To add this pay rate, you must fill out fields properly!"); 135 } 136 } 137 138 144 public XMLObject handleDelete() 145 throws HttpPresentationException, ProjectManagementPresentationException { 146 String payRateID = this.getComms().request.getParameter(PAY_RATE_ID); 147 148 try { 149 PayRateManager payRateManager = PayRateManagerFactory. 150 getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl"); 151 PayRate payRate = payRateManager.findPayRateByID(payRateID); 152 153 String payRateName = "["+payRate.getEmployee().getFirstName()+" "+ 154 payRate.getEmployee().getLastName()+","+payRate.getProject().getName()+"]"; 155 payRate.delete(); 156 this.getSessionData().setUserMessage("The pay rate, " + payRateName + ", was deleted"); 157 } catch(Exception ex) { 160 this.getSessionData().setUserMessage("Please choose a valid pay rate 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 employee = this.getComms().request.getParameter(EMPLOYEE); 180 String project = this.getComms().request.getParameter(PROJECT); 181 String rate = this.getComms().request.getParameter(PAY_RATE); 182 String validFromYYYY = this.getComms().request.getParameter(VALID_FROM_YYYY); 183 String validFromMM = this.getComms().request.getParameter(VALID_FROM_MM); 184 String validFromDD = this.getComms().request.getParameter(VALID_FROM_DD); 185 186 String payRateID = this.getComms().request.getParameter(PAY_RATE_ID); 187 EditHTML page = new EditHTML(); 189 190 PayRate payRate = null; 191 192 try { 193 PayRateManager payRateManager = PayRateManagerFactory. 194 getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl"); 195 payRate = payRateManager.findPayRateByID(payRateID); 196 197 202 203 } catch(NullPointerException ex) { 204 page.setTextErrorText("This is a default HTML page"); 205 return page; 206 } catch(ProjectManagementException ex) { 208 this.getSessionData().setUserMessage("Please choose a valid pay rate to edit"); 209 throw new ClientPageRedirectException(CONTEXT_PAGE); 210 } 211 212 try { 213 page.getElementPayRateID().setValue(payRate.getHandle()); 216 217 HTMLSelectElement sel=page.getElementCboEmployee(); 218 if (null == employee || employee.length() == 0) { 219 employee=payRate.getEmployee().getHandle(); 220 } 221 fillEmployeeSelection(page,sel,employee); 222 sel.setDisabled(disabled); 223 224 sel=page.getElementCboProject(); 225 if (null == project || project.length() == 0) { 226 project=payRate.getProject().getHandle(); 227 } 228 fillProjectSelection(page,sel,project); 229 sel.setDisabled(disabled); 230 231 HTMLInputElement el=page.getElementTxtPayRate(); 232 el.setDisabled(disabled); 233 if (null != rate && rate.length() != 0) { 234 el.setValue(rate); 235 } else { 236 el.setValue(String.valueOf(payRate.getRate())); 237 } 238 239 el=page.getElementTxtValidFromYYYY(); 240 el.setDisabled(disabled); 241 if (null != validFromYYYY && validFromYYYY.length() != 0) { 242 el.setValue(validFromYYYY); 243 } else { 244 Calendar cal=new GregorianCalendar(); 245 cal.setTime(payRate.getValidFrom()); 246 el.setValue(String.valueOf(cal.get(Calendar.YEAR))); 247 } 248 249 el=page.getElementTxtValidFromMM(); 250 el.setDisabled(disabled); 251 if (null != validFromMM && validFromMM.length() != 0) { 252 el.setValue(validFromMM); 253 } else { 254 Calendar cal=new GregorianCalendar(); 255 cal.setTime(payRate.getValidFrom()); 256 el.setValue(String.valueOf(cal.get(Calendar.MONTH)+1)); 257 } 258 259 el=page.getElementTxtValidFromDD(); 260 el.setDisabled(disabled); 261 if (null != validFromDD && validFromDD.length() != 0) { 262 el.setValue(validFromDD); 263 } else { 264 Calendar cal=new GregorianCalendar(); 265 cal.setTime(payRate.getValidFrom()); 266 el.setValue(String.valueOf(cal.get(Calendar.DAY_OF_MONTH))); 267 } 268 269 el=page.getElementBtnSave(); 270 el.setDisabled(disabled); 271 272 if (null == errorMsg) { 273 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText()); 274 } else { 275 page.setTextErrorText(errorMsg); 276 } 277 } catch(ProjectManagementException ex) { 278 throw new ProjectManagementPresentationException("Error populating page for pay rate editing", ex); 279 } 280 281 page.getElementEvent().setValue(MODIFY); 282 return page; 283 } 284 285 292 public XMLObject showAddPage(String errorMsg) 293 throws HttpPresentationException, ProjectManagementPresentationException { 294 295 String employee = this.getComms().request.getParameter(EMPLOYEE); 296 String project = this.getComms().request.getParameter(PROJECT); 297 String rate = this.getComms().request.getParameter(PAY_RATE); 298 String validFromYYYY = this.getComms().request.getParameter(VALID_FROM_YYYY); 299 String validFromMM = this.getComms().request.getParameter(VALID_FROM_MM); 300 String validFromDD = this.getComms().request.getParameter(VALID_FROM_DD); 301 302 EditHTML page = new EditHTML(); 304 305 HTMLSelectElement sel=page.getElementCboEmployee(); 306 fillEmployeeSelection(page,sel,employee); 307 308 sel=page.getElementCboProject(); 309 fillProjectSelection(page,sel,project); 310 311 HTMLInputElement el=page.getElementTxtPayRate(); 312 if (null != rate) { 313 el.setValue(rate); 314 } else { 315 el.setValue(""); 316 } 317 318 Calendar cal=new GregorianCalendar(); 319 el=page.getElementTxtValidFromYYYY(); 320 if (null != validFromYYYY) { 321 el.setValue(validFromYYYY); 322 } else { 323 el.setValue(String.valueOf(cal.get(Calendar.YEAR))); 324 } 325 326 el=page.getElementTxtValidFromMM(); 327 if (null != validFromMM) { 328 el.setValue(validFromMM); 329 } else { 330 el.setValue(String.valueOf(cal.get(Calendar.MONTH)+1)); 331 } 332 333 el=page.getElementTxtValidFromDD(); 334 if (null != validFromDD) { 335 el.setValue(validFromDD); 336 } else { 337 el.setValue(String.valueOf(cal.get(Calendar.DAY_OF_MONTH))); 338 } 339 340 if (null == errorMsg) { 341 page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText()); 342 } else { 343 page.setTextErrorText(errorMsg); 344 } 345 346 return page; 347 } 348 349 356 protected void savePayRate(PayRate thePayRate) 357 throws HttpPresentationException { 358 359 String employee = this.getComms().request.getParameter(EMPLOYEE); 360 String project = this.getComms().request.getParameter(PROJECT); 361 String rate = this.getComms().request.getParameter(PAY_RATE); 362 String validFromYYYY = this.getComms().request.getParameter(VALID_FROM_YYYY); 363 String validFromMM = this.getComms().request.getParameter(VALID_FROM_MM); 364 String validFromDD = this.getComms().request.getParameter(VALID_FROM_DD); 365 366 if (isNullField(employee) || isNullField(project) || isNullField(rate) || 367 !areDateFieldsValid(validFromYYYY,validFromMM,validFromDD)) { 368 throw new ProjectManagementPresentationException("Error adding pay rate",new Exception ()); 369 } 370 371 try { 372 String validFrom=validFromYYYY+"-"+validFromMM+"-"+validFromDD; 373 EmployeeManager employeeManager = EmployeeManagerFactory.getEmployeeManager("projectmanagement.business.employee.EmployeeManagerImpl"); 374 thePayRate.setEmployee(employeeManager.findEmployeeByID(employee)); 375 ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl"); 376 thePayRate.setProject(projectManager.findProjectByID(project)); 377 thePayRate.setRate(Double.parseDouble(rate)); 378 thePayRate.setValidFrom(Date.valueOf(validFrom)); 379 380 thePayRate.save(); 381 } catch(Exception ex) { 382 throw new ProjectManagementPresentationException("Error adding pay rate", ex); 383 } 384 } 385 386 private void fillEmployeeSelection (EditHTML page,HTMLSelectElement cboEmployees, 387 String selectedEmployeeID) throws ProjectManagementPresentationException { 388 HTMLOptionElement optEmployee = page.getElementOptEmployee(); 390 391 try { 392 EmployeeManager employeeManager = EmployeeManagerFactory.getEmployeeManager("projectmanagement.business.employee.EmployeeManagerImpl"); 393 Employee[] employees=employeeManager.getAllEmployees(); 394 395 396 optEmployee.removeChild(optEmployee.getFirstChild()); 398 399 if (employees!=null) { 400 for (int i=0; i<employees.length; i++) { 401 Employee e=employees[i]; 402 HTMLOptionElement clonedOption = (HTMLOptionElement)optEmployee.cloneNode(true); 410 clonedOption.setValue(e.getHandle()); 411 Node optionTextNode = clonedOption.getOwnerDocument(). 412 createTextNode(e.getFirstName()+" "+e.getLastName()); 413 clonedOption.appendChild(optionTextNode); 414 if (e.getHandle().equals(selectedEmployeeID)) { 415 clonedOption.setAttribute("selected","selected"); 416 } 417 cboEmployees.appendChild(clonedOption); 420 } 424 } 425 cboEmployees.removeChild(optEmployee); 426 427 } catch(NullPointerException ex) { 428 } catch (Exception ex) { 429 this.writeDebugMsg("Error populating list of employees: " + ex); 430 throw new ProjectManagementPresentationException("Error populating employee list: ", ex); 431 } 432 433 434 } 435 436 private void fillProjectSelection (EditHTML page,HTMLSelectElement cboProjects, 437 String selectedProjectID) throws ProjectManagementPresentationException { 438 HTMLOptionElement optProject = page.getElementOptProject(); 440 441 try { 442 ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl"); 443 Project[] projects=projectManager.getAllProjects(); 444 optProject.removeChild(optProject.getFirstChild()); 446 447 448 if (projects!=null) { 449 for (int i=0; i<projects.length; i++) { 450 Project p=projects[i]; 451 HTMLOptionElement clonedOption = (HTMLOptionElement)optProject.cloneNode(true); 459 clonedOption.setValue(p.getHandle()); 460 Node optionTextNode = clonedOption.getOwnerDocument(). 461 createTextNode(p.getName()); 462 clonedOption.appendChild(optionTextNode); 463 if (p.getHandle().equals(selectedProjectID)) { 464 clonedOption.setAttribute("selected","selected"); 465 } 466 cboProjects.appendChild(clonedOption); 469 } 473 } cboProjects.removeChild(optProject); 474 } catch(NullPointerException ex) { 475 } catch (Exception ex) { 476 this.writeDebugMsg("Error populating list of projects: " + ex); 477 throw new ProjectManagementPresentationException("Error populating project list: ", ex); 478 } 479 480 481 } 482 483 } 484 485 | Popular Tags |