001
014
015 package com.liferay.portlet.expando.service.persistence;
016
017 import com.liferay.portal.NoSuchModelException;
018 import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021 import com.liferay.portal.kernel.dao.orm.FinderPath;
022 import com.liferay.portal.kernel.dao.orm.Query;
023 import com.liferay.portal.kernel.dao.orm.QueryPos;
024 import com.liferay.portal.kernel.dao.orm.QueryUtil;
025 import com.liferay.portal.kernel.dao.orm.Session;
026 import com.liferay.portal.kernel.exception.SystemException;
027 import com.liferay.portal.kernel.log.Log;
028 import com.liferay.portal.kernel.log.LogFactoryUtil;
029 import com.liferay.portal.kernel.util.GetterUtil;
030 import com.liferay.portal.kernel.util.InstanceFactory;
031 import com.liferay.portal.kernel.util.OrderByComparator;
032 import com.liferay.portal.kernel.util.StringBundler;
033 import com.liferay.portal.kernel.util.StringPool;
034 import com.liferay.portal.kernel.util.StringUtil;
035 import com.liferay.portal.kernel.util.UnmodifiableList;
036 import com.liferay.portal.model.CacheModel;
037 import com.liferay.portal.model.ModelListener;
038 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
039
040 import com.liferay.portlet.expando.NoSuchRowException;
041 import com.liferay.portlet.expando.model.ExpandoRow;
042 import com.liferay.portlet.expando.model.impl.ExpandoRowImpl;
043 import com.liferay.portlet.expando.model.impl.ExpandoRowModelImpl;
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 ExpandoRowPersistenceImpl extends BasePersistenceImpl<ExpandoRow>
064 implements ExpandoRowPersistence {
065
070 public static final String FINDER_CLASS_NAME_ENTITY = ExpandoRowImpl.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_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
076 ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
077 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
078 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
079 ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
080 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
081 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
082 ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
083 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
084 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_TABLEID = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
085 ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
086 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByTableId",
087 new String[] {
088 Long.class.getName(),
089
090 Integer.class.getName(), Integer.class.getName(),
091 OrderByComparator.class.getName()
092 });
093 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID =
094 new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
095 ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
096 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByTableId",
097 new String[] { Long.class.getName() },
098 ExpandoRowModelImpl.TABLEID_COLUMN_BITMASK);
099 public static final FinderPath FINDER_PATH_COUNT_BY_TABLEID = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
100 ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
101 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByTableId",
102 new String[] { Long.class.getName() });
103
104
111 public List<ExpandoRow> findByTableId(long tableId)
112 throws SystemException {
113 return findByTableId(tableId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
114 }
115
116
129 public List<ExpandoRow> findByTableId(long tableId, int start, int end)
130 throws SystemException {
131 return findByTableId(tableId, start, end, null);
132 }
133
134
148 public List<ExpandoRow> findByTableId(long tableId, int start, int end,
149 OrderByComparator orderByComparator) throws SystemException {
150 boolean pagination = true;
151 FinderPath finderPath = null;
152 Object[] finderArgs = null;
153
154 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
155 (orderByComparator == null)) {
156 pagination = false;
157 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID;
158 finderArgs = new Object[] { tableId };
159 }
160 else {
161 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_TABLEID;
162 finderArgs = new Object[] { tableId, start, end, orderByComparator };
163 }
164
165 List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(finderPath,
166 finderArgs, this);
167
168 if ((list != null) && !list.isEmpty()) {
169 for (ExpandoRow expandoRow : list) {
170 if ((tableId != expandoRow.getTableId())) {
171 list = null;
172
173 break;
174 }
175 }
176 }
177
178 if (list == null) {
179 StringBundler query = null;
180
181 if (orderByComparator != null) {
182 query = new StringBundler(3 +
183 (orderByComparator.getOrderByFields().length * 3));
184 }
185 else {
186 query = new StringBundler(3);
187 }
188
189 query.append(_SQL_SELECT_EXPANDOROW_WHERE);
190
191 query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
192
193 if (orderByComparator != null) {
194 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
195 orderByComparator);
196 }
197 else
198 if (pagination) {
199 query.append(ExpandoRowModelImpl.ORDER_BY_JPQL);
200 }
201
202 String sql = query.toString();
203
204 Session session = null;
205
206 try {
207 session = openSession();
208
209 Query q = session.createQuery(sql);
210
211 QueryPos qPos = QueryPos.getInstance(q);
212
213 qPos.add(tableId);
214
215 if (!pagination) {
216 list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
217 start, end, false);
218
219 Collections.sort(list);
220
221 list = new UnmodifiableList<ExpandoRow>(list);
222 }
223 else {
224 list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
225 start, end);
226 }
227
228 cacheResult(list);
229
230 FinderCacheUtil.putResult(finderPath, finderArgs, list);
231 }
232 catch (Exception e) {
233 FinderCacheUtil.removeResult(finderPath, finderArgs);
234
235 throw processException(e);
236 }
237 finally {
238 closeSession(session);
239 }
240 }
241
242 return list;
243 }
244
245
254 public ExpandoRow findByTableId_First(long tableId,
255 OrderByComparator orderByComparator)
256 throws NoSuchRowException, SystemException {
257 ExpandoRow expandoRow = fetchByTableId_First(tableId, orderByComparator);
258
259 if (expandoRow != null) {
260 return expandoRow;
261 }
262
263 StringBundler msg = new StringBundler(4);
264
265 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
266
267 msg.append("tableId=");
268 msg.append(tableId);
269
270 msg.append(StringPool.CLOSE_CURLY_BRACE);
271
272 throw new NoSuchRowException(msg.toString());
273 }
274
275
283 public ExpandoRow fetchByTableId_First(long tableId,
284 OrderByComparator orderByComparator) throws SystemException {
285 List<ExpandoRow> list = findByTableId(tableId, 0, 1, orderByComparator);
286
287 if (!list.isEmpty()) {
288 return list.get(0);
289 }
290
291 return null;
292 }
293
294
303 public ExpandoRow findByTableId_Last(long tableId,
304 OrderByComparator orderByComparator)
305 throws NoSuchRowException, SystemException {
306 ExpandoRow expandoRow = fetchByTableId_Last(tableId, orderByComparator);
307
308 if (expandoRow != null) {
309 return expandoRow;
310 }
311
312 StringBundler msg = new StringBundler(4);
313
314 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
315
316 msg.append("tableId=");
317 msg.append(tableId);
318
319 msg.append(StringPool.CLOSE_CURLY_BRACE);
320
321 throw new NoSuchRowException(msg.toString());
322 }
323
324
332 public ExpandoRow fetchByTableId_Last(long tableId,
333 OrderByComparator orderByComparator) throws SystemException {
334 int count = countByTableId(tableId);
335
336 List<ExpandoRow> list = findByTableId(tableId, count - 1, count,
337 orderByComparator);
338
339 if (!list.isEmpty()) {
340 return list.get(0);
341 }
342
343 return null;
344 }
345
346
356 public ExpandoRow[] findByTableId_PrevAndNext(long rowId, long tableId,
357 OrderByComparator orderByComparator)
358 throws NoSuchRowException, SystemException {
359 ExpandoRow expandoRow = findByPrimaryKey(rowId);
360
361 Session session = null;
362
363 try {
364 session = openSession();
365
366 ExpandoRow[] array = new ExpandoRowImpl[3];
367
368 array[0] = getByTableId_PrevAndNext(session, expandoRow, tableId,
369 orderByComparator, true);
370
371 array[1] = expandoRow;
372
373 array[2] = getByTableId_PrevAndNext(session, expandoRow, tableId,
374 orderByComparator, false);
375
376 return array;
377 }
378 catch (Exception e) {
379 throw processException(e);
380 }
381 finally {
382 closeSession(session);
383 }
384 }
385
386 protected ExpandoRow getByTableId_PrevAndNext(Session session,
387 ExpandoRow expandoRow, long tableId,
388 OrderByComparator orderByComparator, boolean previous) {
389 StringBundler query = null;
390
391 if (orderByComparator != null) {
392 query = new StringBundler(6 +
393 (orderByComparator.getOrderByFields().length * 6));
394 }
395 else {
396 query = new StringBundler(3);
397 }
398
399 query.append(_SQL_SELECT_EXPANDOROW_WHERE);
400
401 query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
402
403 if (orderByComparator != null) {
404 String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
405
406 if (orderByConditionFields.length > 0) {
407 query.append(WHERE_AND);
408 }
409
410 for (int i = 0; i < orderByConditionFields.length; i++) {
411 query.append(_ORDER_BY_ENTITY_ALIAS);
412 query.append(orderByConditionFields[i]);
413
414 if ((i + 1) < orderByConditionFields.length) {
415 if (orderByComparator.isAscending() ^ previous) {
416 query.append(WHERE_GREATER_THAN_HAS_NEXT);
417 }
418 else {
419 query.append(WHERE_LESSER_THAN_HAS_NEXT);
420 }
421 }
422 else {
423 if (orderByComparator.isAscending() ^ previous) {
424 query.append(WHERE_GREATER_THAN);
425 }
426 else {
427 query.append(WHERE_LESSER_THAN);
428 }
429 }
430 }
431
432 query.append(ORDER_BY_CLAUSE);
433
434 String[] orderByFields = orderByComparator.getOrderByFields();
435
436 for (int i = 0; i < orderByFields.length; i++) {
437 query.append(_ORDER_BY_ENTITY_ALIAS);
438 query.append(orderByFields[i]);
439
440 if ((i + 1) < orderByFields.length) {
441 if (orderByComparator.isAscending() ^ previous) {
442 query.append(ORDER_BY_ASC_HAS_NEXT);
443 }
444 else {
445 query.append(ORDER_BY_DESC_HAS_NEXT);
446 }
447 }
448 else {
449 if (orderByComparator.isAscending() ^ previous) {
450 query.append(ORDER_BY_ASC);
451 }
452 else {
453 query.append(ORDER_BY_DESC);
454 }
455 }
456 }
457 }
458 else {
459 query.append(ExpandoRowModelImpl.ORDER_BY_JPQL);
460 }
461
462 String sql = query.toString();
463
464 Query q = session.createQuery(sql);
465
466 q.setFirstResult(0);
467 q.setMaxResults(2);
468
469 QueryPos qPos = QueryPos.getInstance(q);
470
471 qPos.add(tableId);
472
473 if (orderByComparator != null) {
474 Object[] values = orderByComparator.getOrderByConditionValues(expandoRow);
475
476 for (Object value : values) {
477 qPos.add(value);
478 }
479 }
480
481 List<ExpandoRow> list = q.list();
482
483 if (list.size() == 2) {
484 return list.get(1);
485 }
486 else {
487 return null;
488 }
489 }
490
491
497 public void removeByTableId(long tableId) throws SystemException {
498 for (ExpandoRow expandoRow : findByTableId(tableId, QueryUtil.ALL_POS,
499 QueryUtil.ALL_POS, null)) {
500 remove(expandoRow);
501 }
502 }
503
504
511 public int countByTableId(long tableId) throws SystemException {
512 FinderPath finderPath = FINDER_PATH_COUNT_BY_TABLEID;
513
514 Object[] finderArgs = new Object[] { tableId };
515
516 Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
517 this);
518
519 if (count == null) {
520 StringBundler query = new StringBundler(2);
521
522 query.append(_SQL_COUNT_EXPANDOROW_WHERE);
523
524 query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
525
526 String sql = query.toString();
527
528 Session session = null;
529
530 try {
531 session = openSession();
532
533 Query q = session.createQuery(sql);
534
535 QueryPos qPos = QueryPos.getInstance(q);
536
537 qPos.add(tableId);
538
539 count = (Long)q.uniqueResult();
540
541 FinderCacheUtil.putResult(finderPath, finderArgs, count);
542 }
543 catch (Exception e) {
544 FinderCacheUtil.removeResult(finderPath, finderArgs);
545
546 throw processException(e);
547 }
548 finally {
549 closeSession(session);
550 }
551 }
552
553 return count.intValue();
554 }
555
556 private static final String _FINDER_COLUMN_TABLEID_TABLEID_2 = "expandoRow.tableId = ?";
557 public static final FinderPath FINDER_PATH_FETCH_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
558 ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
559 FINDER_CLASS_NAME_ENTITY, "fetchByT_C",
560 new String[] { Long.class.getName(), Long.class.getName() },
561 ExpandoRowModelImpl.TABLEID_COLUMN_BITMASK |
562 ExpandoRowModelImpl.CLASSPK_COLUMN_BITMASK);
563 public static final FinderPath FINDER_PATH_COUNT_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
564 ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
565 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByT_C",
566 new String[] { Long.class.getName(), Long.class.getName() });
567
568
577 public ExpandoRow findByT_C(long tableId, long classPK)
578 throws NoSuchRowException, SystemException {
579 ExpandoRow expandoRow = fetchByT_C(tableId, classPK);
580
581 if (expandoRow == null) {
582 StringBundler msg = new StringBundler(6);
583
584 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
585
586 msg.append("tableId=");
587 msg.append(tableId);
588
589 msg.append(", classPK=");
590 msg.append(classPK);
591
592 msg.append(StringPool.CLOSE_CURLY_BRACE);
593
594 if (_log.isWarnEnabled()) {
595 _log.warn(msg.toString());
596 }
597
598 throw new NoSuchRowException(msg.toString());
599 }
600
601 return expandoRow;
602 }
603
604
612 public ExpandoRow fetchByT_C(long tableId, long classPK)
613 throws SystemException {
614 return fetchByT_C(tableId, classPK, true);
615 }
616
617
626 public ExpandoRow fetchByT_C(long tableId, long classPK,
627 boolean retrieveFromCache) throws SystemException {
628 Object[] finderArgs = new Object[] { tableId, classPK };
629
630 Object result = null;
631
632 if (retrieveFromCache) {
633 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_C,
634 finderArgs, this);
635 }
636
637 if (result instanceof ExpandoRow) {
638 ExpandoRow expandoRow = (ExpandoRow)result;
639
640 if ((tableId != expandoRow.getTableId()) ||
641 (classPK != expandoRow.getClassPK())) {
642 result = null;
643 }
644 }
645
646 if (result == null) {
647 StringBundler query = new StringBundler(4);
648
649 query.append(_SQL_SELECT_EXPANDOROW_WHERE);
650
651 query.append(_FINDER_COLUMN_T_C_TABLEID_2);
652
653 query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
654
655 String sql = query.toString();
656
657 Session session = null;
658
659 try {
660 session = openSession();
661
662 Query q = session.createQuery(sql);
663
664 QueryPos qPos = QueryPos.getInstance(q);
665
666 qPos.add(tableId);
667
668 qPos.add(classPK);
669
670 List<ExpandoRow> list = q.list();
671
672 if (list.isEmpty()) {
673 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
674 finderArgs, list);
675 }
676 else {
677 ExpandoRow expandoRow = list.get(0);
678
679 result = expandoRow;
680
681 cacheResult(expandoRow);
682
683 if ((expandoRow.getTableId() != tableId) ||
684 (expandoRow.getClassPK() != classPK)) {
685 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
686 finderArgs, expandoRow);
687 }
688 }
689 }
690 catch (Exception e) {
691 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
692 finderArgs);
693
694 throw processException(e);
695 }
696 finally {
697 closeSession(session);
698 }
699 }
700
701 if (result instanceof List<?>) {
702 return null;
703 }
704 else {
705 return (ExpandoRow)result;
706 }
707 }
708
709
717 public ExpandoRow removeByT_C(long tableId, long classPK)
718 throws NoSuchRowException, SystemException {
719 ExpandoRow expandoRow = findByT_C(tableId, classPK);
720
721 return remove(expandoRow);
722 }
723
724
732 public int countByT_C(long tableId, long classPK) throws SystemException {
733 FinderPath finderPath = FINDER_PATH_COUNT_BY_T_C;
734
735 Object[] finderArgs = new Object[] { tableId, classPK };
736
737 Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
738 this);
739
740 if (count == null) {
741 StringBundler query = new StringBundler(3);
742
743 query.append(_SQL_COUNT_EXPANDOROW_WHERE);
744
745 query.append(_FINDER_COLUMN_T_C_TABLEID_2);
746
747 query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
748
749 String sql = query.toString();
750
751 Session session = null;
752
753 try {
754 session = openSession();
755
756 Query q = session.createQuery(sql);
757
758 QueryPos qPos = QueryPos.getInstance(q);
759
760 qPos.add(tableId);
761
762 qPos.add(classPK);
763
764 count = (Long)q.uniqueResult();
765
766 FinderCacheUtil.putResult(finderPath, finderArgs, count);
767 }
768 catch (Exception e) {
769 FinderCacheUtil.removeResult(finderPath, finderArgs);
770
771 throw processException(e);
772 }
773 finally {
774 closeSession(session);
775 }
776 }
777
778 return count.intValue();
779 }
780
781 private static final String _FINDER_COLUMN_T_C_TABLEID_2 = "expandoRow.tableId = ? AND ";
782 private static final String _FINDER_COLUMN_T_C_CLASSPK_2 = "expandoRow.classPK = ?";
783
784
789 public void cacheResult(ExpandoRow expandoRow) {
790 EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
791 ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
792
793 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
794 new Object[] {
795 Long.valueOf(expandoRow.getTableId()),
796 Long.valueOf(expandoRow.getClassPK())
797 }, expandoRow);
798
799 expandoRow.resetOriginalValues();
800 }
801
802
807 public void cacheResult(List<ExpandoRow> expandoRows) {
808 for (ExpandoRow expandoRow : expandoRows) {
809 if (EntityCacheUtil.getResult(
810 ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
811 ExpandoRowImpl.class, expandoRow.getPrimaryKey()) == null) {
812 cacheResult(expandoRow);
813 }
814 else {
815 expandoRow.resetOriginalValues();
816 }
817 }
818 }
819
820
827 @Override
828 public void clearCache() {
829 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
830 CacheRegistryUtil.clear(ExpandoRowImpl.class.getName());
831 }
832
833 EntityCacheUtil.clearCache(ExpandoRowImpl.class.getName());
834
835 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
836 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
837 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
838 }
839
840
847 @Override
848 public void clearCache(ExpandoRow expandoRow) {
849 EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
850 ExpandoRowImpl.class, expandoRow.getPrimaryKey());
851
852 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
853 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
854
855 clearUniqueFindersCache(expandoRow);
856 }
857
858 @Override
859 public void clearCache(List<ExpandoRow> expandoRows) {
860 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
861 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
862
863 for (ExpandoRow expandoRow : expandoRows) {
864 EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
865 ExpandoRowImpl.class, expandoRow.getPrimaryKey());
866
867 clearUniqueFindersCache(expandoRow);
868 }
869 }
870
871 protected void cacheUniqueFindersCache(ExpandoRow expandoRow) {
872 if (expandoRow.isNew()) {
873 Object[] args = new Object[] {
874 Long.valueOf(expandoRow.getTableId()),
875 Long.valueOf(expandoRow.getClassPK())
876 };
877
878 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, args,
879 Long.valueOf(1));
880 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C, args, expandoRow);
881 }
882 else {
883 ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
884
885 if ((expandoRowModelImpl.getColumnBitmask() &
886 FINDER_PATH_FETCH_BY_T_C.getColumnBitmask()) != 0) {
887 Object[] args = new Object[] {
888 Long.valueOf(expandoRow.getTableId()),
889 Long.valueOf(expandoRow.getClassPK())
890 };
891
892 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, args,
893 Long.valueOf(1));
894 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C, args,
895 expandoRow);
896 }
897 }
898 }
899
900 protected void clearUniqueFindersCache(ExpandoRow expandoRow) {
901 ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
902
903 Object[] args = new Object[] {
904 Long.valueOf(expandoRow.getTableId()),
905 Long.valueOf(expandoRow.getClassPK())
906 };
907
908 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_T_C, args);
909 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C, args);
910
911 if ((expandoRowModelImpl.getColumnBitmask() &
912 FINDER_PATH_FETCH_BY_T_C.getColumnBitmask()) != 0) {
913 args = new Object[] {
914 Long.valueOf(expandoRowModelImpl.getOriginalTableId()),
915 Long.valueOf(expandoRowModelImpl.getOriginalClassPK())
916 };
917
918 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_T_C, args);
919 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C, args);
920 }
921 }
922
923
929 public ExpandoRow create(long rowId) {
930 ExpandoRow expandoRow = new ExpandoRowImpl();
931
932 expandoRow.setNew(true);
933 expandoRow.setPrimaryKey(rowId);
934
935 return expandoRow;
936 }
937
938
946 public ExpandoRow remove(long rowId)
947 throws NoSuchRowException, SystemException {
948 return remove(Long.valueOf(rowId));
949 }
950
951
959 @Override
960 public ExpandoRow remove(Serializable primaryKey)
961 throws NoSuchRowException, SystemException {
962 Session session = null;
963
964 try {
965 session = openSession();
966
967 ExpandoRow expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
968 primaryKey);
969
970 if (expandoRow == null) {
971 if (_log.isWarnEnabled()) {
972 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
973 }
974
975 throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
976 primaryKey);
977 }
978
979 return remove(expandoRow);
980 }
981 catch (NoSuchRowException nsee) {
982 throw nsee;
983 }
984 catch (Exception e) {
985 throw processException(e);
986 }
987 finally {
988 closeSession(session);
989 }
990 }
991
992 @Override
993 protected ExpandoRow removeImpl(ExpandoRow expandoRow)
994 throws SystemException {
995 expandoRow = toUnwrappedModel(expandoRow);
996
997 Session session = null;
998
999 try {
1000 session = openSession();
1001
1002 if (!session.contains(expandoRow)) {
1003 expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
1004 expandoRow.getPrimaryKeyObj());
1005 }
1006
1007 if (expandoRow != null) {
1008 session.delete(expandoRow);
1009 }
1010 }
1011 catch (Exception e) {
1012 throw processException(e);
1013 }
1014 finally {
1015 closeSession(session);
1016 }
1017
1018 if (expandoRow != null) {
1019 clearCache(expandoRow);
1020 }
1021
1022 return expandoRow;
1023 }
1024
1025 @Override
1026 public ExpandoRow updateImpl(
1027 com.liferay.portlet.expando.model.ExpandoRow expandoRow)
1028 throws SystemException {
1029 expandoRow = toUnwrappedModel(expandoRow);
1030
1031 boolean isNew = expandoRow.isNew();
1032
1033 ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
1034
1035 Session session = null;
1036
1037 try {
1038 session = openSession();
1039
1040 if (expandoRow.isNew()) {
1041 session.save(expandoRow);
1042
1043 expandoRow.setNew(false);
1044 }
1045 else {
1046 session.merge(expandoRow);
1047 }
1048 }
1049 catch (Exception e) {
1050 throw processException(e);
1051 }
1052 finally {
1053 closeSession(session);
1054 }
1055
1056 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1057
1058 if (isNew || !ExpandoRowModelImpl.COLUMN_BITMASK_ENABLED) {
1059 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1060 }
1061
1062 else {
1063 if ((expandoRowModelImpl.getColumnBitmask() &
1064 FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID.getColumnBitmask()) != 0) {
1065 Object[] args = new Object[] {
1066 Long.valueOf(expandoRowModelImpl.getOriginalTableId())
1067 };
1068
1069 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TABLEID, args);
1070 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID,
1071 args);
1072
1073 args = new Object[] {
1074 Long.valueOf(expandoRowModelImpl.getTableId())
1075 };
1076
1077 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TABLEID, args);
1078 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID,
1079 args);
1080 }
1081 }
1082
1083 EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1084 ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
1085
1086 clearUniqueFindersCache(expandoRow);
1087 cacheUniqueFindersCache(expandoRow);
1088
1089 return expandoRow;
1090 }
1091
1092 protected ExpandoRow toUnwrappedModel(ExpandoRow expandoRow) {
1093 if (expandoRow instanceof ExpandoRowImpl) {
1094 return expandoRow;
1095 }
1096
1097 ExpandoRowImpl expandoRowImpl = new ExpandoRowImpl();
1098
1099 expandoRowImpl.setNew(expandoRow.isNew());
1100 expandoRowImpl.setPrimaryKey(expandoRow.getPrimaryKey());
1101
1102 expandoRowImpl.setRowId(expandoRow.getRowId());
1103 expandoRowImpl.setCompanyId(expandoRow.getCompanyId());
1104 expandoRowImpl.setModifiedDate(expandoRow.getModifiedDate());
1105 expandoRowImpl.setTableId(expandoRow.getTableId());
1106 expandoRowImpl.setClassPK(expandoRow.getClassPK());
1107
1108 return expandoRowImpl;
1109 }
1110
1111
1119 @Override
1120 public ExpandoRow findByPrimaryKey(Serializable primaryKey)
1121 throws NoSuchModelException, SystemException {
1122 return findByPrimaryKey(((Long)primaryKey).longValue());
1123 }
1124
1125
1133 public ExpandoRow findByPrimaryKey(long rowId)
1134 throws NoSuchRowException, SystemException {
1135 ExpandoRow expandoRow = fetchByPrimaryKey(rowId);
1136
1137 if (expandoRow == null) {
1138 if (_log.isWarnEnabled()) {
1139 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + rowId);
1140 }
1141
1142 throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1143 rowId);
1144 }
1145
1146 return expandoRow;
1147 }
1148
1149
1156 @Override
1157 public ExpandoRow fetchByPrimaryKey(Serializable primaryKey)
1158 throws SystemException {
1159 return fetchByPrimaryKey(((Long)primaryKey).longValue());
1160 }
1161
1162
1169 public ExpandoRow fetchByPrimaryKey(long rowId) throws SystemException {
1170 ExpandoRow expandoRow = (ExpandoRow)EntityCacheUtil.getResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1171 ExpandoRowImpl.class, rowId);
1172
1173 if (expandoRow == _nullExpandoRow) {
1174 return null;
1175 }
1176
1177 if (expandoRow == null) {
1178 Session session = null;
1179
1180 try {
1181 session = openSession();
1182
1183 expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
1184 Long.valueOf(rowId));
1185
1186 if (expandoRow != null) {
1187 cacheResult(expandoRow);
1188 }
1189 else {
1190 EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1191 ExpandoRowImpl.class, rowId, _nullExpandoRow);
1192 }
1193 }
1194 catch (Exception e) {
1195 EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
1196 ExpandoRowImpl.class, rowId);
1197
1198 throw processException(e);
1199 }
1200 finally {
1201 closeSession(session);
1202 }
1203 }
1204
1205 return expandoRow;
1206 }
1207
1208
1214 public List<ExpandoRow> findAll() throws SystemException {
1215 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1216 }
1217
1218
1230 public List<ExpandoRow> findAll(int start, int end)
1231 throws SystemException {
1232 return findAll(start, end, null);
1233 }
1234
1235
1248 public List<ExpandoRow> findAll(int start, int end,
1249 OrderByComparator orderByComparator) throws SystemException {
1250 boolean pagination = true;
1251 FinderPath finderPath = null;
1252 Object[] finderArgs = null;
1253
1254 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1255 (orderByComparator == null)) {
1256 pagination = false;
1257 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1258 finderArgs = FINDER_ARGS_EMPTY;
1259 }
1260 else {
1261 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1262 finderArgs = new Object[] { start, end, orderByComparator };
1263 }
1264
1265 List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(finderPath,
1266 finderArgs, this);
1267
1268 if (list == null) {
1269 StringBundler query = null;
1270 String sql = null;
1271
1272 if (orderByComparator != null) {
1273 query = new StringBundler(2 +
1274 (orderByComparator.getOrderByFields().length * 3));
1275
1276 query.append(_SQL_SELECT_EXPANDOROW);
1277
1278 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1279 orderByComparator);
1280
1281 sql = query.toString();
1282 }
1283 else {
1284 sql = _SQL_SELECT_EXPANDOROW;
1285
1286 if (pagination) {
1287 sql = sql.concat(ExpandoRowModelImpl.ORDER_BY_JPQL);
1288 }
1289 }
1290
1291 Session session = null;
1292
1293 try {
1294 session = openSession();
1295
1296 Query q = session.createQuery(sql);
1297
1298 if (!pagination) {
1299 list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
1300 start, end, false);
1301
1302 Collections.sort(list);
1303
1304 list = new UnmodifiableList<ExpandoRow>(list);
1305 }
1306 else {
1307 list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
1308 start, end);
1309 }
1310
1311 cacheResult(list);
1312
1313 FinderCacheUtil.putResult(finderPath, finderArgs, list);
1314 }
1315 catch (Exception e) {
1316 FinderCacheUtil.removeResult(finderPath, finderArgs);
1317
1318 throw processException(e);
1319 }
1320 finally {
1321 closeSession(session);
1322 }
1323 }
1324
1325 return list;
1326 }
1327
1328
1333 public void removeAll() throws SystemException {
1334 for (ExpandoRow expandoRow : findAll()) {
1335 remove(expandoRow);
1336 }
1337 }
1338
1339
1345 public int countAll() throws SystemException {
1346 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1347 FINDER_ARGS_EMPTY, this);
1348
1349 if (count == null) {
1350 Session session = null;
1351
1352 try {
1353 session = openSession();
1354
1355 Query q = session.createQuery(_SQL_COUNT_EXPANDOROW);
1356
1357 count = (Long)q.uniqueResult();
1358
1359 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1360 FINDER_ARGS_EMPTY, count);
1361 }
1362 catch (Exception e) {
1363 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1364 FINDER_ARGS_EMPTY);
1365
1366 throw processException(e);
1367 }
1368 finally {
1369 closeSession(session);
1370 }
1371 }
1372
1373 return count.intValue();
1374 }
1375
1376
1379 public void afterPropertiesSet() {
1380 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1381 com.liferay.portal.util.PropsUtil.get(
1382 "value.object.listener.com.liferay.portlet.expando.model.ExpandoRow")));
1383
1384 if (listenerClassNames.length > 0) {
1385 try {
1386 List<ModelListener<ExpandoRow>> listenersList = new ArrayList<ModelListener<ExpandoRow>>();
1387
1388 for (String listenerClassName : listenerClassNames) {
1389 listenersList.add((ModelListener<ExpandoRow>)InstanceFactory.newInstance(
1390 listenerClassName));
1391 }
1392
1393 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1394 }
1395 catch (Exception e) {
1396 _log.error(e);
1397 }
1398 }
1399 }
1400
1401 public void destroy() {
1402 EntityCacheUtil.removeCache(ExpandoRowImpl.class.getName());
1403 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1404 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1405 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1406 }
1407
1408 private static final String _SQL_SELECT_EXPANDOROW = "SELECT expandoRow FROM ExpandoRow expandoRow";
1409 private static final String _SQL_SELECT_EXPANDOROW_WHERE = "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ";
1410 private static final String _SQL_COUNT_EXPANDOROW = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow";
1411 private static final String _SQL_COUNT_EXPANDOROW_WHERE = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow WHERE ";
1412 private static final String _ORDER_BY_ENTITY_ALIAS = "expandoRow.";
1413 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoRow exists with the primary key ";
1414 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoRow exists with the key {";
1415 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1416 private static Log _log = LogFactoryUtil.getLog(ExpandoRowPersistenceImpl.class);
1417 private static ExpandoRow _nullExpandoRow = new ExpandoRowImpl() {
1418 @Override
1419 public Object clone() {
1420 return this;
1421 }
1422
1423 @Override
1424 public CacheModel<ExpandoRow> toCacheModel() {
1425 return _nullExpandoRowCacheModel;
1426 }
1427 };
1428
1429 private static CacheModel<ExpandoRow> _nullExpandoRowCacheModel = new CacheModel<ExpandoRow>() {
1430 public ExpandoRow toEntityModel() {
1431 return _nullExpandoRow;
1432 }
1433 };
1434 }