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