KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > projectmanagement > presentation > payrates > Edit


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 JavaDoc;
15 import java.util.*;
16
17 /**
18  * Manages all actions on pay rates.
19  *
20  * @author Sasa Bojanic
21  * @version 1.0
22  */

23 public class Edit extends BasePO {
24
25    /**
26     * Constants
27     */

28    private static String JavaDoc EMPLOYEE = "cboEmployee";
29    private static String JavaDoc PROJECT = "cboProject";
30    private static String JavaDoc PAY_RATE = "txtPayRate";
31    private static String JavaDoc VALID_FROM_YYYY = "txtValidFromYYYY";
32    private static String JavaDoc VALID_FROM_MM = "txtValidFromMM";
33    private static String JavaDoc VALID_FROM_DD = "txtValidFromDD";
34
35    private static String JavaDoc PAY_RATE_ID = "payRateID";
36    private static String JavaDoc CONTEXT_PAGE = "Context.po";
37    private static String JavaDoc ADD = "add";
38    private static String JavaDoc DELETE = "delete";
39    private static String JavaDoc MODIFY = "modify";
40    private static String JavaDoc DETAILS = "showDetailsPage";
41    private static String JavaDoc EVENT = "event";
42
43    /**
44     * Superclass method override. Returns 2.
45     */

46    protected int getRequiredAuthLevel() {
47       return 2;
48    }
49
50    /**
51     * Default event. Just show the page for editing.
52     */

53    public XMLObject handleDefault()
54          throws HttpPresentationException {
55       return showModifyPage(null,false);
56    }
57
58    /**
59     * handle show add pay rate page event.
60     *
61     * @return html document
62     * @exception HttpPresentationException
63     */

64    public XMLObject handleShowAddPage()
65       throws HttpPresentationException {
66       return showAddPage(null);
67    }
68
69    /**
70     * handle show details pay rate page event.
71     *
72     * @return html document
73     * @exception HttpPresentationException
74     */

75    public XMLObject handleShowDetailsPage ()
76          throws HttpPresentationException {
77       return showModifyPage(null,true);
78    }
79
80    /*
81     * Modifies an existing pay rate
82     *
83     * @return html document
84     * @exception HttpPresentationException
85     */

86    public XMLObject handleModify()
87          throws HttpPresentationException {
88       String JavaDoc payRateID = this.getComms().request.getParameter(PAY_RATE_ID);
89       PayRate payRate=null;
90    
91       // Try to get the pay rate by its ID
92
try {
93        PayRateManager payRateManager = PayRateManagerFactory.
94           getPayRateManager("projectmanagement.business.timewage.PayRateManagerImpl");
95          payRate = payRateManager.findPayRateByID(payRateID);
96      
97       } catch(Exception JavaDoc 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 JavaDoc ex) {
106          return showModifyPage("To modify this pay rate, you must fill out fields properly!",false);
107       }
108    }
109
110    /*
111     * Adds a pay rate to the pay rate database
112     *
113     * @return html document
114     * @exception HttpPresentationException
115     */

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 /*
126  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
127  * We need to allow ProjectManagement_pres to be functional , response
128  * will be default HTML page
129  */

130  
131      } catch(NullPointerException JavaDoc ex) {
132           return showAddPage("You cannot add pay rate while runing ProjectManagement_pres");
133       } catch(Exception JavaDoc ex) {
134          return showAddPage("To add this pay rate, you must fill out fields properly!");
135       }
136    }
137
138    /*
139     * Deletes a PayRate from the PayRate database
140     *
141     * @return html document
142     * @exception HttpPresentationException
143     */

144    public XMLObject handleDelete()
145          throws HttpPresentationException, ProjectManagementPresentationException {
146       String JavaDoc 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 JavaDoc 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 any possible database exception as well as the null pointer
158
// exception if the pay rate is not found and is null after findPayRateByID
159
} catch(Exception JavaDoc ex) {
160          this.getSessionData().setUserMessage("Please choose a valid pay rate to delete");
161       }
162       // Redirect to the catalog page which will display the error message,
163
// if there was one set by the above exception
164
throw new ClientPageRedirectException(CONTEXT_PAGE);
165    }
166
167    /**
168     * Produce HTML for this PO, populated by the pay rate information
169     * that the user wants to edit
170     *
171     * @param errorMsg, the error messages
172     * @param disabled, if controls are disabled
173     * @return html document
174     * @exception HttpPresentationException
175     */

