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