com.gisgraphy.hibernate.criterion.FulltextRestrictionTest.java Source code

Java tutorial

Introduction

Here is the source code for com.gisgraphy.hibernate.criterion.FulltextRestrictionTest.java

Source

/*******************************************************************************
 * Gisgraphy Project 
 *  
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *  
 *    This library 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
 *    Lesser General Public License for more details.
 *  
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
 *  
 *   Copyright 2008  Gisgraphy project 
 * 
 *   David Masclet <davidmasclet@gisgraphy.com>
 ******************************************************************************/
package com.gisgraphy.hibernate.criterion;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.PersistenceException;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projection;
import org.hibernate.transform.Transformers;
import org.junit.Test;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.gisgraphy.domain.geoloc.entity.OpenStreetMap;
import com.gisgraphy.domain.repository.IOpenStreetMapDao;
import com.gisgraphy.fulltext.AbstractIntegrationHttpSolrTestCase;
import com.gisgraphy.helper.GeolocHelper;
import com.gisgraphy.helper.StringHelper;
import com.gisgraphy.hibernate.projection.ProjectionBean;
import com.gisgraphy.hibernate.projection._OpenstreetmapDTO;
import com.gisgraphy.street.StreetType;
import com.gisgraphy.test._DaoHelper;
import com.vividsolutions.jts.geom.LineString;

public class FulltextRestrictionTest extends AbstractIntegrationHttpSolrTestCase {

    private _DaoHelper testDao;

    private IOpenStreetMapDao openStreetMapDao;

    private OpenStreetMap createOpenStreetMap() {
        OpenStreetMap streetOSM = new OpenStreetMap();
        LineString shape = GeolocHelper.createLineString("LINESTRING (30.001 30.001, 40 40)");
        streetOSM.setShape(shape);
        streetOSM.setGid(1L);
        streetOSM.setOpenstreetmapId(1234L);
        streetOSM.setOneWay(false);
        streetOSM.setStreetType(StreetType.FOOTWAY);
        streetOSM.setName("Champs-Elyses");
        streetOSM.setLocation(GeolocHelper.createPoint(30.001F, 40F));

        return StringHelper.updateOpenStreetMapEntityForIndexation(streetOSM);
    }

    @SuppressWarnings("unchecked")
    @Test
    public void testFulltextRestriction() {
        OpenStreetMap streetOSM = createOpenStreetMap();
        openStreetMapDao.save(streetOSM);
        assertNotNull(openStreetMapDao.get(streetOSM.getId()));

        int numberOfLineUpdated = openStreetMapDao.updateTS_vectorColumnForStreetNameSearch();

        assertEquals("It should have 1 lines updated : one for partial and one for fulltext", 1,
                numberOfLineUpdated);

        HibernateCallback hibernateCallback = new HibernateCallback() {

            public Object doInHibernate(Session session) throws PersistenceException {

                Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
                List<String> fieldList = new ArrayList<String>();
                fieldList.add("name");
                fieldList.add("gid");

                Projection projection = ProjectionBean.fieldList(fieldList, true);
                testCriteria.setProjection(projection)
                        .add(new FulltextRestriction(OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME,
                                "Champs elysees"))//case sensitive accent
                        .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

                List<_OpenstreetmapDTO> results = testCriteria.list();
                return results;
            }
        };

        List<_OpenstreetmapDTO> streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallback);
        assertEquals(
                "According to the fulltext restriction, it should have a result (it should be case insensitive,accent insensitive and '-' insensitive",
                1, streets.size());

        assertEquals("According to the fulltext restriction, the result is incorrect", streetOSM.getGid(),
                streets.get(0).getGid());

        HibernateCallback hibernateCallbackminusSign = new HibernateCallback() {

            public Object doInHibernate(Session session) throws PersistenceException {

                Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
                List<String> fieldList = new ArrayList<String>();
                fieldList.add("name");
                fieldList.add("gid");

                Projection projection = ProjectionBean.fieldList(fieldList, true);
                testCriteria.setProjection(projection)
                        .add(new FulltextRestriction(OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME,
                                "Champs-elysees"))//'-'
                        .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

                List<_OpenstreetmapDTO> results = testCriteria.list();
                return results;
            }
        };

        streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallbackminusSign);
        assertEquals("According to the fulltext restriction, it should have a result (it should be '-' insensitive",
                1, streets.size());

        assertEquals("According to the fulltext restriction, the result is incorrect", streetOSM.getGid(),
                streets.get(0).getGid());

        HibernateCallback hibernateCallbackZeroResult = new HibernateCallback() {

            public Object doInHibernate(Session session) throws PersistenceException {

                Criteria testCriteria = session.createCriteria(OpenStreetMap.class);
                List<String> fieldList = new ArrayList<String>();
                fieldList.add("name");
                fieldList.add("gid");

                Projection projection = ProjectionBean.fieldList(fieldList, true);
                testCriteria.setProjection(projection)
                        .add(new FulltextRestriction(OpenStreetMap.FULLTEXTSEARCH_VECTOR_PROPERTY_NAME,
                                "Champ elysees"))//wrong word
                        .setResultTransformer(Transformers.aliasToBean(_OpenstreetmapDTO.class));

                List<_OpenstreetmapDTO> results = testCriteria.list();
                return results;
            }
        };

        streets = (List<_OpenstreetmapDTO>) testDao.testCallback(hibernateCallbackZeroResult);
        assertEquals("According to the fulltext restriction, it should not have result ", 0, streets.size());

    }

    public void setOpenStreetMapDao(IOpenStreetMapDao openStreetMapDao) {
        this.openStreetMapDao = openStreetMapDao;
    }

    public void setTestDao(_DaoHelper testDao) {
        this.testDao = testDao;
    }

}