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

Java tutorial

Introduction

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

Source

/*******************************************************************************
 * 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.search.http;

import com.sfs.DataFilter;
import com.sfs.beans.ObjectTypeBean;
import com.sfs.beans.UserBean;
import com.sfs.whichdoctor.beans.AddressBean;
import com.sfs.whichdoctor.beans.AssessmentBean;
import com.sfs.whichdoctor.beans.MembershipBean;
import com.sfs.whichdoctor.beans.OrganisationBean;
import com.sfs.whichdoctor.beans.PersonBean;
import com.sfs.whichdoctor.beans.RotationBean;
import com.sfs.whichdoctor.beans.SearchBean;
import com.sfs.whichdoctor.beans.SpecialtyBean;
import com.sfs.whichdoctor.dao.MembershipDAO;
import com.sfs.whichdoctor.search.sql.RotationSqlHandler;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

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

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

    /** The rotation sql handler. */
    @Resource
    private RotationSqlHandler rotationSqlHandler;

    /** The membership dao. */
    @Resource
    private MembershipDAO membershipDAO;

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

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

        String firstName = DataFilter.getHtml(request.getParameter("firstName"));
        String lastName = DataFilter.getHtml(request.getParameter("lastName"));
        String strIdentifier = DataFilter.getHtml(request.getParameter("identifier"));
        String strDivision = DataFilter.getHtml(request.getParameter("division"));
        String trainingType = DataFilter.getHtml(request.getParameter("trainingType"));

        String strGUIDList = DataFilter.getHtml(request.getParameter("guidList"));
        String strIncludeGUIDList = DataFilter.getHtml(request.getParameter("includeGUIDList"));
        final String strIdentifierList = DataFilter.getHtml(request.getParameter("identifierList"));

        String strTrainingTimeA = DataFilter.getHtml(request.getParameter("trainingTimeA"));
        String strTrainingTimeB = DataFilter.getHtml(request.getParameter("trainingTimeB"));

        String description = DataFilter.getHtml(request.getParameter("description"));
        String rotationType = DataFilter.getHtml(request.getParameter("rotationType"));
        String rotationTrainingClass = DataFilter.getHtml(request.getParameter("rotationTrainingClass"));
        String rotationTrainingType = DataFilter.getHtml(request.getParameter("rotationTrainingType"));
        String strTrainingOrganisation = DataFilter.getHtml(request.getParameter("specialtyTrainingOrganisation"));
        String strTrainingProgram = DataFilter.getHtml(request.getParameter("specialtyTrainingProgram"));
        String trainingProgramYearA = DataFilter.getHtml(request.getParameter("specialtyTrainingProgramYearA"));
        String trainingProgramYearB = DataFilter.getHtml(request.getParameter("specialtyTrainingProgramYearB"));
        String strSpecialtyStatus = DataFilter.getHtml(request.getParameter("specialtyStatus"));

        String strOrganisation = DataFilter.getHtml(request.getParameter("organisation"));
        String strCity = DataFilter.getHtml(request.getParameter("city"));
        String strCountry = DataFilter.getHtml(request.getParameter("country"));

        String strYearA = DataFilter.getHtml(request.getParameter("yearA"));
        String strYearB = DataFilter.getHtml(request.getParameter("yearB"));
        String strStartDateA = DataFilter.getHtml(request.getParameter("startDateA"));
        String strStartDateB = DataFilter.getHtml(request.getParameter("startDateB"));
        String strEndDateA = DataFilter.getHtml(request.getParameter("endDateA"));
        String strEndDateB = DataFilter.getHtml(request.getParameter("endDateB"));
        String strApproved = DataFilter.getHtml(request.getParameter("approved"));
        String strApprovedCondition = DataFilter.getHtml(request.getParameter("approvedCondition"));
        String strStatus = DataFilter.getHtml(request.getParameter("status"));
        String strStatusReason = DataFilter.getHtml(request.getParameter("statusReason"));

        String strAssessmentTrainingOrganisation = DataFilter
                .getHtml(request.getParameter("assessmentTrainingOrganisation"));
        String strAssessmentTrainingProgram = DataFilter.getHtml(request.getParameter("assessmentTrainingProgram"));
        String strCommitteeSpecialty = DataFilter.getHtml(request.getParameter("committeeSpecialty"));

        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"));

        PersonBean personSearch = new PersonBean();
        PersonBean personConstraints = new PersonBean();

        boolean findPerson = false;
        /* Set first and last name requirements */
        if (StringUtils.isNotBlank(firstName)) {
            personSearch.setFirstName(firstName.trim());
            findPerson = true;
        }
        if (StringUtils.isNotBlank(lastName)) {
            personSearch.setLastName(lastName.trim());
            findPerson = true;
        }
        int identifier = 0;
        try {
            identifier = Integer.parseInt(strIdentifier);
        } catch (NumberFormatException nfe) {
            dataLogger.debug("Error parsing identifier: " + nfe.getMessage());
        }
        if (identifier > 0) {
            personSearch.setPersonIdentifier(identifier);
            findPerson = true;
        }

        if (StringUtils.isNotBlank(strGUIDList)) {
            // Parse the GUID list string into a collection of strings
            searchCriteria.setGUIDList(DataFilter.parseTextDataToCollection(strGUIDList));
        }

        if (StringUtils.isNotBlank(strIncludeGUIDList)) {
            // Parse the GUID list string into a collection of strings
            searchCriteria.setIncludeGUIDList(DataFilter.parseTextDataToCollection(strIncludeGUIDList));
        }

        if (StringUtils.isNotBlank(strIdentifierList)) {
            // Parse the identifier list string into a collection of strings
            searchCriteria.setIdentifierList(DataFilter.parseTextDataToCollection(strIdentifierList));
        }

        /* Tag searches */
        searchCriteria.setTags(this.setTagArray(request, user));

        /* Set membership details */
        MembershipBean mCriteria = this.membershipDAO.getDefaultInstance();
        MembershipBean mConstraints = this.membershipDAO.getDefaultInstance();
        boolean membershipSet = true;

        if (StringUtils.isNotBlank(strDivision)) {
            if (!StringUtils.equalsIgnoreCase(strDivision, "Null")) {
                ObjectTypeBean object = new ObjectTypeBean();
                object.setClassName(strDivision);
                mCriteria.setField("Division", object);
                membershipSet = true;
                findPerson = true;
            }
        }

        if (StringUtils.isNotBlank(trainingType)) {
            if (!StringUtils.equalsIgnoreCase(trainingType, "Null")) {
                ObjectTypeBean object = new ObjectTypeBean();
                object.setClassName(trainingType);
                mCriteria.setField("Training Type", object);
                membershipSet = true;
                findPerson = true;
            }
        }

        if (membershipSet) {
            Collection<MembershipBean> membershipA = new ArrayList<MembershipBean>();
            Collection<MembershipBean> membershipB = new ArrayList<MembershipBean>();
            membershipA.add(mCriteria);
            membershipB.add(mConstraints);
            personSearch.setMembershipDetails(membershipA);
            personConstraints.setMembershipDetails(membershipB);
        }

        /* Set training time */
        if (strTrainingTimeA != null) {
            double trainingTime = 0;
            try {
                trainingTime = Double.parseDouble(strTrainingTimeA) / 100;
            } catch (NumberFormatException nfe) {
                // Error parsing training time A
                trainingTime = 0;
            }
            searchCriteria.setTrainingTime(trainingTime);
        }
        if (StringUtils.isNotBlank(strTrainingTimeB)) {
            double trainingTime = 0;
            try {
                trainingTime = Double.parseDouble(strTrainingTimeB) / 100;
            } catch (NumberFormatException nfe) {
                // Error parsing training time A
                trainingTime = 0;
            }
            if (StringUtils.equalsIgnoreCase(strTrainingTimeB, "+")) {
                /* All training times above trainingTimeA requested */
                trainingTime = 1.0;
            }
            if (StringUtils.equalsIgnoreCase(strTrainingTimeB, "-")) {
                /* All training times below trainingTimeA requested */
                trainingTime = 0.01;
            }
            searchConstraints.setTrainingTime(trainingTime);
        }

        /* Check if specialty details were set */
        boolean specialty = false;
        SpecialtyBean specialtyCriteria = new SpecialtyBean();
        SpecialtyBean specialtyConstraints = new SpecialtyBean();

        if (StringUtils.isNotBlank(strTrainingOrganisation)
                && !StringUtils.equals(strTrainingOrganisation, "Null")) {
            specialtyCriteria.setTrainingOrganisation(strTrainingOrganisation);

            if (StringUtils.isNotBlank(strTrainingProgram) && !StringUtils.equals(strTrainingProgram, "Any")) {
                specialtyCriteria.setTrainingProgram(strTrainingProgram);
            }
            specialty = true;
        }

        if (StringUtils.isNotBlank(trainingProgramYearA)) {
            int trainingProgramYear = 0;
            try {
                trainingProgramYear = Integer.parseInt(trainingProgramYearA);
            } catch (NumberFormatException nfe) {
                dataLogger.debug("Error parsing training year A: " + nfe.getMessage());
            }
            if (StringUtils.equalsIgnoreCase(trainingProgramYearA, "+")) {
                trainingProgramYear = MAXIMUM_YEAR;
            }
            if (StringUtils.equalsIgnoreCase(trainingProgramYearA, "-")) {
                trainingProgramYear = MINIMUM_YEAR;
            }
            specialtyCriteria.setTrainingProgramYear(trainingProgramYear);
        }
        if (StringUtils.isNotBlank(trainingProgramYearB)) {
            int trainingProgramYear = 0;
            try {
                trainingProgramYear = Integer.parseInt(trainingProgramYearB);
            } catch (NumberFormatException nfe) {
                dataLogger.debug("Error parsing training program B: " + nfe.getMessage());
            }
            if (StringUtils.equalsIgnoreCase(trainingProgramYearB, "+")) {
                trainingProgramYear = MAXIMUM_YEAR;
            }
            if (StringUtils.equalsIgnoreCase(trainingProgramYearB, "-")) {
                trainingProgramYear = MINIMUM_YEAR;
            }
            specialtyConstraints.setTrainingProgramYear(trainingProgramYear);
        }

        if (strSpecialtyStatus != null) {
            if (strSpecialtyStatus.compareTo("Null") != 0) {
                specialtyCriteria.setStatus(strSpecialtyStatus);
                specialty = true;
            }
        }
        if (specialty) {
            ArrayList<SpecialtyBean> spCriteria = new ArrayList<SpecialtyBean>();
            ArrayList<SpecialtyBean> spConstraints = new ArrayList<SpecialtyBean>();
            spCriteria.add(specialtyCriteria);
            spConstraints.add(specialtyConstraints);

            personSearch.setSpecialtyList(spCriteria);
            personConstraints.setSpecialtyList(spConstraints);

            findPerson = true;
        }

        /* Determine if Member criteria have been set */
        if (findPerson) {
            searchCriteria.setPersonSearch(personSearch);
            searchConstraints.setPersonSearch(personConstraints);
        }

        /* Set the organisation details */
        OrganisationBean organisationCriteria = new OrganisationBean();
        AddressBean addressCriteria = new AddressBean();
        boolean organisation = false;
        boolean address = false;

        if (StringUtils.isNotBlank(strOrganisation)) {
            organisationCriteria.setName(strOrganisation);
            organisation = true;
        }
        if (StringUtils.isNotBlank(strCity)) {
            addressCriteria.setAddressField(strCity);
            organisation = true;
            address = true;
        }
        if (StringUtils.isNotBlank(strCountry)) {
            addressCriteria.setCountry(strCountry);
            organisation = true;
            address = true;
        }

        if (organisation) {
            if (address) {
                Collection<AddressBean> addresses = new ArrayList<AddressBean>();
                addresses.add(addressCriteria);
                organisationCriteria.setAddress(addresses);
            }
            searchCriteria.setOrganisation1(organisationCriteria);
        }

        if (StringUtils.isNotBlank(description)) {
            searchCriteria.setDescription(description);
        }

        if (StringUtils.isNotBlank(rotationType) && !StringUtils.equals(rotationType, "Null")) {
            searchCriteria.setRotationType(rotationType);
        }

        if (StringUtils.isNotBlank(rotationTrainingClass) && !StringUtils.equals(rotationTrainingClass, "Null")) {
            searchCriteria.setTrainingClass(rotationTrainingClass);
        }

        if (StringUtils.isNotBlank(rotationTrainingType) && !StringUtils.equals(rotationTrainingType, "Any")) {
            searchCriteria.setTrainingType(rotationTrainingType);
        }

        if (StringUtils.isNotBlank(strYearA)) {
            int year = 0;
            try {
                year = Integer.parseInt(strYearA);
            } catch (NumberFormatException nfe) {
                dataLogger.debug("Error parsing year A: " + nfe.getMessage());
            }
            if (StringUtils.equalsIgnoreCase(strYearA, "+")) {
                year = MAXIMUM_YEAR;
            }
            if (StringUtils.equalsIgnoreCase(strYearA, "-")) {
                year = MINIMUM_YEAR;
            }
            searchCriteria.setYear(year);
        }
        if (StringUtils.isNotBlank(strYearB)) {
            int year = 0;
            try {
                year = Integer.parseInt(strYearB);
            } catch (NumberFormatException nfe) {
                dataLogger.debug("Error parsing year B: " + nfe.getMessage());
            }
            if (StringUtils.equalsIgnoreCase(strYearB, "+")) {
                year = MAXIMUM_YEAR;
            }
            if (StringUtils.equalsIgnoreCase(strYearB, "-")) {
                year = MINIMUM_YEAR;
            }
            searchConstraints.setYear(year);
        }

        /* Set StartDate */
        if (StringUtils.isNotBlank(strStartDateA)) {
            searchCriteria.setStartDate(DataFilter.parseDate(strStartDateA, false));
        }
        if (StringUtils.isNotBlank(strStartDateB)) {
            searchConstraints.setStartDate(DataFilter.parseDate(strStartDateB, false));

            if (StringUtils.equalsIgnoreCase(strStartDateB, "+")) {
                /* All dates above Date A requested */
                searchConstraints.setStartDate(getMaximumDate());
            }
            if (StringUtils.equalsIgnoreCase(strStartDateB, "-")) {
                /* Add dates below Date A requested */
                searchConstraints.setStartDate(getMinimumDate());
            }
        }

        /* Set EndDate */
        if (StringUtils.isNotBlank(strEndDateA)) {
            searchCriteria.setEndDate(DataFilter.parseDate(strEndDateA, false));
        }
        if (StringUtils.isNotBlank(strEndDateB)) {
            searchConstraints.setEndDate(DataFilter.parseDate(strEndDateB, false));

            if (StringUtils.equalsIgnoreCase(strEndDateB, "+")) {
                /* All dates above Date A requested */
                searchConstraints.setEndDate(getMaximumDate());
            }
            if (StringUtils.equalsIgnoreCase(strEndDateB, "-")) {
                /* Add dates below Date A requested */
                searchConstraints.setEndDate(getMinimumDate());
            }
        }

        boolean assessmentRequested = false;
        AssessmentBean assessment = new AssessmentBean();

        if (StringUtils.isNotBlank(strAssessmentTrainingOrganisation)
                && !StringUtils.equals(strAssessmentTrainingOrganisation, "Null")) {
            assessment.setTrainingOrganisation(strAssessmentTrainingOrganisation);

            if (StringUtils.isNotBlank(strAssessmentTrainingProgram)
                    && !StringUtils.equals(strAssessmentTrainingProgram, "Any")) {
                assessment.setTrainingProgram(strAssessmentTrainingProgram);
            }
            assessmentRequested = true;
        }

        if (StringUtils.isNotBlank(strCommitteeSpecialty) && !StringUtils.equals(strCommitteeSpecialty, "Null")) {
            assessment.setCommitteeSpecialty(strCommitteeSpecialty);
            assessmentRequested = true;
        }

        /* Set Rotation Approved */
        if (StringUtils.isNotBlank(strApproved) && !StringUtils.equals(strApproved, "Null")) {
            assessment.setApproved(strApproved);
            assessmentRequested = true;
        }

        /* Set Rotation Approved Condition */
        if (StringUtils.isNotBlank(strApprovedCondition) && !StringUtils.equals(strApprovedCondition, "Any")) {
            assessment.setApprovedCondition(strApprovedCondition);
            assessmentRequested = true;
        }

        /* Set Rotation Status */
        if (StringUtils.isNotBlank(strStatus) && !StringUtils.equals(strStatus, "Null")) {
            assessment.setStatus(strStatus);
            assessmentRequested = true;
        }
        /* Set Rotation Status Reason */
        if (StringUtils.isNotBlank(strStatusReason) && !StringUtils.equals(strStatusReason, "Any")) {
            assessment.setStatusReason(strStatusReason);
            assessmentRequested = true;
        }

        if (assessmentRequested) {
            /* Add the assessment bean to the rotation */
            ArrayList<AssessmentBean> assessments = new ArrayList<AssessmentBean>();
            assessments.add(assessment);
            searchCriteria.setAssessment(assessments);
        }

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

            if (StringUtils.equalsIgnoreCase(strCreatedB, "+")) {
                /* All dates above Date A requested */
                searchConstraints.setCreatedDate(getMaximumDate());
            }
            if (StringUtils.equalsIgnoreCase(strCreatedB, "-")) {
                /* 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 (StringUtils.equalsIgnoreCase(strUpdatedB, "+")) {
                /* All dates above Date A requested */
                searchConstraints.setModifiedDate(getMaximumDate());
            }
            if (StringUtils.equalsIgnoreCase(strUpdatedB, "-")) {
                /* Add dates below Date A requested */
                searchConstraints.setModifiedDate(getMinimumDate());
            }
        }

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

        return search;
    }

    /**
     * Process the incoming HttpRequest for search parameters.
     *
     * @param user the user
     *
     * @return the search bean
     */
    public final SearchBean process(final UserBean user) {

        SearchBean search = rotationSqlHandler.initiate(user);

        RotationBean searchCriteria = (RotationBean) search.getSearchCriteria();

        search.setSearchCriteria(searchCriteria);

        return search;
    }
}