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.search.http; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import com.sfs.DataFilter; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.OnlineApplicationBean; import com.sfs.whichdoctor.beans.SearchBean; import com.sfs.whichdoctor.search.sql.OnlineApplicationSqlHandler; /** * The Class OnlineApplicationInputHandler. * * @author David Harrison */ public class OnlineApplicationInputHandler extends InputHandlerBase { /** The online application sql handler. */ @Resource private OnlineApplicationSqlHandler onlineApplicationSqlHandler; /** The data logger. */ private static Logger dataLogger = Logger.getLogger(OnlineApplicationInputHandler.class); /** * Process the incoming HttpRequest for search parameters. * * @param request the request * @param user the user * * @return the search bean */ public final SearchBean process(final HttpServletRequest request, final UserBean user) { SearchBean search = onlineApplicationSqlHandler.initiate(user); OnlineApplicationBean searchCriteria = (OnlineApplicationBean) search.getSearchCriteria(); OnlineApplicationBean searchConstraints = (OnlineApplicationBean) search.getSearchConstraints(); /* Process search form */ String strPersonGUID = DataFilter.getHtml(request.getParameter("personGUID")); String strApplicationKey = DataFilter.getHtml(request.getParameter("applicationKey")); String strPersonName = DataFilter.getHtml(request.getParameter("personName")); String strFirstName = DataFilter.getHtml(request.getParameter("firstName")); String strLastName = DataFilter.getHtml(request.getParameter("lastName")); String strType = DataFilter.getHtml(request.getParameter("applicationType")); String strStatus = DataFilter.getHtml(request.getParameter("applicationStatus")); String strProcessed = DataFilter.getHtml(request.getParameter("processed")); String strCreatedA = DataFilter.getHtml(request.getParameter("createdA")); String strCreatedB = DataFilter.getHtml(request.getParameter("createdB")); String strUpdatedA = DataFilter.getHtml(request.getParameter("updatedA")); String strUpdatedB = DataFilter.getHtml(request.getParameter("updatedB")); /* Set the person GUID */ if (StringUtils.isNotBlank(strPersonGUID)) { int personGUID = 0; try { personGUID = Integer.parseInt(strPersonGUID.trim()); } catch (NumberFormatException nfe) { dataLogger.debug("Error parsing person GUID: " + nfe.getMessage()); } searchCriteria.setPersonGUID(personGUID); } /* Set the application key */ if (StringUtils.isNotBlank(strApplicationKey)) { searchCriteria.setKey(strApplicationKey); } /* Set the person name */ if (StringUtils.isNotBlank(strPersonName)) { searchCriteria.setDescription(strPersonName); } /* Set the first name */ if (StringUtils.isNotBlank(strFirstName)) { searchCriteria.setFirstName(strFirstName); } /* Set the last name */ if (StringUtils.isNotBlank(strLastName)) { searchCriteria.setLastName(strLastName); } /* Set the application type */ if (StringUtils.isNotBlank(strType) && !StringUtils.equals(strType, "Null")) { searchCriteria.setType(strType); } /* Set the application status */ if (StringUtils.isNotBlank(strStatus) && !StringUtils.equals(strStatus, "Null")) { searchCriteria.setStatus(strStatus); } /* Set the processed flag */ if (StringUtils.isNotBlank(strProcessed)) { searchCriteria.setProcessed(false); searchConstraints.setProcessed(true); if (StringUtils.equalsIgnoreCase(strProcessed, "true")) { searchCriteria.setProcessed(true); searchConstraints.setProcessed(true); } if (StringUtils.equalsIgnoreCase(strProcessed, "false")) { searchCriteria.setProcessed(false); searchConstraints.setProcessed(false); } } if (StringUtils.isNotBlank(strCreatedA)) { searchCriteria.setCreatedDate(DataFilter.parseDate(strCreatedA, false)); } if (StringUtils.isNotBlank(strCreatedB)) { searchConstraints.setCreatedDate(DataFilter.parseDate(strCreatedB, false)); if (strCreatedB.compareTo("+") == 0) { /* All dates above Date A requested */ searchConstraints.setCreatedDate(getMaximumDate()); } if (strCreatedB.compareTo("-") == 0) { /* Add dates below Date A requested */ searchConstraints.setCreatedDate(getMinimumDate()); } } if (StringUtils.isNotBlank(strUpdatedA)) { searchCriteria.setModifiedDate(DataFilter.parseDate(strUpdatedA, false)); } if (StringUtils.isNotBlank(strUpdatedB)) { searchConstraints.setModifiedDate(DataFilter.parseDate(strUpdatedB, false)); if (strUpdatedB.compareTo("+") == 0) { /* All dates above Date A requested */ searchConstraints.setModifiedDate(getMaximumDate()); } if (strUpdatedB.compareTo("-") == 0) { /* Add dates below Date A requested */ searchConstraints.setModifiedDate(getMinimumDate()); } } search.setSearchCriteria(searchCriteria); search.setSearchConstraints(searchConstraints); return search; } /** * Return a standard search bean. * * @param user the user * * @return the search bean */ public final SearchBean process(final UserBean user) { SearchBean search = onlineApplicationSqlHandler.initiate(user); OnlineApplicationBean searchCriteria = (OnlineApplicationBean) search.getSearchCriteria(); OnlineApplicationBean searchConstraints = (OnlineApplicationBean) search.getSearchConstraints(); search.setSearchCriteria(searchCriteria); search.setSearchConstraints(searchConstraints); return search; } }