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