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  package com.gisgraphy.domain.repository;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import javax.persistence.PersistenceException;
29  
30  import org.hibernate.Query;
31  import org.hibernate.Session;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.springframework.orm.hibernate3.HibernateCallback;
35  import org.springframework.stereotype.Repository;
36  import org.springframework.util.Assert;
37  
38  import com.gisgraphy.domain.geoloc.entity.ZipCode;
39  import com.gisgraphy.geoloc.ZipcodeNormalizer;
40  
41  /**
42   * A data access object for {@link ZipCode} Object
43   * 
44   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
45   */
46  @Repository
47  public class ZipCodeDao extends GenericDao<ZipCode, Long> implements IZipCodeDao
48  {
49      
50      //CREATE INDEX communes_idxr ON zipcode("code" text_pattern_ops);
51  	
52     /**
53       * The logger
54       */
55      protected static final Logger logger = LoggerFactory
56  	    .getLogger(ZipCodeDao.class);
57  	
58      /**
59       * Default constructor
60       */
61      public ZipCodeDao() {
62  	    super(ZipCode.class);
63      }
64      
65  
66  
67  	/* (non-Javadoc)
68  	 * @see com.gisgraphy.domain.repository.IZipCodeDao#getByCodeAndCountry(java.lang.String, java.lang.String)
69  	 */
70  	@SuppressWarnings("unchecked")
71  	public List<ZipCode> getByCodeAndCountry(final String code, final String countryCode) {
72  		Assert.notNull(code);
73  		Assert.notNull(countryCode);
74  		return (List<ZipCode>) this.getHibernateTemplate().execute(
75  			new HibernateCallback() {
76  
77  			    public Object doInHibernate(Session session)
78  				    throws PersistenceException {
79  				String queryString = "from "
80  					+ getPersistenceClass().getSimpleName()
81  					+ " as z where z.code= ? and z.gisFeature.countryCode= ?";
82  				
83  
84  				Query qry = session.createQuery(queryString);
85  				qry.setCacheable(true);
86  
87  				qry.setParameter(0, code.toUpperCase());
88  				qry.setParameter(1, countryCode.toUpperCase());
89  
90  				List<ZipCode> result = (List<ZipCode>) qry.list();
91  				return result==null? new ArrayList<ZipCode>():result;
92  			    }
93  			});
94  	}
95  	
96  	/* (non-Javadoc)
97  	 * @see com.gisgraphy.domain.repository.IZipCodeDao#getByCodeAndCountry(java.lang.String, java.lang.String)
98  	 */
99  	@SuppressWarnings("unchecked")
100 	public List<ZipCode> getByCodeAndCountrySmart(final String code, final String countryCode) {
101 		Assert.notNull(code);
102 		Assert.notNull(countryCode);
103 		String modifyCode = ZipcodeNormalizer.normalize(code, countryCode);
104 		 return getByCodeAndCountry(modifyCode, countryCode);
105 		 /*   return (List<ZipCode>) this.getHibernateTemplate().execute(
106 			    new HibernateCallback() {
107 				
108 				public Object doInHibernate(Session session)
109 				throws PersistenceException {
110 				    String queryString = "from "
111 					+ getPersistenceClass().getSimpleName()
112 					+ " as z where code like ?  and z.gisFeature.countryCode= ?";
113 				    
114 				    
115 				    Query qry = session.createQuery(queryString);
116 				    qry.setCacheable(true);
117 				    
118 				    qry.setParameter(0, code.trim().split(" ")[0].toUpperCase());
119 				    qry.setParameter(1, countryCode.toUpperCase());
120 				    
121 				    List<ZipCode> result = (List<ZipCode>) qry.list();
122 				    return result==null? new ArrayList<ZipCode>():result;
123 				}
124 			    });
125 		} else {
126 		    return getByCodeAndCountry(code, countryCode);
127 		}*/
128 	}
129 
130 
131 
132 	/* (non-Javadoc)
133 	 * @see com.gisgraphy.domain.repository.IZipCodeDao#listByCode(java.lang.String)
134 	 */
135 	@SuppressWarnings("unchecked")
136 	public List<ZipCode> listByCode(final String code) {
137 		Assert.notNull(code);
138 		return (List<ZipCode>) this.getHibernateTemplate().execute(
139 			new HibernateCallback() {
140 
141 			    public Object doInHibernate(Session session)
142 				    throws PersistenceException {
143 				String queryString = "from "
144 					+ getPersistenceClass().getSimpleName()
145 					+ " as z where z.code= ?";
146 				
147 
148 				Query qry = session.createQuery(queryString);
149 				qry.setCacheable(true);
150 
151 				qry.setParameter(0, code.toUpperCase());
152 
153 				List<ZipCode> result = (List<ZipCode>) qry.list();
154 				return result==null? new ArrayList<ZipCode>():result;
155 			    }
156 			});
157 	}
158 
159     
160   
161 
162 
163 	
164 }