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.beans; import java.util.ArrayList; import java.util.Collection; import java.util.TreeMap; import java.util.Map; import java.util.Date; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang.StringUtils; /** * The training summary results bean holds training results based on a * committee -> people -> rotation datastructure. * * The datastructure is as follows: * TreeMap< String, TreeMap > committee -> * TreeMap< String, ArrayList > person index -> rotations * * @author David Harrison 2nd June 2007 */ public class TrainingSummaryBean extends TrainingPeople { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 34515111L; /** The start date. */ private Date startDate; /** The end date. */ private Date endDate; /** The results. */ private Map<String, Map<String, Collection<RotationBean>>> results = new TreeMap<String, Map<String, Collection<RotationBean>>>(); /** The training types. */ private Collection<String> trainingTypes = new ArrayList<String>(); /** The approval status. */ private String approvalStatus; /** The accreditation status. */ private String accreditationStatus; /** * Sets the start date. * * @param startDateVal the new start date */ public final void setStartDate(final Date startDateVal) { if (startDateVal != null) { this.startDate = new Date(startDateVal.getTime()); } } /** * Gets the start date. * * @return the start date */ public final Date getStartDate() { return this.startDate; } /** * Sets the end date. * * @param endDateVal the new end date */ public final void setEndDate(final Date endDateVal) { if (endDateVal != null) { this.endDate = new Date(endDateVal.getTime()); } } /** * Gets the end date. * * @return the end date */ public final Date getEndDate() { return this.endDate; } /** * Gets the results. * * @return the results */ public final Map<String, Map<String, Collection<RotationBean>>> getResults() { return this.results; } /** * Sets the results. * * @param resultsVal the results */ public final void setResults(final Map<String, Map<String, Collection<RotationBean>>> resultsVal) { this.results = resultsVal; } /** * Sets the results. * * @param committee the committee * @param peopleResults the people results */ public final void setResults(final String committee, final Map<String, Collection<RotationBean>> peopleResults) { if (committee != null) { if (peopleResults != null) { this.results.put(committee, peopleResults); } else { this.results.put(committee, new TreeMap<String, Collection<RotationBean>>()); } } } /** * Sets the results. * * @param rotations the new results */ public final void setResults(final Collection<RotationBean> rotations) { if (rotations != null) { for (RotationBean rotation : rotations) { boolean rotationCategorised = false; if (rotation.getAssessment() != null) { for (AssessmentBean assessment : rotation.getAssessment()) { if (StringUtils.isNotBlank(assessment.getCommitteeSpecialty()) && rotation.getPerson() != null) { rotationCategorised = true; String committee = assessment.getCommitteeSpecialty(); if (checkInclusion(rotation, 1)) { setResult(committee, rotation); } } } } if (!rotationCategorised && rotation.getPerson() != null) { // Rotation has a person but is not categorised under a // specialty // Place under 'Uncategorised' committee if (checkInclusion(rotation, 1)) { setResult("Uncategorised", rotation); } } } } } /** * Check inclusion. * * @param rotation the rotation * @param specialtyId the specialty identifier * * @return true, if successful */ private boolean checkInclusion(final RotationBean rotation, final int specialtyId) { // Subtract one from the requested specialtyIdentifier // This is to work within the concept of an array index which beings at // 0 final int specialtyIdentifier = specialtyId - 1; boolean includeRotation = true; // Check to see whether rotation should be included in result set // By default all rotations are included unless proven otherwise. ArrayList<AssessmentBean> assessments = new ArrayList<AssessmentBean>(); if (rotation.getAssessment() != null) { assessments = (ArrayList<AssessmentBean>) rotation.getAssessment(); } if (assessments.size() > specialtyIdentifier) { AssessmentBean assessment = assessments.get(specialtyIdentifier); if (StringUtils.equals(getApprovalStatus(), "approved")) { // Only want approved rotations for specialty 1 if (!StringUtils.equalsIgnoreCase(assessment.getApproved(), "Approved")) { includeRotation = false; } } if (StringUtils.equals(getApprovalStatus(), "not-approved")) { // Only want non-approved rotations for the assessment if (StringUtils.equalsIgnoreCase(assessment.getApproved(), "Approved")) { includeRotation = false; } } if (StringUtils.equals(getAccreditationStatus(), "accredited")) { // Only want accredited rotations for the assessment if (!StringUtils.equalsIgnoreCase(assessment.getStatus(), "Accredited")) { includeRotation = false; } } if (StringUtils.equals(getAccreditationStatus(), "not-accredited")) { // Only want non-accredited rotations for the assessment if (StringUtils.equalsIgnoreCase(assessment.getStatus(), "Accredited")) { includeRotation = false; } } } return includeRotation; } /** * Sets the result. * * @param committee the committee * @param rotation the rotation */ private void setResult(final String committee, final RotationBean rotation) { Map<String, Collection<RotationBean>> tmResults = this.getResults(committee); ArrayList<RotationBean> updatedRotations = new ArrayList<RotationBean>(); String order = getOrder(rotation.getPerson()); if (tmResults.containsKey(order)) { // Person already exists in committee map, get their array Collection<RotationBean> rotations = tmResults.get(order); if (rotations != null) { updatedRotations = (ArrayList<RotationBean>) rotations; } } updatedRotations.add(rotation); tmResults.put(order, updatedRotations); setResults(committee, tmResults); } /** * Gets the results. * * @param committee the committee * * @return the results */ public final Map<String, Collection<RotationBean>> getResults(final String committee) { Map<String, Collection<RotationBean>> tmResults = new TreeMap<String, Collection<RotationBean>>(); if (this.results != null) { if (this.results.containsKey(committee)) { tmResults = this.results.get(committee); } } return tmResults; } /** * Gets the training committees. * * @return the training committees */ public final Collection<String> getTrainingCommittees() { Collection<String> committees = new ArrayList<String>(); if (this.results != null) { for (String committee : this.results.keySet()) { committees.add(committee); } } return committees; } /** * Gets the training types. * * @return the training types */ public final Collection<String> getTrainingTypes() { return this.trainingTypes; } /** * Sets the training types. * * @param trainingTypesVal the new training types */ public final void setTrainingTypes(final Collection<String> trainingTypesVal) { this.trainingTypes = trainingTypesVal; } /** * Gets the approval status. * * @return the approval status */ public final String getApprovalStatus() { if (this.approvalStatus == null) { this.approvalStatus = ""; } return this.approvalStatus; } /** * Sets the approval status. * * @param approvalStatusVal the new approval status */ public final void setApprovalStatus(final String approvalStatusVal) { this.approvalStatus = approvalStatusVal; } /** * Gets the accreditation status. * * @return the accreditation status */ public final String getAccreditationStatus() { if (this.accreditationStatus == null) { this.accreditationStatus = ""; } return this.accreditationStatus; } /** * Sets the accreditation status. * * @param accreditationStatusVal the new accreditation status */ public final void setAccreditationStatus(final String accreditationStatusVal) { this.accreditationStatus = accreditationStatusVal; } /** * Checks for training type. * * @param trainingType the training type * * @return true, if successful */ public final boolean hasTrainingType(final String trainingType) { for (String type : this.trainingTypes) { if (StringUtils.equalsIgnoreCase(type, trainingType)) { return true; } } return false; } /** * Gets the training for. * * @param person the person * @param committee the committee * * @return the training for */ public final String getTrainingFor(final PersonBean person, final String committee) { String trainingSpecialties = ""; Collection<SpecialtyBean> specialties = person.getSpecialtyList(); if (specialties != null) { for (SpecialtyBean specialty : specialties) { if (StringUtils.equalsIgnoreCase(specialty.getStatus(), "Training for specialty")) { if (!StringUtils.equalsIgnoreCase(specialty.getTrainingProgram(), committee)) { if (StringUtils.isNotBlank(trainingSpecialties)) { trainingSpecialties += " & "; } trainingSpecialties += specialty.getTrainingProgram(); } } } } return trainingSpecialties; } /** * Gets the rotation info. * * @param committee the committee * @param rotation the rotation * * @return the rotation info */ public final String getRotationInfo(final String committee, final RotationBean rotation) { String information = ""; if (rotation != null && rotation.getAssessment() != null) { for (AssessmentBean assessment : rotation.getAssessment()) { if (StringUtils.equalsIgnoreCase(assessment.getCommitteeSpecialty(), committee)) { information = getRotationInfo(assessment.getApproved(), assessment.getApprovedCondition(), assessment.getStatus(), assessment.getStatusReason()); } } } return information; } /** * Gets the rotation info. * * @param approved the approved * @param approvedCondition the approved condition * @param status the status * @param statusReason the status reason * * @return the rotation info */ private String getRotationInfo(final String approved, final String approvedCondition, final String status, final String statusReason) { String information = ""; if (approved != null) { information += approved; } if (StringUtils.isNotBlank(approvedCondition)) { if (StringUtils.isNotBlank(information)) { information += " - "; } information += approvedCondition; } if (status != null) { information += " : " + status; } if (StringUtils.isNotBlank(statusReason)) { if (StringUtils.isNotBlank(information)) { information += " - "; } information += statusReason; } return information; } /** * Gets the rotation div name. * * @param committee the committee * @param rotation the rotation * * @return the rotation div name */ public final String getRotationDivName(final String committee, final RotationBean rotation) { String returnValue = ""; returnValue += committee.replaceAll(" ", "_"); returnValue += "_" + rotation.getGUID(); return returnValue.toLowerCase(); } /** * Clear results. */ public final void clearResults() { this.clearLoadedPeople(); this.results = new TreeMap<String, Map<String, Collection<RotationBean>>>(); } /** * Clone the training summary bean. */ @Override public final TrainingSummaryBean clone() { TrainingSummaryBean instance; try { instance = (TrainingSummaryBean) BeanUtils.cloneBean(this); } catch (Exception e) { instance = null; } return instance; } }