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