1
22
23 package com.liferay.portal.service.persistence;
24
25 import com.liferay.portal.NoSuchPortletException;
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.ModelListener;
35 import com.liferay.portal.model.Portlet;
36 import com.liferay.portal.model.impl.PortletImpl;
37 import com.liferay.portal.model.impl.PortletModelImpl;
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 PortletPersistenceImpl extends BasePersistence
61 implements PortletPersistence {
62 public Portlet create(long id) {
63 Portlet portlet = new PortletImpl();
64
65 portlet.setNew(true);
66 portlet.setPrimaryKey(id);
67
68 return portlet;
69 }
70
71 public Portlet remove(long id)
72 throws NoSuchPortletException, SystemException {
73 Session session = null;
74
75 try {
76 session = openSession();
77
78 Portlet portlet = (Portlet)session.get(PortletImpl.class,
79 new Long(id));
80
81 if (portlet == null) {
82 if (_log.isWarnEnabled()) {
83 _log.warn("No Portlet exists with the primary key " + id);
84 }
85
86 throw new NoSuchPortletException(
87 "No Portlet exists with the primary key " + id);
88 }
89
90 return remove(portlet);
91 }
92 catch (NoSuchPortletException nsee) {
93 throw nsee;
94 }
95 catch (Exception e) {
96 throw HibernateUtil.processException(e);
97 }
98 finally {
99 closeSession(session);
100 }
101 }
102
103 public Portlet remove(Portlet portlet) throws SystemException {
104 ModelListener listener = _getListener();
105
106 if (listener != null) {
107 listener.onBeforeRemove(portlet);
108 }
109
110 portlet = removeImpl(portlet);
111
112 if (listener != null) {
113 listener.onAfterRemove(portlet);
114 }
115
116 return portlet;
117 }
118
119 protected Portlet removeImpl(Portlet portlet) throws SystemException {
120 Session session = null;
121
122 try {
123 session = openSession();
124
125 session.delete(portlet);
126
127 session.flush();
128
129 return portlet;
130 }
131 catch (Exception e) {
132 throw HibernateUtil.processException(e);
133 }
134 finally {
135 closeSession(session);
136
137 FinderCache.clearCache(Portlet.class.getName());
138 }
139 }
140
141 public Portlet update(Portlet portlet) throws SystemException {
142 return update(portlet, false);
143 }
144
145 public Portlet update(Portlet portlet, boolean merge)
146 throws SystemException {
147 ModelListener listener = _getListener();
148
149 boolean isNew = portlet.isNew();
150
151 if (listener != null) {
152 if (isNew) {
153 listener.onBeforeCreate(portlet);
154 }
155 else {
156 listener.onBeforeUpdate(portlet);
157 }
158 }
159
160 portlet = updateImpl(portlet, merge);
161
162 if (listener != null) {
163 if (isNew) {
164 listener.onAfterCreate(portlet);
165 }
166 else {
167 listener.onAfterUpdate(portlet);
168 }
169 }
170
171 return portlet;
172 }
173
174 public Portlet updateImpl(com.liferay.portal.model.Portlet portlet,
175 boolean merge) throws SystemException {
176 Session session = null;
177
178 try {
179 session = openSession();
180
181 if (merge) {
182 session.merge(portlet);
183 }
184 else {
185 if (portlet.isNew()) {
186 session.save(portlet);
187 }
188 }
189
190 session.flush();
191
192 portlet.setNew(false);
193
194 return portlet;
195 }
196 catch (Exception e) {
197 throw HibernateUtil.processException(e);
198 }
199 finally {
200 closeSession(session);
201
202 FinderCache.clearCache(Portlet.class.getName());
203 }
204 }
205
206 public Portlet findByPrimaryKey(long id)
207 throws NoSuchPortletException, SystemException {
208 Portlet portlet = fetchByPrimaryKey(id);
209
210 if (portlet == null) {
211 if (_log.isWarnEnabled()) {
212 _log.warn("No Portlet exists with the primary key " + id);
213 }
214
215 throw new NoSuchPortletException(
216 "No Portlet exists with the primary key " + id);
217 }
218
219 return portlet;
220 }
221
222 public Portlet fetchByPrimaryKey(long id) throws SystemException {
223 Session session = null;
224
225 try {
226 session = openSession();
227
228 return (Portlet)session.get(PortletImpl.class, new Long(id));
229 }
230 catch (Exception e) {
231 throw HibernateUtil.processException(e);
232 }
233 finally {
234 closeSession(session);
235 }
236 }
237
238 public List findByCompanyId(long companyId) throws SystemException {
239 boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
240 String finderClassName = Portlet.class.getName();
241 String finderMethodName = "findByCompanyId";
242 String[] finderParams = new String[] { Long.class.getName() };
243 Object[] finderArgs = new Object[] { new Long(companyId) };
244
245 Object result = null;
246
247 if (finderClassNameCacheEnabled) {
248 result = FinderCache.getResult(finderClassName, finderMethodName,
249 finderParams, finderArgs, getSessionFactory());
250 }
251
252 if (result == null) {
253 Session session = null;
254
255 try {
256 session = openSession();
257
258 StringMaker query = new StringMaker();
259
260 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
261
262 query.append("companyId = ?");
263
264 query.append(" ");
265
266 Query q = session.createQuery(query.toString());
267
268 int queryPos = 0;
269
270 q.setLong(queryPos++, companyId);
271
272 List list = q.list();
273
274 FinderCache.putResult(finderClassNameCacheEnabled,
275 finderClassName, finderMethodName, finderParams,
276 finderArgs, list);
277
278 return list;
279 }
280 catch (Exception e) {
281 throw HibernateUtil.processException(e);
282 }
283 finally {
284 closeSession(session);
285 }
286 }
287 else {
288 return (List)result;
289 }
290 }
291
292 public List findByCompanyId(long companyId, int begin, int end)
293 throws SystemException {
294 return findByCompanyId(companyId, begin, end, null);
295 }
296
297 public List findByCompanyId(long companyId, int begin, int end,
298 OrderByComparator obc) throws SystemException {
299 boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
300 String finderClassName = Portlet.class.getName();
301 String finderMethodName = "findByCompanyId";
302 String[] finderParams = new String[] {
303 Long.class.getName(),
304
305 "java.lang.Integer", "java.lang.Integer",
306 "com.liferay.portal.kernel.util.OrderByComparator"
307 };
308 Object[] finderArgs = new Object[] {
309 new Long(companyId),
310
311 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
312 };
313
314 Object result = null;
315
316 if (finderClassNameCacheEnabled) {
317 result = FinderCache.getResult(finderClassName, finderMethodName,
318 finderParams, finderArgs, getSessionFactory());
319 }
320
321 if (result == null) {
322 Session session = null;
323
324 try {
325 session = openSession();
326
327 StringMaker query = new StringMaker();
328
329 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
330
331 query.append("companyId = ?");
332
333 query.append(" ");
334
335 if (obc != null) {
336 query.append("ORDER BY ");
337 query.append(obc.getOrderBy());
338 }
339
340 Query q = session.createQuery(query.toString());
341
342 int queryPos = 0;
343
344 q.setLong(queryPos++, companyId);
345
346 List list = QueryUtil.list(q, getDialect(), begin, end);
347
348 FinderCache.putResult(finderClassNameCacheEnabled,
349 finderClassName, finderMethodName, finderParams,
350 finderArgs, list);
351
352 return list;
353 }
354 catch (Exception e) {
355 throw HibernateUtil.processException(e);
356 }
357 finally {
358 closeSession(session);
359 }
360 }
361 else {
362 return (List)result;
363 }
364 }
365
366 public Portlet findByCompanyId_First(long companyId, OrderByComparator obc)
367 throws NoSuchPortletException, SystemException {
368 List list = findByCompanyId(companyId, 0, 1, obc);
369
370 if (list.size() == 0) {
371 StringMaker msg = new StringMaker();
372
373 msg.append("No Portlet exists with the key {");
374
375 msg.append("companyId=" + companyId);
376
377 msg.append(StringPool.CLOSE_CURLY_BRACE);
378
379 throw new NoSuchPortletException(msg.toString());
380 }
381 else {
382 return (Portlet)list.get(0);
383 }
384 }
385
386 public Portlet findByCompanyId_Last(long companyId, OrderByComparator obc)
387 throws NoSuchPortletException, SystemException {
388 int count = countByCompanyId(companyId);
389
390 List list = findByCompanyId(companyId, count - 1, count, obc);
391
392 if (list.size() == 0) {
393 StringMaker msg = new StringMaker();
394
395 msg.append("No Portlet exists with the key {");
396
397 msg.append("companyId=" + companyId);
398
399 msg.append(StringPool.CLOSE_CURLY_BRACE);
400
401 throw new NoSuchPortletException(msg.toString());
402 }
403 else {
404 return (Portlet)list.get(0);
405 }
406 }
407
408 public Portlet[] findByCompanyId_PrevAndNext(long id, long companyId,
409 OrderByComparator obc) throws NoSuchPortletException, SystemException {
410 Portlet portlet = findByPrimaryKey(id);
411
412 int count = countByCompanyId(companyId);
413
414 Session session = null;
415
416 try {
417 session = openSession();
418
419 StringMaker query = new StringMaker();
420
421 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
422
423 query.append("companyId = ?");
424
425 query.append(" ");
426
427 if (obc != null) {
428 query.append("ORDER BY ");
429 query.append(obc.getOrderBy());
430 }
431
432 Query q = session.createQuery(query.toString());
433
434 int queryPos = 0;
435
436 q.setLong(queryPos++, companyId);
437
438 Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, portlet);
439
440 Portlet[] array = new PortletImpl[3];
441
442 array[0] = (Portlet)objArray[0];
443 array[1] = (Portlet)objArray[1];
444 array[2] = (Portlet)objArray[2];
445
446 return array;
447 }
448 catch (Exception e) {
449 throw HibernateUtil.processException(e);
450 }
451 finally {
452 closeSession(session);
453 }
454 }
455
456 public Portlet findByC_P(long companyId, String portletId)
457 throws NoSuchPortletException, SystemException {
458 Portlet portlet = fetchByC_P(companyId, portletId);
459
460 if (portlet == null) {
461 StringMaker msg = new StringMaker();
462
463 msg.append("No Portlet exists with the key {");
464
465 msg.append("companyId=" + companyId);
466
467 msg.append(", ");
468 msg.append("portletId=" + portletId);
469
470 msg.append(StringPool.CLOSE_CURLY_BRACE);
471
472 if (_log.isWarnEnabled()) {
473 _log.warn(msg.toString());
474 }
475
476 throw new NoSuchPortletException(msg.toString());
477 }
478
479 return portlet;
480 }
481
482 public Portlet fetchByC_P(long companyId, String portletId)
483 throws SystemException {
484 boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
485 String finderClassName = Portlet.class.getName();
486 String finderMethodName = "fetchByC_P";
487 String[] finderParams = new String[] {
488 Long.class.getName(), String.class.getName()
489 };
490 Object[] finderArgs = new Object[] { new Long(companyId), portletId };
491
492 Object result = null;
493
494 if (finderClassNameCacheEnabled) {
495 result = FinderCache.getResult(finderClassName, finderMethodName,
496 finderParams, finderArgs, getSessionFactory());
497 }
498
499 if (result == null) {
500 Session session = null;
501
502 try {
503 session = openSession();
504
505 StringMaker query = new StringMaker();
506
507 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
508
509 query.append("companyId = ?");
510
511 query.append(" AND ");
512
513 if (portletId == null) {
514 query.append("portletId IS NULL");
515 }
516 else {
517 query.append("portletId = ?");
518 }
519
520 query.append(" ");
521
522 Query q = session.createQuery(query.toString());
523
524 int queryPos = 0;
525
526 q.setLong(queryPos++, companyId);
527
528 if (portletId != null) {
529 q.setString(queryPos++, portletId);
530 }
531
532 List list = q.list();
533
534 FinderCache.putResult(finderClassNameCacheEnabled,
535 finderClassName, finderMethodName, finderParams,
536 finderArgs, list);
537
538 if (list.size() == 0) {
539 return null;
540 }
541 else {
542 return (Portlet)list.get(0);
543 }
544 }
545 catch (Exception e) {
546 throw HibernateUtil.processException(e);
547 }
548 finally {
549 closeSession(session);
550 }
551 }
552 else {
553 List list = (List)result;
554
555 if (list.size() == 0) {
556 return null;
557 }
558 else {
559 return (Portlet)list.get(0);
560 }
561 }
562 }
563
564 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
565 throws SystemException {
566 Session session = null;
567
568 try {
569 session = openSession();
570
571 DynamicQuery query = queryInitializer.initialize(session);
572
573 return query.list();
574 }
575 catch (Exception e) {
576 throw HibernateUtil.processException(e);
577 }
578 finally {
579 closeSession(session);
580 }
581 }
582
583 public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
584 int begin, int end) throws SystemException {
585 Session session = null;
586
587 try {
588 session = openSession();
589
590 DynamicQuery query = queryInitializer.initialize(session);
591
592 query.setLimit(begin, end);
593
594 return query.list();
595 }
596 catch (Exception e) {
597 throw HibernateUtil.processException(e);
598 }
599 finally {
600 closeSession(session);
601 }
602 }
603
604 public List findAll() throws SystemException {
605 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
606 }
607
608 public List findAll(int begin, int end) throws SystemException {
609 return findAll(begin, end, null);
610 }
611
612 public List findAll(int begin, int end, OrderByComparator obc)
613 throws SystemException {
614 boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
615 String finderClassName = Portlet.class.getName();
616 String finderMethodName = "findAll";
617 String[] finderParams = new String[] {
618 "java.lang.Integer", "java.lang.Integer",
619 "com.liferay.portal.kernel.util.OrderByComparator"
620 };
621 Object[] finderArgs = new Object[] {
622 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
623 };
624
625 Object result = null;
626
627 if (finderClassNameCacheEnabled) {
628 result = FinderCache.getResult(finderClassName, finderMethodName,
629 finderParams, finderArgs, getSessionFactory());
630 }
631
632 if (result == null) {
633 Session session = null;
634
635 try {
636 session = openSession();
637
638 StringMaker query = new StringMaker();
639
640 query.append("FROM com.liferay.portal.model.Portlet ");
641
642 if (obc != null) {
643 query.append("ORDER BY ");
644 query.append(obc.getOrderBy());
645 }
646
647 Query q = session.createQuery(query.toString());
648
649 List list = QueryUtil.list(q, getDialect(), begin, end);
650
651 if (obc == null) {
652 Collections.sort(list);
653 }
654
655 FinderCache.putResult(finderClassNameCacheEnabled,
656 finderClassName, finderMethodName, finderParams,
657 finderArgs, list);
658
659 return list;
660 }
661 catch (Exception e) {
662 throw HibernateUtil.processException(e);
663 }
664 finally {
665 closeSession(session);
666 }
667 }
668 else {
669 return (List)result;
670 }
671 }
672
673 public void removeByCompanyId(long companyId) throws SystemException {
674 Iterator itr = findByCompanyId(companyId).iterator();
675
676 while (itr.hasNext()) {
677 Portlet portlet = (Portlet)itr.next();
678
679 remove(portlet);
680 }
681 }
682
683 public void removeByC_P(long companyId, String portletId)
684 throws NoSuchPortletException, SystemException {
685 Portlet portlet = findByC_P(companyId, portletId);
686
687 remove(portlet);
688 }
689
690 public void removeAll() throws SystemException {
691 Iterator itr = findAll().iterator();
692
693 while (itr.hasNext()) {
694 remove((Portlet)itr.next());
695 }
696 }
697
698 public int countByCompanyId(long companyId) throws SystemException {
699 boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
700 String finderClassName = Portlet.class.getName();
701 String finderMethodName = "countByCompanyId";
702 String[] finderParams = new String[] { Long.class.getName() };
703 Object[] finderArgs = new Object[] { new Long(companyId) };
704
705 Object result = null;
706
707 if (finderClassNameCacheEnabled) {
708 result = FinderCache.getResult(finderClassName, finderMethodName,
709 finderParams, finderArgs, getSessionFactory());
710 }
711
712 if (result == null) {
713 Session session = null;
714
715 try {
716 session = openSession();
717
718 StringMaker query = new StringMaker();
719
720 query.append("SELECT COUNT(*) ");
721 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
722
723 query.append("companyId = ?");
724
725 query.append(" ");
726
727 Query q = session.createQuery(query.toString());
728
729 int queryPos = 0;
730
731 q.setLong(queryPos++, companyId);
732
733 Long count = null;
734
735 Iterator itr = q.list().iterator();
736
737 if (itr.hasNext()) {
738 count = (Long)itr.next();
739 }
740
741 if (count == null) {
742 count = new Long(0);
743 }
744
745 FinderCache.putResult(finderClassNameCacheEnabled,
746 finderClassName, finderMethodName, finderParams,
747 finderArgs, count);
748
749 return count.intValue();
750 }
751 catch (Exception e) {
752 throw HibernateUtil.processException(e);
753 }
754 finally {
755 closeSession(session);
756 }
757 }
758 else {
759 return ((Long)result).intValue();
760 }
761 }
762
763 public int countByC_P(long companyId, String portletId)
764 throws SystemException {
765 boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
766 String finderClassName = Portlet.class.getName();
767 String finderMethodName = "countByC_P";
768 String[] finderParams = new String[] {
769 Long.class.getName(), String.class.getName()
770 };
771 Object[] finderArgs = new Object[] { new Long(companyId), portletId };
772
773 Object result = null;
774
775 if (finderClassNameCacheEnabled) {
776 result = FinderCache.getResult(finderClassName, finderMethodName,
777 finderParams, finderArgs, getSessionFactory());
778 }
779
780 if (result == null) {
781 Session session = null;
782
783 try {
784 session = openSession();
785
786 StringMaker query = new StringMaker();
787
788 query.append("SELECT COUNT(*) ");
789 query.append("FROM com.liferay.portal.model.Portlet WHERE ");
790
791 query.append("companyId = ?");
792
793 query.append(" AND ");
794
795 if (portletId == null) {
796 query.append("portletId IS NULL");
797 }
798 else {
799 query.append("portletId = ?");
800 }
801
802 query.append(" ");
803
804 Query q = session.createQuery(query.toString());
805
806 int queryPos = 0;
807
808 q.setLong(queryPos++, companyId);
809
810 if (portletId != null) {
811 q.setString(queryPos++, portletId);
812 }
813
814 Long count = null;
815
816 Iterator itr = q.list().iterator();
817
818 if (itr.hasNext()) {
819 count = (Long)itr.next();
820 }
821
822 if (count == null) {
823 count = new Long(0);
824 }
825
826 FinderCache.putResult(finderClassNameCacheEnabled,
827 finderClassName, finderMethodName, finderParams,
828 finderArgs, count);
829
830 return count.intValue();
831 }
832 catch (Exception e) {
833 throw HibernateUtil.processException(e);
834 }
835 finally {
836 closeSession(session);
837 }
838 }
839 else {
840 return ((Long)result).intValue();
841 }
842 }
843
844 public int countAll() throws SystemException {
845 boolean finderClassNameCacheEnabled = PortletModelImpl.CACHE_ENABLED;
846 String finderClassName = Portlet.class.getName();
847 String finderMethodName = "countAll";
848 String[] finderParams = new String[] { };
849 Object[] finderArgs = new Object[] { };
850
851 Object result = null;
852
853 if (finderClassNameCacheEnabled) {
854 result = FinderCache.getResult(finderClassName, finderMethodName,
855 finderParams, finderArgs, getSessionFactory());
856 }
857
858 if (result == null) {
859 Session session = null;
860
861 try {
862 session = openSession();
863
864 Query q = session.createQuery(
865 "SELECT COUNT(*) FROM com.liferay.portal.model.Portlet");
866
867 Long count = null;
868
869 Iterator itr = q.list().iterator();
870
871 if (itr.hasNext()) {
872 count = (Long)itr.next();
873 }
874
875 if (count == null) {
876 count = new Long(0);
877 }
878
879 FinderCache.putResult(finderClassNameCacheEnabled,
880 finderClassName, finderMethodName, finderParams,
881 finderArgs, count);
882
883 return count.intValue();
884 }
885 catch (Exception e) {
886 throw HibernateUtil.processException(e);
887 }
888 finally {
889 closeSession(session);
890 }
891 }
892 else {
893 return ((Long)result).intValue();
894 }
895 }
896
897 protected void initDao() {
898 }
899
900 private static ModelListener _getListener() {
901 if (Validator.isNotNull(_LISTENER)) {
902 try {
903 return (ModelListener)Class.forName(_LISTENER).newInstance();
904 }
905 catch (Exception e) {
906 _log.error(e);
907 }
908 }
909
910 return null;
911 }
912
913 private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
914 "value.object.listener.com.liferay.portal.model.Portlet"));
915 private static Log _log = LogFactory.getLog(PortletPersistenceImpl.class);
916 }