001    /**
002     * Copyright (c) 2000-2010 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.annotation.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023    import com.liferay.portal.kernel.dao.orm.FinderPath;
024    import com.liferay.portal.kernel.dao.orm.Query;
025    import com.liferay.portal.kernel.dao.orm.QueryPos;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.model.ModelListener;
038    import com.liferay.portal.model.Region;
039    import com.liferay.portal.model.impl.RegionImpl;
040    import com.liferay.portal.model.impl.RegionModelImpl;
041    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
042    
043    import java.io.Serializable;
044    
045    import java.util.ArrayList;
046    import java.util.Collections;
047    import java.util.List;
048    
049    /**
050     * The persistence implementation for the region service.
051     *
052     * <p>
053     * 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.
054     * </p>
055     *
056     * <p>
057     * Caching information and settings can be found in <code>portal.properties</code>
058     * </p>
059     *
060     * @author Brian Wing Shun Chan
061     * @see RegionPersistence
062     * @see RegionUtil
063     * @generated
064     */
065    public class RegionPersistenceImpl extends BasePersistenceImpl<Region>
066            implements RegionPersistence {
067            public static final String FINDER_CLASS_NAME_ENTITY = RegionImpl.class.getName();
068            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
069                    ".List";
070            public static final FinderPath FINDER_PATH_FIND_BY_COUNTRYID = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
071                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
072                            "findByCountryId",
073                            new String[] {
074                                    Long.class.getName(),
075                                    
076                            "java.lang.Integer", "java.lang.Integer",
077                                    "com.liferay.portal.kernel.util.OrderByComparator"
078                            });
079            public static final FinderPath FINDER_PATH_COUNT_BY_COUNTRYID = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
080                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
081                            "countByCountryId", new String[] { Long.class.getName() });
082            public static final FinderPath FINDER_PATH_FIND_BY_ACTIVE = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
083                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
084                            "findByActive",
085                            new String[] {
086                                    Boolean.class.getName(),
087                                    
088                            "java.lang.Integer", "java.lang.Integer",
089                                    "com.liferay.portal.kernel.util.OrderByComparator"
090                            });
091            public static final FinderPath FINDER_PATH_COUNT_BY_ACTIVE = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
092                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
093                            "countByActive", new String[] { Boolean.class.getName() });
094            public static final FinderPath FINDER_PATH_FIND_BY_C_A = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
095                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
096                            "findByC_A",
097                            new String[] {
098                                    Long.class.getName(), Boolean.class.getName(),
099                                    
100                            "java.lang.Integer", "java.lang.Integer",
101                                    "com.liferay.portal.kernel.util.OrderByComparator"
102                            });
103            public static final FinderPath FINDER_PATH_COUNT_BY_C_A = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
104                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
105                            "countByC_A",
106                            new String[] { Long.class.getName(), Boolean.class.getName() });
107            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
108                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
109                            "findAll", new String[0]);
110            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RegionModelImpl.ENTITY_CACHE_ENABLED,
111                            RegionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
112                            "countAll", new String[0]);
113    
114            /**
115             * Caches the region in the entity cache if it is enabled.
116             *
117             * @param region the region to cache
118             */
119            public void cacheResult(Region region) {
120                    EntityCacheUtil.putResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
121                            RegionImpl.class, region.getPrimaryKey(), region);
122            }
123    
124            /**
125             * Caches the regions in the entity cache if it is enabled.
126             *
127             * @param regions the regions to cache
128             */
129            public void cacheResult(List<Region> regions) {
130                    for (Region region : regions) {
131                            if (EntityCacheUtil.getResult(
132                                                    RegionModelImpl.ENTITY_CACHE_ENABLED, RegionImpl.class,
133                                                    region.getPrimaryKey(), this) == null) {
134                                    cacheResult(region);
135                            }
136                    }
137            }
138    
139            /**
140             * Clears the cache for all regions.
141             *
142             * <p>
143             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
144             * </p>
145             */
146            public void clearCache() {
147                    CacheRegistryUtil.clear(RegionImpl.class.getName());
148                    EntityCacheUtil.clearCache(RegionImpl.class.getName());
149                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
150                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
151            }
152    
153            /**
154             * Clears the cache for the region.
155             *
156             * <p>
157             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
158             * </p>
159             */
160            public void clearCache(Region region) {
161                    EntityCacheUtil.removeResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
162                            RegionImpl.class, region.getPrimaryKey());
163            }
164    
165            /**
166             * Creates a new region with the primary key. Does not add the region to the database.
167             *
168             * @param regionId the primary key for the new region
169             * @return the new region
170             */
171            public Region create(long regionId) {
172                    Region region = new RegionImpl();
173    
174                    region.setNew(true);
175                    region.setPrimaryKey(regionId);
176    
177                    return region;
178            }
179    
180            /**
181             * Removes the region with the primary key from the database. Also notifies the appropriate model listeners.
182             *
183             * @param primaryKey the primary key of the region to remove
184             * @return the region that was removed
185             * @throws com.liferay.portal.NoSuchModelException if a region with the primary key could not be found
186             * @throws SystemException if a system exception occurred
187             */
188            public Region remove(Serializable primaryKey)
189                    throws NoSuchModelException, SystemException {
190                    return remove(((Long)primaryKey).longValue());
191            }
192    
193            /**
194             * Removes the region with the primary key from the database. Also notifies the appropriate model listeners.
195             *
196             * @param regionId the primary key of the region to remove
197             * @return the region that was removed
198             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
199             * @throws SystemException if a system exception occurred
200             */
201            public Region remove(long regionId)
202                    throws NoSuchRegionException, SystemException {
203                    Session session = null;
204    
205                    try {
206                            session = openSession();
207    
208                            Region region = (Region)session.get(RegionImpl.class,
209                                            new Long(regionId));
210    
211                            if (region == null) {
212                                    if (_log.isWarnEnabled()) {
213                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + regionId);
214                                    }
215    
216                                    throw new NoSuchRegionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
217                                            regionId);
218                            }
219    
220                            return remove(region);
221                    }
222                    catch (NoSuchRegionException nsee) {
223                            throw nsee;
224                    }
225                    catch (Exception e) {
226                            throw processException(e);
227                    }
228                    finally {
229                            closeSession(session);
230                    }
231            }
232    
233            protected Region removeImpl(Region region) throws SystemException {
234                    region = toUnwrappedModel(region);
235    
236                    Session session = null;
237    
238                    try {
239                            session = openSession();
240    
241                            if (region.isCachedModel() || BatchSessionUtil.isEnabled()) {
242                                    Object staleObject = session.get(RegionImpl.class,
243                                                    region.getPrimaryKeyObj());
244    
245                                    if (staleObject != null) {
246                                            session.evict(staleObject);
247                                    }
248                            }
249    
250                            session.delete(region);
251    
252                            session.flush();
253                    }
254                    catch (Exception e) {
255                            throw processException(e);
256                    }
257                    finally {
258                            closeSession(session);
259                    }
260    
261                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
262    
263                    EntityCacheUtil.removeResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
264                            RegionImpl.class, region.getPrimaryKey());
265    
266                    return region;
267            }
268    
269            public Region updateImpl(com.liferay.portal.model.Region region,
270                    boolean merge) throws SystemException {
271                    region = toUnwrappedModel(region);
272    
273                    Session session = null;
274    
275                    try {
276                            session = openSession();
277    
278                            BatchSessionUtil.update(session, region, merge);
279    
280                            region.setNew(false);
281                    }
282                    catch (Exception e) {
283                            throw processException(e);
284                    }
285                    finally {
286                            closeSession(session);
287                    }
288    
289                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
290    
291                    EntityCacheUtil.putResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
292                            RegionImpl.class, region.getPrimaryKey(), region);
293    
294                    return region;
295            }
296    
297            protected Region toUnwrappedModel(Region region) {
298                    if (region instanceof RegionImpl) {
299                            return region;
300                    }
301    
302                    RegionImpl regionImpl = new RegionImpl();
303    
304                    regionImpl.setNew(region.isNew());
305                    regionImpl.setPrimaryKey(region.getPrimaryKey());
306    
307                    regionImpl.setRegionId(region.getRegionId());
308                    regionImpl.setCountryId(region.getCountryId());
309                    regionImpl.setRegionCode(region.getRegionCode());
310                    regionImpl.setName(region.getName());
311                    regionImpl.setActive(region.isActive());
312    
313                    return regionImpl;
314            }
315    
316            /**
317             * Finds the region with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
318             *
319             * @param primaryKey the primary key of the region to find
320             * @return the region
321             * @throws com.liferay.portal.NoSuchModelException if a region with the primary key could not be found
322             * @throws SystemException if a system exception occurred
323             */
324            public Region findByPrimaryKey(Serializable primaryKey)
325                    throws NoSuchModelException, SystemException {
326                    return findByPrimaryKey(((Long)primaryKey).longValue());
327            }
328    
329            /**
330             * Finds the region with the primary key or throws a {@link com.liferay.portal.NoSuchRegionException} if it could not be found.
331             *
332             * @param regionId the primary key of the region to find
333             * @return the region
334             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
335             * @throws SystemException if a system exception occurred
336             */
337            public Region findByPrimaryKey(long regionId)
338                    throws NoSuchRegionException, SystemException {
339                    Region region = fetchByPrimaryKey(regionId);
340    
341                    if (region == null) {
342                            if (_log.isWarnEnabled()) {
343                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + regionId);
344                            }
345    
346                            throw new NoSuchRegionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
347                                    regionId);
348                    }
349    
350                    return region;
351            }
352    
353            /**
354             * Finds the region with the primary key or returns <code>null</code> if it could not be found.
355             *
356             * @param primaryKey the primary key of the region to find
357             * @return the region, or <code>null</code> if a region with the primary key could not be found
358             * @throws SystemException if a system exception occurred
359             */
360            public Region fetchByPrimaryKey(Serializable primaryKey)
361                    throws SystemException {
362                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
363            }
364    
365            /**
366             * Finds the region with the primary key or returns <code>null</code> if it could not be found.
367             *
368             * @param regionId the primary key of the region to find
369             * @return the region, or <code>null</code> if a region with the primary key could not be found
370             * @throws SystemException if a system exception occurred
371             */
372            public Region fetchByPrimaryKey(long regionId) throws SystemException {
373                    Region region = (Region)EntityCacheUtil.getResult(RegionModelImpl.ENTITY_CACHE_ENABLED,
374                                    RegionImpl.class, regionId, this);
375    
376                    if (region == null) {
377                            Session session = null;
378    
379                            try {
380                                    session = openSession();
381    
382                                    region = (Region)session.get(RegionImpl.class,
383                                                    new Long(regionId));
384                            }
385                            catch (Exception e) {
386                                    throw processException(e);
387                            }
388                            finally {
389                                    if (region != null) {
390                                            cacheResult(region);
391                                    }
392    
393                                    closeSession(session);
394                            }
395                    }
396    
397                    return region;
398            }
399    
400            /**
401             * Finds all the regions where countryId = &#63;.
402             *
403             * @param countryId the country id to search with
404             * @return the matching regions
405             * @throws SystemException if a system exception occurred
406             */
407            public List<Region> findByCountryId(long countryId)
408                    throws SystemException {
409                    return findByCountryId(countryId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
410                            null);
411            }
412    
413            /**
414             * Finds a range of all the regions where countryId = &#63;.
415             *
416             * <p>
417             * 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.
418             * </p>
419             *
420             * @param countryId the country id to search with
421             * @param start the lower bound of the range of regions to return
422             * @param end the upper bound of the range of regions to return (not inclusive)
423             * @return the range of matching regions
424             * @throws SystemException if a system exception occurred
425             */
426            public List<Region> findByCountryId(long countryId, int start, int end)
427                    throws SystemException {
428                    return findByCountryId(countryId, start, end, null);
429            }
430    
431            /**
432             * Finds an ordered range of all the regions where countryId = &#63;.
433             *
434             * <p>
435             * 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.
436             * </p>
437             *
438             * @param countryId the country id to search with
439             * @param start the lower bound of the range of regions to return
440             * @param end the upper bound of the range of regions to return (not inclusive)
441             * @param orderByComparator the comparator to order the results by
442             * @return the ordered range of matching regions
443             * @throws SystemException if a system exception occurred
444             */
445            public List<Region> findByCountryId(long countryId, int start, int end,
446                    OrderByComparator orderByComparator) throws SystemException {
447                    Object[] finderArgs = new Object[] {
448                                    countryId,
449                                    
450                                    String.valueOf(start), String.valueOf(end),
451                                    String.valueOf(orderByComparator)
452                            };
453    
454                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COUNTRYID,
455                                    finderArgs, this);
456    
457                    if (list == null) {
458                            Session session = null;
459    
460                            try {
461                                    session = openSession();
462    
463                                    StringBundler query = null;
464    
465                                    if (orderByComparator != null) {
466                                            query = new StringBundler(3 +
467                                                            (orderByComparator.getOrderByFields().length * 3));
468                                    }
469                                    else {
470                                            query = new StringBundler(3);
471                                    }
472    
473                                    query.append(_SQL_SELECT_REGION_WHERE);
474    
475                                    query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
476    
477                                    if (orderByComparator != null) {
478                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
479                                                    orderByComparator);
480                                    }
481    
482                                    else {
483                                            query.append(RegionModelImpl.ORDER_BY_JPQL);
484                                    }
485    
486                                    String sql = query.toString();
487    
488                                    Query q = session.createQuery(sql);
489    
490                                    QueryPos qPos = QueryPos.getInstance(q);
491    
492                                    qPos.add(countryId);
493    
494                                    list = (List<Region>)QueryUtil.list(q, getDialect(), start, end);
495                            }
496                            catch (Exception e) {
497                                    throw processException(e);
498                            }
499                            finally {
500                                    if (list == null) {
501                                            list = new ArrayList<Region>();
502                                    }
503    
504                                    cacheResult(list);
505    
506                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COUNTRYID,
507                                            finderArgs, list);
508    
509                                    closeSession(session);
510                            }
511                    }
512    
513                    return list;
514            }
515    
516            /**
517             * Finds the first region in the ordered set where countryId = &#63;.
518             *
519             * <p>
520             * 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.
521             * </p>
522             *
523             * @param countryId the country id to search with
524             * @param orderByComparator the comparator to order the set by
525             * @return the first matching region
526             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
527             * @throws SystemException if a system exception occurred
528             */
529            public Region findByCountryId_First(long countryId,
530                    OrderByComparator orderByComparator)
531                    throws NoSuchRegionException, SystemException {
532                    List<Region> list = findByCountryId(countryId, 0, 1, orderByComparator);
533    
534                    if (list.isEmpty()) {
535                            StringBundler msg = new StringBundler(4);
536    
537                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
538    
539                            msg.append("countryId=");
540                            msg.append(countryId);
541    
542                            msg.append(StringPool.CLOSE_CURLY_BRACE);
543    
544                            throw new NoSuchRegionException(msg.toString());
545                    }
546                    else {
547                            return list.get(0);
548                    }
549            }
550    
551            /**
552             * Finds the last region in the ordered set where countryId = &#63;.
553             *
554             * <p>
555             * 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.
556             * </p>
557             *
558             * @param countryId the country id to search with
559             * @param orderByComparator the comparator to order the set by
560             * @return the last matching region
561             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
562             * @throws SystemException if a system exception occurred
563             */
564            public Region findByCountryId_Last(long countryId,
565                    OrderByComparator orderByComparator)
566                    throws NoSuchRegionException, SystemException {
567                    int count = countByCountryId(countryId);
568    
569                    List<Region> list = findByCountryId(countryId, count - 1, count,
570                                    orderByComparator);
571    
572                    if (list.isEmpty()) {
573                            StringBundler msg = new StringBundler(4);
574    
575                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
576    
577                            msg.append("countryId=");
578                            msg.append(countryId);
579    
580                            msg.append(StringPool.CLOSE_CURLY_BRACE);
581    
582                            throw new NoSuchRegionException(msg.toString());
583                    }
584                    else {
585                            return list.get(0);
586                    }
587            }
588    
589            /**
590             * Finds the regions before and after the current region in the ordered set where countryId = &#63;.
591             *
592             * <p>
593             * 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.
594             * </p>
595             *
596             * @param regionId the primary key of the current region
597             * @param countryId the country id to search with
598             * @param orderByComparator the comparator to order the set by
599             * @return the previous, current, and next region
600             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
601             * @throws SystemException if a system exception occurred
602             */
603            public Region[] findByCountryId_PrevAndNext(long regionId, long countryId,
604                    OrderByComparator orderByComparator)
605                    throws NoSuchRegionException, SystemException {
606                    Region region = findByPrimaryKey(regionId);
607    
608                    Session session = null;
609    
610                    try {
611                            session = openSession();
612    
613                            Region[] array = new RegionImpl[3];
614    
615                            array[0] = getByCountryId_PrevAndNext(session, region, countryId,
616                                            orderByComparator, true);
617    
618                            array[1] = region;
619    
620                            array[2] = getByCountryId_PrevAndNext(session, region, countryId,
621                                            orderByComparator, false);
622    
623                            return array;
624                    }
625                    catch (Exception e) {
626                            throw processException(e);
627                    }
628                    finally {
629                            closeSession(session);
630                    }
631            }
632    
633            protected Region getByCountryId_PrevAndNext(Session session, Region region,
634                    long countryId, OrderByComparator orderByComparator, boolean previous) {
635                    StringBundler query = null;
636    
637                    if (orderByComparator != null) {
638                            query = new StringBundler(6 +
639                                            (orderByComparator.getOrderByFields().length * 6));
640                    }
641                    else {
642                            query = new StringBundler(3);
643                    }
644    
645                    query.append(_SQL_SELECT_REGION_WHERE);
646    
647                    query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
648    
649                    if (orderByComparator != null) {
650                            String[] orderByFields = orderByComparator.getOrderByFields();
651    
652                            if (orderByFields.length > 0) {
653                                    query.append(WHERE_AND);
654                            }
655    
656                            for (int i = 0; i < orderByFields.length; i++) {
657                                    query.append(_ORDER_BY_ENTITY_ALIAS);
658                                    query.append(orderByFields[i]);
659    
660                                    if ((i + 1) < orderByFields.length) {
661                                            if (orderByComparator.isAscending() ^ previous) {
662                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
663                                            }
664                                            else {
665                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
666                                            }
667                                    }
668                                    else {
669                                            if (orderByComparator.isAscending() ^ previous) {
670                                                    query.append(WHERE_GREATER_THAN);
671                                            }
672                                            else {
673                                                    query.append(WHERE_LESSER_THAN);
674                                            }
675                                    }
676                            }
677    
678                            query.append(ORDER_BY_CLAUSE);
679    
680                            for (int i = 0; i < orderByFields.length; i++) {
681                                    query.append(_ORDER_BY_ENTITY_ALIAS);
682                                    query.append(orderByFields[i]);
683    
684                                    if ((i + 1) < orderByFields.length) {
685                                            if (orderByComparator.isAscending() ^ previous) {
686                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
687                                            }
688                                            else {
689                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
690                                            }
691                                    }
692                                    else {
693                                            if (orderByComparator.isAscending() ^ previous) {
694                                                    query.append(ORDER_BY_ASC);
695                                            }
696                                            else {
697                                                    query.append(ORDER_BY_DESC);
698                                            }
699                                    }
700                            }
701                    }
702    
703                    else {
704                            query.append(RegionModelImpl.ORDER_BY_JPQL);
705                    }
706    
707                    String sql = query.toString();
708    
709                    Query q = session.createQuery(sql);
710    
711                    q.setFirstResult(0);
712                    q.setMaxResults(2);
713    
714                    QueryPos qPos = QueryPos.getInstance(q);
715    
716                    qPos.add(countryId);
717    
718                    if (orderByComparator != null) {
719                            Object[] values = orderByComparator.getOrderByValues(region);
720    
721                            for (Object value : values) {
722                                    qPos.add(value);
723                            }
724                    }
725    
726                    List<Region> list = q.list();
727    
728                    if (list.size() == 2) {
729                            return list.get(1);
730                    }
731                    else {
732                            return null;
733                    }
734            }
735    
736            /**
737             * Finds all the regions where active = &#63;.
738             *
739             * @param active the active to search with
740             * @return the matching regions
741             * @throws SystemException if a system exception occurred
742             */
743            public List<Region> findByActive(boolean active) throws SystemException {
744                    return findByActive(active, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
745            }
746    
747            /**
748             * Finds a range of all the regions where active = &#63;.
749             *
750             * <p>
751             * 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.
752             * </p>
753             *
754             * @param active the active to search with
755             * @param start the lower bound of the range of regions to return
756             * @param end the upper bound of the range of regions to return (not inclusive)
757             * @return the range of matching regions
758             * @throws SystemException if a system exception occurred
759             */
760            public List<Region> findByActive(boolean active, int start, int end)
761                    throws SystemException {
762                    return findByActive(active, start, end, null);
763            }
764    
765            /**
766             * Finds an ordered range of all the regions where active = &#63;.
767             *
768             * <p>
769             * 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.
770             * </p>
771             *
772             * @param active the active to search with
773             * @param start the lower bound of the range of regions to return
774             * @param end the upper bound of the range of regions to return (not inclusive)
775             * @param orderByComparator the comparator to order the results by
776             * @return the ordered range of matching regions
777             * @throws SystemException if a system exception occurred
778             */
779            public List<Region> findByActive(boolean active, int start, int end,
780                    OrderByComparator orderByComparator) throws SystemException {
781                    Object[] finderArgs = new Object[] {
782                                    active,
783                                    
784                                    String.valueOf(start), String.valueOf(end),
785                                    String.valueOf(orderByComparator)
786                            };
787    
788                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_ACTIVE,
789                                    finderArgs, this);
790    
791                    if (list == null) {
792                            Session session = null;
793    
794                            try {
795                                    session = openSession();
796    
797                                    StringBundler query = null;
798    
799                                    if (orderByComparator != null) {
800                                            query = new StringBundler(3 +
801                                                            (orderByComparator.getOrderByFields().length * 3));
802                                    }
803                                    else {
804                                            query = new StringBundler(3);
805                                    }
806    
807                                    query.append(_SQL_SELECT_REGION_WHERE);
808    
809                                    query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
810    
811                                    if (orderByComparator != null) {
812                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
813                                                    orderByComparator);
814                                    }
815    
816                                    else {
817                                            query.append(RegionModelImpl.ORDER_BY_JPQL);
818                                    }
819    
820                                    String sql = query.toString();
821    
822                                    Query q = session.createQuery(sql);
823    
824                                    QueryPos qPos = QueryPos.getInstance(q);
825    
826                                    qPos.add(active);
827    
828                                    list = (List<Region>)QueryUtil.list(q, getDialect(), start, end);
829                            }
830                            catch (Exception e) {
831                                    throw processException(e);
832                            }
833                            finally {
834                                    if (list == null) {
835                                            list = new ArrayList<Region>();
836                                    }
837    
838                                    cacheResult(list);
839    
840                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_ACTIVE,
841                                            finderArgs, list);
842    
843                                    closeSession(session);
844                            }
845                    }
846    
847                    return list;
848            }
849    
850            /**
851             * Finds the first region in the ordered set where active = &#63;.
852             *
853             * <p>
854             * 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.
855             * </p>
856             *
857             * @param active the active to search with
858             * @param orderByComparator the comparator to order the set by
859             * @return the first matching region
860             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
861             * @throws SystemException if a system exception occurred
862             */
863            public Region findByActive_First(boolean active,
864                    OrderByComparator orderByComparator)
865                    throws NoSuchRegionException, SystemException {
866                    List<Region> list = findByActive(active, 0, 1, orderByComparator);
867    
868                    if (list.isEmpty()) {
869                            StringBundler msg = new StringBundler(4);
870    
871                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
872    
873                            msg.append("active=");
874                            msg.append(active);
875    
876                            msg.append(StringPool.CLOSE_CURLY_BRACE);
877    
878                            throw new NoSuchRegionException(msg.toString());
879                    }
880                    else {
881                            return list.get(0);
882                    }
883            }
884    
885            /**
886             * Finds the last region in the ordered set where active = &#63;.
887             *
888             * <p>
889             * 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.
890             * </p>
891             *
892             * @param active the active to search with
893             * @param orderByComparator the comparator to order the set by
894             * @return the last matching region
895             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
896             * @throws SystemException if a system exception occurred
897             */
898            public Region findByActive_Last(boolean active,
899                    OrderByComparator orderByComparator)
900                    throws NoSuchRegionException, SystemException {
901                    int count = countByActive(active);
902    
903                    List<Region> list = findByActive(active, count - 1, count,
904                                    orderByComparator);
905    
906                    if (list.isEmpty()) {
907                            StringBundler msg = new StringBundler(4);
908    
909                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
910    
911                            msg.append("active=");
912                            msg.append(active);
913    
914                            msg.append(StringPool.CLOSE_CURLY_BRACE);
915    
916                            throw new NoSuchRegionException(msg.toString());
917                    }
918                    else {
919                            return list.get(0);
920                    }
921            }
922    
923            /**
924             * Finds the regions before and after the current region in the ordered set where active = &#63;.
925             *
926             * <p>
927             * 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.
928             * </p>
929             *
930             * @param regionId the primary key of the current region
931             * @param active the active to search with
932             * @param orderByComparator the comparator to order the set by
933             * @return the previous, current, and next region
934             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
935             * @throws SystemException if a system exception occurred
936             */
937            public Region[] findByActive_PrevAndNext(long regionId, boolean active,
938                    OrderByComparator orderByComparator)
939                    throws NoSuchRegionException, SystemException {
940                    Region region = findByPrimaryKey(regionId);
941    
942                    Session session = null;
943    
944                    try {
945                            session = openSession();
946    
947                            Region[] array = new RegionImpl[3];
948    
949                            array[0] = getByActive_PrevAndNext(session, region, active,
950                                            orderByComparator, true);
951    
952                            array[1] = region;
953    
954                            array[2] = getByActive_PrevAndNext(session, region, active,
955                                            orderByComparator, false);
956    
957                            return array;
958                    }
959                    catch (Exception e) {
960                            throw processException(e);
961                    }
962                    finally {
963                            closeSession(session);
964                    }
965            }
966    
967            protected Region getByActive_PrevAndNext(Session session, Region region,
968                    boolean active, OrderByComparator orderByComparator, boolean previous) {
969                    StringBundler query = null;
970    
971                    if (orderByComparator != null) {
972                            query = new StringBundler(6 +
973                                            (orderByComparator.getOrderByFields().length * 6));
974                    }
975                    else {
976                            query = new StringBundler(3);
977                    }
978    
979                    query.append(_SQL_SELECT_REGION_WHERE);
980    
981                    query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
982    
983                    if (orderByComparator != null) {
984                            String[] orderByFields = orderByComparator.getOrderByFields();
985    
986                            if (orderByFields.length > 0) {
987                                    query.append(WHERE_AND);
988                            }
989    
990                            for (int i = 0; i < orderByFields.length; i++) {
991                                    query.append(_ORDER_BY_ENTITY_ALIAS);
992                                    query.append(orderByFields[i]);
993    
994                                    if ((i + 1) < orderByFields.length) {
995                                            if (orderByComparator.isAscending() ^ previous) {
996                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
997                                            }
998                                            else {
999                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1000                                            }
1001                                    }
1002                                    else {
1003                                            if (orderByComparator.isAscending() ^ previous) {
1004                                                    query.append(WHERE_GREATER_THAN);
1005                                            }
1006                                            else {
1007                                                    query.append(WHERE_LESSER_THAN);
1008                                            }
1009                                    }
1010                            }
1011    
1012                            query.append(ORDER_BY_CLAUSE);
1013    
1014                            for (int i = 0; i < orderByFields.length; i++) {
1015                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1016                                    query.append(orderByFields[i]);
1017    
1018                                    if ((i + 1) < orderByFields.length) {
1019                                            if (orderByComparator.isAscending() ^ previous) {
1020                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1021                                            }
1022                                            else {
1023                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1024                                            }
1025                                    }
1026                                    else {
1027                                            if (orderByComparator.isAscending() ^ previous) {
1028                                                    query.append(ORDER_BY_ASC);
1029                                            }
1030                                            else {
1031                                                    query.append(ORDER_BY_DESC);
1032                                            }
1033                                    }
1034                            }
1035                    }
1036    
1037                    else {
1038                            query.append(RegionModelImpl.ORDER_BY_JPQL);
1039                    }
1040    
1041                    String sql = query.toString();
1042    
1043                    Query q = session.createQuery(sql);
1044    
1045                    q.setFirstResult(0);
1046                    q.setMaxResults(2);
1047    
1048                    QueryPos qPos = QueryPos.getInstance(q);
1049    
1050                    qPos.add(active);
1051    
1052                    if (orderByComparator != null) {
1053                            Object[] values = orderByComparator.getOrderByValues(region);
1054    
1055                            for (Object value : values) {
1056                                    qPos.add(value);
1057                            }
1058                    }
1059    
1060                    List<Region> list = q.list();
1061    
1062                    if (list.size() == 2) {
1063                            return list.get(1);
1064                    }
1065                    else {
1066                            return null;
1067                    }
1068            }
1069    
1070            /**
1071             * Finds all the regions where countryId = &#63; and active = &#63;.
1072             *
1073             * @param countryId the country id to search with
1074             * @param active the active to search with
1075             * @return the matching regions
1076             * @throws SystemException if a system exception occurred
1077             */
1078            public List<Region> findByC_A(long countryId, boolean active)
1079                    throws SystemException {
1080                    return findByC_A(countryId, active, QueryUtil.ALL_POS,
1081                            QueryUtil.ALL_POS, null);
1082            }
1083    
1084            /**
1085             * Finds a range of all the regions where countryId = &#63; and active = &#63;.
1086             *
1087             * <p>
1088             * 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.
1089             * </p>
1090             *
1091             * @param countryId the country id to search with
1092             * @param active the active to search with
1093             * @param start the lower bound of the range of regions to return
1094             * @param end the upper bound of the range of regions to return (not inclusive)
1095             * @return the range of matching regions
1096             * @throws SystemException if a system exception occurred
1097             */
1098            public List<Region> findByC_A(long countryId, boolean active, int start,
1099                    int end) throws SystemException {
1100                    return findByC_A(countryId, active, start, end, null);
1101            }
1102    
1103            /**
1104             * Finds an ordered range of all the regions where countryId = &#63; and active = &#63;.
1105             *
1106             * <p>
1107             * 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.
1108             * </p>
1109             *
1110             * @param countryId the country id to search with
1111             * @param active the active to search with
1112             * @param start the lower bound of the range of regions to return
1113             * @param end the upper bound of the range of regions to return (not inclusive)
1114             * @param orderByComparator the comparator to order the results by
1115             * @return the ordered range of matching regions
1116             * @throws SystemException if a system exception occurred
1117             */
1118            public List<Region> findByC_A(long countryId, boolean active, int start,
1119                    int end, OrderByComparator orderByComparator) throws SystemException {
1120                    Object[] finderArgs = new Object[] {
1121                                    countryId, active,
1122                                    
1123                                    String.valueOf(start), String.valueOf(end),
1124                                    String.valueOf(orderByComparator)
1125                            };
1126    
1127                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_A,
1128                                    finderArgs, this);
1129    
1130                    if (list == null) {
1131                            Session session = null;
1132    
1133                            try {
1134                                    session = openSession();
1135    
1136                                    StringBundler query = null;
1137    
1138                                    if (orderByComparator != null) {
1139                                            query = new StringBundler(4 +
1140                                                            (orderByComparator.getOrderByFields().length * 3));
1141                                    }
1142                                    else {
1143                                            query = new StringBundler(4);
1144                                    }
1145    
1146                                    query.append(_SQL_SELECT_REGION_WHERE);
1147    
1148                                    query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1149    
1150                                    query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1151    
1152                                    if (orderByComparator != null) {
1153                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1154                                                    orderByComparator);
1155                                    }
1156    
1157                                    else {
1158                                            query.append(RegionModelImpl.ORDER_BY_JPQL);
1159                                    }
1160    
1161                                    String sql = query.toString();
1162    
1163                                    Query q = session.createQuery(sql);
1164    
1165                                    QueryPos qPos = QueryPos.getInstance(q);
1166    
1167                                    qPos.add(countryId);
1168    
1169                                    qPos.add(active);
1170    
1171                                    list = (List<Region>)QueryUtil.list(q, getDialect(), start, end);
1172                            }
1173                            catch (Exception e) {
1174                                    throw processException(e);
1175                            }
1176                            finally {
1177                                    if (list == null) {
1178                                            list = new ArrayList<Region>();
1179                                    }
1180    
1181                                    cacheResult(list);
1182    
1183                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_A, finderArgs,
1184                                            list);
1185    
1186                                    closeSession(session);
1187                            }
1188                    }
1189    
1190                    return list;
1191            }
1192    
1193            /**
1194             * Finds the first region in the ordered set where countryId = &#63; and active = &#63;.
1195             *
1196             * <p>
1197             * 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.
1198             * </p>
1199             *
1200             * @param countryId the country id to search with
1201             * @param active the active to search with
1202             * @param orderByComparator the comparator to order the set by
1203             * @return the first matching region
1204             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
1205             * @throws SystemException if a system exception occurred
1206             */
1207            public Region findByC_A_First(long countryId, boolean active,
1208                    OrderByComparator orderByComparator)
1209                    throws NoSuchRegionException, SystemException {
1210                    List<Region> list = findByC_A(countryId, active, 0, 1, orderByComparator);
1211    
1212                    if (list.isEmpty()) {
1213                            StringBundler msg = new StringBundler(6);
1214    
1215                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1216    
1217                            msg.append("countryId=");
1218                            msg.append(countryId);
1219    
1220                            msg.append(", active=");
1221                            msg.append(active);
1222    
1223                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1224    
1225                            throw new NoSuchRegionException(msg.toString());
1226                    }
1227                    else {
1228                            return list.get(0);
1229                    }
1230            }
1231    
1232            /**
1233             * Finds the last region in the ordered set where countryId = &#63; and active = &#63;.
1234             *
1235             * <p>
1236             * 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.
1237             * </p>
1238             *
1239             * @param countryId the country id to search with
1240             * @param active the active to search with
1241             * @param orderByComparator the comparator to order the set by
1242             * @return the last matching region
1243             * @throws com.liferay.portal.NoSuchRegionException if a matching region could not be found
1244             * @throws SystemException if a system exception occurred
1245             */
1246            public Region findByC_A_Last(long countryId, boolean active,
1247                    OrderByComparator orderByComparator)
1248                    throws NoSuchRegionException, SystemException {
1249                    int count = countByC_A(countryId, active);
1250    
1251                    List<Region> list = findByC_A(countryId, active, count - 1, count,
1252                                    orderByComparator);
1253    
1254                    if (list.isEmpty()) {
1255                            StringBundler msg = new StringBundler(6);
1256    
1257                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1258    
1259                            msg.append("countryId=");
1260                            msg.append(countryId);
1261    
1262                            msg.append(", active=");
1263                            msg.append(active);
1264    
1265                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1266    
1267                            throw new NoSuchRegionException(msg.toString());
1268                    }
1269                    else {
1270                            return list.get(0);
1271                    }
1272            }
1273    
1274            /**
1275             * Finds the regions before and after the current region in the ordered set where countryId = &#63; and active = &#63;.
1276             *
1277             * <p>
1278             * 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.
1279             * </p>
1280             *
1281             * @param regionId the primary key of the current region
1282             * @param countryId the country id to search with
1283             * @param active the active to search with
1284             * @param orderByComparator the comparator to order the set by
1285             * @return the previous, current, and next region
1286             * @throws com.liferay.portal.NoSuchRegionException if a region with the primary key could not be found
1287             * @throws SystemException if a system exception occurred
1288             */
1289            public Region[] findByC_A_PrevAndNext(long regionId, long countryId,
1290                    boolean active, OrderByComparator orderByComparator)
1291                    throws NoSuchRegionException, SystemException {
1292                    Region region = findByPrimaryKey(regionId);
1293    
1294                    Session session = null;
1295    
1296                    try {
1297                            session = openSession();
1298    
1299                            Region[] array = new RegionImpl[3];
1300    
1301                            array[0] = getByC_A_PrevAndNext(session, region, countryId, active,
1302                                            orderByComparator, true);
1303    
1304                            array[1] = region;
1305    
1306                            array[2] = getByC_A_PrevAndNext(session, region, countryId, active,
1307                                            orderByComparator, false);
1308    
1309                            return array;
1310                    }
1311                    catch (Exception e) {
1312                            throw processException(e);
1313                    }
1314                    finally {
1315                            closeSession(session);
1316                    }
1317            }
1318    
1319            protected Region getByC_A_PrevAndNext(Session session, Region region,
1320                    long countryId, boolean active, OrderByComparator orderByComparator,
1321                    boolean previous) {
1322                    StringBundler query = null;
1323    
1324                    if (orderByComparator != null) {
1325                            query = new StringBundler(6 +
1326                                            (orderByComparator.getOrderByFields().length * 6));
1327                    }
1328                    else {
1329                            query = new StringBundler(3);
1330                    }
1331    
1332                    query.append(_SQL_SELECT_REGION_WHERE);
1333    
1334                    query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1335    
1336                    query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1337    
1338                    if (orderByComparator != null) {
1339                            String[] orderByFields = orderByComparator.getOrderByFields();
1340    
1341                            if (orderByFields.length > 0) {
1342                                    query.append(WHERE_AND);
1343                            }
1344    
1345                            for (int i = 0; i < orderByFields.length; i++) {
1346                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1347                                    query.append(orderByFields[i]);
1348    
1349                                    if ((i + 1) < orderByFields.length) {
1350                                            if (orderByComparator.isAscending() ^ previous) {
1351                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1352                                            }
1353                                            else {
1354                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1355                                            }
1356                                    }
1357                                    else {
1358                                            if (orderByComparator.isAscending() ^ previous) {
1359                                                    query.append(WHERE_GREATER_THAN);
1360                                            }
1361                                            else {
1362                                                    query.append(WHERE_LESSER_THAN);
1363                                            }
1364                                    }
1365                            }
1366    
1367                            query.append(ORDER_BY_CLAUSE);
1368    
1369                            for (int i = 0; i < orderByFields.length; i++) {
1370                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1371                                    query.append(orderByFields[i]);
1372    
1373                                    if ((i + 1) < orderByFields.length) {
1374                                            if (orderByComparator.isAscending() ^ previous) {
1375                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1376                                            }
1377                                            else {
1378                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1379                                            }
1380                                    }
1381                                    else {
1382                                            if (orderByComparator.isAscending() ^ previous) {
1383                                                    query.append(ORDER_BY_ASC);
1384                                            }
1385                                            else {
1386                                                    query.append(ORDER_BY_DESC);
1387                                            }
1388                                    }
1389                            }
1390                    }
1391    
1392                    else {
1393                            query.append(RegionModelImpl.ORDER_BY_JPQL);
1394                    }
1395    
1396                    String sql = query.toString();
1397    
1398                    Query q = session.createQuery(sql);
1399    
1400                    q.setFirstResult(0);
1401                    q.setMaxResults(2);
1402    
1403                    QueryPos qPos = QueryPos.getInstance(q);
1404    
1405                    qPos.add(countryId);
1406    
1407                    qPos.add(active);
1408    
1409                    if (orderByComparator != null) {
1410                            Object[] values = orderByComparator.getOrderByValues(region);
1411    
1412                            for (Object value : values) {
1413                                    qPos.add(value);
1414                            }
1415                    }
1416    
1417                    List<Region> list = q.list();
1418    
1419                    if (list.size() == 2) {
1420                            return list.get(1);
1421                    }
1422                    else {
1423                            return null;
1424                    }
1425            }
1426    
1427            /**
1428             * Finds all the regions.
1429             *
1430             * @return the regions
1431             * @throws SystemException if a system exception occurred
1432             */
1433            public List<Region> findAll() throws SystemException {
1434                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1435            }
1436    
1437            /**
1438             * Finds a range of all the regions.
1439             *
1440             * <p>
1441             * 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.
1442             * </p>
1443             *
1444             * @param start the lower bound of the range of regions to return
1445             * @param end the upper bound of the range of regions to return (not inclusive)
1446             * @return the range of regions
1447             * @throws SystemException if a system exception occurred
1448             */
1449            public List<Region> findAll(int start, int end) throws SystemException {
1450                    return findAll(start, end, null);
1451            }
1452    
1453            /**
1454             * Finds an ordered range of all the regions.
1455             *
1456             * <p>
1457             * 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.
1458             * </p>
1459             *
1460             * @param start the lower bound of the range of regions to return
1461             * @param end the upper bound of the range of regions to return (not inclusive)
1462             * @param orderByComparator the comparator to order the results by
1463             * @return the ordered range of regions
1464             * @throws SystemException if a system exception occurred
1465             */
1466            public List<Region> findAll(int start, int end,
1467                    OrderByComparator orderByComparator) throws SystemException {
1468                    Object[] finderArgs = new Object[] {
1469                                    String.valueOf(start), String.valueOf(end),
1470                                    String.valueOf(orderByComparator)
1471                            };
1472    
1473                    List<Region> list = (List<Region>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1474                                    finderArgs, this);
1475    
1476                    if (list == null) {
1477                            Session session = null;
1478    
1479                            try {
1480                                    session = openSession();
1481    
1482                                    StringBundler query = null;
1483                                    String sql = null;
1484    
1485                                    if (orderByComparator != null) {
1486                                            query = new StringBundler(2 +
1487                                                            (orderByComparator.getOrderByFields().length * 3));
1488    
1489                                            query.append(_SQL_SELECT_REGION);
1490    
1491                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1492                                                    orderByComparator);
1493    
1494                                            sql = query.toString();
1495                                    }
1496                                    else {
1497                                            sql = _SQL_SELECT_REGION.concat(RegionModelImpl.ORDER_BY_JPQL);
1498                                    }
1499    
1500                                    Query q = session.createQuery(sql);
1501    
1502                                    if (orderByComparator == null) {
1503                                            list = (List<Region>)QueryUtil.list(q, getDialect(), start,
1504                                                            end, false);
1505    
1506                                            Collections.sort(list);
1507                                    }
1508                                    else {
1509                                            list = (List<Region>)QueryUtil.list(q, getDialect(), start,
1510                                                            end);
1511                                    }
1512                            }
1513                            catch (Exception e) {
1514                                    throw processException(e);
1515                            }
1516                            finally {
1517                                    if (list == null) {
1518                                            list = new ArrayList<Region>();
1519                                    }
1520    
1521                                    cacheResult(list);
1522    
1523                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1524    
1525                                    closeSession(session);
1526                            }
1527                    }
1528    
1529                    return list;
1530            }
1531    
1532            /**
1533             * Removes all the regions where countryId = &#63; from the database.
1534             *
1535             * @param countryId the country id to search with
1536             * @throws SystemException if a system exception occurred
1537             */
1538            public void removeByCountryId(long countryId) throws SystemException {
1539                    for (Region region : findByCountryId(countryId)) {
1540                            remove(region);
1541                    }
1542            }
1543    
1544            /**
1545             * Removes all the regions where active = &#63; from the database.
1546             *
1547             * @param active the active to search with
1548             * @throws SystemException if a system exception occurred
1549             */
1550            public void removeByActive(boolean active) throws SystemException {
1551                    for (Region region : findByActive(active)) {
1552                            remove(region);
1553                    }
1554            }
1555    
1556            /**
1557             * Removes all the regions where countryId = &#63; and active = &#63; from the database.
1558             *
1559             * @param countryId the country id to search with
1560             * @param active the active to search with
1561             * @throws SystemException if a system exception occurred
1562             */
1563            public void removeByC_A(long countryId, boolean active)
1564                    throws SystemException {
1565                    for (Region region : findByC_A(countryId, active)) {
1566                            remove(region);
1567                    }
1568            }
1569    
1570            /**
1571             * Removes all the regions from the database.
1572             *
1573             * @throws SystemException if a system exception occurred
1574             */
1575            public void removeAll() throws SystemException {
1576                    for (Region region : findAll()) {
1577                            remove(region);
1578                    }
1579            }
1580    
1581            /**
1582             * Counts all the regions where countryId = &#63;.
1583             *
1584             * @param countryId the country id to search with
1585             * @return the number of matching regions
1586             * @throws SystemException if a system exception occurred
1587             */
1588            public int countByCountryId(long countryId) throws SystemException {
1589                    Object[] finderArgs = new Object[] { countryId };
1590    
1591                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COUNTRYID,
1592                                    finderArgs, this);
1593    
1594                    if (count == null) {
1595                            Session session = null;
1596    
1597                            try {
1598                                    session = openSession();
1599    
1600                                    StringBundler query = new StringBundler(2);
1601    
1602                                    query.append(_SQL_COUNT_REGION_WHERE);
1603    
1604                                    query.append(_FINDER_COLUMN_COUNTRYID_COUNTRYID_2);
1605    
1606                                    String sql = query.toString();
1607    
1608                                    Query q = session.createQuery(sql);
1609    
1610                                    QueryPos qPos = QueryPos.getInstance(q);
1611    
1612                                    qPos.add(countryId);
1613    
1614                                    count = (Long)q.uniqueResult();
1615                            }
1616                            catch (Exception e) {
1617                                    throw processException(e);
1618                            }
1619                            finally {
1620                                    if (count == null) {
1621                                            count = Long.valueOf(0);
1622                                    }
1623    
1624                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COUNTRYID,
1625                                            finderArgs, count);
1626    
1627                                    closeSession(session);
1628                            }
1629                    }
1630    
1631                    return count.intValue();
1632            }
1633    
1634            /**
1635             * Counts all the regions where active = &#63;.
1636             *
1637             * @param active the active to search with
1638             * @return the number of matching regions
1639             * @throws SystemException if a system exception occurred
1640             */
1641            public int countByActive(boolean active) throws SystemException {
1642                    Object[] finderArgs = new Object[] { active };
1643    
1644                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_ACTIVE,
1645                                    finderArgs, this);
1646    
1647                    if (count == null) {
1648                            Session session = null;
1649    
1650                            try {
1651                                    session = openSession();
1652    
1653                                    StringBundler query = new StringBundler(2);
1654    
1655                                    query.append(_SQL_COUNT_REGION_WHERE);
1656    
1657                                    query.append(_FINDER_COLUMN_ACTIVE_ACTIVE_2);
1658    
1659                                    String sql = query.toString();
1660    
1661                                    Query q = session.createQuery(sql);
1662    
1663                                    QueryPos qPos = QueryPos.getInstance(q);
1664    
1665                                    qPos.add(active);
1666    
1667                                    count = (Long)q.uniqueResult();
1668                            }
1669                            catch (Exception e) {
1670                                    throw processException(e);
1671                            }
1672                            finally {
1673                                    if (count == null) {
1674                                            count = Long.valueOf(0);
1675                                    }
1676    
1677                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_ACTIVE,
1678                                            finderArgs, count);
1679    
1680                                    closeSession(session);
1681                            }
1682                    }
1683    
1684                    return count.intValue();
1685            }
1686    
1687            /**
1688             * Counts all the regions where countryId = &#63; and active = &#63;.
1689             *
1690             * @param countryId the country id to search with
1691             * @param active the active to search with
1692             * @return the number of matching regions
1693             * @throws SystemException if a system exception occurred
1694             */
1695            public int countByC_A(long countryId, boolean active)
1696                    throws SystemException {
1697                    Object[] finderArgs = new Object[] { countryId, active };
1698    
1699                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_A,
1700                                    finderArgs, this);
1701    
1702                    if (count == null) {
1703                            Session session = null;
1704    
1705                            try {
1706                                    session = openSession();
1707    
1708                                    StringBundler query = new StringBundler(3);
1709    
1710                                    query.append(_SQL_COUNT_REGION_WHERE);
1711    
1712                                    query.append(_FINDER_COLUMN_C_A_COUNTRYID_2);
1713    
1714                                    query.append(_FINDER_COLUMN_C_A_ACTIVE_2);
1715    
1716                                    String sql = query.toString();
1717    
1718                                    Query q = session.createQuery(sql);
1719    
1720                                    QueryPos qPos = QueryPos.getInstance(q);
1721    
1722                                    qPos.add(countryId);
1723    
1724                                    qPos.add(active);
1725    
1726                                    count = (Long)q.uniqueResult();
1727                            }
1728                            catch (Exception e) {
1729                                    throw processException(e);
1730                            }
1731                            finally {
1732                                    if (count == null) {
1733                                            count = Long.valueOf(0);
1734                                    }
1735    
1736                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_A, finderArgs,
1737                                            count);
1738    
1739                                    closeSession(session);
1740                            }
1741                    }
1742    
1743                    return count.intValue();
1744            }
1745    
1746            /**
1747             * Counts all the regions.
1748             *
1749             * @return the number of regions
1750             * @throws SystemException if a system exception occurred
1751             */
1752            public int countAll() throws SystemException {
1753                    Object[] finderArgs = new Object[0];
1754    
1755                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1756                                    finderArgs, this);
1757    
1758                    if (count == null) {
1759                            Session session = null;
1760    
1761                            try {
1762                                    session = openSession();
1763    
1764                                    Query q = session.createQuery(_SQL_COUNT_REGION);
1765    
1766                                    count = (Long)q.uniqueResult();
1767                            }
1768                            catch (Exception e) {
1769                                    throw processException(e);
1770                            }
1771                            finally {
1772                                    if (count == null) {
1773                                            count = Long.valueOf(0);
1774                                    }
1775    
1776                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1777                                            count);
1778    
1779                                    closeSession(session);
1780                            }
1781                    }
1782    
1783                    return count.intValue();
1784            }
1785    
1786            /**
1787             * Initializes the region persistence.
1788             */
1789            public void afterPropertiesSet() {
1790                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1791                                            com.liferay.portal.util.PropsUtil.get(
1792                                                    "value.object.listener.com.liferay.portal.model.Region")));
1793    
1794                    if (listenerClassNames.length > 0) {
1795                            try {
1796                                    List<ModelListener<Region>> listenersList = new ArrayList<ModelListener<Region>>();
1797    
1798                                    for (String listenerClassName : listenerClassNames) {
1799                                            listenersList.add((ModelListener<Region>)InstanceFactory.newInstance(
1800                                                            listenerClassName));
1801                                    }
1802    
1803                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1804                            }
1805                            catch (Exception e) {
1806                                    _log.error(e);
1807                            }
1808                    }
1809            }
1810    
1811            @BeanReference(type = AccountPersistence.class)
1812            protected AccountPersistence accountPersistence;
1813            @BeanReference(type = AddressPersistence.class)
1814            protected AddressPersistence addressPersistence;
1815            @BeanReference(type = BrowserTrackerPersistence.class)
1816            protected BrowserTrackerPersistence browserTrackerPersistence;
1817            @BeanReference(type = ClassNamePersistence.class)
1818            protected ClassNamePersistence classNamePersistence;
1819            @BeanReference(type = ClusterGroupPersistence.class)
1820            protected ClusterGroupPersistence clusterGroupPersistence;
1821            @BeanReference(type = CompanyPersistence.class)
1822            protected CompanyPersistence companyPersistence;
1823            @BeanReference(type = ContactPersistence.class)
1824            protected ContactPersistence contactPersistence;
1825            @BeanReference(type = CountryPersistence.class)
1826            protected CountryPersistence countryPersistence;
1827            @BeanReference(type = EmailAddressPersistence.class)
1828            protected EmailAddressPersistence emailAddressPersistence;
1829            @BeanReference(type = GroupPersistence.class)
1830            protected GroupPersistence groupPersistence;
1831            @BeanReference(type = ImagePersistence.class)
1832            protected ImagePersistence imagePersistence;
1833            @BeanReference(type = LayoutPersistence.class)
1834            protected LayoutPersistence layoutPersistence;
1835            @BeanReference(type = LayoutPrototypePersistence.class)
1836            protected LayoutPrototypePersistence layoutPrototypePersistence;
1837            @BeanReference(type = LayoutSetPersistence.class)
1838            protected LayoutSetPersistence layoutSetPersistence;
1839            @BeanReference(type = LayoutSetPrototypePersistence.class)
1840            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1841            @BeanReference(type = ListTypePersistence.class)
1842            protected ListTypePersistence listTypePersistence;
1843            @BeanReference(type = LockPersistence.class)
1844            protected LockPersistence lockPersistence;
1845            @BeanReference(type = MembershipRequestPersistence.class)
1846            protected MembershipRequestPersistence membershipRequestPersistence;
1847            @BeanReference(type = OrganizationPersistence.class)
1848            protected OrganizationPersistence organizationPersistence;
1849            @BeanReference(type = OrgGroupPermissionPersistence.class)
1850            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1851            @BeanReference(type = OrgGroupRolePersistence.class)
1852            protected OrgGroupRolePersistence orgGroupRolePersistence;
1853            @BeanReference(type = OrgLaborPersistence.class)
1854            protected OrgLaborPersistence orgLaborPersistence;
1855            @BeanReference(type = PasswordPolicyPersistence.class)
1856            protected PasswordPolicyPersistence passwordPolicyPersistence;
1857            @BeanReference(type = PasswordPolicyRelPersistence.class)
1858            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1859            @BeanReference(type = PasswordTrackerPersistence.class)
1860            protected PasswordTrackerPersistence passwordTrackerPersistence;
1861            @BeanReference(type = PermissionPersistence.class)
1862            protected PermissionPersistence permissionPersistence;
1863            @BeanReference(type = PhonePersistence.class)
1864            protected PhonePersistence phonePersistence;
1865            @BeanReference(type = PluginSettingPersistence.class)
1866            protected PluginSettingPersistence pluginSettingPersistence;
1867            @BeanReference(type = PortletPersistence.class)
1868            protected PortletPersistence portletPersistence;
1869            @BeanReference(type = PortletItemPersistence.class)
1870            protected PortletItemPersistence portletItemPersistence;
1871            @BeanReference(type = PortletPreferencesPersistence.class)
1872            protected PortletPreferencesPersistence portletPreferencesPersistence;
1873            @BeanReference(type = RegionPersistence.class)
1874            protected RegionPersistence regionPersistence;
1875            @BeanReference(type = ReleasePersistence.class)
1876            protected ReleasePersistence releasePersistence;
1877            @BeanReference(type = ResourcePersistence.class)
1878            protected ResourcePersistence resourcePersistence;
1879            @BeanReference(type = ResourceActionPersistence.class)
1880            protected ResourceActionPersistence resourceActionPersistence;
1881            @BeanReference(type = ResourceCodePersistence.class)
1882            protected ResourceCodePersistence resourceCodePersistence;
1883            @BeanReference(type = ResourcePermissionPersistence.class)
1884            protected ResourcePermissionPersistence resourcePermissionPersistence;
1885            @BeanReference(type = RolePersistence.class)
1886            protected RolePersistence rolePersistence;
1887            @BeanReference(type = ServiceComponentPersistence.class)
1888            protected ServiceComponentPersistence serviceComponentPersistence;
1889            @BeanReference(type = ShardPersistence.class)
1890            protected ShardPersistence shardPersistence;
1891            @BeanReference(type = SubscriptionPersistence.class)
1892            protected SubscriptionPersistence subscriptionPersistence;
1893            @BeanReference(type = TicketPersistence.class)
1894            protected TicketPersistence ticketPersistence;
1895            @BeanReference(type = TeamPersistence.class)
1896            protected TeamPersistence teamPersistence;
1897            @BeanReference(type = UserPersistence.class)
1898            protected UserPersistence userPersistence;
1899            @BeanReference(type = UserGroupPersistence.class)
1900            protected UserGroupPersistence userGroupPersistence;
1901            @BeanReference(type = UserGroupGroupRolePersistence.class)
1902            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1903            @BeanReference(type = UserGroupRolePersistence.class)
1904            protected UserGroupRolePersistence userGroupRolePersistence;
1905            @BeanReference(type = UserIdMapperPersistence.class)
1906            protected UserIdMapperPersistence userIdMapperPersistence;
1907            @BeanReference(type = UserTrackerPersistence.class)
1908            protected UserTrackerPersistence userTrackerPersistence;
1909            @BeanReference(type = UserTrackerPathPersistence.class)
1910            protected UserTrackerPathPersistence userTrackerPathPersistence;
1911            @BeanReference(type = WebDAVPropsPersistence.class)
1912            protected WebDAVPropsPersistence webDAVPropsPersistence;
1913            @BeanReference(type = WebsitePersistence.class)
1914            protected WebsitePersistence websitePersistence;
1915            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1916            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1917            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1918            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1919            private static final String _SQL_SELECT_REGION = "SELECT region FROM Region region";
1920            private static final String _SQL_SELECT_REGION_WHERE = "SELECT region FROM Region region WHERE ";
1921            private static final String _SQL_COUNT_REGION = "SELECT COUNT(region) FROM Region region";
1922            private static final String _SQL_COUNT_REGION_WHERE = "SELECT COUNT(region) FROM Region region WHERE ";
1923            private static final String _FINDER_COLUMN_COUNTRYID_COUNTRYID_2 = "region.countryId = ?";
1924            private static final String _FINDER_COLUMN_ACTIVE_ACTIVE_2 = "region.active = ?";
1925            private static final String _FINDER_COLUMN_C_A_COUNTRYID_2 = "region.countryId = ? AND ";
1926            private static final String _FINDER_COLUMN_C_A_ACTIVE_2 = "region.active = ?";
1927            private static final String _ORDER_BY_ENTITY_ALIAS = "region.";
1928            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Region exists with the primary key ";
1929            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Region exists with the key {";
1930            private static Log _log = LogFactoryUtil.getLog(RegionPersistenceImpl.class);
1931    }