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