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.event; import com.sfs.Formatter; import com.sfs.whichdoctor.beans.ExamBean; import com.sfs.whichdoctor.beans.FinancialSummaryBean; import com.sfs.whichdoctor.beans.MembershipBean; import com.sfs.whichdoctor.beans.MemoBean; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.beans.ProjectBean; import com.sfs.whichdoctor.beans.ReimbursementBean; import com.sfs.whichdoctor.beans.RotationBean; import com.sfs.whichdoctor.beans.SupervisorBean; import com.sfs.whichdoctor.beans.TransactionSummaryBean; import com.sfs.whichdoctor.beans.WorkshopBean; import com.sfs.whichdoctor.formatter.OutputFormatter; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import org.apache.commons.lang.StringUtils; /** * The Class PersonXmlEventImpl. * * @author David Harrison */ public class PersonXmlEventImpl implements PersonXmlEvent { /** The xml event writer. */ private XmlEventWriter xmlEventWriter; /** * Sets the xml event writer. * * @param xmlEventWriterRef the new xml event writer */ public final void setXmlEventWriter(final XmlEventWriter xmlEventWriterRef) { this.xmlEventWriter = xmlEventWriterRef; } /** * Gets the xml events document. * * @param person the person * @param preferences the preferences * * @return the xml events document */ public final String getXmlEventsDocument(final PersonBean person, final PreferencesBean preferences) { return this.xmlEventWriter.getXml(this.getXmlEvents(person, preferences)); } /** * Gets the xml events. * * @param person the person * @param preferences the preferences * * @return the xml events */ public final Collection<XmlEvent> getXmlEvents(final PersonBean person, final PreferencesBean preferences) { Collection<XmlEvent> events = new ArrayList<XmlEvent>(); // Personal details - birthdate, deceased, if (person.getBirthDate() != null) { XmlEvent event = new XmlEvent(); event.setStartDate(person.getBirthDate()); event.setTitle("Born " + Formatter.numericDate(person.getBirthDate())); events.add(event); } if (person.getDeceasedDate() != null) { XmlEvent event = new XmlEvent(); event.setStartDate(person.getDeceasedDate()); event.setTitle("Deceased " + Formatter.numericDate(person.getDeceasedDate())); events.add(event); } // Membership details - registered, fellowship and retired if (person.getPrimaryMembership() != null) { if (person.getPrimaryMembership().getJoinedDate() != null) { Date registered = new Date(person.getPrimaryMembership().getJoinedDate().getTime()); XmlEvent event = new XmlEvent(); event.setStartDate(registered); event.setTitle("Registered " + Formatter.numericDate(registered)); events.add(event); } if (person.getPrimaryMembership().getDateField("Fellowship Date") != null) { final Date fellowship = new Date( person.getPrimaryMembership().getDateField("Fellowship Date").getTime()); XmlEvent event = new XmlEvent(); event.setStartDate(fellowship); event.setTitle("Attained Fellowship " + Formatter.numericDate(fellowship)); events.add(event); } if (person.getPrimaryMembership().getLeftDate() != null) { Date retired = new Date(person.getPrimaryMembership().getLeftDate().getTime()); XmlEvent event = new XmlEvent(); event.setStartDate(retired); event.setTitle("Retired " + Formatter.numericDate(retired)); events.add(event); } } // Examinations if (person.getExams() != null) { for (ExamBean exam : person.getExams()) { if (exam.getDateSat() != null) { events.add(getExamEvent(exam, person, preferences)); } } } // Projects if (person.getProjects() != null) { for (ProjectBean project : person.getProjects()) { if (project.getSubmitted() != null) { events.add(getProjectEvent(project, person, preferences)); } } } // Workshops if (person.getWorkshops() != null) { for (WorkshopBean workshop : person.getWorkshops()) { if (workshop.getWorkshopDate() != null) { XmlEvent event = new XmlEvent(); event.setTitle(workshop.getType() + " Workshop"); event.setStartDate(workshop.getWorkshopDate()); if (StringUtils.isNotBlank(workshop.getMemo())) { event.setDescription(workshop.getMemo()); } event.setUrl(getDetailUrl(person.getGUID(), preferences)); events.add(event); } } } // Training rotations if (person.getRotations() != null) { for (RotationBean rotation : person.getRotations()) { if (rotation.getStartDate() != null) { events.add(getRotationEvent(rotation, preferences)); } } } // Debits, credits and receipts if (person.getFinancialSummary() != null) { FinancialSummaryBean ledger = person.getFinancialSummary(); if (ledger.getTransactions() != null) { for (TransactionSummaryBean transaction : ledger.getTransactions()) { if (transaction.getIssued() != null) { events.add(getTransactionEvent(transaction, preferences)); } } } } // Reimbursements if (person.getReimbursements() != null) { for (ReimbursementBean reimbursement : person.getReimbursements()) { if (reimbursement.getIssued() != null) { events.add(getReimbursementEvent(reimbursement, preferences)); } } } // Memos if (person.getMemo() != null) { for (MemoBean memo : person.getMemo()) { if (memo.getCreatedDate() != null) { XmlEvent event = new XmlEvent(); event.setStartDate(memo.getCreatedDate()); event.setTitle(memo.getType() + " Memo (" + memo.getPriorityString() + ")"); if (StringUtils.isNotBlank(memo.getMessage())) { event.setDescription(memo.getMessage()); } event.setUrl(getDetailUrl(person.getGUID(), preferences)); events.add(event); } } } return events; } /** * Gets the detail url. * * @param guid the guid * @param preferences the preferences * * @return the detail url */ private String getDetailUrl(final int guid, final PreferencesBean preferences) { return preferences.buildUrl("read", "people", "guid", guid); } /** * Gets the training url. * * @param guid the guid * @param preferences the preferences * * @return the training url */ private String getTrainingUrl(final int guid, final PreferencesBean preferences) { return preferences.buildUrl("read", "people", "guid", guid, "Type=Training"); } /** * Gets the financial url. * * @param type the type * @param guid the guid * @param preferences the preferences * * @return the financial url */ private String getFinancialUrl(final String type, final int guid, final PreferencesBean preferences) { String url = ""; if (type != null) { if (type.compareToIgnoreCase("Credit") == 0) { url = preferences.buildUrl("read", "credits", "guid", guid); } if (type.compareToIgnoreCase("Debit") == 0) { url = preferences.buildUrl("read", "debits", "guid", guid); } if (type.compareToIgnoreCase("Receipt") == 0) { url = preferences.buildUrl("read", "receipts", "guid", guid); } } return url; } /** * Gets the reimbursement url. * * @param guid the guid * @param preferences the preferences * * @return the reimbursement url */ private String getReimbursementUrl(final int guid, final PreferencesBean preferences) { return preferences.buildUrl("read", "reimbursements", "guid", guid); } /** * Gets the rotation url. * * @param guid the guid * @param preferences the preferences * * @return the rotation url */ private String getRotationUrl(final int guid, final PreferencesBean preferences) { return preferences.buildUrl("read", "rotations", "guid", guid); } /** * Gets the rotation event. * * @param rotation the rotation * @param preferences the preferences * * @return the rotation event */ private XmlEvent getRotationEvent(final RotationBean rotation, final PreferencesBean preferences) { XmlEvent event = new XmlEvent(); event.setStartDate(rotation.getStartDate()); event.setEndDate(rotation.getEndDate()); event.setTitle(rotation.getRotationType() + ": " + rotation.getDescription()); // Training details StringBuffer trgb = new StringBuffer(); trgb.append(rotation.getTotalMonths()); trgb.append(" months ("); trgb.append(rotation.getTotalDays()); trgb.append(" days @ "); trgb.append(Formatter.toPercent(rotation.getTrainingTime(), 0, "%")); trgb.append(" time)<br/>"); // Build organisation description StringBuffer orgb = new StringBuffer(); if (StringUtils.isNotBlank(rotation.getOrganisation1Name())) { orgb.append(rotation.getOrganisation1Name()); } if (StringUtils.isNotBlank(rotation.getOrganisation2Name())) { if (orgb.length() > 0) { orgb.append(" and "); } orgb.append(rotation.getOrganisation2Name()); } if (orgb.length() > 0) { orgb.insert(0, "Undertaken at "); orgb.append(".<br/>"); } // Build supervisor description StringBuffer supb = new StringBuffer(); if (rotation.getSupervisors() != null) { for (SupervisorBean supervisor : rotation.getSupervisors()) { if (supb.length() > 0) { supb.append(", "); } supb.append(OutputFormatter.toCasualName(supervisor.getPerson())); } } if (supb.length() > 0) { supb.insert(0, "Supervised by "); supb.append("."); } event.setDescription(trgb.toString() + orgb.toString() + supb.toString()); event.setUrl(this.getRotationUrl(rotation.getGUID(), preferences)); return event; } /** * Gets the exam event. * * @param exam the exam * @param person the person * @param preferences the preferences * * @return the exam event */ private XmlEvent getExamEvent(final ExamBean exam, final PersonBean person, final PreferencesBean preferences) { XmlEvent event = new XmlEvent(); String title = exam.getType() + ": " + exam.getStatus(); if (StringUtils.isNotBlank(exam.getStatusLevel())) { title += exam.getStatusLevel(); } event.setTitle(title); event.setStartDate(exam.getDateSat()); event.setDescription(exam.getLocation()); event.setUrl(getTrainingUrl(person.getGUID(), preferences)); return event; } /** * Gets the project event. * * @param project the project * @param person the person * @param preferences the preferences * * @return the project event */ private XmlEvent getProjectEvent(final ProjectBean project, final PersonBean person, final PreferencesBean preferences) { XmlEvent event = new XmlEvent(); final String title = "Project: " + project.getTitle() + " (" + project.getStatus() + ")"; event.setTitle(title); event.setStartDate(project.getSubmitted()); event.setEndDate(project.getResubmitted()); event.setDescription(project.getMemo()); event.setUrl(getTrainingUrl(person.getGUID(), preferences)); return event; } /** * Gets the reimbursement event. * * @param reimbursement the reimbursement * @param preferences the preferences * * @return the reimbursement event */ private XmlEvent getReimbursementEvent(final ReimbursementBean reimbursement, final PreferencesBean preferences) { XmlEvent event = new XmlEvent(); event.setStartDate(reimbursement.getIssued()); event.setTitle("Reimbursement: " + reimbursement.getAbbreviation() + reimbursement.getNumber()); StringBuffer sb = new StringBuffer(); if (StringUtils.isNotBlank(reimbursement.getDescription())) { sb.append("<em>" + reimbursement.getDescription() + "</em><br/>"); } if (StringUtils.isNotBlank(reimbursement.getLocation())) { sb.append("Location: " + reimbursement.getLocation() + "<br/>"); } sb.append("Total value: " + Formatter.toCurrency(reimbursement.getNetValue())); event.setDescription(sb.toString()); event.setUrl(this.getReimbursementUrl(reimbursement.getGUID(), preferences)); return event; } /** * Gets the transaction event. * * @param transaction the transaction * @param preferences the preferences * * @return the transaction event */ private XmlEvent getTransactionEvent(final TransactionSummaryBean transaction, final PreferencesBean preferences) { XmlEvent event = new XmlEvent(); event.setStartDate(transaction.getIssued()); event.setTitle(transaction.getCategory() + ": " + transaction.getAbbreviation() + transaction.getNumber()); StringBuffer sb = new StringBuffer(); if (StringUtils.isNotBlank(transaction.getDescription())) { sb.append("<em>" + transaction.getDescription() + "</em><br/>"); } sb.append("Total value: " + Formatter.toCurrency(transaction.getNetValue())); event.setDescription(sb.toString()); event.setUrl(this.getFinancialUrl(transaction.getCategory(), transaction.getId(), preferences)); return event; } }