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