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.export.writer; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import org.apache.commons.lang.StringUtils; import com.sfs.Formatter; import com.sfs.beans.BuilderBean; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.beans.ReceiptBean; import com.sfs.whichdoctor.beans.RevenueBean; import com.sfs.whichdoctor.beans.RevenueAnalysisBean; import com.sfs.whichdoctor.formatter.RevenueAnalysisFormatter; /** * * @author David Harrison */ public class RevenueAnalysisWriter extends ExportWriterBase { /** * Instantiates a new revenue analysis writer. * * @param type the type * @param resourceBundle the resource bundle */ public RevenueAnalysisWriter(final String type, final String resourceBundle) { this.setType(type); this.setKeys(resourceBundle); } /** * Process the export. * * @param resultsVal the results * @param title the title * @param displayRef the display * @param preferencesRef the preferences * * @return the string */ public final String process(final Collection<Object> resultsVal, final Collection<String> title, final BuilderBean displayRef, final PreferencesBean preferencesRef) { StringBuffer export = new StringBuffer(); Collection<Object> results = new ArrayList<Object>(); BuilderBean display = new BuilderBean(); PreferencesBean preferences = new PreferencesBean(); if (resultsVal != null) { results = resultsVal; } if (displayRef != null) { display = displayRef; } if (preferencesRef != null) { preferences = preferencesRef; } String revenueType = preferences.getOption("export", "type"); if (StringUtils.isBlank(revenueType)) { revenueType = "batchReference"; } RevenueAnalysisBean revenueAnalysis = new RevenueAnalysisBean(); /* Cast collection object to revenue analysis */ for (Object objRevenueAnalysis : results) { revenueAnalysis = (RevenueAnalysisBean) objRevenueAnalysis; } export.append(this.getKeys().getString("EXPORT_PREFIX")); for (String titleLine : title) { export.append(this.getKeys().getString("HEADER_PREFIX")); export.append(" "); export.append(titleLine); export.append(this.getKeys().getString("HEADER_SUFFIX")); } export.append(this.getKeys().getString("HEADER_PREFIX")); export.append(" "); if (revenueType.compareTo("batchAnalysis") == 0) { export.append("Revenue Summary by Batch Number"); } if (revenueType.compareTo("streamAnalysis") == 0) { export.append("Revenue Summary by Revenue Stream"); } export.append(this.getKeys().getString("HEADER_SUFFIX")); export.append(this.getKeys().getString("LIST_BEGINNING")); export.append(this.getKeys().getString("ROW_BEGINNING")); StringBuffer header = new StringBuffer(); for (String headerLine : display.getOrder()) { if (header.length() > 0) { header.append(this.getKeys().getString("ITEM_DIVIDER")); } header.append(this.getKeys().getString("HEADERITEM_PREFIX")); if (StringUtils.equalsIgnoreCase(headerLine, "GST Breakdown")) { header.append(this.getFormattedGSTBreakdownHeader(revenueAnalysis)); } else { header.append(headerLine); } header.append(this.getKeys().getString("HEADERITEM_SUFFIX")); } export.append(header.toString()); export.append(this.getKeys().getString("ROW_END")); for (RevenueBean revenue : revenueAnalysis.getRevenue()) { export.append(this.getKeys().getString("ROW_BEGINNING")); StringBuffer fieldValue = new StringBuffer(); for (String fieldName : display.getOrder()) { String field = RevenueAnalysisFormatter.getField(revenue, fieldName, this.getType()); if (StringUtils.equalsIgnoreCase(fieldName, "GST Breakdown")) { field = this.getFormattedGSTBreakdownField(revenue, this.getType()); } if (fieldValue.length() > 0) { fieldValue.append(this.getKeys().getString("ITEM_DIVIDER")); } fieldValue.append(this.getKeys().getString("ITEM_PREFIX")); fieldValue.append(field); fieldValue.append(this.getKeys().getString("ITEM_SUFFIX")); } export.append(fieldValue.toString()); export.append(this.getKeys().getString("ROW_END")); } export.append(this.getKeys().getString("LIST_END")); for (RevenueBean revenue : revenueAnalysis.getRevenue()) { HashMap<String, BuilderBean> sections = display.getSections(); if (sections != null) { for (String sectionName : display.getSectionOrder()) { BuilderBean section = sections.get(sectionName); export.append(this.getKeys().getString("SECTION_PREFIX")); export.append(" "); if (revenueType.compareTo("batchAnalysis") == 0) { export.append("Breakdown of payments for batch number: "); export.append(revenue.getBatchNo()); } if (revenueType.compareTo("streamAnalysis") == 0) { export.append("Breakdown of payments for revenue stream: "); export.append(revenue.getRevenueType()); } export.append(this.getKeys().getString("SECTION_SUFFIX")); export.append(this.getKeys().getString("LIST_BEGINNING")); export.append(this.getKeys().getString("ROW_BEGINNING")); StringBuffer sectionHeader = new StringBuffer(); for (String headerLine : section.getOrder()) { if (sectionHeader.length() > 0) { sectionHeader.append(this.getKeys().getString("ITEM_DIVIDER")); } sectionHeader.append(this.getKeys().getString("HEADERITEM_PREFIX")); sectionHeader.append(headerLine); sectionHeader.append(this.getKeys().getString("HEADERITEM_SUFFIX")); } export.append(sectionHeader.toString()); export.append(this.getKeys().getString("ROW_END")); Collection<Object> receipts = RevenueAnalysisFormatter.getCollection(revenue, sectionName); export.append(getPaymentExportList(receipts, section, sectionName)); export.append(this.getKeys().getString("LIST_END")); } } } export.append(this.getKeys().getString("EXPORT_SUFFIX")); return export.toString(); } /** * Gets the payment export list. * * @param receipts the receipts * @param section the section * @param sectionName the section name * * @return the payment export list */ private String getPaymentExportList(final Collection<Object> receipts, final BuilderBean section, final String sectionName) { final StringBuffer export = new StringBuffer(); if (receipts != null) { for (Object objReceipt : receipts) { ReceiptBean receipt = (ReceiptBean) objReceipt; Collection<Object> payments = RevenueAnalysisFormatter.getCollection(receipt); if (payments != null) { for (Object objPayment : payments) { export.append(this.getKeys().getString("ROW_BEGINNING")); StringBuffer fieldValue = new StringBuffer(); for (String fieldName : section.getOrder()) { String field = RevenueAnalysisFormatter.getField(receipt, objPayment, sectionName, fieldName, this.getType()); if (fieldValue.length() > 0) { fieldValue.append(this.getKeys().getString("ITEM_DIVIDER")); } fieldValue.append(this.getKeys().getString("ITEM_PREFIX")); fieldValue.append(field); fieldValue.append(this.getKeys().getString("ITEM_SUFFIX")); } export.append(fieldValue); export.append(this.getKeys().getString("ROW_END")); } } } } return export.toString(); } /** * Gets the formatted gst breakdown header. * * @param revenueAnalysis the revenue analysis * @return the formatted gst breakdown header */ private String getFormattedGSTBreakdownHeader(final RevenueAnalysisBean revenueAnalysis) { final StringBuffer header = new StringBuffer(); if (revenueAnalysis != null && revenueAnalysis.getRevenue() != null && revenueAnalysis.getRevenue().size() > 0) { final RevenueBean revenue = revenueAnalysis.getRevenue().iterator().next(); int i = 1; for (Double gstRate : revenue.getGSTValues().keySet()) { header.append("GST Value @ "); header.append(Formatter.toPercentage(gstRate, "%")); if (i < revenue.getGSTValues().size()) { header.append(this.getKeys().getString("HEADERITEM_SUFFIX")); header.append(this.getKeys().getString("ITEM_DIVIDER")); header.append(this.getKeys().getString("HEADERITEM_PREFIX")); } i++; } } return header.toString(); } /** * Gets the formatted gst breakdown field. * * @param revenue the revenue * @param format the format for the export * @return the formatted gst breakdown field */ private String getFormattedGSTBreakdownField(final RevenueBean revenue, final String format) { final StringBuffer field = new StringBuffer(); int i = 1; for (Double gstRate : revenue.getGSTValues().keySet()) { final Double gstValue = revenue.getGSTValues().get(gstRate); if (StringUtils.equalsIgnoreCase(format, "html")) { field.append("<div style=\"text-align: right\">"); } field.append(Formatter.toCurrency(gstValue, "$")); if (StringUtils.equalsIgnoreCase(format, "html")) { field.append("</div>"); } if (i < revenue.getGSTValues().size()) { field.append(this.getKeys().getString("ITEM_SUFFIX")); field.append(this.getKeys().getString("ITEM_DIVIDER")); field.append(this.getKeys().getString("ITEM_PREFIX")); } i++; } return field.toString(); } }