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