Java tutorial
/******************************************************************************* * Copyright (c) 2012 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 org.apache.commons.lang.StringUtils; import com.sfs.Formatter; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.beans.TrainingStatusBean; import com.sfs.whichdoctor.beans.TrainingStatusAnalysisBean; import com.sfs.whichdoctor.beans.trainingstatus.AssessmentStatus; import com.sfs.whichdoctor.beans.trainingstatus.ExamStatus; import com.sfs.whichdoctor.beans.trainingstatus.OnlineToolStatus; import com.sfs.whichdoctor.beans.trainingstatus.ReportStatus; public class TrainingStatusFormatter { public static String getField(final TrainingStatusBean status, final TrainingStatusAnalysisBean trainingStatus, final String field, final String format, final PreferencesBean preferences) { String value = ""; if (status == null || field == null) { return value; } PersonBean person = new PersonBean(); if (status.getPerson() != null) { person = status.getPerson(); } if (field.compareTo("MIN") == 0) { value = String.valueOf(person.getPersonIdentifier()); } if (field.compareTo("Person's Name") == 0) { value = OutputFormatter.toCasualName(person); } if (field.compareTo("Preferred Name") == 0) { value = person.getPreferredName(); } if (field.compareTo("Last Name") == 0) { value = person.getLastName(); } if (field.compareTo("Membership Status") == 0) { value = person.getMembershipField("Status"); } if (field.compareTo("Division") == 0) { value = person.getMembershipField("Division"); } if (field.compareTo("Membership Type") == 0) { value = person.getMembershipField("Membership Type"); } if (field.compareTo("Training Type") == 0) { value = person.getMembershipField("Training Type"); } if (field.compareTo("Training Program") == 0) { value = status.getTrainingOrganisation() + " - " + status.getTrainingProgram(); } if (field.compareTo("Training Year") == 0) { value = String.valueOf(status.getYear()); } if (field.compareTo("PREP/pre-PREP") == 0) { value = Formatter.convertBoolean(status.isPrep()); } if (field.compareTo("Online Tools") == 0) { StringBuilder sb = new StringBuilder(); if (status.getOnlineToolsStatus() != null) { for (String key : status.getOnlineToolsStatus().keySet()) { OnlineToolStatus ot = status.getOnlineToolsStatus().get(key); if (sb.length() > 0) { if (StringUtils.equalsIgnoreCase(format, "html")) { sb.append("<br />"); } else { sb.append(", "); } } sb.append(ot.getName()); sb.append(": "); sb.append(ot.getCompletedCount()); sb.append(" / "); sb.append(ot.getRequiredCount()); } } value = sb.toString(); } if (field.compareTo("AT: Interim Report(s) I") == 0) { value = getReportsByType(status, "Interim Report 1", format); } if (field.compareTo("AT: Final Supervisor Report(s)") == 0) { value = getReportsByType(status, "Final Report", format); } if (field.compareTo("AT: Trainee's Report(s)") == 0) { value = getReportsByType(status, "Trainee's Report (NZ)", format); } if (field.compareTo("BT: Mid-Year Progress Report(s)") == 0) { value = getReportsByType(status, "Mid Year Report", format); } if (field.compareTo("BT: Annual Progress Report(s)") == 0) { value = getReportsByType(status, "Annual Progress Report", format); } if (field.compareTo("Written Exams") == 0) { value = getExamsByType(status, "Written Exam", format); } if (field.compareTo("Clinical Exams") == 0) { value = getExamsByType(status, "Clinical Exam", format); } if (field.compareTo("Manual Assessments") == 0) { StringBuilder sb = new StringBuilder(); if (status.getAssessmentsStatus() != null) { for (String key : status.getAssessmentsStatus().keySet()) { AssessmentStatus as = status.getAssessmentsStatus().get(key); if (sb.length() > 0) { if (StringUtils.equalsIgnoreCase(format, "html")) { sb.append("<br />"); } else { sb.append(", "); } } sb.append(as.getAssessmentType()); if (StringUtils.isNotBlank(as.getProjectType())) { sb.append(" - "); sb.append(as.getProjectType()); } sb.append(": "); sb.append(as.getSubmissionCount()); sb.append(" / "); sb.append(as.getRequiredCount()); } } value = sb.toString(); } if (value == null) { value = ""; } return value; } /** * Gets the reports filtered by type. * * @param status the status * @param type the type * @param format the format * @return the reports by type */ private static String getReportsByType(final TrainingStatusBean status, final String type, final String format) { StringBuilder sb = new StringBuilder(); if (status.getReportsStatus() != null) { for (String key : status.getReportsStatus().keySet()) { ReportStatus rs = status.getReportsStatus().get(key); if (StringUtils.equalsIgnoreCase(rs.getReportType(), type)) { if (sb.length() > 0) { if (StringUtils.equalsIgnoreCase(format, "html")) { sb.append("<br />"); } else { sb.append(", "); } } sb.append(rs.getReportStatus()); sb.append(" - "); sb.append(rs.getAuthors()); } } } return sb.toString(); } /** * Gets the exams by type. * * @param status the status * @param type the type * @param format the format * @return the exams by type */ private static String getExamsByType(final TrainingStatusBean status, final String type, final String format) { StringBuilder sb = new StringBuilder(); if (status.getExamsStatus() != null) { for (String key : status.getExamsStatus().keySet()) { ExamStatus es = status.getExamsStatus().get(key); if (StringUtils.equalsIgnoreCase(es.getExamType(), type)) { if (sb.length() > 0) { if (StringUtils.equalsIgnoreCase(format, "html")) { sb.append("<br />"); } else { sb.append(", "); } } sb.append(es.getPassCount()); sb.append(" / "); sb.append(es.getAttemptCount()); } } } return sb.toString(); } }