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