Java tutorial
/******************************************************************************* * 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 com.sfs.DataFilter; import com.sfs.beans.BuilderBean; import com.sfs.beans.UserBean; import com.sfs.whichdoctor.beans.ReimbursementBean; import com.sfs.whichdoctor.beans.SearchBean; import com.sfs.whichdoctor.beans.TagBean; import com.sfs.whichdoctor.dao.ReimbursementDAO; import com.sfs.whichdoctor.search.TagSearchDAO; 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; /** * The Class ReimbursementSqlHandler. * * @author David Harrison */ public class ReimbursementSqlHandler extends SqlHandlerBase { /** The data logger. */ private static Logger dataLogger = Logger.getLogger(ReimbursementSqlHandler.class); /** The Constant LIMIT. */ private static final int LIMIT = 50; /** The tag search dao. */ @Resource private TagSearchDAO tagSearchDAO; /** The reimbursement dao. */ @Resource private ReimbursementDAO reimbursementDAO; /** * Instantiates a new reimbursement sql handler. */ public ReimbursementSqlHandler() { super(); this.setType("reimbursement"); this.setIdentifierColumn("reimbursement.GUID"); this.setDefaultOrder("ReimbursementNo"); } /** * 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(); ReimbursementBean searchCriteria = new ReimbursementBean(); ReimbursementBean searchConstraints = new ReimbursementBean(); searchCriteria.setCancelled(false); searchConstraints.setCancelled(true); /** * If user is not an Administrator or in the Finances group then they * shouldn't be able to view secured reimbursements. **/ if (user != null && !user.isFinancialUser()) { searchCriteria.setSecurity("Standard"); } search.setRequestedPage(1); search.setLimit(LIMIT); search.setOrderColumn("reimbursement.ReimbursementNo"); search.setOrderColumn2(""); search.setOrderColumn3(""); search.setOrderAscending(true); search.setType("reimbursement"); search.setSearchCriteria(searchCriteria); search.setSearchConstraints(searchConstraints); return search; } /** * Gets the group by. * * @return the group by */ public final String getGroupBy() { return " GROUP BY reimbursement.GUID "; } /** * Gets the count sql. * * @return the count sql */ public final String getCountSql() { return "SELECT count(DISTINCT reimbursement.GUID) " + this.getSQL().getValue("reimbursement/search"); } /** * Gets the select sql. * * @return the select sql */ public final String getSelectSql() { return "SELECT DISTINCT reimbursement.GUID," + " sum(expenseclaim.NetValue) AS ReimbursementTotalValue " + this.getSQL().getValue("reimbursement/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 { ReimbursementBean reimbursement = this.reimbursementDAO.loadGUID(uniqueId, loadDetails); results.add(reimbursement); } catch (Exception e) { dataLogger .error("Error loading reimbursement (" + 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 { ReimbursementBean searchCriteria = null; ReimbursementBean searchConstraints = null; if (objCriteria instanceof ReimbursementBean) { searchCriteria = (ReimbursementBean) objCriteria; } if (objConstraints instanceof ReimbursementBean) { searchConstraints = (ReimbursementBean) objConstraints; } if (searchCriteria == null) { throw new IllegalArgumentException("The search criteria must be a " + "valid ReimbursementBean"); } if (searchConstraints == null) { throw new IllegalArgumentException("The search constraints must be " + "a valid ReimbursementBean"); } StringBuffer sqlWHERE = new StringBuffer(); StringBuffer description = new StringBuffer(); Collection<Object> parameters = new ArrayList<Object>(); if (searchCriteria.getTags() != null) { try { for (TagBean tag : searchCriteria.getTags()) { Map<String[], Collection<Object>> results = this.tagSearchDAO.construct(tag, new TagBean()); for (String[] index : results.keySet()) { String tagWHERE = index[0]; String tagDescription = index[1]; Collection<Object> tagParameters = results.get(index); if (tagWHERE.compareTo("") != 0) { /* A WHERE condition is defined */ sqlWHERE.append(" " + this.getSQL().getValue("reimbursement/searchTags") + " WHERE " + tagWHERE + ")"); /* Add to the description and process the arrays */ description.append(tagDescription); if (tagParameters != null) { parameters.addAll(tagParameters); } } } } } catch (Exception e) { dataLogger.error("Error setting tag search options: " + e.getMessage()); } } if (searchCriteria.getBasicSearch() != null) { if (searchCriteria.getBasicSearch().compareTo("") != 0) { String searchString = searchCriteria.getBasicSearch().trim(); int basicSearch = 0; try { basicSearch = Integer.parseInt(searchCriteria.getBasicSearch()); } catch (NumberFormatException nfe) { dataLogger.debug("Error parsing BasicSearch: " + nfe.getMessage()); } if (basicSearch == 0) { // If the search string has : in the first ten characters // the financial // abbreviation is probably included - strip it out for // search purposes if (searchString.indexOf(": ") > 3 && searchString.indexOf(": ") < 10) { searchString = searchString.substring(3, searchString.length()); } String field = "%" + searchString + "%"; sqlWHERE.append(" AND (concat(reimbursement.ReimbursementNo, ': ', " + "reimbursement.Description) LIKE ?)"); description.append( " and a reimbursement description like '" + searchCriteria.getBasicSearch() + "'"); parameters.add(field); } else { sqlWHERE.append(" AND (reimbursement.ReimbursementNo LIKE ?)"); description.append(" and a reimbursement number of '" + searchCriteria.getBasicSearch() + "'"); parameters.add("%" + searchCriteria.getBasicSearch()); } } } if (searchCriteria.getNumber() != null) { boolean SearchConstraints = false; if (searchCriteria.getNumber().compareTo("") != 0) { if (searchConstraints.getNumber() != null) { if (searchConstraints.getNumber().compareTo("") != 0) { SearchConstraints = true; } } if (SearchConstraints) { if (searchConstraints.getNumber().compareTo("-") == 0) { // Less than Reimbursement specified sqlWHERE.append(" AND reimbursment.ReimbursementNo <= ?"); description.append( " and a reimbursement number less than '" + searchCriteria.getNumber() + "'"); parameters.add(searchCriteria.getNumber()); } else if (searchConstraints.getNumber().compareTo("+") == 0) { // Greater then Reimbursement specified sqlWHERE.append(" AND reimbursement.ReimbursementNo >= ?"); description.append( " and a reimbursement number greater than '" + searchCriteria.getNumber() + "'"); parameters.add(searchCriteria.getNumber()); } else { // Range between a and b - find whether greater than or // less than. int intA = 0; int intB = 0; try { intA = Integer.parseInt(searchCriteria.getNumber()); } catch (NumberFormatException nfe) { dataLogger.debug("Error parsing ReimbursementNo: " + nfe.getMessage()); } try { intB = Integer.parseInt(searchConstraints.getNumber()); } catch (NumberFormatException nfe) { dataLogger.debug("Error parsing ReimbursementNo: " + nfe.getMessage()); } if (intA == intB) { // criteria A and B are the same sqlWHERE.append(" AND reimbursement.ReimbursementNo LIKE ?"); description .append(" and a reimbursement number of '" + searchCriteria.getNumber() + "'"); parameters.add("%" + searchCriteria.getNumber()); } if (intA < intB) { // criteria A is less than B sqlWHERE.append(" AND reimbursement.ReimbursementNo BETWEEN ? AND ?"); description.append(" and a reimbursement number between '" + searchCriteria.getNumber() + "' and '" + searchConstraints.getNumber() + "'"); parameters.add(searchCriteria.getNumber()); parameters.add(searchConstraints.getNumber()); } if (intA > intB) { // Criteria A is greater than B sqlWHERE.append(" AND reimbursement.ReimbursementNo BETWEEN ? AND ?"); description.append(" and a reimbursement number between '" + searchConstraints.getNumber() + "' and '" + searchCriteria.getNumber() + "'"); parameters.add(searchConstraints.getNumber()); parameters.add(searchCriteria.getNumber()); } } } else { sqlWHERE.append(" AND reimbursement.ReimbursementNo LIKE ?"); description.append(" and a reimbursement number of '" + searchCriteria.getNumber() + "'"); parameters.add("%" + searchCriteria.getNumber()); } } } if (searchCriteria.getGUIDList() != null) { final StringBuffer guidWHERE = new StringBuffer(); for (String guid : searchCriteria.getGUIDList()) { if (StringUtils.isNotBlank(guid)) { guidWHERE.append(" OR reimbursement.GUID = ?"); parameters.add(guid); } } if (guidWHERE.length() > 0) { // Append the guidWHERE buffer to the sqlWHERE buffer sqlWHERE.append(" AND ("); // Append the guidWHERE but strip the first OR statement sqlWHERE.append(guidWHERE.toString().substring(4)); sqlWHERE.append(")"); description.append(" and has a GUID in the supplied list"); } } if (searchCriteria.getIdentifierList() != null) { final StringBuffer identifierWHERE = new StringBuffer(); for (String identifier : searchCriteria.getIdentifierList()) { if (StringUtils.isNotBlank(identifier)) { identifierWHERE.append(" OR reimbursement.ReimbursementNo LIKE ?"); parameters.add("%" + identifier); } } if (identifierWHERE.length() > 0) { // Append the identifierWHERE buffer to the sqlWHERE buffer sqlWHERE.append(" AND ("); // Append the identifierWHERE but strip the first OR statement sqlWHERE.append(identifierWHERE.toString().substring(4)); sqlWHERE.append(")"); description.append(" and has a reimbursement number " + "in the supplied list"); } } if (searchCriteria.getPersonId() > 0) { sqlWHERE.append(" AND reimbursement.PersonId = ?"); description.append(" and person GUID equal to '" + searchCriteria.getPersonId() + "'"); parameters.add(searchCriteria.getPersonId()); } if (searchCriteria.getOrganisationId() > 0) { sqlWHERE.append(" AND reimbursement.OrganisationId = ?"); description.append(" and organisation GUID equal to '" + searchCriteria.getOrganisationId() + "'"); parameters.add(searchCriteria.getOrganisationId()); } if (searchCriteria.getCancelled()) { if (searchConstraints.getCancelled()) { // Only cancelled sqlWHERE.append(" AND reimbursement.Cancelled = true"); description.append(" and the reimbursement is cancelled"); } } else { if (!searchConstraints.getCancelled()) { // Only active sqlWHERE.append(" AND reimbursement.Cancelled = false"); description.append(" and the reimbursement is not cancelled"); } } if (searchCriteria.getClassName() != null) { if (searchCriteria.getClassName().compareTo("") != 0) { sqlWHERE.append(" AND financialtype.Class LIKE ?"); description.append(" and a reimbursement class like '" + searchCriteria.getClassName() + "'"); parameters.add("%" + searchCriteria.getClassName() + "%"); } } if (searchCriteria.getTypeName() != null) { if (searchCriteria.getTypeName().compareTo("") != 0) { sqlWHERE.append(" AND financialtype.Name LIKE ?"); description.append(" and a reimbursement type like '" + searchCriteria.getTypeName() + "'"); parameters.add("%" + searchCriteria.getTypeName() + "%"); } } if (searchCriteria.getDescription() != null) { if (searchCriteria.getDescription().compareTo("") != 0) { if (searchCriteria.getDescription().indexOf("\"") > -1) { // Description contains "" so treat as a specific search sqlWHERE.append(" AND reimbursement.Description LIKE ?"); description.append( " and a reimbursement description like '" + searchCriteria.getDescription() + "'"); parameters.add(StringUtils.replace(searchCriteria.getDescription(), "\"", "")); } else { sqlWHERE.append(" AND reimbursement.Description LIKE ?"); description.append( " and a reimbursement description like '" + searchCriteria.getDescription() + "'"); parameters.add("%" + searchCriteria.getDescription() + "%"); } } } if (searchCriteria.getLocation() != null) { if (searchCriteria.getLocation().compareTo("") != 0) { if (searchCriteria.getLocation().indexOf("\"") > -1) { // Location contains "" so treat as a specific search sqlWHERE.append(" AND reimbursement.Location LIKE ?"); description.append(" and a location like '" + searchCriteria.getLocation() + "'"); parameters.add(StringUtils.replace(searchCriteria.getLocation(), "\"", "")); } else { sqlWHERE.append(" AND reimbursement.Location LIKE ?"); description.append(" and a location like '" + searchCriteria.getLocation() + "'"); parameters.add("%" + searchCriteria.getLocation() + "%"); } } } if (searchCriteria.getSecurity() != null) { if (searchCriteria.getSecurity().compareTo("") != 0) { sqlWHERE.append(" AND financialtype.Security = ?"); description.append(" and a security setting of '" + searchCriteria.getSecurity() + "'"); parameters.add(searchCriteria.getSecurity()); } } // Other searches: cancelled, date issued. if (searchCriteria.getIssued() != null) { if (searchConstraints.getIssued() != null) { int larger = searchCriteria.getIssued().compareTo(searchConstraints.getIssued()); if (larger > 0) { // SearchCriteria date after SearchConstraint date String fieldA = this.getDf().format(searchCriteria.getIssued()); String fieldB = this.getDf().format(searchConstraints.getIssued()); sqlWHERE.append(" AND reimbursement.Issued BETWEEN ? AND ?"); description.append(" and issued between '" + fieldB + "' and '" + fieldA + "'"); parameters.add(fieldB); parameters.add(fieldA); } if (larger < 0) { // SearchCriteria date before SearchConstraint date String fieldA = this.getDf().format(searchCriteria.getIssued()); String fieldB = this.getDf().format(searchConstraints.getIssued()); sqlWHERE.append(" AND reimbursement.Issued BETWEEN ? AND ?"); description.append(" and issued between '" + fieldA + "' and '" + fieldB + "'"); parameters.add(fieldA); parameters.add(fieldB); } if (larger == 0) { // SearchCritier and SearchConstraint are equal String field = this.getDf().format(searchCriteria.getIssued()); sqlWHERE.append(" AND reimbursement.Issued = ?"); description.append(" and issued on '" + field + "'"); parameters.add(field); } } else { String field = this.getDf().format(searchCriteria.getIssued()); sqlWHERE.append(" AND reimbursement.Issued = ?"); description.append(" and issued on '" + field + "'"); parameters.add(field); } } if (searchCriteria.getMeetingDate() != null) { if (searchConstraints.getMeetingDate() != null) { int larger = searchCriteria.getMeetingDate().compareTo(searchConstraints.getMeetingDate()); if (larger > 0) { // SearchCriteria date after SearchConstraint date String fieldA = this.getDf().format(searchCriteria.getMeetingDate()); String fieldB = this.getDf().format(searchConstraints.getMeetingDate()); sqlWHERE.append(" AND reimbursement.MeetingDate BETWEEN ? AND ?"); description.append(" and a meeting between '" + fieldB + "' and '" + fieldA + "'"); parameters.add(fieldB); parameters.add(fieldA); } if (larger < 0) { // SearchCriteria date before SearchConstraint date String fieldA = this.getDf().format(searchCriteria.getMeetingDate()); String fieldB = this.getDf().format(searchConstraints.getMeetingDate()); sqlWHERE.append(" AND reimbursement.MeetingDate BETWEEN ? AND ?"); description.append(" and a meeting between '" + fieldA + "' and '" + fieldB + "'"); parameters.add(fieldA); parameters.add(fieldB); } if (larger == 0) { // SearchCritier and SearchConstraint are equal String field = this.getDf().format(searchCriteria.getMeetingDate()); sqlWHERE.append(" AND reimbursement.MeetingDate = ?"); description.append(" and a meeting on '" + field + "'"); parameters.add(field); } } else { String field = this.getDf().format(searchCriteria.getMeetingDate()); sqlWHERE.append(" AND reimbursement.MeetingDate = ?"); description.append(" and a meeting on '" + field + "'"); parameters.add(field); } } if (searchCriteria.getCreatedDate() != null) { if (searchConstraints.getCreatedDate() != null) { int larger = searchCriteria.getCreatedDate().compareTo(searchConstraints.getCreatedDate()); if (larger > 0) { // SearchCriteria date after SearchConstraint date String fieldA = this.getDf().format(searchCriteria.getCreatedDate()); String fieldB = this.getDf().format(searchConstraints.getCreatedDate()); sqlWHERE.append(" AND guid.CreatedDate BETWEEN ? AND ?"); description.append(" and created between '" + fieldB + "' and '" + fieldA + "'"); parameters.add(fieldB); parameters.add(fieldA); } if (larger < 0) { // SearchCriteria date before SearchConstraint date String fieldA = this.getDf().format(searchCriteria.getCreatedDate()); String fieldB = this.getDf().format(searchConstraints.getCreatedDate()); sqlWHERE.append(" AND guid.CreatedDate BETWEEN ? AND ?"); description.append(" and created between '" + fieldB + "' and '" + fieldA + "'"); parameters.add(fieldA); parameters.add(fieldB); } if (larger == 0) { // SearchCritier and SearchConstraint are equal String field = this.getDf().format(searchCriteria.getCreatedDate()); sqlWHERE.append(" AND guid.CreatedDate = ?"); description.append(" and created on '" + field + "'"); parameters.add(field); } } else { String field = this.getDf().format(searchCriteria.getCreatedDate()); sqlWHERE.append(" AND guid.CreatedDate = ?"); description.append(" and created on '" + field + "'"); parameters.add(field); } } if (searchCriteria.getModifiedDate() != null) { if (searchConstraints.getModifiedDate() != null) { int larger = searchCriteria.getModifiedDate().compareTo(searchConstraints.getModifiedDate()); if (larger > 0) { // SearchCriteria date after SearchConstraint date String fieldA = this.getDf().format(searchCriteria.getModifiedDate()); String fieldB = this.getDf().format(searchConstraints.getModifiedDate()); sqlWHERE.append(" AND guid.ModifiedDate BETWEEN ? AND ?"); description.append(" and modified between '" + fieldB + "' and '" + fieldA + "'"); parameters.add(fieldB); parameters.add(fieldA); } if (larger < 0) { // SearchCriteria date before SearchConstraint date String fieldA = this.getDf().format(searchCriteria.getModifiedDate()); String fieldB = this.getDf().format(searchConstraints.getModifiedDate()); sqlWHERE.append(" AND guid.ModifiedDate BETWEEN ? AND ?"); description.append(" and modified between '" + fieldB + "' and '" + fieldA + "'"); parameters.add(fieldA); parameters.add(fieldB); } if (larger == 0) { // SearchCritier and SearchConstraint are equal String field = this.getDf().format(searchCriteria.getModifiedDate()); sqlWHERE.append(" AND guid.ModifiedDate = ?"); description.append(" and modified on '" + field + "'"); parameters.add(field); } } else { String field = this.getDf().format(searchCriteria.getModifiedDate()); sqlWHERE.append(" AND guid.ModifiedDate = ?"); description.append(" and modified on '" + field + "'"); parameters.add(field); } } if (searchCriteria.getIncludeGUIDList() != null) { final StringBuffer guidWHERE = new StringBuffer(); for (String guid : searchCriteria.getIncludeGUIDList()) { if (StringUtils.isNotBlank(guid)) { guidWHERE.append(" OR reimbursement.GUID = ?"); parameters.add(guid); } } if (guidWHERE.length() > 0) { // Append the guidWHERE buffer to the sqlWHERE buffer sqlWHERE.append(" OR ("); // Append the guidWHERE but strip the first OR statement sqlWHERE.append(guidWHERE.toString().substring(4)); sqlWHERE.append(")"); description.append(" and has a GUID in the supplied list"); } } 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; } }