1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchCountryException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.Country;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.model.impl.CountryImpl;
41  import com.liferay.portal.model.impl.CountryModelImpl;
42  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.Iterator;
50  import java.util.List;
51  
52  /**
53   * <a href="CountryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class CountryPersistenceImpl extends BasePersistenceImpl
59      implements CountryPersistence {
60      public Country create(long countryId) {
61          Country country = new CountryImpl();
62  
63          country.setNew(true);
64          country.setPrimaryKey(countryId);
65  
66          return country;
67      }
68  
69      public Country remove(long countryId)
70          throws NoSuchCountryException, SystemException {
71          Session session = null;
72  
73          try {
74              session = openSession();
75  
76              Country country = (Country)session.get(CountryImpl.class,
77                      new Long(countryId));
78  
79              if (country == null) {
80                  if (_log.isWarnEnabled()) {
81                      _log.warn("No Country exists with the primary key " +
82                          countryId);
83                  }
84  
85                  throw new NoSuchCountryException(
86                      "No Country exists with the primary key " + countryId);
87              }
88  
89              return remove(country);
90          }
91          catch (NoSuchCountryException nsee) {
92              throw nsee;
93          }
94          catch (Exception e) {
95              throw processException(e);
96          }
97          finally {
98              closeSession(session);
99          }
100     }
101 
102     public Country remove(Country country) throws SystemException {
103         if (_listeners.length > 0) {
104             for (ModelListener listener : _listeners) {
105                 listener.onBeforeRemove(country);
106             }
107         }
108 
109         country = removeImpl(country);
110 
111         if (_listeners.length > 0) {
112             for (ModelListener listener : _listeners) {
113                 listener.onAfterRemove(country);
114             }
115         }
116 
117         return country;
118     }
119 
120     protected Country removeImpl(Country country) throws SystemException {
121         Session session = null;
122 
123         try {
124             session = openSession();
125 
126             session.delete(country);
127 
128             session.flush();
129 
130             return country;
131         }
132         catch (Exception e) {
133             throw processException(e);
134         }
135         finally {
136             closeSession(session);
137 
138             FinderCacheUtil.clearCache(Country.class.getName());
139         }
140     }
141 
142     /**
143      * @deprecated Use <code>update(Country country, boolean merge)</code>.
144      */
145     public Country update(Country country) throws SystemException {
146         if (_log.isWarnEnabled()) {
147             _log.warn(
148                 "Using the deprecated update(Country country) method. Use update(Country country, boolean merge) instead.");
149         }
150 
151         return update(country, false);
152     }
153 
154     /**
155      * Add, update, or merge, the entity. This method also calls the model
156      * listeners to trigger the proper events associated with adding, deleting,
157      * or updating an entity.
158      *
159      * @param        country the entity to add, update, or merge
160      * @param        merge boolean value for whether to merge the entity. The
161      *                default value is false. Setting merge to true is more
162      *                expensive and should only be true when country is
163      *                transient. See LEP-5473 for a detailed discussion of this
164      *                method.
165      * @return        true if the portlet can be displayed via Ajax
166      */
167     public Country update(Country country, boolean merge)
168         throws SystemException {
169         boolean isNew = country.isNew();
170 
171         if (_listeners.length > 0) {
172             for (ModelListener listener : _listeners) {
173                 if (isNew) {
174                     listener.onBeforeCreate(country);
175                 }
176                 else {
177                     listener.onBeforeUpdate(country);
178                 }
179             }
180         }
181 
182         country = updateImpl(country, merge);
183 
184         if (_listeners.length > 0) {
185             for (ModelListener listener : _listeners) {
186                 if (isNew) {
187                     listener.onAfterCreate(country);
188                 }
189                 else {
190                     listener.onAfterUpdate(country);
191                 }
192             }
193         }
194 
195         return country;
196     }
197 
198     public Country updateImpl(com.liferay.portal.model.Country country,
199         boolean merge) throws SystemException {
200         Session session = null;
201 
202         try {
203             session = openSession();
204 
205             if (merge) {
206                 session.merge(country);
207             }
208             else {
209                 if (country.isNew()) {
210                     session.save(country);
211                 }
212             }
213 
214             session.flush();
215 
216             country.setNew(false);
217 
218             return country;
219         }
220         catch (Exception e) {
221             throw processException(e);
222         }
223         finally {
224             closeSession(session);
225 
226             FinderCacheUtil.clearCache(Country.class.getName());
227         }
228     }
229 
230     public Country findByPrimaryKey(long countryId)
231         throws NoSuchCountryException, SystemException {
232         Country country = fetchByPrimaryKey(countryId);
233 
234         if (country == null) {
235             if (_log.isWarnEnabled()) {
236                 _log.warn("No Country exists with the primary key " +
237                     countryId);
238             }
239 
240             throw new NoSuchCountryException(
241                 "No Country exists with the primary key " + countryId);
242         }
243 
244         return country;
245     }
246 
247     public Country fetchByPrimaryKey(long countryId) throws SystemException {
248         Session session = null;
249 
250         try {
251             session = openSession();
252 
253             return (Country)session.get(CountryImpl.class, new Long(countryId));
254         }
255         catch (Exception e) {
256             throw processException(e);
257         }
258         finally {
259             closeSession(session);
260         }
261     }
262 
263     public Country findByName(String name)
264         throws NoSuchCountryException, SystemException {
265         Country country = fetchByName(name);
266 
267         if (country == null) {
268             StringBuilder msg = new StringBuilder();
269 
270             msg.append("No Country exists with the key {");
271 
272             msg.append("name=" + name);
273 
274             msg.append(StringPool.CLOSE_CURLY_BRACE);
275 
276             if (_log.isWarnEnabled()) {
277                 _log.warn(msg.toString());
278             }
279 
280             throw new NoSuchCountryException(msg.toString());
281         }
282 
283         return country;
284     }
285 
286     public Country fetchByName(String name) throws SystemException {
287         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
288         String finderClassName = Country.class.getName();
289         String finderMethodName = "fetchByName";
290         String[] finderParams = new String[] { String.class.getName() };
291         Object[] finderArgs = new Object[] { name };
292 
293         Object result = null;
294 
295         if (finderClassNameCacheEnabled) {
296             result = FinderCacheUtil.getResult(finderClassName,
297                     finderMethodName, finderParams, finderArgs, this);
298         }
299 
300         if (result == null) {
301             Session session = null;
302 
303             try {
304                 session = openSession();
305 
306                 StringBuilder query = new StringBuilder();
307 
308                 query.append("FROM com.liferay.portal.model.Country WHERE ");
309 
310                 if (name == null) {
311                     query.append("name IS NULL");
312                 }
313                 else {
314                     query.append("name = ?");
315                 }
316 
317                 query.append(" ");
318 
319                 query.append("ORDER BY ");
320 
321                 query.append("name ASC");
322 
323                 Query q = session.createQuery(query.toString());
324 
325                 QueryPos qPos = QueryPos.getInstance(q);
326 
327                 if (name != null) {
328                     qPos.add(name);
329                 }
330 
331                 List<Country> list = q.list();
332 
333                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
334                     finderClassName, finderMethodName, finderParams,
335                     finderArgs, list);
336 
337                 if (list.size() == 0) {
338                     return null;
339                 }
340                 else {
341                     return list.get(0);
342                 }
343             }
344             catch (Exception e) {
345                 throw processException(e);
346             }
347             finally {
348                 closeSession(session);
349             }
350         }
351         else {
352             List<Country> list = (List<Country>)result;
353 
354             if (list.size() == 0) {
355                 return null;
356             }
357             else {
358                 return list.get(0);
359             }
360         }
361     }
362 
363     public Country findByA2(String a2)
364         throws NoSuchCountryException, SystemException {
365         Country country = fetchByA2(a2);
366 
367         if (country == null) {
368             StringBuilder msg = new StringBuilder();
369 
370             msg.append("No Country exists with the key {");
371 
372             msg.append("a2=" + a2);
373 
374             msg.append(StringPool.CLOSE_CURLY_BRACE);
375 
376             if (_log.isWarnEnabled()) {
377                 _log.warn(msg.toString());
378             }
379 
380             throw new NoSuchCountryException(msg.toString());
381         }
382 
383         return country;
384     }
385 
386     public Country fetchByA2(String a2) throws SystemException {
387         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
388         String finderClassName = Country.class.getName();
389         String finderMethodName = "fetchByA2";
390         String[] finderParams = new String[] { String.class.getName() };
391         Object[] finderArgs = new Object[] { a2 };
392 
393         Object result = null;
394 
395         if (finderClassNameCacheEnabled) {
396             result = FinderCacheUtil.getResult(finderClassName,
397                     finderMethodName, finderParams, finderArgs, this);
398         }
399 
400         if (result == null) {
401             Session session = null;
402 
403             try {
404                 session = openSession();
405 
406                 StringBuilder query = new StringBuilder();
407 
408                 query.append("FROM com.liferay.portal.model.Country WHERE ");
409 
410                 if (a2 == null) {
411                     query.append("a2 IS NULL");
412                 }
413                 else {
414                     query.append("a2 = ?");
415                 }
416 
417                 query.append(" ");
418 
419                 query.append("ORDER BY ");
420 
421                 query.append("name ASC");
422 
423                 Query q = session.createQuery(query.toString());
424 
425                 QueryPos qPos = QueryPos.getInstance(q);
426 
427                 if (a2 != null) {
428                     qPos.add(a2);
429                 }
430 
431                 List<Country> list = q.list();
432 
433                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
434                     finderClassName, finderMethodName, finderParams,
435                     finderArgs, list);
436 
437                 if (list.size() == 0) {
438                     return null;
439                 }
440                 else {
441                     return list.get(0);
442                 }
443             }
444             catch (Exception e) {
445                 throw processException(e);
446             }
447             finally {
448                 closeSession(session);
449             }
450         }
451         else {
452             List<Country> list = (List<Country>)result;
453 
454             if (list.size() == 0) {
455                 return null;
456             }
457             else {
458                 return list.get(0);
459             }
460         }
461     }
462 
463     public Country findByA3(String a3)
464         throws NoSuchCountryException, SystemException {
465         Country country = fetchByA3(a3);
466 
467         if (country == null) {
468             StringBuilder msg = new StringBuilder();
469 
470             msg.append("No Country exists with the key {");
471 
472             msg.append("a3=" + a3);
473 
474             msg.append(StringPool.CLOSE_CURLY_BRACE);
475 
476             if (_log.isWarnEnabled()) {
477                 _log.warn(msg.toString());
478             }
479 
480             throw new NoSuchCountryException(msg.toString());
481         }
482 
483         return country;
484     }
485 
486     public Country fetchByA3(String a3) throws SystemException {
487         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
488         String finderClassName = Country.class.getName();
489         String finderMethodName = "fetchByA3";
490         String[] finderParams = new String[] { String.class.getName() };
491         Object[] finderArgs = new Object[] { a3 };
492 
493         Object result = null;
494 
495         if (finderClassNameCacheEnabled) {
496             result = FinderCacheUtil.getResult(finderClassName,
497                     finderMethodName, finderParams, finderArgs, this);
498         }
499 
500         if (result == null) {
501             Session session = null;
502 
503             try {
504                 session = openSession();
505 
506                 StringBuilder query = new StringBuilder();
507 
508                 query.append("FROM com.liferay.portal.model.Country WHERE ");
509 
510                 if (a3 == null) {
511                     query.append("a3 IS NULL");
512                 }
513                 else {
514                     query.append("a3 = ?");
515                 }
516 
517                 query.append(" ");
518 
519                 query.append("ORDER BY ");
520 
521                 query.append("name ASC");
522 
523                 Query q = session.createQuery(query.toString());
524 
525                 QueryPos qPos = QueryPos.getInstance(q);
526 
527                 if (a3 != null) {
528                     qPos.add(a3);
529                 }
530 
531                 List<Country> list = q.list();
532 
533                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
534                     finderClassName, finderMethodName, finderParams,
535                     finderArgs, list);
536 
537                 if (list.size() == 0) {
538                     return null;
539                 }
540                 else {
541                     return list.get(0);
542                 }
543             }
544             catch (Exception e) {
545                 throw processException(e);
546             }
547             finally {
548                 closeSession(session);
549             }
550         }
551         else {
552             List<Country> list = (List<Country>)result;
553 
554             if (list.size() == 0) {
555                 return null;
556             }
557             else {
558                 return list.get(0);
559             }
560         }
561     }
562 
563     public List<Country> findByActive(boolean active) throws SystemException {
564         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
565         String finderClassName = Country.class.getName();
566         String finderMethodName = "findByActive";
567         String[] finderParams = new String[] { Boolean.class.getName() };
568         Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
569 
570         Object result = null;
571 
572         if (finderClassNameCacheEnabled) {
573             result = FinderCacheUtil.getResult(finderClassName,
574                     finderMethodName, finderParams, finderArgs, this);
575         }
576 
577         if (result == null) {
578             Session session = null;
579 
580             try {
581                 session = openSession();
582 
583                 StringBuilder query = new StringBuilder();
584 
585                 query.append("FROM com.liferay.portal.model.Country WHERE ");
586 
587                 query.append("active_ = ?");
588 
589                 query.append(" ");
590 
591                 query.append("ORDER BY ");
592 
593                 query.append("name ASC");
594 
595                 Query q = session.createQuery(query.toString());
596 
597                 QueryPos qPos = QueryPos.getInstance(q);
598 
599                 qPos.add(active);
600 
601                 List<Country> list = q.list();
602 
603                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
604                     finderClassName, finderMethodName, finderParams,
605                     finderArgs, list);
606 
607                 return list;
608             }
609             catch (Exception e) {
610                 throw processException(e);
611             }
612             finally {
613                 closeSession(session);
614             }
615         }
616         else {
617             return (List<Country>)result;
618         }
619     }
620 
621     public List<Country> findByActive(boolean active, int start, int end)
622         throws SystemException {
623         return findByActive(active, start, end, null);
624     }
625 
626     public List<Country> findByActive(boolean active, int start, int end,
627         OrderByComparator obc) throws SystemException {
628         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
629         String finderClassName = Country.class.getName();
630         String finderMethodName = "findByActive";
631         String[] finderParams = new String[] {
632                 Boolean.class.getName(),
633                 
634                 "java.lang.Integer", "java.lang.Integer",
635                 "com.liferay.portal.kernel.util.OrderByComparator"
636             };
637         Object[] finderArgs = new Object[] {
638                 Boolean.valueOf(active),
639                 
640                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
641             };
642 
643         Object result = null;
644 
645         if (finderClassNameCacheEnabled) {
646             result = FinderCacheUtil.getResult(finderClassName,
647                     finderMethodName, finderParams, finderArgs, this);
648         }
649 
650         if (result == null) {
651             Session session = null;
652 
653             try {
654                 session = openSession();
655 
656                 StringBuilder query = new StringBuilder();
657 
658                 query.append("FROM com.liferay.portal.model.Country WHERE ");
659 
660                 query.append("active_ = ?");
661 
662                 query.append(" ");
663 
664                 if (obc != null) {
665                     query.append("ORDER BY ");
666                     query.append(obc.getOrderBy());
667                 }
668 
669                 else {
670                     query.append("ORDER BY ");
671 
672                     query.append("name ASC");
673                 }
674 
675                 Query q = session.createQuery(query.toString());
676 
677                 QueryPos qPos = QueryPos.getInstance(q);
678 
679                 qPos.add(active);
680 
681                 List<Country> list = (List<Country>)QueryUtil.list(q,
682                         getDialect(), start, end);
683 
684                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
685                     finderClassName, finderMethodName, finderParams,
686                     finderArgs, list);
687 
688                 return list;
689             }
690             catch (Exception e) {
691                 throw processException(e);
692             }
693             finally {
694                 closeSession(session);
695             }
696         }
697         else {
698             return (List<Country>)result;
699         }
700     }
701 
702     public Country findByActive_First(boolean active, OrderByComparator obc)
703         throws NoSuchCountryException, SystemException {
704         List<Country> list = findByActive(active, 0, 1, obc);
705 
706         if (list.size() == 0) {
707             StringBuilder msg = new StringBuilder();
708 
709             msg.append("No Country exists with the key {");
710 
711             msg.append("active=" + active);
712 
713             msg.append(StringPool.CLOSE_CURLY_BRACE);
714 
715             throw new NoSuchCountryException(msg.toString());
716         }
717         else {
718             return list.get(0);
719         }
720     }
721 
722     public Country findByActive_Last(boolean active, OrderByComparator obc)
723         throws NoSuchCountryException, SystemException {
724         int count = countByActive(active);
725 
726         List<Country> list = findByActive(active, count - 1, count, obc);
727 
728         if (list.size() == 0) {
729             StringBuilder msg = new StringBuilder();
730 
731             msg.append("No Country exists with the key {");
732 
733             msg.append("active=" + active);
734 
735             msg.append(StringPool.CLOSE_CURLY_BRACE);
736 
737             throw new NoSuchCountryException(msg.toString());
738         }
739         else {
740             return list.get(0);
741         }
742     }
743 
744     public Country[] findByActive_PrevAndNext(long countryId, boolean active,
745         OrderByComparator obc) throws NoSuchCountryException, SystemException {
746         Country country = findByPrimaryKey(countryId);
747 
748         int count = countByActive(active);
749 
750         Session session = null;
751 
752         try {
753             session = openSession();
754 
755             StringBuilder query = new StringBuilder();
756 
757             query.append("FROM com.liferay.portal.model.Country WHERE ");
758 
759             query.append("active_ = ?");
760 
761             query.append(" ");
762 
763             if (obc != null) {
764                 query.append("ORDER BY ");
765                 query.append(obc.getOrderBy());
766             }
767 
768             else {
769                 query.append("ORDER BY ");
770 
771                 query.append("name ASC");
772             }
773 
774             Query q = session.createQuery(query.toString());
775 
776             QueryPos qPos = QueryPos.getInstance(q);
777 
778             qPos.add(active);
779 
780             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, country);
781 
782             Country[] array = new CountryImpl[3];
783 
784             array[0] = (Country)objArray[0];
785             array[1] = (Country)objArray[1];
786             array[2] = (Country)objArray[2];
787 
788             return array;
789         }
790         catch (Exception e) {
791             throw processException(e);
792         }
793         finally {
794             closeSession(session);
795         }
796     }
797 
798     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
799         throws SystemException {
800         Session session = null;
801 
802         try {
803             session = openSession();
804 
805             dynamicQuery.compile(session);
806 
807             return dynamicQuery.list();
808         }
809         catch (Exception e) {
810             throw processException(e);
811         }
812         finally {
813             closeSession(session);
814         }
815     }
816 
817     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
818         int start, int end) throws SystemException {
819         Session session = null;
820 
821         try {
822             session = openSession();
823 
824             dynamicQuery.setLimit(start, end);
825 
826             dynamicQuery.compile(session);
827 
828             return dynamicQuery.list();
829         }
830         catch (Exception e) {
831             throw processException(e);
832         }
833         finally {
834             closeSession(session);
835         }
836     }
837 
838     public List<Country> findAll() throws SystemException {
839         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
840     }
841 
842     public List<Country> findAll(int start, int end) throws SystemException {
843         return findAll(start, end, null);
844     }
845 
846     public List<Country> findAll(int start, int end, OrderByComparator obc)
847         throws SystemException {
848         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
849         String finderClassName = Country.class.getName();
850         String finderMethodName = "findAll";
851         String[] finderParams = new String[] {
852                 "java.lang.Integer", "java.lang.Integer",
853                 "com.liferay.portal.kernel.util.OrderByComparator"
854             };
855         Object[] finderArgs = new Object[] {
856                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
857             };
858 
859         Object result = null;
860 
861         if (finderClassNameCacheEnabled) {
862             result = FinderCacheUtil.getResult(finderClassName,
863                     finderMethodName, finderParams, finderArgs, this);
864         }
865 
866         if (result == null) {
867             Session session = null;
868 
869             try {
870                 session = openSession();
871 
872                 StringBuilder query = new StringBuilder();
873 
874                 query.append("FROM com.liferay.portal.model.Country ");
875 
876                 if (obc != null) {
877                     query.append("ORDER BY ");
878                     query.append(obc.getOrderBy());
879                 }
880 
881                 else {
882                     query.append("ORDER BY ");
883 
884                     query.append("name ASC");
885                 }
886 
887                 Query q = session.createQuery(query.toString());
888 
889                 List<Country> list = (List<Country>)QueryUtil.list(q,
890                         getDialect(), start, end);
891 
892                 if (obc == null) {
893                     Collections.sort(list);
894                 }
895 
896                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
897                     finderClassName, finderMethodName, finderParams,
898                     finderArgs, list);
899 
900                 return list;
901             }
902             catch (Exception e) {
903                 throw processException(e);
904             }
905             finally {
906                 closeSession(session);
907             }
908         }
909         else {
910             return (List<Country>)result;
911         }
912     }
913 
914     public void removeByName(String name)
915         throws NoSuchCountryException, SystemException {
916         Country country = findByName(name);
917 
918         remove(country);
919     }
920 
921     public void removeByA2(String a2)
922         throws NoSuchCountryException, SystemException {
923         Country country = findByA2(a2);
924 
925         remove(country);
926     }
927 
928     public void removeByA3(String a3)
929         throws NoSuchCountryException, SystemException {
930         Country country = findByA3(a3);
931 
932         remove(country);
933     }
934 
935     public void removeByActive(boolean active) throws SystemException {
936         for (Country country : findByActive(active)) {
937             remove(country);
938         }
939     }
940 
941     public void removeAll() throws SystemException {
942         for (Country country : findAll()) {
943             remove(country);
944         }
945     }
946 
947     public int countByName(String name) throws SystemException {
948         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
949         String finderClassName = Country.class.getName();
950         String finderMethodName = "countByName";
951         String[] finderParams = new String[] { String.class.getName() };
952         Object[] finderArgs = new Object[] { name };
953 
954         Object result = null;
955 
956         if (finderClassNameCacheEnabled) {
957             result = FinderCacheUtil.getResult(finderClassName,
958                     finderMethodName, finderParams, finderArgs, this);
959         }
960 
961         if (result == null) {
962             Session session = null;
963 
964             try {
965                 session = openSession();
966 
967                 StringBuilder query = new StringBuilder();
968 
969                 query.append("SELECT COUNT(*) ");
970                 query.append("FROM com.liferay.portal.model.Country WHERE ");
971 
972                 if (name == null) {
973                     query.append("name IS NULL");
974                 }
975                 else {
976                     query.append("name = ?");
977                 }
978 
979                 query.append(" ");
980 
981                 Query q = session.createQuery(query.toString());
982 
983                 QueryPos qPos = QueryPos.getInstance(q);
984 
985                 if (name != null) {
986                     qPos.add(name);
987                 }
988 
989                 Long count = null;
990 
991                 Iterator<Long> itr = q.list().iterator();
992 
993                 if (itr.hasNext()) {
994                     count = itr.next();
995                 }
996 
997                 if (count == null) {
998                     count = new Long(0);
999                 }
1000
1001                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1002                    finderClassName, finderMethodName, finderParams,
1003                    finderArgs, count);
1004
1005                return count.intValue();
1006            }
1007            catch (Exception e) {
1008                throw processException(e);
1009            }
1010            finally {
1011                closeSession(session);
1012            }
1013        }
1014        else {
1015            return ((Long)result).intValue();
1016        }
1017    }
1018
1019    public int countByA2(String a2) throws SystemException {
1020        boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
1021        String finderClassName = Country.class.getName();
1022        String finderMethodName = "countByA2";
1023        String[] finderParams = new String[] { String.class.getName() };
1024        Object[] finderArgs = new Object[] { a2 };
1025
1026        Object result = null;
1027
1028        if (finderClassNameCacheEnabled) {
1029            result = FinderCacheUtil.getResult(finderClassName,
1030                    finderMethodName, finderParams, finderArgs, this);
1031        }
1032
1033        if (result == null) {
1034            Session session = null;
1035
1036            try {
1037                session = openSession();
1038
1039                StringBuilder query = new StringBuilder();
1040
1041                query.append("SELECT COUNT(*) ");
1042                query.append("FROM com.liferay.portal.model.Country WHERE ");
1043
1044                if (a2 == null) {
1045                    query.append("a2 IS NULL");
1046                }
1047                else {
1048                    query.append("a2 = ?");
1049                }
1050
1051                query.append(" ");
1052
1053                Query q = session.createQuery(query.toString());
1054
1055                QueryPos qPos = QueryPos.getInstance(q);
1056
1057                if (a2 != null) {
1058                    qPos.add(a2);
1059                }
1060
1061                Long count = null;
1062
1063                Iterator<Long> itr = q.list().iterator();
1064
1065                if (itr.hasNext()) {
1066                    count = itr.next();
1067                }
1068
1069                if (count == null) {
1070                    count = new Long(0);
1071                }
1072
1073                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1074                    finderClassName, finderMethodName, finderParams,
1075                    finderArgs, count);
1076
1077                return count.intValue();
1078            }
1079            catch (Exception e) {
1080                throw processException(e);
1081            }
1082            finally {
1083                closeSession(session);
1084            }
1085        }
1086        else {
1087            return ((Long)result).intValue();
1088        }
1089    }
1090
1091    public int countByA3(String a3) throws SystemException {
1092        boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
1093        String finderClassName = Country.class.getName();
1094        String finderMethodName = "countByA3";
1095        String[] finderParams = new String[] { String.class.getName() };
1096        Object[] finderArgs = new Object[] { a3 };
1097
1098        Object result = null;
1099
1100        if (finderClassNameCacheEnabled) {
1101            result = FinderCacheUtil.getResult(finderClassName,
1102                    finderMethodName, finderParams, finderArgs, this);
1103        }
1104
1105        if (result == null) {
1106            Session session = null;
1107
1108            try {
1109                session = openSession();
1110
1111                StringBuilder query = new StringBuilder();
1112
1113                query.append("SELECT COUNT(*) ");
1114                query.append("FROM com.liferay.portal.model.Country WHERE ");
1115
1116                if (a3 == null) {
1117                    query.append("a3 IS NULL");
1118                }
1119                else {
1120                    query.append("a3 = ?");
1121                }
1122
1123                query.append(" ");
1124
1125                Query q = session.createQuery(query.toString());
1126
1127                QueryPos qPos = QueryPos.getInstance(q);
1128
1129                if (a3 != null) {
1130                    qPos.add(a3);
1131                }
1132
1133                Long count = null;
1134
1135                Iterator<Long> itr = q.list().iterator();
1136
1137                if (itr.hasNext()) {
1138                    count = itr.next();
1139                }
1140
1141                if (count == null) {
1142                    count = new Long(0);
1143                }
1144
1145                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1146                    finderClassName, finderMethodName, finderParams,
1147                    finderArgs, count);
1148
1149                return count.intValue();
1150            }
1151            catch (Exception e) {
1152                throw processException(e);
1153            }
1154            finally {
1155                closeSession(session);
1156            }
1157        }
1158        else {
1159            return ((Long)result).intValue();
1160        }
1161    }
1162
1163    public int countByActive(boolean active) throws SystemException {
1164        boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
1165        String finderClassName = Country.class.getName();
1166        String finderMethodName = "countByActive";
1167        String[] finderParams = new String[] { Boolean.class.getName() };
1168        Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
1169
1170        Object result = null;
1171
1172        if (finderClassNameCacheEnabled) {
1173            result = FinderCacheUtil.getResult(finderClassName,
1174                    finderMethodName, finderParams, finderArgs, this);
1175        }
1176
1177        if (result == null) {
1178            Session session = null;
1179
1180            try {
1181                session = openSession();
1182
1183                StringBuilder query = new StringBuilder();
1184
1185                query.append("SELECT COUNT(*) ");
1186                query.append("FROM com.liferay.portal.model.Country WHERE ");
1187
1188                query.append("active_ = ?");
1189
1190                query.append(" ");
1191
1192                Query q = session.createQuery(query.toString());
1193
1194                QueryPos qPos = QueryPos.getInstance(q);
1195
1196                qPos.add(active);
1197
1198                Long count = null;
1199
1200                Iterator<Long> itr = q.list().iterator();
1201
1202                if (itr.hasNext()) {
1203                    count = itr.next();
1204                }
1205
1206                if (count == null) {
1207                    count = new Long(0);
1208                }
1209
1210                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1211                    finderClassName, finderMethodName, finderParams,
1212                    finderArgs, count);
1213
1214                return count.intValue();
1215            }
1216            catch (Exception e) {
1217                throw processException(e);
1218            }
1219            finally {
1220                closeSession(session);
1221            }
1222        }
1223        else {
1224            return ((Long)result).intValue();
1225        }
1226    }
1227
1228    public int countAll() throws SystemException {
1229        boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
1230        String finderClassName = Country.class.getName();
1231        String finderMethodName = "countAll";
1232        String[] finderParams = new String[] {  };
1233        Object[] finderArgs = new Object[] {  };
1234
1235        Object result = null;
1236
1237        if (finderClassNameCacheEnabled) {
1238            result = FinderCacheUtil.getResult(finderClassName,
1239                    finderMethodName, finderParams, finderArgs, this);
1240        }
1241
1242        if (result == null) {
1243            Session session = null;
1244
1245            try {
1246                session = openSession();
1247
1248                Query q = session.createQuery(
1249                        "SELECT COUNT(*) FROM com.liferay.portal.model.Country");
1250
1251                Long count = null;
1252
1253                Iterator<Long> itr = q.list().iterator();
1254
1255                if (itr.hasNext()) {
1256                    count = itr.next();
1257                }
1258
1259                if (count == null) {
1260                    count = new Long(0);
1261                }
1262
1263                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1264                    finderClassName, finderMethodName, finderParams,
1265                    finderArgs, count);
1266
1267                return count.intValue();
1268            }
1269            catch (Exception e) {
1270                throw processException(e);
1271            }
1272            finally {
1273                closeSession(session);
1274            }
1275        }
1276        else {
1277            return ((Long)result).intValue();
1278        }
1279    }
1280
1281    public void registerListener(ModelListener listener) {
1282        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1283
1284        listeners.add(listener);
1285
1286        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1287    }
1288
1289    public void unregisterListener(ModelListener listener) {
1290        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1291
1292        listeners.remove(listener);
1293
1294        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1295    }
1296
1297    public void afterPropertiesSet() {
1298        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1299                    com.liferay.portal.util.PropsUtil.get(
1300                        "value.object.listener.com.liferay.portal.model.Country")));
1301
1302        if (listenerClassNames.length > 0) {
1303            try {
1304                List<ModelListener> listeners = new ArrayList<ModelListener>();
1305
1306                for (String listenerClassName : listenerClassNames) {
1307                    listeners.add((ModelListener)Class.forName(
1308                            listenerClassName).newInstance());
1309                }
1310
1311                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1312            }
1313            catch (Exception e) {
1314                _log.error(e);
1315            }
1316        }
1317    }
1318
1319    private static Log _log = LogFactory.getLog(CountryPersistenceImpl.class);
1320    private ModelListener[] _listeners = new ModelListener[0];
1321}