001: package projectmanagement.presentation.customers;
002:
003: import projectmanagement.presentation.*;
004: import projectmanagement.spec.*;
005: import projectmanagement.spec.customer.*;
006: import com.lutris.appserver.server.httpPresentation.*;
007: import org.enhydra.xml.xmlc.XMLObject;
008:
009: import org.w3c.dom.*;
010: import org.w3c.dom.html.*;
011:
012: /**
013: * Manages all actions on customers.
014: *
015: * @author Sasa Bojanic
016: * @version 1.0
017: */
018: public class Edit extends BasePO {
019:
020: /**
021: * Constants
022: */
023: private static String COMPANY_NAME = "txtCompanyName";
024: private static String CONTACT_NAME = "txtContactName";
025: private static String CONTACT_TITLE = "txtContactTitle";
026: private static String ADDRESS = "txtAddress";
027: private static String CITY = "txtCity";
028: private static String REGION = "txtRegion";
029: private static String POSTAL_CODE = "txtPostalCode";
030: private static String COUNTRY = "txtCountry";
031: private static String PHONE = "txtPhone";
032: private static String FAX = "txtFax";
033: private static String EMAIL = "txtEmail";
034: private static String NOTES = "txtNotes";
035:
036: private static String CUSTOMER_ID = "customerID";
037: private static String CONTEXT_PAGE = "Context.po";
038: private static String ADD = "add";
039: private static String DELETE = "delete";
040: private static String MODIFY = "modify";
041: private static String DETAILS = "showDetailsPage";
042: private static String EVENT = "event";
043:
044: /**
045: * Superclass method override. Returns 1.
046: */
047: protected int getRequiredAuthLevel() {
048: String event = "";
049: try {
050: event = this .getComms().request.getParameter(EVENT);
051: } catch (Exception ex) {
052: }
053:
054: if (event != null && event.equalsIgnoreCase(DETAILS)) {
055: return 1;
056: } else {
057: return 2;
058: }
059: }
060:
061: /**
062: * Default event. Just show the page for editing.
063: */
064: public XMLObject handleDefault() throws HttpPresentationException {
065: return showModifyPage(null, false);
066: }
067:
068: /**
069: * handle show add customer page event.
070: *
071: * @return html document
072: * @exception HttpPresentationException
073: */
074: public XMLObject handleShowAddPage()
075: throws HttpPresentationException {
076: return showAddPage(null);
077: }
078:
079: /**
080: * handle show details customer page event.
081: *
082: * @return html document
083: * @exception HttpPresentationException
084: */
085: public XMLObject handleShowDetailsPage()
086: throws HttpPresentationException {
087: return showModifyPage(null, true);
088: }
089:
090: /*
091: * Modifies an existing customer
092: *
093: * @return html document
094: * @exception HttpPresentationException
095: */
096: public XMLObject handleModify() throws HttpPresentationException {
097: String customerID = this .getComms().request
098: .getParameter(CUSTOMER_ID);
099: Customer customer = null;
100:
101: // Try to get the customer by its ID
102: try {
103:
104: CustomerManager customerManager = CustomerManagerFactory
105: .getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
106: customer = customerManager.findCustomerByID(customerID);
107:
108: } catch (Exception ex) {
109: this .getSessionData().setUserMessage(
110: "Please choose a valid customer to edit");
111: throw new ClientPageRedirectException(CONTEXT_PAGE);
112: }
113:
114: try {
115: saveCustomer(customer);
116: throw new ClientPageRedirectException(CONTEXT_PAGE);
117: } catch (Exception ex) {
118: return showModifyPage(
119: "To modify this customer, you must fill out fields properly!",
120: false);
121: }
122: }
123:
124: /*
125: * Adds a customer to the customer database
126: *
127: * @return html document
128: * @exception HttpPresentationException
129: */
130: public XMLObject handleAdd() throws HttpPresentationException {
131: try {
132: CustomerManager customerManager = CustomerManagerFactory
133: .getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
134: Customer customer = customerManager.getCustomer();
135:
136: saveCustomer(customer);
137: throw new ClientPageRedirectException(CONTEXT_PAGE);
138: /*
139: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
140: * We need to allow ProjectManagement_pres to be functional , response
141: * will be default HTML page
142: */
143:
144: } catch (NullPointerException ex) {
145: return showAddPage("You cannot add customer while runing ProjectManagement_pres");
146: } catch (Exception ex) {
147: return showAddPage("To add this customer, you must fill out fields properly!");
148: }
149: }
150:
151: /*
152: * Deletes a Customer from the Customer database
153: *
154: * @return html document
155: * @exception HttpPresentationException
156: */
157: public XMLObject handleDelete() throws HttpPresentationException,
158: ProjectManagementPresentationException {
159: String customerID = this .getComms().request
160: .getParameter(CUSTOMER_ID);
161:
162: try {
163: CustomerManager customerManager = CustomerManagerFactory
164: .getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
165: Customer customer = customerManager
166: .findCustomerByID(customerID);
167:
168: String companyName = customer.getCompanyName();
169: customer.delete();
170: this .getSessionData().setUserMessage(
171: "The customer, " + companyName + ", was deleted");
172: // Catch any possible database exception as well as the null pointer
173: // exception if the customer is not found and is null after findCustomerByID
174: } catch (Exception ex) {
175: this .getSessionData().setUserMessage(
176: "Please choose a valid customer to delete");
177: }
178: // Redirect to the catalog page which will display the error message,
179: // if there was one set by the above exception
180: throw new ClientPageRedirectException(CONTEXT_PAGE);
181: }
182:
183: /**
184: * Produce HTML for this PO, populated by the customer information
185: * that the user wants to edit
186: *
187: * @param errorMsg, the error messages
188: * @param disabled, if controls are disabled
189: * @return html document
190: * @exception HttpPresentationException
191: */
192: public XMLObject showModifyPage(String errorMsg, boolean disabled)
193: throws HttpPresentationException,
194: ProjectManagementPresentationException {
195: String companyName = this .getComms().request
196: .getParameter(COMPANY_NAME);
197: String contactName = this .getComms().request
198: .getParameter(CONTACT_NAME);
199: String contactTitle = this .getComms().request
200: .getParameter(CONTACT_TITLE);
201: String address = this .getComms().request.getParameter(ADDRESS);
202: String city = this .getComms().request.getParameter(CITY);
203: String region = this .getComms().request.getParameter(REGION);
204: String postalCode = this .getComms().request
205: .getParameter(POSTAL_CODE);
206: String country = this .getComms().request.getParameter(COUNTRY);
207: String phone = this .getComms().request.getParameter(PHONE);
208: String fax = this .getComms().request.getParameter(FAX);
209: String email = this .getComms().request.getParameter(EMAIL);
210: String notes = this .getComms().request.getParameter(NOTES);
211:
212: String customerID = this .getComms().request
213: .getParameter(CUSTOMER_ID);
214: // Instantiate the page object
215: EditHTML page = new EditHTML();
216:
217: Customer customer = null;
218:
219: try {
220: CustomerManager customerManager = CustomerManagerFactory
221: .getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl");
222: customer = customerManager.findCustomerByID(customerID);
223: /*
224: * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run ProjectManagement_pres )
225: * We need to allow ProjectManagement_pres to be functional , response
226: * will be default HTML page with message
227: */
228:
229: } catch (NullPointerException ex) {
230: page.setTextErrorText("This is a default HTML page");
231: return page;
232: // Catch any possible database exception in findCustomerByID()
233: } catch (ProjectManagementException ex) {
234: this .getSessionData().setUserMessage(
235: "Please choose a valid customer to edit");
236: throw new ClientPageRedirectException(CONTEXT_PAGE);
237: }
238:
239: try {
240: // If we received a valid customerID then try to show the customer's contents,
241: // otherwise try to use the HTML input parameters
242: page.getElementCustomerID().setValue(customer.getHandle());
243:
244: HTMLInputElement el = page.getElementTxtCompanyName();
245: el.setDisabled(disabled);
246: if (null != companyName && companyName.length() != 0) {
247: el.setValue(companyName);
248: } else {
249: el.setValue(customer.getCompanyName());
250: }
251:
252: el = page.getElementTxtContactName();
253: el.setDisabled(disabled);
254: if (null != contactName && contactName.length() != 0) {
255: el.setValue(contactName);
256: } else {
257: el.setValue(customer.getContactName());
258: }
259:
260: el = page.getElementTxtContactTitle();
261: el.setDisabled(disabled);
262: if (null != contactTitle && contactTitle.length() != 0) {
263: el.setValue(contactTitle);
264: } else {
265: el.setValue(customer.getContactTitle());
266: }
267:
268: el = page.getElementTxtAddress();
269: el.setDisabled(disabled);
270: if (null != address && address.length() != 0) {
271: el.setValue(address);
272: } else {
273: el.setValue(customer.getAddress());
274: }
275:
276: el = page.getElementTxtCity();
277: el.setDisabled(disabled);
278: if (null != city && city.length() != 0) {
279: el.setValue(city);
280: } else {
281: el.setValue(customer.getCity());
282: }
283:
284: el = page.getElementTxtRegion();
285: el.setDisabled(disabled);
286: if (null != region && region.length() != 0) {
287: el.setValue(region);
288: } else {
289: el.setValue(customer.getRegion());
290: }
291:
292: el = page.getElementTxtPostalCode();
293: el.setDisabled(disabled);
294: if (null != postalCode && postalCode.length() != 0) {
295: el.setValue(postalCode);
296: } else {
297: el.setValue(customer.getPostalCode());
298: }
299:
300: el = page.getElementTxtCountry();
301: el.setDisabled(disabled);
302: if (null != country && country.length() != 0) {
303: el.setValue(country);
304: } else {
305: el.setValue(customer.getCountry());
306: }
307:
308: el = page.getElementTxtPhone();
309: el.setDisabled(disabled);
310: if (null != phone && phone.length() != 0) {
311: el.setValue(phone);
312: } else {
313: el.setValue(customer.getPhone());
314: }
315:
316: el = page.getElementTxtFax();
317: el.setDisabled(disabled);
318: if (null != fax && fax.length() != 0) {
319: el.setValue(fax);
320: } else {
321: el.setValue(customer.getFax());
322: }
323:
324: el = page.getElementTxtEmail();
325: el.setDisabled(disabled);
326: if (null != email && email.length() != 0) {
327: el.setValue(email);
328: } else {
329: el.setValue(customer.getEmail());
330: }
331:
332: //HTMLTextAreaElement tael=page.getElementTxtNotes();
333: el = page.getElementTxtNotes();
334: el.setDisabled(disabled);
335: if (null != notes && notes.length() != 0) {
336: el.setValue(notes);
337: } else {
338: el.setValue(customer.getNotes());
339: }
340:
341: el = page.getElementBtnSave();
342: el.setDisabled(disabled);
343:
344: if (null == errorMsg) {
345: page.getElementErrorText().getParentNode().removeChild(
346: page.getElementErrorText());
347: } else {
348: page.setTextErrorText(errorMsg);
349: }
350: } catch (ProjectManagementException ex) {
351: throw new ProjectManagementPresentationException(
352: "Error populating page for customer editing", ex);
353: }
354:
355: page.getElementEvent().setValue(MODIFY);
356: return page;
357: }
358:
359: /**
360: * Produce HTML for this PO
361: *
362: * @param errorMsg, the error messages
363: * @return html document
364: * @exception HttpPresentationException
365: */
366: public XMLObject showAddPage(String errorMsg)
367: throws HttpPresentationException,
368: ProjectManagementPresentationException {
369:
370: String companyName = this .getComms().request
371: .getParameter(COMPANY_NAME);
372: String contactName = this .getComms().request
373: .getParameter(CONTACT_NAME);
374: String contactTitle = this .getComms().request
375: .getParameter(CONTACT_TITLE);
376: String address = this .getComms().request.getParameter(ADDRESS);
377: String city = this .getComms().request.getParameter(CITY);
378: String region = this .getComms().request.getParameter(REGION);
379: String postalCode = this .getComms().request
380: .getParameter(POSTAL_CODE);
381: String country = this .getComms().request.getParameter(COUNTRY);
382: String phone = this .getComms().request.getParameter(PHONE);
383: String fax = this .getComms().request.getParameter(FAX);
384: String email = this .getComms().request.getParameter(EMAIL);
385: String notes = this .getComms().request.getParameter(NOTES);
386:
387: // Instantiate the page object
388: EditHTML page = new EditHTML();
389:
390: HTMLInputElement el = page.getElementTxtCompanyName();
391: if (null != companyName) {
392: el.setValue(companyName);
393: } else {
394: el.setValue("");
395: }
396:
397: el = page.getElementTxtContactName();
398: if (null != contactName) {
399: el.setValue(contactName);
400: } else {
401: el.setValue("");
402: }
403:
404: el = page.getElementTxtContactTitle();
405: if (null != contactTitle) {
406: el.setValue(contactTitle);
407: } else {
408: el.setValue("");
409: }
410:
411: el = page.getElementTxtAddress();
412: if (null != address) {
413: el.setValue(address);
414: } else {
415: el.setValue("");
416: }
417:
418: el = page.getElementTxtCity();
419: if (null != city) {
420: el.setValue(city);
421: } else {
422: el.setValue("");
423: }
424:
425: el = page.getElementTxtRegion();
426: if (null != region) {
427: el.setValue(region);
428: } else {
429: el.setValue("");
430: }
431:
432: el = page.getElementTxtPostalCode();
433: if (null != postalCode) {
434: el.setValue(postalCode);
435: } else {
436: el.setValue("");
437: }
438:
439: el = page.getElementTxtCountry();
440: if (null != country) {
441: el.setValue(country);
442: } else {
443: el.setValue("");
444: }
445:
446: el = page.getElementTxtPhone();
447: if (null != phone) {
448: el.setValue(phone);
449: } else {
450: el.setValue("");
451: }
452:
453: el = page.getElementTxtFax();
454: if (null != fax) {
455: el.setValue(fax);
456: } else {
457: el.setValue("");
458: }
459:
460: el = page.getElementTxtEmail();
461: if (null != email) {
462: el.setValue(email);
463: } else {
464: el.setValue("");
465: }
466:
467: //HTMLTextAreaElement tael=page.getElementTxtNotes();
468: el = page.getElementTxtNotes();
469: if (null != notes) {
470: el.setValue(notes);
471: } else {
472: el.setValue("");
473: }
474:
475: if (null == errorMsg) {
476: page.getElementErrorText().getParentNode().removeChild(
477: page.getElementErrorText());
478: } else {
479: page.setTextErrorText(errorMsg);
480: }
481:
482: return page;
483: }
484:
485: /**
486: * Method to save a new or existing customer to the database
487: *
488: * @param customer, the customer to be saved
489: * @return html document
490: * @exception HttpPresentationException
491: */
492: protected void saveCustomer(Customer theCustomer)
493: throws HttpPresentationException {
494: String companyName = this .getComms().request
495: .getParameter(COMPANY_NAME);
496: String contactName = this .getComms().request
497: .getParameter(CONTACT_NAME);
498: String contactTitle = this .getComms().request
499: .getParameter(CONTACT_TITLE);
500: String address = this .getComms().request.getParameter(ADDRESS);
501: String city = this .getComms().request.getParameter(CITY);
502: String region = this .getComms().request.getParameter(REGION);
503: String postalCode = this .getComms().request
504: .getParameter(POSTAL_CODE);
505: String country = this .getComms().request.getParameter(COUNTRY);
506: String phone = this .getComms().request.getParameter(PHONE);
507: String fax = this .getComms().request.getParameter(FAX);
508: String email = this .getComms().request.getParameter(EMAIL);
509: String notes = this .getComms().request.getParameter(NOTES);
510:
511: if (isNullField(companyName) || isNullField(contactName)) {
512: throw new ProjectManagementPresentationException(
513: "Error adding customer", new Exception());
514: }
515:
516: try {
517: theCustomer.setCompanyName(companyName);
518: theCustomer.setContactName(contactName);
519: theCustomer.setContactTitle(contactTitle);
520: theCustomer.setAddress(address);
521: theCustomer.setCity(city);
522: theCustomer.setRegion(region);
523: theCustomer.setPostalCode(postalCode);
524: theCustomer.setCountry(country);
525: theCustomer.setPhone(phone);
526: theCustomer.setFax(fax);
527: theCustomer.setEmail(email);
528: theCustomer.setNotes(notes);
529:
530: theCustomer.save();
531: } catch (Exception ex) {
532: throw new ProjectManagementPresentationException(
533: "Error adding customer", ex);
534: }
535: }
536:
537: }
|