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.beans; import java.util.ArrayList; import java.util.Collection; import java.util.TreeMap; import java.util.Date; import org.apache.commons.lang.StringUtils; import com.sfs.Formatter; import com.sfs.beans.UserBean; /** * The Class SearchBean. * * @author David Harrison 14th June 2003 */ public class SearchBean implements java.io.Serializable { /** The Constant serialVersionUID. */ private static final long serialVersionUID = 1L; /** The Constant DEFAULT_LIMIT. */ private static final int DEFAULT_LIMIT = 20; /** The Constant REMOVE_OR. */ private static final int REMOVE_OR = 4; /** The Constant REMOVE_AND. */ private static final int REMOVE_AND = 5; /** The Constant REMOVE_AND_NOT. */ private static final int REMOVE_AND_NOT = 9; /** The id. */ private int id; /** The dn. */ private String dn; /** The user name. */ private String userName; /** The saved search. */ private boolean savedSearch = false; /** The name. */ private String name; /** The display order. */ private int displayOrder; /** The type. */ private String type; /** The action. */ private String action; /** The search criteria. */ private Object searchCriteria; /** The search constraints. */ private Object searchConstraints; /** The search array. */ private Collection<Object> searchArray; /** The search array description. */ private String searchArrayDescription; /** The order column. */ private String orderColumn; /** The order column2. */ private String orderColumn2; /** The order column3. */ private String orderColumn3; /** The order ascending. */ private boolean orderAscending; /** The limit. */ private int limit = DEFAULT_LIMIT; /** The requested page. */ private int requestedPage; /** The search vectors. */ private TreeMap<Integer, Object[]> searchVectors = new TreeMap<Integer, Object[]>(); /** The created date. */ private Date createdDate; /** The public search. */ private boolean publicSearch = false; /** The favourite. */ private boolean favourite = false; /** The description. */ private String description; /** * Sets the id. * * @param idVal the new id */ public final void setId(final int idVal) { this.id = idVal; } /** * Gets the id. * * @return the id */ public final int getId() { return this.id; } /** * Sets the distinguished name. * * @param dnVal the new DN */ public final void setDN(final String dnVal) { this.dn = dnVal; } /** * Gets the distinguished name. * * @return the DN */ public final String getDN() { if (this.dn == null) { this.dn = ""; } return this.dn; } /** * Sets the user's name. * * @param userNameVal the new user name */ public final void setUserName(final String userNameVal) { this.userName = userNameVal; } /** * Gets the user's name. * * @return the user's name */ public final String getUserName() { if (this.userName == null) { this.userName = ""; } return this.userName; } /** * Sets the saved search. * * @param savedSearchVal the new saved search */ public final void setSavedSearch(final boolean savedSearchVal) { this.savedSearch = savedSearchVal; } /** * Gets the saved search. * * @return the saved search */ public final boolean getSavedSearch() { return this.savedSearch; } /** * Sets the name. * * @param nameVal the new name */ public final void setName(final String nameVal) { this.name = nameVal; } /** * Gets the name. * * @return the name */ public final String getName() { return this.name; } /** * Gets the search description. * * @return the search description */ public final String getSearchDescription() { String strDescription = ""; if (StringUtils.isNotBlank(this.name)) { strDescription = "a saved search named '" + this.name + "'"; } return strDescription; } /** * Sets the display order. * * @param displayOrderVal the new display order */ public final void setDisplayOrder(final int displayOrderVal) { this.displayOrder = displayOrderVal; } /** * Gets the display order. * * @return the display order */ public final int getDisplayOrder() { return this.displayOrder; } /** * Sets the type. * * @param typeVal the new type */ public final void setType(final String typeVal) { this.type = typeVal; } /** * Gets the type. * * @return the type */ public final String getType() { return this.type; } /** * Gets the search event. * * @return the search event */ public final String getSearchEvent() { String searchEvent = ""; if (this.type != null) { searchEvent = Formatter.toPlural(type) + "_search"; } return searchEvent.toLowerCase(); } /** * Sets the action. * * @param actionVal the new action */ public final void setAction(final String actionVal) { this.action = actionVal; } /** * Gets the action. * * @return the action */ public final String getAction() { if (this.action == null) { this.action = ""; } return this.action; } /** * Sets the search criteria. * * @param searchCriteriaRef the new search criteria */ public final void setSearchCriteria(final Object searchCriteriaRef) { this.searchCriteria = searchCriteriaRef; } /** * Gets the search criteria. * * @return the search criteria */ public final Object getSearchCriteria() { return this.searchCriteria; } /** * Sets the search constraints. * * @param searchConstraintsRef the new search constraints */ public final void setSearchConstraints(final Object searchConstraintsRef) { this.searchConstraints = searchConstraintsRef; } /** * Gets the search constraints. * * @return the search constraints */ public final Object getSearchConstraints() { return this.searchConstraints; } /** * Sets the search array. * * @param searchArrayVal the search array * @param descriptionVal the description */ public final void setSearchArray(final Collection<Object> searchArrayVal, final String descriptionVal) { this.searchArray = searchArrayVal; String strDescription = ""; if (StringUtils.isNotBlank(descriptionVal)) { strDescription = descriptionVal; } if (strDescription.startsWith(" and ")) { strDescription = strDescription.substring(REMOVE_AND); } this.searchArrayDescription = strDescription; } /** * Void search array. */ public final void voidSearchArray() { this.searchArray = null; } /** * Gets the search array. * * @return the search array */ public final Collection<Object> getSearchArray() { return this.searchArray; } /** * Gets the search array description. * * @return the search array description */ public final String getSearchArrayDescription() { if (this.searchArrayDescription == null) { this.searchArrayDescription = ""; } return this.searchArrayDescription; } /** * Sets the order column. * * @param orderColumnVal the new order column */ public final void setOrderColumn(final String orderColumnVal) { this.orderColumn = orderColumnVal; } /** * Gets the order column. * * @return the order column */ public final String getOrderColumn() { return this.orderColumn; } /** * Sets the order column2. * * @param orderColumn2Val the new order column2 */ public final void setOrderColumn2(final String orderColumn2Val) { this.orderColumn2 = orderColumn2Val; } /** * Gets the order column2. * * @return the order column2 */ public final String getOrderColumn2() { return this.orderColumn2; } /** * Sets the order column3. * * @param orderColumn3Val the new order column3 */ public final void setOrderColumn3(final String orderColumn3Val) { this.orderColumn3 = orderColumn3Val; } /** * Gets the order column3. * * @return the order column3 */ public final String getOrderColumn3() { return this.orderColumn3; } /** * Sets the order ascending. * * @param orderAscendingVal the new order ascending */ public final void setOrderAscending(final boolean orderAscendingVal) { this.orderAscending = orderAscendingVal; } /** * Gets the order ascending. * * @return the order ascending */ public final boolean getOrderAscending() { return this.orderAscending; } /** * Sets the created date. * * @param createdDateVal the new created date */ public final void setCreatedDate(final Date createdDateVal) { if (createdDateVal != null) { this.createdDate = new Date(createdDateVal.getTime()); } } /** * Gets the created date. * * @return the created date */ public final Date getCreatedDate() { return this.createdDate; } /** * Sets the limit. * * @param limitVal the new limit */ public final void setLimit(final int limitVal) { this.limit = limitVal; } /** * Gets the limit. * * @return the limit */ public final int getLimit() { return this.limit; } /** * Sets the public search. * * @param publicSearchVal the new public search */ public final void setPublicSearch(final boolean publicSearchVal) { this.publicSearch = publicSearchVal; } /** * Gets the public search. * * @return the public search */ public final boolean getPublicSearch() { return this.publicSearch; } /** * Sets the favourite. * * @param favouriteVal the new favourite */ public final void setFavourite(final boolean favouriteVal) { this.favourite = favouriteVal; } /** * Gets the favourite. * * @return the favourite */ public final boolean getFavourite() { return this.favourite; } /** * Sets the description. * * @param descriptionVal the new description */ public final void setDescription(final String descriptionVal) { this.description = descriptionVal; } /** * Gets the description. * * @return the description */ public final String getDescription() { return this.description; } /** * Sets the requested page. * * @param requestedPageVal the new requested page */ public final void setRequestedPage(final int requestedPageVal) { this.requestedPage = requestedPageVal; } /** * Gets the requested page. * * @return the requested page */ public final int getRequestedPage() { return this.requestedPage; } /** * Where searchVector Object[] values are: object[0] = (String) sql * object[1] = (String) action object[2] = (Collection< Object >) parameters * object[3] = (String) description. * * @param searchVectorsVal the search vectors */ public final void setSearchVectors(final TreeMap<Integer, Object[]> searchVectorsVal) { this.searchVectors = searchVectorsVal; } /** * Gets the search vectors. * * @return the search vectors */ public final TreeMap<Integer, Object[]> getSearchVectors() { return this.searchVectors; } /** * Add vector to search array. * * @param actionVal the action * @param searchParameters the search parameters * @param sql the sql * @param descriptionString the description string */ public final void setSQLWhereComponents(final String sql, final String actionVal, final Collection<Object> searchParameters, final String descriptionString) { if (StringUtils.isNotBlank(sql)) { String sqlWhere = sql; if (sql.startsWith(" AND ")) { sqlWhere = sql.substring(REMOVE_AND); } String strDescription = ""; if (StringUtils.isNotBlank(descriptionString)) { strDescription = descriptionString; if (descriptionString.startsWith(" and ")) { strDescription = descriptionString.substring(REMOVE_AND); } } Object[] vector = new Object[] { sqlWhere, actionVal, searchParameters, strDescription }; if (this.searchVectors == null) { this.searchVectors = new TreeMap<Integer, Object[]>(); } int index = this.searchVectors.size(); this.searchVectors.put(index, vector); } } /** * Removes the search vector. * * @param vectorindex the vectorindex */ public final void removeSearchVector(final int vectorindex) { TreeMap<Integer, Object[]> newVectors = new TreeMap<Integer, Object[]>(); if (this.searchVectors != null) { for (Integer index : this.searchVectors.keySet()) { Object[] vector = this.searchVectors.get(index); if (vectorindex != index) { /* Vector is not getting removed add to new vector map */ Integer newIndex = new Integer(newVectors.size()); newVectors.put(newIndex, vector); } } } setSearchVectors(newVectors); } /** * Gets the sQL where statement. * * @return the sQL where statement */ public final String getSQLWhereStatement() { String sql = ""; if (this.searchVectors != null) { for (Integer index : this.searchVectors.keySet()) { Object[] vector = this.searchVectors.get(index); String sqlVector = ""; if (vector != null) { /* Get sql string */ sqlVector = (String) vector[0]; String strAction = (String) vector[1]; if (StringUtils.equalsIgnoreCase(strAction, "Subtract")) { sqlVector = " AND NOT (" + sqlVector + ")"; } if (StringUtils.equalsIgnoreCase(strAction, "Subsearch")) { sqlVector = " AND (" + sqlVector + ")"; } if (StringUtils.equalsIgnoreCase(strAction, "Add")) { sqlVector = " OR (" + sqlVector + ")"; } } if (StringUtils.isNotBlank(sql)) { /* Check to see if operator is applied, if not add a default */ /* AND operator */ if (!sqlVector.startsWith(" AND ") && !sqlVector.startsWith(" OR ")) { sqlVector = " AND " + sqlVector; } /* Check to see if sql begins with an operator, if so remove */ if (sql.startsWith(" AND NOT ")) { sql = sql.substring(REMOVE_AND_NOT); } if (sql.startsWith(" AND ")) { sql = sql.substring(REMOVE_AND); } if (sql.startsWith(" OR ")) { sql = sql.substring(REMOVE_OR); } sql = "(" + sql + sqlVector + ")"; } else { sql = sqlVector; } } } return sql; } /** * Gets the search sql. * * @return the search sql */ public final String getSearchSQL() { String sqlStatement = ""; String sql = getSQLWhereStatement(); if (StringUtils.isNotBlank(sql)) { // Trim statement if required if (sql.startsWith(" AND ")) { sql = sql.substring(REMOVE_AND); } if (sql.startsWith(" OR ")) { sql = sql.substring(REMOVE_OR); } sqlStatement = " AND " + sql; } return sqlStatement; } /** * Gets the search parameters. * * @return the search parameters */ @SuppressWarnings("unchecked") public final Collection<Object> getSearchParameters() { ArrayList<Object> parameters = new ArrayList<Object>(); if (this.searchVectors != null) { for (Integer index : this.searchVectors.keySet()) { Object[] vector = this.searchVectors.get(index); if (vector[2] != null) { Collection<Object> vectorParameters = (Collection<Object>) vector[2]; if (vectorParameters != null) { parameters.addAll(vectorParameters); } } } } return parameters; } /** * Gets the sQL array statement. * * @param idField the id field * * @return the sQL array statement */ public final String getSQLArrayStatement(final String idField) { StringBuffer sqlWhere = new StringBuffer(); if (this.searchArray != null) { sqlWhere.append(" AND ("); for (int i = 0; i < this.searchArray.size(); i++) { if (i > 0) { sqlWhere.append(" OR "); } sqlWhere.append(idField); sqlWhere.append(" = ?"); } sqlWhere.append(")"); } return sqlWhere.toString(); } /** * Clear previous search. */ public final void clearPreviousSearch() { this.action = null; this.searchCriteria = null; this.searchConstraints = null; this.searchArray = null; } /** * Reset search. */ public final void resetSearch() { this.action = ""; this.searchCriteria = null; this.searchConstraints = null; this.searchVectors = new TreeMap<Integer, Object[]>(); } /** * Modify action. * * @param user the user * * @return true, if successful */ public final boolean modifyAction(final UserBean user) { boolean modify = false; if (user != null) { if (this.getPublicSearch()) { modify = true; } if (StringUtils.equals(user.getDN(), this.getDN())) { modify = true; } if (user.isMemberOf("Administrator")) { modify = true; } } return modify; } /** * Delete action. * * @param user the user * * @return true, if successful */ public final boolean deleteAction(final UserBean user) { boolean delete = false; if (user != null) { if (this.getPublicSearch()) { delete = true; } if (StringUtils.equals(user.getDN(), this.getDN())) { delete = true; } if (user.isMemberOf("Administrator")) { delete = true; } } return delete; } /** * Clone the search bean. */ public final SearchBean clone() { SearchBean search = new SearchBean(); search.setId(this.id); search.setDN(this.dn); search.setUserName(this.userName); search.setSavedSearch(this.savedSearch); search.setName(this.name); search.setDisplayOrder(this.displayOrder); search.setType(this.type); search.setAction(this.action); search.setSearchCriteria(this.searchCriteria); search.setSearchConstraints(this.searchConstraints); search.setSearchArray(this.searchArray, this.searchArrayDescription); search.setOrderColumn(this.orderColumn); search.setOrderColumn2(this.orderColumn2); search.setOrderColumn3(this.orderColumn3); search.setOrderAscending(this.orderAscending); search.setLimit(this.limit); search.setRequestedPage(this.requestedPage); search.setSearchVectors(this.searchVectors); search.setCreatedDate(this.createdDate); search.setPublicSearch(this.publicSearch); search.setFavourite(this.favourite); search.setDescription(this.description); return search; } }