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