com.sfs.whichdoctor.dao.BulkContactDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.dao.BulkContactDAOImpl.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.dao;

import com.sfs.beans.BuilderBean;
import com.sfs.beans.UserBean;
import com.sfs.whichdoctor.beans.BulkContactBean;
import com.sfs.whichdoctor.beans.CreditBean;
import com.sfs.whichdoctor.beans.DebitBean;
import com.sfs.whichdoctor.beans.FinancialAnalysisCore;
import com.sfs.whichdoctor.beans.GroupBean;
import com.sfs.whichdoctor.beans.ItemBean;
import com.sfs.whichdoctor.beans.OrganisationBean;
import com.sfs.whichdoctor.beans.PersonBean;
import com.sfs.whichdoctor.beans.ReimbursementBean;
import com.sfs.whichdoctor.beans.ReceiptBean;
import com.sfs.whichdoctor.beans.RotationBean;
import com.sfs.whichdoctor.beans.SearchBean;
import com.sfs.whichdoctor.beans.SearchHistoryBean;
import com.sfs.whichdoctor.beans.SearchResultsBean;
import com.sfs.whichdoctor.search.SearchDAO;
import com.sfs.whichdoctor.search.WhichDoctorSearchDaoException;

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

import javax.annotation.Resource;

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

/**
 * The Class BulkContactDAOImpl.
 *
 * @author David Harrison
 */
public class BulkContactDAOImpl implements BulkContactDAO {

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

    /** The group dao. */
    @Resource
    private GroupDAO groupDAO;

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

    /** The person dao. */
    @Resource
    private PersonDAO personDAO;

    /** The organisation dao. */
    @Resource
    private OrganisationDAO organisationDAO;

    /**
     * Prepare the bulk contact bean.
     *
     * @param contactBean the bulk contact bean
     * @param searchHistory the search history
     * @return the bulk contact bean
     */
    public final BulkContactBean prepare(final BulkContactBean contactBean, final SearchHistoryBean searchHistory) {

        BulkContactBean bulkContact = performSearch(contactBean, searchHistory, true);

        return bulkContact;
    }

    /**
     * Prepare the bulk contact bean.
     *
     * @param contactBean the bulk contact bean
     * @param exportMap the export map
     * @param user the user
     * @return the bulk contact bean
     */
    public final BulkContactBean prepare(final BulkContactBean contactBean,
            final TreeMap<String, ItemBean> exportMap, final UserBean user) {

        TreeMap<Integer, Integer> exportGUIDs = new TreeMap<Integer, Integer>();
        Collection<Object> guids = new ArrayList<Object>();

        /** Get a list of the unique GUIDs **/
        for (String key : exportMap.keySet()) {
            try {
                ItemBean details = (ItemBean) exportMap.get(key);
                exportGUIDs.put(details.getObject1GUID(), details.getObject1GUID());
            } catch (Exception e) {
                dataLogger.error("Error casting object to export map: " + e.getMessage());
            }
        }
        for (Integer guid : exportGUIDs.keySet()) {
            guids.add(guid);
        }

        BulkContactBean bulkContact = performSearch(contactBean, guids, user, true);

        return bulkContact;
    }

    /**
     * Prepare the bulk contact bean.
     *
     * @param contactBean the bulk contact bean
     * @param groupGUID the group guid
     * @param user the user
     * @return the bulk contact bean
     */
    public final BulkContactBean prepare(final BulkContactBean contactBean, final int groupGUID,
            final UserBean user) {

        GroupBean group = null;
        /** Try loading the group **/
        try {
            BuilderBean builderBean = new BuilderBean();
            builderBean.setParameter("ITEMS", true);

            group = groupDAO.loadGUID(groupGUID, builderBean);
        } catch (WhichDoctorDaoException wde) {
            dataLogger.error("Error loading group for bulk contact: " + wde.getMessage());
        }

        TreeMap<Integer, Integer> exportGUIDs = new TreeMap<Integer, Integer>();
        Collection<Object> guids = new ArrayList<Object>();

        if (group != null) {

            contactBean.addReferenceGUID(group.getGUID());

            dataLogger.debug("Group type: " + group.getObjectType());
            contactBean.setObjectType(group.getObjectType());

            if (group.getItems() != null) {

                for (String key : group.getItems().keySet()) {
                    ItemBean item = group.getItems().get(key);
                    if (item.display(contactBean.getStartDate(), contactBean.getEndDate())) {
                        exportGUIDs.put(item.getObject2GUID(), item.getObject2GUID());
                    }
                }
                for (Integer guid : exportGUIDs.keySet()) {
                    guids.add(guid);
                }
            }
        }

        BulkContactBean bulkContact = performSearch(contactBean, guids, user, false);

        return bulkContact;
    }

