001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.NoSuchModelException;
018 import com.liferay.portal.NoSuchPortletException;
019 import com.liferay.portal.kernel.bean.BeanReference;
020 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
023 import com.liferay.portal.kernel.dao.orm.FinderPath;
024 import com.liferay.portal.kernel.dao.orm.Query;
025 import com.liferay.portal.kernel.dao.orm.QueryPos;
026 import com.liferay.portal.kernel.dao.orm.QueryUtil;
027 import com.liferay.portal.kernel.dao.orm.Session;
028 import com.liferay.portal.kernel.exception.SystemException;
029 import com.liferay.portal.kernel.log.Log;
030 import com.liferay.portal.kernel.log.LogFactoryUtil;
031 import com.liferay.portal.kernel.util.GetterUtil;
032 import com.liferay.portal.kernel.util.InstanceFactory;
033 import com.liferay.portal.kernel.util.OrderByComparator;
034 import com.liferay.portal.kernel.util.StringBundler;
035 import com.liferay.portal.kernel.util.StringPool;
036 import com.liferay.portal.kernel.util.StringUtil;
037 import com.liferay.portal.kernel.util.Validator;
038 import com.liferay.portal.model.CacheModel;
039 import com.liferay.portal.model.ModelListener;
040 import com.liferay.portal.model.Portlet;
041 import com.liferay.portal.model.impl.PortletImpl;
042 import com.liferay.portal.model.impl.PortletModelImpl;
043 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
044
045 import java.io.Serializable;
046
047 import java.util.ArrayList;
048 import java.util.Collections;
049 import java.util.List;
050
051
063 public class PortletPersistenceImpl extends BasePersistenceImpl<Portlet>
064 implements PortletPersistence {
065
070 public static final String FINDER_CLASS_NAME_ENTITY = PortletImpl.class.getName();
071 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072 ".List1";
073 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074 ".List2";
075 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID =
076 new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
077 PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
078 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompanyId",
079 new String[] {
080 Long.class.getName(),
081
082 "java.lang.Integer", "java.lang.Integer",
083 "com.liferay.portal.kernel.util.OrderByComparator"
084 });
085 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID =
086 new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
087 PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
088 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompanyId",
089 new String[] { Long.class.getName() },
090 PortletModelImpl.COMPANYID_COLUMN_BITMASK);
091 public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
092 PortletModelImpl.FINDER_CACHE_ENABLED, Long.class,
093 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompanyId",
094 new String[] { Long.class.getName() });
095 public static final FinderPath FINDER_PATH_FETCH_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
096 PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
097 FINDER_CLASS_NAME_ENTITY, "fetchByC_P",
098 new String[] { Long.class.getName(), String.class.getName() },
099 PortletModelImpl.COMPANYID_COLUMN_BITMASK |
100 PortletModelImpl.PORTLETID_COLUMN_BITMASK);
101 public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
102 PortletModelImpl.FINDER_CACHE_ENABLED, Long.class,
103 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_P",
104 new String[] { Long.class.getName(), String.class.getName() });
105 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
106 PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
107 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
108 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
109 PortletModelImpl.FINDER_CACHE_ENABLED, PortletImpl.class,
110 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
111 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PortletModelImpl.ENTITY_CACHE_ENABLED,
112 PortletModelImpl.FINDER_CACHE_ENABLED, Long.class,
113 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
114
115
120 public void cacheResult(Portlet portlet) {
121 EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
122 PortletImpl.class, portlet.getPrimaryKey(), portlet);
123
124 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
125 new Object[] {
126 Long.valueOf(portlet.getCompanyId()),
127
128 portlet.getPortletId()
129 }, portlet);
130
131 portlet.resetOriginalValues();
132 }
133
134
139 public void cacheResult(List<Portlet> portlets) {
140 for (Portlet portlet : portlets) {
141 if (EntityCacheUtil.getResult(
142 PortletModelImpl.ENTITY_CACHE_ENABLED,
143 PortletImpl.class, portlet.getPrimaryKey()) == null) {
144 cacheResult(portlet);
145 }
146 else {
147 portlet.resetOriginalValues();
148 }
149 }
150 }
151
152
159 @Override
160 public void clearCache() {
161 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
162 CacheRegistryUtil.clear(PortletImpl.class.getName());
163 }
164
165 EntityCacheUtil.clearCache(PortletImpl.class.getName());
166
167 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
168 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
169 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
170 }
171
172
179 @Override
180 public void clearCache(Portlet portlet) {
181 EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
182 PortletImpl.class, portlet.getPrimaryKey());
183
184 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
185 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
186
187 clearUniqueFindersCache(portlet);
188 }
189
190 @Override
191 public void clearCache(List<Portlet> portlets) {
192 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
193 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
194
195 for (Portlet portlet : portlets) {
196 EntityCacheUtil.removeResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
197 PortletImpl.class, portlet.getPrimaryKey());
198
199 clearUniqueFindersCache(portlet);
200 }
201 }
202
203 protected void clearUniqueFindersCache(Portlet portlet) {
204 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
205 new Object[] {
206 Long.valueOf(portlet.getCompanyId()),
207
208 portlet.getPortletId()
209 });
210 }
211
212
218 public Portlet create(long id) {
219 Portlet portlet = new PortletImpl();
220
221 portlet.setNew(true);
222 portlet.setPrimaryKey(id);
223
224 return portlet;
225 }
226
227
235 public Portlet remove(long id)
236 throws NoSuchPortletException, SystemException {
237 return remove(Long.valueOf(id));
238 }
239
240
248 @Override
249 public Portlet remove(Serializable primaryKey)
250 throws NoSuchPortletException, SystemException {
251 Session session = null;
252
253 try {
254 session = openSession();
255
256 Portlet portlet = (Portlet)session.get(PortletImpl.class, primaryKey);
257
258 if (portlet == null) {
259 if (_log.isWarnEnabled()) {
260 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
261 }
262
263 throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
264 primaryKey);
265 }
266
267 return remove(portlet);
268 }
269 catch (NoSuchPortletException nsee) {
270 throw nsee;
271 }
272 catch (Exception e) {
273 throw processException(e);
274 }
275 finally {
276 closeSession(session);
277 }
278 }
279
280 @Override
281 protected Portlet removeImpl(Portlet portlet) throws SystemException {
282 portlet = toUnwrappedModel(portlet);
283
284 Session session = null;
285
286 try {
287 session = openSession();
288
289 BatchSessionUtil.delete(session, portlet);
290 }
291 catch (Exception e) {
292 throw processException(e);
293 }
294 finally {
295 closeSession(session);
296 }
297
298 clearCache(portlet);
299
300 return portlet;
301 }
302
303 @Override
304 public Portlet updateImpl(com.liferay.portal.model.Portlet portlet,
305 boolean merge) throws SystemException {
306 portlet = toUnwrappedModel(portlet);
307
308 boolean isNew = portlet.isNew();
309
310 PortletModelImpl portletModelImpl = (PortletModelImpl)portlet;
311
312 Session session = null;
313
314 try {
315 session = openSession();
316
317 BatchSessionUtil.update(session, portlet, merge);
318
319 portlet.setNew(false);
320 }
321 catch (Exception e) {
322 throw processException(e);
323 }
324 finally {
325 closeSession(session);
326 }
327
328 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
329
330 if (isNew || !PortletModelImpl.COLUMN_BITMASK_ENABLED) {
331 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
332 }
333
334 else {
335 if ((portletModelImpl.getColumnBitmask() &
336 FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
337 Object[] args = new Object[] {
338 Long.valueOf(portletModelImpl.getOriginalCompanyId())
339 };
340
341 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
342 args);
343 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
344 args);
345
346 args = new Object[] {
347 Long.valueOf(portletModelImpl.getCompanyId())
348 };
349
350 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_COMPANYID,
351 args);
352 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
353 args);
354 }
355 }
356
357 EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
358 PortletImpl.class, portlet.getPrimaryKey(), portlet);
359
360 if (isNew) {
361 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
362 new Object[] {
363 Long.valueOf(portlet.getCompanyId()),
364
365 portlet.getPortletId()
366 }, portlet);
367 }
368 else {
369 if ((portletModelImpl.getColumnBitmask() &
370 FINDER_PATH_FETCH_BY_C_P.getColumnBitmask()) != 0) {
371 Object[] args = new Object[] {
372 Long.valueOf(portletModelImpl.getOriginalCompanyId()),
373
374 portletModelImpl.getOriginalPortletId()
375 };
376
377 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_P, args);
378
379 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P, args);
380
381 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
382 new Object[] {
383 Long.valueOf(portlet.getCompanyId()),
384
385 portlet.getPortletId()
386 }, portlet);
387 }
388 }
389
390 return portlet;
391 }
392
393 protected Portlet toUnwrappedModel(Portlet portlet) {
394 if (portlet instanceof PortletImpl) {
395 return portlet;
396 }
397
398 PortletImpl portletImpl = new PortletImpl();
399
400 portletImpl.setNew(portlet.isNew());
401 portletImpl.setPrimaryKey(portlet.getPrimaryKey());
402
403 portletImpl.setId(portlet.getId());
404 portletImpl.setCompanyId(portlet.getCompanyId());
405 portletImpl.setPortletId(portlet.getPortletId());
406 portletImpl.setRoles(portlet.getRoles());
407 portletImpl.setActive(portlet.isActive());
408
409 return portletImpl;
410 }
411
412
420 @Override
421 public Portlet findByPrimaryKey(Serializable primaryKey)
422 throws NoSuchModelException, SystemException {
423 return findByPrimaryKey(((Long)primaryKey).longValue());
424 }
425
426
434 public Portlet findByPrimaryKey(long id)
435 throws NoSuchPortletException, SystemException {
436 Portlet portlet = fetchByPrimaryKey(id);
437
438 if (portlet == null) {
439 if (_log.isWarnEnabled()) {
440 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + id);
441 }
442
443 throw new NoSuchPortletException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
444 id);
445 }
446
447 return portlet;
448 }
449
450
457 @Override
458 public Portlet fetchByPrimaryKey(Serializable primaryKey)
459 throws SystemException {
460 return fetchByPrimaryKey(((Long)primaryKey).longValue());
461 }
462
463
470 public Portlet fetchByPrimaryKey(long id) throws SystemException {
471 Portlet portlet = (Portlet)EntityCacheUtil.getResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
472 PortletImpl.class, id);
473
474 if (portlet == _nullPortlet) {
475 return null;
476 }
477
478 if (portlet == null) {
479 Session session = null;
480
481 boolean hasException = false;
482
483 try {
484 session = openSession();
485
486 portlet = (Portlet)session.get(PortletImpl.class,
487 Long.valueOf(id));
488 }
489 catch (Exception e) {
490 hasException = true;
491
492 throw processException(e);
493 }
494 finally {
495 if (portlet != null) {
496 cacheResult(portlet);
497 }
498 else if (!hasException) {
499 EntityCacheUtil.putResult(PortletModelImpl.ENTITY_CACHE_ENABLED,
500 PortletImpl.class, id, _nullPortlet);
501 }
502
503 closeSession(session);
504 }
505 }
506
507 return portlet;
508 }
509
510
517 public List<Portlet> findByCompanyId(long companyId)
518 throws SystemException {
519 return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
520 null);
521 }
522
523
536 public List<Portlet> findByCompanyId(long companyId, int start, int end)
537 throws SystemException {
538 return findByCompanyId(companyId, start, end, null);
539 }
540
541
555 public List<Portlet> findByCompanyId(long companyId, int start, int end,
556 OrderByComparator orderByComparator) throws SystemException {
557 FinderPath finderPath = null;
558 Object[] finderArgs = null;
559
560 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
561 (orderByComparator == null)) {
562 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID;
563 finderArgs = new Object[] { companyId };
564 }
565 else {
566 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID;
567 finderArgs = new Object[] { companyId, start, end, orderByComparator };
568 }
569
570 List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(finderPath,
571 finderArgs, this);
572
573 if ((list != null) && !list.isEmpty()) {
574 for (Portlet portlet : list) {
575 if ((companyId != portlet.getCompanyId())) {
576 list = null;
577
578 break;
579 }
580 }
581 }
582
583 if (list == null) {
584 StringBundler query = null;
585
586 if (orderByComparator != null) {
587 query = new StringBundler(3 +
588 (orderByComparator.getOrderByFields().length * 3));
589 }
590 else {
591 query = new StringBundler(2);
592 }
593
594 query.append(_SQL_SELECT_PORTLET_WHERE);
595
596 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
597
598 if (orderByComparator != null) {
599 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
600 orderByComparator);
601 }
602
603 String sql = query.toString();
604
605 Session session = null;
606
607 try {
608 session = openSession();
609
610 Query q = session.createQuery(sql);
611
612 QueryPos qPos = QueryPos.getInstance(q);
613
614 qPos.add(companyId);
615
616 list = (List<Portlet>)QueryUtil.list(q, getDialect(), start, end);
617 }
618 catch (Exception e) {
619 throw processException(e);
620 }
621 finally {
622 if (list == null) {
623 FinderCacheUtil.removeResult(finderPath, finderArgs);
624 }
625 else {
626 cacheResult(list);
627
628 FinderCacheUtil.putResult(finderPath, finderArgs, list);
629 }
630
631 closeSession(session);
632 }
633 }
634
635 return list;
636 }
637
638
647 public Portlet findByCompanyId_First(long companyId,
648 OrderByComparator orderByComparator)
649 throws NoSuchPortletException, SystemException {
650 Portlet portlet = fetchByCompanyId_First(companyId, orderByComparator);
651
652 if (portlet != null) {
653 return portlet;
654 }
655
656 StringBundler msg = new StringBundler(4);
657
658 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
659
660 msg.append("companyId=");
661 msg.append(companyId);
662
663 msg.append(StringPool.CLOSE_CURLY_BRACE);
664
665 throw new NoSuchPortletException(msg.toString());
666 }
667
668
676 public Portlet fetchByCompanyId_First(long companyId,
677 OrderByComparator orderByComparator) throws SystemException {
678 List<Portlet> list = findByCompanyId(companyId, 0, 1, orderByComparator);
679
680 if (!list.isEmpty()) {
681 return list.get(0);
682 }
683
684 return null;
685 }
686
687
696 public Portlet findByCompanyId_Last(long companyId,
697 OrderByComparator orderByComparator)
698 throws NoSuchPortletException, SystemException {
699 Portlet portlet = fetchByCompanyId_Last(companyId, orderByComparator);
700
701 if (portlet != null) {
702 return portlet;
703 }
704
705 StringBundler msg = new StringBundler(4);
706
707 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
708
709 msg.append("companyId=");
710 msg.append(companyId);
711
712 msg.append(StringPool.CLOSE_CURLY_BRACE);
713
714 throw new NoSuchPortletException(msg.toString());
715 }
716
717
725 public Portlet fetchByCompanyId_Last(long companyId,
726 OrderByComparator orderByComparator) throws SystemException {
727 int count = countByCompanyId(companyId);
728
729 List<Portlet> list = findByCompanyId(companyId, count - 1, count,
730 orderByComparator);
731
732 if (!list.isEmpty()) {
733 return list.get(0);
734 }
735
736 return null;
737 }
738
739
749 public Portlet[] findByCompanyId_PrevAndNext(long id, long companyId,
750 OrderByComparator orderByComparator)
751 throws NoSuchPortletException, SystemException {
752 Portlet portlet = findByPrimaryKey(id);
753
754 Session session = null;
755
756 try {
757 session = openSession();
758
759 Portlet[] array = new PortletImpl[3];
760
761 array[0] = getByCompanyId_PrevAndNext(session, portlet, companyId,
762 orderByComparator, true);
763
764 array[1] = portlet;
765
766 array[2] = getByCompanyId_PrevAndNext(session, portlet, companyId,
767 orderByComparator, false);
768
769 return array;
770 }
771 catch (Exception e) {
772 throw processException(e);
773 }
774 finally {
775 closeSession(session);
776 }
777 }
778
779 protected Portlet getByCompanyId_PrevAndNext(Session session,
780 Portlet portlet, long companyId, OrderByComparator orderByComparator,
781 boolean previous) {
782 StringBundler query = null;
783
784 if (orderByComparator != null) {
785 query = new StringBundler(6 +
786 (orderByComparator.getOrderByFields().length * 6));
787 }
788 else {
789 query = new StringBundler(3);
790 }
791
792 query.append(_SQL_SELECT_PORTLET_WHERE);
793
794 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
795
796 if (orderByComparator != null) {
797 String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
798
799 if (orderByConditionFields.length > 0) {
800 query.append(WHERE_AND);
801 }
802
803 for (int i = 0; i < orderByConditionFields.length; i++) {
804 query.append(_ORDER_BY_ENTITY_ALIAS);
805 query.append(orderByConditionFields[i]);
806
807 if ((i + 1) < orderByConditionFields.length) {
808 if (orderByComparator.isAscending() ^ previous) {
809 query.append(WHERE_GREATER_THAN_HAS_NEXT);
810 }
811 else {
812 query.append(WHERE_LESSER_THAN_HAS_NEXT);
813 }
814 }
815 else {
816 if (orderByComparator.isAscending() ^ previous) {
817 query.append(WHERE_GREATER_THAN);
818 }
819 else {
820 query.append(WHERE_LESSER_THAN);
821 }
822 }
823 }
824
825 query.append(ORDER_BY_CLAUSE);
826
827 String[] orderByFields = orderByComparator.getOrderByFields();
828
829 for (int i = 0; i < orderByFields.length; i++) {
830 query.append(_ORDER_BY_ENTITY_ALIAS);
831 query.append(orderByFields[i]);
832
833 if ((i + 1) < orderByFields.length) {
834 if (orderByComparator.isAscending() ^ previous) {
835 query.append(ORDER_BY_ASC_HAS_NEXT);
836 }
837 else {
838 query.append(ORDER_BY_DESC_HAS_NEXT);
839 }
840 }
841 else {
842 if (orderByComparator.isAscending() ^ previous) {
843 query.append(ORDER_BY_ASC);
844 }
845 else {
846 query.append(ORDER_BY_DESC);
847 }
848 }
849 }
850 }
851
852 String sql = query.toString();
853
854 Query q = session.createQuery(sql);
855
856 q.setFirstResult(0);
857 q.setMaxResults(2);
858
859 QueryPos qPos = QueryPos.getInstance(q);
860
861 qPos.add(companyId);
862
863 if (orderByComparator != null) {
864 Object[] values = orderByComparator.getOrderByConditionValues(portlet);
865
866 for (Object value : values) {
867 qPos.add(value);
868 }
869 }
870
871 List<Portlet> list = q.list();
872
873 if (list.size() == 2) {
874 return list.get(1);
875 }
876 else {
877 return null;
878 }
879 }
880
881
890 public Portlet findByC_P(long companyId, String portletId)
891 throws NoSuchPortletException, SystemException {
892 Portlet portlet = fetchByC_P(companyId, portletId);
893
894 if (portlet == null) {
895 StringBundler msg = new StringBundler(6);
896
897 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
898
899 msg.append("companyId=");
900 msg.append(companyId);
901
902 msg.append(", portletId=");
903 msg.append(portletId);
904
905 msg.append(StringPool.CLOSE_CURLY_BRACE);
906
907 if (_log.isWarnEnabled()) {
908 _log.warn(msg.toString());
909 }
910
911 throw new NoSuchPortletException(msg.toString());
912 }
913
914 return portlet;
915 }
916
917
925 public Portlet fetchByC_P(long companyId, String portletId)
926 throws SystemException {
927 return fetchByC_P(companyId, portletId, true);
928 }
929
930
939 public Portlet fetchByC_P(long companyId, String portletId,
940 boolean retrieveFromCache) throws SystemException {
941 Object[] finderArgs = new Object[] { companyId, portletId };
942
943 Object result = null;
944
945 if (retrieveFromCache) {
946 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
947 finderArgs, this);
948 }
949
950 if (result instanceof Portlet) {
951 Portlet portlet = (Portlet)result;
952
953 if ((companyId != portlet.getCompanyId()) ||
954 !Validator.equals(portletId, portlet.getPortletId())) {
955 result = null;
956 }
957 }
958
959 if (result == null) {
960 StringBundler query = new StringBundler(3);
961
962 query.append(_SQL_SELECT_PORTLET_WHERE);
963
964 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
965
966 if (portletId == null) {
967 query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
968 }
969 else {
970 if (portletId.equals(StringPool.BLANK)) {
971 query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
972 }
973 else {
974 query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
975 }
976 }
977
978 String sql = query.toString();
979
980 Session session = null;
981
982 try {
983 session = openSession();
984
985 Query q = session.createQuery(sql);
986
987 QueryPos qPos = QueryPos.getInstance(q);
988
989 qPos.add(companyId);
990
991 if (portletId != null) {
992 qPos.add(portletId);
993 }
994
995 List<Portlet> list = q.list();
996
997 result = list;
998
999 Portlet portlet = null;
1000
1001 if (list.isEmpty()) {
1002 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
1003 finderArgs, list);
1004 }
1005 else {
1006 portlet = list.get(0);
1007
1008 cacheResult(portlet);
1009
1010 if ((portlet.getCompanyId() != companyId) ||
1011 (portlet.getPortletId() == null) ||
1012 !portlet.getPortletId().equals(portletId)) {
1013 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
1014 finderArgs, portlet);
1015 }
1016 }
1017
1018 return portlet;
1019 }
1020 catch (Exception e) {
1021 throw processException(e);
1022 }
1023 finally {
1024 if (result == null) {
1025 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
1026 finderArgs);
1027 }
1028
1029 closeSession(session);
1030 }
1031 }
1032 else {
1033 if (result instanceof List<?>) {
1034 return null;
1035 }
1036 else {
1037 return (Portlet)result;
1038 }
1039 }
1040 }
1041
1042
1048 public List<Portlet> findAll() throws SystemException {
1049 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1050 }
1051
1052
1064 public List<Portlet> findAll(int start, int end) throws SystemException {
1065 return findAll(start, end, null);
1066 }
1067
1068
1081 public List<Portlet> findAll(int start, int end,
1082 OrderByComparator orderByComparator) throws SystemException {
1083 FinderPath finderPath = null;
1084 Object[] finderArgs = new Object[] { start, end, orderByComparator };
1085
1086 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1087 (orderByComparator == null)) {
1088 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1089 finderArgs = FINDER_ARGS_EMPTY;
1090 }
1091 else {
1092 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1093 finderArgs = new Object[] { start, end, orderByComparator };
1094 }
1095
1096 List<Portlet> list = (List<Portlet>)FinderCacheUtil.getResult(finderPath,
1097 finderArgs, this);
1098
1099 if (list == null) {
1100 StringBundler query = null;
1101 String sql = null;
1102
1103 if (orderByComparator != null) {
1104 query = new StringBundler(2 +
1105 (orderByComparator.getOrderByFields().length * 3));
1106
1107 query.append(_SQL_SELECT_PORTLET);
1108
1109 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1110 orderByComparator);
1111
1112 sql = query.toString();
1113 }
1114 else {
1115 sql = _SQL_SELECT_PORTLET;
1116 }
1117
1118 Session session = null;
1119
1120 try {
1121 session = openSession();
1122
1123 Query q = session.createQuery(sql);
1124
1125 if (orderByComparator == null) {
1126 list = (List<Portlet>)QueryUtil.list(q, getDialect(),
1127 start, end, false);
1128
1129 Collections.sort(list);
1130 }
1131 else {
1132 list = (List<Portlet>)QueryUtil.list(q, getDialect(),
1133 start, end);
1134 }
1135 }
1136 catch (Exception e) {
1137 throw processException(e);
1138 }
1139 finally {
1140 if (list == null) {
1141 FinderCacheUtil.removeResult(finderPath, finderArgs);
1142 }
1143 else {
1144 cacheResult(list);
1145
1146 FinderCacheUtil.putResult(finderPath, finderArgs, list);
1147 }
1148
1149 closeSession(session);
1150 }
1151 }
1152
1153 return list;
1154 }
1155
1156
1162 public void removeByCompanyId(long companyId) throws SystemException {
1163 for (Portlet portlet : findByCompanyId(companyId)) {
1164 remove(portlet);
1165 }
1166 }
1167
1168
1176 public Portlet removeByC_P(long companyId, String portletId)
1177 throws NoSuchPortletException, SystemException {
1178 Portlet portlet = findByC_P(companyId, portletId);
1179
1180 return remove(portlet);
1181 }
1182
1183
1188 public void removeAll() throws SystemException {
1189 for (Portlet portlet : findAll()) {
1190 remove(portlet);
1191 }
1192 }
1193
1194
1201 public int countByCompanyId(long companyId) throws SystemException {
1202 Object[] finderArgs = new Object[] { companyId };
1203
1204 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
1205 finderArgs, this);
1206
1207 if (count == null) {
1208 StringBundler query = new StringBundler(2);
1209
1210 query.append(_SQL_COUNT_PORTLET_WHERE);
1211
1212 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1213
1214 String sql = query.toString();
1215
1216 Session session = null;
1217
1218 try {
1219 session = openSession();
1220
1221 Query q = session.createQuery(sql);
1222
1223 QueryPos qPos = QueryPos.getInstance(q);
1224
1225 qPos.add(companyId);
1226
1227 count = (Long)q.uniqueResult();
1228 }
1229 catch (Exception e) {
1230 throw processException(e);
1231 }
1232 finally {
1233 if (count == null) {
1234 count = Long.valueOf(0);
1235 }
1236
1237 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
1238 finderArgs, count);
1239
1240 closeSession(session);
1241 }
1242 }
1243
1244 return count.intValue();
1245 }
1246
1247
1255 public int countByC_P(long companyId, String portletId)
1256 throws SystemException {
1257 Object[] finderArgs = new Object[] { companyId, portletId };
1258
1259 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
1260 finderArgs, this);
1261
1262 if (count == null) {
1263 StringBundler query = new StringBundler(3);
1264
1265 query.append(_SQL_COUNT_PORTLET_WHERE);
1266
1267 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1268
1269 if (portletId == null) {
1270 query.append(_FINDER_COLUMN_C_P_PORTLETID_1);
1271 }
1272 else {
1273 if (portletId.equals(StringPool.BLANK)) {
1274 query.append(_FINDER_COLUMN_C_P_PORTLETID_3);
1275 }
1276 else {
1277 query.append(_FINDER_COLUMN_C_P_PORTLETID_2);
1278 }
1279 }
1280
1281 String sql = query.toString();
1282
1283 Session session = null;
1284
1285 try {
1286 session = openSession();
1287
1288 Query q = session.createQuery(sql);
1289
1290 QueryPos qPos = QueryPos.getInstance(q);
1291
1292 qPos.add(companyId);
1293
1294 if (portletId != null) {
1295 qPos.add(portletId);
1296 }
1297
1298 count = (Long)q.uniqueResult();
1299 }
1300 catch (Exception e) {
1301 throw processException(e);
1302 }
1303 finally {
1304 if (count == null) {
1305 count = Long.valueOf(0);
1306 }
1307
1308 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
1309 count);
1310
1311 closeSession(session);
1312 }
1313 }
1314
1315 return count.intValue();
1316 }
1317
1318
1324 public int countAll() throws SystemException {
1325 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1326 FINDER_ARGS_EMPTY, this);
1327
1328 if (count == null) {
1329 Session session = null;
1330
1331 try {
1332 session = openSession();
1333
1334 Query q = session.createQuery(_SQL_COUNT_PORTLET);
1335
1336 count = (Long)q.uniqueResult();
1337 }
1338 catch (Exception e) {
1339 throw processException(e);
1340 }
1341 finally {
1342 if (count == null) {
1343 count = Long.valueOf(0);
1344 }
1345
1346 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1347 FINDER_ARGS_EMPTY, count);
1348
1349 closeSession(session);
1350 }
1351 }
1352
1353 return count.intValue();
1354 }
1355
1356
1359 public void afterPropertiesSet() {
1360 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1361 com.liferay.portal.util.PropsUtil.get(
1362 "value.object.listener.com.liferay.portal.model.Portlet")));
1363
1364 if (listenerClassNames.length > 0) {
1365 try {
1366 List<ModelListener<Portlet>> listenersList = new ArrayList<ModelListener<Portlet>>();
1367
1368 for (String listenerClassName : listenerClassNames) {
1369 listenersList.add((ModelListener<Portlet>)InstanceFactory.newInstance(
1370 listenerClassName));
1371 }
1372
1373 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1374 }
1375 catch (Exception e) {
1376 _log.error(e);
1377 }
1378 }
1379 }
1380
1381 public void destroy() {
1382 EntityCacheUtil.removeCache(PortletImpl.class.getName());
1383 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1384 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1385 }
1386
1387 @BeanReference(type = AccountPersistence.class)
1388 protected AccountPersistence accountPersistence;
1389 @BeanReference(type = AddressPersistence.class)
1390 protected AddressPersistence addressPersistence;
1391 @BeanReference(type = BrowserTrackerPersistence.class)
1392 protected BrowserTrackerPersistence browserTrackerPersistence;
1393 @BeanReference(type = ClassNamePersistence.class)
1394 protected ClassNamePersistence classNamePersistence;
1395 @BeanReference(type = ClusterGroupPersistence.class)
1396 protected ClusterGroupPersistence clusterGroupPersistence;
1397 @BeanReference(type = CompanyPersistence.class)
1398 protected CompanyPersistence companyPersistence;
1399 @BeanReference(type = ContactPersistence.class)
1400 protected ContactPersistence contactPersistence;
1401 @BeanReference(type = CountryPersistence.class)
1402 protected CountryPersistence countryPersistence;
1403 @BeanReference(type = EmailAddressPersistence.class)
1404 protected EmailAddressPersistence emailAddressPersistence;
1405 @BeanReference(type = GroupPersistence.class)
1406 protected GroupPersistence groupPersistence;
1407 @BeanReference(type = ImagePersistence.class)
1408 protected ImagePersistence imagePersistence;
1409 @BeanReference(type = LayoutPersistence.class)
1410 protected LayoutPersistence layoutPersistence;
1411 @BeanReference(type = LayoutBranchPersistence.class)
1412 protected LayoutBranchPersistence layoutBranchPersistence;
1413 @BeanReference(type = LayoutPrototypePersistence.class)
1414 protected LayoutPrototypePersistence layoutPrototypePersistence;
1415 @BeanReference(type = LayoutRevisionPersistence.class)
1416 protected LayoutRevisionPersistence layoutRevisionPersistence;
1417 @BeanReference(type = LayoutSetPersistence.class)
1418 protected LayoutSetPersistence layoutSetPersistence;
1419 @BeanReference(type = LayoutSetBranchPersistence.class)
1420 protected LayoutSetBranchPersistence layoutSetBranchPersistence;
1421 @BeanReference(type = LayoutSetPrototypePersistence.class)
1422 protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1423 @BeanReference(type = ListTypePersistence.class)
1424 protected ListTypePersistence listTypePersistence;
1425 @BeanReference(type = LockPersistence.class)
1426 protected LockPersistence lockPersistence;
1427 @BeanReference(type = MembershipRequestPersistence.class)
1428 protected MembershipRequestPersistence membershipRequestPersistence;
1429 @BeanReference(type = OrganizationPersistence.class)
1430 protected OrganizationPersistence organizationPersistence;
1431 @BeanReference(type = OrgGroupPermissionPersistence.class)
1432 protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1433 @BeanReference(type = OrgGroupRolePersistence.class)
1434 protected OrgGroupRolePersistence orgGroupRolePersistence;
1435 @BeanReference(type = OrgLaborPersistence.class)
1436 protected OrgLaborPersistence orgLaborPersistence;
1437 @BeanReference(type = PasswordPolicyPersistence.class)
1438 protected PasswordPolicyPersistence passwordPolicyPersistence;
1439 @BeanReference(type = PasswordPolicyRelPersistence.class)
1440 protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1441 @BeanReference(type = PasswordTrackerPersistence.class)
1442 protected PasswordTrackerPersistence passwordTrackerPersistence;
1443 @BeanReference(type = PermissionPersistence.class)
1444 protected PermissionPersistence permissionPersistence;
1445 @BeanReference(type = PhonePersistence.class)
1446 protected PhonePersistence phonePersistence;
1447 @BeanReference(type = PluginSettingPersistence.class)
1448 protected PluginSettingPersistence pluginSettingPersistence;
1449 @BeanReference(type = PortalPreferencesPersistence.class)
1450 protected PortalPreferencesPersistence portalPreferencesPersistence;
1451 @BeanReference(type = PortletPersistence.class)
1452 protected PortletPersistence portletPersistence;
1453 @BeanReference(type = PortletItemPersistence.class)
1454 protected PortletItemPersistence portletItemPersistence;
1455 @BeanReference(type = PortletPreferencesPersistence.class)
1456 protected PortletPreferencesPersistence portletPreferencesPersistence;
1457 @BeanReference(type = RegionPersistence.class)
1458 protected RegionPersistence regionPersistence;
1459 @BeanReference(type = ReleasePersistence.class)
1460 protected ReleasePersistence releasePersistence;
1461 @BeanReference(type = RepositoryPersistence.class)
1462 protected RepositoryPersistence repositoryPersistence;
1463 @BeanReference(type = RepositoryEntryPersistence.class)
1464 protected RepositoryEntryPersistence repositoryEntryPersistence;
1465 @BeanReference(type = ResourcePersistence.class)
1466 protected ResourcePersistence resourcePersistence;
1467 @BeanReference(type = ResourceActionPersistence.class)
1468 protected ResourceActionPersistence resourceActionPersistence;
1469 @BeanReference(type = ResourceBlockPersistence.class)
1470 protected ResourceBlockPersistence resourceBlockPersistence;
1471 @BeanReference(type = ResourceBlockPermissionPersistence.class)
1472 protected ResourceBlockPermissionPersistence resourceBlockPermissionPersistence;
1473 @BeanReference(type = ResourceCodePersistence.class)
1474 protected ResourceCodePersistence resourceCodePersistence;
1475 @BeanReference(type = ResourcePermissionPersistence.class)
1476 protected ResourcePermissionPersistence resourcePermissionPersistence;
1477 @BeanReference(type = ResourceTypePermissionPersistence.class)
1478 protected ResourceTypePermissionPersistence resourceTypePermissionPersistence;
1479 @BeanReference(type = RolePersistence.class)
1480 protected RolePersistence rolePersistence;
1481 @BeanReference(type = ServiceComponentPersistence.class)
1482 protected ServiceComponentPersistence serviceComponentPersistence;
1483 @BeanReference(type = ShardPersistence.class)
1484 protected ShardPersistence shardPersistence;
1485 @BeanReference(type = SubscriptionPersistence.class)
1486 protected SubscriptionPersistence subscriptionPersistence;
1487 @BeanReference(type = TeamPersistence.class)
1488 protected TeamPersistence teamPersistence;
1489 @BeanReference(type = TicketPersistence.class)
1490 protected TicketPersistence ticketPersistence;
1491 @BeanReference(type = UserPersistence.class)
1492 protected UserPersistence userPersistence;
1493 @BeanReference(type = UserGroupPersistence.class)
1494 protected UserGroupPersistence userGroupPersistence;
1495 @BeanReference(type = UserGroupGroupRolePersistence.class)
1496 protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1497 @BeanReference(type = UserGroupRolePersistence.class)
1498 protected UserGroupRolePersistence userGroupRolePersistence;
1499 @BeanReference(type = UserIdMapperPersistence.class)
1500 protected UserIdMapperPersistence userIdMapperPersistence;
1501 @BeanReference(type = UserNotificationEventPersistence.class)
1502 protected UserNotificationEventPersistence userNotificationEventPersistence;
1503 @BeanReference(type = UserTrackerPersistence.class)
1504 protected UserTrackerPersistence userTrackerPersistence;
1505 @BeanReference(type = UserTrackerPathPersistence.class)
1506 protected UserTrackerPathPersistence userTrackerPathPersistence;
1507 @BeanReference(type = VirtualHostPersistence.class)
1508 protected VirtualHostPersistence virtualHostPersistence;
1509 @BeanReference(type = WebDAVPropsPersistence.class)
1510 protected WebDAVPropsPersistence webDAVPropsPersistence;
1511 @BeanReference(type = WebsitePersistence.class)
1512 protected WebsitePersistence websitePersistence;
1513 @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1514 protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1515 @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1516 protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1517 private static final String _SQL_SELECT_PORTLET = "SELECT portlet FROM Portlet portlet";
1518 private static final String _SQL_SELECT_PORTLET_WHERE = "SELECT portlet FROM Portlet portlet WHERE ";
1519 private static final String _SQL_COUNT_PORTLET = "SELECT COUNT(portlet) FROM Portlet portlet";
1520 private static final String _SQL_COUNT_PORTLET_WHERE = "SELECT COUNT(portlet) FROM Portlet portlet WHERE ";
1521 private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "portlet.companyId = ?";
1522 private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "portlet.companyId = ? AND ";
1523 private static final String _FINDER_COLUMN_C_P_PORTLETID_1 = "portlet.portletId IS NULL";
1524 private static final String _FINDER_COLUMN_C_P_PORTLETID_2 = "portlet.portletId = ?";
1525 private static final String _FINDER_COLUMN_C_P_PORTLETID_3 = "(portlet.portletId IS NULL OR portlet.portletId = ?)";
1526 private static final String _ORDER_BY_ENTITY_ALIAS = "portlet.";
1527 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Portlet exists with the primary key ";
1528 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Portlet exists with the key {";
1529 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1530 private static Log _log = LogFactoryUtil.getLog(PortletPersistenceImpl.class);
1531 private static Portlet _nullPortlet = new PortletImpl() {
1532 @Override
1533 public Object clone() {
1534 return this;
1535 }
1536
1537 @Override
1538 public CacheModel<Portlet> toCacheModel() {
1539 return _nullPortletCacheModel;
1540 }
1541 };
1542
1543 private static CacheModel<Portlet> _nullPortletCacheModel = new CacheModel<Portlet>() {
1544 public Portlet toEntityModel() {
1545 return _nullPortlet;
1546 }
1547 };
1548 }