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