Java tutorial
/******************************************************************************* * Copyright (c) 2009 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.xml.writer; import com.sfs.Formatter; import com.sfs.beans.ObjectTypeBean; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.ItemBean; import com.sfs.whichdoctor.beans.MembershipBean; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.formatter.OutputFormatter; import com.sfs.whichdoctor.xml.writer.helper.AccreditationXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.AddressXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.EmailXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.ExamXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.HistoryXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.MemoXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.PhoneXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.ProjectXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.QualificationXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.SpecialtyXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.SupervisorXmlHelper; import com.sfs.whichdoctor.xml.writer.helper.WorkshopXmlHelper; import java.util.Collection; import java.util.TreeMap; import org.apache.commons.lang.StringUtils; /** * The Class PersonXmlWriter. */ public class PersonXmlWriter { /** * Instantiates a new person xml writer. */ protected PersonXmlWriter() { throw new UnsupportedOperationException(); } /** * Output the list of people as an XML string. * * @param people the people * @param preferences the preferences * @param user the user * * @return the string */ public static String save(final Collection<PersonBean> people, final PreferencesBean preferences, final UserBean user) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("whichdoctor").writeAttribute("version", preferences.getOption("system", "version")); xmlwriter.writeXml(getPeopleXml(people, preferences)); xmlwriter.writeXml(PreferencesXmlWriter.save(preferences, user, false)); xmlwriter.endEntity(); return xmlwriter.getXml(); } /** * Output the list of people as an XML string. * * @param people the people * @param preferences the preferences * @return the xml string */ public static String getPeopleXml(final Collection<PersonBean> people, final PreferencesBean preferences) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("members"); for (PersonBean person : people) { xmlwriter.writeXml(getPersonXml(person, preferences)); } xmlwriter.endEntity(); return xmlwriter.getXml(); } /** * Output the person as an XML string. * * @param person the person * @param preferences the preferences * @return the xml string */ public static String getPersonXml(final PersonBean person, final PreferencesBean preferences) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("member").writeAttribute("GUID", person.getGUID()).writeAttribute("personId", person.getId()); // Add the person details xmlwriter.writeXml(getDetailsXml(person, false)); // Add the history xmlwriter.writeXml(HistoryXmlHelper.getHistoryXml(person)); // Add the training section xmlwriter.writeXml(getTrainingXml(person, preferences)); // Process workshops if (person.getWorkshops() != null) { xmlwriter.writeXml(WorkshopXmlHelper.getWorkshopsXml(person.getWorkshops())); } // Process currently supervised if (person.getCurrentlySupervising() != null && person.getCurrentlySupervising().size() > 0) { xmlwriter.writeXml(SupervisorXmlHelper.getSupervisedXml(person.getCurrentlySupervising(), preferences)); } // Process specialties if (person.getSpecialtyList() != null) { xmlwriter.writeXml(SpecialtyXmlHelper.getSpecialtiesXml(person.getSpecialtyList())); } // Process qualifications if (person.getQualifications() != null) { xmlwriter.writeXml(QualificationXmlHelper.getQualificationsXml(person.getQualifications())); } // Process memos if (person.getMemo() != null) { xmlwriter.writeXml(MemoXmlHelper.getMemosXml(person.getMemo())); } // Display employment history if (person.getEmployers() != null) { xmlwriter.writeXml(getEmployersXml(person.getEmployers())); } // Process financial summary if (person.getFinancialSummary() != null) { xmlwriter.writeXml(FinancialSummaryXmlWriter.getFinancialSummaryXml(person.getFinancialSummary())); } xmlwriter.endEntity(); return xmlwriter.getXml(); } /** * Output the person details as an XML string. * * @param person the person * @return the xml string */ public static String getDetailsXml(final PersonBean person) { return getDetailsXml(person, true); } /** * Output the person details as an XML string. * * @param person the person * @param includeWrapper the include wrapper * @return the xml string */ public static String getDetailsXml(final PersonBean person, final boolean includeWrapper) { final XmlWriter xmlwriter = new XmlWriter(); if (person != null) { if (includeWrapper) { xmlwriter.writeEntity("member").writeAttribute("GUID", person.getGUID()).writeAttribute("personId", person.getId()); } xmlwriter.writeXml(getPersonDetailsXml(person)); if (includeWrapper) { xmlwriter.endEntity(); } } return xmlwriter.getXml(); } /** * Output the person details as an XML string. * * @param person the person * @return the xml string */ private static String getPersonDetailsXml(final PersonBean person) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("MIN").writeText(person.getPersonIdentifier()).endEntity(); if (StringUtils.isNotBlank(person.getGender())) { xmlwriter.writeEntity("gender").writeText(person.getGender()).endEntity(); } xmlwriter.writeEntity("name"); xmlwriter.writeEntity("title").writeText(person.getTitle()).endEntity(); xmlwriter.writeEntity("firstName").writeText(person.getFirstName()).endEntity(); xmlwriter.writeEntity("middleName").writeText(person.getMiddleName()).endEntity(); xmlwriter.writeEntity("lastName").writeText(person.getLastName()).endEntity(); xmlwriter.writeEntity("preferredName").writeText(person.getPreferredName()).endEntity(); xmlwriter.endEntity(); xmlwriter.writeEntity("formalName").writeText(OutputFormatter.toMemberName(person)).endEntity(); xmlwriter.writeEntity("formattedName").writeText(OutputFormatter.toContactName(person)).endEntity(); if (StringUtils.isNotBlank(person.getAge())) { xmlwriter.writeEntity("age").writeText(person.getAge()).endEntity(); } if (person.getBirthDate() != null) { xmlwriter.writeEntity("birthDate").writeText(Formatter.convertDate(person.getBirthDate())).endEntity(); } if (person.getDeceasedDate() != null) { xmlwriter.writeEntity("deceasedDate").writeText(Formatter.convertDate(person.getDeceasedDate())) .endEntity(); } if (person.getMembershipDetails() != null) { for (MembershipBean membership : person.getMembershipDetails()) { xmlwriter.writeXml(getMembershipXml(membership)); } } if (person.getWorksOverseas()) { xmlwriter.writeEntity("worksOverseas").writeText("Yes").endEntity(); } else { xmlwriter.writeEntity("worksOverseas").writeText("No").endEntity(); } if (StringUtils.isNotBlank(person.getRegion())) { xmlwriter.writeEntity("region").writeText(person.getRegion()).endEntity(); } // Process addresses if (person.getAddress().size() > 0) { xmlwriter.writeXml(AddressXmlHelper.getAddressesXml(person.getAddress())); } // Process phones if (person.getPhone().size() > 0) { xmlwriter.writeXml(PhoneXmlHelper.getPhonesXml(person.getPhone())); } // Process emails if (person.getEmail().size() > 0) { xmlwriter.writeXml(EmailXmlHelper.getEmailsXml(person.getEmail())); } if (StringUtils.isNotBlank(person.getSeeAlso())) { xmlwriter.writeEntity("seeAlso").writeText(person.getSeeAlso()).endEntity(); } if (StringUtils.isNotBlank(person.getSummaryTotal())) { xmlwriter.writeEntity("summaryTotal").writeText(person.getSummaryTotal()).endEntity(); } return xmlwriter.getXml(); } /** * Gets the membership xml string. * * @param membership the membership * * @return the membership xml string */ private static String getMembershipXml(final MembershipBean membership) { final XmlWriter xmlwriter = new XmlWriter(); // TODO Have separate membership entities rather than just // having everything as one big clump. if (membership.getJoinedDate() != null) { xmlwriter.writeEntity("registeredDate").writeText(Formatter.convertDate(membership.getJoinedDate())) .endEntity(); } if (membership.getLeftDate() != null) { xmlwriter.writeEntity("retiredDate").writeText(Formatter.convertDate(membership.getLeftDate())) .endEntity(); } if (membership.getIntField("Candidate Number") > 0) { xmlwriter.writeEntity("candidateNumber").writeText(membership.getField("Candidate Number")).endEntity(); } if (membership.getField("Status").compareTo("") != 0) { xmlwriter.writeEntity("status").writeText(membership.getField("Status")).endEntity(); } if (membership.getObjectTypeField("Membership Type") != null) { ObjectTypeBean object = membership.getObjectTypeField("Membership Type"); xmlwriter.writeEntity("membership").writeText(object.getName()).endEntity(); } if (membership.getField("Division").compareTo("") != 0) { xmlwriter.writeEntity("division").writeText(membership.getField("Division")).endEntity(); } if (membership.getField("Region Representative").compareTo("") != 0) { xmlwriter.writeEntity("regionRepresentative").writeText(membership.getField("Region Representative")) .endEntity(); } final String fne = membership.getField("Financial Excemption"); if (StringUtils.isNotBlank(fne)) { xmlwriter.writeEntity("financialExcemption").writeText(fne).endEntity(); } if (membership.getIntField("FRACP") > 0) { xmlwriter.writeEntity("FRACP").writeText(membership.getField("FRACP")).endEntity(); } if (membership.getDateField("Fellowship Date") != null) { xmlwriter.writeEntity("fellowshipDate") .writeText(Formatter.convertDate(membership.getDateField("Fellowship Date"))).endEntity(); } if (membership.getField("Admitting SAC").compareTo("") != 0) { xmlwriter.writeEntity("admittingSAC").writeText(membership.getField("Admitting SAC")).endEntity(); } if (membership.getField("Admitting Country").compareTo("") != 0) { xmlwriter.writeEntity("admittingCountry").writeText(membership.getField("Admitting Country")) .endEntity(); } return xmlwriter.getXml(); } /** * Gets the training xml. * * @param person the person * @param preferences the preferences * @return the training xml */ private static String getTrainingXml(final PersonBean person, final PreferencesBean preferences) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("training"); if (person.getMembershipField("Training Type").compareTo("") != 0) { xmlwriter.writeAttribute("type", person.getMembershipField("Training Type")); } if (person.getMembershipField("Supervisor Status").compareTo("") != 0) { xmlwriter.writeEntity("supervisorStatus").writeText(person.getMembershipField("Supervisor Status")) .endEntity(); } if (person.getMentors() != null) { xmlwriter.writeXml(getMentorsXml(person.getMentors())); } xmlwriter.writeEntity("curriculumYear").writeText(person.getCurriculumYear()).endEntity(); if (person.getExams() != null) { xmlwriter.writeXml(ExamXmlHelper.getExamsXml(person.getExams())); if (person.getExamDeadline() != null) { xmlwriter.writeEntity("examDeadline").writeText(Formatter.convertDate(person.getExamDeadline())) .endEntity(); } } if (person.getProjects() != null) { xmlwriter.writeXml(ProjectXmlHelper.getProjectsXml(person.getProjects())); } for (String type : preferences.getTrainingTypes()) { if (person.getTrainingSummary(type) != null) { xmlwriter.writeXml(AccreditationXmlHelper.getSummaryXml(person.getTrainingSummary(type), type)); } } if (person.getRotations() != null) { xmlwriter.writeXml(RotationXmlWriter.getRotationsXml(person.getRotations(), preferences)); } // Close training element xmlwriter.endEntity(); return xmlwriter.getXml(); } /** * Gets the mentors xml. * * @param mentors the mentors * * @return the mentors xml */ private static String getMentorsXml(final TreeMap<String, ItemBean> mentors) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("mentors"); for (String orderIndex : mentors.keySet()) { ItemBean item = mentors.get(orderIndex); xmlwriter.writeEntity("mentor").writeAttribute("personId", item.getObject2GUID()); xmlwriter.writeEntity("name").writeText(item.getName()).endEntity(); if (item.getStartDate() != null) { xmlwriter.writeEntity("startDate").writeText(Formatter.convertDate(item.getStartDate())) .endEntity(); } if (item.getEndDate() != null) { xmlwriter.writeEntity("endDate").writeText(Formatter.convertDate(item.getEndDate())).endEntity(); } if (item.getTitle() != null) { xmlwriter.writeEntity("description").writeText(item.getTitle()).endEntity(); } xmlwriter.endEntity(); } xmlwriter.endEntity(); return xmlwriter.getXml(); } /** * Gets the employers xml. * * @param employers the employers * * @return the employers xml */ private static String getEmployersXml(final TreeMap<String, ItemBean> employers) { final XmlWriter xmlwriter = new XmlWriter(); xmlwriter.writeEntity("employers"); for (String orderIndex : employers.keySet()) { ItemBean item = employers.get(orderIndex); xmlwriter.writeEntity("employer").writeAttribute("GUID", item.getObject1GUID()); xmlwriter.writeEntity("name").writeText(item.getName()).endEntity(); if (item.getStartDate() != null) { xmlwriter.writeEntity("startDate").writeText(Formatter.convertDate(item.getStartDate())) .endEntity(); } if (item.getEndDate() != null) { xmlwriter.writeEntity("endDate").writeText(Formatter.convertDate(item.getEndDate())).endEntity(); } if (item.getTitle() != null) { xmlwriter.writeEntity("description").writeText(item.getTitle()).endEntity(); } xmlwriter.endEntity(); } xmlwriter.endEntity(); return xmlwriter.getXml(); } }