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