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.bean.InitializingBean;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.Query;
31  import com.liferay.portal.kernel.dao.orm.QueryPos;
32  import com.liferay.portal.kernel.dao.orm.QueryUtil;
33  import com.liferay.portal.kernel.dao.orm.Session;
34  import com.liferay.portal.kernel.util.GetterUtil;
35  import com.liferay.portal.kernel.util.ListUtil;
36  import com.liferay.portal.kernel.util.OrderByComparator;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.model.Country;
40  import com.liferay.portal.model.ModelListener;
41  import com.liferay.portal.model.impl.CountryImpl;
42  import com.liferay.portal.model.impl.CountryModelImpl;
43  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  import java.util.ArrayList;
49  import java.util.Collections;
50  import java.util.Iterator;
51  import java.util.List;
52  
53  /**
54   * <a href="CountryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class CountryPersistenceImpl extends BasePersistenceImpl
60      implements CountryPersistence, InitializingBean {
61      public Country create(long countryId) {
62          Country country = new CountryImpl();
63  
64          country.setNew(true);
65          country.setPrimaryKey(countryId);
66  
67          return country;
68      }
69  
70      public Country remove(long countryId)
71          throws NoSuchCountryException, SystemException {
72          Session session = null;
73  
74          try {
75              session = openSession();
76  
77              Country country = (Country)session.get(CountryImpl.class,
78                      new Long(countryId));
79  
80              if (country == null) {
81                  if (_log.isWarnEnabled()) {
82                      _log.warn("No Country exists with the primary key " +
83                          countryId);
84                  }
85  
86                  throw new NoSuchCountryException(
87                      "No Country exists with the primary key " + countryId);
88              }
89  
90              return remove(country);
91          }
92          catch (NoSuchCountryException nsee) {
93              throw nsee;
94          }
95          catch (Exception e) {
96              throw processException(e);
97          }
98          finally {
99              closeSession(session);
100         }
101     }
102 
103     public Country remove(Country country) throws SystemException {
104         if (_listeners.length > 0) {
105             for (ModelListener listener : _listeners) {
106                 listener.onBeforeRemove(country);
107             }
108         }
109 
110         country = removeImpl(country);
111 
112         if (_listeners.length > 0) {
113             for (ModelListener listener : _listeners) {
114                 listener.onAfterRemove(country);
115             }
116         }
117 
118         return country;
119     }
120 
121     protected Country removeImpl(Country country) throws SystemException {
122         Session session = null;
123 
124         try {
125             session = openSession();
126 
127             session.delete(country);
128 
129             session.flush();
130 
131             return country;
132         }
133         catch (Exception e) {
134             throw processException(e);
135         }
136         finally {
137             closeSession(session);
138 
139             FinderCacheUtil.clearCache(Country.class.getName());
140         }
141     }
142 
143     /**
144      * @deprecated Use <code>update(Country country, boolean merge)</code>.
145      */
146     public Country update(Country country) throws SystemException {
147         if (_log.isWarnEnabled()) {
148             _log.warn(
149                 "Using the deprecated update(Country country) method. Use update(Country country, boolean merge) instead.");
150         }
151 
152         return update(country, false);
153     }
154 
155     /**
156      * Add, update, or merge, the entity. This method also calls the model
157      * listeners to trigger the proper events associated with adding, deleting,
158      * or updating an entity.
159      *
160      * @param        country the entity to add, update, or merge
161      * @param        merge boolean value for whether to merge the entity. The
162      *                default value is false. Setting merge to true is more
163      *                expensive and should only be true when country is
164      *                transient. See LEP-5473 for a detailed discussion of this
165      *                method.
166      * @return        true if the portlet can be displayed via Ajax
167      */
168     public Country update(Country country, boolean merge)
169         throws SystemException {
170         boolean isNew = country.isNew();
171 
172         if (_listeners.length > 0) {
173             for (ModelListener listener : _listeners) {
174                 if (isNew) {
175                     listener.onBeforeCreate(country);
176                 }
177                 else {
178                     listener.onBeforeUpdate(country);
179                 }
180             }
181         }
182 
183         country = updateImpl(country, merge);
184 
185         if (_listeners.length > 0) {
186             for (ModelListener listener : _listeners) {
187                 if (isNew) {
188                     listener.onAfterCreate(country);
189                 }
190                 else {
191                     listener.onAfterUpdate(country);
192                 }
193             }
194         }
195 
196         return country;
197     }
198 
199     public Country updateImpl(com.liferay.portal.model.Country country,
200         boolean merge) throws SystemException {
201         Session session = null;
202 
203         try {
204             session = openSession();
205 
206             if (merge) {
207                 session.merge(country);
208             }
209             else {
210                 if (country.isNew()) {
211                     session.save(country);
212                 }
213             }
214 
215             session.flush();
216 
217             country.setNew(false);
218 
219             return country;
220         }
221         catch (Exception e) {
222             throw processException(e);
223         }
224         finally {
225             closeSession(session);
226 
227             FinderCacheUtil.clearCache(Country.class.getName());
228         }
229     }
230 
231     public Country findByPrimaryKey(long countryId)
232         throws NoSuchCountryException, SystemException {
233         Country country = fetchByPrimaryKey(countryId);
234 
235         if (country == null) {
236             if (_log.isWarnEnabled()) {
237                 _log.warn("No Country exists with the primary key " +
238                     countryId);
239             }
240 
241             throw new NoSuchCountryException(
242                 "No Country exists with the primary key " + countryId);
243         }
244 
245         return country;
246     }
247 
248     public Country fetchByPrimaryKey(long countryId) throws SystemException {
249         Session session = null;
250 
251         try {
252             session = openSession();
253 
254             return (Country)session.get(CountryImpl.class, new Long(countryId));
255         }
256         catch (Exception e) {
257             throw processException(e);
258         }
259         finally {
260             closeSession(session);
261         }
262     }
263 
264     public List<Country> findByActive(boolean active) throws SystemException {
265         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
266         String finderClassName = Country.class.getName();
267         String finderMethodName = "findByActive";
268         String[] finderParams = new String[] { Boolean.class.getName() };
269         Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
270 
271         Object result = null;
272 
273         if (finderClassNameCacheEnabled) {
274             result = FinderCacheUtil.getResult(finderClassName,
275                     finderMethodName, finderParams, finderArgs, this);
276         }
277 
278         if (result == null) {
279             Session session = null;
280 
281             try {
282                 session = openSession();
283 
284                 StringBuilder query = new StringBuilder();
285 
286                 query.append("FROM com.liferay.portal.model.Country WHERE ");
287 
288                 query.append("active_ = ?");
289 
290                 query.append(" ");
291 
292                 query.append("ORDER BY ");
293 
294                 query.append("name ASC");
295 
296                 Query q = session.createQuery(query.toString());
297 
298                 QueryPos qPos = QueryPos.getInstance(q);
299 
300                 qPos.add(active);
301 
302                 List<Country> list = q.list();
303 
304                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
305                     finderClassName, finderMethodName, finderParams,
306                     finderArgs, list);
307 
308                 return list;
309             }
310             catch (Exception e) {
311                 throw processException(e);
312             }
313             finally {
314                 closeSession(session);
315             }
316         }
317         else {
318             return (List<Country>)result;
319         }
320     }
321 
322     public List<Country> findByActive(boolean active, int start, int end)
323         throws SystemException {
324         return findByActive(active, start, end, null);
325     }
326 
327     public List<Country> findByActive(boolean active, int start, int end,
328         OrderByComparator obc) throws SystemException {
329         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
330         String finderClassName = Country.class.getName();
331         String finderMethodName = "findByActive";
332         String[] finderParams = new String[] {
333                 Boolean.class.getName(),
334                 
335                 "java.lang.Integer", "java.lang.Integer",
336                 "com.liferay.portal.kernel.util.OrderByComparator"
337             };
338         Object[] finderArgs = new Object[] {
339                 Boolean.valueOf(active),
340                 
341                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
342             };
343 
344         Object result = null;
345 
346         if (finderClassNameCacheEnabled) {
347             result = FinderCacheUtil.getResult(finderClassName,
348                     finderMethodName, finderParams, finderArgs, this);
349         }
350 
351         if (result == null) {
352             Session session = null;
353 
354             try {
355                 session = openSession();
356 
357                 StringBuilder query = new StringBuilder();
358 
359                 query.append("FROM com.liferay.portal.model.Country WHERE ");
360 
361                 query.append("active_ = ?");
362 
363                 query.append(" ");
364 
365                 if (obc != null) {
366                     query.append("ORDER BY ");
367                     query.append(obc.getOrderBy());
368                 }
369 
370                 else {
371                     query.append("ORDER BY ");
372 
373                     query.append("name ASC");
374                 }
375 
376                 Query q = session.createQuery(query.toString());
377 
378                 QueryPos qPos = QueryPos.getInstance(q);
379 
380                 qPos.add(active);
381 
382                 List<Country> list = (List<Country>)QueryUtil.list(q,
383                         getDialect(), start, end);
384 
385                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
386                     finderClassName, finderMethodName, finderParams,
387                     finderArgs, list);
388 
389                 return list;
390             }
391             catch (Exception e) {
392                 throw processException(e);
393             }
394             finally {
395                 closeSession(session);
396             }
397         }
398         else {
399             return (List<Country>)result;
400         }
401     }
402 
403     public Country findByActive_First(boolean active, OrderByComparator obc)
404         throws NoSuchCountryException, SystemException {
405         List<Country> list = findByActive(active, 0, 1, obc);
406 
407         if (list.size() == 0) {
408             StringBuilder msg = new StringBuilder();
409 
410             msg.append("No Country exists with the key {");
411 
412             msg.append("active=" + active);
413 
414             msg.append(StringPool.CLOSE_CURLY_BRACE);
415 
416             throw new NoSuchCountryException(msg.toString());
417         }
418         else {
419             return list.get(0);
420         }
421     }
422 
423     public Country findByActive_Last(boolean active, OrderByComparator obc)
424         throws NoSuchCountryException, SystemException {
425         int count = countByActive(active);
426 
427         List<Country> list = findByActive(active, count - 1, count, obc);
428 
429         if (list.size() == 0) {
430             StringBuilder msg = new StringBuilder();
431 
432             msg.append("No Country exists with the key {");
433 
434             msg.append("active=" + active);
435 
436             msg.append(StringPool.CLOSE_CURLY_BRACE);
437 
438             throw new NoSuchCountryException(msg.toString());
439         }
440         else {
441             return list.get(0);
442         }
443     }
444 
445     public Country[] findByActive_PrevAndNext(long countryId, boolean active,
446         OrderByComparator obc) throws NoSuchCountryException, SystemException {
447         Country country = findByPrimaryKey(countryId);
448 
449         int count = countByActive(active);
450 
451         Session session = null;
452 
453         try {
454             session = openSession();
455 
456             StringBuilder query = new StringBuilder();
457 
458             query.append("FROM com.liferay.portal.model.Country WHERE ");
459 
460             query.append("active_ = ?");
461 
462             query.append(" ");
463 
464             if (obc != null) {
465                 query.append("ORDER BY ");
466                 query.append(obc.getOrderBy());
467             }
468 
469             else {
470                 query.append("ORDER BY ");
471 
472                 query.append("name ASC");
473             }
474 
475             Query q = session.createQuery(query.toString());
476 
477             QueryPos qPos = QueryPos.getInstance(q);
478 
479             qPos.add(active);
480 
481             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, country);
482 
483             Country[] array = new CountryImpl[3];
484 
485             array[0] = (Country)objArray[0];
486             array[1] = (Country)objArray[1];
487             array[2] = (Country)objArray[2];
488 
489             return array;
490         }
491         catch (Exception e) {
492             throw processException(e);
493         }
494         finally {
495             closeSession(session);
496         }
497     }
498 
499     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
500         throws SystemException {
501         Session session = null;
502 
503         try {
504             session = openSession();
505 
506             dynamicQuery.compile(session);
507 
508             return dynamicQuery.list();
509         }
510         catch (Exception e) {
511             throw processException(e);
512         }
513         finally {
514             closeSession(session);
515         }
516     }
517 
518     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
519         int start, int end) throws SystemException {
520         Session session = null;
521 
522         try {
523             session = openSession();
524 
525             dynamicQuery.setLimit(start, end);
526 
527             dynamicQuery.compile(session);
528 
529             return dynamicQuery.list();
530         }
531         catch (Exception e) {
532             throw processException(e);
533         }
534         finally {
535             closeSession(session);
536         }
537     }
538 
539     public List<Country> findAll() throws SystemException {
540         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
541     }
542 
543     public List<Country> findAll(int start, int end) throws SystemException {
544         return findAll(start, end, null);
545     }
546 
547     public List<Country> findAll(int start, int end, OrderByComparator obc)
548         throws SystemException {
549         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
550         String finderClassName = Country.class.getName();
551         String finderMethodName = "findAll";
552         String[] finderParams = new String[] {
553                 "java.lang.Integer", "java.lang.Integer",
554                 "com.liferay.portal.kernel.util.OrderByComparator"
555             };
556         Object[] finderArgs = new Object[] {
557                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
558             };
559 
560         Object result = null;
561 
562         if (finderClassNameCacheEnabled) {
563             result = FinderCacheUtil.getResult(finderClassName,
564                     finderMethodName, finderParams, finderArgs, this);
565         }
566 
567         if (result == null) {
568             Session session = null;
569 
570             try {
571                 session = openSession();
572 
573                 StringBuilder query = new StringBuilder();
574 
575                 query.append("FROM com.liferay.portal.model.Country ");
576 
577                 if (obc != null) {
578                     query.append("ORDER BY ");
579                     query.append(obc.getOrderBy());
580                 }
581 
582                 else {
583                     query.append("ORDER BY ");
584 
585                     query.append("name ASC");
586                 }
587 
588                 Query q = session.createQuery(query.toString());
589 
590                 List<Country> list = (List<Country>)QueryUtil.list(q,
591                         getDialect(), start, end);
592 
593                 if (obc == null) {
594                     Collections.sort(list);
595                 }
596 
597                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
598                     finderClassName, finderMethodName, finderParams,
599                     finderArgs, list);
600 
601                 return list;
602             }
603             catch (Exception e) {
604                 throw processException(e);
605             }
606             finally {
607                 closeSession(session);
608             }
609         }
610         else {
611             return (List<Country>)result;
612         }
613     }
614 
615     public void removeByActive(boolean active) throws SystemException {
616         for (Country country : findByActive(active)) {
617             remove(country);
618         }
619     }
620 
621     public void removeAll() throws SystemException {
622         for (Country country : findAll()) {
623             remove(country);
624         }
625     }
626 
627     public int countByActive(boolean active) throws SystemException {
628         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
629         String finderClassName = Country.class.getName();
630         String finderMethodName = "countByActive";
631         String[] finderParams = new String[] { Boolean.class.getName() };
632         Object[] finderArgs = new Object[] { Boolean.valueOf(active) };
633 
634         Object result = null;
635 
636         if (finderClassNameCacheEnabled) {
637             result = FinderCacheUtil.getResult(finderClassName,
638                     finderMethodName, finderParams, finderArgs, this);
639         }
640 
641         if (result == null) {
642             Session session = null;
643 
644             try {
645                 session = openSession();
646 
647                 StringBuilder query = new StringBuilder();
648 
649                 query.append("SELECT COUNT(*) ");
650                 query.append("FROM com.liferay.portal.model.Country WHERE ");
651 
652                 query.append("active_ = ?");
653 
654                 query.append(" ");
655 
656                 Query q = session.createQuery(query.toString());
657 
658                 QueryPos qPos = QueryPos.getInstance(q);
659 
660                 qPos.add(active);
661 
662                 Long count = null;
663 
664                 Iterator<Long> itr = q.list().iterator();
665 
666                 if (itr.hasNext()) {
667                     count = itr.next();
668                 }
669 
670                 if (count == null) {
671                     count = new Long(0);
672                 }
673 
674                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
675                     finderClassName, finderMethodName, finderParams,
676                     finderArgs, count);
677 
678                 return count.intValue();
679             }
680             catch (Exception e) {
681                 throw processException(e);
682             }
683             finally {
684                 closeSession(session);
685             }
686         }
687         else {
688             return ((Long)result).intValue();
689         }
690     }
691 
692     public int countAll() throws SystemException {
693         boolean finderClassNameCacheEnabled = CountryModelImpl.CACHE_ENABLED;
694         String finderClassName = Country.class.getName();
695         String finderMethodName = "countAll";
696         String[] finderParams = new String[] {  };
697         Object[] finderArgs = new Object[] {  };
698 
699         Object result = null;
700 
701         if (finderClassNameCacheEnabled) {
702             result = FinderCacheUtil.getResult(finderClassName,
703                     finderMethodName, finderParams, finderArgs, this);
704         }
705 
706         if (result == null) {
707             Session session = null;
708 
709             try {
710                 session = openSession();
711 
712                 Query q = session.createQuery(
713                         "SELECT COUNT(*) FROM com.liferay.portal.model.Country");
714 
715                 Long count = null;
716 
717                 Iterator<Long> itr = q.list().iterator();
718 
719                 if (itr.hasNext()) {
720                     count = itr.next();
721                 }
722 
723                 if (count == null) {
724                     count = new Long(0);
725                 }
726 
727                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
728                     finderClassName, finderMethodName, finderParams,
729                     finderArgs, count);
730 
731                 return count.intValue();
732             }
733             catch (Exception e) {
734                 throw processException(e);
735             }
736             finally {
737                 closeSession(session);
738             }
739         }
740         else {
741             return ((Long)result).intValue();
742         }
743     }
744 
745     public void registerListener(ModelListener listener) {
746         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
747 
748         listeners.add(listener);
749 
750         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
751     }
752 
753     public void unregisterListener(ModelListener listener) {
754         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
755 
756         listeners.remove(listener);
757 
758         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
759     }
760 
761     public void afterPropertiesSet() {
762         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
763                     com.liferay.portal.util.PropsUtil.get(
764                         "value.object.listener.com.liferay.portal.model.Country")));
765 
766         if (listenerClassNames.length > 0) {
767             try {
768                 List<ModelListener> listeners = new ArrayList<ModelListener>();
769 
770                 for (String listenerClassName : listenerClassNames) {
771                     listeners.add((ModelListener)Class.forName(
772                             listenerClassName).newInstance());
773                 }
774 
775                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
776             }
777             catch (Exception e) {
778                 _log.error(e);
779             }
780         }
781     }
782 
783     private static Log _log = LogFactory.getLog(CountryPersistenceImpl.class);
784     private ModelListener[] _listeners = new ModelListener[0];
785 }