KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
15  * Manages all actions on projects.
16  *
17  * @author Sasa Bojanic
18  * @version 1.0
19  */

20 public class Edit extends BasePO {
21
22    /**
23     * Constants
24     */

25    private static String JavaDoc CUSTOMER = "cboCustomer";
26    private static String JavaDoc NAME = "txtName";
27    private static String JavaDoc MONEY_PER_HOUR = "txtMoneyPerHour";
28
29    private static String JavaDoc PROJECT_ID = "projectID";
30    private static String JavaDoc CONTEXT_PAGE = "Context.po";
31    private static String JavaDoc ADD = "add";
32    private static String JavaDoc DELETE = "delete";
33    private static String JavaDoc MODIFY = "modify";
34    private static String JavaDoc DETAILS = "showDetailsPage";
35    private static String JavaDoc EVENT = "event";
36
37    /**
38     * Superclass method override. Returns 1.
39     */

40    protected int getRequiredAuthLevel() {
41       /*String event="";
42       try {
43          event=this.getComms().request.getParameter(EVENT);
44       } catch (Exception ex) {}
45
46       if (event!=null && event.equalsIgnoreCase(DETAILS)) {
47          return 1;
48       } else {
49          return 2;
50       }*/

51       return 2;
52    }
53
54    /**
55     * Default event. Just show the page for editing.
56     */

57    public XMLObject handleDefault()
58          throws HttpPresentationException {
59       return showModifyPage(null,false);
60    }
61
62    /**
63     * handle show add project page event.
64     *
65     * @return html document
66     * @exception HttpPresentationException
67     */

68    public XMLObject handleShowAddPage()
69       throws HttpPresentationException {
70       return showAddPage(null);
71    }
72
73    /**
74     * handle show details project page event.
75     *
76     * @return html document
77     * @exception HttpPresentationException
78     */

79    public XMLObject handleShowDetailsPage ()
80          throws HttpPresentationException {
81       return showModifyPage(null,true);
82    }
83
84    /*
85     * Modifies an existing project
86     *
87     * @return html document
88     * @exception HttpPresentationException
89     */

90    public XMLObject handleModify()
91          throws HttpPresentationException {
92       String JavaDoc projectID = this.getComms().request.getParameter(PROJECT_ID);
93       Project project=null;
94
95       // Try to get the project by its ID
96
try {
97 ProjectManager projectManager = ProjectManagerFactory.getProjectManager("projectmanagement.business.project.ProjectManagerImpl");
98      project = projectManager.findProjectByID(projectID);
99     
100       } catch(Exception JavaDoc 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 JavaDoc ex) {
109          return showModifyPage("To add this project, you must fill out fields properly!",false);
110       }
111    }
112
113    /*
114     * Adds a project to the project database
115     *
116     * @return html document
117     * @exception HttpPresentationException
118     */

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

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

146    public XMLObject handleDelete()
147          throws HttpPresentationException, ProjectManagementPresentationException {
148       String JavaDoc 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 JavaDoc projectName = project.getName();
155          project.delete();
156          this.getSessionData().setUserMessage("The project, " + projectName + ", was deleted");
157          // Catch any possible database exception as well as the null pointer
158
// exception if the project is not found and is null after findProjectByID
159
} catch(Exception JavaDoc ex) {
160          this.getSessionData().setUserMessage("Please choose a valid project 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 project 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 customer = this.getComms().request.getParameter(CUSTOMER);
180       String JavaDoc name = this.getComms().request.getParameter(NAME);
181       String JavaDoc moneyPerHour = this.getComms().request.getParameter(MONEY_PER_HOUR);
182
183       String JavaDoc projectID = this.getComms().request.getParameter(PROJECT_ID);
184       // Instantiate the page object
185
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   /*
193  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
194  * We need to allow ProjectManagement_pres to be functional , response
195  * will be default HTML page with message
196  */

197  
198       } catch(NullPointerException JavaDoc ex) {
199            page.setTextErrorText("This is a default HTML page");
200            return page;
201          // Catch any possible database exception in findProjectByID()
202
} 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          // If we received a valid projectID then try to show the project's contents,
209
// otherwise try to use the HTML input parameters
210
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    /**
254     * Produce HTML for this PO
255     *
256     * @param errorMsg, the error messages
257     * @return html document
258     * @exception HttpPresentationException
259     */

260    public XMLObject showAddPage(String JavaDoc errorMsg)
261          throws HttpPresentationException, ProjectManagementPresentationException {
262
263       String JavaDoc customer = this.getComms().request.getParameter(CUSTOMER);
264       String JavaDoc name = this.getComms().request.getParameter(NAME);
265       String JavaDoc moneyPerHour = this.getComms().request.getParameter(MONEY_PER_HOUR);
266
267       // Instantiate the page object
268
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    /**
298     * Method to save a new or existing project to the database
299     *
300     * @param project, the project to be saved
301     * @return html document
302     * @exception HttpPresentationException
303     */

304    protected void saveProject(Project theProject)
305          throws HttpPresentationException {
306
307       String JavaDoc customer = this.getComms().request.getParameter(CUSTOMER);
308       String JavaDoc name = this.getComms().request.getParameter(NAME);
309       String JavaDoc 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 JavaDoc());
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 JavaDoc ex) {
325          throw new ProjectManagementPresentationException("Error adding project", ex);
326       }
327    }
328
329    private void fillSelection (EditHTML page,HTMLSelectElement cboCustomers,
330          String JavaDoc selectedCustomerID) throws ProjectManagementPresentationException {
331       //
332
HTMLOptionElement optCustomer = page.getElementOptCustomer();
333       
334       try {
335        
336        CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
337      Customer[] customers = customerManager.getAllCustomers();
338
339        // Remove the dummy storyboard text from the prototype HTML
340
optCustomer.removeChild(optCustomer.getFirstChild());
341
342          if (customers!=null) {
343             for (int i=0; i<customers.length; i++) {
344                Customer c=customers[i];
345                // Now populate the combo with customers
346
// This algorithm is obscure because options
347
// are not normal HTML elements
348
// First populate the option value (the customer database ID).
349
// Then append a text child as the option
350
// text, which is what is displayed as the text
351
// in each row of the select box
352
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                // Do only a shallow copy of the option as we don't want the text child
361
// of the node option
362
cboCustomers.appendChild(clonedOption);
363                // Alternative way to insert nodes below
364
// insertBefore(newNode, oldNode);
365
// discSelect.insertBefore(clonedOption, templateOption);
366
}
367          }
368          cboCustomers.removeChild(optCustomer);
369       } catch(NullPointerException JavaDoc ex) {
370       } catch (Exception JavaDoc 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