    /**
     * Prepare the bulk contact bean.
     *
     * @param contactBean the bulk contact bean
     * @param financialSummary the financial summary
     * @param user the user
     * @return the bulk contact bean
     */
    public final BulkContactBean prepare(final BulkContactBean contactBean,
            final FinancialAnalysisCore financialSummary, final UserBean user) {

        BulkContactBean bulkContact = contactBean.clone();

        BuilderBean loadDetails = new BuilderBean();
        loadDetails.setParameter("EMAIL", true);
        loadDetails.setParameter("EMAIL_CLASS", bulkContact.getContactClass());
        loadDetails.setParameter("EMAIL_TYPE", bulkContact.getContactType());

        if (financialSummary.getPeople() != null) {

            // Build a Collection of guids
            Collection<Object> guids = new ArrayList<Object>();
            for (Integer guid : financialSummary.getPeople().keySet()) {
                guids.add(guid);
            }

            if (guids.size() > 0) {
                // Perform a person search to load those associated with it
                SearchResultsBean results = null;

                try {
                    SearchBean search = searchDAO.initiate("person", user);
                    search.setLimit(0);
                    search.setSearchArray(guids, "People in financial summary");

                    results = searchDAO.search(search, loadDetails);

                } catch (WhichDoctorSearchDaoException wdse) {
                    dataLogger.error("Error performing financial summary contact search: " + wdse.getMessage());
                }

                if (results != null && results.getSearchResults() != null) {
                    for (Object objPerson : results.getSearchResults()) {
                        PersonBean person = (PersonBean) objPerson;
                        bulkContact.addPerson(person);
                    }
                }
            }
        }

        if (financialSummary.getOrganisations() != null) {

            /** Build a Collection of guids **/
            Collection<Object> guids = new ArrayList<Object>();
            for (Integer guid : financialSummary.getOrganisations().keySet()) {
                guids.add(guid);
            }

            if (guids.size() > 0) {
                // Perform an organisation search to load those associated with it
                SearchResultsBean results = null;

                try {
                    SearchBean search = searchDAO.initiate("organisation", user);
                    search.setLimit(0);
                    search.setSearchArray(guids, "Organisations in financial summary");

                    results = searchDAO.search(search, loadDetails);

                } catch (WhichDoctorSearchDaoException wdse) {
                    dataLogger.error("Error performing financial summary contact search: " + wdse.getMessage());
                }

                if (results != null && results.getSearchResults() != null) {
                    for (Object objOrganisation : results.getSearchResults()) {
                        OrganisationBean organisation = (OrganisationBean) objOrganisation;
                        bulkContact.addOrganisation(organisation);
                    }
                }
            }
        }
        return bulkContact;
    }

    /**
     * Refresh the bulk contact bean.
     *
     * @param contactBean the bulk contact bean
     * @param user the user
     * @return the bulk contact bean
     */
    public final BulkContactBean refresh(final BulkContactBean contactBean, final UserBean user) {

        BulkContactBean bulkContact = contactBean.clone();

        Collection<Integer> peopleCollection = bulkContact.getPeopleGUIDs();
        Collection<Integer> organisationsCollection = bulkContact.getOrganisationGUIDs();

        /** Reset the BulkContactBean's people/organisation objects **/
        bulkContact.clearResults();

        BuilderBean loadDetails = new BuilderBean();
        loadDetails.setParameter("EMAIL", true);
        loadDetails.setParameter("EMAIL_CLASS", bulkContact.getContactClass());
        loadDetails.setParameter("EMAIL_TYPE", bulkContact.getContactType());

        if (peopleCollection.size() > 0) {
            /** At least one person is defined perform search **/
            Collection<Object> people = new ArrayList<Object>();
            for (Integer guid : peopleCollection) {
                people.add(guid);
            }
            SearchResultsBean results = null;
            try {
                SearchBean search = searchDAO.initiate("person", user);
                search.setSearchArray(people, "Bulk people contacts");
                search.setLimit(0);

                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact person search: " + wdse.getMessage());
            }

            if (results != null && results.getSearchResults() != null) {
                /** Iterate through the person results **/
                for (Object objPerson : results.getSearchResults()) {
                    PersonBean person = (PersonBean) objPerson;
                    bulkContact.addPerson(person);
                }
            }
        }

        if (organisationsCollection.size() > 0) {
            /** At least one organisation is defined perform search **/
            Collection<Object> organisations = new ArrayList<Object>();
            for (Integer guid : organisationsCollection) {
                organisations.add(guid);
            }
            SearchResultsBean results = null;
            try {
                SearchBean search = searchDAO.initiate("organisation", user);
                search.setSearchArray(organisations, "Bulk organisation contacts");
                search.setLimit(0);

                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact organisation search: " + wdse.getMessage());
            }

            if (results != null && results.getSearchResults() != null) {
                /** Iterate through the organisation results **/
                for (Object objOrganisation : results.getSearchResults()) {
                    OrganisationBean organisation = (OrganisationBean) objOrganisation;
                    bulkContact.addOrganisation(organisation);
                }
            }
        }
        return bulkContact;
    }

