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