176    public XMLObject showModifyPage(String JavaDoc errorMsg,boolean disabled)
177          throws HttpPresentationException, ProjectManagementPresentationException {
178        
179       String JavaDoc employee = this.getComms().request.getParameter(EMPLOYEE);
180       String JavaDoc project = this.getComms().request.getParameter(PROJECT);
181       String JavaDoc rate = this.getComms().request.getParameter(PAY_RATE);
182       String JavaDoc validFromYYYY = this.getComms().request.getParameter(VALID_FROM_YYYY);
183       String JavaDoc validFromMM = this.getComms().request.getParameter(VALID_FROM_MM);
184       String JavaDoc validFromDD = this.getComms().request.getParameter(VALID_FROM_DD);
185
186       String JavaDoc payRateID = this.getComms().request.getParameter(PAY_RATE_ID);
187       // Instantiate the page object
188
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   /*
198  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
199  * We need to allow ProjectManagement_pres to be functional , response
200  * will be default HTML page
201  */

202  
203       } catch(NullPointerException JavaDoc ex) {
204            page.setTextErrorText("This is a default HTML page");
205            return page;
206          // Catch any possible database exception in findPayRateByID()
207
} 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          // If we received a valid payRateID then try to show the pay rate's contents,
214
// otherwise try to use the HTML input parameters
215
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    /**
286     * Produce HTML for this PO
287     *
288     * @param errorMsg, the error messages
289     * @return html document
290     * @exception HttpPresentationException
291     */

292    public XMLObject showAddPage(String JavaDoc errorMsg)
293          throws HttpPresentationException, ProjectManagementPresentationException {
294
295       String JavaDoc employee = this.getComms().request.getParameter(EMPLOYEE);
296       String JavaDoc project = this.getComms().request.getParameter(PROJECT);
297       String JavaDoc rate = this.getComms().request.getParameter(PAY_RATE);
298       String JavaDoc validFromYYYY = this.getComms().request.getParameter(VALID_FROM_YYYY);
299       String JavaDoc validFromMM = this.getComms().request.getParameter(VALID_FROM_MM);
300       String JavaDoc validFromDD = this.getComms().request.getParameter(VALID_FROM_DD);
301
302       // Instantiate the page object
303
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    /**
350     * Method to save a new or existing pay rate to the database
351     *
352     * @param payRate, the pay rate to be saved
353     * @return html document
354     * @exception HttpPresentationException
355     */

356    protected void savePayRate(PayRate thePayRate)
357          throws HttpPresentationException {
358
359       String JavaDoc employee = this.getComms().request.getParameter(EMPLOYEE);
360       String JavaDoc project = this.getComms().request.getParameter(PROJECT);
361       String JavaDoc rate = this.getComms().request.getParameter(PAY_RATE);
362       String JavaDoc validFromYYYY = this.getComms().request.getParameter(VALID_FROM_YYYY);
363       String JavaDoc validFromMM = this.getComms().request.getParameter(VALID_FROM_MM);
364       String JavaDoc 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 JavaDoc());
369       }
370
371       try {
372          String JavaDoc 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 JavaDoc ex) {
382          throw new ProjectManagementPresentationException("Error adding pay rate", ex);
383       }
384    }
385
386    private void fillEmployeeSelection (EditHTML page,HTMLSelectElement cboEmployees,
387          String JavaDoc selectedEmployeeID) throws ProjectManagementPresentationException {
388       //
389
HTMLOptionElement optEmployee = page.getElementOptEmployee();
390       
391       try {
392  EmployeeManager employeeManager = EmployeeManagerFactory.getEmployeeManager("projectmanagement.business.employee.EmployeeManagerImpl");
393 Employee[] employees=employeeManager.getAllEmployees();
394
395
396      // Remove the dummy storyboard text from the prototype HTML
397
optEmployee.removeChild(optEmployee.getFirstChild());
398        
399          if (employees!=null) {
400             for (int i=0; i<employees.length; i++) {
401                Employee e=employees[i];
402                // Now populate the combo with employees
403
// This algorithm is obscure because options
404
// are not normal HTML elements
405
// First populate the option value (the employee database ID).
406
// Then append a text child as the option
407
// text, which is what is displayed as the text
408
// in each row of the select box
409
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                // Do only a shallow copy of the option as we don't want the text child
418
// of the node option
419
cboEmployees.appendChild(clonedOption);
420                // Alternative way to insert nodes below
421
// insertBefore(newNode, oldNode);
422
// discSelect.insertBefore(clonedOption, templateOption);
423
}
424          }
425       cboEmployees.removeChild(optEmployee);
426   
427       } catch(NullPointerException JavaDoc ex) {
428       } catch (Exception JavaDoc 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 JavaDoc selectedProjectID) throws ProjectManagementPresentationException {
438       //
439
HTMLOptionElement optProject = page.getElementOptProject();
440       
441       try {
442       ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
443       Project[] projects=projectManager.getAllProjects();
444      // Remove the dummy storyboard text from the prototype HTML
445
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                // Now populate the combo with projects
452
// This algorithm is obscure because options
453
// are not normal HTML elements
454
// First populate the option value (the project database ID).
455
// Then append a text child as the option
456
// text, which is what is displayed as the text
457
// in each row of the select box
458
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                // Do only a shallow copy of the option as we don't want the text child
467
// of the node option
468
cboProjects.appendChild(clonedOption);
469                // Alternative way to insert nodes below
470
// insertBefore(newNode, oldNode);
471
// discSelect.insertBefore(clonedOption, templateOption);
472
}
473          } cboProjects.removeChild(optProject);
474       } catch(NullPointerException JavaDoc ex) {
475       } catch (Exception JavaDoc 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