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 com.sfs.Formatter; import com.sfs.beans.ObjectTypeBean; import java.util.Date; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Random; import java.util.TreeMap; import java.util.Map; import java.util.Calendar; import org.apache.commons.lang.StringUtils; /** * The Class PersonBean. * * @author David Harrison 30th May 2003 */ public class PersonBean extends WhichDoctorCoreIdentityBean { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The Constant DAYS_IN_YEAR. */ private static final int DAYS_IN_YEAR = 365; /** The Constant DAYS_IN_MONTH. */ private static final int DAYS_IN_MONTH = 30; /** The Constant HOURS. */ private static final int HOURS = 24; /** The Constant MINUTES. */ private static final int MINUTES = 60; /** The Constant SECONDS. */ private static final int SECONDS = 60; /** The Constant MILLISECONDS. */ private static final int MILLISECONDS = 1000; /** The Constant CANDIDATE_YEAR. */ private static final int CANDIDATE_YEAR = 2000; /** The person identifier. */ private int personIdentifier = 0; /** The first name. */ private String firstName; /** The middle name. */ private String middleName; /** The last name. */ private String lastName; /** The maiden name. */ private String maidenName; /** The full name. */ private String fullName; /** The preferred name. */ private String preferredName; /** The gender. */ private String gender; /** The title. */ private String title; /** The honors. */ private String honors; /** The qualifications. */ private Collection<QualificationBean> qualifications; /** The birth date. */ private Date birthDate; /** The deceased date. */ private Date deceasedDate; /** The region representatives. */ private Collection<PersonBean> regionRepresentatives; /** The currently supervised people. */ private Collection<PersonBean> currentlySupervising; /** The previously supervised people. */ private Collection<PersonBean> previouslySupervised; /** The training summary. */ private Map<String, TreeMap<String, AccreditationBean[]>> trainingSummary; /** The rotations. */ private Collection<RotationBean> rotations; /** The online tools. */ private Collection<OnlineToolBean> onlineTools; /** The exams. */ private Collection<ExamBean> exams; /** The date the person first applied for training. */ private Date dateFirstApplied; /** The exam deadline. */ private Date examDeadline; /** The projects. */ private Collection<ProjectBean> projects; /** The workshops. */ private Collection<WorkshopBean> workshops; /** The mentors. */ private TreeMap<String, ItemBean> mentors; /** The employers. */ private TreeMap<String, ItemBean> employers; /** The training status. */ private String trainingStatus; /** The training status detail. */ private String trainingStatusDetail; /** The training status mapping. */ private String trainingStatusMapping; /** The membership details. */ private Collection<MembershipBean> membershipDetails; /** The primary membership. */ private MembershipBean primaryMembership; /** The see also. */ private String seeAlso; /** The summary total. */ private String summaryTotal; /** The password. */ private String password; /** The Constant RANDOMNUMBER. */ private static final int RANDOMNUMBER = 7485; /** * Sets the person identifier. * * @param personIdentifierVal the new person identifier */ public final void setPersonIdentifier(final int personIdentifierVal) { this.personIdentifier = personIdentifierVal; } /** * Gets the person identifier. * * @return the person identifier */ public final int getPersonIdentifier() { return this.personIdentifier; } /** * Sets the first name. * * @param firstNameVal the new first name */ public final void setFirstName(final String firstNameVal) { this.firstName = firstNameVal; } /** * Gets the first name. * * @return the first name */ public final String getFirstName() { String strFirstName = ""; if (StringUtils.isNotBlank(this.firstName)) { strFirstName = this.firstName; } else { if (StringUtils.isNotBlank(this.preferredName)) { strFirstName = this.preferredName; } } return strFirstName.trim(); } /** * Sets the middle name. * * @param middleNameVal the new middle name */ public final void setMiddleName(final String middleNameVal) { this.middleName = middleNameVal; } /** * Gets the middle name. * * @return the middle name */ public final String getMiddleName() { if (this.middleName == null) { this.middleName = ""; } return this.middleName.trim(); } /** * Sets the last name. * * @param lastNameVal the new last name */ public final void setLastName(final String lastNameVal) { this.lastName = lastNameVal; } /** * Gets the last name. * * @return the last name */ public final String getLastName() { if (this.lastName == null) { this.lastName = ""; } return this.lastName.trim(); } /** * Sets the maiden name. * * @param maidenNameVal the new maiden name */ public final void setMaidenName(final String maidenNameVal) { this.maidenName = maidenNameVal; } /** * Gets the maiden name. * * @return the maiden name */ public final String getMaidenName() { if (this.maidenName == null) { this.maidenName = ""; } return this.maidenName.trim(); } /** * Sets the full name. * * @param fullNameVal the new full name */ public final void setFullName(final String fullNameVal) { this.fullName = fullNameVal; } /** * Gets the full name. * * @return the full name */ public final String getFullName() { if (this.fullName == null) { this.fullName = ""; } return this.fullName.trim(); } /** * Sets the preferred name. * * @param preferredNameVal the new preferred name */ public final void setPreferredName(final String preferredNameVal) { this.preferredName = preferredNameVal; } /** * Gets the preferred name. * * @return the preferred name */ public final String getPreferredName() { String strPreferredName = this.preferredName; if (StringUtils.isBlank(strPreferredName)) { strPreferredName = getFirstName(); } return strPreferredName.trim(); } /** * Sets the gender. * * @param genderVal the new gender */ public final void setGender(final String genderVal) { this.gender = genderVal; } /** * Gets the gender. * * @return the gender */ public final String getGender() { if (this.gender == null) { this.gender = ""; } return this.gender.trim(); } /** * Sets the title. * * @param titleVal the new title */ public final void setTitle(final String titleVal) { this.title = titleVal; } /** * Gets the title. * * @return the title */ public final String getTitle() { if (this.title == null) { this.title = ""; } return this.title.trim(); } /** * Sets the qualifications. * * @param qualificationsVal the new qualifications */ public final void setQualifications(final Collection<QualificationBean> qualificationsVal) { this.qualifications = qualificationsVal; } /** * Gets the qualifications. * * @return the qualifications */ public final Collection<QualificationBean> getQualifications() { if (this.qualifications == null) { this.qualifications = new ArrayList<QualificationBean>(); } return this.qualifications; } /** * Sets the honors. * * @param honorsVal the new honors */ public final void setHonors(final String honorsVal) { this.honors = honorsVal; } /** * Gets the honors. * * @return the honors */ public final String getHonors() { if (this.honors == null) { this.honors = ""; } return this.honors.trim(); } /** * Sets the birth date. * * @param birthDateVal the new birth date */ public final void setBirthDate(final Date birthDateVal) { if (birthDateVal != null) { this.birthDate = new Date(birthDateVal.getTime()); } } /** * Gets the birth date. * * @return the birth date */ public final Date getBirthDate() { return this.birthDate; } /** * Sets the deceased date. * * @param deceasedDateVal the new deceased date */ public final void setDeceasedDate(final Date deceasedDateVal) { if (deceasedDateVal != null) { this.deceasedDate = new Date(deceasedDateVal.getTime()); } } /** * Gets the deceased date. * * @return the deceased date */ public final Date getDeceasedDate() { return this.deceasedDate; } /** * Sets the password. * * @param passwordVal the new password */ public final void setPassword(final String passwordVal) { this.password = passwordVal; } /** * Gets the password. * * @return the password */ public final String getPassword() { if (StringUtils.isBlank(this.password)) { this.password = this.generatePassword(); } return this.password; } /** * Gets the age. * * @return the age */ public final String getAge() { final StringBuffer age = new StringBuffer(); if (this.birthDate != null) { long startDate = 0; long endDate = 0; Calendar difference = Calendar.getInstance(); difference.setTime(this.birthDate); startDate = difference.getTimeInMillis(); if (this.deceasedDate != null) { difference.setTime(this.deceasedDate); endDate = difference.getTimeInMillis(); age.append("Died aged "); } else { Date currentDate = Calendar.getInstance().getTime(); difference.setTime(currentDate); endDate = difference.getTimeInMillis(); } final long fiscalDays = (endDate - startDate) / (MILLISECONDS * SECONDS * MINUTES * HOURS); long years = fiscalDays / DAYS_IN_YEAR; long remainingDays = fiscalDays - (years * DAYS_IN_YEAR); double months = remainingDays / DAYS_IN_MONTH; int intMonths = (int) Formatter.round(months, 0); age.append(String.valueOf(years)); age.append(" years"); if (intMonths > 0) { age.append(", "); age.append(String.valueOf(intMonths)); age.append(" month"); } if (intMonths > 1) { age.append("s"); } } return age.toString().trim(); } /** * Sets the training summary. * * @param type the type * @param summary the summary */ public final void setTrainingSummary(final String type, final TreeMap<String, AccreditationBean[]> summary) { if (this.trainingSummary == null) { this.trainingSummary = new HashMap<String, TreeMap<String, AccreditationBean[]>>(); } this.trainingSummary.put(type, summary); } /** * Gets the training summary. * * @param type the type * * @return the training summary */ public final TreeMap<String, AccreditationBean[]> getTrainingSummary(final String type) { TreeMap<String, AccreditationBean[]> summary = null; if (this.trainingSummary == null) { this.trainingSummary = new HashMap<String, TreeMap<String, AccreditationBean[]>>(); } if (this.trainingSummary.containsKey(type)) { summary = this.trainingSummary.get(type); } return summary; } /** * Sets the rotations. * * @param rotationsVal the new rotations */ public final void setRotations(final Collection<RotationBean> rotationsVal) { this.rotations = rotationsVal; } /** * Gets the rotations. * * @return the rotations */ public final Collection<RotationBean> getRotations() { if (this.rotations == null) { this.rotations = new ArrayList<RotationBean>(); } return this.rotations; } /** * Sets the online tools. * * @param rotationsVal the new online tools */ public final void setOnlineTools(final Collection<OnlineToolBean> onlineToolsVal) { this.onlineTools = onlineToolsVal; } /** * Gets the online tools. * * @return the online tools */ public final Collection<OnlineToolBean> getOnlineTools() { if (this.onlineTools == null) { this.onlineTools = new ArrayList<OnlineToolBean>(); } return this.onlineTools; } /** * Sets the exams. * * @param examsVal the new exams */ public final void setExams(final Collection<ExamBean> examsVal) { this.exams = examsVal; } /** * Gets the exams. * * @return the exams */ public final Collection<ExamBean> getExams() { if (this.exams == null) { this.exams = new ArrayList<ExamBean>(); } return this.exams; } /** * Sets the projects. * * @param projectsVal the new projects */ public final void setProjects(final Collection<ProjectBean> projectsVal) { this.projects = projectsVal; } /** * Gets the projects. * * @return the projects */ public final Collection<ProjectBean> getProjects() { if (this.projects == null) { this.projects = new ArrayList<ProjectBean>(); } return this.projects; } /** * Sets the workshops. * * @param workshopsVal the new workshops */ public final void setWorkshops(final Collection<WorkshopBean> workshopsVal) { this.workshops = workshopsVal; } /** * Gets the workshops. * * @return the workshops */ public final Collection<WorkshopBean> getWorkshops() { if (this.workshops == null) { this.workshops = new ArrayList<WorkshopBean>(); } return this.workshops; } /** * Sets the mentors. * * @param mentorsVal the mentors */ public final void setMentors(final TreeMap<String, ItemBean> mentorsVal) { this.mentors = mentorsVal; } /** * Gets the mentors. * * @return the mentors */ public final TreeMap<String, ItemBean> getMentors() { if (this.mentors == null) { this.mentors = new TreeMap<String, ItemBean>(); } return this.mentors; } /** * Sets the employers. * * @param employersVal the employers */ public final void setEmployers(final TreeMap<String, ItemBean> employersVal) { this.employers = employersVal; } /** * Gets the employers. * * @return the employers */ public final TreeMap<String, ItemBean> getEmployers() { if (this.employers == null) { this.employers = new TreeMap<String, ItemBean>(); } return this.employers; } /** * Sets the training status. * * @param trainingStatusVal the new training status */ public final void setTrainingStatus(final String trainingStatusVal) { this.trainingStatus = trainingStatusVal; } /** * Gets the training status. * * @return the training status */ public final String getTrainingStatus() { if (this.trainingStatus == null) { this.trainingStatus = ""; } return this.trainingStatus; } /** * Sets the training status detail. * * @param trainingStatusVal the new training status detail */ public final void setTrainingStatusDetail(final String trainingStatusDetailVal) { this.trainingStatusDetail = trainingStatusDetailVal; } /** * Gets the training status detail. * * @return the training status detail */ public final String getTrainingStatusDetail() { if (this.trainingStatusDetail == null) { this.trainingStatusDetail = ""; } return this.trainingStatusDetail; } /** * Sets the training status mapping. * * @param trainingStatusVal the new training status mapping */ public final void setTrainingStatusMapping(final String trainingStatusMappingVal) { this.trainingStatusMapping = trainingStatusMappingVal; } /** * Gets the training status mapping. * * @return the training status mapping */ public final String getTrainingStatusMapping() { if (this.trainingStatusMapping == null) { this.trainingStatusMapping = ""; } return this.trainingStatusMapping; } /** * Gets the works overseas flag. * * @return the works overseas */ public final boolean getWorksOverseas() { boolean worksOverseas = false; if (StringUtils.equalsIgnoreCase(this.getRegion(), "Outside N.Z.")) { worksOverseas = true; } return worksOverseas; } /** * Gets the region representatives. * * @return the region representatives */ public final Collection<PersonBean> getRegionRepresentatives() { if (this.regionRepresentatives == null) { this.regionRepresentatives = new ArrayList<PersonBean>(); } return this.regionRepresentatives; } /** * Sets the region representatives. * * @param representatives the new region representatives */ public final void setRegionRepresentatives(final Collection<PersonBean> representatives) { this.regionRepresentatives = representatives; } /** * Gets the currently supervised people. * * @return the list of currently supervised people */ public final Collection<PersonBean> getCurrentlySupervising() { if (this.currentlySupervising == null) { this.currentlySupervising = new ArrayList<PersonBean>(); } return this.currentlySupervising; } /** * Sets the currently supervised people. * * @param supervisedVal the list of currently supervised people */ public final void setCurrentlySupervising(final Collection<PersonBean> supervisedVal) { this.currentlySupervising = supervisedVal; } /** * Gets the previously supervised people. * * @return the list of previously supervised people */ public final Collection<PersonBean> getPreviouslySupervised() { if (this.previouslySupervised == null) { this.previouslySupervised = new ArrayList<PersonBean>(); } return this.previouslySupervised; } /** * Sets the previously supervised people. * * @param supervisedVal the list of previously supervised people */ public final void setPreviouslySupervised(final Collection<PersonBean> supervisedVal) { this.previouslySupervised = supervisedVal; } /** * Sets the membership details. * * @param membershipDetailsVal the new membership details */ public final void setMembershipDetails(final Collection<MembershipBean> membershipDetailsVal) { if (membershipDetailsVal != null && membershipDetailsVal.size() > 0) { Object[] membershipArray = membershipDetailsVal.toArray(); this.primaryMembership = (MembershipBean) membershipArray[0]; } this.membershipDetails = membershipDetailsVal; } /** * Gets the membership details. * * @return the membership details */ public final Collection<MembershipBean> getMembershipDetails() { return this.membershipDetails; } /** * Gets the primary membership. * * @return the primary membership */ public final MembershipBean getPrimaryMembership() { return this.primaryMembership; } /** * Gets the membership field. * * @param fieldName the field name * * @return the membership field */ public final String getMembershipField(final String fieldName) { String membershipField = ""; if (this.primaryMembership != null) { membershipField = this.primaryMembership.getField(fieldName); } return membershipField; } /** * Gets the membership object. * * @param fieldName the field name * * @return the membership object */ public final ObjectTypeBean getMembershipObject(final String fieldName) { ObjectTypeBean objectType = null; if (this.primaryMembership != null) { objectType = this.primaryMembership.getObjectTypeField(fieldName); } return objectType; } /** * Gets the personal reference. * * @return the personal reference */ public final String getPersonalReference() { if (this.gender != null) { if (this.gender.compareTo("Female") == 0) { return "her"; } } return "his"; } /** * Sets the date the person first applied for training. * * @param dateFirstAppliedVal the date the person first applied for training */ public final void setDateFirstApplied(final Date dateFirstAppliedVal) { if (dateFirstAppliedVal != null) { this.dateFirstApplied = new Date(dateFirstAppliedVal.getTime()); } } /** * Gets the date the person first applied for training. * * @return the date first applied for training */ public final Date getDateFirstApplied() { return this.dateFirstApplied; } /** * Sets the exam deadline. * * @param examDeadlineVal the new exam deadline */ public final void setExamDeadline(final Date examDeadlineVal) { if (examDeadlineVal != null) { this.examDeadline = new Date(examDeadlineVal.getTime()); } } /** * Gets the exam deadline. * * @return the exam deadline */ public final Date getExamDeadline() { return this.examDeadline; } /** * Holds any other training committees in the training analysis. * * @param seeAlsoVal the new see also */ public final void setSeeAlso(final String seeAlsoVal) { this.seeAlso = seeAlsoVal; } /** * Gets the see also. * * @return the see also */ public final String getSeeAlso() { return this.seeAlso; } /** * Sets the summary total. * * @param summaryTotalVal the new summary total */ public final void setSummaryTotal(final String summaryTotalVal) { this.summaryTotal = summaryTotalVal; } /** * Gets the summary total. * * @return the summary total */ public final String getSummaryTotal() { return this.summaryTotal; } /** * Gets the accreditation total. * * @param committee the committee * @param trainingTypes the training types * * @return the accreditation total */ public final String getAccreditationTotal(final String committee, final Collection<String> trainingTypes) { int totalCore = 0; int totalNonCore = 0; if (committee != null) { for (String type : trainingTypes) { TreeMap<String, AccreditationBean[]> accreds = this.getTrainingSummary(type); if (accreds != null) { for (String key : accreds.keySet()) { AccreditationBean[] details = accreds.get(key); AccreditationBean core = details[0]; AccreditationBean nonCore = details[0]; if (StringUtils.equalsIgnoreCase(committee, core.getSpecialtyType())) { totalCore += core.getWeeksCertified(); totalNonCore += nonCore.getWeeksCertified(); } } } } } return Formatter.getWholeMonths(totalCore) + " (" + Formatter.getWholeMonths(totalNonCore) + ")"; } /** * Gets the curriculum year. * * @return the curriculum year */ public final String getCurriculumYear() { TreeMap<Integer, Integer> years = new TreeMap<Integer, Integer>(); if (this.getSpecialtyList() != null) { for (SpecialtyBean specialty : this.getSpecialtyList()) { if (StringUtils.equalsIgnoreCase(specialty.getStatus(), "In training") && specialty.getTrainingProgramYear() > 0) { years.put(specialty.getTrainingProgramYear(), 1); } } } String value = ""; int count = 1; for (int year : years.keySet()) { if (count > 1) { if (count == years.size()) { value += " and "; } else { value += ", "; } } value += String.valueOf(year); ++count; } return value; } /** * Gets the training memos. * * @return the training memos */ public final Collection<MemoBean> getTrainingMemos() { Collection<MemoBean> trainingMemos = this.getMemo(); if (this.getRotations() != null) { // Go through rotations looking for memos... for (RotationBean rotation : this.getRotations()) { if (rotation != null && rotation.getMemo() != null) { for (MemoBean memo : rotation.getMemo()) { if (memo != null) { trainingMemos.add(memo); } } } } } return trainingMemos; } /** * Gets the vote number. * * @param year the year * * @return the vote number */ public final int getVoteNumber(final int year) { return getVoteNumber(this.getGUID(), year); } /** * Gets the vote number. * * @param guid the guid * @param year the year * * @return the vote number */ public static int getVoteNumber(final int guid, final int year) { // Assumes the elections will be in the year 2000+ int lastTwoDigits = year - CANDIDATE_YEAR; int voteNumber = (lastTwoDigits * RANDOMNUMBER) + year + guid; return voteNumber; } /** * Gets the voter guid. * * @param voteNumber the vote number * @param year the year * * @return the voter guid */ public static int getVoterGUID(final int voteNumber, final int year) { int lastTwoDigits = year - CANDIDATE_YEAR; int guid = voteNumber - year - (lastTwoDigits * RANDOMNUMBER); return guid; } /** * Gets the order index. * * @return the order index */ public final String getOrderIndex() { return this.getLastName() + "_" + this.getFirstName() + "_" + this.getPersonIdentifier(); } /** * Generates a random eight character password for the ISB. * * @return an eight character random password */ private String generatePassword() { StringBuffer sb = new StringBuffer(); Random randomNumber = new Random(); final int maxLength = 8; final int randomNo = 125; final int excludeChar1 = 48; final int excludeChar2 = 57; final int excludeChar3 = 65; final int excludeChar4 = 90; final int excludeChar5 = 97; final int excludeChar6 = 122; for (int i = 0; i < maxLength;) { int value = randomNumber.nextInt(randomNo); if ((value >= excludeChar1 && value <= excludeChar2) || (value >= excludeChar3 && value <= excludeChar4) || (value >= excludeChar5 && value <= excludeChar6)) { sb.append((char) value); i++; } } String strPassword = sb.toString().toLowerCase(); strPassword = StringUtils.replace(strPassword, "1", "z"); strPassword = StringUtils.replace(strPassword, "0", "y"); strPassword = StringUtils.replace(strPassword, "o", "9"); strPassword = StringUtils.replace(strPassword, "l", "8"); return strPassword; } }