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

Java tutorial

Introduction

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

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.sfs.whichdoctor.analysis.FinancialSummaryAnalysisDAO;
import com.sfs.whichdoctor.analysis.WhichDoctorAnalysisDaoException;
import com.sfs.whichdoctor.beans.FinancialSummaryBean;
import com.sfs.whichdoctor.beans.PersonBean;
import com.sfs.whichdoctor.beans.OrganisationBean;

/**
 * This class updates the search index which is used to perform fast lookups.
 *
 * @author David Harrison
 */
public class SearchIndexDAOImpl extends BaseDAOImpl implements SearchIndexDAO {

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

    /** The financial summary analysis dao. */
    @Resource
    private FinancialSummaryAnalysisDAO financialSummaryAnalysisDAO;

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

    @Resource
    private OrganisationDAO organisationDAO;

    /**
     * Update the search index integer value.
     *
     * @param guid the guid
     * @param indextype the indextype
     * @param objectTypeId1 the object type id1
     * @param objectTypeId2 the object type id2
     * @param value the value
     *
     * @return true, if successful
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final boolean update(final int guid, final String indextype, final int objectTypeId1,
            final int objectTypeId2, final int value) throws WhichDoctorDaoException {

        boolean success = false;

        try {
            this.getJdbcTemplateWriter().update(this.getSQL().getValue("searchindex/numeric"),
                    new Object[] { guid, indextype, objectTypeId1, objectTypeId2, value, value });

            success = true;

        } catch (DataAccessException de) {
            dataLogger.error("Error updating index: " + de.getMessage());
        }
        return success;
    }

    /**
     * Update the search index double value.
     *
     * @param guid the guid
     * @param indextype the indextype
     * @param objectTypeId1 the object type id1
     * @param objectTypeId2 the object type id2
     * @param value the value
     *
     * @return true, if successful
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final boolean update(final int guid, final String indextype, final int objectTypeId1,
            final int objectTypeId2, final double value) throws WhichDoctorDaoException {

        boolean success = false;

        try {
            this.getJdbcTemplateWriter().update(this.getSQL().getValue("searchindex/currency"),
                    new Object[] { guid, indextype, objectTypeId1, objectTypeId2, value, value });

            success = true;

        } catch (DataAccessException de) {
            dataLogger.error("Error updating index: " + de.getMessage());
        }
        return success;
    }

    /**
     * Update the search index string value.
     *
     * @param guid the guid
     * @param indextype the indextype
     * @param objectTypeId1 the object type id1
     * @param objectTypeId2 the object type id2
     * @param value the value
     *
     * @return true, if successful
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final boolean update(final int guid, final String indextype, final int objectTypeId1,
            final int objectTypeId2, final String value) throws WhichDoctorDaoException {

        boolean success = false;

        try {
            this.getJdbcTemplateWriter().update(this.getSQL().getValue("searchindex/text"),
                    new Object[] { guid, indextype, objectTypeId1, objectTypeId2, value, value });

            success = true;

        } catch (DataAccessException de) {
            dataLogger.error("Error updating index: " + de.getMessage());
        }
        return success;
    }

    /**
     * Update the current balance index for the supplied guid/type combination.
     *
     * @param guid the guid
     * @param type the type
     *
     * @return true, if successful
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final boolean updateCurrentBalanceIndex(final int guid, final String type)
            throws WhichDoctorDaoException {

        boolean success = false;

        FinancialSummaryBean search = new FinancialSummaryBean();
        search.setLimit(1);
        search.setRequestedPage(1);
        search.setClosingDate(Calendar.getInstance().getTime());

        boolean performSearch = false;

        if (StringUtils.equalsIgnoreCase(type, "person")) {
            try {
                PersonBean person = this.personDAO.loadGUID(guid);
                search.addPerson(person);
                performSearch = true;
            } catch (WhichDoctorDaoException wde) {
                dataLogger.error("Error loading person: " + wde.getMessage());
            }
        }
        if (StringUtils.equalsIgnoreCase(type, "organisation")) {
            try {
                OrganisationBean org = this.organisationDAO.loadGUID(guid);
                search.addOrganisation(org);
                performSearch = true;
            } catch (WhichDoctorDaoException wde) {
                dataLogger.error("Error loading organisation: " + wde.getMessage());
            }
        }

        if (performSearch) {
            // A person or organisation has been added, perform search
            FinancialSummaryBean results = null;

            try {
                results = this.financialSummaryAnalysisDAO.search(search);
            } catch (WhichDoctorAnalysisDaoException wdae) {
                dataLogger.error("Error loading financial summary results: " + wdae.getMessage());
            }

            if (results != null) {
                // A result was returned, update the index
                success = this.update(guid, "Current Balance", 0, 0, results.getClosingBalance());
            }
        }
        return success;
    }

    /**
     * Delete the search index value.
     *
     * @param guid the guid
     * @param indextype the indextype
     *
     * @return true, if successful
     *
     * @throws WhichDoctorDaoException the which doctor dao exception
     */
    public final boolean delete(final int guid, final String indextype) throws WhichDoctorDaoException {

        boolean success = false;

        try {
            this.getJdbcTemplateWriter().update(this.getSQL().getValue("searchindex/delete"),
                    new Object[] { guid, indextype });

            success = true;

        } catch (DataAccessException de) {
            dataLogger.error("Error deleting index: " + de.getMessage());
        }
        return success;
    }
}