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 18 public class Edit extends BasePO { 19 20 23 private static String COMPANY_NAME = "txtCompanyName"; 24 private static String CONTACT_NAME = "txtContactName"; 25 private static String CONTACT_TITLE = "txtContactTitle"; 26 private static String ADDRESS = "txtAddress"; 27 private static String CITY = "txtCity"; 28 private static String REGION = "txtRegion"; 29 private static String POSTAL_CODE = "txtPostalCode"; 30 private static String COUNTRY = "txtCountry"; 31 private static String PHONE = "txtPhone"; 32 private static String FAX = "txtFax"; 33 private static String EMAIL = "txtEmail"; 34 private static String NOTES = "txtNotes"; 35 36 private static String CUSTOMER_ID = "customerID"; 37 private static String CONTEXT_PAGE = "Context.po"; 38 private static String ADD = "add"; 39 private static String DELETE = "delete"; 40 private static String MODIFY = "modify"; 41 private static String DETAILS = "showDetailsPage"; 42 private static String EVENT = "event"; 43 44 47 protected int getRequiredAuthLevel() { 48 String event=""; 49 try { 50 event=this.getComms().request.getParameter(EVENT); 51 } catch (Exception ex) {} 52 53 if (event!=null && event.equalsIgnoreCase(DETAILS)) { 54 return 1; 55 } else { 56 return 2; 57 } 58 } 59 60 63 public XMLObject handleDefault() 64 throws HttpPresentationException { 65 return showModifyPage(null,false); 66 } 67 68 74 public XMLObject handleShowAddPage() 75 throws HttpPresentationException { 76 return showAddPage(null); 77 } 78 79 85 public XMLObject handleShowDetailsPage () 86 throws HttpPresentationException { 87 return showModifyPage(null,true); 88 } 89 90 96 public XMLObject handleModify() 97 throws HttpPresentationException { 98 String customerID = this.getComms().request.getParameter(CUSTOMER_ID); 99 Customer customer=null; 100 101 try { 103 104 CustomerManager customerManager = CustomerManagerFactory.getCustomerManager("projectmanagement.business.customer.CustomerManagerImpl"); 105 customer = customerManager.findCustomerByID(customerID); 106 107 } catch(Exception 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 ex) { 116 return showModifyPage("To modify this customer, you must fill out fields properly!",false); 117 } 118 } 119 120 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 139 140 } catch(NullPointerException ex) { 141 return showAddPage("You cannot add customer while runing ProjectManagement_pres"); 142 } catch(Exception ex) { 143 return showAddPage("To add this customer, you must fill out fields properly!"); 144 } 145 } 146 147 153 public XMLObject handleDelete() 154 throws HttpPresentationException, ProjectManagementPresentationException { 155 String 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 companyName = customer.getCompanyName(); 163 customer.delete(); 164 this.getSessionData().setUserMessage("The customer, " + companyName + ", was deleted"); 165 } catch(Exception ex) { 168 this.getSessionData().setUserMessage("Please choose a valid customer to delete"); 169 } 170 throw new ClientPageRedirectException(CONTEXT_PAGE); 173 } 174 175 184 public XMLObject showModifyPage(String errorMsg,boolean disabled) 185 throws HttpPresentationException, ProjectManagementPresentationException { 186 String companyName = this.getComms().request.getParameter(COMPANY_NAME); 187 String contactName = this.getComms().request.getParameter(CONTACT_NAME); 188 String contactTitle = this.getComms().request.getParameter(CONTACT_TITLE); 189 String address = this.getComms().request.getParameter(ADDRESS); 190 String city = this.getComms().request.getParameter(CITY); 191 String region = this.getComms().request.getParameter(REGION); 192 String postalCode = this.getComms().request.getParameter(POSTAL_CODE); 193 String country = this.getComms().request.getParameter(COUNTRY); 194 String phone = this.getComms().request.getParameter(PHONE); 195 String fax = this.getComms().request.getParameter(FAX); 196 String email = this.getComms().request.getParameter(EMAIL); 197 String notes = this.getComms().request.getParameter(NOTES); 198 199 String customerID = this.getComms().request.getParameter(CUSTOMER_ID); 200 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 213 214 } catch(NullPointerException ex) { 215 page.setTextErrorText("This is a default HTML page"); 216 return page; 217 } 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 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 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 348 public XMLObject showAddPage(String errorMsg) 349 throws HttpPresentationException, ProjectManagementPresentationException { 350 351 String companyName = this.getComms().request.getParameter(COMPANY_NAME); 352 String contactName = this.getComms().request.getParameter(CONTACT_NAME); 353 String contactTitle = this.getComms().request.getParameter(CONTACT_TITLE); 354 String address = this.getComms().request.getParameter(ADDRESS); 355 String city = this.getComms().request.getParameter(CITY); 356 String region = this.getComms().request.getParameter(REGION); 357 String postalCode = this.getComms().request.getParameter(POSTAL_CODE); 358 String country = this.getComms().request.getParameter(COUNTRY); 359 String phone = this.getComms().request.getParameter(PHONE); 360 String fax = this.getComms().request.getParameter(FAX); 361 String email = this.getComms().request.getParameter(EMAIL); 362 String notes = this.getComms().request.getParameter(NOTES); 363 364 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 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 468 protected void saveCustomer(Customer theCustomer) 469 throws HttpPresentationException { 470 String companyName = this.getComms().request.getParameter(COMPANY_NAME); 471 String contactName = this.getComms().request.getParameter(CONTACT_NAME); 472 String contactTitle = this.getComms().request.getParameter(CONTACT_TITLE); 473 String address = this.getComms().request.getParameter(ADDRESS); 474 String city = this.getComms().request.getParameter(CITY); 475 String region = this.getComms().request.getParameter(REGION); 476 String postalCode = this.getComms().request.getParameter(POSTAL_CODE); 477 String country = this.getComms().request.getParameter(COUNTRY); 478 String phone = this.getComms().request.getParameter(PHONE); 479 String fax = this.getComms().request.getParameter(FAX); 480 String email = this.getComms().request.getParameter(EMAIL); 481 String notes = this.getComms().request.getParameter(NOTES); 482 483 if (isNullField(companyName) || isNullField(contactName)) { 484 throw new ProjectManagementPresentationException("Error adding customer",new Exception ()); 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 ex) { 503 throw new ProjectManagementPresentationException("Error adding customer", ex); 504 } 505 } 506 507 } 508 509 | Popular Tags |