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