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             if (BatchSessionUtil.isEnabled()) {
127                 Object staleObject = session.get(RegionImpl.class,
128                         region.getPrimaryKeyObj());
129 
130                 if (staleObject != null) {
131                     session.evict(staleObject);
132                 }
133             }
134 
135             session.delete(region);
136 
137             session.flush();
138 
139             return region;
140         }
141         catch (Exception e) {
142             throw processException(e);
143         }
144         finally {
145             closeSession(session);
146 
147             FinderCacheUtil.clearCache(Region.class.getName());
148         }
149     }
150 
151     /**
152      * @deprecated Use <code>update(Region region, boolean merge)</code>.
153      */
154     public Region update(Region region) throws SystemException {
155         if (_log.isWarnEnabled()) {
156             _log.warn(
157                 "Using the deprecated update(Region region) method. Use update(Region region, boolean merge) instead.");
158         }
159 
160         return update(region, false);
161     }
162 
163     /**
164      * Add, update, or merge, the entity. This method also calls the model
165      * listeners to trigger the proper events associated with adding, deleting,
166      * or updating an entity.
167      *
168      * @param        region the entity to add, update, or merge
169      * @param        merge boolean value for whether to merge the entity. The
170      *                default value is false. Setting merge to true is more
171      *                expensive and should only be true when region is
172      *                transient. See LEP-5473 for a detailed discussion of this
173      *                method.
174      * @return        true if the portlet can be displayed via Ajax
175      */
176     public Region update(Region region, boolean merge)
177         throws SystemException {
178         boolean isNew = region.isNew();
179 
180         if (_listeners.length > 0) {
181             for (ModelListener listener : _listeners) {
182                 if (isNew) {
183                     listener.onBeforeCreate(region);
184                 }
185                 else {
186                     listener.onBeforeUpdate(region);
187                 }
188             }
189         }
190 
191         region = updateImpl(region, merge);
192 
193         if (_listeners.length > 0) {
194             for (ModelListener listener : _listeners) {
195                 if (isNew) {
196                     listener.onAfterCreate(region);
197                 }
198                 else {
199                     listener.onAfterUpdate(region);
200                 }
201             }
202         }
203 
204         return region;
205     }
206 
207     public Region updateImpl(com.liferay.portal.model.Region region,
208         boolean merge) throws SystemException {
209         Session session = null;
210 
211         try {
212             session = openSession();
213 
214             BatchSessionUtil.update(session, region, merge);
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 = null;
1090
1091                if (obc == null) {
1092                    list = (List<Region>)QueryUtil.list(q, getDialect(), start,
1093                            end, false);
1094
1095                    Collections.sort(list);
1096                }
1097                else {
1098                    list = (List<Region>)QueryUtil.list(q, getDialect(), start,
1099                            end);
1100                }
1101
1102                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1103                    finderClassName, finderMethodName, finderParams,
1104                    finderArgs, list);
1105
1106                return list;
1107            }
1108            catch (Exception e) {
1109                throw processException(e);
1110            }
1111            finally {
1112                closeSession(session);
1113            }
1114        }
1115        else {
1116            return (List<Region>)result;
1117        }
1118    }
1119
1120    public void removeByCountryId(long countryId) throws SystemException {
1121        for (Region region : findByCountryId(countryId)) {
1122            remove(region);
1123        }
1124    }
1125
1126    public void removeByActive(boolean active) throws SystemException {
1127        for (Region region : findByActive(active)) {
1128            remove(region);
1129        }
1130    }
1131
1132    public void removeByC_A(long countryId, boolean active)
1133        throws SystemException {
1134        for (Region region : findByC_A(countryId, active)) {
1135            remove(region);
1136        }
1137    }
1138
1139    public void removeAll() throws SystemException {
1140        for (Region region : findAll()) {
1141            remove(region);
1142        }
1143    }
1144
1145    public int countByCountryId(long countryId) throws SystemException {
1146        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1147        String finderClassName = Region.class.getName();
1148        String finderMethodName = "countByCountryId";
1149        String[] finderParams = new String[] { Long.class.getName() };
1150        Object[] finderArgs = new Object[] { new Long(countryId) };
1151
1152        Object result = null;
1153
1154        if (finderClassNameCacheEnabled) {
1155            result = FinderCacheUtil.getResult(finderClassName,
1156                    finderMethodName, finderParams, finderArgs, this);
1157        }
1158
1159        if (result == null) {
1160            Session session = null;
1161
1162            try {
1163                session = openSession();
1164
1165                StringBuilder query = new StringBuilder();
1166
1167                query.append("SELECT COUNT(*) ");
1168                query.append("FROM com.liferay.portal.model.Region WHERE ");
1169
1170                query.append("countryId = ?");
1171
1172                query.append(" ");
1173
1174                Query q = session.createQuery(query.toString());
1175
1176                QueryPos qPos = QueryPos.getInstance(q);
1177
1178                qPos.add(countryId);
1179
1180                Long count = null;
1181
1182                Iterator<Long> itr = q.list().iterator();
1183
1184                if (itr.hasNext()) {
1185                    count = itr.next();
1186                }
1187
1188                if (count == null) {
1189                    count = new Long(0);
1190                }
1191
1192                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1193                    finderClassName, finderMethodName, finderParams,
1194                    finderArgs, count);
1195
1196                return count.intValue();
1197            }
1198            catch (Exception e) {
1199                throw processException(e);
1200            }
1201            finally {
1202                closeSession(session);
1203            }
1204        }
1205        else {
1206            return ((Long)result).intValue();
1207        }
1208    }
1209
1210    public int countByActive(boolean active) throws SystemException {
1211        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1212        String finderClassName = Region.class.getName();
1213        String finderMethodName = "countByActive";
1214        String[] finderParams = new String[] { Boolean.class.getName() };
1215        Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
1216
1217        Object result = null;
1218
1219        if (finderClassNameCacheEnabled) {
1220            result = FinderCacheUtil.getResult(finderClassName,
1221                    finderMethodName, finderParams, finderArgs, this);
1222        }
1223
1224        if (result == null) {
1225            Session session = null;
1226
1227            try {
1228                session = openSession();
1229
1230                StringBuilder query = new StringBuilder();
1231
1232                query.append("SELECT COUNT(*) ");
1233                query.append("FROM com.liferay.portal.model.Region WHERE ");
1234
1235                query.append("active_ = ?");
1236
1237                query.append(" ");
1238
1239                Query q = session.createQuery(query.toString());
1240
1241                QueryPos qPos = QueryPos.getInstance(q);
1242
1243                qPos.add(active);
1244
1245                Long count = null;
1246
1247                Iterator<Long> itr = q.list().iterator();
1248
1249                if (itr.hasNext()) {
1250                    count = itr.next();
1251                }
1252
1253                if (count == null) {
1254                    count = new Long(0);
1255                }
1256
1257                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1258                    finderClassName, finderMethodName, finderParams,
1259                    finderArgs, count);
1260
1261                return count.intValue();
1262            }
1263            catch (Exception e) {
1264                throw processException(e);
1265            }
1266            finally {
1267                closeSession(session);
1268            }
1269        }
1270        else {
1271            return ((Long)result).intValue();
1272        }
1273    }
1274
1275    public int countByC_A(long countryId, boolean active)
1276        throws SystemException {
1277        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1278        String finderClassName = Region.class.getName();
1279        String finderMethodName = "countByC_A";
1280        String[] finderParams = new String[] {
1281                Long.class.getName(), Boolean.class.getName()
1282            };
1283        Object[] finderArgs = new Object[] {
1284                new Long(countryId), Boolean.valueOf(active)
1285            };
1286
1287        Object result = null;
1288
1289        if (finderClassNameCacheEnabled) {
1290            result = FinderCacheUtil.getResult(finderClassName,
1291                    finderMethodName, finderParams, finderArgs, this);
1292        }
1293
1294        if (result == null) {
1295            Session session = null;
1296
1297            try {
1298                session = openSession();
1299
1300                StringBuilder query = new StringBuilder();
1301
1302                query.append("SELECT COUNT(*) ");
1303                query.append("FROM com.liferay.portal.model.Region WHERE ");
1304
1305                query.append("countryId = ?");
1306
1307                query.append(" AND ");
1308
1309                query.append("active_ = ?");
1310
1311                query.append(" ");
1312
1313                Query q = session.createQuery(query.toString());
1314
1315                QueryPos qPos = QueryPos.getInstance(q);
1316
1317                qPos.add(countryId);
1318
1319                qPos.add(active);
1320
1321                Long count = null;
1322
1323                Iterator<Long> itr = q.list().iterator();
1324
1325                if (itr.hasNext()) {
1326                    count = itr.next();
1327                }
1328
1329                if (count == null) {
1330                    count = new Long(0);
1331                }
1332
1333                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1334                    finderClassName, finderMethodName, finderParams,
1335                    finderArgs, count);
1336
1337                return count.intValue();
1338            }
1339            catch (Exception e) {
1340                throw processException(e);
1341            }
1342            finally {
1343                closeSession(session);
1344            }
1345        }
1346        else {
1347            return ((Long)result).intValue();
1348        }
1349    }
1350
1351    public int countAll() throws SystemException {
1352        boolean finderClassNameCacheEnabled = RegionModelImpl.CACHE_ENABLED;
1353        String finderClassName = Region.class.getName();
1354        String finderMethodName = "countAll";
1355        String[] finderParams = new String[] {  };
1356        Object[] finderArgs = new Object[] {  };
1357
1358        Object result = null;
1359
1360        if (finderClassNameCacheEnabled) {
1361            result = FinderCacheUtil.getResult(finderClassName,
1362                    finderMethodName, finderParams, finderArgs, this);
1363        }
1364
1365        if (result == null) {
1366            Session session = null;
1367
1368            try {
1369                session = openSession();
1370
1371                Query q = session.createQuery(
1372                        "SELECT COUNT(*) FROM com.liferay.portal.model.Region");
1373
1374                Long count = null;
1375
1376                Iterator<Long> itr = q.list().iterator();
1377
1378                if (itr.hasNext()) {
1379                    count = itr.next();
1380                }
1381
1382                if (count == null) {
1383                    count = new Long(0);
1384                }
1385
1386                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1387                    finderClassName, finderMethodName, finderParams,
1388                    finderArgs, count);
1389
1390                return count.intValue();
1391            }
1392            catch (Exception e) {
1393                throw processException(e);
1394            }
1395            finally {
1396                closeSession(session);
1397            }
1398        }
1399        else {
1400            return ((Long)result).intValue();
1401        }
1402    }
1403
1404    public void registerListener(ModelListener listener) {
1405        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1406
1407        listeners.add(listener);
1408
1409        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1410    }
1411
1412    public void unregisterListener(ModelListener listener) {
1413        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1414
1415        listeners.remove(listener);
1416
1417        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1418    }
1419
1420    public void afterPropertiesSet() {
1421        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1422                    com.liferay.portal.util.PropsUtil.get(
1423                        "value.object.listener.com.liferay.portal.model.Region")));
1424
1425        if (listenerClassNames.length > 0) {
1426            try {
1427                List<ModelListener> listeners = new ArrayList<ModelListener>();
1428
1429                for (String listenerClassName : listenerClassNames) {
1430                    listeners.add((ModelListener)Class.forName(
1431                            listenerClassName).newInstance());
1432                }
1433
1434                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1435            }
1436            catch (Exception e) {
1437                _log.error(e);
1438            }
1439        }
1440    }
1441
1442    private static Log _log = LogFactory.getLog(RegionPersistenceImpl.class);
1443    private ModelListener[] _listeners = new ModelListener[0];
1444}