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