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.formatter; import com.sfs.Formatter; import com.sfs.whichdoctor.beans.AccreditationBean; import com.sfs.whichdoctor.beans.AssessmentBean; import com.sfs.whichdoctor.beans.MemoBean; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.beans.ReportBean; import com.sfs.whichdoctor.beans.RotationBean; import com.sfs.whichdoctor.beans.SupervisorBean; import java.util.ArrayList; import java.util.Collection; import org.apache.commons.lang.StringUtils; public class RotationFormatter { public static String getField(final RotationBean rotation, final String field, final String format, final PreferencesBean preferences) { String value = ""; if (rotation == null || field == null) { return value; } if (field.compareTo("Tags") == 0) { value = OutputFormatter.toTagList(rotation.getTags()); } if (field.compareTo("GUID") == 0) { if (rotation.getGUID() > 0) { value = String.valueOf(rotation.getGUID()); } } if (field.compareTo("RotationId") == 0) { if (rotation.getId() > 0) { value = String.valueOf(rotation.getId()); } } if (field.compareTo("MIN") == 0) { if (rotation.getPerson() != null) { value = String.valueOf(rotation.getPerson().getPersonIdentifier()); } } if (field.compareTo("Title") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = person.getTitle(); } } if (field.compareTo("Preferred Name") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = person.getPreferredName(); } } if (field.compareTo("Last Name") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = person.getLastName(); } } if (field.compareTo("Formatted Name") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = OutputFormatter.toFormattedName(person); } } if (field.compareTo("Person") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = OutputFormatter.toCasualName(person); } } if (StringUtils.equals(field, "First Organisation") && StringUtils.isNotBlank(rotation.getOrganisation1Name()) && !rotation.getInterruption()) { value = rotation.getOrganisation1Name(); if (StringUtils.isNotBlank(rotation.getOrganisation1Type())) { value += " (" + rotation.getOrganisation1Type() + ")"; } } if (StringUtils.equals(field, "Second Organisation") && StringUtils.isNotBlank(rotation.getOrganisation2Name())) { value = rotation.getOrganisation2Name(); if (StringUtils.isNotBlank(rotation.getOrganisation2Type())) { value += " (" + rotation.getOrganisation2Type() + ")"; } } if (field.compareTo("Address (mail merge)") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); if (person.getAddress().size() > 0) { value = OutputFormatter.toAddress(person.getFirstAddress(), preferences); } } } if (field.compareTo("Type") == 0) { value = rotation.getRotationType(); } if (field.compareTo("Rotation Type") == 0) { value = rotation.getTrainingClass(); } if (field.compareTo("Interruption Reason") == 0) { value = rotation.getTrainingType(); if (rotation.getInterruption() && StringUtils.isNotBlank(rotation.getOrganisation1Name())) { value += " - " + rotation.getOrganisation1Name(); } } if (field.compareTo("Description") == 0) { value = rotation.getDescription(); } if (field.compareTo("Starting Date") == 0) { if (rotation.getStartDate() != null) { value = Formatter.conventionalDate(rotation.getStartDate()); } } if (field.compareTo("Completion Date") == 0) { if (rotation.getEndDate() != null) { value = Formatter.conventionalDate(rotation.getEndDate()); } } if (field.compareTo("Training Year") == 0) { if (rotation.getYear() > 0) { value = String.valueOf(rotation.getYear()); } } if (field.compareTo("Training Time") == 0) { value = String.valueOf(rotation.getTrainingTime() * 100) + "%"; } if (field.compareTo("Total Months") == 0) { value = String.valueOf(rotation.getTotalMonths()); } if (field.compareTo("Rotation Approved") == 0) { if (rotation.getAssessment() != null) { int i = 0; for (AssessmentBean assessment : rotation.getAssessment()) { if (value.compareTo("") != 0) { value += " / "; } value += assessment.getApproved(); if (assessment.getApprovedCondition().compareTo("") != 0) { value += " - " + assessment.getApprovedCondition(); } if (i < (rotation.getAssessment().size() - 1)) { value += " and "; } i++; } } } if (field.compareTo("Rotation Status") == 0) { if (rotation.getAssessment() != null) { int i = 0; for (AssessmentBean assessment : rotation.getAssessment()) { if (value.compareTo("") != 0) { value += " / "; } value += assessment.getStatus(); if (assessment.getStatusReason().compareTo("") != 0) { value += " - " + assessment.getStatusReason(); } if (i < (rotation.getAssessment().size() - 1)) { value += " and "; } i++; } } } if (field.compareTo("Supervisors") == 0) { StringBuilder supervisors = new StringBuilder(); if (rotation.getSupervisors() != null) { for (SupervisorBean supervisor : rotation.getSupervisors()) { if (supervisor.getPerson() != null) { if (supervisors.length() > 0) { supervisors.append(", "); } supervisors.append(OutputFormatter.toFormattedName(supervisor.getPerson())); if (StringUtils.isNotBlank(supervisor.getRelationshipType())) { supervisors.append(" - "); supervisors.append(supervisor.getRelationshipType()); } } } } value = supervisors.toString(); } if (field.compareTo("Created By") == 0) { value = rotation.getCreatedBy(); } if (field.compareTo("Date Created") == 0 && rotation.getCreatedDate() != null) { value = Formatter.conventionalDate(rotation.getCreatedDate()); } if (field.compareTo("Modified By") == 0) { value = rotation.getModifiedBy(); } if (field.compareTo("Date Modified") == 0 && rotation.getModifiedDate() != null) { value = Formatter.conventionalDate(rotation.getModifiedDate()); } if (value == null) { value = ""; } return value; } public static String getField(final RotationBean rotation, final Object object, final String section, final String field, final String format) { String value = ""; if (section == null) { return value; } if (field == null) { return value; } if (field.compareTo("GUID") == 0) { value = String.valueOf(rotation.getGUID()); } if (field.compareTo("MIN") == 0) { if (rotation.getPerson() != null) { value = String.valueOf(rotation.getPerson().getPersonIdentifier()); } } if (field.compareTo("Title") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = person.getTitle(); } } if (field.compareTo("Preferred Name") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = person.getPreferredName(); } } if (field.compareTo("Last Name") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = person.getLastName(); } } if (field.compareTo("Formatted Name") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = OutputFormatter.toFormattedName(person); } } if (field.compareTo("Person") == 0) { if (rotation.getPerson() != null) { PersonBean person = rotation.getPerson(); value = OutputFormatter.toCasualName(person); } } if (field.compareTo("Type") == 0) { value = rotation.getRotationType(); } if (field.compareTo("Description") == 0) { value = rotation.getDescription(); } if (field.compareTo("Training Year") == 0 && rotation.getYear() > 0) { value = String.valueOf(rotation.getYear()); } if (field.compareTo("Starting Date") == 0) { if (rotation.getStartDate() != null) { value = Formatter.conventionalDate(rotation.getStartDate()); } } if (field.compareTo("Completion Date") == 0) { if (rotation.getEndDate() != null) { value = Formatter.conventionalDate(rotation.getEndDate()); } } if (field.compareTo("Training Time") == 0) { value = String.valueOf(rotation.getTrainingTime() * 100) + "%"; } if (section.compareTo("Accreditations") == 0) { AccreditationBean accreditation = (AccreditationBean) object; if (field.compareTo("Accreditation Type") == 0) { value = accreditation.getAccreditationType(); } if (field.compareTo("Accreditation Specialty") == 0) { value = accreditation.getSpecialtyType(); } if (field.compareTo("Accreditation Sub-Specialty") == 0) { value = accreditation.getSpecialtySubType(); } if (field.compareTo("Weeks Approved") == 0) { value = String.valueOf(accreditation.getWeeksApproved()); } if (field.compareTo("Weeks Certified") == 0) { value = String.valueOf(accreditation.getWeeksCertified()); } if (field.compareTo("Core Accreditation") == 0) { value = Formatter.convertBoolean(accreditation.getCore()); } } if (section.compareTo("Reports") == 0) { ReportBean report = (ReportBean) object; if (field.compareTo("Report Type") == 0) { value = report.getReportType(); } if (field.compareTo("Report Status") == 0) { value = report.getReportStatus(); } if (field.compareTo("Report Authors") == 0 && report.getAuthors() != null) { StringBuilder authors = new StringBuilder(); for (PersonBean person : report.getAuthors()) { if (authors.length() > 0) { authors.append(", "); } authors.append(OutputFormatter.toFormattedName(person)); } value = authors.toString(); } } if (section.compareTo("Memos") == 0) { MemoBean memo = (MemoBean) object; if (field.compareTo("Type") == 0) { value = memo.getType(); } if (field.compareTo("Date Created") == 0) { value = Formatter.conventionalDate(memo.getCreatedDate()); } if (field.compareTo("Date Expires") == 0) { value = Formatter.conventionalDate(memo.getExpires()); } if (field.compareTo("Message") == 0) { value = memo.getMessage(); } } if (value == null) { value = ""; } return value; } public static Collection<Object> getCollection(final RotationBean rotation, final String section) { Collection<Object> collection = new ArrayList<Object>(); if (section != null) { if (section.compareTo("Accreditations") == 0) { if (rotation.getAccreditation() != null) { for (AccreditationBean accreditation : rotation.getAccreditation()) { collection.add(accreditation); } } } if (section.compareTo("Reports") == 0) { if (rotation.getReports() != null) { for (ReportBean report : rotation.getReports()) { collection.add(report); } } } if (section.compareTo("Memos") == 0) { if (rotation.getMemo() != null) { for (MemoBean memo : rotation.getMemo()) { collection.add(memo); } } } } return collection; } }