001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            Session session = null;
448    
449                            try {
450                                    session = openSession();
451    
452                                    StringBundler query = null;
453    
454                                    if (orderByComparator != null) {
455                                            query = new StringBundler(3 +
456                                                            (orderByComparator.getOrderByFields().length * 3));
457                                    }
458                                    else {
459                                            query = new StringBundler(3);
460                                    }
461    
462                                    query.append(_SQL_SELECT_REGION_WHERE);
463    
464                                    query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
465    
466                                    if (orderByComparator != null) {
467                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
468                                                    orderByComparator);
469                                    }
470    
471                                    else {
472                                            query.append(RegionModelImpl.ORDER_BY_JPQL);
473                                    }
474    
475                                    String sql = query.toString();
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                                            list = new ArrayList<Region>();
491                                    }
492    
493                                    cacheResult(list);
494    
495                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COUNTRYID,
496                                            finderArgs, list);
497    
498                                    closeSession(session);
499                            }
500                    }
501    
502                    return list;
503            }
504    
505            /**
506             * Finds the first region in the ordered set where countryId = &#63;.
507             *
508             * <p>
509             * 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.
510             * </p>
511             *
512             * @param countryId the country id to search with
513             * @param orderByComparator the comparator to order the set by
514             * @return the first matching region
515             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
516             * @throws SystemException if a system exception occurred
517             */
518            public Region findByCountryId_First(long countryId,
519                    OrderByComparator orderByComparator)
520                    throws NoSuchRegionException, SystemException {
521                    List<Region> list = findByCountryId(countryId, 0, 1, orderByComparator);
522    
523                    if (list.isEmpty()) {
524                            StringBundler msg = new StringBundler(4);
525    
526                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
527    
528                            msg.append("countryId=");
529                            msg.append(countryId);
530    
531                            msg.append(StringPool.CLOSE_CURLY_BRACE);
532    
533                            throw new NoSuchRegionException(msg.toString());
534                    }
535                    else {
536                            return list.get(0);
537                    }
538            }
539    
540            /**
541             * Finds the last region in the ordered set where countryId = &#63;.
542             *
543             * <p>
544             * 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.
545             * </p>
546             *
547             * @param countryId the country id to search with
548             * @param orderByComparator the comparator to order the set by
549             * @return the last matching region
550             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
551             * @throws SystemException if a system exception occurred
552             */
553            public Region findByCountryId_Last(long countryId,
554                    OrderByComparator orderByComparator)
555                    throws NoSuchRegionException, SystemException {
556                    int count = countByCountryId(countryId);
557    
558                    List<Region> list = findByCountryId(countryId, count - 1, count,
559                                    orderByComparator);
560    
561                    if (list.isEmpty()) {
562                            StringBundler msg = new StringBundler(4);
563    
564                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
565    
566                            msg.append("countryId=");
567                            msg.append(countryId);
568    
569                            msg.append(StringPool.CLOSE_CURLY_BRACE);
570    
571                            throw new NoSuchRegionException(msg.toString());
572                    }
573                    else {
574                            return list.get(0);
575                    }
576            }
577    
578            /**
579             * Finds the regions before and after the current region in the ordered set where countryId = &#63;.
580             *
581             * <p>
582             * 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.
583             * </p>
584             *
585             * @param regionId the primary key of the current region
586             * @param countryId the country id to search with
587             * @param orderByComparator the comparator to order the set by
588             * @return the previous, current, and next region
589             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
590             * @throws SystemException if a system exception occurred
591             */
592            public Region[] findByCountryId_PrevAndNext(long regionId, long countryId,
593                    OrderByComparator orderByComparator)
594                    throws NoSuchRegionException, SystemException {
595                    Region region = findByPrimaryKey(regionId);
596    
597                    Session session = null;
598    
599                    try {
600                            session = openSession();
601    
602                            Region[] array = new RegionImpl[3];
603    
604                            array[0] = getByCountryId_PrevAndNext(session, region, countryId,
605                                            orderByComparator, true);
606    
607                            array[1] = region;
608    
609                            array[2] = getByCountryId_PrevAndNext(session, region, countryId,
610                                            orderByComparator, false);
611    
612                            return array;
613                    }
614                    catch (Exception e) {
615                            throw processException(e);
616                    }
617                    finally {
618                            closeSession(session);
619                    }
620            }
621    
622            protected Region getByCountryId_PrevAndNext(Session session, Region region,
623                    long countryId, OrderByComparator orderByComparator, boolean previous) {
624                    StringBundler query = null;
625    
626                    if (orderByComparator != null) {
627                            query = new StringBundler(6 +
628                                            (orderByComparator.getOrderByFields().length * 6));
629                    }
630                    else {
631                            query = new StringBundler(3);
632                    }
633    
634                    query.append(_SQL_SELECT_REGION_WHERE);
635    
636                    query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
637    
638                    if (orderByComparator != null) {
639                            String[] orderByFields = orderByComparator.getOrderByFields();
640    
641                            if (orderByFields.length > 0) {
642                                    query.append(WHERE_AND);
643                            }
644    
645                            for (int i = 0; i < orderByFields.length; i++) {
646                                    query.append(_ORDER_BY_ENTITY_ALIAS);
647                                    query.append(orderByFields[i]);
648    
649                                    if ((i + 1) < orderByFields.length) {
650                                            if (orderByComparator.isAscending() ^ previous) {
651                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
652                                            }
653                                            else {
654                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
655                                            }
656                                    }
657                                    else {
658                                            if (orderByComparator.isAscending() ^ previous) {
659                                                    query.append(WHERE_GREATER_THAN);
660                                            }
661                                            else {
662                                                    query.append(WHERE_LESSER_THAN);
663                                            }
664                                    }
665                            }
666    
667                            query.append(ORDER_BY_CLAUSE);
668    
669                            for (int i = 0; i < orderByFields.length; i++) {
670                                    query.append(_ORDER_BY_ENTITY_ALIAS);
671                                    query.append(orderByFields[i]);
672    
673                                    if ((i + 1) < orderByFields.length) {
674                                            if (orderByComparator.isAscending() ^ previous) {
675                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
676                                            }
677                                            else {
678                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
679                                            }
680                                    }
681                                    else {
682                                            if (orderByComparator.isAscending() ^ previous) {
683                                                    query.append(ORDER_BY_ASC);
684                                            }
685                                            else {
686                                                    query.append(ORDER_BY_DESC);
687                                            }
688                                    }
689                            }
690                    }
691    
692                    else {
693                            query.append(RegionModelImpl.ORDER_BY_JPQL);
694                    }
695    
696                    String sql = query.toString();
697    
698                    Query q = session.createQuery(sql);
699    
700                    q.setFirstResult(0);
701                    q.setMaxResults(2);
702    
703                    QueryPos qPos = QueryPos.getInstance(q);
704    
705                    qPos.add(countryId);
706    
707                    if (orderByComparator != null) {
708                            Object[] values = orderByComparator.getOrderByValues(region);
709    
710                            for (Object value : values) {
711                                    qPos.add(value);
712                            }
713                    }
714    
715                    List<Region> list = q.list();
716    
717                    if (list.size() == 2) {
718                            return list.get(1);
719                    }
720                    else {
721                            return null;
722                    }
723            }
724    
725            /**
726             * Finds all the regions where active = &#63;.
727             *
728             * @param active the active to search with
729             * @return the matching regions
730             * @throws SystemException if a system exception occurred
731             */
732            public List<Region> findByActive(boolean active) throws SystemException {
733                    return findByActive(active, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
734            }
735    
736            /**
737             * Finds a range of all the regions where active = &#63;.
738             *
739             * <p>
740             * 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.
741             * </p>
742             *
743             * @param active the active to search with
744             * @param start the lower bound of the range of regions to return
745             * @param end the upper bound of the range of regions to return (not inclusive)
746             * @return the range of matching regions
747             * @throws SystemException if a system exception occurred
748             */
749            public List<Region> findByActive(boolean active, int start, int end)
750                    throws SystemException {
751                    return findByActive(active, start, end, null);
752            }
753    
754            /**
755             * Finds an ordered range of all the regions where active = &#63;.
756             *
757             * <p>
758             * 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.
759             * </p>
760             *
761             * @param active the active to search with
762             * @param start the lower bound of the range of regions to return
763             * @param end the upper bound of the range of regions to return (not inclusive)
764             * @param orderByComparator the comparator to order the results by
765             * @return the ordered range of matching regions
766             * @throws SystemException if a system exception occurred
767             */
768            public List<Region> findByActive(boolean active, int start, int end,
769                    OrderByComparator orderByComparator) throws SystemException {
770                    Object[] finderArgs = new Object[] {
771                                    active,
772                                    
773                                    String.valueOf(start), String.valueOf(end),
774                                    String.valueOf(orderByComparator)
775                            };
776    
777                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_ACTIVE,
778                                    finderArgs, this);
779    
780                    if (list == null) {
781                            Session session = null;
782    
783                            try {
784                                    session = openSession();
785    
786                                    StringBundler query = null;
787    
788                                    if (orderByComparator != null) {
789                                            query = new StringBundler(3 +
790                                                            (orderByComparator.getOrderByFields().length * 3));
791                                    }
792                                    else {
793                                            query = new StringBundler(3);
794                                    }
795    
796                                    query.append(_SQL_SELECT_REGION_WHERE);
797    
798                                    query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
799    
800                                    if (orderByComparator != null) {
801                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
802                                                    orderByComparator);
803                                    }
804    
805                                    else {
806                                            query.append(RegionModelImpl.ORDER_BY_JPQL);
807                                    }
808    
809                                    String sql = query.toString();
810    
811                                    Query q = session.createQuery(sql);
812    
813                                    QueryPos qPos = QueryPos.getInstance(q);
814    
815                                    qPos.add(active);
816    
817                                    list = (List<Region>)QueryUtil.list(q, getDialect(), start, end);
818                            }
819                            catch (Exception e) {
820                                    throw processException(e);
821                            }
822                            finally {
823                                    if (list == null) {
824                                            list = new ArrayList<Region>();
825                                    }
826    
827                                    cacheResult(list);
828    
829                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_ACTIVE,
830                                            finderArgs, list);
831    
832                                    closeSession(session);
833                            }
834                    }
835    
836                    return list;
837            }
838    
839            /**
840             * Finds the first region in the ordered set where active = &#63;.
841             *
842             * <p>
843             * 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.
844             * </p>
845             *
846             * @param active the active to search with
847             * @param orderByComparator the comparator to order the set by
848             * @return the first matching region
849             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
850             * @throws SystemException if a system exception occurred
851             */
852            public Region findByActive_First(boolean active,
853                    OrderByComparator orderByComparator)
854                    throws NoSuchRegionException, SystemException {
855                    List<Region> list = findByActive(active, 0, 1, orderByComparator);
856    
857                    if (list.isEmpty()) {
858                            StringBundler msg = new StringBundler(4);
859    
860                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
861    
862                            msg.append("active=");
863                            msg.append(active);
864    
865                            msg.append(StringPool.CLOSE_CURLY_BRACE);
866    
867                            throw new NoSuchRegionException(msg.toString());
868                    }
869                    else {
870                            return list.get(0);
871                    }
872            }
873    
874            /**
875             * Finds the last region in the ordered set where active = &#63;.
876             *
877             * <p>
878             * 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.
879             * </p>
880             *
881             * @param active the active to search with
882             * @param orderByComparator the comparator to order the set by
883             * @return the last matching region
884             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
885             * @throws SystemException if a system exception occurred
886             */
887            public Region findByActive_Last(boolean active,
888                    OrderByComparator orderByComparator)
889                    throws NoSuchRegionException, SystemException {
890                    int count = countByActive(active);
891    
892                    List<Region> list = findByActive(active, count - 1, count,
893                                    orderByComparator);
894    
895                    if (list.isEmpty()) {
896                            StringBundler msg = new StringBundler(4);
897    
898                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
899    
900                            msg.append("active=");
901                            msg.append(active);
902    
903                            msg.append(StringPool.CLOSE_CURLY_BRACE);
904    
905                            throw new NoSuchRegionException(msg.toString());
906                    }
907                    else {
908                            return list.get(0);
909                    }
910            }
911    
912            /**
913             * Finds the regions before and after the current region in the ordered set where active = &#63;.
914             *
915             * <p>
916             * 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.
917             * </p>
918             *
919             * @param regionId the primary key of the current region
920             * @param active the active to search with
921             * @param orderByComparator the comparator to order the set by
922             * @return the previous, current, and next region
923             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
924             * @throws SystemException if a system exception occurred
925             */
926            public Region[] findByActive_PrevAndNext(long regionId, boolean active,
927                    OrderByComparator orderByComparator)
928                    throws NoSuchRegionException, SystemException {
929                    Region region = findByPrimaryKey(regionId);
930    
931                    Session session = null;
932    
933                    try {
934                            session = openSession();
935    
936                            Region[] array = new RegionImpl[3];
937    
938                            array[0] = getByActive_PrevAndNext(session, region, active,
939                                            orderByComparator, true);
940    
941                            array[1] = region;
942    
943                            array[2] = getByActive_PrevAndNext(session, region, active,
944                                            orderByComparator, false);
945    
946                            return array;
947                    }
948                    catch (Exception e) {
949                            throw processException(e);
950                    }
951                    finally {
952                            closeSession(session);
953                    }
954            }
955    
956            protected Region getByActive_PrevAndNext(Session session, Region region,
957                    boolean active, OrderByComparator orderByComparator, boolean previous) {
958                    StringBundler query = null;
959    
960                    if (orderByComparator != null) {
961                            query = new StringBundler(6 +
962                                            (orderByComparator.getOrderByFields().length * 6));
963                    }
964                    else {
965                            query = new StringBundler(3);
966                    }
967    
968                    query.append(_SQL_SELECT_REGION_WHERE);
969    
970                    query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
971    
972                    if (orderByComparator != null) {
973                            String[] orderByFields = orderByComparator.getOrderByFields();
974    
975                            if (orderByFields.length > 0) {
976                                    query.append(WHERE_AND);
977                            }
978    
979                            for (int i = 0; i < orderByFields.length; i++) {
980                                    query.append(_ORDER_BY_ENTITY_ALIAS);
981                                    query.append(orderByFields[i]);
982    
983                                    if ((i + 1) < orderByFields.length) {
984                                            if (orderByComparator.isAscending() ^ previous) {
985                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
986                                            }
987                                            else {
988                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
989                                            }
990                                    }
991                                    else {
992                                            if (orderByComparator.isAscending() ^ previous) {
993                                                    query.append(WHERE_GREATER_THAN);
994                                            }
995                                            else {
996                                                    query.append(WHERE_LESSER_THAN);
997                                            }
998                                    }
999                            }
1000    
1001                            query.append(ORDER_BY_CLAUSE);
1002    
1003                            for (int i = 0; i < orderByFields.length; i++) {
1004                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1005                                    query.append(orderByFields[i]);
1006    
1007                                    if ((i + 1) < orderByFields.length) {
1008                                            if (orderByComparator.isAscending() ^ previous) {
1009                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1010                                            }
1011                                            else {
1012                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1013                                            }
1014                                    }
1015                                    else {
1016                                            if (orderByComparator.isAscending() ^ previous) {
1017                                                    query.append(ORDER_BY_ASC);
1018                                            }
1019                                            else {
1020                                                    query.append(ORDER_BY_DESC);
1021                                            }
1022                                    }
1023                            }
1024                    }
1025    
1026                    else {
1027                            query.append(RegionModelImpl.ORDER_BY_JPQL);
1028                    }
1029    
1030                    String sql = query.toString();
1031    
1032                    Query q = session.createQuery(sql);
1033    
1034                    q.setFirstResult(0);
1035                    q.setMaxResults(2);
1036    
1037                    QueryPos qPos = QueryPos.getInstance(q);
1038    
1039                    qPos.add(active);
1040    
1041                    if (orderByComparator != null) {
1042                            Object[] values = orderByComparator.getOrderByValues(region);
1043    
1044                            for (Object value : values) {
1045                                    qPos.add(value);
1046                            }
1047                    }
1048    
1049                    List<Region> list = q.list();
1050    
1051                    if (list.size() == 2) {
1052                            return list.get(1);
1053                    }
1054                    else {
1055                            return null;
1056                    }
1057            }
1058    
1059            /**
1060             * Finds all the regions where countryId = &#63; and active = &#63;.
1061             *
1062             * @param countryId the country id to search with
1063             * @param active the active to search with
1064             * @return the matching regions
1065             * @throws SystemException if a system exception occurred
1066             */
1067            public List<Region> findByC_A(long countryId, boolean active)
1068                    throws SystemException {
1069                    return findByC_A(countryId, active, QueryUtil.ALL_POS,
1070                            QueryUtil.ALL_POS, null);
1071            }
1072    
1073            /**
1074             * Finds a range of all the regions where countryId = &#63; and active = &#63;.
1075             *
1076             * <p>
1077             * 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.
1078             * </p>
1079             *
1080             * @param countryId the country id to search with
1081             * @param active the active to search with
1082             * @param start the lower bound of the range of regions to return
1083             * @param end the upper bound of the range of regions to return (not inclusive)
1084             * @return the range of matching regions
1085             * @throws SystemException if a system exception occurred
1086             */
1087            public List<Region> findByC_A(long countryId, boolean active, int start,
1088                    int end) throws SystemException {
1089                    return findByC_A(countryId, active, start, end, null);
1090            }
1091    
1092            /**
1093             * Finds an ordered range of all the regions where countryId = &#63; and active = &#63;.
1094             *
1095             * <p>
1096             * 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.
1097             * </p>
1098             *
1099             * @param countryId the country id to search with
1100             * @param active the active to search with
1101             * @param start the lower bound of the range of regions to return
1102             * @param end the upper bound of the range of regions to return (not inclusive)
1103             * @param orderByComparator the comparator to order the results by
1104             * @return the ordered range of matching regions
1105             * @throws SystemException if a system exception occurred
1106             */
1107            public List<Region> findByC_A(long countryId, boolean active, int start,
1108                    int end, OrderByComparator orderByComparator) throws SystemException {
1109                    Object[] finderArgs = new Object[] {
1110                                    countryId, active,
1111                                    
1112                                    String.valueOf(start), String.valueOf(end),
1113                                    String.valueOf(orderByComparator)
1114                            };
1115    
1116                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_A,
1117                                    finderArgs, this);
1118    
1119                    if (list == null) {
1120                            Session session = null;
1121    
1122                            try {
1123                                    session = openSession();
1124    
1125                                    StringBundler query = null;
1126    
1127                                    if (orderByComparator != null) {
1128                                            query = new StringBundler(4 +
1129                                                            (orderByComparator.getOrderByFields().length * 3));
1130                                    }
1131                                    else {
1132                                            query = new StringBundler(4);
1133                                    }
1134    
1135                                    query.append(_SQL_SELECT_REGION_WHERE);
1136    
1137                                    query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1138    
1139                                    query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1140    
1141                                    if (orderByComparator != null) {
1142                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1143                                                    orderByComparator);
1144                                    }
1145    
1146                                    else {
1147                                            query.append(RegionModelImpl.ORDER_BY_JPQL);
1148                                    }
1149    
1150                                    String sql = query.toString();
1151    
1152                                    Query q = session.createQuery(sql);
1153    
1154                                    QueryPos qPos = QueryPos.getInstance(q);
1155    
1156                                    qPos.add(countryId);
1157    
1158                                    qPos.add(active);
1159    
1160                                    list = (List<Region>)QueryUtil.list(q, getDialect(), start, end);
1161                            }
1162                            catch (Exception e) {
1163                                    throw processException(e);
1164                            }
1165                            finally {
1166                                    if (list == null) {
1167                                            list = new ArrayList<Region>();
1168                                    }
1169    
1170                                    cacheResult(list);
1171    
1172                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_A, finderArgs,
1173                                            list);
1174    
1175                                    closeSession(session);
1176                            }
1177                    }
1178    
1179                    return list;
1180            }
1181    
1182            /**
1183             * Finds the first region in the ordered set where countryId = &#63; and active = &#63;.
1184             *
1185             * <p>
1186             * 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.
1187             * </p>
1188             *
1189             * @param countryId the country id to search with
1190             * @param active the active to search with
1191             * @param orderByComparator the comparator to order the set by
1192             * @return the first matching region
1193             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
1194             * @throws SystemException if a system exception occurred
1195             */
1196            public Region findByC_A_First(long countryId, boolean active,
1197                    OrderByComparator orderByComparator)
1198                    throws NoSuchRegionException, SystemException {
1199                    List<Region> list = findByC_A(countryId, active, 0, 1, orderByComparator);
1200    
1201                    if (list.isEmpty()) {
1202                            StringBundler msg = new StringBundler(6);
1203    
1204                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1205    
1206                            msg.append("countryId=");
1207                            msg.append(countryId);
1208    
1209                            msg.append(", active=");
1210                            msg.append(active);
1211    
1212                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1213    
1214                            throw new NoSuchRegionException(msg.toString());
1215                    }
1216                    else {
1217                            return list.get(0);
1218                    }
1219            }
1220    
1221            /**
1222             * Finds the last region in the ordered set where countryId = &#63; and active = &#63;.
1223             *
1224             * <p>
1225             * 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.
1226             * </p>
1227             *
1228             * @param countryId the country id to search with
1229             * @param active the active to search with
1230             * @param orderByComparator the comparator to order the set by
1231             * @return the last matching region
1232             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
1233             * @throws SystemException if a system exception occurred
1234             */
1235            public Region findByC_A_Last(long countryId, boolean active,
1236                    OrderByComparator orderByComparator)
1237                    throws NoSuchRegionException, SystemException {
1238                    int count = countByC_A(countryId, active);
1239    
1240                    List<Region> list = findByC_A(countryId, active, count - 1, count,
1241                                    orderByComparator);
1242    
1243                    if (list.isEmpty()) {
1244                            StringBundler msg = new StringBundler(6);
1245    
1246                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1247    
1248                            msg.append("countryId=");
1249                            msg.append(countryId);
1250    
1251                            msg.append(", active=");
1252                            msg.append(active);
1253    
1254                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1255    
1256                            throw new NoSuchRegionException(msg.toString());
1257                    }
1258                    else {
1259                            return list.get(0);
1260                    }
1261            }
1262    
1263            /**
1264             * Finds the regions before and after the current region in the ordered set where countryId = &#63; and active = &#63;.
1265             *
1266             * <p>
1267             * 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.
1268             * </p>
1269             *
1270             * @param regionId the primary key of the current region
1271             * @param countryId the country id to search with
1272             * @param active the active to search with
1273             * @param orderByComparator the comparator to order the set by
1274             * @return the previous, current, and next region
1275             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
1276             * @throws SystemException if a system exception occurred
1277             */
1278            public Region[] findByC_A_PrevAndNext(long regionId, long countryId,
1279                    boolean active, OrderByComparator orderByComparator)
1280                    throws NoSuchRegionException, SystemException {
1281                    Region region = findByPrimaryKey(regionId);
1282    
1283                    Session session = null;
1284    
1285                    try {
1286                            session = openSession();
1287    
1288                            Region[] array = new RegionImpl[3];
1289    
1290                            array[0] = getByC_A_PrevAndNext(session, region, countryId, active,
1291                                            orderByComparator, true);
1292    
1293                            array[1] = region;
1294    
1295                            array[2] = getByC_A_PrevAndNext(session, region, countryId, active,
1296                                            orderByComparator, false);
1297    
1298                            return array;
1299                    }
1300                    catch (Exception e) {
1301                            throw processException(e);
1302                    }
1303                    finally {
1304                            closeSession(session);
1305                    }
1306            }
1307    
1308            protected Region getByC_A_PrevAndNext(Session session, Region region,
1309                    long countryId, boolean active, OrderByComparator orderByComparator,
1310                    boolean previous) {
1311                    StringBundler query = null;
1312    
1313                    if (orderByComparator != null) {
1314                            query = new StringBundler(6 +
1315                                            (orderByComparator.getOrderByFields().length * 6));
1316                    }
1317                    else {
1318                            query = new StringBundler(3);
1319                    }
1320    
1321                    query.append(_SQL_SELECT_REGION_WHERE);
1322    
1323                    query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1324    
1325                    query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1326    
1327                    if (orderByComparator != null) {
1328                            String[] orderByFields = orderByComparator.getOrderByFields();
1329    
1330                            if (orderByFields.length > 0) {
1331                                    query.append(WHERE_AND);
1332                            }
1333    
1334                            for (int i = 0; i < orderByFields.length; i++) {
1335                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1336                                    query.append(orderByFields[i]);
1337    
1338                                    if ((i + 1) < orderByFields.length) {
1339                                            if (orderByComparator.isAscending() ^ previous) {
1340                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1341                                            }
1342                                            else {
1343                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1344                                            }
1345                                    }
1346                                    else {
1347                                            if (orderByComparator.isAscending() ^ previous) {
1348                                                    query.append(WHERE_GREATER_THAN);
1349                                            }
1350                                            else {
1351                                                    query.append(WHERE_LESSER_THAN);
1352                                            }
1353                                    }
1354                            }
1355    
1356                            query.append(ORDER_BY_CLAUSE);
1357    
1358                            for (int i = 0; i < orderByFields.length; i++) {
1359                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1360                                    query.append(orderByFields[i]);
1361    
1362                                    if ((i + 1) < orderByFields.length) {
1363                                            if (orderByComparator.isAscending() ^ previous) {
1364                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1365                                            }
1366                                            else {
1367                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1368                                            }
1369                                    }
1370                                    else {
1371                                            if (orderByComparator.isAscending() ^ previous) {
1372                                                    query.append(ORDER_BY_ASC);
1373                                            }
1374                                            else {
1375                                                    query.append(ORDER_BY_DESC);
1376                                            }
1377                                    }
1378                            }
1379                    }
1380    
1381                    else {
1382                            query.append(RegionModelImpl.ORDER_BY_JPQL);
1383                    }
1384    
1385                    String sql = query.toString();
1386    
1387                    Query q = session.createQuery(sql);
1388    
1389                    q.setFirstResult(0);
1390                    q.setMaxResults(2);
1391    
1392                    QueryPos qPos = QueryPos.getInstance(q);
1393    
1394                    qPos.add(countryId);
1395    
1396                    qPos.add(active);
1397    
1398                    if (orderByComparator != null) {
1399                            Object[] values = orderByComparator.getOrderByValues(region);
1400    
1401                            for (Object value : values) {
1402                                    qPos.add(value);
1403                            }
1404                    }
1405    
1406                    List<Region> list = q.list();
1407    
1408                    if (list.size() == 2) {
1409                            return list.get(1);
1410                    }
1411                    else {
1412                            return null;
1413                    }
1414            }
1415    
1416            /**
1417             * Finds all the regions.
1418             *
1419             * @return the regions
1420             * @throws SystemException if a system exception occurred
1421             */
1422            public List<Region> findAll() throws SystemException {
1423                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1424            }
1425    
1426            /**
1427             * Finds a range of all the regions.
1428             *
1429             * <p>
1430             * 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.
1431             * </p>
1432             *
1433             * @param start the lower bound of the range of regions to return
1434             * @param end the upper bound of the range of regions to return (not inclusive)
1435             * @return the range of regions
1436             * @throws SystemException if a system exception occurred
1437             */
1438            public List<Region> findAll(int start, int end) throws SystemException {
1439                    return findAll(start, end, null);
1440            }
1441    
1442            /**
1443             * Finds an ordered range of all the regions.
1444             *
1445             * <p>
1446             * 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.
1447             * </p>
1448             *
1449             * @param start the lower bound of the range of regions to return
1450             * @param end the upper bound of the range of regions to return (not inclusive)
1451             * @param orderByComparator the comparator to order the results by
1452             * @return the ordered range of regions
1453             * @throws SystemException if a system exception occurred
1454             */
1455            public List<Region> findAll(int start, int end,
1456                    OrderByComparator orderByComparator) throws SystemException {
1457                    Object[] finderArgs = new Object[] {
1458                                    String.valueOf(start), String.valueOf(end),
1459                                    String.valueOf(orderByComparator)
1460                            };
1461    
1462                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1463                                    finderArgs, this);
1464    
1465                    if (list == null) {
1466                            Session session = null;
1467    
1468                            try {
1469                                    session = openSession();
1470    
1471                                    StringBundler query = null;
1472                                    String sql = null;
1473    
1474                                    if (orderByComparator != null) {
1475                                            query = new StringBundler(2 +
1476                                                            (orderByComparator.getOrderByFields().length * 3));
1477    
1478                                            query.append(_SQL_SELECT_REGION);
1479    
1480                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1481                                                    orderByComparator);
1482    
1483                                            sql = query.toString();
1484                                    }
1485                                    else {
1486                                            sql = _SQL_SELECT_REGION.concat(RegionModelImpl.ORDER_BY_JPQL);
1487                                    }
1488    
1489                                    Query q = session.createQuery(sql);
1490    
1491                                    if (orderByComparator == null) {
1492                                            list = (List<Region>)QueryUtil.list(q, getDialect(), start,
1493                                                            end, false);
1494    
1495                                            Collections.sort(list);
1496                                    }
1497                                    else {
1498                                            list = (List<Region>)QueryUtil.list(q, getDialect(), start,
1499                                                            end);
1500                                    }
1501                            }
1502                            catch (Exception e) {
1503                                    throw processException(e);
1504                            }
1505                            finally {
1506                                    if (list == null) {
1507                                            list = new ArrayList<Region>();
1508                                    }
1509    
1510                                    cacheResult(list);
1511    
1512                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1513    
1514                                    closeSession(session);
1515                            }
1516                    }
1517    
1518                    return list;
1519            }
1520    
1521            /**
1522             * Removes all the regions where countryId = &#63; from the database.
1523             *
1524             * @param countryId the country id to search with
1525             * @throws SystemException if a system exception occurred
1526             */
1527            public void removeByCountryId(long countryId) throws SystemException {
1528                    for (Region region : findByCountryId(countryId)) {
1529                            remove(region);
1530                    }
1531            }
1532    
1533            /**
1534             * Removes all the regions where active = &#63; from the database.
1535             *
1536             * @param active the active to search with
1537             * @throws SystemException if a system exception occurred
1538             */
1539            public void removeByActive(boolean active) throws SystemException {
1540                    for (Region region : findByActive(active)) {
1541                            remove(region);
1542                    }
1543            }
1544    
1545            /**
1546             * Removes all the regions where countryId = &#63; and active = &#63; from the database.
1547             *
1548             * @param countryId the country id to search with
1549             * @param active the active to search with
1550             * @throws SystemException if a system exception occurred
1551             */
1552            public void removeByC_A(long countryId, boolean active)
1553                    throws SystemException {
1554                    for (Region region : findByC_A(countryId, active)) {
1555                            remove(region);
1556                    }
1557            }
1558    
1559            /**
1560             * Removes all the regions from the database.
1561             *
1562             * @throws SystemException if a system exception occurred
1563             */
1564            public void removeAll() throws SystemException {
1565                    for (Region region : findAll()) {
1566                            remove(region);
1567                    }
1568            }
1569    
1570            /**
1571             * Counts all the regions where countryId = &#63;.
1572             *
1573             * @param countryId the country id to search with
1574             * @return the number of matching regions
1575             * @throws SystemException if a system exception occurred
1576             */
1577            public int countByCountryId(long countryId) throws SystemException {
1578                    Object[] finderArgs = new Object[] { countryId };
1579    
1580                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COUNTRYID,
1581                                    finderArgs, this);
1582    
1583                    if (count == null) {
1584                            Session session = null;
1585    
1586                            try {
1587                                    session = openSession();
1588    
1589                                    StringBundler query = new StringBundler(2);
1590    
1591                                    query.append(_SQL_COUNT_REGION_WHERE);
1592    
1593                                    query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
1594    
1595                                    String sql = query.toString();
1596    
1597                                    Query q = session.createQuery(sql);
1598    
1599                                    QueryPos qPos = QueryPos.getInstance(q);
1600    
1601                                    qPos.add(countryId);
1602    
1603                                    count = (Long)q.uniqueResult();
1604                            }
1605                            catch (Exception e) {
1606                                    throw processException(e);
1607                            }
1608                            finally {
1609                                    if (count == null) {
1610                                            count = Long.valueOf(0);
1611                                    }
1612    
1613                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COUNTRYID,
1614                                            finderArgs, count);
1615    
1616                                    closeSession(session);
1617                            }
1618                    }
1619    
1620                    return count.intValue();
1621            }
1622    
1623            /**
1624             * Counts all the regions where active = &#63;.
1625             *
1626             * @param active the active to search with
1627             * @return the number of matching regions
1628             * @throws SystemException if a system exception occurred
1629             */
1630            public int countByActive(boolean active) throws SystemException {
1631                    Object[] finderArgs = new Object[] { active };
1632    
1633                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_ACTIVE,
1634                                    finderArgs, this);
1635    
1636                    if (count == null) {
1637                            Session session = null;
1638    
1639                            try {
1640                                    session = openSession();
1641    
1642                                    StringBundler query = new StringBundler(2);
1643    
1644                                    query.append(_SQL_COUNT_REGION_WHERE);
1645    
1646                                    query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
1647    
1648                                    String sql = query.toString();
1649    
1650                                    Query q = session.createQuery(sql);
1651    
1652                                    QueryPos qPos = QueryPos.getInstance(q);
1653    
1654                                    qPos.add(active);
1655    
1656                                    count = (Long)q.uniqueResult();
1657                            }
1658                            catch (Exception e) {
1659                                    throw processException(e);
1660                            }
1661                            finally {
1662                                    if (count == null) {
1663                                            count = Long.valueOf(0);
1664                                    }
1665    
1666                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_ACTIVE,
1667                                            finderArgs, count);
1668    
1669                                    closeSession(session);
1670                            }
1671                    }
1672    
1673                    return count.intValue();
1674            }
1675    
1676            /**
1677             * Counts all the regions where countryId = &#63; and active = &#63;.
1678             *
1679             * @param countryId the country id to search with
1680             * @param active the active to search with
1681             * @return the number of matching regions
1682             * @throws SystemException if a system exception occurred
1683             */
1684            public int countByC_A(long countryId, boolean active)
1685                    throws SystemException {
1686                    Object[] finderArgs = new Object[] { countryId, active };
1687    
1688                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_A,
1689                                    finderArgs, this);
1690    
1691                    if (count == null) {
1692                            Session session = null;
1693    
1694                            try {
1695                                    session = openSession();
1696    
1697                                    StringBundler query = new StringBundler(3);
1698    
1699                                    query.append(_SQL_COUNT_REGION_WHERE);
1700    
1701                                    query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1702    
1703                                    query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1704    
1705                                    String sql = query.toString();
1706    
1707                                    Query q = session.createQuery(sql);
1708    
1709                                    QueryPos qPos = QueryPos.getInstance(q);
1710    
1711                                    qPos.add(countryId);
1712    
1713                                    qPos.add(active);
1714    
1715                                    count = (Long)q.uniqueResult();
1716                            }
1717                            catch (Exception e) {
1718                                    throw processException(e);
1719                            }
1720                            finally {
1721                                    if (count == null) {
1722                                            count = Long.valueOf(0);
1723                                    }
1724    
1725                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_A, finderArgs,
1726                                            count);
1727    
1728                                    closeSession(session);
1729                            }
1730                    }
1731    
1732                    return count.intValue();
1733            }
1734    
1735            /**
1736             * Counts all the regions.
1737             *
1738             * @return the number of regions
1739             * @throws SystemException if a system exception occurred
1740             */
1741            public int countAll() throws SystemException {
1742                    Object[] finderArgs = new Object[0];
1743    
1744                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1745                                    finderArgs, this);
1746    
1747                    if (count == null) {
1748                            Session session = null;
1749    
1750                            try {
1751                                    session = openSession();
1752    
1753                                    Query q = session.createQuery(_SQL_COUNT_REGION);
1754    
1755                                    count = (Long)q.uniqueResult();
1756                            }
1757                            catch (Exception e) {
1758                                    throw processException(e);
1759                            }
1760                            finally {
1761                                    if (count == null) {
1762                                            count = Long.valueOf(0);
1763                                    }
1764    
1765                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1766                                            count);
1767    
1768                                    closeSession(session);
1769                            }
1770                    }
1771    
1772                    return count.intValue();
1773            }
1774    
1775            /**
1776             * Initializes the region persistence.
1777             */
1778            public void afterPropertiesSet() {
1779                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1780                                            com.liferay.portal.util.PropsUtil.get(
1781                                                    "value.object.listener.com.liferay.portal.model.Region")));
1782    
1783                    if (listenerClassNames.length > 0) {
1784                            try {
1785                                    List<ModelListener<Region>> listenersList = new ArrayList<ModelListener<Region>>();
1786    
1787                                    for (String listenerClassName : listenerClassNames) {
1788                                            listenersList.add((ModelListener<Region>)InstanceFactory.newInstance(
1789                                                            listenerClassName));
1790                                    }
1791    
1792                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1793                            }
1794                            catch (Exception e) {
1795                                    _log.error(e);
1796                            }
1797                    }
1798            }
1799    
1800            public void destroy() {
1801                    EntityCacheUtil.removeCache(RegionImpl.class.getName());
1802                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1803                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1804            }
1805    
1806            @BeanReference(type = AccountPersistence.class)
1807            protected AccountPersistence accountPersistence;
1808            @BeanReference(type = AddressPersistence.class)
1809            protected AddressPersistence addressPersistence;
1810            @BeanReference(type = BrowserTrackerPersistence.class)
1811            protected BrowserTrackerPersistence browserTrackerPersistence;
1812            @BeanReference(type = ClassNamePersistence.class)
1813            protected ClassNamePersistence classNamePersistence;
1814            @BeanReference(type = ClusterGroupPersistence.class)
1815            protected ClusterGroupPersistence clusterGroupPersistence;
1816            @BeanReference(type = CompanyPersistence.class)
1817            protected CompanyPersistence companyPersistence;
1818            @BeanReference(type = ContactPersistence.class)
1819            protected ContactPersistence contactPersistence;
1820            @BeanReference(type = CountryPersistence.class)
1821            protected CountryPersistence countryPersistence;
1822            @BeanReference(type = EmailAddressPersistence.class)
1823            protected EmailAddressPersistence emailAddressPersistence;
1824            @BeanReference(type = GroupPersistence.class)
1825            protected GroupPersistence groupPersistence;
1826            @BeanReference(type = ImagePersistence.class)
1827            protected ImagePersistence imagePersistence;
1828            @BeanReference(type = LayoutPersistence.class)
1829            protected LayoutPersistence layoutPersistence;
1830            @BeanReference(type = LayoutPrototypePersistence.class)
1831            protected LayoutPrototypePersistence layoutPrototypePersistence;
1832            @BeanReference(type = LayoutSetPersistence.class)
1833            protected LayoutSetPersistence layoutSetPersistence;
1834            @BeanReference(type = LayoutSetPrototypePersistence.class)
1835            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1836            @BeanReference(type = ListTypePersistence.class)
1837            protected ListTypePersistence listTypePersistence;
1838            @BeanReference(type = LockPersistence.class)
1839            protected LockPersistence lockPersistence;
1840            @BeanReference(type = MembershipRequestPersistence.class)
1841            protected MembershipRequestPersistence membershipRequestPersistence;
1842            @BeanReference(type = OrganizationPersistence.class)
1843            protected OrganizationPersistence organizationPersistence;
1844            @BeanReference(type = OrgGroupPermissionPersistence.class)
1845            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1846            @BeanReference(type = OrgGroupRolePersistence.class)
1847            protected OrgGroupRolePersistence orgGroupRolePersistence;
1848            @BeanReference(type = OrgLaborPersistence.class)
1849            protected OrgLaborPersistence orgLaborPersistence;
1850            @BeanReference(type = PasswordPolicyPersistence.class)
1851            protected PasswordPolicyPersistence passwordPolicyPersistence;
1852            @BeanReference(type = PasswordPolicyRelPersistence.class)
1853            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1854            @BeanReference(type = PasswordTrackerPersistence.class)
1855            protected PasswordTrackerPersistence passwordTrackerPersistence;
1856            @BeanReference(type = PermissionPersistence.class)
1857            protected PermissionPersistence permissionPersistence;
1858            @BeanReference(type = PhonePersistence.class)
1859            protected PhonePersistence phonePersistence;
1860            @BeanReference(type = PluginSettingPersistence.class)
1861            protected PluginSettingPersistence pluginSettingPersistence;
1862            @BeanReference(type = PortletPersistence.class)
1863            protected PortletPersistence portletPersistence;
1864            @BeanReference(type = PortletItemPersistence.class)
1865            protected PortletItemPersistence portletItemPersistence;
1866            @BeanReference(type = PortletPreferencesPersistence.class)
1867            protected PortletPreferencesPersistence portletPreferencesPersistence;
1868            @BeanReference(type = RegionPersistence.class)
1869            protected RegionPersistence regionPersistence;
1870            @BeanReference(type = ReleasePersistence.class)
1871            protected ReleasePersistence releasePersistence;
1872            @BeanReference(type = ResourcePersistence.class)
1873            protected ResourcePersistence resourcePersistence;
1874            @BeanReference(type = ResourceActionPersistence.class)
1875            protected ResourceActionPersistence resourceActionPersistence;
1876            @BeanReference(type = ResourceCodePersistence.class)
1877            protected ResourceCodePersistence resourceCodePersistence;
1878            @BeanReference(type = ResourcePermissionPersistence.class)
1879            protected ResourcePermissionPersistence resourcePermissionPersistence;
1880            @BeanReference(type = RolePersistence.class)
1881            protected RolePersistence rolePersistence;
1882            @BeanReference(type = ServiceComponentPersistence.class)
1883            protected ServiceComponentPersistence serviceComponentPersistence;
1884            @BeanReference(type = ShardPersistence.class)
1885            protected ShardPersistence shardPersistence;
1886            @BeanReference(type = SubscriptionPersistence.class)
1887            protected SubscriptionPersistence subscriptionPersistence;
1888            @BeanReference(type = TicketPersistence.class)
1889            protected TicketPersistence ticketPersistence;
1890            @BeanReference(type = TeamPersistence.class)
1891            protected TeamPersistence teamPersistence;
1892            @BeanReference(type = UserPersistence.class)
1893            protected UserPersistence userPersistence;
1894            @BeanReference(type = UserGroupPersistence.class)
1895            protected UserGroupPersistence userGroupPersistence;
1896            @BeanReference(type = UserGroupGroupRolePersistence.class)
1897            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1898            @BeanReference(type = UserGroupRolePersistence.class)
1899            protected UserGroupRolePersistence userGroupRolePersistence;
1900            @BeanReference(type = UserIdMapperPersistence.class)
1901            protected UserIdMapperPersistence userIdMapperPersistence;
1902            @BeanReference(type = UserTrackerPersistence.class)
1903            protected UserTrackerPersistence userTrackerPersistence;
1904            @BeanReference(type = UserTrackerPathPersistence.class)
1905            protected UserTrackerPathPersistence userTrackerPathPersistence;
1906            @BeanReference(type = WebDAVPropsPersistence.class)
1907            protected WebDAVPropsPersistence webDAVPropsPersistence;
1908            @BeanReference(type = WebsitePersistence.class)
1909            protected WebsitePersistence websitePersistence;
1910            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1911            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1912            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1913            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1914            private static final String _SQL_SELECT_REGION = "SELECT region FROM Region region";
1915            private static final String _SQL_SELECT_REGION_WHERE = "SELECT region FROM Region region WHERE ";
1916            private static final String _SQL_COUNT_REGION = "SELECT COUNT(region) FROM Region region";
1917            private static final String _SQL_COUNT_REGION_WHERE = "SELECT COUNT(region) FROM Region region WHERE ";
1918            private static final String _FINDER_COLUMN_COUNTRYID_COUNTRYID_2 = "region.countryId = ?";
1919            private static final String _FINDER_COLUMN_ACTIVE_ACTIVE_2 = "region.active = ?";
1920            private static final String _FINDER_COLUMN_C_A_COUNTRYID_2 = "region.countryId = ? AND ";
1921            private static final String _FINDER_COLUMN_C_A_ACTIVE_2 = "region.active = ?";
1922            private static final String _ORDER_BY_ENTITY_ALIAS = "region.";
1923            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Region exists with the primary key ";
1924            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Region exists with the key {";
1925            private static Log _log = LogFactoryUtil.getLog(RegionPersistenceImpl.class);
1926    }