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