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