1   /**
2    * Copyright (c) 2000-2008 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.NoSuchRegionException;
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.GetterUtil;
30  import com.liferay.portal.kernel.util.OrderByComparator;
31  import com.liferay.portal.kernel.util.StringMaker;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.model.ModelListener;
35  import com.liferay.portal.model.Region;
36  import com.liferay.portal.model.impl.RegionImpl;
37  import com.liferay.portal.model.impl.RegionModelImpl;
38  import com.liferay.portal.spring.hibernate.FinderCache;
39  import com.liferay.portal.spring.hibernate.HibernateUtil;
40  import com.liferay.portal.util.PropsUtil;
41  
42  import com.liferay.util.dao.hibernate.QueryUtil;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import org.hibernate.Query;
48  import org.hibernate.Session;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.Iterator;
53  import java.util.List;
54  
55  /**
56   * <a href="RegionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class RegionPersistenceImpl extends BasePersistence
62      implements RegionPersistence {
63      public Region create(long regionId) {
64          Region region = new RegionImpl();
65  
66          region.setNew(true);
67          region.setPrimaryKey(regionId);
68  
69          return region;
70      }
71  
72      public Region remove(long regionId)
73          throws NoSuchRegionException, SystemException {
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              Region region = (Region)session.get(RegionImpl.class,
80                      new Long(regionId));
81  
82              if (region == null) {
83                  if (_log.isWarnEnabled()) {
84                      _log.warn("No Region exists with the primary key " +
85                          regionId);
86                  }
87  
88                  throw new NoSuchRegionException(
89                      "No Region exists with the primary key " + regionId);
90              }
91  
92              return remove(region);
93          }
94          catch (NoSuchRegionException nsee) {
95              throw nsee;
96          }
97          catch (Exception e) {
98              throw HibernateUtil.processException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public Region remove(Region region) throws SystemException {
106         if (_listeners != null) {
107             for (ModelListener listener : _listeners) {
108                 listener.onBeforeRemove(region);
109             }
110         }
111 
112         region = removeImpl(region);
113 
114         if (_listeners != null) {
115             for (ModelListener listener : _listeners) {
116                 listener.onAfterRemove(region);
117             }
118         }
119 
120         return region;
121     }
122 
123     protected Region removeImpl(Region region) throws SystemException {
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             session.delete(region);
130 
131             session.flush();
132 
133             return region;
134         }
135         catch (Exception e) {
136             throw HibernateUtil.processException(e);
137         }
138         finally {
139             closeSession(session);
140 
141             FinderCache.clearCache(Region.class.getName());
142         }
143     }
144 
145     /**
146      * @deprecated Use <code>update(Region region, boolean merge)</code>.
147      */
148     public Region update(Region region) throws SystemException {
149         if (_log.isWarnEnabled()) {
150             _log.warn(
151                 "Using the deprecated update(Region region) method. Use update(Region region, boolean merge) instead.");
152         }
153 
154         return update(region, false);
155     }
156 
157     /**
158      * Add, update, or merge, the entity. This method also calls the model
159      * listeners to trigger the proper events associated with adding, deleting,
160      * or updating an entity.
161      *
162      * @param        region the entity to add, update, or merge
163      * @param        merge boolean value for whether to merge the entity. The
164      *                default value is false. Setting merge to true is more
165      *                expensive and should only be true when region is
166      *                transient. See LEP-5473 for a detailed discussion of this
167      *                method.
168      * @return        true if the portlet can be displayed via Ajax
169      */
170     public Region update(Region region, boolean merge)
171         throws SystemException {
172         boolean isNew = region.isNew();
173 
174         if (_listeners != null) {
175             for (ModelListener listener : _listeners) {
176                 if (isNew) {
177                     listener.onBeforeCreate(region);
178                 }
179                 else {
180                     listener.onBeforeUpdate(region);
181                 }
182             }
183         }
184 
185         region = updateImpl(region, merge);
186 
187         if (_listeners != null) {
188             for (ModelListener listener : _listeners) {
189                 if (isNew) {
190                     listener.onAfterCreate(region);
191                 }
192                 else {
193                     listener.onAfterUpdate(region);
194                 }
195             }
196         }
197 
198         return region;
199     }
200 
201     public Region updateImpl(com.liferay.portal.model.Region region,
202         boolean merge) throws SystemException {
203         Session session = null;
204 
205         try {
206             session = openSession();
207 
208             if (merge) {
209                 session.merge(region);
210             }
211             else {
212                 if (region.isNew()) {
213                     session.save(region);
214                 }
215             }
216 
217             session.flush();
218 
219             region.setNew(false);
220 
221             return region;
222         }
223         catch (Exception e) {
224             throw HibernateUtil.processException(e);
225         }
226         finally {
227             closeSession(session);
228 
229             FinderCache.clearCache(Region.class.getName());
230         }
231     }
232 
233     public Region findByPrimaryKey(long regionId)
234         throws NoSuchRegionException, SystemException {
235         Region region = fetchByPrimaryKey(regionId);
236 
237         if (region == null) {
238             if (_log.isWarnEnabled()) {
239                 _log.warn("No Region exists with the primary key " + regionId);
240             }
241 
242             throw new NoSuchRegionException(
243                 "No Region exists with the primary key " + regionId);
244         }
245 
246         return region;
247     }
248 
249     public Region fetchByPrimaryKey(long regionId) throws SystemException {
250         Session session = null;
251 
252         try {
253             session = openSession();
254 
255             return (Region)session.get(RegionImpl.class, new Long(regionId));
256         }
257         catch (Exception e) {
258             throw HibernateUtil.processException(e);
259         }
260         finally {
261             closeSession(session);
262         }
263     }
264 
265     public List<Region> findByCountryId(long countryId)
266         throws SystemException {
267         boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
268         String finderClassName = Region.class.getName();
269         String finderMethodName = "findByCountryId";
270         String[] finderParams = new String[] { Long.class.getName() };
271         Object[] finderArgs = new Object[] { new Long(countryId) };
272 
273         Object result = null;
274 
275         if (finderClassNameCacheEnabled) {
276             result = FinderCache.getResult(finderClassName, finderMethodName,
277                     finderParams, finderArgs, getSessionFactory());
278         }
279 
280         if (result == null) {
281             Session session = null;
282 
283             try {
284                 session = openSession();
285 
286                 StringMaker query = new StringMaker();
287 
288                 query.append("FROM com.liferay.portal.model.Region WHERE ");
289 
290                 query.append("countryId = ?");
291 
292                 query.append(" ");
293 
294                 query.append("ORDER BY ");
295 
296                 query.append("name ASC");
297 
298                 Query q = session.createQuery(query.toString());
299 
300                 int queryPos = 0;
301 
302                 q.setLong(queryPos++, countryId);
303 
304                 List<Region> list = q.list();
305 
306                 FinderCache.putResult(finderClassNameCacheEnabled,
307                     finderClassName, finderMethodName, finderParams,
308                     finderArgs, list);
309 
310                 return list;
311             }
312             catch (Exception e) {
313                 throw HibernateUtil.processException(e);
314             }
315             finally {
316                 closeSession(session);
317             }
318         }
319         else {
320             return (List<Region>)result;
321         }
322     }
323 
324     public List<Region> findByCountryId(long countryId, int begin, int end)
325         throws SystemException {
326         return findByCountryId(countryId, begin, end, null);
327     }
328 
329     public List<Region> findByCountryId(long countryId, int begin, int end,
330         OrderByComparator obc) throws SystemException {
331         boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
332         String finderClassName = Region.class.getName();
333         String finderMethodName = "findByCountryId";
334         String[] finderParams = new String[] {
335                 Long.class.getName(),
336                 
337                 "java.lang.Integer", "java.lang.Integer",
338                 "com.liferay.portal.kernel.util.OrderByComparator"
339             };
340         Object[] finderArgs = new Object[] {
341                 new Long(countryId),
342                 
343                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
344             };
345 
346         Object result = null;
347 
348         if (finderClassNameCacheEnabled) {
349             result = FinderCache.getResult(finderClassName, finderMethodName,
350                     finderParams, finderArgs, getSessionFactory());
351         }
352 
353         if (result == null) {
354             Session session = null;
355 
356             try {
357                 session = openSession();
358 
359                 StringMaker query = new StringMaker();
360 
361                 query.append("FROM com.liferay.portal.model.Region WHERE ");
362 
363                 query.append("countryId = ?");
364 
365                 query.append(" ");
366 
367                 if (obc != null) {
368                     query.append("ORDER BY ");
369                     query.append(obc.getOrderBy());
370                 }
371 
372                 else {
373                     query.append("ORDER BY ");
374 
375                     query.append("name ASC");
376                 }
377 
378                 Query q = session.createQuery(query.toString());
379 
380                 int queryPos = 0;
381 
382                 q.setLong(queryPos++, countryId);
383 
384                 List<Region> list = (List<Region>)QueryUtil.list(q,
385                         getDialect(), begin, end);
386 
387                 FinderCache.putResult(finderClassNameCacheEnabled,
388                     finderClassName, finderMethodName, finderParams,
389                     finderArgs, list);
390 
391                 return list;
392             }
393             catch (Exception e) {
394                 throw HibernateUtil.processException(e);
395             }
396             finally {
397                 closeSession(session);
398             }
399         }
400         else {
401             return (List<Region>)result;
402         }
403     }
404 
405     public Region findByCountryId_First(long countryId, OrderByComparator obc)
406         throws NoSuchRegionException, SystemException {
407         List<Region> list = findByCountryId(countryId, 0, 1, obc);
408 
409         if (list.size() == 0) {
410             StringMaker msg = new StringMaker();
411 
412             msg.append("No Region exists with the key {");
413 
414             msg.append("countryId=" + countryId);
415 
416             msg.append(StringPool.CLOSE_CURLY_BRACE);
417 
418             throw new NoSuchRegionException(msg.toString());
419         }
420         else {
421             return list.get(0);
422         }
423     }
424 
425     public Region findByCountryId_Last(long countryId, OrderByComparator obc)
426         throws NoSuchRegionException, SystemException {
427         int count = countByCountryId(countryId);
428 
429         List<Region> list = findByCountryId(countryId, count - 1, count, obc);
430 
431         if (list.size() == 0) {
432             StringMaker msg = new StringMaker();
433 
434             msg.append("No Region exists with the key {");
435 
436             msg.append("countryId=" + countryId);
437 
438             msg.append(StringPool.CLOSE_CURLY_BRACE);
439 
440             throw new NoSuchRegionException(msg.toString());
441         }
442         else {
443             return list.get(0);
444         }
445     }
446 
447     public Region[] findByCountryId_PrevAndNext(long regionId, long countryId,
448         OrderByComparator obc) throws NoSuchRegionException, SystemException {
449         Region region = findByPrimaryKey(regionId);
450 
451         int count = countByCountryId(countryId);
452 
453         Session session = null;
454 
455         try {
456             session = openSession();
457 
458             StringMaker query = new StringMaker();
459 
460             query.append("FROM com.liferay.portal.model.Region WHERE ");
461 
462             query.append("countryId = ?");
463 
464             query.append(" ");
465 
466             if (obc != null) {
467                 query.append("ORDER BY ");
468                 query.append(obc.getOrderBy());
469             }
470 
471             else {
472                 query.append("ORDER BY ");
473 
474                 query.append("name ASC");
475             }
476 
477             Query q = session.createQuery(query.toString());
478 
479             int queryPos = 0;
480 
481             q.setLong(queryPos++, countryId);
482 
483             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, region);
484 
485             Region[] array = new RegionImpl[3];
486 
487             array[0] = (Region)objArray[0];
488             array[1] = (Region)objArray[1];
489             array[2] = (Region)objArray[2];
490 
491             return array;
492         }
493         catch (Exception e) {
494             throw HibernateUtil.processException(e);
495         }
496         finally {
497             closeSession(session);
498         }
499     }
500 
501     public List<Region> findByActive(boolean active) throws SystemException {
502         boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
503         String finderClassName = Region.class.getName();
504         String finderMethodName = "findByActive";
505         String[] finderParams = new String[] { Boolean.class.getName() };
506         Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
507 
508         Object result = null;
509 
510         if (finderClassNameCacheEnabled) {
511             result = FinderCache.getResult(finderClassName, finderMethodName,
512                     finderParams, finderArgs, getSessionFactory());
513         }
514 
515         if (result == null) {
516             Session session = null;
517 
518             try {
519                 session = openSession();
520 
521                 StringMaker query = new StringMaker();
522 
523                 query.append("FROM com.liferay.portal.model.Region WHERE ");
524 
525                 query.append("active_ = ?");
526 
527                 query.append(" ");
528 
529                 query.append("ORDER BY ");
530 
531                 query.append("name ASC");
532 
533                 Query q = session.createQuery(query.toString());
534 
535                 int queryPos = 0;
536 
537                 q.setBoolean(queryPos++, active);
538 
539                 List<Region> list = q.list();
540 
541                 FinderCache.putResult(finderClassNameCacheEnabled,
542                     finderClassName, finderMethodName, finderParams,
543                     finderArgs, list);
544 
545                 return list;
546             }
547             catch (Exception e) {
548                 throw HibernateUtil.processException(e);
549             }
550             finally {
551                 closeSession(session);
552             }
553         }
554         else {
555             return (List<Region>)result;
556         }
557     }
558 
559     public List<Region> findByActive(boolean active, int begin, int end)
560         throws SystemException {
561         return findByActive(active, begin, end, null);
562     }
563 
564     public List<Region> findByActive(boolean active, int begin, int end,
565         OrderByComparator obc) throws SystemException {
566         boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
567         String finderClassName = Region.class.getName();
568         String finderMethodName = "findByActive";
569         String[] finderParams = new String[] {
570                 Boolean.class.getName(),
571                 
572                 "java.lang.Integer", "java.lang.Integer",
573                 "com.liferay.portal.kernel.util.OrderByComparator"
574             };
575         Object[] finderArgs = new Object[] {
576                 Boolean.valueOf(active),
577                 
578                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
579             };
580 
581         Object result = null;
582 
583         if (finderClassNameCacheEnabled) {
584             result = FinderCache.getResult(finderClassName, finderMethodName,
585                     finderParams, finderArgs, getSessionFactory());
586         }
587 
588         if (result == null) {
589             Session session = null;
590 
591             try {
592                 session = openSession();
593 
594                 StringMaker query = new StringMaker();
595 
596                 query.append("FROM com.liferay.portal.model.Region WHERE ");
597 
598                 query.append("active_ = ?");
599 
600                 query.append(" ");
601 
602                 if (obc != null) {
603                     query.append("ORDER BY ");
604                     query.append(obc.getOrderBy());
605                 }
606 
607                 else {
608                     query.append("ORDER BY ");
609 
610                     query.append("name ASC");
611                 }
612 
613                 Query q = session.createQuery(query.toString());
614 
615                 int queryPos = 0;
616 
617                 q.setBoolean(queryPos++, active);
618 
619                 List<Region> list = (List<Region>)QueryUtil.list(q,
620                         getDialect(), begin, end);
621 
622                 FinderCache.putResult(finderClassNameCacheEnabled,
623                     finderClassName, finderMethodName, finderParams,
624                     finderArgs, list);
625 
626                 return list;
627             }
628             catch (Exception e) {
629                 throw HibernateUtil.processException(e);
630             }
631             finally {
632                 closeSession(session);
633             }
634         }
635         else {
636             return (List<Region>)result;
637         }
638     }
639 
640     public Region findByActive_First(boolean active, OrderByComparator obc)
641         throws NoSuchRegionException, SystemException {
642         List<Region> list = findByActive(active, 0, 1, obc);
643 
644         if (list.size() == 0) {
645             StringMaker msg = new StringMaker();
646 
647             msg.append("No Region exists with the key {");
648 
649             msg.append("active=" + active);
650 
651             msg.append(StringPool.CLOSE_CURLY_BRACE);
652 
653             throw new NoSuchRegionException(msg.toString());
654         }
655         else {
656             return list.get(0);
657         }
658     }
659 
660     public Region findByActive_Last(boolean active, OrderByComparator obc)
661         throws NoSuchRegionException, SystemException {
662         int count = countByActive(active);
663 
664         List<Region> list = findByActive(active, count - 1, count, obc);
665 
666         if (list.size() == 0) {
667             StringMaker msg = new StringMaker();
668 
669             msg.append("No Region exists with the key {");
670 
671             msg.append("active=" + active);
672 
673             msg.append(StringPool.CLOSE_CURLY_BRACE);
674 
675             throw new NoSuchRegionException(msg.toString());
676         }
677         else {
678             return list.get(0);
679         }
680     }
681 
682     public Region[] findByActive_PrevAndNext(long regionId, boolean active,
683         OrderByComparator obc) throws NoSuchRegionException, SystemException {
684         Region region = findByPrimaryKey(regionId);
685 
686         int count = countByActive(active);
687 
688         Session session = null;
689 
690         try {
691             session = openSession();
692 
693             StringMaker query = new StringMaker();
694 
695             query.append("FROM com.liferay.portal.model.Region WHERE ");
696 
697             query.append("active_ = ?");
698 
699             query.append(" ");
700 
701             if (obc != null) {
702                 query.append("ORDER BY ");
703                 query.append(obc.getOrderBy());
704             }
705 
706             else {
707                 query.append("ORDER BY ");
708 
709                 query.append("name ASC");
710             }
711 
712             Query q = session.createQuery(query.toString());
713 
714             int queryPos = 0;
715 
716             q.setBoolean(queryPos++, active);
717 
718             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, region);
719 
720             Region[] array = new RegionImpl[3];
721 
722             array[0] = (Region)objArray[0];
723             array[1] = (Region)objArray[1];
724             array[2] = (Region)objArray[2];
725 
726             return array;
727         }
728         catch (Exception e) {
729             throw HibernateUtil.processException(e);
730         }
731         finally {
732             closeSession(session);
733         }
734     }
735 
736     public List<Region> findByC_A(long countryId, boolean active)
737         throws SystemException {
738         boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
739         String finderClassName = Region.class.getName();
740         String finderMethodName = "findByC_A";
741         String[] finderParams = new String[] {
742                 Long.class.getName(), Boolean.class.getName()
743             };
744         Object[] finderArgs = new Object[] {
745                 new Long(countryId), Boolean.valueOf(active)
746             };
747 
748         Object result = null;
749 
750         if (finderClassNameCacheEnabled) {
751             result = FinderCache.getResult(finderClassName, finderMethodName,
752                     finderParams, finderArgs, getSessionFactory());
753         }
754 
755         if (result == null) {
756             Session session = null;
757 
758             try {
759                 session = openSession();
760 
761                 StringMaker query = new StringMaker();
762 
763                 query.append("FROM com.liferay.portal.model.Region WHERE ");
764 
765                 query.append("countryId = ?");
766 
767                 query.append(" AND ");
768 
769                 query.append("active_ = ?");
770 
771                 query.append(" ");
772 
773                 query.append("ORDER BY ");
774 
775                 query.append("name ASC");
776 
777                 Query q = session.createQuery(query.toString());
778 
779                 int queryPos = 0;
780 
781                 q.setLong(queryPos++, countryId);
782 
783                 q.setBoolean(queryPos++, active);
784 
785                 List<Region> list = q.list();
786 
787                 FinderCache.putResult(finderClassNameCacheEnabled,
788                     finderClassName, finderMethodName, finderParams,
789                     finderArgs, list);
790 
791                 return list;
792             }
793             catch (Exception e) {
794                 throw HibernateUtil.processException(e);
795             }
796             finally {
797                 closeSession(session);
798             }
799         }
800         else {
801             return (List<Region>)result;
802         }
803     }
804 
805     public List<Region> findByC_A(long countryId, boolean active, int begin,
806         int end) throws SystemException {
807         return findByC_A(countryId, active, begin, end, null);
808     }
809 
810     public List<Region> findByC_A(long countryId, boolean active, int begin,
811         int end, OrderByComparator obc) throws SystemException {
812         boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
813         String finderClassName = Region.class.getName();
814         String finderMethodName = "findByC_A";
815         String[] finderParams = new String[] {
816                 Long.class.getName(), Boolean.class.getName(),
817                 
818                 "java.lang.Integer", "java.lang.Integer",
819                 "com.liferay.portal.kernel.util.OrderByComparator"
820             };
821         Object[] finderArgs = new Object[] {
822                 new Long(countryId), Boolean.valueOf(active),
823                 
824                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
825             };
826 
827         Object result = null;
828 
829         if (finderClassNameCacheEnabled) {
830             result = FinderCache.getResult(finderClassName, finderMethodName,
831                     finderParams, finderArgs, getSessionFactory());
832         }
833 
834         if (result == null) {
835             Session session = null;
836 
837             try {
838                 session = openSession();
839 
840                 StringMaker query = new StringMaker();
841 
842                 query.append("FROM com.liferay.portal.model.Region WHERE ");
843 
844                 query.append("countryId = ?");
845 
846                 query.append(" AND ");
847 
848                 query.append("active_ = ?");
849 
850                 query.append(" ");
851 
852                 if (obc != null) {
853                     query.append("ORDER BY ");
854                     query.append(obc.getOrderBy());
855                 }
856 
857                 else {
858                     query.append("ORDER BY ");
859 
860                     query.append("name ASC");
861                 }
862 
863                 Query q = session.createQuery(query.toString());
864 
865                 int queryPos = 0;
866 
867                 q.setLong(queryPos++, countryId);
868 
869                 q.setBoolean(queryPos++, active);
870 
871                 List<Region> list = (List<Region>)QueryUtil.list(q,
872                         getDialect(), begin, end);
873 
874                 FinderCache.putResult(finderClassNameCacheEnabled,
875                     finderClassName, finderMethodName, finderParams,
876                     finderArgs, list);
877 
878                 return list;
879             }
880             catch (Exception e) {
881                 throw HibernateUtil.processException(e);
882             }
883             finally {
884                 closeSession(session);
885             }
886         }
887         else {
888             return (List<Region>)result;
889         }
890     }
891 
892     public Region findByC_A_First(long countryId, boolean active,
893         OrderByComparator obc) throws NoSuchRegionException, SystemException {
894         List<Region> list = findByC_A(countryId, active, 0, 1, obc);
895 
896         if (list.size() == 0) {
897             StringMaker msg = new StringMaker();
898 
899             msg.append("No Region exists with the key {");
900 
901             msg.append("countryId=" + countryId);
902 
903             msg.append(", ");
904             msg.append("active=" + active);
905 
906             msg.append(StringPool.CLOSE_CURLY_BRACE);
907 
908             throw new NoSuchRegionException(msg.toString());
909         }
910         else {
911             return list.get(0);
912         }
913     }
914 
915     public Region findByC_A_Last(long countryId, boolean active,
916         OrderByComparator obc) throws NoSuchRegionException, SystemException {
917         int count = countByC_A(countryId, active);
918 
919         List<Region> list = findByC_A(countryId, active, count - 1, count, obc);
920 
921         if (list.size() == 0) {
922             StringMaker msg = new StringMaker();
923 
924             msg.append("No Region exists with the key {");
925 
926             msg.append("countryId=" + countryId);
927 
928             msg.append(", ");
929             msg.append("active=" + active);
930 
931             msg.append(StringPool.CLOSE_CURLY_BRACE);
932 
933             throw new NoSuchRegionException(msg.toString());
934         }
935         else {
936             return list.get(0);
937         }
938     }
939 
940     public Region[] findByC_A_PrevAndNext(long regionId, long countryId,
941         boolean active, OrderByComparator obc)
942         throws NoSuchRegionException, SystemException {
943         Region region = findByPrimaryKey(regionId);
944 
945         int count = countByC_A(countryId, active);
946 
947         Session session = null;
948 
949         try {
950             session = openSession();
951 
952             StringMaker query = new StringMaker();
953 
954             query.append("FROM com.liferay.portal.model.Region WHERE ");
955 
956             query.append("countryId = ?");
957 
958             query.append(" AND ");
959 
960             query.append("active_ = ?");
961 
962             query.append(" ");
963 
964             if (obc != null) {
965                 query.append("ORDER BY ");
966                 query.append(obc.getOrderBy());
967             }
968 
969             else {
970                 query.append("ORDER BY ");
971 
972                 query.append("name ASC");
973             }
974 
975             Query q = session.createQuery(query.toString());
976 
977             int queryPos = 0;
978 
979             q.setLong(queryPos++, countryId);
980 
981             q.setBoolean(queryPos++, active);
982 
983             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, region);
984 
985             Region[] array = new RegionImpl[3];
986 
987             array[0] = (Region)objArray[0];
988             array[1] = (Region)objArray[1];
989             array[2] = (Region)objArray[2];
990 
991             return array;
992         }
993         catch (Exception e) {
994             throw HibernateUtil.processException(e);
995         }
996         finally {
997             closeSession(session);
998         }
999     }
1000
1001    public List<Region> findWithDynamicQuery(
1002        DynamicQueryInitializer queryInitializer) throws SystemException {
1003        Session session = null;
1004
1005        try {
1006            session = openSession();
1007
1008            DynamicQuery query = queryInitializer.initialize(session);
1009
1010            return query.list();
1011        }
1012        catch (Exception e) {
1013            throw HibernateUtil.processException(e);
1014        }
1015        finally {
1016            closeSession(session);
1017        }
1018    }
1019
1020    public List<Region> findWithDynamicQuery(
1021        DynamicQueryInitializer queryInitializer, int begin, int end)
1022        throws SystemException {
1023        Session session = null;
1024
1025        try {
1026            session = openSession();
1027
1028            DynamicQuery query = queryInitializer.initialize(session);
1029
1030            query.setLimit(begin, end);
1031
1032            return query.list();
1033        }
1034        catch (Exception e) {
1035            throw HibernateUtil.processException(e);
1036        }
1037        finally {
1038            closeSession(session);
1039        }
1040    }
1041
1042    public List<Region> findAll() throws SystemException {
1043        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1044    }
1045
1046    public List<Region> findAll(int begin, int end) throws SystemException {
1047        return findAll(begin, end, null);
1048    }
1049
1050    public List<Region> findAll(int begin, int end, OrderByComparator obc)
1051        throws SystemException {
1052        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1053        String finderClassName = Region.class.getName();
1054        String finderMethodName = "findAll";
1055        String[] finderParams = new String[] {
1056                "java.lang.Integer", "java.lang.Integer",
1057                "com.liferay.portal.kernel.util.OrderByComparator"
1058            };
1059        Object[] finderArgs = new Object[] {
1060                String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
1061            };
1062
1063        Object result = null;
1064
1065        if (finderClassNameCacheEnabled) {
1066            result = FinderCache.getResult(finderClassName, finderMethodName,
1067                    finderParams, finderArgs, getSessionFactory());
1068        }
1069
1070        if (result == null) {
1071            Session session = null;
1072
1073            try {
1074                session = openSession();
1075
1076                StringMaker query = new StringMaker();
1077
1078                query.append("FROM com.liferay.portal.model.Region ");
1079
1080                if (obc != null) {
1081                    query.append("ORDER BY ");
1082                    query.append(obc.getOrderBy());
1083                }
1084
1085                else {
1086                    query.append("ORDER BY ");
1087
1088                    query.append("name ASC");
1089                }
1090
1091                Query q = session.createQuery(query.toString());
1092
1093                List<Region> list = (List<Region>)QueryUtil.list(q,
1094                        getDialect(), begin, end);
1095
1096                if (obc == null) {
1097                    Collections.sort(list);
1098                }
1099
1100                FinderCache.putResult(finderClassNameCacheEnabled,
1101                    finderClassName, finderMethodName, finderParams,
1102                    finderArgs, list);
1103
1104                return list;
1105            }
1106            catch (Exception e) {
1107                throw HibernateUtil.processException(e);
1108            }
1109            finally {
1110                closeSession(session);
1111            }
1112        }
1113        else {
1114            return (List<Region>)result;
1115        }
1116    }
1117
1118    public void removeByCountryId(long countryId) throws SystemException {
1119        for (Region region : findByCountryId(countryId)) {
1120            remove(region);
1121        }
1122    }
1123
1124    public void removeByActive(boolean active) throws SystemException {
1125        for (Region region : findByActive(active)) {
1126            remove(region);
1127        }
1128    }
1129
1130    public void removeByC_A(long countryId, boolean active)
1131        throws SystemException {
1132        for (Region region : findByC_A(countryId, active)) {
1133            remove(region);
1134        }
1135    }
1136
1137    public void removeAll() throws SystemException {
1138        for (Region region : findAll()) {
1139            remove(region);
1140        }
1141    }
1142
1143    public int countByCountryId(long countryId) throws SystemException {
1144        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1145        String finderClassName = Region.class.getName();
1146        String finderMethodName = "countByCountryId";
1147        String[] finderParams = new String[] { Long.class.getName() };
1148        Object[] finderArgs = new Object[] { new Long(countryId) };
1149
1150        Object result = null;
1151
1152        if (finderClassNameCacheEnabled) {
1153            result = FinderCache.getResult(finderClassName, finderMethodName,
1154                    finderParams, finderArgs, getSessionFactory());
1155        }
1156
1157        if (result == null) {
1158            Session session = null;
1159
1160            try {
1161                session = openSession();
1162
1163                StringMaker query = new StringMaker();
1164
1165                query.append("SELECT COUNT(*) ");
1166                query.append("FROM com.liferay.portal.model.Region WHERE ");
1167
1168                query.append("countryId = ?");
1169
1170                query.append(" ");
1171
1172                Query q = session.createQuery(query.toString());
1173
1174                int queryPos = 0;
1175
1176                q.setLong(queryPos++, countryId);
1177
1178                Long count = null;
1179
1180                Iterator<Long> itr = q.list().iterator();
1181
1182                if (itr.hasNext()) {
1183                    count = itr.next();
1184                }
1185
1186                if (count == null) {
1187                    count = new Long(0);
1188                }
1189
1190                FinderCache.putResult(finderClassNameCacheEnabled,
1191                    finderClassName, finderMethodName, finderParams,
1192                    finderArgs, count);
1193
1194                return count.intValue();
1195            }
1196            catch (Exception e) {
1197                throw HibernateUtil.processException(e);
1198            }
1199            finally {
1200                closeSession(session);
1201            }
1202        }
1203        else {
1204            return ((Long)result).intValue();
1205        }
1206    }
1207
1208    public int countByActive(boolean active) throws SystemException {
1209        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1210        String finderClassName = Region.class.getName();
1211        String finderMethodName = "countByActive";
1212        String[] finderParams = new String[] { Boolean.class.getName() };
1213        Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
1214
1215        Object result = null;
1216
1217        if (finderClassNameCacheEnabled) {
1218            result = FinderCache.getResult(finderClassName, finderMethodName,
1219                    finderParams, finderArgs, getSessionFactory());
1220        }
1221
1222        if (result == null) {
1223            Session session = null;
1224
1225            try {
1226                session = openSession();
1227
1228                StringMaker query = new StringMaker();
1229
1230                query.append("SELECT COUNT(*) ");
1231                query.append("FROM com.liferay.portal.model.Region WHERE ");
1232
1233                query.append("active_ = ?");
1234
1235                query.append(" ");
1236
1237                Query q = session.createQuery(query.toString());
1238
1239                int queryPos = 0;
1240
1241                q.setBoolean(queryPos++, active);
1242
1243                Long count = null;
1244
1245                Iterator<Long> itr = q.list().iterator();
1246
1247                if (itr.hasNext()) {
1248                    count = itr.next();
1249                }
1250
1251                if (count == null) {
1252                    count = new Long(0);
1253                }
1254
1255                FinderCache.putResult(finderClassNameCacheEnabled,
1256                    finderClassName, finderMethodName, finderParams,
1257                    finderArgs, count);
1258
1259                return count.intValue();
1260            }
1261            catch (Exception e) {
1262                throw HibernateUtil.processException(e);
1263            }
1264            finally {
1265                closeSession(session);
1266            }
1267        }
1268        else {
1269            return ((Long)result).intValue();
1270        }
1271    }
1272
1273    public int countByC_A(long countryId, boolean active)
1274        throws SystemException {
1275        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1276        String finderClassName = Region.class.getName();
1277        String finderMethodName = "countByC_A";
1278        String[] finderParams = new String[] {
1279                Long.class.getName(), Boolean.class.getName()
1280            };
1281        Object[] finderArgs = new Object[] {
1282                new Long(countryId), Boolean.valueOf(active)
1283            };
1284
1285        Object result = null;
1286
1287        if (finderClassNameCacheEnabled) {
1288            result = FinderCache.getResult(finderClassName, finderMethodName,
1289                    finderParams, finderArgs, getSessionFactory());
1290        }
1291
1292        if (result == null) {
1293            Session session = null;
1294
1295            try {
1296                session = openSession();
1297
1298                StringMaker query = new StringMaker();
1299
1300                query.append("SELECT COUNT(*) ");
1301                query.append("FROM com.liferay.portal.model.Region WHERE ");
1302
1303                query.append("countryId = ?");
1304
1305                query.append(" AND ");
1306
1307                query.append("active_ = ?");
1308
1309                query.append(" ");
1310
1311                Query q = session.createQuery(query.toString());
1312
1313                int queryPos = 0;
1314
1315                q.setLong(queryPos++, countryId);
1316
1317                q.setBoolean(queryPos++, active);
1318
1319                Long count = null;
1320
1321                Iterator<Long> itr = q.list().iterator();
1322
1323                if (itr.hasNext()) {
1324                    count = itr.next();
1325                }
1326
1327                if (count == null) {
1328                    count = new Long(0);
1329                }
1330
1331                FinderCache.putResult(finderClassNameCacheEnabled,
1332                    finderClassName, finderMethodName, finderParams,
1333                    finderArgs, count);
1334
1335                return count.intValue();
1336            }
1337            catch (Exception e) {
1338                throw HibernateUtil.processException(e);
1339            }
1340            finally {
1341                closeSession(session);
1342            }
1343        }
1344        else {
1345            return ((Long)result).intValue();
1346        }
1347    }
1348
1349    public int countAll() throws SystemException {
1350        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1351        String finderClassName = Region.class.getName();
1352        String finderMethodName = "countAll";
1353        String[] finderParams = new String[] {  };
1354        Object[] finderArgs = new Object[] {  };
1355
1356        Object result = null;
1357
1358        if (finderClassNameCacheEnabled) {
1359            result = FinderCache.getResult(finderClassName, finderMethodName,
1360                    finderParams, finderArgs, getSessionFactory());
1361        }
1362
1363        if (result == null) {
1364            Session session = null;
1365
1366            try {
1367                session = openSession();
1368
1369                Query q = session.createQuery(
1370                        "SELECT COUNT(*) FROM com.liferay.portal.model.Region");
1371
1372                Long count = null;
1373
1374                Iterator<Long> itr = q.list().iterator();
1375
1376                if (itr.hasNext()) {
1377                    count = itr.next();
1378                }
1379
1380                if (count == null) {
1381                    count = new Long(0);
1382                }
1383
1384                FinderCache.putResult(finderClassNameCacheEnabled,
1385                    finderClassName, finderMethodName, finderParams,
1386                    finderArgs, count);
1387
1388                return count.intValue();
1389            }
1390            catch (Exception e) {
1391                throw HibernateUtil.processException(e);
1392            }
1393            finally {
1394                closeSession(session);
1395            }
1396        }
1397        else {
1398            return ((Long)result).intValue();
1399        }
1400    }
1401
1402    protected void initDao() {
1403        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1404                    PropsUtil.get(
1405                        "value.object.listener.com.liferay.portal.model.Region")));
1406
1407        if (listenerClassNames.length > 0) {
1408            try {
1409                List<ModelListener> listeners = new ArrayList<ModelListener>();
1410
1411                for (String listenerClassName : listenerClassNames) {
1412                    listeners.add((ModelListener)Class.forName(
1413                            listenerClassName).newInstance());
1414                }
1415
1416                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1417            }
1418            catch (Exception e) {
1419                _log.error(e);
1420            }
1421        }
1422    }
1423
1424    private static Log _log = LogFactory.getLog(RegionPersistenceImpl.class);
1425    private ModelListener[] _listeners;
1426}