util.MysqlSearch.java Source code

Java tutorial

Introduction

Here is the source code for util.MysqlSearch.java

Source

/**
* Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org]
*
* This file is part of Redbasin OpenDocShare community project.
*
* Redbasin OpenDocShare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * This contains some search engine constraint building logic.
 *
 * @author Smitha Gudur (smitha@redbasinnetworks.com)
 * @version $Revision: 1.1 $
 */

package util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.StringTokenizer;
import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;

/**
 * This contains some search engine constraint building logic.
 *
 * @author Smitha Gudur (smitha@redbasinnetworks.com)
 * @version $Revision: 1.1 $
 */
public class MysqlSearch {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    /**
     * This method generates a constraint based on a set of column
     * name strings and an arbitrary expression.
     *
     * @param columns The list of all columns
     * @param expression The tag array for strip exclusion
     * @return String return the constraint
     */
    public String getConstraint(ArrayList columns, String expression) {
        logger.info("expression =" + expression);
        //String modexpr = expression.replaceAll("[[\\W]&&[\\S]]", "");

        // refer to this: http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
        // Match a single quote and replace it with single backslash followed by single quote for sql matching
        String modexpr = expression.replaceAll("'", "\\\\'");
        // Match a double quote and replace it with a single backslash followed by a double quote for sql matching
        modexpr = expression.replaceAll("\\\"", "\\\\\"");
        // Match a backslash and replace it with a double backslash for sql matching
        modexpr = expression.replaceAll("\\\\", "\\\\\\\\");
        // Match a percent and replace it with a backslash followed by percent for sql matching
        modexpr = expression.replaceAll("%", "\\\\%");
        // Match a hyphen and replace it with a backslash followed by hyphen for sql matching
        modexpr = expression.replaceAll("_", "\\\\_");

        StringBuffer constraint = new StringBuffer();
        List keywords = new ArrayList();
        StringTokenizer st = new StringTokenizer(modexpr, " ");
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            logger.info("token = " + token);
            //keywords.add(st.nextToken());
            keywords.add(token);
        }
        for (int i = 0; i < columns.size(); i++) {
            for (int j = 0; j < keywords.size(); j++) {
                if ((i != 0) && (j == 0)) {
                    constraint = constraint.append(" or ");
                }
                /*
                 if (j == 0) {
                 constraint = constraint.append("(");
                 }
                */
                if (j != 0) {
                    //constraint = constraint.append(" and ");
                    constraint = constraint.append(" or ");
                }

                // like is case insensitive, so dont need to change it
                constraint = constraint.append(" ").append((String) columns.get(i)).append(" like ").append("'%")
                        .append((String) keywords.get(j)).append("%'");
                /*
                 if (j == (keywords.size() -1)) {
                 constraint = constraint.append(")");
                 }
                */
            }
        }
        logger.info("replaceAll expression =" + constraint.toString());
        return constraint.toString();
    }

    public HashSet getUniqueTags(HashSet tagSet1, HashSet tagSet2) {
        if (tagSet1 != null && tagSet2 != null) {
            tagSet1.removeAll(tagSet2);
            tagSet1.addAll(tagSet2);
        }
        if (tagSet1 == null && tagSet2 != null) {
            return tagSet2;
        }
        return tagSet1;
    }

    /*
       public static void main(String[] args) {
           List columns = new ArrayList();
           columns.add("desc");
           columns.add("name");
           logger.info(MysqlSearch.getConstraint(columns, args[0]));
       }
    */

    /**
     * This method generates a constraint based on a set of column
     * name strings and an arbitrary expression.
     *
     * @param columns The list of all columns
     * @param expression The tag array for strip exclusion
     * @param sqlConstraint it could be either "or" or "and"
     * @return String return the constraint
     */
    public String getConstraint(ArrayList columns, String expression, String sqlConstraint) {
        logger.info("expression =" + expression);
        //String modexpr = expression.replaceAll("[[\\W]&&[\\S]]", "");

        // refer to this: http://dev.mysql.com/doc/refman/5.0/en/string-syntax.html
        // Match a single quote and replace it with single backslash followed by single quote for sql matching
        String modexpr = expression.replaceAll("'", "\\\\'");
        // Match a double quote and replace it with a single backslash followed by a double quote for sql matching
        modexpr = expression.replaceAll("\\\"", "\\\\\"");
        // Match a backslash and replace it with a double backslash for sql matching
        modexpr = expression.replaceAll("\\\\", "\\\\\\\\");
        // Match a percent and replace it with a backslash followed by percent for sql matching
        modexpr = expression.replaceAll("%", "\\\\%");
        // Match a hyphen and replace it with a backslash followed by hyphen for sql matching
        modexpr = expression.replaceAll("_", "\\\\_");

        StringBuffer constraint = new StringBuffer();
        List keywords = new ArrayList();
        StringTokenizer st = new StringTokenizer(modexpr, " ");
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            logger.info("token = " + token);
            //keywords.add(st.nextToken());
            keywords.add(token);
        }
        for (int i = 0; i < columns.size(); i++) {
            for (int j = 0; j < keywords.size(); j++) {
                if ((i != 0) && (j == 0)) {
                    //constraint = constraint.append(" or ");
                    constraint = constraint.append(sqlConstraint);
                }
                /*
                 if (j == 0) {
                 constraint = constraint.append("(");
                 }
                */
                if (j != 0) {
                    //constraint = constraint.append(" and ");
                    //constraint = constraint.append(" or ");
                    constraint = constraint.append(sqlConstraint);
                }

                // like is case insensitive, so dont need to change it
                constraint = constraint.append(" ").append((String) columns.get(i)).append(" like ").append("'%")
                        .append((String) keywords.get(j)).append("%'");
                /*
                 if (j == (keywords.size() -1)) {
                 constraint = constraint.append(")");
                 }
                */
            }
        }
        logger.info("replaceAll expression =" + constraint.toString());
        return constraint.toString();
    }
}