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            public RegionPersistenceImpl() {
1850                    setModelClass(Region.class);
1851            }
1852    
1853            /**
1854             * Caches the region in the entity cache if it is enabled.
1855             *
1856             * @param region the region
1857             */
1858            @Override
1859            public void cacheResult(Region region) {
1860                    EntityCacheUtil.putResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
1861                            RegionImpl.class, region.getPrimaryKey(), region);
1862    
1863                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_R,
1864                            new Object[] { region.getCountryId(), region.getRegionCode() },
1865                            region);
1866    
1867                    region.resetOriginalValues();
1868            }
1869    
1870            /**
1871             * Caches the regions in the entity cache if it is enabled.
1872             *
1873             * @param regions the regions
1874             */
1875            @Override
1876            public void cacheResult(List<Region> regions) {
1877                    for (Region region : regions) {
1878                            if (EntityCacheUtil.getResult(
1879                                                    RegionModelImpl.ENTITY_CACHE_ENABLED, RegionImpl.class,
1880                                                    region.getPrimaryKey()) == null) {
1881                                    cacheResult(region);
1882                            }
1883                            else {
1884                                    region.resetOriginalValues();
1885                            }
1886                    }
1887            }
1888    
1889            /**
1890             * Clears the cache for all regions.
1891             *
1892             * <p>
1893             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1894             * </p>
1895             */
1896            @Override
1897            public void clearCache() {
1898                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
1899                            CacheRegistryUtil.clear(RegionImpl.class.getName());
1900                    }
1901    
1902                    EntityCacheUtil.clearCache(RegionImpl.class.getName());
1903    
1904                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
1905                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1906                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1907            }
1908    
1909            /**
1910             * Clears the cache for the region.
1911             *
1912             * <p>
1913             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1914             * </p>
1915             */
1916            @Override
1917            public void clearCache(Region region) {
1918                    EntityCacheUtil.removeResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
1919                            RegionImpl.class, region.getPrimaryKey());
1920    
1921                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1922                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1923    
1924                    clearUniqueFindersCache(region);
1925            }
1926    
1927            @Override
1928            public void clearCache(List<Region> regions) {
1929                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1930                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1931    
1932                    for (Region region : regions) {
1933                            EntityCacheUtil.removeResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
1934                                    RegionImpl.class, region.getPrimaryKey());
1935    
1936                            clearUniqueFindersCache(region);
1937                    }
1938            }
1939    
1940            protected void cacheUniqueFindersCache(Region region) {
1941                    if (region.isNew()) {
1942                            Object[] args = new Object[] {
1943                                            region.getCountryId(), region.getRegionCode()
1944                                    };
1945    
1946                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_R, args,
1947                                    Long.valueOf(1));
1948                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_R, args, region);
1949                    }
1950                    else {
1951                            RegionModelImpl regionModelImpl = (RegionModelImpl)region;
1952    
1953                            if ((regionModelImpl.getColumnBitmask() &
1954                                            FINDER_PATH_FETCH_BY_C_R.getColumnBitmask()) != 0) {
1955                                    Object[] args = new Object[] {
1956                                                    region.getCountryId(), region.getRegionCode()
1957                                            };
1958    
1959                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_R, args,
1960                                            Long.valueOf(1));
1961                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_R, args, region);
1962                            }
1963                    }
1964            }
1965    
1966            protected void clearUniqueFindersCache(Region region) {
1967                    RegionModelImpl regionModelImpl = (RegionModelImpl)region;
1968    
1969                    Object[] args = new Object[] {
1970                                    region.getCountryId(), region.getRegionCode()
1971                            };
1972    
1973                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_R, args);
1974                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_R, args);
1975    
1976                    if ((regionModelImpl.getColumnBitmask() &
1977                                    FINDER_PATH_FETCH_BY_C_R.getColumnBitmask()) != 0) {
1978                            args = new Object[] {
1979                                            regionModelImpl.getOriginalCountryId(),
1980                                            regionModelImpl.getOriginalRegionCode()
1981                                    };
1982    
1983                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_R, args);
1984                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_R, args);
1985                    }
1986            }
1987    
1988            /**
1989             * Creates a new region with the primary key. Does not add the region to the database.
1990             *
1991             * @param regionId the primary key for the new region
1992             * @return the new region
1993             */
1994            @Override
1995            public Region create(long regionId) {
1996                    Region region = new RegionImpl();
1997    
1998                    region.setNew(true);
1999                    region.setPrimaryKey(regionId);
2000    
2001                    return region;
2002            }
2003    
2004            /**
2005             * Removes the region with the primary key from the database. Also notifies the appropriate model listeners.
2006             *
2007             * @param regionId the primary key of the region
2008             * @return the region that was removed
2009             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
2010             * @throws SystemException if a system exception occurred
2011             */
2012            @Override
2013            public Region remove(long regionId)
2014                    throws NoSuchRegionException, SystemException {
2015                    return remove((Serializable)regionId);
2016            }
2017    
2018            /**
2019             * Removes the region with the primary key from the database. Also notifies the appropriate model listeners.
2020             *
2021             * @param primaryKey the primary key of the region
2022             * @return the region that was removed
2023             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
2024             * @throws SystemException if a system exception occurred
2025             */
2026            @Override
2027            public Region remove(Serializable primaryKey)
2028                    throws NoSuchRegionException, SystemException {
2029                    Session session = null;
2030    
2031                    try {
2032                            session = openSession();
2033    
2034                            Region region = (Region)session.get(RegionImpl.class, primaryKey);
2035    
2036                            if (region == null) {
2037                                    if (_log.isWarnEnabled()) {
2038                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
2039                                    }
2040    
2041                                    throw new NoSuchRegionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
2042                                            primaryKey);
2043                            }
2044    
2045                            return remove(region);
2046                    }
2047                    catch (NoSuchRegionException nsee) {
2048                            throw nsee;
2049                    }
2050                    catch (Exception e) {
2051                            throw processException(e);
2052                    }
2053                    finally {
2054                            closeSession(session);
2055                    }
2056            }
2057    
2058            @Override
2059            protected Region removeImpl(Region region) throws SystemException {
2060                    region = toUnwrappedModel(region);
2061    
2062                    Session session = null;
2063    
2064                    try {
2065                            session = openSession();
2066    
2067                            if (!session.contains(region)) {
2068                                    region = (Region)session.get(RegionImpl.class,
2069                                                    region.getPrimaryKeyObj());
2070                            }
2071    
2072                            if (region != null) {
2073                                    session.delete(region);
2074                            }
2075                    }
2076                    catch (Exception e) {
2077                            throw processException(e);
2078                    }
2079                    finally {
2080                            closeSession(session);
2081                    }
2082    
2083                    if (region != null) {
2084                            clearCache(region);
2085                    }
2086    
2087                    return region;
2088            }
2089    
2090            @Override
2091            public Region updateImpl(com.liferay.portal.model.Region region)
2092                    throws SystemException {
2093                    region = toUnwrappedModel(region);
2094    
2095                    boolean isNew = region.isNew();
2096    
2097                    RegionModelImpl regionModelImpl = (RegionModelImpl)region;
2098    
2099                    Session session = null;
2100    
2101                    try {
2102                            session = openSession();
2103    
2104                            if (region.isNew()) {
2105                                    session.save(region);
2106    
2107                                    region.setNew(false);
2108                            }
2109                            else {
2110                                    session.merge(region);
2111                            }
2112                    }
2113                    catch (Exception e) {
2114                            throw processException(e);
2115                    }
2116                    finally {
2117                            closeSession(session);
2118                    }
2119    
2120                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2121    
2122                    if (isNew || !RegionModelImpl.COLUMN_BITMASK_ENABLED) {
2123                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2124                    }
2125    
2126                    else {
2127                            if ((regionModelImpl.getColumnBitmask() &
2128                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COUNTRYID.getColumnBitmask()) != 0) {
2129                                    Object[] args = new Object[] {
2130                                                    regionModelImpl.getOriginalCountryId()
2131                                            };
2132    
2133                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COUNTRYID,
2134                                            args);
2135                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COUNTRYID,
2136                                            args);
2137    
2138                                    args = new Object[] { regionModelImpl.getCountryId() };
2139    
2140                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COUNTRYID,
2141                                            args);
2142                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COUNTRYID,
2143                                            args);
2144                            }
2145    
2146                            if ((regionModelImpl.getColumnBitmask() &
2147                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACTIVE.getColumnBitmask()) != 0) {
2148                                    Object[] args = new Object[] { regionModelImpl.getOriginalActive() };
2149    
2150                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ACTIVE, args);
2151                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACTIVE,
2152                                            args);
2153    
2154                                    args = new Object[] { regionModelImpl.getActive() };
2155    
2156                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ACTIVE, args);
2157                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ACTIVE,
2158                                            args);
2159                            }
2160    
2161                            if ((regionModelImpl.getColumnBitmask() &
2162                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_A.getColumnBitmask()) != 0) {
2163                                    Object[] args = new Object[] {
2164                                                    regionModelImpl.getOriginalCountryId(),
2165                                                    regionModelImpl.getOriginalActive()
2166                                            };
2167    
2168                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_A, args);
2169                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_A,
2170                                            args);
2171    
2172                                    args = new Object[] {
2173                                                    regionModelImpl.getCountryId(),
2174                                                    regionModelImpl.getActive()
2175                                            };
2176    
2177                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_A, args);
2178                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_A,
2179                                            args);
2180                            }
2181                    }
2182    
2183                    EntityCacheUtil.putResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
2184                            RegionImpl.class, region.getPrimaryKey(), region);
2185    
2186                    clearUniqueFindersCache(region);
2187                    cacheUniqueFindersCache(region);
2188    
2189                    return region;
2190            }
2191    
2192            protected Region toUnwrappedModel(Region region) {
2193                    if (region instanceof RegionImpl) {
2194                            return region;
2195                    }
2196    
2197                    RegionImpl regionImpl = new RegionImpl();
2198    
2199                    regionImpl.setNew(region.isNew());
2200                    regionImpl.setPrimaryKey(region.getPrimaryKey());
2201    
2202                    regionImpl.setRegionId(region.getRegionId());
2203                    regionImpl.setCountryId(region.getCountryId());
2204                    regionImpl.setRegionCode(region.getRegionCode());
2205                    regionImpl.setName(region.getName());
2206                    regionImpl.setActive(region.isActive());
2207    
2208                    return regionImpl;
2209            }
2210    
2211            /**
2212             * Returns the region with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
2213             *
2214             * @param primaryKey the primary key of the region
2215             * @return the region
2216             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
2217             * @throws SystemException if a system exception occurred
2218             */
2219            @Override
2220            public Region findByPrimaryKey(Serializable primaryKey)
2221                    throws NoSuchRegionException, SystemException {
2222                    Region region = fetchByPrimaryKey(primaryKey);
2223    
2224                    if (region == null) {
2225                            if (_log.isWarnEnabled()) {
2226                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
2227                            }
2228    
2229                            throw new NoSuchRegionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
2230                                    primaryKey);
2231                    }
2232    
2233                    return region;
2234            }
2235    
2236            /**
2237             * Returns the region with the primary key or throws a {@link com.liferay.portal.NoSuchRegionException} if it could not be found.
2238             *
2239             * @param regionId the primary key of the region
2240             * @return the region
2241             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
2242             * @throws SystemException if a system exception occurred
2243             */
2244            @Override
2245            public Region findByPrimaryKey(long regionId)
2246                    throws NoSuchRegionException, SystemException {
2247                    return findByPrimaryKey((Serializable)regionId);
2248            }
2249    
2250            /**
2251             * Returns the region with the primary key or returns <code>null</code> if it could not be found.
2252             *
2253             * @param primaryKey the primary key of the region
2254             * @return the region, or <code>null</code> if a region with the primary key could not be found
2255             * @throws SystemException if a system exception occurred
2256             */
2257            @Override
2258            public Region fetchByPrimaryKey(Serializable primaryKey)
2259                    throws SystemException {
2260                    Region region = (Region)EntityCacheUtil.getResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
2261                                    RegionImpl.class, primaryKey);
2262    
2263                    if (region == _nullRegion) {
2264                            return null;
2265                    }
2266    
2267                    if (region == null) {
2268                            Session session = null;
2269    
2270                            try {
2271                                    session = openSession();
2272    
2273                                    region = (Region)session.get(RegionImpl.class, primaryKey);
2274    
2275                                    if (region != null) {
2276                                            cacheResult(region);
2277                                    }
2278                                    else {
2279                                            EntityCacheUtil.putResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
2280                                                    RegionImpl.class, primaryKey, _nullRegion);
2281                                    }
2282                            }
2283                            catch (Exception e) {
2284                                    EntityCacheUtil.removeResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
2285                                            RegionImpl.class, primaryKey);
2286    
2287                                    throw processException(e);
2288                            }
2289                            finally {
2290                                    closeSession(session);
2291                            }
2292                    }
2293    
2294                    return region;
2295            }
2296    
2297            /**
2298             * Returns the region with the primary key or returns <code>null</code> if it could not be found.
2299             *
2300             * @param regionId the primary key of the region
2301             * @return the region, or <code>null</code> if a region with the primary key could not be found
2302             * @throws SystemException if a system exception occurred
2303             */
2304            @Override
2305            public Region fetchByPrimaryKey(long regionId) throws SystemException {
2306                    return fetchByPrimaryKey((Serializable)regionId);
2307            }
2308    
2309            /**
2310             * Returns all the regions.
2311             *
2312             * @return the regions
2313             * @throws SystemException if a system exception occurred
2314             */
2315            @Override
2316            public List<Region> findAll() throws SystemException {
2317                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2318            }
2319    
2320            /**
2321             * Returns a range of all the regions.
2322             *
2323             * <p>
2324             * 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.
2325             * </p>
2326             *
2327             * @param start the lower bound of the range of regions
2328             * @param end the upper bound of the range of regions (not inclusive)
2329             * @return the range of regions
2330             * @throws SystemException if a system exception occurred
2331             */
2332            @Override
2333            public List<Region> findAll(int start, int end) throws SystemException {
2334                    return findAll(start, end, null);
2335            }
2336    
2337            /**
2338             * Returns an ordered range of all the regions.
2339             *
2340             * <p>
2341             * 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.
2342             * </p>
2343             *
2344             * @param start the lower bound of the range of regions
2345             * @param end the upper bound of the range of regions (not inclusive)
2346             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2347             * @return the ordered range of regions
2348             * @throws SystemException if a system exception occurred
2349             */
2350            @Override
2351            public List<Region> findAll(int start, int end,
2352                    OrderByComparator orderByComparator) throws SystemException {
2353                    boolean pagination = true;
2354                    FinderPath finderPath = null;
2355                    Object[] finderArgs = null;
2356    
2357                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2358                                    (orderByComparator == null)) {
2359                            pagination = false;
2360                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
2361                            finderArgs = FINDER_ARGS_EMPTY;
2362                    }
2363                    else {
2364                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
2365                            finderArgs = new Object[] { start, end, orderByComparator };
2366                    }
2367    
2368                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(finderPath,
2369                                    finderArgs, this);
2370    
2371                    if (list == null) {
2372                            StringBundler query = null;
2373                            String sql = null;
2374    
2375                            if (orderByComparator != null) {
2376                                    query = new StringBundler(2 +
2377                                                    (orderByComparator.getOrderByFields().length * 3));
2378    
2379                                    query.append(_SQL_SELECT_REGION);
2380    
2381                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2382                                            orderByComparator);
2383    
2384                                    sql = query.toString();
2385                            }
2386                            else {
2387                                    sql = _SQL_SELECT_REGION;
2388    
2389                                    if (pagination) {
2390                                            sql = sql.concat(RegionModelImpl.ORDER_BY_JPQL);
2391                                    }
2392                            }
2393    
2394                            Session session = null;
2395    
2396                            try {
2397                                    session = openSession();
2398    
2399                                    Query q = session.createQuery(sql);
2400    
2401                                    if (!pagination) {
2402                                            list = (List<Region>)QueryUtil.list(q, getDialect(), start,
2403                                                            end, false);
2404    
2405                                            Collections.sort(list);
2406    
2407                                            list = new UnmodifiableList<Region>(list);
2408                                    }
2409                                    else {
2410                                            list = (List<Region>)QueryUtil.list(q, getDialect(), start,
2411                                                            end);
2412                                    }
2413    
2414                                    cacheResult(list);
2415    
2416                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2417                            }
2418                            catch (Exception e) {
2419                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2420    
2421                                    throw processException(e);
2422                            }
2423                            finally {
2424                                    closeSession(session);
2425                            }
2426                    }
2427    
2428                    return list;
2429            }
2430    
2431            /**
2432             * Removes all the regions from the database.
2433             *
2434             * @throws SystemException if a system exception occurred
2435             */
2436            @Override
2437            public void removeAll() throws SystemException {
2438                    for (Region region : findAll()) {
2439                            remove(region);
2440                    }
2441            }
2442    
2443            /**
2444             * Returns the number of regions.
2445             *
2446             * @return the number of regions
2447             * @throws SystemException if a system exception occurred
2448             */
2449            @Override
2450            public int countAll() throws SystemException {
2451                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
2452                                    FINDER_ARGS_EMPTY, this);
2453    
2454                    if (count == null) {
2455                            Session session = null;
2456    
2457                            try {
2458                                    session = openSession();
2459    
2460                                    Query q = session.createQuery(_SQL_COUNT_REGION);
2461    
2462                                    count = (Long)q.uniqueResult();
2463    
2464                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
2465                                            FINDER_ARGS_EMPTY, count);
2466                            }
2467                            catch (Exception e) {
2468                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
2469                                            FINDER_ARGS_EMPTY);
2470    
2471                                    throw processException(e);
2472                            }
2473                            finally {
2474                                    closeSession(session);
2475                            }
2476                    }
2477    
2478                    return count.intValue();
2479            }
2480    
2481            @Override
2482            protected Set<String> getBadColumnNames() {
2483                    return _badColumnNames;
2484            }
2485    
2486            /**
2487             * Initializes the region persistence.
2488             */
2489            public void afterPropertiesSet() {
2490                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2491                                            com.liferay.portal.util.PropsUtil.get(
2492                                                    "value.object.listener.com.liferay.portal.model.Region")));
2493    
2494                    if (listenerClassNames.length > 0) {
2495                            try {
2496                                    List<ModelListener<Region>> listenersList = new ArrayList<ModelListener<Region>>();
2497    
2498                                    for (String listenerClassName : listenerClassNames) {
2499                                            listenersList.add((ModelListener<Region>)InstanceFactory.newInstance(
2500                                                            getClassLoader(), listenerClassName));
2501                                    }
2502    
2503                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2504                            }
2505                            catch (Exception e) {
2506                                    _log.error(e);
2507                            }
2508                    }
2509            }
2510    
2511            public void destroy() {
2512                    EntityCacheUtil.removeCache(RegionImpl.class.getName());
2513                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
2514                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2515                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2516            }
2517    
2518            private static final String _SQL_SELECT_REGION = "SELECT region FROM Region region";
2519            private static final String _SQL_SELECT_REGION_WHERE = "SELECT region FROM Region region WHERE ";
2520            private static final String _SQL_COUNT_REGION = "SELECT COUNT(region) FROM Region region";
2521            private static final String _SQL_COUNT_REGION_WHERE = "SELECT COUNT(region) FROM Region region WHERE ";
2522            private static final String _ORDER_BY_ENTITY_ALIAS = "region.";
2523            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Region exists with the primary key ";
2524            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Region exists with the key {";
2525            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
2526            private static Log _log = LogFactoryUtil.getLog(RegionPersistenceImpl.class);
2527            private static Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
2528                                    "active"
2529                            });
2530            private static Region _nullRegion = new RegionImpl() {
2531                            @Override
2532                            public Object clone() {
2533                                    return this;
2534                            }
2535    
2536                            @Override
2537                            public CacheModel<Region> toCacheModel() {
2538                                    return _nullRegionCacheModel;
2539                            }
2540                    };
2541    
2542            private static CacheModel<Region> _nullRegionCacheModel = new CacheModel<Region>() {
2543                            @Override
2544                            public Region toEntityModel() {
2545                                    return _nullRegion;
2546                            }
2547                    };
2548    }