    /**
     * Load the contact.
     *
     * @param bulkContact the bulk contact bean
     * @param loadDetails the load details
     * @param personGUID the person guid
     * @param organisationGUID the organisation guid
     * @return the bulk contact bean
     */
    private BulkContactBean loadContact(final BulkContactBean bulkContact, final BuilderBean loadDetails,
            final int personGUID, final int organisationGUID) {

        if (personGUID > 0 && !bulkContact.containsPerson(personGUID)) {
            try {
                PersonBean person = personDAO.loadGUID(personGUID, loadDetails);
                bulkContact.addPerson(person);
            } catch (WhichDoctorDaoException wde) {
                dataLogger.error("Error loading bulk contact person: " + wde.getMessage());
            }
        }
        if (organisationGUID > 0 && !bulkContact.containsOrganisation(organisationGUID)) {
            try {
                OrganisationBean organisation = organisationDAO.loadGUID(organisationGUID, loadDetails);
                bulkContact.addOrganisation(organisation);
            } catch (WhichDoctorDaoException wde) {
                dataLogger.error("Error loading bulk contact organisation: " + wde.getMessage());
            }
        }
        return bulkContact;
    }

    /**
     * Perform a search for bulk contact items.
     *
     * @param contactBean the contact bean
     * @param guids the guids
     * @param user the user
     * @param addIds the add ids
     * @return the bulk contact bean
     */
    private BulkContactBean performSearch(final BulkContactBean contactBean, final Collection<Object> guids,
            final UserBean user, final boolean addIds) {

        BulkContactBean bulkContact = contactBean.clone();

        SearchHistoryBean history = new SearchHistoryBean();

        if (guids != null && guids.size() > 0) {
            if (StringUtils.equalsIgnoreCase(bulkContact.getObjectType(), "people")) {
                SearchBean search = searchDAO.initiate("person", user);
                search.setSearchArray(guids, "People identified in export");
                history.setPersonSearch(search);
            }
            if (StringUtils.equalsIgnoreCase(bulkContact.getObjectType(), "organisations")) {
                SearchBean search = searchDAO.initiate("organisation", user);
                search.setSearchArray(guids, "Organisations identified in export");
                history.setOrganisationSearch(search);
            }
            if (StringUtils.equalsIgnoreCase(bulkContact.getObjectType(), "debits")) {
                SearchBean search = searchDAO.initiate("debit", user);
                search.setSearchArray(guids, "Debits identified in export");
                history.setDebitSearch(search);
            }
            if (StringUtils.equalsIgnoreCase(bulkContact.getObjectType(), "credits")) {
                SearchBean search = searchDAO.initiate("credit", user);
                search.setSearchArray(guids, "Credits identified in export");
                history.setCreditSearch(search);
            }
            if (StringUtils.equalsIgnoreCase(bulkContact.getObjectType(), "reimbursements")) {
                SearchBean search = searchDAO.initiate("reimbursement", user);
                search.setSearchArray(guids, "Reimbursements identified in export");
                history.setReimbursementSearch(search);
            }
            if (StringUtils.equalsIgnoreCase(bulkContact.getObjectType(), "receipts")) {
                SearchBean search = searchDAO.initiate("receipt", user);
                search.setSearchArray(guids, "Receipts identified in export");
                history.setReceiptSearch(search);
            }
            if (StringUtils.equalsIgnoreCase(bulkContact.getObjectType(), "rotations")) {
                SearchBean search = searchDAO.initiate("rotation", user);
                search.setSearchArray(guids, "Rotations identified in export");
                history.setRotationSearch(search);
            }

            bulkContact = performSearch(bulkContact, history, addIds);
        }

        return bulkContact;
    }

