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.beans.UserBean; import com.sfs.whichdoctor.beans.AddressBean; import com.sfs.whichdoctor.beans.CreditBean; import com.sfs.whichdoctor.beans.FinancialSummaryBean; import com.sfs.whichdoctor.beans.GroupBean; import com.sfs.whichdoctor.beans.DebitBean; import com.sfs.whichdoctor.beans.PersonBean; import com.sfs.whichdoctor.beans.PhoneBean; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.beans.ReceiptBean; import com.sfs.whichdoctor.beans.ReimbursementBean; import com.sfs.whichdoctor.beans.RotationBean; import com.sfs.whichdoctor.beans.SearchBean; import com.sfs.whichdoctor.beans.TagBean; import java.util.Collection; import org.apache.commons.lang.StringUtils; /** * The Class OutputFormatter. * * @author David Harrison */ public class OutputFormatter { /** * Instantiates a new data filter. */ protected OutputFormatter() { throw new UnsupportedOperationException(); } /** * Output the person bean to the person's formal name. * * @param person the person * * @return the string */ public static String toMemberName(final PersonBean person) { final StringBuffer name = new StringBuffer(); if (person != null) { if (StringUtils.isNotBlank(person.getTitle())) { name.append(person.getTitle().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getFirstName())) { name.append(person.getFirstName().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getMiddleName())) { name.append(person.getMiddleName().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getLastName())) { name.append(person.getLastName().trim()); } if (StringUtils.isNotBlank(person.getHonors())) { name.append(", "); name.append(person.getHonors().trim()); } final String type = person.getMembershipField("Membership Type"); if (StringUtils.equals(type, "Member: Fellow") || StringUtils.equals(type, "Member: Life Fellow")) { name.append(", FRACP"); } } return name.toString(); } /** * Output the person's formatted legal name. * * @param person the person * * @return the string */ public static String toLegalName(final PersonBean person) { final StringBuffer name = new StringBuffer(); if (person != null) { if (StringUtils.isNotBlank(person.getFirstName())) { name.append(person.getFirstName().trim()); } if (name.length() > 0) { name.append(" "); } if (StringUtils.isNotBlank(person.getMiddleName())) { name.append(person.getMiddleName().trim()); } if (name.length() > 0) { name.append(" "); } if (StringUtils.isNotBlank(person.getLastName())) { name.append(person.getLastName().trim()); } } return name.toString(); } /** * Output the person bean to the person's casual name. * * @param person the person * * @return the string */ public static String toContactName(final PersonBean person) { return toContactName(person, true); } /** * Output the person bean to the person's casual name. * * @param person the person * @param includeId a flag to include the id * * @return the string */ public static String toContactName(final PersonBean person, final boolean includeId) { final StringBuffer name = new StringBuffer(); if (person != null) { if (StringUtils.isNotBlank(person.getTitle())) { name.append(person.getTitle().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getPreferredName())) { name.append(person.getPreferredName().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getLastName())) { name.append(person.getLastName().trim()); } if (includeId) { final String type = person.getMembershipField("Membership Type"); if (StringUtils.equals(type, "Member: Fellow") || StringUtils.equals(type, "Member: Life Fellow")) { name.append(", FRACP"); } } } return name.toString(); } /** * To formatted name. * * @param person the person * * @return the string */ public static String toFormattedName(final PersonBean person) { final StringBuffer name = new StringBuffer(); if (person != null) { if (StringUtils.isNotBlank(person.getPreferredName())) { name.append(person.getPreferredName().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getLastName())) { name.append(person.getLastName().trim()); name.append(" "); } if (person.getPersonIdentifier() > 0) { name.append("("); name.append(person.getPersonIdentifier()); name.append(")"); } } return name.toString(); } /** * To formatted rotation. * * @param rotation the rotation * * @return the string */ public static String toFormattedRotation(final RotationBean rotation) { final StringBuffer name = new StringBuffer(); if (rotation != null && rotation.getPerson() != null) { final PersonBean person = rotation.getPerson(); if (StringUtils.isNotBlank(person.getPreferredName())) { name.append(person.getPreferredName().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getLastName())) { name.append(person.getLastName().trim()); name.append(": "); } } if (rotation != null) { if (StringUtils.isNotBlank(rotation.getDescription())) { name.append(rotation.getDescription().trim()); } name.append(" ("); name.append(rotation.getId()); name.append(")"); } return name.toString(); } /** * To formatted debit. * * @param debit the debit * * @return the string */ public static String toFormattedDebit(final DebitBean debit) { final StringBuffer name = new StringBuffer(); if (debit != null) { if (StringUtils.isNotBlank(debit.getAbbreviation())) { name.append(debit.getAbbreviation().trim()); name.append(" "); } if (StringUtils.isNotBlank(debit.getNumber())) { name.append(debit.getNumber().trim()); name.append(": "); } if (StringUtils.isNotBlank(debit.getDescription())) { name.append(debit.getDescription().trim()); } } return name.toString(); } /** * To formatted credit. * * @param credit the credit * * @return the string */ public static String toFormattedCredit(final CreditBean credit) { final StringBuffer name = new StringBuffer(); if (credit != null) { if (StringUtils.isNotBlank(credit.getAbbreviation())) { name.append(credit.getAbbreviation().trim()); name.append(" "); } if (StringUtils.isNotBlank(credit.getNumber())) { name.append(credit.getNumber().trim()); name.append(": "); } if (StringUtils.isNotBlank(credit.getDescription())) { name.append(credit.getDescription().trim()); } } return name.toString(); } /** * To formatted receipt. * * @param receipt the receipt * * @return the string */ public static String toFormattedReceipt(final ReceiptBean receipt) { final StringBuffer name = new StringBuffer(); if (receipt != null) { if (StringUtils.isNotBlank(receipt.getAbbreviation())) { name.append(receipt.getAbbreviation().trim()); name.append(" "); } if (StringUtils.isNotBlank(receipt.getNumber())) { name.append(receipt.getNumber().trim()); name.append(": "); } if (StringUtils.isNotBlank(receipt.getDescription())) { name.append(receipt.getDescription().trim()); } } return name.toString(); } /** * To formatted reimbursement. * * @param reimbursement the reimbursement * * @return the string */ public static String toFormattedReimbursement(final ReimbursementBean reimbursement) { final StringBuffer name = new StringBuffer(); if (reimbursement != null) { if (StringUtils.isNotBlank(reimbursement.getAbbreviation())) { name.append(reimbursement.getAbbreviation().trim()); name.append(" "); } if (StringUtils.isNotBlank(reimbursement.getNumber())) { name.append(reimbursement.getNumber().trim()); name.append(": "); } if (StringUtils.isNotBlank(reimbursement.getDescription())) { name.append(reimbursement.getDescription().trim()); } } return name.toString(); } /** * To formatted group. * * @param group the group * * @return the string */ public static String toFormattedGroup(final GroupBean group) { final StringBuffer name = new StringBuffer(); if (group != null) { if (StringUtils.isNotBlank(group.getName())) { name.append(group.getName().trim()); } if (StringUtils.isNotBlank(group.getType())) { name.append(" ("); name.append(group.getType().trim()); name.append(")"); } } return name.toString(); } /** * To formatted search. * * @param search the search * * @return the string */ public static String toFormattedSearch(final SearchBean search) { final StringBuffer name = new StringBuffer(); if (search != null) { if (StringUtils.isNotBlank(search.getName())) { name.append(search.getName().trim()); } } return name.toString(); } /** * To formatted vote number. * * @param voteNumber the vote number * * @return the string */ public static String toFormattedVoteNumber(final int voteNumber) { final StringBuffer vote = new StringBuffer(); vote.append(voteNumber); final int maxVoteLength = 6; while (vote.length() < maxVoteLength) { // Pad the number with zeros to make it at least 6 characters vote.insert(0, "0"); } return vote.toString(); } /** * To casual name. * * @param person the person * * @return the string */ public static String toCasualName(final PersonBean person) { final StringBuffer name = new StringBuffer(); if (person != null) { if (StringUtils.isNotBlank(person.getTitle())) { name.append(person.getTitle().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getPreferredName())) { name.append(person.getPreferredName().trim()); name.append(" "); } if (StringUtils.isNotBlank(person.getLastName())) { name.append(person.getLastName().trim()); } } return name.toString(); } /** * To tag list. * * @param tags the tags * * @return the string */ public static String toTagList(final Collection<TagBean> tags) { final StringBuffer tagList = new StringBuffer(); if (tags != null) { for (TagBean tag : tags) { if (tagList.length() > 0) { tagList.append(", "); } tagList.append(tag.getTagName().trim()); } } return tagList.toString(); } /** * To address. * * @param address the address * @param preferences the preferences * @return the string */ public static String toAddress(final AddressBean address, final PreferencesBean preferences) { boolean localAddress = false; if (preferences != null && StringUtils.equalsIgnoreCase(address.getCountry(), preferences.getOption("system", "mailingcountry"))) { localAddress = true; } final StringBuffer bfAddress = new StringBuffer(); final String city = address.getCity(); boolean stateExists = false; boolean stateAbbreviation = false; if (StringUtils.isNotBlank(address.getState())) { stateExists = true; if (StringUtils.isNotBlank(address.getStateAbbreviation())) { stateAbbreviation = true; } } for (int i = 0; i < address.getAddressFieldCount(); i++) { if (StringUtils.isNotBlank(address.getAddressField(i))) { boolean displayField = true; if (StringUtils.equalsIgnoreCase(address.getCountry(), "Australia") && i == address.getAddressFieldCount() - 1) { // Last field, check to see if it should be shown if (StringUtils.equalsIgnoreCase(city, "Sydney") || StringUtils.equalsIgnoreCase(city, "Adelaide") || StringUtils.equalsIgnoreCase(city, "Melbourne") || StringUtils.equalsIgnoreCase(city, "Darwin") || StringUtils.equalsIgnoreCase(city, "Canberra")) { displayField = false; } } if (displayField) { if (bfAddress.length() > 0) { bfAddress.append("<br />"); } bfAddress.append(address.getAddressField(i).trim()); } if (!stateExists && StringUtils.equalsIgnoreCase(city, address.getAddressField(i))) { // Last field in address, check for postcode bfAddress.append(getPostCode(address)); } } } if (stateExists) { if (stateAbbreviation) { bfAddress.append(" "); bfAddress.append(address.getStateAbbreviation().trim()); bfAddress.append(getPostCode(address)); } else { bfAddress.append("<br />"); bfAddress.append(address.getState()); bfAddress.append(getPostCode(address)); } } if (!localAddress && StringUtils.isNotBlank(address.getCountry())) { bfAddress.append("<br />"); bfAddress.append(address.getCountry()); } return bfAddress.toString(); } /** * Return a formatted address that only includes street details. * * @param address the address * @param preferences the preferences * @return the string */ public static String toStreetAddress(final AddressBean address, final PreferencesBean preferences) { // Set the country, state, abbreviation and postcode to an empty string address.setCountry(""); address.setState(""); address.setStateAbbreviation(""); address.setPostCode(""); return toAddress(address, preferences); } /** * Gets the post code. * * @param address the address * * @return the post code */ public static String getPostCode(final AddressBean address) { StringBuffer postCode = new StringBuffer(); if (StringUtils.isNotBlank(address.getPostCode())) { postCode.append(" "); postCode.append(address.getPostCode()); } return postCode.toString(); } /** * Gets the phone number. * * @param phone the phone * * @return the phone number */ public static String getPhoneNumber(final PhoneBean phone) { return getPhoneNumber(phone, false); } /** * Gets the phone number. * * @param phone the phone * @param showInternational the show international * * @return the phone number */ public static String getPhoneNumber(final PhoneBean phone, final boolean showInternational) { StringBuffer phoneNumber = new StringBuffer(); if (StringUtils.isNotBlank(phone.getNumber())) { boolean showCountry = true; boolean showArea = false; final int nzCountryCode = 64; if (!showInternational && phone.getCountryCode() == nzCountryCode) { showCountry = false; } if (phone.getAreaCode() > 0) { showArea = true; } if (showCountry) { phoneNumber.append("+"); phoneNumber.append(phone.getCountryCode()); if (showArea) { phoneNumber.append(" "); phoneNumber.append(phone.getAreaCode()); } } else { if (showArea) { phoneNumber.append("0"); phoneNumber.append(phone.getAreaCode()); } } String number = phone.getNumber(); final int dividerPosition = 4; if (number.length() > dividerPosition) { number = number.substring(0, dividerPosition - 1) + " " + number.substring(dividerPosition - 1, number.length()); } if (StringUtils.isNotBlank(number)) { phoneNumber.append(" "); phoneNumber.append(number.trim()); } if (StringUtils.isNotBlank(phone.getExtension())) { phoneNumber.append(" ext. "); phoneNumber.append(phone.getExtension().trim()); } } return phoneNumber.toString(); } /** * Contact alert. * * @param person the person * @param addresses the addresses * * @return true, if successful */ public static boolean contactAlert(final PersonBean person, final Collection<AddressBean> addresses) { boolean contactAlert = false; final String status = person.getMembershipField("Status"); if (StringUtils.equalsIgnoreCase(status, "Deceased")) { contactAlert = true; } if (addresses != null) { for (AddressBean address : addresses) { if (address.getReturnedMail() || address.getRequestNoMail()) { contactAlert = true; } } } return contactAlert; } /** * Gets the human readable balance. * * @param openingBalance the opening balance flag * @param open the open * @param restricted the restricted * @param user the user * * @return the human readable balance */ public static String getBalance(final boolean openingBalance, final FinancialSummaryBean open, final FinancialSummaryBean restricted, final UserBean user) { StringBuffer humanBalance = new StringBuffer(); double balance = open.getOpeningBalance(); if (!openingBalance) { balance = open.getClosingBalance(); } String strBalance = Formatter.toCurrency(balance, "$"); if (strBalance.indexOf("$") != 0) { // Negative value strBalance = StringUtils.replace(strBalance, "(", ""); strBalance = StringUtils.replace(strBalance, ")", "") + " CR"; } else { strBalance = strBalance + " DB"; } humanBalance.append(strBalance); if (user.isFinancialUser()) { // Get restricted balance.... double restrictedBalance = restricted.getOpeningBalance(); if (!openingBalance) { restrictedBalance = restricted.getClosingBalance(); } String strRestrictedBalance = Formatter.toCurrency(restrictedBalance, "$"); if (strRestrictedBalance.indexOf("$") != 0) { // Negative value strRestrictedBalance = StringUtils.replace(strRestrictedBalance, "(", ""); strRestrictedBalance = StringUtils.replace(strRestrictedBalance, ")", "") + " CR"; } else { strRestrictedBalance = strRestrictedBalance + " DB"; } humanBalance.append(" plus restricted: "); humanBalance.append(strRestrictedBalance); humanBalance.append(" = "); // Determine total with open and secured.... String strFinalBalance = Formatter.toCurrency(balance + restrictedBalance, "$"); if (strFinalBalance.indexOf("$") != 0) { // Negative value strFinalBalance = StringUtils.replace(strFinalBalance, "(", ""); strFinalBalance = StringUtils.replace(strFinalBalance, ")", "") + " CR"; } else { strFinalBalance = strFinalBalance + " DB"; } humanBalance.append(strFinalBalance); } return humanBalance.toString(); } }