com.sfs.whichdoctor.search.http.AddressVerificationInputHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.search.http.AddressVerificationInputHandler.java

Source

/*******************************************************************************
 * Copyright (c) 2011 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.AddressVerificationBean;
import com.sfs.whichdoctor.beans.SearchBean;
import com.sfs.whichdoctor.search.sql.AddressVerificationSqlHandler;

/**
 * The Class AddressVerificationInputHandler.
 *
 * @author David Harrison
 */
public class AddressVerificationInputHandler extends InputHandlerBase {

    /** The data logger. */
    private static Logger dataLogger = Logger.getLogger(AddressVerificationInputHandler.class);

    /** The address verification sql handler. */
    @Resource
    private AddressVerificationSqlHandler addressVerificationSqlHandler;

    /**
     * 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 = addressVerificationSqlHandler.initiate(user);

        AddressVerificationBean searchCriteria = (AddressVerificationBean) search.getSearchCriteria();
        AddressVerificationBean searchConstraints = (AddressVerificationBean) search.getSearchConstraints();

        /* Process search form */
        String strIdentifier = DataFilter.getHtml(request.getParameter("identifier"));
        String strReturnCode = DataFilter.getHtml(request.getParameter("returnCode"));
        String strProcessStatus = DataFilter.getHtml(request.getParameter("processStatus"));

        String strCreatedA = DataFilter.getHtml(request.getParameter("createdA"));
        String strCreatedB = DataFilter.getHtml(request.getParameter("createdB"));

        /* Set identity (identifier/name) requirements */
        if (StringUtils.isNotBlank(strIdentifier)) {
            int identifier = 0;
            try {
                identifier = Integer.parseInt(strIdentifier.trim());
            } catch (NumberFormatException nfe) {
                dataLogger.debug("Error parsing identifier: " + nfe.getMessage());
            }
            if (identifier > 0) {
                searchCriteria.setPersonIdentifier(identifier);
            } else {
                searchCriteria.setPersonName(strIdentifier);
            }
        }

        if (StringUtils.isNotBlank(strReturnCode) && !StringUtils.equalsIgnoreCase(strReturnCode, "Null")) {
            searchCriteria.setReturnCode(strReturnCode);
        }

        if (StringUtils.isNotBlank(strProcessStatus) && !StringUtils.equalsIgnoreCase(strProcessStatus, "Null")) {
            searchCriteria.setProcessStatus(strProcessStatus);
        }

        if (StringUtils.isNotBlank(strCreatedA)) {
            searchCriteria.setCreatedDate(DataFilter.parseDate(strCreatedA, false));
        }
        if (StringUtils.isNotBlank(strCreatedB)) {
            searchConstraints.setCreatedDate(DataFilter.parseDate(strCreatedB, false));

            if (StringUtils.equals(strCreatedB, "+")) {
                /* All dates above Date A requested */
                searchConstraints.setCreatedDate(getMaximumDate());
            }
            if (StringUtils.equals(strCreatedB, "-")) {
                /* Add dates below Date A requested */
                searchConstraints.setCreatedDate(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 = addressVerificationSqlHandler.initiate(user);

        AddressVerificationBean searchCriteria = (AddressVerificationBean) search.getSearchCriteria();
        AddressVerificationBean searchConstraints = (AddressVerificationBean) search.getSearchConstraints();

        search.setSearchCriteria(searchCriteria);
        search.setSearchConstraints(searchConstraints);

        return search;
    }
}