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