1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchCountryException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.Country;
33  import com.liferay.portal.model.impl.CountryImpl;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  
38  import com.liferay.util.dao.hibernate.QueryUtil;
39  
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  import org.hibernate.Query;
44  import org.hibernate.Session;
45  
46  import java.util.Collections;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  /**
51   * <a href="CountryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class CountryPersistenceImpl extends BasePersistence
57      implements CountryPersistence {
58      public Country create(long countryId) {
59          Country country = new CountryImpl();
60          country.setNew(true);
61          country.setPrimaryKey(countryId);
62  
63          return country;
64      }
65  
66      public Country remove(long countryId)
67          throws NoSuchCountryException, SystemException {
68          Session session = null;
69  
70          try {
71              session = openSession();
72  
73              Country country = (Country)session.get(CountryImpl.class,
74                      new Long(countryId));
75  
76              if (country == null) {
77                  if (_log.isWarnEnabled()) {
78                      _log.warn("No Country exists with the primary key " +
79                          countryId);
80                  }
81  
82                  throw new NoSuchCountryException(
83                      "No Country exists with the primary key " + countryId);
84              }
85  
86              return remove(country);
87          }
88          catch (NoSuchCountryException nsee) {
89              throw nsee;
90          }
91          catch (Exception e) {
92              throw HibernateUtil.processException(e);
93          }
94          finally {
95              closeSession(session);
96          }
97      }
98  
99      public Country remove(Country country) throws SystemException {
100         Session session = null;
101 
102         try {
103             session = openSession();
104             session.delete(country);
105             session.flush();
106 
107             return country;
108         }
109         catch (Exception e) {
110             throw HibernateUtil.processException(e);
111         }
112         finally {
113             closeSession(session);
114             FinderCache.clearCache(Country.class.getName());
115         }
116     }
117 
118     public Country update(com.liferay.portal.model.Country country)
119         throws SystemException {
120         return update(country, false);
121     }
122 
123     public Country update(com.liferay.portal.model.Country country,
124         boolean merge) throws SystemException {
125         Session session = null;
126 
127         try {
128             session = openSession();
129 
130             if (merge) {
131                 session.merge(country);
132             }
133             else {
134                 if (country.isNew()) {
135                     session.save(country);
136                 }
137             }
138 
139             session.flush();
140             country.setNew(false);
141 
142             return country;
143         }
144         catch (Exception e) {
145             throw HibernateUtil.processException(e);
146         }
147         finally {
148             closeSession(session);
149             FinderCache.clearCache(Country.class.getName());
150         }
151     }
152 
153     public Country findByPrimaryKey(long countryId)
154         throws NoSuchCountryException, SystemException {
155         Country country = fetchByPrimaryKey(countryId);
156 
157         if (country == null) {
158             if (_log.isWarnEnabled()) {
159                 _log.warn("No Country exists with the primary key " +
160                     countryId);
161             }
162 
163             throw new NoSuchCountryException(
164                 "No Country exists with the primary key " + countryId);
165         }
166 
167         return country;
168     }
169 
170     public Country fetchByPrimaryKey(long countryId) throws SystemException {
171         Session session = null;
172 
173         try {
174             session = openSession();
175 
176             return (Country)session.get(CountryImpl.class, new Long(countryId));
177         }
178         catch (Exception e) {
179             throw HibernateUtil.processException(e);
180         }
181         finally {
182             closeSession(session);
183         }
184     }
185 
186     public List findByActive(boolean active) throws SystemException {
187         String finderClassName = Country.class.getName();
188         String finderMethodName = "findByActive";
189         String[] finderParams = new String[] { Boolean.class.getName() };
190         Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
191         Object result = FinderCache.getResult(finderClassName,
192                 finderMethodName, finderParams, finderArgs, getSessionFactory());
193 
194         if (result == null) {
195             Session session = null;
196 
197             try {
198                 session = openSession();
199 
200                 StringMaker query = new StringMaker();
201                 query.append("FROM com.liferay.portal.model.Country WHERE ");
202                 query.append("active_ = ?");
203                 query.append(" ");
204                 query.append("ORDER BY ");
205                 query.append("name ASC");
206 
207                 Query q = session.createQuery(query.toString());
208                 int queryPos = 0;
209                 q.setBoolean(queryPos++, active);
210 
211                 List list = q.list();
212                 FinderCache.putResult(finderClassName, finderMethodName,
213                     finderParams, finderArgs, list);
214 
215                 return list;
216             }
217             catch (Exception e) {
218                 throw HibernateUtil.processException(e);
219             }
220             finally {
221                 closeSession(session);
222             }
223         }
224         else {
225             return (List)result;
226         }
227     }
228 
229     public List findByActive(boolean active, int begin, int end)
230         throws SystemException {
231         return findByActive(active, begin, end, null);
232     }
233 
234     public List findByActive(boolean active, int begin, int end,
235         OrderByComparator obc) throws SystemException {
236         String finderClassName = Country.class.getName();
237         String finderMethodName = "findByActive";
238         String[] finderParams = new String[] {
239                 Boolean.class.getName(), "java.lang.Integer",
240                 "java.lang.Integer",
241                 "com.liferay.portal.kernel.util.OrderByComparator"
242             };
243         Object[] finderArgs = new Object[] {
244                 Boolean.valueOf(active), String.valueOf(begin),
245                 String.valueOf(end), String.valueOf(obc)
246             };
247         Object result = FinderCache.getResult(finderClassName,
248                 finderMethodName, finderParams, finderArgs, getSessionFactory());
249 
250         if (result == null) {
251             Session session = null;
252 
253             try {
254                 session = openSession();
255 
256                 StringMaker query = new StringMaker();
257                 query.append("FROM com.liferay.portal.model.Country WHERE ");
258                 query.append("active_ = ?");
259                 query.append(" ");
260 
261                 if (obc != null) {
262                     query.append("ORDER BY ");
263                     query.append(obc.getOrderBy());
264                 }
265                 else {
266                     query.append("ORDER BY ");
267                     query.append("name ASC");
268                 }
269 
270                 Query q = session.createQuery(query.toString());
271                 int queryPos = 0;
272                 q.setBoolean(queryPos++, active);
273 
274                 List list = QueryUtil.list(q, getDialect(), begin, end);
275                 FinderCache.putResult(finderClassName, finderMethodName,
276                     finderParams, finderArgs, list);
277 
278                 return list;
279             }
280             catch (Exception e) {
281                 throw HibernateUtil.processException(e);
282             }
283             finally {
284                 closeSession(session);
285             }
286         }
287         else {
288             return (List)result;
289         }
290     }
291 
292     public Country findByActive_First(boolean active, OrderByComparator obc)
293         throws NoSuchCountryException, SystemException {
294         List list = findByActive(active, 0, 1, obc);
295 
296         if (list.size() == 0) {
297             StringMaker msg = new StringMaker();
298             msg.append("No Country exists with the key ");
299             msg.append(StringPool.OPEN_CURLY_BRACE);
300             msg.append("active=");
301             msg.append(active);
302             msg.append(StringPool.CLOSE_CURLY_BRACE);
303             throw new NoSuchCountryException(msg.toString());
304         }
305         else {
306             return (Country)list.get(0);
307         }
308     }
309 
310     public Country findByActive_Last(boolean active, OrderByComparator obc)
311         throws NoSuchCountryException, SystemException {
312         int count = countByActive(active);
313         List list = findByActive(active, count - 1, count, obc);
314 
315         if (list.size() == 0) {
316             StringMaker msg = new StringMaker();
317             msg.append("No Country exists with the key ");
318             msg.append(StringPool.OPEN_CURLY_BRACE);
319             msg.append("active=");
320             msg.append(active);
321             msg.append(StringPool.CLOSE_CURLY_BRACE);
322             throw new NoSuchCountryException(msg.toString());
323         }
324         else {
325             return (Country)list.get(0);
326         }
327     }
328 
329     public Country[] findByActive_PrevAndNext(long countryId, boolean active,
330         OrderByComparator obc) throws NoSuchCountryException, SystemException {
331         Country country = findByPrimaryKey(countryId);
332         int count = countByActive(active);
333         Session session = null;
334 
335         try {
336             session = openSession();
337 
338             StringMaker query = new StringMaker();
339             query.append("FROM com.liferay.portal.model.Country WHERE ");
340             query.append("active_ = ?");
341             query.append(" ");
342 
343             if (obc != null) {
344                 query.append("ORDER BY ");
345                 query.append(obc.getOrderBy());
346             }
347             else {
348                 query.append("ORDER BY ");
349                 query.append("name ASC");
350             }
351 
352             Query q = session.createQuery(query.toString());
353             int queryPos = 0;
354             q.setBoolean(queryPos++, active);
355 
356             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, country);
357             Country[] array = new CountryImpl[3];
358             array[0] = (Country)objArray[0];
359             array[1] = (Country)objArray[1];
360             array[2] = (Country)objArray[2];
361 
362             return array;
363         }
364         catch (Exception e) {
365             throw HibernateUtil.processException(e);
366         }
367         finally {
368             closeSession(session);
369         }
370     }
371 
372     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
373         throws SystemException {
374         Session session = null;
375 
376         try {
377             session = openSession();
378 
379             DynamicQuery query = queryInitializer.initialize(session);
380 
381             return query.list();
382         }
383         catch (Exception e) {
384             throw HibernateUtil.processException(e);
385         }
386         finally {
387             closeSession(session);
388         }
389     }
390 
391     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
392         int begin, int end) throws SystemException {
393         Session session = null;
394 
395         try {
396             session = openSession();
397 
398             DynamicQuery query = queryInitializer.initialize(session);
399             query.setLimit(begin, end);
400 
401             return query.list();
402         }
403         catch (Exception e) {
404             throw HibernateUtil.processException(e);
405         }
406         finally {
407             closeSession(session);
408         }
409     }
410 
411     public List findAll() throws SystemException {
412         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
413     }
414 
415     public List findAll(int begin, int end) throws SystemException {
416         return findAll(begin, end, null);
417     }
418 
419     public List findAll(int begin, int end, OrderByComparator obc)
420         throws SystemException {
421         String finderClassName = Country.class.getName();
422         String finderMethodName = "findAll";
423         String[] finderParams = new String[] {
424                 "java.lang.Integer", "java.lang.Integer",
425                 "com.liferay.portal.kernel.util.OrderByComparator"
426             };
427         Object[] finderArgs = new Object[] {
428                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
429             };
430         Object result = FinderCache.getResult(finderClassName,
431                 finderMethodName, finderParams, finderArgs, getSessionFactory());
432 
433         if (result == null) {
434             Session session = null;
435 
436             try {
437                 session = openSession();
438 
439                 StringMaker query = new StringMaker();
440                 query.append("FROM com.liferay.portal.model.Country ");
441 
442                 if (obc != null) {
443                     query.append("ORDER BY ");
444                     query.append(obc.getOrderBy());
445                 }
446                 else {
447                     query.append("ORDER BY ");
448                     query.append("name ASC");
449                 }
450 
451                 Query q = session.createQuery(query.toString());
452                 List list = QueryUtil.list(q, getDialect(), begin, end);
453 
454                 if (obc == null) {
455                     Collections.sort(list);
456                 }
457 
458                 FinderCache.putResult(finderClassName, finderMethodName,
459                     finderParams, finderArgs, list);
460 
461                 return list;
462             }
463             catch (Exception e) {
464                 throw HibernateUtil.processException(e);
465             }
466             finally {
467                 closeSession(session);
468             }
469         }
470         else {
471             return (List)result;
472         }
473     }
474 
475     public void removeByActive(boolean active) throws SystemException {
476         Iterator itr = findByActive(active).iterator();
477 
478         while (itr.hasNext()) {
479             Country country = (Country)itr.next();
480             remove(country);
481         }
482     }
483 
484     public void removeAll() throws SystemException {
485         Iterator itr = findAll().iterator();
486 
487         while (itr.hasNext()) {
488             remove((Country)itr.next());
489         }
490     }
491 
492     public int countByActive(boolean active) throws SystemException {
493         String finderClassName = Country.class.getName();
494         String finderMethodName = "countByActive";
495         String[] finderParams = new String[] { Boolean.class.getName() };
496         Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
497         Object result = FinderCache.getResult(finderClassName,
498                 finderMethodName, finderParams, finderArgs, getSessionFactory());
499 
500         if (result == null) {
501             Session session = null;
502 
503             try {
504                 session = openSession();
505 
506                 StringMaker query = new StringMaker();
507                 query.append("SELECT COUNT(*) ");
508                 query.append("FROM com.liferay.portal.model.Country WHERE ");
509                 query.append("active_ = ?");
510                 query.append(" ");
511 
512                 Query q = session.createQuery(query.toString());
513                 int queryPos = 0;
514                 q.setBoolean(queryPos++, active);
515 
516                 Long count = null;
517                 Iterator itr = q.list().iterator();
518 
519                 if (itr.hasNext()) {
520                     count = (Long)itr.next();
521                 }
522 
523                 if (count == null) {
524                     count = new Long(0);
525                 }
526 
527                 FinderCache.putResult(finderClassName, finderMethodName,
528                     finderParams, finderArgs, count);
529 
530                 return count.intValue();
531             }
532             catch (Exception e) {
533                 throw HibernateUtil.processException(e);
534             }
535             finally {
536                 closeSession(session);
537             }
538         }
539         else {
540             return ((Long)result).intValue();
541         }
542     }
543 
544     public int countAll() throws SystemException {
545         String finderClassName = Country.class.getName();
546         String finderMethodName = "countAll";
547         String[] finderParams = new String[] {  };
548         Object[] finderArgs = new Object[] {  };
549         Object result = FinderCache.getResult(finderClassName,
550                 finderMethodName, finderParams, finderArgs, getSessionFactory());
551 
552         if (result == null) {
553             Session session = null;
554 
555             try {
556                 session = openSession();
557 
558                 StringMaker query = new StringMaker();
559                 query.append("SELECT COUNT(*) ");
560                 query.append("FROM com.liferay.portal.model.Country");
561 
562                 Query q = session.createQuery(query.toString());
563                 Long count = null;
564                 Iterator itr = q.list().iterator();
565 
566                 if (itr.hasNext()) {
567                     count = (Long)itr.next();
568                 }
569 
570                 if (count == null) {
571                     count = new Long(0);
572                 }
573 
574                 FinderCache.putResult(finderClassName, finderMethodName,
575                     finderParams, finderArgs, count);
576 
577                 return count.intValue();
578             }
579             catch (Exception e) {
580                 throw HibernateUtil.processException(e);
581             }
582             finally {
583                 closeSession(session);
584             }
585         }
586         else {
587             return ((Long)result).intValue();
588         }
589     }
590 
591     protected void initDao() {
592     }
593 
594     private static Log _log = LogFactory.getLog(CountryPersistenceImpl.class);
595 }