com.sfs.whichdoctor.search.sql.IsbMessageSqlHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.whichdoctor.search.sql.IsbMessageSqlHandler.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.sql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

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

import com.sfs.DataFilter;
import com.sfs.beans.BuilderBean;
import com.sfs.beans.UserBean;
import com.sfs.whichdoctor.beans.IsbMessageBean;
import com.sfs.whichdoctor.beans.SearchBean;
import com.sfs.whichdoctor.dao.IsbMessageDAO;

/**
 * The Class IsbMessageSqlHandler.
 *
 * @author David Harrison
 */
public class IsbMessageSqlHandler extends SqlHandlerBase {

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

    /** The Constant LIMIT. */
    private static final int LIMIT = 50;

    /** The isb message dao. */
    @Resource
    private IsbMessageDAO isbMessageDAO;

    /**
     * Instantiates a new isb message sql handler.
     */
    public IsbMessageSqlHandler() {
        super();
        this.setType("isbmessage");
        this.setIdentifierColumn("isbmessages.Id");
        this.setDefaultOrder("isbmessages.Created");
        this.setDefaultConnection(false);
    }

    /**
     * Initiate a SearchBean for the search type.
     *
     * @param user the user
     *
     * @return configured SearchBean
     */
    public final SearchBean initiate(final UserBean user) {

        SearchBean search = new SearchBean();
        IsbMessageBean searchCriteria = new IsbMessageBean();
        IsbMessageBean searchConstraints = new IsbMessageBean();

        search.setRequestedPage(1);
        search.setOrderColumn("isbmessages.Created");
        search.setOrderColumn2("isbmessages.Id");
        search.setLimit(LIMIT);
        search.setOrderAscending(false);
        search.setType("isbmessage");

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

        return search;
    }

    /**
     * Gets the group by.
     *
     * @return the group by
     */
    public final String getGroupBy() {
        return "";
    }

    /**
     * Gets the count sql.
     *
     * @return the count sql
     */
    public final String getCountSql() {
        return this.getSQL().getValue("isbmessage/count");
    }

    /**
     * Gets the select sql.
     *
     * @return the select sql
     */
    public final String getSelectSql() {
        return this.getSQL().getValue("isbmessage/search");
    }

    /**
     * Load the identified objects and return results as a Collection of
     * Objects.
     *
     * @param uniqueIds the unique ids
     * @param loadDetails the load details
     *
     * @return a Collection of Objects
     */
    public final Collection<Object> load(final Collection<Integer> uniqueIds, final BuilderBean loadDetails) {

        Collection<Object> results = new ArrayList<Object>();

        if (uniqueIds != null) {
            for (Integer uniqueId : uniqueIds) {
                try {
                    IsbMessageBean isbMessage = this.isbMessageDAO.load(uniqueId);
                    results.add(isbMessage);
                } catch (Exception e) {
                    dataLogger.error("Error loading ISB message (" + uniqueId + ") for search: " + e.getMessage());
                }
            }
        }
        return results;
    }

    /**
     * Construct the SQL string, description and parameters.
     *
     * @param objCriteria Object containing search criteria values
     * @param objConstraints Object containing search constraint values
     *
     * @return Map containing a String[] { sql, description } =>
     *         Collection< Object > parameters
     *
     * @throws IllegalArgumentException the illegal argument exception
     */
    public final Map<String[], Collection<Object>> construct(final Object objCriteria, final Object objConstraints)
            throws IllegalArgumentException {

        IsbMessageBean searchCriteria = null;

        if (objCriteria instanceof IsbMessageBean) {
            searchCriteria = (IsbMessageBean) objCriteria;
        }

        if (searchCriteria == null) {
            throw new IllegalArgumentException("The search criteria must be a " + "valid IsbMessageBean");
        }

        StringBuffer sqlWHERE = new StringBuffer();
        StringBuffer description = new StringBuffer();
        Collection<Object> parameters = new ArrayList<Object>();

        if (StringUtils.isNotBlank(searchCriteria.getTarget())) {
            String field = searchCriteria.getTarget();
            sqlWHERE.append(" AND isbmessages.IsbTarget =  ?");
            description.append(" and an ISB target of '" + searchCriteria.getTarget() + "'");
            parameters.add(field);
        }

        if (StringUtils.isNotBlank(searchCriteria.getAction())) {
            if (!StringUtils.equalsIgnoreCase(searchCriteria.getAction(), "Null")) {
                String field = searchCriteria.getAction();
                sqlWHERE.append(" AND isbmessages.Action =  ?");
                description.append(" and an ISB action of '" + searchCriteria.getAction() + "'");
                parameters.add(field);
            }
        }

        if (StringUtils.isNotBlank(searchCriteria.getIdentifier())) {
            String field = searchCriteria.getIdentifier();
            sqlWHERE.append(" AND isbmessages.Identifier =  ?");
            description.append(" and an ISB identifier of '" + searchCriteria.getIdentifier() + "'");
            parameters.add(field);
        }

        // Only select inbound messages
        if (StringUtils.equalsIgnoreCase(searchCriteria.getDescription(), "Inbound")) {
            sqlWHERE.append(" AND isbmessages.IsInbound = true");
            description.append(" and is an inbound ISB message");
        }

        // Only select inbound messages
        if (StringUtils.equalsIgnoreCase(searchCriteria.getDescription(), "Outbound")) {
            sqlWHERE.append(" AND isbmessages.IsInbound = false");
            description.append(" and is an outbound ISB message");
        }

        String[] index = new String[] { sqlWHERE.toString(), DataFilter.getHtml(description.toString()) };

        Map<String[], Collection<Object>> results = new HashMap<String[], Collection<Object>>();

        results.put(index, parameters);

        return results;
    }
}