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.prepare; import com.sfs.DataFilter; import com.sfs.Formatter; import com.sfs.beans.BuilderBean; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.PreferencesBean; import com.sfs.whichdoctor.dao.PreferencesDAO; import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.TreeMap; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; /** * The Class ExportHandlerBase. * * @author David Harrison */ public abstract class ExportHandlerBase implements ExportHandler { /** The Constant CSV. */ protected static final String CSV = "csv"; /** The Constant TXT. */ protected static final String TXT = "txt"; /** The Constant HTML. */ protected static final String HTML = "html"; /** The Constant CSV_BUNDLE. */ protected static final String CSV_BUNDLE = "com.sfs.whichdoctor.export.csv"; /** The Constant TXT_BUNDLE. */ protected static final String TXT_BUNDLE = "com.sfs.whichdoctor.export.txt"; /** The Constant HTML_BUNDLE. */ protected static final String HTML_BUNDLE = "com.sfs.whichdoctor.export.html"; /** The field names. */ private String[] fieldNames = {}; /** The sections. */ private TreeMap<String, String[]> sections = new TreeMap<String, String[]>(); /** The preferences dao. */ @Resource private PreferencesDAO preferencesDAO; /** * Gets the field names. * * @return the field names */ public final String[] getFieldNames() { return this.fieldNames; } /** * Sets the field names. * * @param fieldNamesVal the new field names */ protected final void setFieldNames(final String[] fieldNamesVal) { this.fieldNames = fieldNamesVal; } /** * Gets the sections. * * @return the sections */ public final TreeMap<String, String[]> getSections() { if (this.sections == null) { this.sections = new TreeMap<String, String[]>(); } return this.sections; } /** * Sets the sections. * * @param sectionsVal the sections val */ public final void setSections(final TreeMap<String, String[]> sectionsVal) { this.sections = sectionsVal; } /** * Prepare custom export. * * @param request the request * * @return the builder bean */ protected final BuilderBean prepareCustomExport(final HttpServletRequest request) { final BuilderBean display = new BuilderBean(); for (String fieldName : this.getFieldNames()) { final String inputName = "custom_" + StringUtils.replace(fieldName, " ", "_"); final String inputValue = request.getParameter(inputName); if (StringUtils.equalsIgnoreCase(inputValue, "true")) { display.setParameter(fieldName, true); } } for (String section : this.getSections().keySet()) { final String[] sectionEntries = this.getSections().get(section); final BuilderBean details = new BuilderBean(); boolean detailsSet = false; for (String entry : sectionEntries) { final String inputName = StringUtils.replace(section + "_" + entry, " ", "_"); final String inputValue = request.getParameter(inputName); if (StringUtils.equalsIgnoreCase(inputValue, "true")) { details.setParameter(entry, true); detailsSet = true; } } if (detailsSet) { display.addSection(section, details); } } return display; } /** * Gets the load options. * * @param display the display * @param request the request * @param user the user * * @return the load options */ public abstract BuilderBean getLoadOptions(final BuilderBean display, final HttpServletRequest request, final UserBean user); /** * Gets the title. * * @param request the request * @param bannerText the banner text * @param user the user * @param preferences the preferences * * @return the title */ public final Collection<String> getTitle(final HttpServletRequest request, final String bannerText, final UserBean user, final PreferencesBean preferences) { String openingDate = DataFilter.getHtml(request.getParameter("OpeningDate")); String closingDate = DataFilter.getHtml(request.getParameter("ClosingDate")); String titleString = DataFilter.getHtml(request.getParameter("ExportTitle")); Collection<String> titleArray = new ArrayList<String>(); if (StringUtils.isNotBlank(bannerText)) { titleArray.add(preferences.getOption("system", "companyname")); String strBanner = bannerText + " performed on "; strBanner += Formatter.convertDate(Calendar.getInstance().getTime()); if (user != null) { strBanner += " by " + user.getPreferredName() + " " + user.getLastName(); } titleArray.add(strBanner); } if (StringUtils.isNotBlank(titleString)) { titleArray.add(titleString); } String dateRange = ""; if (StringUtils.isNotBlank(openingDate)) { dateRange += "Opening date: " + openingDate; } if (StringUtils.isNotBlank(closingDate)) { if (StringUtils.isNotBlank(dateRange)) { dateRange += ", "; } dateRange += "Closing date: " + closingDate; } if (StringUtils.isNotBlank(dateRange)) { titleArray.add(dateRange); } return titleArray; } /** * Gets the preferences. * * @return the preferences */ public final PreferencesBean getPreferences() { PreferencesBean preferences = new PreferencesBean(); try { preferences = preferencesDAO.load(); } catch (Exception e) { preferences = new PreferencesBean(); } return preferences; } }