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