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