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