View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  /**
24   * $Id: SpatialRelateExpression.java 87 2008-02-25 19:43:14Z maesenka $ This
25   * work was partially supported by the European Commission, under the 6th
26   * Framework Programme, contract IST-2-004688-STP. This library is free
27   * software; you can redistribute it and/or modify it under the terms of the GNU
28   * Lesser General Public License as published by the Free Software Foundation;
29   * either version 2.1 of the License, or (at your option) any later version.
30   * This library is distributed in the hope that it will be useful, but WITHOUT
31   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
32   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
33   * details. You should have received a copy of the GNU Lesser General Public
34   * License along with this library; if not, write to the Free Software
35   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36   */
37  package com.gisgraphy.hibernate.criterion;
38  
39  import org.hibernate.Criteria;
40  import org.hibernate.EntityMode;
41  import org.hibernate.Hibernate;
42  import org.hibernate.HibernateException;
43  import org.hibernate.criterion.CriteriaQuery;
44  import org.hibernate.criterion.Criterion;
45  import org.hibernate.engine.TypedValue;
46  
47  import com.gisgraphy.helper.StringHelper;
48  
49  /**
50   * An implementation of the <code>Criterion</code> interface that implements
51   * Partial word search, it must be coupled with the DAO to have a common format
52   * =>the DAO store a special string and this criteria search 
53   * for that string (the searchterms have to be transform too)
54   * 
55   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
56   */
57  public class PartialWordSearchRestriction implements Criterion {
58  
59      	/**
60  	 * generated serial version id
61  	 */
62  	private static final long serialVersionUID = -1118549308820955458L;
63  	private String ts_vectorColumnName;
64  	private String searchedText;
65  
66  	/**
67       * @param ts_vectorColumnName The name of the ts_vector column
68       * @param searchedText the text to search
69       */
70      public PartialWordSearchRestriction(String ts_vectorColumnName, String searchedText) {
71      if (ts_vectorColumnName == null ){
72      	throw new IllegalArgumentException("the name of the ts_vector column can not be null");
73      }
74      if (searchedText == null){
75      	throw new IllegalArgumentException("the search text can not be null");
76      }
77  	this.ts_vectorColumnName = ts_vectorColumnName;
78  	this.searchedText = searchedText;
79      }
80  
81    
82      /*
83       * (non-Javadoc)
84       * 
85       * @see org.hibernate.criterion.Criterion#getTypedValues(org.hibernate.Criteria,
86       *      org.hibernate.criterion.CriteriaQuery)
87       */
88      public TypedValue[] getTypedValues(Criteria criteria,
89  	    CriteriaQuery criteriaQuery) throws HibernateException {
90  	return  new TypedValue[] { new TypedValue( Hibernate.STRING, StringHelper.transformStringForPartialWordSearch(searchedText,StringHelper.WHITESPACE_CHAR_DELIMITER), EntityMode.POJO )};
91  
92      }
93  
94      /*
95       * (non-Javadoc)
96       * 
97       * @see org.hibernate.criterion.Criterion#toSqlString(org.hibernate.Criteria,
98       *      org.hibernate.criterion.CriteriaQuery)
99       */
100     public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery)
101 	    throws HibernateException {
102 	String columnName = criteriaQuery.getColumn(criteria,
103 			ts_vectorColumnName);
104 	StringBuffer result = new StringBuffer("(").append(columnName).append(
105 			" @@ plainto_tsquery('simple',?)").append(")");
106 			return result.toString();
107 
108     }
109 
110 
111 }