001: package projectmanagement.presentation.payrates;
002:
003: import projectmanagement.presentation.*;
004: import projectmanagement.spec.*;
005: import projectmanagement.spec.timewage.*;
006: import projectmanagement.spec.project.*;
007: import projectmanagement.spec.employee.*;
008: import com.lutris.appserver.server.httpPresentation.*;
009: import org.enhydra.xml.xmlc.XMLObject;
010:
011: import org.w3c.dom.*;
012: import org.w3c.dom.html.*;
013:
014: import java.sql.Date;
015: import java.util.*;
016:
017: /**
018: * Manages all actions on pay rates.
019: *
020: * @author Sasa Bojanic
021: * @version 1.0
022: */
023: public class Edit extends BasePO {
024:
025: /**
026: * Constants
027: */
028: private static String EMPLOYEE = "cboEmployee";
029: private static String PROJECT = "cboProject";
030: private static String PAY_RATE = "txtPayRate";
031: private static String VALID_FROM_YYYY = "txtValidFromYYYY";
032: private static String VALID_FROM_MM = "txtValidFromMM";
033: private static String VALID_FROM_DD = "txtValidFromDD";
034:
035: private static String PAY_RATE_ID = "payRateID";
036: private static String CONTEXT_PAGE = "Context.po";
037: private static String ADD = "add";
038: private static String DELETE = "delete";
039: private static String MODIFY = "modify";
040: private static String DETAILS = "showDetailsPage";
041: private static String EVENT = "event";
042:
043: /**
044: * Superclass method override. Returns 2.
045: */
046: protected int getRequiredAuthLevel() {
047: return 2;
048: }
049:
050: /**
051: * Default event. Just show the page for editing.
052: */
053: public XMLObject handleDefault() throws HttpPresentationException {
054: return showModifyPage(null, false);
055: }
056:
057: /**
058: * handle show add pay rate page event.
059: *
060: * @return html document
061: * @exception HttpPresentationException
062: */
063: public XMLObject handleShowAddPage()
064: throws HttpPresentationException {
065: return showAddPage(null);
066: }
067:
068: /**
069: * handle show details pay rate page event.
070: *
071: * @return html document
072: * @exception HttpPresentationException
073: */
074: public XMLObject handleShowDetailsPage()
075: throws HttpPresentationException {
076: return showModifyPage(null, true);
077: }
078:
079: /*
080: * Modifies an existing pay rate
081: *
082: * @return html document
083: * @exception HttpPresentationException
084: */
085: public XMLObject handleModify() throws HttpPresentationException {
086: String payRateID = this .getComms().request
087: .getParameter(PAY_RATE_ID);
088: PayRate payRate = null;
089:
090: // Try to get the pay rate by its ID
091: try {
092: PayRateManager payRateManager = PayRateManagerFactory
093: .getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl");
094: payRate = payRateManager.findPayRateByID(payRateID);
095:
096: } catch (Exception ex) {
097: this .getSessionData().setUserMessage(
098: "Please choose a valid pay rate to edit");
099: 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(
107: "To modify this pay rate, you must fill out fields properly!",
108: false);
109: }
110: }
111:
112: /*
113: * Adds a pay rate to the pay rate database
114: *
115: * @return html document
116: * @exception HttpPresentationException
117: */
118: public XMLObject handleAdd() throws HttpPresentationException {
119: try {
120: PayRateManager payRateManager = PayRateManagerFactory
121: .getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl");
122: PayRate payRate = payRateManager.getPayRate();
123:
124: savePayRate(payRate);
125: throw new ClientPageRedirectException(CONTEXT_PAGE);
126: /*
127: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
128: * We need to allow ProjectManagement_pres to be functional , response
129: * will be default HTML page
130: */
131:
132: } catch (NullPointerException ex) {
133: return showAddPage("You cannot add pay rate while runing ProjectManagement_pres");
134: } catch (Exception ex) {
135: return showAddPage("To add this pay rate, you must fill out fields properly!");
136: }
137: }
138:
139: /*
140: * Deletes a PayRate from the PayRate database
141: *
142: * @return html document
143: * @exception HttpPresentationException
144: */
145: public XMLObject handleDelete() throws HttpPresentationException,
146: ProjectManagementPresentationException {
147: String payRateID = this .getComms().request
148: .getParameter(PAY_RATE_ID);
149:
150: try {
151: PayRateManager payRateManager = PayRateManagerFactory
152: .getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl");
153: PayRate payRate = payRateManager.findPayRateByID(payRateID);
154:
155: String payRateName = "["
156: + payRate.getEmployee().getFirstName() + " "
157: + payRate.getEmployee().getLastName() + ","
158: + payRate.getProject().getName() + "]";
159: payRate.delete();
160: this .getSessionData().setUserMessage(
161: "The pay rate, " + payRateName + ", was deleted");
162: // Catch any possible database exception as well as the null pointer
163: // exception if the pay rate is not found and is null after findPayRateByID
164: } catch (Exception ex) {
165: this .getSessionData().setUserMessage(
166: "Please choose a valid pay rate to delete");
167: }
168: // Redirect to the catalog page which will display the error message,
169: // if there was one set by the above exception
170: throw new ClientPageRedirectException(CONTEXT_PAGE);
171: }
172:
173: /**
174: * Produce HTML for this PO, populated by the pay rate information
175: * that the user wants to edit
176: *
177: * @param errorMsg, the error messages
178: * @param disabled, if controls are disabled
179: * @return html document
180: * @exception HttpPresentationException
181: */
182: public XMLObject showModifyPage(String errorMsg, boolean disabled)
183: throws HttpPresentationException,
184: ProjectManagementPresentationException {
185:
186: String employee = this .getComms().request
187: .getParameter(EMPLOYEE);
188: String project = this .getComms().request.getParameter(PROJECT);
189: String rate = this .getComms().request.getParameter(PAY_RATE);
190: String validFromYYYY = this .getComms().request
191: .getParameter(VALID_FROM_YYYY);
192: String validFromMM = this .getComms().request
193: .getParameter(VALID_FROM_MM);
194: String validFromDD = this .getComms().request
195: .getParameter(VALID_FROM_DD);
196:
197: String payRateID = this .getComms().request
198: .getParameter(PAY_RATE_ID);
199: // Instantiate the page object
200: EditHTML page = new EditHTML();
201:
202: PayRate payRate = null;
203:
204: try {
205: PayRateManager payRateManager = PayRateManagerFactory
206: .getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl");
207: payRate = payRateManager.findPayRateByID(payRateID);
208:
209: /*
210: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
211: * We need to allow ProjectManagement_pres to be functional , response
212: * will be default HTML page
213: */
214:
215: } catch (NullPointerException ex) {
216: page.setTextErrorText("This is a default HTML page");
217: return page;
218: // Catch any possible database exception in findPayRateByID()
219: } catch (ProjectManagementException ex) {
220: this .getSessionData().setUserMessage(
221: "Please choose a valid pay rate to edit");
222: throw new ClientPageRedirectException(CONTEXT_PAGE);
223: }
224:
225: try {
226: // If we received a valid payRateID then try to show the pay rate's contents,
227: // otherwise try to use the HTML input parameters
228: page.getElementPayRateID().setValue(payRate.getHandle());
229:
230: HTMLSelectElement sel = page.getElementCboEmployee();
231: if (null == employee || employee.length() == 0) {
232: employee = payRate.getEmployee().getHandle();
233: }
234: fillEmployeeSelection(page, sel, employee);
235: sel.setDisabled(disabled);
236:
237: sel = page.getElementCboProject();
238: if (null == project || project.length() == 0) {
239: project = payRate.getProject().getHandle();
240: }
241: fillProjectSelection(page, sel, project);
242: sel.setDisabled(disabled);
243:
244: HTMLInputElement el = page.getElementTxtPayRate();
245: el.setDisabled(disabled);
246: if (null != rate && rate.length() != 0) {
247: el.setValue(rate);
248: } else {
249: el.setValue(String.valueOf(payRate.getRate()));
250: }
251:
252: el = page.getElementTxtValidFromYYYY();
253: el.setDisabled(disabled);
254: if (null != validFromYYYY && validFromYYYY.length() != 0) {
255: el.setValue(validFromYYYY);
256: } else {
257: Calendar cal = new GregorianCalendar();
258: cal.setTime(payRate.getValidFrom());
259: el.setValue(String.valueOf(cal.get(Calendar.YEAR)));
260: }
261:
262: el = page.getElementTxtValidFromMM();
263: el.setDisabled(disabled);
264: if (null != validFromMM && validFromMM.length() != 0) {
265: el.setValue(validFromMM);
266: } else {
267: Calendar cal = new GregorianCalendar();
268: cal.setTime(payRate.getValidFrom());
269: el
270: .setValue(String.valueOf(cal
271: .get(Calendar.MONTH) + 1));
272: }
273:
274: el = page.getElementTxtValidFromDD();
275: el.setDisabled(disabled);
276: if (null != validFromDD && validFromDD.length() != 0) {
277: el.setValue(validFromDD);
278: } else {
279: Calendar cal = new GregorianCalendar();
280: cal.setTime(payRate.getValidFrom());
281: el.setValue(String.valueOf(cal
282: .get(Calendar.DAY_OF_MONTH)));
283: }
284:
285: el = page.getElementBtnSave();
286: el.setDisabled(disabled);
287:
288: if (null == errorMsg) {
289: page.getElementErrorText().getParentNode().removeChild(
290: page.getElementErrorText());
291: } else {
292: page.setTextErrorText(errorMsg);
293: }
294: } catch (ProjectManagementException ex) {
295: throw new ProjectManagementPresentationException(
296: "Error populating page for pay rate editing", ex);
297: }
298:
299: page.getElementEvent().setValue(MODIFY);
300: return page;
301: }
302:
303: /**
304: * Produce HTML for this PO
305: *
306: * @param errorMsg, the error messages
307: * @return html document
308: * @exception HttpPresentationException
309: */
310: public XMLObject showAddPage(String errorMsg)
311: throws HttpPresentationException,
312: ProjectManagementPresentationException {
313:
314: String employee = this .getComms().request
315: .getParameter(EMPLOYEE);
316: String project = this .getComms().request.getParameter(PROJECT);
317: String rate = this .getComms().request.getParameter(PAY_RATE);
318: String validFromYYYY = this .getComms().request
319: .getParameter(VALID_FROM_YYYY);
320: String validFromMM = this .getComms().request
321: .getParameter(VALID_FROM_MM);
322: String validFromDD = this .getComms().request
323: .getParameter(VALID_FROM_DD);
324:
325: // Instantiate the page object
326: EditHTML page = new EditHTML();
327:
328: HTMLSelectElement sel = page.getElementCboEmployee();
329: fillEmployeeSelection(page, sel, employee);
330:
331: sel = page.getElementCboProject();
332: fillProjectSelection(page, sel, project);
333:
334: HTMLInputElement el = page.getElementTxtPayRate();
335: if (null != rate) {
336: el.setValue(rate);
337: } else {
338: el.setValue("");
339: }
340:
341: Calendar cal = new GregorianCalendar();
342: el = page.getElementTxtValidFromYYYY();
343: if (null != validFromYYYY) {
344: el.setValue(validFromYYYY);
345: } else {
346: el.setValue(String.valueOf(cal.get(Calendar.YEAR)));
347: }
348:
349: el = page.getElementTxtValidFromMM();
350: if (null != validFromMM) {
351: el.setValue(validFromMM);
352: } else {
353: el.setValue(String.valueOf(cal.get(Calendar.MONTH) + 1));
354: }
355:
356: el = page.getElementTxtValidFromDD();
357: if (null != validFromDD) {
358: el.setValue(validFromDD);
359: } else {
360: el.setValue(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
361: }
362:
363: if (null == errorMsg) {
364: page.getElementErrorText().getParentNode().removeChild(
365: page.getElementErrorText());
366: } else {
367: page.setTextErrorText(errorMsg);
368: }
369:
370: return page;
371: }
372:
373: /**
374: * Method to save a new or existing pay rate to the database
375: *
376: * @param payRate, the pay rate to be saved
377: * @return html document
378: * @exception HttpPresentationException
379: */
380: protected void savePayRate(PayRate thePayRate)
381: throws HttpPresentationException {
382:
383: String employee = this .getComms().request
384: .getParameter(EMPLOYEE);
385: String project = this .getComms().request.getParameter(PROJECT);
386: String rate = this .getComms().request.getParameter(PAY_RATE);
387: String validFromYYYY = this .getComms().request
388: .getParameter(VALID_FROM_YYYY);
389: String validFromMM = this .getComms().request
390: .getParameter(VALID_FROM_MM);
391: String validFromDD = this .getComms().request
392: .getParameter(VALID_FROM_DD);
393:
394: if (isNullField(employee)
395: || isNullField(project)
396: || isNullField(rate)
397: || !areDateFieldsValid(validFromYYYY, validFromMM,
398: validFromDD)) {
399: throw new ProjectManagementPresentationException(
400: "Error adding pay rate", new Exception());
401: }
402:
403: try {
404: String validFrom = validFromYYYY + "-" + validFromMM + "-"
405: + validFromDD;
406: EmployeeManager employeeManager = EmployeeManagerFactory
407: .getEmployeeManager("projectmanagement.business.employee.EmployeeManagerImpl");
408: thePayRate.setEmployee(employeeManager
409: .findEmployeeByID(employee));
410: ProjectManager projectManager = ProjectManagerFactory
411: .getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
412: thePayRate.setProject(projectManager
413: .findProjectByID(project));
414: thePayRate.setRate(Double.parseDouble(rate));
415: thePayRate.setValidFrom(Date.valueOf(validFrom));
416:
417: thePayRate.save();
418: } catch (Exception ex) {
419: throw new ProjectManagementPresentationException(
420: "Error adding pay rate", ex);
421: }
422: }
423:
424: private void fillEmployeeSelection(EditHTML page,
425: HTMLSelectElement cboEmployees, String selectedEmployeeID)
426: throws ProjectManagementPresentationException {
427: //
428: HTMLOptionElement optEmployee = page.getElementOptEmployee();
429:
430: try {
431: EmployeeManager employeeManager = EmployeeManagerFactory
432: .getEmployeeManager("projectmanagement.business.employee.EmployeeManagerImpl");
433: Employee[] employees = employeeManager.getAllEmployees();
434:
435: // Remove the dummy storyboard text from the prototype HTML
436: optEmployee.removeChild(optEmployee.getFirstChild());
437:
438: if (employees != null) {
439: for (int i = 0; i < employees.length; i++) {
440: Employee e = employees[i];
441: // Now populate the combo with employees
442: // This algorithm is obscure because options
443: // are not normal HTML elements
444: // First populate the option value (the employee database ID).
445: // Then append a text child as the option
446: // text, which is what is displayed as the text
447: // in each row of the select box
448: HTMLOptionElement clonedOption = (HTMLOptionElement) optEmployee
449: .cloneNode(true);
450: clonedOption.setValue(e.getHandle());
451: Node optionTextNode = clonedOption
452: .getOwnerDocument().createTextNode(
453: e.getFirstName() + " "
454: + e.getLastName());
455: clonedOption.appendChild(optionTextNode);
456: if (e.getHandle().equals(selectedEmployeeID)) {
457: clonedOption.setAttribute("selected",
458: "selected");
459: }
460: // Do only a shallow copy of the option as we don't want the text child
461: // of the node option
462: cboEmployees.appendChild(clonedOption);
463: // Alternative way to insert nodes below
464: // insertBefore(newNode, oldNode);
465: // discSelect.insertBefore(clonedOption, templateOption);
466: }
467: }
468: cboEmployees.removeChild(optEmployee);
469:
470: } catch (NullPointerException ex) {
471: } catch (Exception ex) {
472: this .writeDebugMsg("Error populating list of employees: "
473: + ex);
474: throw new ProjectManagementPresentationException(
475: "Error populating employee list: ", ex);
476: }
477:
478: }
479:
480: private void fillProjectSelection(EditHTML page,
481: HTMLSelectElement cboProjects, String selectedProjectID)
482: throws ProjectManagementPresentationException {
483: //
484: HTMLOptionElement optProject = page.getElementOptProject();
485:
486: try {
487: ProjectManager projectManager = ProjectManagerFactory
488: .getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
489: Project[] projects = projectManager.getAllProjects();
490: // Remove the dummy storyboard text from the prototype HTML
491: optProject.removeChild(optProject.getFirstChild());
492:
493: if (projects != null) {
494: for (int i = 0; i < projects.length; i++) {
495: Project p = projects[i];
496: // Now populate the combo with projects
497: // This algorithm is obscure because options
498: // are not normal HTML elements
499: // First populate the option value (the project database ID).
500: // Then append a text child as the option
501: // text, which is what is displayed as the text
502: // in each row of the select box
503: HTMLOptionElement clonedOption = (HTMLOptionElement) optProject
504: .cloneNode(true);
505: clonedOption.setValue(p.getHandle());
506: Node optionTextNode = clonedOption
507: .getOwnerDocument().createTextNode(
508: p.getName());
509: clonedOption.appendChild(optionTextNode);
510: if (p.getHandle().equals(selectedProjectID)) {
511: clonedOption.setAttribute("selected",
512: "selected");
513: }
514: // Do only a shallow copy of the option as we don't want the text child
515: // of the node option
516: cboProjects.appendChild(clonedOption);
517: // Alternative way to insert nodes below
518: // insertBefore(newNode, oldNode);
519: // discSelect.insertBefore(clonedOption, templateOption);
520: }
521: }
522: cboProjects.removeChild(optProject);
523: } catch (NullPointerException ex) {
524: } catch (Exception ex) {
525: this .writeDebugMsg("Error populating list of projects: "
526: + ex);
527: throw new ProjectManagementPresentationException(
528: "Error populating project list: ", ex);
529: }
530:
531: }
532:
533: }
|