/** * Copyright (c) 2001, Keith Wong * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software * is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package mypackage; import objectivehtml.common.*; import objectivehtml.htmlwidget.*; import objectivehtml.htmlwidget.exception.*; import objectivehtml.oms.exception.*; import java.util.*; import java.text.*; import javax.servlet.http.*; /** * CustomerForm * This is an example of how to use the objective html toolkit. Please read all the comments * in the code as they give handy tips in how to be use this toolkit. * The example was written by Keith Wong, email: keithwong@optushome.com.au */ public class CustomerForm extends CustomerFormDesign { /** Indicates whether the form was successfully processed or an error occurred */ private Throwable m_objThrowable = null; private Customer m_objCustomer; // keeps a record of the current customer /** * Constructor */ public CustomerForm() throws Exception { super(); } // end constructor /** * This method validates the form details first. * @return true if the form data is ok, false otherwise */ private boolean validateForm() { boolean bValid = true; // if title is blank if (m_selTitle.isSelected(0) == true) { bValid = false; // sets the class attribute of the table cell with "Title *" text m_tblLayout.getTableCell(0, 0).setClassAttribute("error"); } // end if title is blank // if first name is blank if (m_txtFirstName.getText().trim().length() == 0) { bValid = false; m_tblLayout.getTableCell(1, 0).setClassAttribute("error"); } // end if first name is blank // if last name is blank if (m_txtLastName.getText().trim().length() == 0) { bValid = false; m_tblLayout.getTableCell(2, 0).setClassAttribute("error"); } // end if last name is blank // if email is blank if (m_txtEmail.getText().trim().length() == 0) { bValid = false; m_tblLayout.getTableCell(11, 0).setClassAttribute("error"); } // end if email is blank // if invalid then lets set a user message if (bValid == false) { m_htmUserMessage.setText("Please fill in the mandatory fields highlighted below."); m_parUserMessage.setVisible(true); m_parUserMessage.setClassAttribute("error"); } // end if not valid // lets check the date is correct formatted if (bValid) { SimpleDateFormat objFormatter = new SimpleDateFormat("yyyy-MM-dd"); objFormatter.setLenient(false); try { if (m_txtBirthDate.getText() != null && m_txtBirthDate.getText().trim().length() > 0) { Date objBirthDate = objFormatter.parse(m_txtBirthDate.getText().trim()); } // end if birth date is set } // end try catch (ParseException pe) { bValid = false; m_htmUserMessage.setText("The date of birth must in the format yyyy-mm-dd, e.g. 1977-03-29"); m_parUserMessage.setVisible(true); m_parUserMessage.setClassAttribute("error"); } // end pe } // end if still valid return bValid; } // end validateForm /** * This updates the postcode fields with the default values if applicable. */ public void updatePostCodeFields(String sNewPostCode) { m_tblLayout.setAttribute("class", null, true); // apply to all children m_tblLayout.setAttribute("class", "normal"); // only applies to the table itself // lets try to set the default for the other fields PostCode objPostCode = (PostCode)PostCodeDomain.retrievePostCodeDetails(sNewPostCode); if (objPostCode != null) { m_txtSuburb.setText(objPostCode.getSuburb()); m_txtState.setText(objPostCode.getState()); } // end if postcode exists } // end updatePostCodeFields /** * This loads the form from the database. */ public void populateForm(long lCustomerId) { m_objCustomer = CustomerDomain.retrieveCustomerDetails(lCustomerId); m_selTitle.setSelected(m_objCustomer.getTitle(), true); m_txtFirstName.setText(m_objCustomer.getFirstName()); m_txtLastName.setText(m_objCustomer.getLastName()); SimpleDateFormat objFormatter = new SimpleDateFormat("yyyy-MM-dd"); if (m_objCustomer.getBirthDate() != null) m_txtBirthDate.setText(objFormatter.format(m_objCustomer.getBirthDate())); if (m_objCustomer.getGender() != null) { m_radMale.setChecked(m_objCustomer.getGender().equals("M") ? true : false); m_radFemale.setChecked(m_objCustomer.getGender().equals("F") ? true : false); } // end if gender exists m_txaStreet.setText(m_objCustomer.getStreet()); m_txtPostCode.setText(m_objCustomer.getPostCode()); m_txtSuburb.setText(m_objCustomer.getSuburb()); m_txtState.setText(m_objCustomer.getState()); m_cbxPostalAddress.setChecked(m_objCustomer.getPostalAddress()); m_txtTelephone.setText(m_objCustomer.getTelephone()); m_txtEmail.setText(m_objCustomer.getEmail()); } // end populateForm /** * This method should be called when the save action is triggered. */ public void saveForm() throws InvalidAttributeException { // lets clear out all class styles set for errors // you can do this by calling the overloaded version of setAttribute() // provided by all container widgets // n.b giving the setAttribute a null value will clear out the attribute, // so that the attribute is not shown at all when printHtml is called m_tblLayout.setAttribute("class", null, true); // apply to all children // lets set the attribute back to the normal m_tblLayout.setAttribute("class", "normal"); // only applies to the table itself if (validateForm()) { // save customer details saveFormToCustomer(); CustomerDomain.saveCustomerDetails(m_objCustomer); m_htmUserMessage.setText("Your customer details have been saved."); m_parUserMessage.setVisible(true); m_parUserMessage.setClassAttribute("info"); } // end if form is valid } // end saveForm /** * This method saves the form details to the customer object */ protected void saveFormToCustomer() { // lets grab all the fields from the form and set it into the customer object m_objCustomer.setTitle(m_selTitle.getSelectedValue()); m_objCustomer.setFirstName(m_txtFirstName.getText()); m_objCustomer.setLastName(m_txtLastName.getText()); SimpleDateFormat objFormatter = new SimpleDateFormat("yyyy-MM-dd"); try { if (m_txtBirthDate.getText() != null && m_txtBirthDate.getText().trim().length() > 0) m_objCustomer.setBirthDate(new java.sql.Date(objFormatter.parse(m_txtBirthDate.getText().trim()).getTime())); else m_objCustomer.setBirthDate(null); } // end try catch (ParseException pe) { pe.printStackTrace(); } // end pe if (m_radMale.isChecked()) m_objCustomer.setGender("M"); else if (m_radFemale.isChecked()) m_objCustomer.setGender("F"); m_objCustomer.setStreet(m_txaStreet.getText()); m_objCustomer.setPostCode(m_txtPostCode.getText()); m_objCustomer.setSuburb(m_txtSuburb.getText()); m_objCustomer.setState(m_txtState.getText()); m_objCustomer.setPostalAddress(m_cbxPostalAddress.isChecked()); m_objCustomer.setTelephone(m_txtTelephone.getText()); m_objCustomer.setEmail(m_txtEmail.getText()); } // end saveFormToCustomer /** * This method clears any stored exceptions. */ public void clearException() { m_objThrowable = null; } // end clearException /** * This method should be called after a business method (or slot method) * is called to check whether an exception occurred. */ public void checkException(HttpServletRequest objRequest) { if (objRequest.getAttribute("ohtml.result") != null) { EventResult objResult = (EventResult)objRequest.getAttribute("ohtml.result"); if (objResult.getResult() == null || objResult.getResult().equals("success")) { if (m_objThrowable != null) { objResult.setResult("failure"); objResult.setAttribute("error", m_objThrowable); } // end if something failed else { objResult.setResult("success"); } // end else everything ok } // end if not failure // lets clear the exception as we've processed this exception clearException(); } // end if result exists } // end checkException } // end class