1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
45
46 @Repository
47 public class CountryDao extends GenericGisDao<Country> implements ICountryDao {
48
49
50
51
52 public CountryDao() {
53 super(Country.class);
54 }
55
56
57
58
59
60
61
62
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
77
78
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
113
114
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
148
149
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
168
169
170
171
172
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
200
201
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
217
218
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 }