com.sfs.whichdoctor.checker.PersonChecker.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.checker.PersonChecker.java

Source

/*******************************************************************************
 * Copyright (c) 2012 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.checker;

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

import javax.annotation.Resource;

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

import com.sfs.beans.UserBean;
import com.sfs.whichdoctor.beans.PersonBean;
import com.sfs.whichdoctor.beans.SearchBean;
import com.sfs.whichdoctor.beans.SearchResultsBean;
import com.sfs.whichdoctor.dao.PersonDAOImpl;
import com.sfs.whichdoctor.search.SearchDAO;
import com.sfs.whichdoctor.search.WhichDoctorSearchDaoException;

/**
 * The Class PersonChecker.
 */
public class PersonChecker {

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

    /** The search dao. */
    @Resource
    SearchDAO searchDAO;

    /**
     * Check to see if the supplied person details are similar to others.
     *
     * @param person the person
     * @return the list
     */
    public Collection<PersonBean> similar(final PersonBean person) {

        Collection<PersonBean> people = new ArrayList<PersonBean>();

        boolean checkRequired = false;

        if (person != null) {
            if (person.getPersonIdentifier() > 0 || StringUtils.isNotBlank(person.getFirstName())
                    || StringUtils.isNotBlank(person.getLastName())) {
                checkRequired = true;
            }
        }

        if (checkRequired) {
            // Setup initial search object
            SearchBean search = searchDAO.initiate("person", new UserBean());

            PersonBean criteria = (PersonBean) search.getSearchCriteria();

            if (person.getPersonIdentifier() > 0) {
                criteria.setPersonIdentifier(person.getPersonIdentifier());
            }
            if (StringUtils.isNotBlank(person.getFirstName())) {
                criteria.setFirstName(person.getFirstName());
            }
            if (StringUtils.isNotBlank(person.getLastName())) {
                criteria.setLastName(person.getLastName());
            }

            search.setSearchCriteria(criteria);

            try {
                SearchResultsBean results = searchDAO.search(search);

                if (results != null && results.getSearchResults() != null) {
                    for (Object result : results.getSearchResults()) {
                        PersonBean foundPerson = (PersonBean) result;
                        // Ensure that the GUIDs do not match, i.e. it isn't the source
                        if (person.getGUID() != foundPerson.getGUID()) {
                            people.add(foundPerson);
                        }
                    }
                }

            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing the similar person search: " + wdse.getMessage());
            }
        }
        return people;
    }
}