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