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