001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.NoSuchRegionException;
019    import com.liferay.portal.kernel.annotation.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.model.ModelListener;
038    import com.liferay.portal.model.Region;
039    import com.liferay.portal.model.impl.RegionImpl;
040    import com.liferay.portal.model.impl.RegionModelImpl;
041    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042    
043    import java.io.Serializable;
044    
045    import java.util.ArrayList;
046    import java.util.Collections;
047    import java.util.List;
048    
049    /**
050     * The persistence implementation for the region service.
051     *
052     * <p>
053     * Never modify or reference this class directly. Always use {@link RegionUtil} to access the region persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
054     * </p>
055     *
056     * <p>
057     * Caching information and settings can be found in <code>portal.properties</code>
058     * </p>
059     *
060     * @author Brian Wing Shun Chan
061     * @see RegionPersistence
062     * @see RegionUtil
063     * @generated
064     */
065    public class RegionPersistenceImpl extends BasePersistenceImpl<Region>
066            implements RegionPersistence {
067            public static final String FINDER_CLASS_NAME_ENTITY = RegionImpl.class.getName();
068            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
069                    ".List";
070            public static final FinderPath FINDER_PATH_FIND_BY_COUNTRYID = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
071                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
072                            "findByCountryId",
073                            new String[] {
074                                    Long.class.getName(),
075                                    
076                            "java.lang.Integer", "java.lang.Integer",
077                                    "com.liferay.portal.kernel.util.OrderByComparator"
078                            });
079            public static final FinderPath FINDER_PATH_COUNT_BY_COUNTRYID = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
080                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
081                            "countByCountryId", new String[] { Long.class.getName() });
082            public static final FinderPath FINDER_PATH_FIND_BY_ACTIVE = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
083                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
084                            "findByActive",
085                            new String[] {
086                                    Boolean.class.getName(),
087                                    
088                            "java.lang.Integer", "java.lang.Integer",
089                                    "com.liferay.portal.kernel.util.OrderByComparator"
090                            });
091            public static final FinderPath FINDER_PATH_COUNT_BY_ACTIVE = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
092                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
093                            "countByActive", new String[] { Boolean.class.getName() });
094            public static final FinderPath FINDER_PATH_FIND_BY_C_A = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
095                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
096                            "findByC_A",
097                            new String[] {
098                                    Long.class.getName(), Boolean.class.getName(),
099                                    
100                            "java.lang.Integer", "java.lang.Integer",
101                                    "com.liferay.portal.kernel.util.OrderByComparator"
102                            });
103            public static final FinderPath FINDER_PATH_COUNT_BY_C_A = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
104                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
105                            "countByC_A",
106                            new String[] { Long.class.getName(), Boolean.class.getName() });
107            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
108                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
109                            "findAll", new String[0]);
110            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
111                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
112                            "countAll", new String[0]);
113    
114            /**
115             * Caches the region in the entity cache if it is enabled.
116             *
117             * @param region the region to cache
118             */
119            public void cacheResult(Region region) {
120                    EntityCacheUtil.putResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
121                            RegionImpl.class, region.getPrimaryKey(), region);
122            }
123    
124            /**
125             * Caches the regions in the entity cache if it is enabled.
126             *
127             * @param regions the regions to cache
128             */
129            public void cacheResult(List<Region> regions) {
130                    for (Region region : regions) {
131                            if (EntityCacheUtil.getResult(
132                                                    RegionModelImpl.ENTITY_CACHE_ENABLED, RegionImpl.class,
133                                                    region.getPrimaryKey(), this) == null) {
134                                    cacheResult(region);
135                            }
136                    }
137            }
138    
139            /**
140             * Clears the cache for all regions.
141             *
142             * <p>
143             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
144             * </p>
145             */
146            public void clearCache() {
147                    CacheRegistryUtil.clear(RegionImpl.class.getName());
148                    EntityCacheUtil.clearCache(RegionImpl.class.getName());
149                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
150                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
151            }
152    
153            /**
154             * Clears the cache for the region.
155             *
156             * <p>
157             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
158             * </p>
159             */
160            public void clearCache(Region region) {
161                    EntityCacheUtil.removeResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
162                            RegionImpl.class, region.getPrimaryKey());
163            }
164    
165            /**
166             * Creates a new region with the primary key. Does not add the region to the database.
167             *
168             * @param regionId the primary key for the new region
169             * @return the new region
170             */
171            public Region create(long regionId) {
172                    Region region = new RegionImpl();
173    
174                    region.setNew(true);
175                    region.setPrimaryKey(regionId);
176    
177                    return region;
178            }
179    
180            /**
181             * Removes the region with the primary key from the database. Also notifies the appropriate model listeners.
182             *
183             * @param primaryKey the primary key of the region to remove
184             * @return the region that was removed
185             * @throws com.liferay.portal.NoSuchModelException if a region with the primary key could not be found
186             * @throws SystemException if a system exception occurred
187             */
188            public Region remove(Serializable primaryKey)
189                    throws NoSuchModelException, SystemException {
190                    return remove(((Long)primaryKey).longValue());
191            }
192    
193            /**
194             * Removes the region with the primary key from the database. Also notifies the appropriate model listeners.
195             *
196             * @param regionId the primary key of the region to remove
197             * @return the region that was removed
198             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
199             * @throws SystemException if a system exception occurred
200             */
201            public Region remove(long regionId)
202                    throws NoSuchRegionException, SystemException {
203                    Session session = null;
204    
205                    try {
206                            session = openSession();
207    
208                            Region region = (Region)session.get(RegionImpl.class,
209                                            new Long(regionId));
210    
211                            if (region == null) {
212                                    if (_log.isWarnEnabled()) {
213                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + regionId);
214                                    }
215    
216                                    throw new NoSuchRegionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
217                                            regionId);
218                            }
219    
220                            return remove(region);
221                    }
222                    catch (NoSuchRegionException nsee) {
223                            throw nsee;
224                    }
225                    catch (Exception e) {
226                            throw processException(e);
227                    }
228                    finally {
229                            closeSession(session);
230                    }
231            }
232    
233            protected Region removeImpl(Region region) throws SystemException {
234                    region = toUnwrappedModel(region);
235    
236                    Session session = null;
237    
238                    try {
239                            session = openSession();
240    
241                            BatchSessionUtil.delete(session, region);
242                    }
243                    catch (Exception e) {
244                            throw processException(e);
245                    }
246                    finally {
247                            closeSession(session);
248                    }
249    
250                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
251    
252                    EntityCacheUtil.removeResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
253                            RegionImpl.class, region.getPrimaryKey());
254    
255                    return region;
256            }
257    
258            public Region updateImpl(com.liferay.portal.model.Region region,
259                    boolean merge) throws SystemException {
260                    region = toUnwrappedModel(region);
261    
262                    Session session = null;
263    
264                    try {
265                            session = openSession();
266    
267                            BatchSessionUtil.update(session, region, merge);
268    
269                            region.setNew(false);
270                    }
271                    catch (Exception e) {
272                            throw processException(e);
273                    }
274                    finally {
275                            closeSession(session);
276                    }
277    
278                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
279    
280                    EntityCacheUtil.putResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
281                            RegionImpl.class, region.getPrimaryKey(), region);
282    
283                    return region;
284            }
285    
286            protected Region toUnwrappedModel(Region region) {
287                    if (region instanceof RegionImpl) {
288                            return region;
289                    }
290    
291                    RegionImpl regionImpl = new RegionImpl();
292    
293                    regionImpl.setNew(region.isNew());
294                    regionImpl.setPrimaryKey(region.getPrimaryKey());
295    
296                    regionImpl.setRegionId(region.getRegionId());
297                    regionImpl.setCountryId(region.getCountryId());
298                    regionImpl.setRegionCode(region.getRegionCode());
299                    regionImpl.setName(region.getName());
300                    regionImpl.setActive(region.isActive());
301    
302                    return regionImpl;
303            }
304    
305            /**
306             * Finds the region with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
307             *
308             * @param primaryKey the primary key of the region to find
309             * @return the region
310             * @throws com.liferay.portal.NoSuchModelException if a region with the primary key could not be found
311             * @throws SystemException if a system exception occurred
312             */
313            public Region findByPrimaryKey(Serializable primaryKey)
314                    throws NoSuchModelException, SystemException {
315                    return findByPrimaryKey(((Long)primaryKey).longValue());
316            }
317    
318            /**
319             * Finds the region with the primary key or throws a {@link com.liferay.portal.NoSuchRegionException} if it could not be found.
320             *
321             * @param regionId the primary key of the region to find
322             * @return the region
323             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
324             * @throws SystemException if a system exception occurred
325             */
326            public Region findByPrimaryKey(long regionId)
327                    throws NoSuchRegionException, SystemException {
328                    Region region = fetchByPrimaryKey(regionId);
329    
330                    if (region == null) {
331                            if (_log.isWarnEnabled()) {
332                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + regionId);
333                            }
334    
335                            throw new NoSuchRegionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
336                                    regionId);
337                    }
338    
339                    return region;
340            }
341    
342            /**
343             * Finds the region with the primary key or returns <code>null</code> if it could not be found.
344             *
345             * @param primaryKey the primary key of the region to find
346             * @return the region, or <code>null</code> if a region with the primary key could not be found
347             * @throws SystemException if a system exception occurred
348             */
349            public Region fetchByPrimaryKey(Serializable primaryKey)
350                    throws SystemException {
351                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
352            }
353    
354            /**
355             * Finds the region with the primary key or returns <code>null</code> if it could not be found.
356             *
357             * @param regionId the primary key of the region to find
358             * @return the region, or <code>null</code> if a region with the primary key could not be found
359             * @throws SystemException if a system exception occurred
360             */
361            public Region fetchByPrimaryKey(long regionId) throws SystemException {
362                    Region region = (Region)EntityCacheUtil.getResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
363                                    RegionImpl.class, regionId, this);
364    
365                    if (region == null) {
366                            Session session = null;
367    
368                            try {
369                                    session = openSession();
370    
371                                    region = (Region)session.get(RegionImpl.class,
372                                                    new Long(regionId));
373                            }
374                            catch (Exception e) {
375                                    throw processException(e);
376                            }
377                            finally {
378                                    if (region != null) {
379                                            cacheResult(region);
380                                    }
381    
382                                    closeSession(session);
383                            }
384                    }
385    
386                    return region;
387            }
388    
389            /**
390             * Finds all the regions where countryId = &#63;.
391             *
392             * @param countryId the country id to search with
393             * @return the matching regions
394             * @throws SystemException if a system exception occurred
395             */
396            public List<Region> findByCountryId(long countryId)
397                    throws SystemException {
398                    return findByCountryId(countryId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
399                            null);
400            }
401    
402            /**
403             * Finds a range of all the regions where countryId = &#63;.
404             *
405             * <p>
406             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
407             * </p>
408             *
409             * @param countryId the country id to search with
410             * @param start the lower bound of the range of regions to return
411             * @param end the upper bound of the range of regions to return (not inclusive)
412             * @return the range of matching regions
413             * @throws SystemException if a system exception occurred
414             */
415            public List<Region> findByCountryId(long countryId, int start, int end)
416                    throws SystemException {
417                    return findByCountryId(countryId, start, end, null);
418            }
419    
420            /**
421             * Finds an ordered range of all the regions where countryId = &#63;.
422             *
423             * <p>
424             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
425             * </p>
426             *
427             * @param countryId the country id to search with
428             * @param start the lower bound of the range of regions to return
429             * @param end the upper bound of the range of regions to return (not inclusive)
430             * @param orderByComparator the comparator to order the results by
431             * @return the ordered range of matching regions
432             * @throws SystemException if a system exception occurred
433             */
434            public List<Region> findByCountryId(long countryId, int start, int end,
435                    OrderByComparator orderByComparator) throws SystemException {
436                    Object[] finderArgs = new Object[] {
437                                    countryId,
438                                    
439                                    String.valueOf(start), String.valueOf(end),
440                                    String.valueOf(orderByComparator)
441                            };
442    
443                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COUNTRYID,
444                                    finderArgs, this);
445    
446                    if (list == null) {
447                            StringBundler query = null;
448    
449                            if (orderByComparator != null) {
450                                    query = new StringBundler(3 +
451                                                    (orderByComparator.getOrderByFields().length * 3));
452                            }
453                            else {
454                                    query = new StringBundler(3);
455                            }
456    
457                            query.append(_SQL_SELECT_REGION_WHERE);
458    
459                            query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
460    
461                            if (orderByComparator != null) {
462                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
463                                            orderByComparator);
464                            }
465    
466                            else {
467                                    query.append(RegionModelImpl.ORDER_BY_JPQL);
468                            }
469    
470                            String sql = query.toString();
471    
472                            Session session = null;
473    
474                            try {
475                                    session = openSession();
476    
477                                    Query q = session.createQuery(sql);
478    
479                                    QueryPos qPos = QueryPos.getInstance(q);
480    
481                                    qPos.add(countryId);
482    
483                                    list = (List<Region>)QueryUtil.list(q, getDialect(), start, end);
484                            }
485                            catch (Exception e) {
486                                    throw processException(e);
487                            }
488                            finally {
489                                    if (list == null) {
490                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_COUNTRYID,
491                                                    finderArgs);
492                                    }
493                                    else {
494                                            cacheResult(list);
495    
496                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COUNTRYID,
497                                                    finderArgs, list);
498                                    }
499    
500                                    closeSession(session);
501                            }
502                    }
503    
504                    return list;
505            }
506    
507            /**
508             * Finds the first region in the ordered set where countryId = &#63;.
509             *
510             * <p>
511             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
512             * </p>
513             *
514             * @param countryId the country id to search with
515             * @param orderByComparator the comparator to order the set by
516             * @return the first matching region
517             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
518             * @throws SystemException if a system exception occurred
519             */
520            public Region findByCountryId_First(long countryId,
521                    OrderByComparator orderByComparator)
522                    throws NoSuchRegionException, SystemException {
523                    List<Region> list = findByCountryId(countryId, 0, 1, orderByComparator);
524    
525                    if (list.isEmpty()) {
526                            StringBundler msg = new StringBundler(4);
527    
528                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
529    
530                            msg.append("countryId=");
531                            msg.append(countryId);
532    
533                            msg.append(StringPool.CLOSE_CURLY_BRACE);
534    
535                            throw new NoSuchRegionException(msg.toString());
536                    }
537                    else {
538                            return list.get(0);
539                    }
540            }
541    
542            /**
543             * Finds the last region in the ordered set where countryId = &#63;.
544             *
545             * <p>
546             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
547             * </p>
548             *
549             * @param countryId the country id to search with
550             * @param orderByComparator the comparator to order the set by
551             * @return the last matching region
552             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
553             * @throws SystemException if a system exception occurred
554             */
555            public Region findByCountryId_Last(long countryId,
556                    OrderByComparator orderByComparator)
557                    throws NoSuchRegionException, SystemException {
558                    int count = countByCountryId(countryId);
559    
560                    List<Region> list = findByCountryId(countryId, count - 1, count,
561                                    orderByComparator);
562    
563                    if (list.isEmpty()) {
564                            StringBundler msg = new StringBundler(4);
565    
566                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
567    
568                            msg.append("countryId=");
569                            msg.append(countryId);
570    
571                            msg.append(StringPool.CLOSE_CURLY_BRACE);
572    
573                            throw new NoSuchRegionException(msg.toString());
574                    }
575                    else {
576                            return list.get(0);
577                    }
578            }
579    
580            /**
581             * Finds the regions before and after the current region in the ordered set where countryId = &#63;.
582             *
583             * <p>
584             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
585             * </p>
586             *
587             * @param regionId the primary key of the current region
588             * @param countryId the country id to search with
589             * @param orderByComparator the comparator to order the set by
590             * @return the previous, current, and next region
591             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
592             * @throws SystemException if a system exception occurred
593             */
594            public Region[] findByCountryId_PrevAndNext(long regionId, long countryId,
595                    OrderByComparator orderByComparator)
596                    throws NoSuchRegionException, SystemException {
597                    Region region = findByPrimaryKey(regionId);
598    
599                    Session session = null;
600    
601                    try {
602                            session = openSession();
603    
604                            Region[] array = new RegionImpl[3];
605    
606                            array[0] = getByCountryId_PrevAndNext(session, region, countryId,
607                                            orderByComparator, true);
608    
609                            array[1] = region;
610    
611                            array[2] = getByCountryId_PrevAndNext(session, region, countryId,
612                                            orderByComparator, false);
613    
614                            return array;
615                    }
616                    catch (Exception e) {
617                            throw processException(e);
618                    }
619                    finally {
620                            closeSession(session);
621                    }
622            }
623    
624            protected Region getByCountryId_PrevAndNext(Session session, Region region,
625                    long countryId, OrderByComparator orderByComparator, boolean previous) {
626                    StringBundler query = null;
627    
628                    if (orderByComparator != null) {
629                            query = new StringBundler(6 +
630                                            (orderByComparator.getOrderByFields().length * 6));
631                    }
632                    else {
633                            query = new StringBundler(3);
634                    }
635    
636                    query.append(_SQL_SELECT_REGION_WHERE);
637    
638                    query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
639    
640                    if (orderByComparator != null) {
641                            String[] orderByFields = orderByComparator.getOrderByFields();
642    
643                            if (orderByFields.length > 0) {
644                                    query.append(WHERE_AND);
645                            }
646    
647                            for (int i = 0; i < orderByFields.length; i++) {
648                                    query.append(_ORDER_BY_ENTITY_ALIAS);
649                                    query.append(orderByFields[i]);
650    
651                                    if ((i + 1) < orderByFields.length) {
652                                            if (orderByComparator.isAscending() ^ previous) {
653                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
654                                            }
655                                            else {
656                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
657                                            }
658                                    }
659                                    else {
660                                            if (orderByComparator.isAscending() ^ previous) {
661                                                    query.append(WHERE_GREATER_THAN);
662                                            }
663                                            else {
664                                                    query.append(WHERE_LESSER_THAN);
665                                            }
666                                    }
667                            }
668    
669                            query.append(ORDER_BY_CLAUSE);
670    
671                            for (int i = 0; i < orderByFields.length; i++) {
672                                    query.append(_ORDER_BY_ENTITY_ALIAS);
673                                    query.append(orderByFields[i]);
674    
675                                    if ((i + 1) < orderByFields.length) {
676                                            if (orderByComparator.isAscending() ^ previous) {
677                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
678                                            }
679                                            else {
680                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
681                                            }
682                                    }
683                                    else {
684                                            if (orderByComparator.isAscending() ^ previous) {
685                                                    query.append(ORDER_BY_ASC);
686                                            }
687                                            else {
688                                                    query.append(ORDER_BY_DESC);
689                                            }
690                                    }
691                            }
692                    }
693    
694                    else {
695                            query.append(RegionModelImpl.ORDER_BY_JPQL);
696                    }
697    
698                    String sql = query.toString();
699    
700                    Query q = session.createQuery(sql);
701    
702                    q.setFirstResult(0);
703                    q.setMaxResults(2);
704    
705                    QueryPos qPos = QueryPos.getInstance(q);
706    
707                    qPos.add(countryId);
708    
709                    if (orderByComparator != null) {
710                            Object[] values = orderByComparator.getOrderByValues(region);
711    
712                            for (Object value : values) {
713                                    qPos.add(value);
714                            }
715                    }
716    
717                    List<Region> list = q.list();
718    
719                    if (list.size() == 2) {
720                            return list.get(1);
721                    }
722                    else {
723                            return null;
724                    }
725            }
726    
727            /**
728             * Finds all the regions where active = &#63;.
729             *
730             * @param active the active to search with
731             * @return the matching regions
732             * @throws SystemException if a system exception occurred
733             */
734            public List<Region> findByActive(boolean active) throws SystemException {
735                    return findByActive(active, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
736            }
737    
738            /**
739             * Finds a range of all the regions where active = &#63;.
740             *
741             * <p>
742             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
743             * </p>
744             *
745             * @param active the active to search with
746             * @param start the lower bound of the range of regions to return
747             * @param end the upper bound of the range of regions to return (not inclusive)
748             * @return the range of matching regions
749             * @throws SystemException if a system exception occurred
750             */
751            public List<Region> findByActive(boolean active, int start, int end)
752                    throws SystemException {
753                    return findByActive(active, start, end, null);
754            }
755    
756            /**
757             * Finds an ordered range of all the regions where active = &#63;.
758             *
759             * <p>
760             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
761             * </p>
762             *
763             * @param active the active to search with
764             * @param start the lower bound of the range of regions to return
765             * @param end the upper bound of the range of regions to return (not inclusive)
766             * @param orderByComparator the comparator to order the results by
767             * @return the ordered range of matching regions
768             * @throws SystemException if a system exception occurred
769             */
770            public List<Region> findByActive(boolean active, int start, int end,
771                    OrderByComparator orderByComparator) throws SystemException {
772                    Object[] finderArgs = new Object[] {
773                                    active,
774                                    
775                                    String.valueOf(start), String.valueOf(end),
776                                    String.valueOf(orderByComparator)
777                            };
778    
779                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_ACTIVE,
780                                    finderArgs, this);
781    
782                    if (list == null) {
783                            StringBundler query = null;
784    
785                            if (orderByComparator != null) {
786                                    query = new StringBundler(3 +
787                                                    (orderByComparator.getOrderByFields().length * 3));
788                            }
789                            else {
790                                    query = new StringBundler(3);
791                            }
792    
793                            query.append(_SQL_SELECT_REGION_WHERE);
794    
795                            query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
796    
797                            if (orderByComparator != null) {
798                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
799                                            orderByComparator);
800                            }
801    
802                            else {
803                                    query.append(RegionModelImpl.ORDER_BY_JPQL);
804                            }
805    
806                            String sql = query.toString();
807    
808                            Session session = null;
809    
810                            try {
811                                    session = openSession();
812    
813                                    Query q = session.createQuery(sql);
814    
815                                    QueryPos qPos = QueryPos.getInstance(q);
816    
817                                    qPos.add(active);
818    
819                                    list = (List<Region>)QueryUtil.list(q, getDialect(), start, end);
820                            }
821                            catch (Exception e) {
822                                    throw processException(e);
823                            }
824                            finally {
825                                    if (list == null) {
826                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_ACTIVE,
827                                                    finderArgs);
828                                    }
829                                    else {
830                                            cacheResult(list);
831    
832                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_ACTIVE,
833                                                    finderArgs, list);
834                                    }
835    
836                                    closeSession(session);
837                            }
838                    }
839    
840                    return list;
841            }
842    
843            /**
844             * Finds the first region in the ordered set where active = &#63;.
845             *
846             * <p>
847             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
848             * </p>
849             *
850             * @param active the active to search with
851             * @param orderByComparator the comparator to order the set by
852             * @return the first matching region
853             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
854             * @throws SystemException if a system exception occurred
855             */
856            public Region findByActive_First(boolean active,
857                    OrderByComparator orderByComparator)
858                    throws NoSuchRegionException, SystemException {
859                    List<Region> list = findByActive(active, 0, 1, orderByComparator);
860    
861                    if (list.isEmpty()) {
862                            StringBundler msg = new StringBundler(4);
863    
864                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
865    
866                            msg.append("active=");
867                            msg.append(active);
868    
869                            msg.append(StringPool.CLOSE_CURLY_BRACE);
870    
871                            throw new NoSuchRegionException(msg.toString());
872                    }
873                    else {
874                            return list.get(0);
875                    }
876            }
877    
878            /**
879             * Finds the last region in the ordered set where active = &#63;.
880             *
881             * <p>
882             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
883             * </p>
884             *
885             * @param active the active to search with
886             * @param orderByComparator the comparator to order the set by
887             * @return the last matching region
888             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
889             * @throws SystemException if a system exception occurred
890             */
891            public Region findByActive_Last(boolean active,
892                    OrderByComparator orderByComparator)
893                    throws NoSuchRegionException, SystemException {
894                    int count = countByActive(active);
895    
896                    List<Region> list = findByActive(active, count - 1, count,
897                                    orderByComparator);
898    
899                    if (list.isEmpty()) {
900                            StringBundler msg = new StringBundler(4);
901    
902                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
903    
904                            msg.append("active=");
905                            msg.append(active);
906    
907                            msg.append(StringPool.CLOSE_CURLY_BRACE);
908    
909                            throw new NoSuchRegionException(msg.toString());
910                    }
911                    else {
912                            return list.get(0);
913                    }
914            }
915    
916            /**
917             * Finds the regions before and after the current region in the ordered set where active = &#63;.
918             *
919             * <p>
920             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
921             * </p>
922             *
923             * @param regionId the primary key of the current region
924             * @param active the active to search with
925             * @param orderByComparator the comparator to order the set by
926             * @return the previous, current, and next region
927             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
928             * @throws SystemException if a system exception occurred
929             */
930            public Region[] findByActive_PrevAndNext(long regionId, boolean active,
931                    OrderByComparator orderByComparator)
932                    throws NoSuchRegionException, SystemException {
933                    Region region = findByPrimaryKey(regionId);
934    
935                    Session session = null;
936    
937                    try {
938                            session = openSession();
939    
940                            Region[] array = new RegionImpl[3];
941    
942                            array[0] = getByActive_PrevAndNext(session, region, active,
943                                            orderByComparator, true);
944    
945                            array[1] = region;
946    
947                            array[2] = getByActive_PrevAndNext(session, region, active,
948                                            orderByComparator, false);
949    
950                            return array;
951                    }
952                    catch (Exception e) {
953                            throw processException(e);
954                    }
955                    finally {
956                            closeSession(session);
957                    }
958            }
959    
960            protected Region getByActive_PrevAndNext(Session session, Region region,
961                    boolean active, OrderByComparator orderByComparator, boolean previous) {
962                    StringBundler query = null;
963    
964                    if (orderByComparator != null) {
965                            query = new StringBundler(6 +
966                                            (orderByComparator.getOrderByFields().length * 6));
967                    }
968                    else {
969                            query = new StringBundler(3);
970                    }
971    
972                    query.append(_SQL_SELECT_REGION_WHERE);
973    
974                    query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
975    
976                    if (orderByComparator != null) {
977                            String[] orderByFields = orderByComparator.getOrderByFields();
978    
979                            if (orderByFields.length > 0) {
980                                    query.append(WHERE_AND);
981                            }
982    
983                            for (int i = 0; i < orderByFields.length; i++) {
984                                    query.append(_ORDER_BY_ENTITY_ALIAS);
985                                    query.append(orderByFields[i]);
986    
987                                    if ((i + 1) < orderByFields.length) {
988                                            if (orderByComparator.isAscending() ^ previous) {
989                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
990                                            }
991                                            else {
992                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
993                                            }
994                                    }
995                                    else {
996                                            if (orderByComparator.isAscending() ^ previous) {
997                                                    query.append(WHERE_GREATER_THAN);
998                                            }
999                                            else {
1000                                                    query.append(WHERE_LESSER_THAN);
1001                                            }
1002                                    }
1003                            }
1004    
1005                            query.append(ORDER_BY_CLAUSE);
1006    
1007                            for (int i = 0; i < orderByFields.length; i++) {
1008                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1009                                    query.append(orderByFields[i]);
1010    
1011                                    if ((i + 1) < orderByFields.length) {
1012                                            if (orderByComparator.isAscending() ^ previous) {
1013                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1014                                            }
1015                                            else {
1016                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1017                                            }
1018                                    }
1019                                    else {
1020                                            if (orderByComparator.isAscending() ^ previous) {
1021                                                    query.append(ORDER_BY_ASC);
1022                                            }
1023                                            else {
1024                                                    query.append(ORDER_BY_DESC);
1025                                            }
1026                                    }
1027                            }
1028                    }
1029    
1030                    else {
1031                            query.append(RegionModelImpl.ORDER_BY_JPQL);
1032                    }
1033    
1034                    String sql = query.toString();
1035    
1036                    Query q = session.createQuery(sql);
1037    
1038                    q.setFirstResult(0);
1039                    q.setMaxResults(2);
1040    
1041                    QueryPos qPos = QueryPos.getInstance(q);
1042    
1043                    qPos.add(active);
1044    
1045                    if (orderByComparator != null) {
1046                            Object[] values = orderByComparator.getOrderByValues(region);
1047    
1048                            for (Object value : values) {
1049                                    qPos.add(value);
1050                            }
1051                    }
1052    
1053                    List<Region> list = q.list();
1054    
1055                    if (list.size() == 2) {
1056                            return list.get(1);
1057                    }
1058                    else {
1059                            return null;
1060                    }
1061            }
1062    
1063            /**
1064             * Finds all the regions where countryId = &#63; and active = &#63;.
1065             *
1066             * @param countryId the country id to search with
1067             * @param active the active to search with
1068             * @return the matching regions
1069             * @throws SystemException if a system exception occurred
1070             */
1071            public List<Region> findByC_A(long countryId, boolean active)
1072                    throws SystemException {
1073                    return findByC_A(countryId, active, QueryUtil.ALL_POS,
1074                            QueryUtil.ALL_POS, null);
1075            }
1076    
1077            /**
1078             * Finds a range of all the regions where countryId = &#63; and active = &#63;.
1079             *
1080             * <p>
1081             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1082             * </p>
1083             *
1084             * @param countryId the country id to search with
1085             * @param active the active to search with
1086             * @param start the lower bound of the range of regions to return
1087             * @param end the upper bound of the range of regions to return (not inclusive)
1088             * @return the range of matching regions
1089             * @throws SystemException if a system exception occurred
1090             */
1091            public List<Region> findByC_A(long countryId, boolean active, int start,
1092                    int end) throws SystemException {
1093                    return findByC_A(countryId, active, start, end, null);
1094            }
1095    
1096            /**
1097             * Finds an ordered range of all the regions where countryId = &#63; and active = &#63;.
1098             *
1099             * <p>
1100             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1101             * </p>
1102             *
1103             * @param countryId the country id to search with
1104             * @param active the active to search with
1105             * @param start the lower bound of the range of regions to return
1106             * @param end the upper bound of the range of regions to return (not inclusive)
1107             * @param orderByComparator the comparator to order the results by
1108             * @return the ordered range of matching regions
1109             * @throws SystemException if a system exception occurred
1110             */
1111            public List<Region> findByC_A(long countryId, boolean active, int start,
1112                    int end, OrderByComparator orderByComparator) throws SystemException {
1113                    Object[] finderArgs = new Object[] {
1114                                    countryId, active,
1115                                    
1116                                    String.valueOf(start), String.valueOf(end),
1117                                    String.valueOf(orderByComparator)
1118                            };
1119    
1120                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_A,
1121                                    finderArgs, this);
1122    
1123                    if (list == null) {
1124                            StringBundler query = null;
1125    
1126                            if (orderByComparator != null) {
1127                                    query = new StringBundler(4 +
1128                                                    (orderByComparator.getOrderByFields().length * 3));
1129                            }
1130                            else {
1131                                    query = new StringBundler(4);
1132                            }
1133    
1134                            query.append(_SQL_SELECT_REGION_WHERE);
1135    
1136                            query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1137    
1138                            query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1139    
1140                            if (orderByComparator != null) {
1141                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1142                                            orderByComparator);
1143                            }
1144    
1145                            else {
1146                                    query.append(RegionModelImpl.ORDER_BY_JPQL);
1147                            }
1148    
1149                            String sql = query.toString();
1150    
1151                            Session session = null;
1152    
1153                            try {
1154                                    session = openSession();
1155    
1156                                    Query q = session.createQuery(sql);
1157    
1158                                    QueryPos qPos = QueryPos.getInstance(q);
1159    
1160                                    qPos.add(countryId);
1161    
1162                                    qPos.add(active);
1163    
1164                                    list = (List<Region>)QueryUtil.list(q, getDialect(), start, end);
1165                            }
1166                            catch (Exception e) {
1167                                    throw processException(e);
1168                            }
1169                            finally {
1170                                    if (list == null) {
1171                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_C_A,
1172                                                    finderArgs);
1173                                    }
1174                                    else {
1175                                            cacheResult(list);
1176    
1177                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_A,
1178                                                    finderArgs, list);
1179                                    }
1180    
1181                                    closeSession(session);
1182                            }
1183                    }
1184    
1185                    return list;
1186            }
1187    
1188            /**
1189             * Finds the first region in the ordered set where countryId = &#63; and active = &#63;.
1190             *
1191             * <p>
1192             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1193             * </p>
1194             *
1195             * @param countryId the country id to search with
1196             * @param active the active to search with
1197             * @param orderByComparator the comparator to order the set by
1198             * @return the first matching region
1199             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
1200             * @throws SystemException if a system exception occurred
1201             */
1202            public Region findByC_A_First(long countryId, boolean active,
1203                    OrderByComparator orderByComparator)
1204                    throws NoSuchRegionException, SystemException {
1205                    List<Region> list = findByC_A(countryId, active, 0, 1, orderByComparator);
1206    
1207                    if (list.isEmpty()) {
1208                            StringBundler msg = new StringBundler(6);
1209    
1210                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1211    
1212                            msg.append("countryId=");
1213                            msg.append(countryId);
1214    
1215                            msg.append(", active=");
1216                            msg.append(active);
1217    
1218                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1219    
1220                            throw new NoSuchRegionException(msg.toString());
1221                    }
1222                    else {
1223                            return list.get(0);
1224                    }
1225            }
1226    
1227            /**
1228             * Finds the last region in the ordered set where countryId = &#63; and active = &#63;.
1229             *
1230             * <p>
1231             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1232             * </p>
1233             *
1234             * @param countryId the country id to search with
1235             * @param active the active to search with
1236             * @param orderByComparator the comparator to order the set by
1237             * @return the last matching region
1238             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
1239             * @throws SystemException if a system exception occurred
1240             */
1241            public Region findByC_A_Last(long countryId, boolean active,
1242                    OrderByComparator orderByComparator)
1243                    throws NoSuchRegionException, SystemException {
1244                    int count = countByC_A(countryId, active);
1245    
1246                    List<Region> list = findByC_A(countryId, active, count - 1, count,
1247                                    orderByComparator);
1248    
1249                    if (list.isEmpty()) {
1250                            StringBundler msg = new StringBundler(6);
1251    
1252                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1253    
1254                            msg.append("countryId=");
1255                            msg.append(countryId);
1256    
1257                            msg.append(", active=");
1258                            msg.append(active);
1259    
1260                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1261    
1262                            throw new NoSuchRegionException(msg.toString());
1263                    }
1264                    else {
1265                            return list.get(0);
1266                    }
1267            }
1268    
1269            /**
1270             * Finds the regions before and after the current region in the ordered set where countryId = &#63; and active = &#63;.
1271             *
1272             * <p>
1273             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1274             * </p>
1275             *
1276             * @param regionId the primary key of the current region
1277             * @param countryId the country id to search with
1278             * @param active the active to search with
1279             * @param orderByComparator the comparator to order the set by
1280             * @return the previous, current, and next region
1281             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
1282             * @throws SystemException if a system exception occurred
1283             */
1284            public Region[] findByC_A_PrevAndNext(long regionId, long countryId,
1285                    boolean active, OrderByComparator orderByComparator)
1286                    throws NoSuchRegionException, SystemException {
1287                    Region region = findByPrimaryKey(regionId);
1288    
1289                    Session session = null;
1290    
1291                    try {
1292                            session = openSession();
1293    
1294                            Region[] array = new RegionImpl[3];
1295    
1296                            array[0] = getByC_A_PrevAndNext(session, region, countryId, active,
1297                                            orderByComparator, true);
1298    
1299                            array[1] = region;
1300    
1301                            array[2] = getByC_A_PrevAndNext(session, region, countryId, active,
1302                                            orderByComparator, false);
1303    
1304                            return array;
1305                    }
1306                    catch (Exception e) {
1307                            throw processException(e);
1308                    }
1309                    finally {
1310                            closeSession(session);
1311                    }
1312            }
1313    
1314            protected Region getByC_A_PrevAndNext(Session session, Region region,
1315                    long countryId, boolean active, OrderByComparator orderByComparator,
1316                    boolean previous) {
1317                    StringBundler query = null;
1318    
1319                    if (orderByComparator != null) {
1320                            query = new StringBundler(6 +
1321                                            (orderByComparator.getOrderByFields().length * 6));
1322                    }
1323                    else {
1324                            query = new StringBundler(3);
1325                    }
1326    
1327                    query.append(_SQL_SELECT_REGION_WHERE);
1328    
1329                    query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1330    
1331                    query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1332    
1333                    if (orderByComparator != null) {
1334                            String[] orderByFields = orderByComparator.getOrderByFields();
1335    
1336                            if (orderByFields.length > 0) {
1337                                    query.append(WHERE_AND);
1338                            }
1339    
1340                            for (int i = 0; i < orderByFields.length; i++) {
1341                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1342                                    query.append(orderByFields[i]);
1343    
1344                                    if ((i + 1) < orderByFields.length) {
1345                                            if (orderByComparator.isAscending() ^ previous) {
1346                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1347                                            }
1348                                            else {
1349                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1350                                            }
1351                                    }
1352                                    else {
1353                                            if (orderByComparator.isAscending() ^ previous) {
1354                                                    query.append(WHERE_GREATER_THAN);
1355                                            }
1356                                            else {
1357                                                    query.append(WHERE_LESSER_THAN);
1358                                            }
1359                                    }
1360                            }
1361    
1362                            query.append(ORDER_BY_CLAUSE);
1363    
1364                            for (int i = 0; i < orderByFields.length; i++) {
1365                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1366                                    query.append(orderByFields[i]);
1367    
1368                                    if ((i + 1) < orderByFields.length) {
1369                                            if (orderByComparator.isAscending() ^ previous) {
1370                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1371                                            }
1372                                            else {
1373                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1374                                            }
1375                                    }
1376                                    else {
1377                                            if (orderByComparator.isAscending() ^ previous) {
1378                                                    query.append(ORDER_BY_ASC);
1379                                            }
1380                                            else {
1381                                                    query.append(ORDER_BY_DESC);
1382                                            }
1383                                    }
1384                            }
1385                    }
1386    
1387                    else {
1388                            query.append(RegionModelImpl.ORDER_BY_JPQL);
1389                    }
1390    
1391                    String sql = query.toString();
1392    
1393                    Query q = session.createQuery(sql);
1394    
1395                    q.setFirstResult(0);
1396                    q.setMaxResults(2);
1397    
1398                    QueryPos qPos = QueryPos.getInstance(q);
1399    
1400                    qPos.add(countryId);
1401    
1402                    qPos.add(active);
1403    
1404                    if (orderByComparator != null) {
1405                            Object[] values = orderByComparator.getOrderByValues(region);
1406    
1407                            for (Object value : values) {
1408                                    qPos.add(value);
1409                            }
1410                    }
1411    
1412                    List<Region> list = q.list();
1413    
1414                    if (list.size() == 2) {
1415                            return list.get(1);
1416                    }
1417                    else {
1418                            return null;
1419                    }
1420            }
1421    
1422            /**
1423             * Finds all the regions.
1424             *
1425             * @return the regions
1426             * @throws SystemException if a system exception occurred
1427             */
1428            public List<Region> findAll() throws SystemException {
1429                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1430            }
1431    
1432            /**
1433             * Finds a range of all the regions.
1434             *
1435             * <p>
1436             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1437             * </p>
1438             *
1439             * @param start the lower bound of the range of regions to return
1440             * @param end the upper bound of the range of regions to return (not inclusive)
1441             * @return the range of regions
1442             * @throws SystemException if a system exception occurred
1443             */
1444            public List<Region> findAll(int start, int end) throws SystemException {
1445                    return findAll(start, end, null);
1446            }
1447    
1448            /**
1449             * Finds an ordered range of all the regions.
1450             *
1451             * <p>
1452             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1453             * </p>
1454             *
1455             * @param start the lower bound of the range of regions to return
1456             * @param end the upper bound of the range of regions to return (not inclusive)
1457             * @param orderByComparator the comparator to order the results by
1458             * @return the ordered range of regions
1459             * @throws SystemException if a system exception occurred
1460             */
1461            public List<Region> findAll(int start, int end,
1462                    OrderByComparator orderByComparator) throws SystemException {
1463                    Object[] finderArgs = new Object[] {
1464                                    String.valueOf(start), String.valueOf(end),
1465                                    String.valueOf(orderByComparator)
1466                            };
1467    
1468                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1469                                    finderArgs, this);
1470    
1471                    if (list == null) {
1472                            StringBundler query = null;
1473                            String sql = null;
1474    
1475                            if (orderByComparator != null) {
1476                                    query = new StringBundler(2 +
1477                                                    (orderByComparator.getOrderByFields().length * 3));
1478    
1479                                    query.append(_SQL_SELECT_REGION);
1480    
1481                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1482                                            orderByComparator);
1483    
1484                                    sql = query.toString();
1485                            }
1486                            else {
1487                                    sql = _SQL_SELECT_REGION.concat(RegionModelImpl.ORDER_BY_JPQL);
1488                            }
1489    
1490                            Session session = null;
1491    
1492                            try {
1493                                    session = openSession();
1494    
1495                                    Query q = session.createQuery(sql);
1496    
1497                                    if (orderByComparator == null) {
1498                                            list = (List<Region>)QueryUtil.list(q, getDialect(), start,
1499                                                            end, false);
1500    
1501                                            Collections.sort(list);
1502                                    }
1503                                    else {
1504                                            list = (List<Region>)QueryUtil.list(q, getDialect(), start,
1505                                                            end);
1506                                    }
1507                            }
1508                            catch (Exception e) {
1509                                    throw processException(e);
1510                            }
1511                            finally {
1512                                    if (list == null) {
1513                                            FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
1514                                                    finderArgs);
1515                                    }
1516                                    else {
1517                                            cacheResult(list);
1518    
1519                                            FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
1520                                                    list);
1521                                    }
1522    
1523                                    closeSession(session);
1524                            }
1525                    }
1526    
1527                    return list;
1528            }
1529    
1530            /**
1531             * Removes all the regions where countryId = &#63; from the database.
1532             *
1533             * @param countryId the country id to search with
1534             * @throws SystemException if a system exception occurred
1535             */
1536            public void removeByCountryId(long countryId) throws SystemException {
1537                    for (Region region : findByCountryId(countryId)) {
1538                            remove(region);
1539                    }
1540            }
1541    
1542            /**
1543             * Removes all the regions where active = &#63; from the database.
1544             *
1545             * @param active the active to search with
1546             * @throws SystemException if a system exception occurred
1547             */
1548            public void removeByActive(boolean active) throws SystemException {
1549                    for (Region region : findByActive(active)) {
1550                            remove(region);
1551                    }
1552            }
1553    
1554            /**
1555             * Removes all the regions where countryId = &#63; and active = &#63; from the database.
1556             *
1557             * @param countryId the country id to search with
1558             * @param active the active to search with
1559             * @throws SystemException if a system exception occurred
1560             */
1561            public void removeByC_A(long countryId, boolean active)
1562                    throws SystemException {
1563                    for (Region region : findByC_A(countryId, active)) {
1564                            remove(region);
1565                    }
1566            }
1567    
1568            /**
1569             * Removes all the regions from the database.
1570             *
1571             * @throws SystemException if a system exception occurred
1572             */
1573            public void removeAll() throws SystemException {
1574                    for (Region region : findAll()) {
1575                            remove(region);
1576                    }
1577            }
1578    
1579            /**
1580             * Counts all the regions where countryId = &#63;.
1581             *
1582             * @param countryId the country id to search with
1583             * @return the number of matching regions
1584             * @throws SystemException if a system exception occurred
1585             */
1586            public int countByCountryId(long countryId) throws SystemException {
1587                    Object[] finderArgs = new Object[] { countryId };
1588    
1589                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COUNTRYID,
1590                                    finderArgs, this);
1591    
1592                    if (count == null) {
1593                            StringBundler query = new StringBundler(2);
1594    
1595                            query.append(_SQL_COUNT_REGION_WHERE);
1596    
1597                            query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
1598    
1599                            String sql = query.toString();
1600    
1601                            Session session = null;
1602    
1603                            try {
1604                                    session = openSession();
1605    
1606                                    Query q = session.createQuery(sql);
1607    
1608                                    QueryPos qPos = QueryPos.getInstance(q);
1609    
1610                                    qPos.add(countryId);
1611    
1612                                    count = (Long)q.uniqueResult();
1613                            }
1614                            catch (Exception e) {
1615                                    throw processException(e);
1616                            }
1617                            finally {
1618                                    if (count == null) {
1619                                            count = Long.valueOf(0);
1620                                    }
1621    
1622                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COUNTRYID,
1623                                            finderArgs, count);
1624    
1625                                    closeSession(session);
1626                            }
1627                    }
1628    
1629                    return count.intValue();
1630            }
1631    
1632            /**
1633             * Counts all the regions where active = &#63;.
1634             *
1635             * @param active the active to search with
1636             * @return the number of matching regions
1637             * @throws SystemException if a system exception occurred
1638             */
1639            public int countByActive(boolean active) throws SystemException {
1640                    Object[] finderArgs = new Object[] { active };
1641    
1642                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_ACTIVE,
1643                                    finderArgs, this);
1644    
1645                    if (count == null) {
1646                            StringBundler query = new StringBundler(2);
1647    
1648                            query.append(_SQL_COUNT_REGION_WHERE);
1649    
1650                            query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
1651    
1652                            String sql = query.toString();
1653    
1654                            Session session = null;
1655    
1656                            try {
1657                                    session = openSession();
1658    
1659                                    Query q = session.createQuery(sql);
1660    
1661                                    QueryPos qPos = QueryPos.getInstance(q);
1662    
1663                                    qPos.add(active);
1664    
1665                                    count = (Long)q.uniqueResult();
1666                            }
1667                            catch (Exception e) {
1668                                    throw processException(e);
1669                            }
1670                            finally {
1671                                    if (count == null) {
1672                                            count = Long.valueOf(0);
1673                                    }
1674    
1675                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_ACTIVE,
1676                                            finderArgs, count);
1677    
1678                                    closeSession(session);
1679                            }
1680                    }
1681    
1682                    return count.intValue();
1683            }
1684    
1685            /**
1686             * Counts all the regions where countryId = &#63; and active = &#63;.
1687             *
1688             * @param countryId the country id to search with
1689             * @param active the active to search with
1690             * @return the number of matching regions
1691             * @throws SystemException if a system exception occurred
1692             */
1693            public int countByC_A(long countryId, boolean active)
1694                    throws SystemException {
1695                    Object[] finderArgs = new Object[] { countryId, active };
1696    
1697                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_A,
1698                                    finderArgs, this);
1699    
1700                    if (count == null) {
1701                            StringBundler query = new StringBundler(3);
1702    
1703                            query.append(_SQL_COUNT_REGION_WHERE);
1704    
1705                            query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1706    
1707                            query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1708    
1709                            String sql = query.toString();
1710    
1711                            Session session = null;
1712    
1713                            try {
1714                                    session = openSession();
1715    
1716                                    Query q = session.createQuery(sql);
1717    
1718                                    QueryPos qPos = QueryPos.getInstance(q);
1719    
1720                                    qPos.add(countryId);
1721    
1722                                    qPos.add(active);
1723    
1724                                    count = (Long)q.uniqueResult();
1725                            }
1726                            catch (Exception e) {
1727                                    throw processException(e);
1728                            }
1729                            finally {
1730                                    if (count == null) {
1731                                            count = Long.valueOf(0);
1732                                    }
1733    
1734                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_A, finderArgs,
1735                                            count);
1736    
1737                                    closeSession(session);
1738                            }
1739                    }
1740    
1741                    return count.intValue();
1742            }
1743    
1744            /**
1745             * Counts all the regions.
1746             *
1747             * @return the number of regions
1748             * @throws SystemException if a system exception occurred
1749             */
1750            public int countAll() throws SystemException {
1751                    Object[] finderArgs = new Object[0];
1752    
1753                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1754                                    finderArgs, this);
1755    
1756                    if (count == null) {
1757                            Session session = null;
1758    
1759                            try {
1760                                    session = openSession();
1761    
1762                                    Query q = session.createQuery(_SQL_COUNT_REGION);
1763    
1764                                    count = (Long)q.uniqueResult();
1765                            }
1766                            catch (Exception e) {
1767                                    throw processException(e);
1768                            }
1769                            finally {
1770                                    if (count == null) {
1771                                            count = Long.valueOf(0);
1772                                    }
1773    
1774                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1775                                            count);
1776    
1777                                    closeSession(session);
1778                            }
1779                    }
1780    
1781                    return count.intValue();
1782            }
1783    
1784            /**
1785             * Initializes the region persistence.
1786             */
1787            public void afterPropertiesSet() {
1788                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1789                                            com.liferay.portal.util.PropsUtil.get(
1790                                                    "value.object.listener.com.liferay.portal.model.Region")));
1791    
1792                    if (listenerClassNames.length > 0) {
1793                            try {
1794                                    List<ModelListener<Region>> listenersList = new ArrayList<ModelListener<Region>>();
1795    
1796                                    for (String listenerClassName : listenerClassNames) {
1797                                            listenersList.add((ModelListener<Region>)InstanceFactory.newInstance(
1798                                                            listenerClassName));
1799                                    }
1800    
1801                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1802                            }
1803                            catch (Exception e) {
1804                                    _log.error(e);
1805                            }
1806                    }
1807            }
1808    
1809            public void destroy() {
1810                    EntityCacheUtil.removeCache(RegionImpl.class.getName());
1811                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1812                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1813            }
1814    
1815            @BeanReference(type = AccountPersistence.class)
1816            protected AccountPersistence accountPersistence;
1817            @BeanReference(type = AddressPersistence.class)
1818            protected AddressPersistence addressPersistence;
1819            @BeanReference(type = BrowserTrackerPersistence.class)
1820            protected BrowserTrackerPersistence browserTrackerPersistence;
1821            @BeanReference(type = ClassNamePersistence.class)
1822            protected ClassNamePersistence classNamePersistence;
1823            @BeanReference(type = ClusterGroupPersistence.class)
1824            protected ClusterGroupPersistence clusterGroupPersistence;
1825            @BeanReference(type = CompanyPersistence.class)
1826            protected CompanyPersistence companyPersistence;
1827            @BeanReference(type = ContactPersistence.class)
1828            protected ContactPersistence contactPersistence;
1829            @BeanReference(type = CountryPersistence.class)
1830            protected CountryPersistence countryPersistence;
1831            @BeanReference(type = EmailAddressPersistence.class)
1832            protected EmailAddressPersistence emailAddressPersistence;
1833            @BeanReference(type = GroupPersistence.class)
1834            protected GroupPersistence groupPersistence;
1835            @BeanReference(type = ImagePersistence.class)
1836            protected ImagePersistence imagePersistence;
1837            @BeanReference(type = LayoutPersistence.class)
1838            protected LayoutPersistence layoutPersistence;
1839            @BeanReference(type = LayoutPrototypePersistence.class)
1840            protected LayoutPrototypePersistence layoutPrototypePersistence;
1841            @BeanReference(type = LayoutSetPersistence.class)
1842            protected LayoutSetPersistence layoutSetPersistence;
1843            @BeanReference(type = LayoutSetPrototypePersistence.class)
1844            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1845            @BeanReference(type = ListTypePersistence.class)
1846            protected ListTypePersistence listTypePersistence;
1847            @BeanReference(type = LockPersistence.class)
1848            protected LockPersistence lockPersistence;
1849            @BeanReference(type = MembershipRequestPersistence.class)
1850            protected MembershipRequestPersistence membershipRequestPersistence;
1851            @BeanReference(type = OrganizationPersistence.class)
1852            protected OrganizationPersistence organizationPersistence;
1853            @BeanReference(type = OrgGroupPermissionPersistence.class)
1854            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1855            @BeanReference(type = OrgGroupRolePersistence.class)
1856            protected OrgGroupRolePersistence orgGroupRolePersistence;
1857            @BeanReference(type = OrgLaborPersistence.class)
1858            protected OrgLaborPersistence orgLaborPersistence;
1859            @BeanReference(type = PasswordPolicyPersistence.class)
1860            protected PasswordPolicyPersistence passwordPolicyPersistence;
1861            @BeanReference(type = PasswordPolicyRelPersistence.class)
1862            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1863            @BeanReference(type = PasswordTrackerPersistence.class)
1864            protected PasswordTrackerPersistence passwordTrackerPersistence;
1865            @BeanReference(type = PermissionPersistence.class)
1866            protected PermissionPersistence permissionPersistence;
1867            @BeanReference(type = PhonePersistence.class)
1868            protected PhonePersistence phonePersistence;
1869            @BeanReference(type = PluginSettingPersistence.class)
1870            protected PluginSettingPersistence pluginSettingPersistence;
1871            @BeanReference(type = PortletPersistence.class)
1872            protected PortletPersistence portletPersistence;
1873            @BeanReference(type = PortletItemPersistence.class)
1874            protected PortletItemPersistence portletItemPersistence;
1875            @BeanReference(type = PortletPreferencesPersistence.class)
1876            protected PortletPreferencesPersistence portletPreferencesPersistence;
1877            @BeanReference(type = RegionPersistence.class)
1878            protected RegionPersistence regionPersistence;
1879            @BeanReference(type = ReleasePersistence.class)
1880            protected ReleasePersistence releasePersistence;
1881            @BeanReference(type = ResourcePersistence.class)
1882            protected ResourcePersistence resourcePersistence;
1883            @BeanReference(type = ResourceActionPersistence.class)
1884            protected ResourceActionPersistence resourceActionPersistence;
1885            @BeanReference(type = ResourceCodePersistence.class)
1886            protected ResourceCodePersistence resourceCodePersistence;
1887            @BeanReference(type = ResourcePermissionPersistence.class)
1888            protected ResourcePermissionPersistence resourcePermissionPersistence;
1889            @BeanReference(type = RolePersistence.class)
1890            protected RolePersistence rolePersistence;
1891            @BeanReference(type = ServiceComponentPersistence.class)
1892            protected ServiceComponentPersistence serviceComponentPersistence;
1893            @BeanReference(type = ShardPersistence.class)
1894            protected ShardPersistence shardPersistence;
1895            @BeanReference(type = SubscriptionPersistence.class)
1896            protected SubscriptionPersistence subscriptionPersistence;
1897            @BeanReference(type = TicketPersistence.class)
1898            protected TicketPersistence ticketPersistence;
1899            @BeanReference(type = TeamPersistence.class)
1900            protected TeamPersistence teamPersistence;
1901            @BeanReference(type = UserPersistence.class)
1902            protected UserPersistence userPersistence;
1903            @BeanReference(type = UserGroupPersistence.class)
1904            protected UserGroupPersistence userGroupPersistence;
1905            @BeanReference(type = UserGroupGroupRolePersistence.class)
1906            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1907            @BeanReference(type = UserGroupRolePersistence.class)
1908            protected UserGroupRolePersistence userGroupRolePersistence;
1909            @BeanReference(type = UserIdMapperPersistence.class)
1910            protected UserIdMapperPersistence userIdMapperPersistence;
1911            @BeanReference(type = UserTrackerPersistence.class)
1912            protected UserTrackerPersistence userTrackerPersistence;
1913            @BeanReference(type = UserTrackerPathPersistence.class)
1914            protected UserTrackerPathPersistence userTrackerPathPersistence;
1915            @BeanReference(type = WebDAVPropsPersistence.class)
1916            protected WebDAVPropsPersistence webDAVPropsPersistence;
1917            @BeanReference(type = WebsitePersistence.class)
1918            protected WebsitePersistence websitePersistence;
1919            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1920            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1921            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1922            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1923            private static final String _SQL_SELECT_REGION = "SELECT region FROM Region region";
1924            private static final String _SQL_SELECT_REGION_WHERE = "SELECT region FROM Region region WHERE ";
1925            private static final String _SQL_COUNT_REGION = "SELECT COUNT(region) FROM Region region";
1926            private static final String _SQL_COUNT_REGION_WHERE = "SELECT COUNT(region) FROM Region region WHERE ";
1927            private static final String _FINDER_COLUMN_COUNTRYID_COUNTRYID_2 = "region.countryId = ?";
1928            private static final String _FINDER_COLUMN_ACTIVE_ACTIVE_2 = "region.active = ?";
1929            private static final String _FINDER_COLUMN_C_A_COUNTRYID_2 = "region.countryId = ? AND ";
1930            private static final String _FINDER_COLUMN_C_A_ACTIVE_2 = "region.active = ?";
1931            private static final String _ORDER_BY_ENTITY_ALIAS = "region.";
1932            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Region exists with the primary key ";
1933            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Region exists with the key {";
1934            private static Log _log = LogFactoryUtil.getLog(RegionPersistenceImpl.class);
1935    }