    /**
     * Perform a search for bulk contact items.
     *
     * @param contactBean the contact bean
     * @param searchHistory the search history
     * @param addIds the add ids
     * @return the bulk contact bean
     */
    private BulkContactBean performSearch(final BulkContactBean contactBean, final SearchHistoryBean searchHistory,
            final boolean addIds) {

        BulkContactBean bc = contactBean.clone();

        SearchResultsBean results = null;

        BuilderBean loadDetails = new BuilderBean();
        loadDetails.setParameter("EMAIL", true);
        loadDetails.setParameter("EMAIL_CLASS", bc.getContactClass());
        loadDetails.setParameter("EMAIL_TYPE", bc.getContactType());

        if (StringUtils.equalsIgnoreCase(bc.getObjectType(), "people") && searchHistory.getPersonSearch() != null) {

            SearchBean search = searchHistory.getPersonSearch().clone();
            search.setLimit(0);
            try {
                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact search: " + wdse.getMessage());
            }
            if (results != null && results.getSearchResults() != null) {
                for (Object objPerson : results.getSearchResults()) {
                    PersonBean person = (PersonBean) objPerson;
                    if (addIds) {
                        bc.addReferenceGUID(person.getGUID());
                    }
                    bc.addPerson(person);
                }
            }
        }

        if (StringUtils.equalsIgnoreCase(bc.getObjectType(), "organisations")
                && searchHistory.getOrganisationSearch() != null) {

            SearchBean search = searchHistory.getOrganisationSearch().clone();
            search.setLimit(0);
            try {
                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact search: " + wdse.getMessage());
            }
            if (results != null && results.getSearchResults() != null) {
                for (Object objOrganisation : results.getSearchResults()) {
                    OrganisationBean organisation = (OrganisationBean) objOrganisation;
                    if (addIds) {
                        bc.addReferenceGUID(organisation.getGUID());
                    }
                    bc.addOrganisation(organisation);
                }
            }
        }

        if (StringUtils.equalsIgnoreCase(bc.getObjectType(), "debits") && searchHistory.getDebitSearch() != null) {

            SearchBean search = searchHistory.getDebitSearch().clone();
            search.setLimit(0);
            try {
                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact search: " + wdse.getMessage());
            }
            if (results != null && results.getSearchResults() != null) {
                for (Object objDebit : results.getSearchResults()) {
                    DebitBean debit = (DebitBean) objDebit;
                    if (addIds) {
                        bc.addReferenceGUID(debit.getGUID());
                    }
                    bc = loadContact(bc, loadDetails, debit.getPersonId(), debit.getOrganisationId());
                }
            }
        }

        if (StringUtils.equalsIgnoreCase(bc.getObjectType(), "credits")
                && searchHistory.getCreditSearch() != null) {

            SearchBean search = searchHistory.getCreditSearch().clone();
            search.setLimit(0);
            try {
                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact search: " + wdse.getMessage());
            }
            if (results != null && results.getSearchResults() != null) {
                for (Object objCredit : results.getSearchResults()) {
                    CreditBean credit = (CreditBean) objCredit;
                    if (addIds) {
                        bc.addReferenceGUID(credit.getGUID());
                    }
                    bc = loadContact(bc, loadDetails, credit.getPersonId(), credit.getOrganisationId());
                }
            }
        }

        if (StringUtils.equalsIgnoreCase(bc.getObjectType(), "reimbursements")
                && searchHistory.getReimbursementSearch() != null) {

            SearchBean search = searchHistory.getReimbursementSearch().clone();
            search.setLimit(0);
            try {
                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact search: " + wdse.getMessage());
            }
            if (results != null && results.getSearchResults() != null) {
                for (Object objReimbursement : results.getSearchResults()) {
                    ReimbursementBean reimbursement = (ReimbursementBean) objReimbursement;
                    if (addIds) {
                        bc.addReferenceGUID(reimbursement.getGUID());
                    }
                    bc = loadContact(bc, loadDetails, reimbursement.getPersonId(),
                            reimbursement.getOrganisationId());
                }
            }
        }

        if (StringUtils.equalsIgnoreCase(bc.getObjectType(), "receipts")
                && searchHistory.getReceiptSearch() != null) {

            SearchBean search = searchHistory.getReceiptSearch().clone();
            search.setLimit(0);
            try {
                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact search: " + wdse.getMessage());
            }
            if (results != null && results.getSearchResults() != null) {
                for (Object objReceipt : results.getSearchResults()) {
                    ReceiptBean receipt = (ReceiptBean) objReceipt;
                    if (addIds) {
                        bc.addReferenceGUID(receipt.getGUID());
                    }
                    bc = loadContact(bc, loadDetails, receipt.getPersonId(), receipt.getOrganisationId());
                }
            }
        }

        if (StringUtils.equalsIgnoreCase(bc.getObjectType(), "rotations")
                && searchHistory.getRotationSearch() != null) {

            SearchBean search = searchHistory.getRotationSearch().clone();
            search.setLimit(0);
            try {
                results = searchDAO.search(search, loadDetails);
            } catch (WhichDoctorSearchDaoException wdse) {
                dataLogger.error("Error performing bulk contact search: " + wdse.getMessage());
            }
            if (results != null && results.getSearchResults() != null) {
                for (Object objRotation : results.getSearchResults()) {
                    RotationBean rotation = (RotationBean) objRotation;
                    if (addIds) {
                        bc.addReferenceGUID(rotation.getGUID());
                    }
                    bc = loadContact(bc, loadDetails, rotation.getPersonId(), 0);
                }
            }
        }

        return bc;
    }
}