KickJava   Java API By Example, From Geeks To Geeks.

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


1 package projectmanagement.presentation.customers;
2
3 import projectmanagement.presentation.*;
4 import projectmanagement.spec.*;
5 import projectmanagement.spec.customer.*;
6 import com.lutris.appserver.server.httpPresentation.*;
7 import org.enhydra.xml.xmlc.XMLObject;
8
9 import org.w3c.dom.*;
10 import org.w3c.dom.html.*;
11
12 /**
13  * Manages all actions on customers.
14  *
15  * @author Sasa Bojanic
16  * @version 1.0
17  */

18 public class Edit extends BasePO {
19
20    /**
21     * Constants
22     */

23    private static String JavaDoc COMPANY_NAME = "txtCompanyName";
24    private static String JavaDoc CONTACT_NAME = "txtContactName";
25    private static String JavaDoc CONTACT_TITLE = "txtContactTitle";
26    private static String JavaDoc ADDRESS = "txtAddress";
27    private static String JavaDoc CITY = "txtCity";
28    private static String JavaDoc REGION = "txtRegion";
29    private static String JavaDoc POSTAL_CODE = "txtPostalCode";
30    private static String JavaDoc COUNTRY = "txtCountry";
31    private static String JavaDoc PHONE = "txtPhone";
32    private static String JavaDoc FAX = "txtFax";
33    private static String JavaDoc EMAIL = "txtEmail";
34    private static String JavaDoc NOTES = "txtNotes";
35
36    private static String JavaDoc CUSTOMER_ID = "customerID";
37    private static String JavaDoc CONTEXT_PAGE = "Context.po";
38    private static String JavaDoc ADD = "add";
39    private static String JavaDoc DELETE = "delete";
40    private static String JavaDoc MODIFY = "modify";
41    private static String JavaDoc DETAILS = "showDetailsPage";
42    private static String JavaDoc EVENT = "event";
43
44    /**
45     * Superclass method override. Returns 1.
46     */

47    protected int getRequiredAuthLevel() {
48       String JavaDoc event="";
49       try {
50          event=this.getComms().request.getParameter(EVENT);
51       } catch (Exception JavaDoc ex) {}
52
53       if (event!=null && event.equalsIgnoreCase(DETAILS)) {
54          return 1;
55       } else {
56          return 2;
57       }
58    }
59
60    /**
61     * Default event. Just show the page for editing.
62     */

63    public XMLObject handleDefault()
64          throws HttpPresentationException {
65       return showModifyPage(null,false);
66    }
67
68    /**
69     * handle show add customer page event.
70     *
71     * @return html document
72     * @exception HttpPresentationException
73     */

74    public XMLObject handleShowAddPage()
75       throws HttpPresentationException {
76       return showAddPage(null);
77    }
78
79    /**
80     * handle show details customer page event.
81     *
82     * @return html document
83     * @exception HttpPresentationException
84     */

85    public XMLObject handleShowDetailsPage ()
86          throws HttpPresentationException {
87       return showModifyPage(null,true);
88    }
89
90    /*
91     * Modifies an existing customer
92     *
93     * @return html document
94     * @exception HttpPresentationException
95     */

96    public XMLObject handleModify()
97          throws HttpPresentationException {
98       String JavaDoc customerID = this.getComms().request.getParameter(CUSTOMER_ID);
99       Customer customer=null;
100
101       // Try to get the customer by its ID
102
try {
103        
104       CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
105       customer = customerManager.findCustomerByID(customerID);
106     
107       } catch(Exception JavaDoc ex) {
108          this.getSessionData().setUserMessage("Please choose a valid customer to edit");
109          throw new ClientPageRedirectException(CONTEXT_PAGE);
110       }
111
112       try {
113          saveCustomer(customer);
114          throw new ClientPageRedirectException(CONTEXT_PAGE);
115       } catch(Exception JavaDoc ex) {
116          return showModifyPage("To modify this customer, you must fill out fields properly!",false);
117       }
118    }
119
120    /*
121     * Adds a customer to the customer database
122     *
123     * @return html document
124     * @exception HttpPresentationException
125     */

126    public XMLObject handleAdd()
127          throws HttpPresentationException {
128       try {
129           CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
130            Customer customer = customerManager.getCustomer();
131    
132          saveCustomer(customer);
133          throw new ClientPageRedirectException(CONTEXT_PAGE);
134 /*
135  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
136  * We need to allow ProjectManagement_pres to be functional , response
137  * will be default HTML page
138  */

139  
140       } catch(NullPointerException JavaDoc ex) {
141           return showAddPage("You cannot add customer while runing ProjectManagement_pres");
142       } catch(Exception JavaDoc ex) {
143          return showAddPage("To add this customer, you must fill out fields properly!");
144       }
145    }
146
147    /*
148     * Deletes a Customer from the Customer database
149     *
150     * @return html document
151     * @exception HttpPresentationException
152     */

153    public XMLObject handleDelete()
154          throws HttpPresentationException, ProjectManagementPresentationException {
155       String JavaDoc customerID = this.getComms().request.getParameter(CUSTOMER_ID);
156
157       try {
158 CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
159       Customer customer = customerManager.findCustomerByID(customerID);
160       
161       
162          String JavaDoc companyName = customer.getCompanyName();
163          customer.delete();
164          this.getSessionData().setUserMessage("The customer, " + companyName + ", was deleted");
165          // Catch any possible database exception as well as the null pointer
166
// exception if the customer is not found and is null after findCustomerByID
167
} catch(Exception JavaDoc ex) {
168          this.getSessionData().setUserMessage("Please choose a valid customer to delete");
169       }
170       // Redirect to the catalog page which will display the error message,
171
// if there was one set by the above exception
172
throw new ClientPageRedirectException(CONTEXT_PAGE);
173    }
174
175    /**
176     * Produce HTML for this PO, populated by the customer information
177     * that the user wants to edit
178     *
179     * @param errorMsg, the error messages
180     * @param disabled, if controls are disabled
181     * @return html document
182     * @exception HttpPresentationException
183     */

184    public XMLObject showModifyPage(String JavaDoc errorMsg,boolean disabled)
185          throws HttpPresentationException, ProjectManagementPresentationException {
186       String JavaDoc companyName = this.getComms().request.getParameter(COMPANY_NAME);
187       String JavaDoc contactName = this.getComms().request.getParameter(CONTACT_NAME);
188       String JavaDoc contactTitle = this.getComms().request.getParameter(CONTACT_TITLE);
189       String JavaDoc address = this.getComms().request.getParameter(ADDRESS);
190       String JavaDoc city = this.getComms().request.getParameter(CITY);
191       String JavaDoc region = this.getComms().request.getParameter(REGION);
192       String JavaDoc postalCode = this.getComms().request.getParameter(POSTAL_CODE);
193       String JavaDoc country = this.getComms().request.getParameter(COUNTRY);
194       String JavaDoc phone = this.getComms().request.getParameter(PHONE);
195       String JavaDoc fax = this.getComms().request.getParameter(FAX);
196       String JavaDoc email = this.getComms().request.getParameter(EMAIL);
197       String JavaDoc notes = this.getComms().request.getParameter(NOTES);
198
199       String JavaDoc customerID = this.getComms().request.getParameter(CUSTOMER_ID);
200       // Instantiate the page object
201
EditHTML page = new EditHTML();
202
203       Customer customer = null;
204
205       try {
206     CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
207        customer = customerManager.findCustomerByID(customerID);
208   /*
209  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
210  * We need to allow ProjectManagement_pres to be functional , response
211  * will be default HTML page with message
212  */

213  
214       } catch(NullPointerException JavaDoc ex) {
215            page.setTextErrorText("This is a default HTML page");
216            return page;
217          // Catch any possible database exception in findCustomerByID()
218
} catch(ProjectManagementException ex) {
219          this.getSessionData().setUserMessage("Please choose a valid customer to edit");
220          throw new ClientPageRedirectException(CONTEXT_PAGE);
221       }
222
223       try {
224          // If we received a valid customerID then try to show the customer's contents,
225
// otherwise try to use the HTML input parameters
226
page.getElementCustomerID().setValue(customer.getHandle());
227
228          HTMLInputElement el=page.getElementTxtCompanyName();
229          el.setDisabled(disabled);
230          if (null != companyName && companyName.length() != 0) {
231             el.setValue(companyName);
232          } else {
233             el.setValue(customer.getCompanyName());
234          }
235
236          el=page.getElementTxtContactName();
237          el.setDisabled(disabled);
238          if (null != contactName && contactName.length() != 0) {
239             el.setValue(contactName);
240          } else {
241             el.setValue(customer.getContactName());
242          }
243
244          el=page.getElementTxtContactTitle();
245          el.setDisabled(disabled);
246          if (null != contactTitle && contactTitle.length() != 0) {
247             el.setValue(contactTitle);
248          } else {
249             el.setValue(customer.getContactTitle());
250          }
251
252          el=page.getElementTxtAddress();
253          el.setDisabled(disabled);
254          if (null != address && address.length() != 0) {
255             el.setValue(address);
256          } else {
257             el.setValue(customer.getAddress());
258          }
259
260          el=page.getElementTxtCity();
261          el.setDisabled(disabled);
262          if (null != city && city.length() != 0) {
263             el.setValue(city);
264          } else {
265             el.setValue(customer.getCity());
266          }
267
268          el=page.getElementTxtRegion();
269          el.setDisabled(disabled);
270          if (null != region && region.length() != 0) {
271             el.setValue(region);
272          } else {
273             el.setValue(customer.getRegion());
274          }
275
276          el=page.getElementTxtPostalCode();
277          el.setDisabled(disabled);
278          if (null != postalCode && postalCode.length() != 0) {
279             el.setValue(postalCode);
280          } else {
281             el.setValue(customer.getPostalCode());
282          }
283
284          el=page.getElementTxtCountry();
285          el.setDisabled(disabled);
286          if (null != country && country.length() != 0) {
287             el.setValue(country);
288          } else {
289             el.setValue(customer.getCountry());
290          }
291
292          el=page.getElementTxtPhone();
293          el.setDisabled(disabled);
294          if (null != phone && phone.length() != 0) {
295             el.setValue(phone);
296          } else {
297             el.setValue(customer.getPhone());
298          }
299
300          el=page.getElementTxtFax();
301          el.setDisabled(disabled);
302          if (null != fax && fax.length() != 0) {
303             el.setValue(fax);
304          } else {
305             el.setValue(customer.getFax());
306          }
307
308          el=page.getElementTxtEmail();
309          el.setDisabled(disabled);
310          if (null != email && email.length() != 0) {
311             el.setValue(email);
312          } else {
313             el.setValue(customer.getEmail());
314          }
315
316          //HTMLTextAreaElement tael=page.getElementTxtNotes();
317
el=page.getElementTxtNotes();
318          el.setDisabled(disabled);
319          if (null != notes && notes.length() != 0) {
320             el.setValue(notes);
321          } else {
322             el.setValue(customer.getNotes());
323          }
324
325          el=page.getElementBtnSave();
326          el.setDisabled(disabled);
327
328          if (null == errorMsg) {
329             page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
330          } else {
331             page.setTextErrorText(errorMsg);
332          }
333       } catch(ProjectManagementException ex) {
334          throw new ProjectManagementPresentationException("Error populating page for customer editing", ex);
335       }
336
337       page.getElementEvent().setValue(MODIFY);
338       return page;
339    }
340
341    /**
342     * Produce HTML for this PO
343     *
344     * @param errorMsg, the error messages
345     * @return html document
346     * @exception HttpPresentationException
347     */

348    public XMLObject showAddPage(String JavaDoc errorMsg)
349          throws HttpPresentationException, ProjectManagementPresentationException {
350
351       String JavaDoc companyName = this.getComms().request.getParameter(COMPANY_NAME);
352       String JavaDoc contactName = this.getComms().request.getParameter(CONTACT_NAME);
353       String JavaDoc contactTitle = this.getComms().request.getParameter(CONTACT_TITLE);
354       String JavaDoc address = this.getComms().request.getParameter(ADDRESS);
355       String JavaDoc city = this.getComms().request.getParameter(CITY);
356       String JavaDoc region = this.getComms().request.getParameter(REGION);
357       String JavaDoc postalCode = this.getComms().request.getParameter(POSTAL_CODE);
358       String JavaDoc country = this.getComms().request.getParameter(COUNTRY);
359       String JavaDoc phone = this.getComms().request.getParameter(PHONE);
360       String JavaDoc fax = this.getComms().request.getParameter(FAX);
361       String JavaDoc email = this.getComms().request.getParameter(EMAIL);
362       String JavaDoc notes = this.getComms().request.getParameter(NOTES);
363
364       // Instantiate the page object
365
EditHTML page = new EditHTML();
366
367       HTMLInputElement el=page.getElementTxtCompanyName();
368       if (null != companyName) {
369          el.setValue(companyName);
370       } else {
371          el.setValue("");
372       }
373
374       el=page.getElementTxtContactName();
375       if (null != contactName) {
376          el.setValue(contactName);
377       } else {
378          el.setValue("");
379       }
380
381       el=page.getElementTxtContactTitle();
382       if (null != contactTitle) {
383          el.setValue(contactTitle);
384       } else {
385          el.setValue("");
386       }
387
388       el=page.getElementTxtAddress();
389       if (null != address) {
390          el.setValue(address);
391       } else {
392          el.setValue("");
393       }
394
395       el=page.getElementTxtCity();
396       if (null != city) {
397          el.setValue(city);
398       } else {
399          el.setValue("");
400       }
401
402       el=page.getElementTxtRegion();
403       if (null != region) {
404          el.setValue(region);
405       } else {
406          el.setValue("");
407       }
408
409       el=page.getElementTxtPostalCode();
410       if (null != postalCode) {
411          el.setValue(postalCode);
412       } else {
413          el.setValue("");
414       }
415
416       el=page.getElementTxtCountry();
417       if (null != country) {
418          el.setValue(country);
419       } else {
420          el.setValue("");
421       }
422
423       el=page.getElementTxtPhone();
424       if (null != phone) {
425          el.setValue(phone);
426       } else {
427          el.setValue("");
428       }
429
430       el=page.getElementTxtFax();
431       if (null != fax) {
432          el.setValue(fax);
433       } else {
434          el.setValue("");
435       }
436
437       el=page.getElementTxtEmail();
438       if (null != email) {
439          el.setValue(email);
440       } else {
441          el.setValue("");
442       }
443
444       //HTMLTextAreaElement tael=page.getElementTxtNotes();
445
el=page.getElementTxtNotes();
446       if (null != notes) {
447          el.setValue(notes);
448       } else {
449          el.setValue("");
450       }
451
452       if (null == errorMsg) {
453          page.getElementErrorText().getParentNode().removeChild(page.getElementErrorText());
454       } else {
455          page.setTextErrorText(errorMsg);
456       }
457
458       return page;
459    }
460
461    /**
462     * Method to save a new or existing customer to the database
463     *
464     * @param customer, the customer to be saved
465     * @return html document
466     * @exception HttpPresentationException
467     */

468    protected void saveCustomer(Customer theCustomer)
469          throws HttpPresentationException {
470       String JavaDoc companyName = this.getComms().request.getParameter(COMPANY_NAME);
471       String JavaDoc contactName = this.getComms().request.getParameter(CONTACT_NAME);
472       String JavaDoc contactTitle = this.getComms().request.getParameter(CONTACT_TITLE);
473       String JavaDoc address = this.getComms().request.getParameter(ADDRESS);
474       String JavaDoc city = this.getComms().request.getParameter(CITY);
475       String JavaDoc region = this.getComms().request.getParameter(REGION);
476       String JavaDoc postalCode = this.getComms().request.getParameter(POSTAL_CODE);
477       String JavaDoc country = this.getComms().request.getParameter(COUNTRY);
478       String JavaDoc phone = this.getComms().request.getParameter(PHONE);
479       String JavaDoc fax = this.getComms().request.getParameter(FAX);
480       String JavaDoc email = this.getComms().request.getParameter(EMAIL);
481       String JavaDoc notes = this.getComms().request.getParameter(NOTES);
482
483       if (isNullField(companyName) || isNullField(contactName)) {
484          throw new ProjectManagementPresentationException("Error adding customer",new Exception JavaDoc());
485       }
486
487       try {
488          theCustomer.setCompanyName(companyName);
489          theCustomer.setContactName(contactName);
490          theCustomer.setContactTitle(contactTitle);
491          theCustomer.setAddress(address);
492          theCustomer.setCity(city);
493          theCustomer.setRegion(region);
494          theCustomer.setPostalCode(postalCode);
495          theCustomer.setCountry(country);
496          theCustomer.setPhone(phone);
497          theCustomer.setFax(fax);
498          theCustomer.setEmail(email);
499          theCustomer.setNotes(notes);
500
501          theCustomer.save();
502       } catch(Exception JavaDoc ex) {
503          throw new ProjectManagementPresentationException("Error adding customer", ex);
504       }
505    }
506
507 }
508
509
Popular Tags