Java tutorial
/******************************************************************************* * Copyright (c) 2010 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 java.util.TreeMap; import org.apache.commons.lang.StringUtils; import com.sfs.Formatter; import com.sfs.beans.BuilderBean; import com.sfs.whichdoctor.beans.AgedDebtorsPeriod; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.beans.AgedDebtorsAnalysisBean; import com.sfs.whichdoctor.beans.AgedDebtorsBreakdown; import com.sfs.whichdoctor.beans.AgedDebtorsGrouping; import com.sfs.whichdoctor.beans.AgedDebtorsRecord; import com.sfs.whichdoctor.formatter.AgedDebtorsAnalysisFormatter; /** * The Class AgedDebtorsAnalysisWriter. * * @author David Harrison */ public class AgedDebtorsAnalysisWriter extends ExportWriterBase { /** * Instantiates a new aged debtors analysis writer. * * @param type the type * @param resourceBundle the resource bundle */ public AgedDebtorsAnalysisWriter(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 groupBy = preferences.getOption("export", "type"); if (StringUtils.isBlank(groupBy)) { groupBy = "Summary"; } AgedDebtorsAnalysisBean agedDebtors = new AgedDebtorsAnalysisBean(); /* Cast collection object to aged debtors */ for (Object objAgedDebtors : results) { agedDebtors = (AgedDebtorsAnalysisBean) objAgedDebtors; } 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")); } if (!StringUtils.equalsIgnoreCase(groupBy, "Summary")) { export.append(this.getKeys().getString("HEADER_PREFIX")); export.append(" "); export.append("Aged Debtors breakdown by " + groupBy); 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, "Period Breakdown")) { header.append(this.getFormattedPeriodBreakdownHeader(agedDebtors)); } else { header.append(headerLine); } header.append(this.getKeys().getString("HEADERITEM_SUFFIX")); } export.append(header.toString()); export.append(this.getKeys().getString("ROW_END")); for (String groupingOrder : agedDebtors.getGroupings().keySet()) { AgedDebtorsGrouping group = agedDebtors.getGroupings().get(groupingOrder); export.append(this.getKeys().getString("ROW_BEGINNING")); StringBuffer fieldValue = new StringBuffer(); for (String fieldName : display.getOrder()) { String field = AgedDebtorsAnalysisFormatter.getField(group, fieldName, this.getType()); if (StringUtils.equalsIgnoreCase(fieldName, "Period Breakdown")) { field = this.getFormattedPeriodBreakdownField(agedDebtors.getPeriods(), group, 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 (String groupOrder : agedDebtors.getGroupings().keySet()) { AgedDebtorsGrouping grouping = agedDebtors.getGroupings().get(groupOrder); HashMap<String, BuilderBean> sections = display.getSections(); if (sections != null) { for (String sectionName : display.getSectionOrder()) { BuilderBean section = sections.get(sectionName); if (!StringUtils.equalsIgnoreCase(groupBy, "Summary")) { String groupName = grouping.getName(); if (StringUtils.isBlank(groupName)) { groupName = "Uncategorised"; } export.append(this.getKeys().getString("SECTION_PREFIX")); export.append(" "); export.append("Breakdown of " + groupName + " records"); 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")); if (StringUtils.equalsIgnoreCase(headerLine, "Period Breakdown")) { sectionHeader.append(this.getFormattedPeriodBreakdownHeader(agedDebtors)); } else { sectionHeader.append(headerLine); } sectionHeader.append(this.getKeys().getString("HEADERITEM_SUFFIX")); } export.append(sectionHeader.toString()); export.append(this.getKeys().getString("ROW_END")); Collection<Object> records = AgedDebtorsAnalysisFormatter.getCollection(grouping, sectionName); export.append(getRecordExportList(records, agedDebtors, section, sectionName)); export.append(this.getKeys().getString("LIST_END")); } } } export.append(this.getKeys().getString("EXPORT_SUFFIX")); return export.toString(); } /** * Gets the record export list. * * @param records the records * @param agedDebtors the aged debtors * @param section the section * @param sectionName the section name * @return the record export list */ private String getRecordExportList(final Collection<Object> records, final AgedDebtorsAnalysisBean agedDebtors, final BuilderBean section, final String sectionName) { StringBuffer export = new StringBuffer(); if (records != null) { for (Object objRecord : records) { AgedDebtorsRecord record = (AgedDebtorsRecord) objRecord; export.append(this.getKeys().getString("ROW_BEGINNING")); StringBuffer fieldValue = new StringBuffer(); for (String fieldName : section.getOrder()) { String field = AgedDebtorsAnalysisFormatter.getField(record, sectionName, fieldName, this.getType()); if (StringUtils.equalsIgnoreCase(fieldName, "Period Breakdown")) { field = this.getFormattedPeriodBreakdownField(agedDebtors.getPeriods(), record, 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 period breakdown header. * * @param agedDebtors the aged debtors * @return the formatted period breakdown header */ private String getFormattedPeriodBreakdownHeader(final AgedDebtorsAnalysisBean agedDebtors) { StringBuffer header = new StringBuffer(); if (agedDebtors != null && agedDebtors.getPeriods() != null && agedDebtors.getPeriods().size() > 0) { int i = 1; for (int id : agedDebtors.getPeriods().keySet()) { AgedDebtorsPeriod period = agedDebtors.getPeriods().get(id); header.append(period.getTitle()); if (i < agedDebtors.getPeriods().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 period breakdown field. * * @param periods the periods * @param group the group * @param format the format * @return the formatted period breakdown field */ private String getFormattedPeriodBreakdownField(final TreeMap<Integer, AgedDebtorsPeriod> periods, final AgedDebtorsBreakdown breakdown, final String format) { StringBuffer field = new StringBuffer(); int i = 1; for (int id : periods.keySet()) { AgedDebtorsPeriod period = periods.get(id); AgedDebtorsPeriod bPeriod = breakdown.getPeriodBreakdown(period); if (StringUtils.equalsIgnoreCase(format, "html")) { field.append("<div style=\"text-align: right\">"); } field.append(Formatter.toCurrency(bPeriod.getTotal(), "$")); if (StringUtils.equalsIgnoreCase(format, "html")) { field.append("</div>"); } if (i < periods.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(); } }