Java tutorial
/******************************************************************************* * Copyright (c) 2010 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.whichdoctor.beans; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import org.apache.commons.lang.StringUtils; import org.jdom.Document; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; import com.sfs.DataFilter; /** * The Class OnlineApplicationBean. * * @author David Harrison 24th March 2008 */ public class OnlineApplicationBean extends WhichDoctorBean { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The applicationXml. */ private String applicationXml; /** The key. */ private String key; /** The first name. */ private String firstName; /** The last name. */ private String lastName; /** The person guid. */ private int personGUID; /** The status. */ private String status; /** The recordNumber. */ private String recordNumber; /** The type. */ private String type; /** The processed. */ private boolean processed; /** The existing record. */ private PersonBean existingRecord; /** * Try parsing the XML document and setting the relevant online application * attributes e.g. key, content * * @param xmlApplicationVal the xml payload * * @throws JDOMException the JDOM exception */ public final void parseXmlApplication(final String xmlApplicationVal) throws JDOMException { // Ensure there are no "& " characters String xmlApplication = StringUtils.replace(xmlApplicationVal, ">", "|gt;"); xmlApplication = StringUtils.replace(xmlApplication, "<", "|lt;"); xmlApplication = StringUtils.replace(xmlApplication, "&", "|amp;"); // Replace the & characters left xmlApplication = StringUtils.replace(xmlApplication, "&", "&"); // Bring the special characters back xmlApplication = StringUtils.replace(xmlApplication, "|gt;", ">"); xmlApplication = StringUtils.replace(xmlApplication, "|lt;", "<"); xmlApplication = StringUtils.replace(xmlApplication, "|amp;", "&"); if (xmlApplication.startsWith("<?xml ")) { xmlApplication = StringUtils.replace(xmlApplication, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""); } // Parse the online application XML document to get the identifier final SAXBuilder saxBuilder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); final Reader stringReader = new StringReader(xmlApplication); Document applicationDocument = null; try { applicationDocument = saxBuilder.build(stringReader); } catch (JDOMException e) { // Error parsing the XML document throw new JDOMException( "Error parsing online application XML: " + e.getMessage() + "\n" + xmlApplication); } catch (IOException e) { // Error parsing the XML document throw new JDOMException("Error reading online application XML string: " + e.getMessage()); } if (applicationDocument != null) { // Get the key for this application try { this.setKey(applicationDocument.getRootElement().getChild("applicationkey").getValue()); } catch (NullPointerException npe) { throw new JDOMException("The applicationkey element does not exist"); } // Get the first name for this application try { this.setFirstName(applicationDocument.getRootElement().getChild("personaldetails") .getChild("firstname").getValue()); } catch (NullPointerException npe) { throw new JDOMException("The firstname element does not exist"); } // Get the last name for this application try { this.setLastName(applicationDocument.getRootElement().getChild("personaldetails") .getChild("lastname").getValue()); } catch (NullPointerException npe) { throw new JDOMException("The lastname element does not exist"); } XMLOutputter outputter = new XMLOutputter(); outputter.setFormat(Format.getPrettyFormat()); this.applicationXml = outputter.outputString(applicationDocument); } else { throw new JDOMException("The parsed application XML document was null"); } } /** * Sets the application XML. * * @param applicationXmlVal the new application XML */ public final void setApplicationXml(final String applicationXmlVal) { this.applicationXml = applicationXmlVal; } /** * Gets the application xml. * * @return the application xml */ public final String getApplicationXml() { if (this.applicationXml == null) { this.applicationXml = ""; } return this.applicationXml; } /** * Sets the application key. * * @param keyVal the new application key */ public final void setKey(final String keyVal) { this.key = keyVal; } /** * Gets the application key. * * @return the application key */ public final String getKey() { if (this.key == null) { this.key = ""; } return this.key; } /** * Sets the applicant's first name. * * @param firstNameVal the applicant's first name */ public final void setFirstName(final String firstNameVal) { this.firstName = firstNameVal; } /** * Gets the applicant's first name. * * @return the applicant's first name */ public final String getFirstName() { if (this.firstName == null) { this.firstName = ""; } return this.firstName; } /** * Sets the applicant's last name. * * @param lastNameVal the applicant's last name */ public final void setLastName(final String lastNameVal) { this.lastName = lastNameVal; } /** * Gets the applicant's last name. * * @return the applicant's last name */ public final String getLastName() { if (this.lastName == null) { this.lastName = ""; } return this.lastName; } /** * Sets the applicant's GUID. * * @param personGUIDVal the applicant's GUID */ public final void setPersonGUID(final int personGUIDVal) { this.personGUID = personGUIDVal; } /** * Gets the applicant's GUID. * * @return the applicant's GUID */ public final int getPersonGUID() { return this.personGUID; } /** * Sets the status. * * @param statusVal the new action */ public final void setStatus(final String statusVal) { this.status = statusVal; } /** * Gets the status. * * @return the status */ public final String getStatus() { if (this.status == null) { this.status = ""; } return this.status; } /** * Sets the record number. * * @param recordNumberVal the new record value */ public final void setRecordNumber(final String recordNumberVal) { this.recordNumber = recordNumberVal; } /** * Gets the record number. * * @return the record number */ public final String getRecordNumber() { if (recordNumber == null) { recordNumber = ""; } return this.recordNumber; } /** * Sets the type. * * @param typeVal the new type */ public final void setType(final String typeVal) { this.type = typeVal; } /** * Gets the type. * * @return the type */ public final String getType() { if (this.type == null) { this.type = ""; } return this.type; } /** * Sets the processed flag. * * @param processedVal the new processed flag */ public final void setProcessed(final boolean processedVal) { this.processed = processedVal; } /** * Gets the processed flag. * * @return the processed */ public final boolean getProcessed() { return this.processed; } /** * Sets the existing record. * * @param existingRecordVal the new existing record */ public final void setExistingRecord(final PersonBean existingRecordVal) { this.existingRecord = existingRecordVal; } /** * Gets the existing record. * * @return the existing record */ public final PersonBean getExistingRecord() { return this.existingRecord; } /** * Gets the tabbed xml payload. * * @return the tabbed xml payload */ public String getFormattedApplicationXml() { final String htmlXml = DataFilter.getHtml(this.getApplicationXml()); final String tabbedHtmlXML = StringUtils.replace(htmlXml, " ", " "); return StringUtils.replace(tabbedHtmlXML, "\n", "<br/>"); } }