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   *
25   */
26  package com.gisgraphy.domain.repository;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.persistence.PersistenceException;
32  
33  import org.hibernate.Query;
34  import org.hibernate.Session;
35  import org.springframework.orm.hibernate3.HibernateCallback;
36  import org.springframework.stereotype.Repository;
37  
38  import com.gisgraphy.domain.geoloc.entity.Country;
39  import com.gisgraphy.domain.repository.exception.DuplicateNameException;
40  
41  /**
42   * A data access object for {@link Country}
43   * 
44   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
45   */
46  @Repository
47  public class CountryDao extends GenericGisDao<Country> implements ICountryDao {
48  
49      /**
50       * Default Constructor
51       */
52      public CountryDao() {
53  	super(Country.class);
54      }
55  
56      /**
57       * @param country
58       *                the country to save
59       * @return The saved instance
60       * @throws DuplicateNameException
61       *                 If a country with the same name already exists in the
62       *                 datastore
63       */
64      @Override
65      public Country save(Country country) {
66  	if (country != null && country.getId() == null
67  		&& getByName(country.getName()) != null) {
68  	    throw new DuplicateNameException(
69  		    "A country with the specified name already exists : "
70  			    + country.getName());
71  	}
72  	return super.save(country);
73      }
74  
75      /*
76       * (non-Javadoc)
77       * 
78       * @see com.gisgraphy.domain.repository.ICountryDao#getByIso3166Alpha2Code(java.lang.String)
79       */
80      public Country getByIso3166Alpha2Code(final String iso3166Alpha2Code) {
81  	if (iso3166Alpha2Code == null) {
82  	    return null;
83  	}
84  	if (iso3166Alpha2Code.length() != 2) {
85  	    logger
86  		    .info("can not retrieve country with iso639Alpha2LanguageCode="
87  			    + iso3166Alpha2Code
88  			    + " : iso639Alpha2LanguageCode must have a length of 2 ");
89  	    return null;
90  	}
91  	return (Country) this.getHibernateTemplate().execute(
92  		new HibernateCallback() {
93  
94  		    public Object doInHibernate(Session session)
95  			    throws PersistenceException {
96  			String queryString = "from "
97  				+ Country.class.getSimpleName()
98  				+ " as c where c.iso3166Alpha2Code= ?";
99  
100 			Query qry = session.createQuery(queryString);
101 			qry.setCacheable(true);
102 
103 			qry.setParameter(0, iso3166Alpha2Code.toUpperCase());
104 			Country result = (Country) qry.uniqueResult();
105 
106 			return result;
107 		    }
108 		});
109     }
110 
111     /*
112      * (non-Javadoc)
113      * 
114      * @see com.gisgraphy.domain.repository.ICountryDao#getByIso3166Alpha3Code(java.lang.String)
115      */
116     public Country getByIso3166Alpha3Code(final String iso3166Alpha3Code) {
117 	if (iso3166Alpha3Code == null) {
118 	    return null;
119 	}
120 	if (iso3166Alpha3Code.length() != 3) {
121 	    logger.info("can not retrieve country with iso3166Alpha3Code="
122 		    + iso3166Alpha3Code
123 		    + " : iso3166Alpha3Code must have a length of 3 ");
124 	    return null;
125 	}
126 	return (Country) this.getHibernateTemplate().execute(
127 		new HibernateCallback() {
128 
129 		    public Object doInHibernate(Session session)
130 			    throws PersistenceException {
131 			String queryString = "from "
132 				+ Country.class.getSimpleName()
133 				+ " as c where c.iso3166Alpha3Code= ?";
134 
135 			Query qry = session.createQuery(queryString);
136 			qry.setCacheable(true);
137 
138 			qry.setParameter(0, iso3166Alpha3Code.toUpperCase());
139 			Country result = (Country) qry.uniqueResult();
140 
141 			return result;
142 		    }
143 		});
144     }
145 
146     /*
147      * (non-Javadoc)
148      * 
149      * @see com.gisgraphy.domain.repository.ICountryDao#getByIso3166Code(java.lang.String)
150      */
151     public Country getByIso3166Code(String iso3166Code) {
152 	if (iso3166Code == null) {
153 	    return null;
154 	}
155 	if (iso3166Code.length() == 2) {
156 	    return getByIso3166Alpha2Code(iso3166Code);
157 	} else if (iso3166Code.length() == 3) {
158 	    return getByIso3166Alpha3Code(iso3166Code);
159 	} else {
160 	    logger.info("can not retrieve country with iso3166Code="
161 		    + iso3166Code
162 		    + " : iso3166Code must have a length of 2 or 3");
163 	    return null;
164 	}
165     }
166 
167     // TODO v2 getneighboors and set neighboors en post import des polygon de
168     // country
169     /*
170      * (non-Javadoc)
171      * 
172      * @see com.gisgraphy.domain.repository.ICountryDao#getByName(java.lang.String)
173      */
174     public Country getByName(final String name) {
175 	if (name == null) {
176 	    return null;
177 	}
178 	return (Country) this.getHibernateTemplate().execute(
179 		new HibernateCallback() {
180 
181 		    public Object doInHibernate(Session session)
182 			    throws PersistenceException {
183 			String queryString = "from "
184 				+ Country.class.getSimpleName()
185 				+ " as c where c.name= ?";
186 
187 			Query qry = session.createQuery(queryString);
188 			qry.setCacheable(true);
189 
190 			qry.setParameter(0, name);
191 			Country result = (Country) qry.uniqueResult();
192 
193 			return result;
194 		    }
195 		});
196     }
197 
198     /*
199      * (non-Javadoc)
200      * 
201      * @see com.gisgraphy.domain.repository.GenericDao#deleteAll()
202      */
203     @Override
204     public int deleteAll() {
205 	List<Country> all = getAll();
206 	int deleted = all.size();
207 	if (deleted != 0) {
208 	    super.deleteAll(all);
209 	    super.flushAndClear();
210 	}
211 	flushAndClear();
212 	return deleted;
213     }
214 
215     /*
216      * (non-Javadoc)
217      * 
218      * @see com.gisgraphy.domain.repository.ICountryDao#getAllSortedByName()
219      */
220     @SuppressWarnings("unchecked")
221     public List<Country> getAllSortedByName() {
222 	return (List<Country>) this.getHibernateTemplate().execute(
223 		new HibernateCallback() {
224 
225 		    public Object doInHibernate(Session session)
226 			    throws PersistenceException {
227 			String queryString = "from "
228 				+ persistentClass.getSimpleName()
229 				+ " order by name";
230 
231 			Query qry = session.createQuery(queryString);
232 			qry.setCacheable(true);
233 			List<Country> results = (List<Country>) qry.list();
234 			if (results == null) {
235 			    results = new ArrayList<Country>();
236 			}
237 			return results;
238 		    }
239 		});
240 
241     }
242 
243     @SuppressWarnings("unchecked")
244 	public List<Long> listFeatureIds() {
245 		return ((List<Long>) this.getHibernateTemplate().execute(
246 			new HibernateCallback() {
247 
248 			    public Object doInHibernate(final Session session)
249 				    throws PersistenceException {
250 				final String queryString = "select featureId from "
251 					+ persistentClass.getSimpleName();
252 
253 				final Query qry = session.createQuery(queryString);
254 				qry.setCacheable(false);
255 				return qry.list();
256 
257 			    }
258 			}));
259 	}
260 
261 }