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