001
014
015 package com.liferay.portal.service.persistence.impl;
016
017 import aQute.bnd.annotation.ProviderType;
018
019 import com.liferay.portal.NoSuchListTypeException;
020 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022 import com.liferay.portal.kernel.dao.orm.FinderPath;
023 import com.liferay.portal.kernel.dao.orm.Query;
024 import com.liferay.portal.kernel.dao.orm.QueryPos;
025 import com.liferay.portal.kernel.dao.orm.QueryUtil;
026 import com.liferay.portal.kernel.dao.orm.Session;
027 import com.liferay.portal.kernel.log.Log;
028 import com.liferay.portal.kernel.log.LogFactoryUtil;
029 import com.liferay.portal.kernel.util.OrderByComparator;
030 import com.liferay.portal.kernel.util.SetUtil;
031 import com.liferay.portal.kernel.util.StringBundler;
032 import com.liferay.portal.kernel.util.StringPool;
033 import com.liferay.portal.kernel.util.StringUtil;
034 import com.liferay.portal.kernel.util.Validator;
035 import com.liferay.portal.model.CacheModel;
036 import com.liferay.portal.model.ListType;
037 import com.liferay.portal.model.MVCCModel;
038 import com.liferay.portal.model.impl.ListTypeImpl;
039 import com.liferay.portal.model.impl.ListTypeModelImpl;
040 import com.liferay.portal.service.persistence.ListTypePersistence;
041
042 import java.io.Serializable;
043
044 import java.util.Collections;
045 import java.util.HashMap;
046 import java.util.HashSet;
047 import java.util.Iterator;
048 import java.util.List;
049 import java.util.Map;
050 import java.util.Set;
051
052
064 @ProviderType
065 public class ListTypePersistenceImpl extends BasePersistenceImpl<ListType>
066 implements ListTypePersistence {
067
072 public static final String FINDER_CLASS_NAME_ENTITY = ListTypeImpl.class.getName();
073 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074 ".List1";
075 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
076 ".List2";
077 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
078 ListTypeModelImpl.FINDER_CACHE_ENABLED, ListTypeImpl.class,
079 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
080 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
081 ListTypeModelImpl.FINDER_CACHE_ENABLED, ListTypeImpl.class,
082 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
083 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
084 ListTypeModelImpl.FINDER_CACHE_ENABLED, Long.class,
085 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
086 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_TYPE = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
087 ListTypeModelImpl.FINDER_CACHE_ENABLED, ListTypeImpl.class,
088 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByType",
089 new String[] {
090 String.class.getName(),
091
092 Integer.class.getName(), Integer.class.getName(),
093 OrderByComparator.class.getName()
094 });
095 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
096 ListTypeModelImpl.FINDER_CACHE_ENABLED, ListTypeImpl.class,
097 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByType",
098 new String[] { String.class.getName() },
099 ListTypeModelImpl.TYPE_COLUMN_BITMASK |
100 ListTypeModelImpl.NAME_COLUMN_BITMASK);
101 public static final FinderPath FINDER_PATH_COUNT_BY_TYPE = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
102 ListTypeModelImpl.FINDER_CACHE_ENABLED, Long.class,
103 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByType",
104 new String[] { String.class.getName() });
105
106
112 @Override
113 public List<ListType> findByType(String type) {
114 return findByType(type, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
115 }
116
117
129 @Override
130 public List<ListType> findByType(String type, int start, int end) {
131 return findByType(type, start, end, null);
132 }
133
134
147 @Override
148 public List<ListType> findByType(String type, int start, int end,
149 OrderByComparator<ListType> orderByComparator) {
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_TYPE;
158 finderArgs = new Object[] { type };
159 }
160 else {
161 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_TYPE;
162 finderArgs = new Object[] { type, start, end, orderByComparator };
163 }
164
165 List<ListType> list = (List<ListType>)FinderCacheUtil.getResult(finderPath,
166 finderArgs, this);
167
168 if ((list != null) && !list.isEmpty()) {
169 for (ListType listType : list) {
170 if (!Validator.equals(type, listType.getType())) {
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_LISTTYPE_WHERE);
190
191 boolean bindType = false;
192
193 if (type == null) {
194 query.append(_FINDER_COLUMN_TYPE_TYPE_1);
195 }
196 else if (type.equals(StringPool.BLANK)) {
197 query.append(_FINDER_COLUMN_TYPE_TYPE_3);
198 }
199 else {
200 bindType = true;
201
202 query.append(_FINDER_COLUMN_TYPE_TYPE_2);
203 }
204
205 if (orderByComparator != null) {
206 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
207 orderByComparator);
208 }
209 else
210 if (pagination) {
211 query.append(ListTypeModelImpl.ORDER_BY_JPQL);
212 }
213
214 String sql = query.toString();
215
216 Session session = null;
217
218 try {
219 session = openSession();
220
221 Query q = session.createQuery(sql);
222
223 QueryPos qPos = QueryPos.getInstance(q);
224
225 if (bindType) {
226 qPos.add(type);
227 }
228
229 if (!pagination) {
230 list = (List<ListType>)QueryUtil.list(q, getDialect(),
231 start, end, false);
232
233 Collections.sort(list);
234
235 list = Collections.unmodifiableList(list);
236 }
237 else {
238 list = (List<ListType>)QueryUtil.list(q, getDialect(),
239 start, end);
240 }
241
242 cacheResult(list);
243
244 FinderCacheUtil.putResult(finderPath, finderArgs, list);
245 }
246 catch (Exception e) {
247 FinderCacheUtil.removeResult(finderPath, finderArgs);
248
249 throw processException(e);
250 }
251 finally {
252 closeSession(session);
253 }
254 }
255
256 return list;
257 }
258
259
267 @Override
268 public ListType findByType_First(String type,
269 OrderByComparator<ListType> orderByComparator)
270 throws NoSuchListTypeException {
271 ListType listType = fetchByType_First(type, orderByComparator);
272
273 if (listType != null) {
274 return listType;
275 }
276
277 StringBundler msg = new StringBundler(4);
278
279 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
280
281 msg.append("type=");
282 msg.append(type);
283
284 msg.append(StringPool.CLOSE_CURLY_BRACE);
285
286 throw new NoSuchListTypeException(msg.toString());
287 }
288
289
296 @Override
297 public ListType fetchByType_First(String type,
298 OrderByComparator<ListType> orderByComparator) {
299 List<ListType> list = findByType(type, 0, 1, orderByComparator);
300
301 if (!list.isEmpty()) {
302 return list.get(0);
303 }
304
305 return null;
306 }
307
308
316 @Override
317 public ListType findByType_Last(String type,
318 OrderByComparator<ListType> orderByComparator)
319 throws NoSuchListTypeException {
320 ListType listType = fetchByType_Last(type, orderByComparator);
321
322 if (listType != null) {
323 return listType;
324 }
325
326 StringBundler msg = new StringBundler(4);
327
328 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
329
330 msg.append("type=");
331 msg.append(type);
332
333 msg.append(StringPool.CLOSE_CURLY_BRACE);
334
335 throw new NoSuchListTypeException(msg.toString());
336 }
337
338
345 @Override
346 public ListType fetchByType_Last(String type,
347 OrderByComparator<ListType> orderByComparator) {
348 int count = countByType(type);
349
350 if (count == 0) {
351 return null;
352 }
353
354 List<ListType> list = findByType(type, count - 1, count,
355 orderByComparator);
356
357 if (!list.isEmpty()) {
358 return list.get(0);
359 }
360
361 return null;
362 }
363
364
373 @Override
374 public ListType[] findByType_PrevAndNext(long listTypeId, String type,
375 OrderByComparator<ListType> orderByComparator)
376 throws NoSuchListTypeException {
377 ListType listType = findByPrimaryKey(listTypeId);
378
379 Session session = null;
380
381 try {
382 session = openSession();
383
384 ListType[] array = new ListTypeImpl[3];
385
386 array[0] = getByType_PrevAndNext(session, listType, type,
387 orderByComparator, true);
388
389 array[1] = listType;
390
391 array[2] = getByType_PrevAndNext(session, listType, type,
392 orderByComparator, false);
393
394 return array;
395 }
396 catch (Exception e) {
397 throw processException(e);
398 }
399 finally {
400 closeSession(session);
401 }
402 }
403
404 protected ListType getByType_PrevAndNext(Session session,
405 ListType listType, String type,
406 OrderByComparator<ListType> orderByComparator, boolean previous) {
407 StringBundler query = null;
408
409 if (orderByComparator != null) {
410 query = new StringBundler(6 +
411 (orderByComparator.getOrderByFields().length * 6));
412 }
413 else {
414 query = new StringBundler(3);
415 }
416
417 query.append(_SQL_SELECT_LISTTYPE_WHERE);
418
419 boolean bindType = false;
420
421 if (type == null) {
422 query.append(_FINDER_COLUMN_TYPE_TYPE_1);
423 }
424 else if (type.equals(StringPool.BLANK)) {
425 query.append(_FINDER_COLUMN_TYPE_TYPE_3);
426 }
427 else {
428 bindType = true;
429
430 query.append(_FINDER_COLUMN_TYPE_TYPE_2);
431 }
432
433 if (orderByComparator != null) {
434 String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
435
436 if (orderByConditionFields.length > 0) {
437 query.append(WHERE_AND);
438 }
439
440 for (int i = 0; i < orderByConditionFields.length; i++) {
441 query.append(_ORDER_BY_ENTITY_ALIAS);
442 query.append(orderByConditionFields[i]);
443
444 if ((i + 1) < orderByConditionFields.length) {
445 if (orderByComparator.isAscending() ^ previous) {
446 query.append(WHERE_GREATER_THAN_HAS_NEXT);
447 }
448 else {
449 query.append(WHERE_LESSER_THAN_HAS_NEXT);
450 }
451 }
452 else {
453 if (orderByComparator.isAscending() ^ previous) {
454 query.append(WHERE_GREATER_THAN);
455 }
456 else {
457 query.append(WHERE_LESSER_THAN);
458 }
459 }
460 }
461
462 query.append(ORDER_BY_CLAUSE);
463
464 String[] orderByFields = orderByComparator.getOrderByFields();
465
466 for (int i = 0; i < orderByFields.length; i++) {
467 query.append(_ORDER_BY_ENTITY_ALIAS);
468 query.append(orderByFields[i]);
469
470 if ((i + 1) < orderByFields.length) {
471 if (orderByComparator.isAscending() ^ previous) {
472 query.append(ORDER_BY_ASC_HAS_NEXT);
473 }
474 else {
475 query.append(ORDER_BY_DESC_HAS_NEXT);
476 }
477 }
478 else {
479 if (orderByComparator.isAscending() ^ previous) {
480 query.append(ORDER_BY_ASC);
481 }
482 else {
483 query.append(ORDER_BY_DESC);
484 }
485 }
486 }
487 }
488 else {
489 query.append(ListTypeModelImpl.ORDER_BY_JPQL);
490 }
491
492 String sql = query.toString();
493
494 Query q = session.createQuery(sql);
495
496 q.setFirstResult(0);
497 q.setMaxResults(2);
498
499 QueryPos qPos = QueryPos.getInstance(q);
500
501 if (bindType) {
502 qPos.add(type);
503 }
504
505 if (orderByComparator != null) {
506 Object[] values = orderByComparator.getOrderByConditionValues(listType);
507
508 for (Object value : values) {
509 qPos.add(value);
510 }
511 }
512
513 List<ListType> list = q.list();
514
515 if (list.size() == 2) {
516 return list.get(1);
517 }
518 else {
519 return null;
520 }
521 }
522
523
528 @Override
529 public void removeByType(String type) {
530 for (ListType listType : findByType(type, QueryUtil.ALL_POS,
531 QueryUtil.ALL_POS, null)) {
532 remove(listType);
533 }
534 }
535
536
542 @Override
543 public int countByType(String type) {
544 FinderPath finderPath = FINDER_PATH_COUNT_BY_TYPE;
545
546 Object[] finderArgs = new Object[] { type };
547
548 Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
549 this);
550
551 if (count == null) {
552 StringBundler query = new StringBundler(2);
553
554 query.append(_SQL_COUNT_LISTTYPE_WHERE);
555
556 boolean bindType = false;
557
558 if (type == null) {
559 query.append(_FINDER_COLUMN_TYPE_TYPE_1);
560 }
561 else if (type.equals(StringPool.BLANK)) {
562 query.append(_FINDER_COLUMN_TYPE_TYPE_3);
563 }
564 else {
565 bindType = true;
566
567 query.append(_FINDER_COLUMN_TYPE_TYPE_2);
568 }
569
570 String sql = query.toString();
571
572 Session session = null;
573
574 try {
575 session = openSession();
576
577 Query q = session.createQuery(sql);
578
579 QueryPos qPos = QueryPos.getInstance(q);
580
581 if (bindType) {
582 qPos.add(type);
583 }
584
585 count = (Long)q.uniqueResult();
586
587 FinderCacheUtil.putResult(finderPath, finderArgs, count);
588 }
589 catch (Exception e) {
590 FinderCacheUtil.removeResult(finderPath, finderArgs);
591
592 throw processException(e);
593 }
594 finally {
595 closeSession(session);
596 }
597 }
598
599 return count.intValue();
600 }
601
602 private static final String _FINDER_COLUMN_TYPE_TYPE_1 = "listType.type IS NULL";
603 private static final String _FINDER_COLUMN_TYPE_TYPE_2 = "listType.type = ?";
604 private static final String _FINDER_COLUMN_TYPE_TYPE_3 = "(listType.type IS NULL OR listType.type = '')";
605 public static final FinderPath FINDER_PATH_FETCH_BY_N_T = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
606 ListTypeModelImpl.FINDER_CACHE_ENABLED, ListTypeImpl.class,
607 FINDER_CLASS_NAME_ENTITY, "fetchByN_T",
608 new String[] { String.class.getName(), String.class.getName() },
609 ListTypeModelImpl.NAME_COLUMN_BITMASK |
610 ListTypeModelImpl.TYPE_COLUMN_BITMASK);
611 public static final FinderPath FINDER_PATH_COUNT_BY_N_T = new FinderPath(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
612 ListTypeModelImpl.FINDER_CACHE_ENABLED, Long.class,
613 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByN_T",
614 new String[] { String.class.getName(), String.class.getName() });
615
616
624 @Override
625 public ListType findByN_T(String name, String type)
626 throws NoSuchListTypeException {
627 ListType listType = fetchByN_T(name, type);
628
629 if (listType == null) {
630 StringBundler msg = new StringBundler(6);
631
632 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
633
634 msg.append("name=");
635 msg.append(name);
636
637 msg.append(", type=");
638 msg.append(type);
639
640 msg.append(StringPool.CLOSE_CURLY_BRACE);
641
642 if (_log.isWarnEnabled()) {
643 _log.warn(msg.toString());
644 }
645
646 throw new NoSuchListTypeException(msg.toString());
647 }
648
649 return listType;
650 }
651
652
659 @Override
660 public ListType fetchByN_T(String name, String type) {
661 return fetchByN_T(name, type, true);
662 }
663
664
672 @Override
673 public ListType fetchByN_T(String name, String type,
674 boolean retrieveFromCache) {
675 Object[] finderArgs = new Object[] { name, type };
676
677 Object result = null;
678
679 if (retrieveFromCache) {
680 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_N_T,
681 finderArgs, this);
682 }
683
684 if (result instanceof ListType) {
685 ListType listType = (ListType)result;
686
687 if (!Validator.equals(name, listType.getName()) ||
688 !Validator.equals(type, listType.getType())) {
689 result = null;
690 }
691 }
692
693 if (result == null) {
694 StringBundler query = new StringBundler(4);
695
696 query.append(_SQL_SELECT_LISTTYPE_WHERE);
697
698 boolean bindName = false;
699
700 if (name == null) {
701 query.append(_FINDER_COLUMN_N_T_NAME_1);
702 }
703 else if (name.equals(StringPool.BLANK)) {
704 query.append(_FINDER_COLUMN_N_T_NAME_3);
705 }
706 else {
707 bindName = true;
708
709 query.append(_FINDER_COLUMN_N_T_NAME_2);
710 }
711
712 boolean bindType = false;
713
714 if (type == null) {
715 query.append(_FINDER_COLUMN_N_T_TYPE_1);
716 }
717 else if (type.equals(StringPool.BLANK)) {
718 query.append(_FINDER_COLUMN_N_T_TYPE_3);
719 }
720 else {
721 bindType = true;
722
723 query.append(_FINDER_COLUMN_N_T_TYPE_2);
724 }
725
726 String sql = query.toString();
727
728 Session session = null;
729
730 try {
731 session = openSession();
732
733 Query q = session.createQuery(sql);
734
735 QueryPos qPos = QueryPos.getInstance(q);
736
737 if (bindName) {
738 qPos.add(name);
739 }
740
741 if (bindType) {
742 qPos.add(type);
743 }
744
745 List<ListType> list = q.list();
746
747 if (list.isEmpty()) {
748 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
749 finderArgs, list);
750 }
751 else {
752 if ((list.size() > 1) && _log.isWarnEnabled()) {
753 _log.warn(
754 "ListTypePersistenceImpl.fetchByN_T(String, String, boolean) with parameters (" +
755 StringUtil.merge(finderArgs) +
756 ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
757 }
758
759 ListType listType = list.get(0);
760
761 result = listType;
762
763 cacheResult(listType);
764
765 if ((listType.getName() == null) ||
766 !listType.getName().equals(name) ||
767 (listType.getType() == null) ||
768 !listType.getType().equals(type)) {
769 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
770 finderArgs, listType);
771 }
772 }
773 }
774 catch (Exception e) {
775 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_T,
776 finderArgs);
777
778 throw processException(e);
779 }
780 finally {
781 closeSession(session);
782 }
783 }
784
785 if (result instanceof List<?>) {
786 return null;
787 }
788 else {
789 return (ListType)result;
790 }
791 }
792
793
800 @Override
801 public ListType removeByN_T(String name, String type)
802 throws NoSuchListTypeException {
803 ListType listType = findByN_T(name, type);
804
805 return remove(listType);
806 }
807
808
815 @Override
816 public int countByN_T(String name, String type) {
817 FinderPath finderPath = FINDER_PATH_COUNT_BY_N_T;
818
819 Object[] finderArgs = new Object[] { name, type };
820
821 Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
822 this);
823
824 if (count == null) {
825 StringBundler query = new StringBundler(3);
826
827 query.append(_SQL_COUNT_LISTTYPE_WHERE);
828
829 boolean bindName = false;
830
831 if (name == null) {
832 query.append(_FINDER_COLUMN_N_T_NAME_1);
833 }
834 else if (name.equals(StringPool.BLANK)) {
835 query.append(_FINDER_COLUMN_N_T_NAME_3);
836 }
837 else {
838 bindName = true;
839
840 query.append(_FINDER_COLUMN_N_T_NAME_2);
841 }
842
843 boolean bindType = false;
844
845 if (type == null) {
846 query.append(_FINDER_COLUMN_N_T_TYPE_1);
847 }
848 else if (type.equals(StringPool.BLANK)) {
849 query.append(_FINDER_COLUMN_N_T_TYPE_3);
850 }
851 else {
852 bindType = true;
853
854 query.append(_FINDER_COLUMN_N_T_TYPE_2);
855 }
856
857 String sql = query.toString();
858
859 Session session = null;
860
861 try {
862 session = openSession();
863
864 Query q = session.createQuery(sql);
865
866 QueryPos qPos = QueryPos.getInstance(q);
867
868 if (bindName) {
869 qPos.add(name);
870 }
871
872 if (bindType) {
873 qPos.add(type);
874 }
875
876 count = (Long)q.uniqueResult();
877
878 FinderCacheUtil.putResult(finderPath, finderArgs, count);
879 }
880 catch (Exception e) {
881 FinderCacheUtil.removeResult(finderPath, finderArgs);
882
883 throw processException(e);
884 }
885 finally {
886 closeSession(session);
887 }
888 }
889
890 return count.intValue();
891 }
892
893 private static final String _FINDER_COLUMN_N_T_NAME_1 = "listType.name IS NULL AND ";
894 private static final String _FINDER_COLUMN_N_T_NAME_2 = "listType.name = ? AND ";
895 private static final String _FINDER_COLUMN_N_T_NAME_3 = "(listType.name IS NULL OR listType.name = '') AND ";
896 private static final String _FINDER_COLUMN_N_T_TYPE_1 = "listType.type IS NULL";
897 private static final String _FINDER_COLUMN_N_T_TYPE_2 = "listType.type = ?";
898 private static final String _FINDER_COLUMN_N_T_TYPE_3 = "(listType.type IS NULL OR listType.type = '')";
899
900 public ListTypePersistenceImpl() {
901 setModelClass(ListType.class);
902 }
903
904
909 @Override
910 public void cacheResult(ListType listType) {
911 EntityCacheUtil.putResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
912 ListTypeImpl.class, listType.getPrimaryKey(), listType);
913
914 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T,
915 new Object[] { listType.getName(), listType.getType() }, listType);
916
917 listType.resetOriginalValues();
918 }
919
920
925 @Override
926 public void cacheResult(List<ListType> listTypes) {
927 for (ListType listType : listTypes) {
928 if (EntityCacheUtil.getResult(
929 ListTypeModelImpl.ENTITY_CACHE_ENABLED,
930 ListTypeImpl.class, listType.getPrimaryKey()) == null) {
931 cacheResult(listType);
932 }
933 else {
934 listType.resetOriginalValues();
935 }
936 }
937 }
938
939
946 @Override
947 public void clearCache() {
948 EntityCacheUtil.clearCache(ListTypeImpl.class);
949
950 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
951 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
952 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
953 }
954
955
962 @Override
963 public void clearCache(ListType listType) {
964 EntityCacheUtil.removeResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
965 ListTypeImpl.class, listType.getPrimaryKey());
966
967 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
968 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
969
970 clearUniqueFindersCache(listType);
971 }
972
973 @Override
974 public void clearCache(List<ListType> listTypes) {
975 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
976 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
977
978 for (ListType listType : listTypes) {
979 EntityCacheUtil.removeResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
980 ListTypeImpl.class, listType.getPrimaryKey());
981
982 clearUniqueFindersCache(listType);
983 }
984 }
985
986 protected void cacheUniqueFindersCache(ListType listType) {
987 if (listType.isNew()) {
988 Object[] args = new Object[] { listType.getName(), listType.getType() };
989
990 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_N_T, args,
991 Long.valueOf(1));
992 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T, args, listType);
993 }
994 else {
995 ListTypeModelImpl listTypeModelImpl = (ListTypeModelImpl)listType;
996
997 if ((listTypeModelImpl.getColumnBitmask() &
998 FINDER_PATH_FETCH_BY_N_T.getColumnBitmask()) != 0) {
999 Object[] args = new Object[] {
1000 listType.getName(), listType.getType()
1001 };
1002
1003 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_N_T, args,
1004 Long.valueOf(1));
1005 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_N_T, args,
1006 listType);
1007 }
1008 }
1009 }
1010
1011 protected void clearUniqueFindersCache(ListType listType) {
1012 ListTypeModelImpl listTypeModelImpl = (ListTypeModelImpl)listType;
1013
1014 Object[] args = new Object[] { listType.getName(), listType.getType() };
1015
1016 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_N_T, args);
1017 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_T, args);
1018
1019 if ((listTypeModelImpl.getColumnBitmask() &
1020 FINDER_PATH_FETCH_BY_N_T.getColumnBitmask()) != 0) {
1021 args = new Object[] {
1022 listTypeModelImpl.getOriginalName(),
1023 listTypeModelImpl.getOriginalType()
1024 };
1025
1026 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_N_T, args);
1027 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_N_T, args);
1028 }
1029 }
1030
1031
1037 @Override
1038 public ListType create(long listTypeId) {
1039 ListType listType = new ListTypeImpl();
1040
1041 listType.setNew(true);
1042 listType.setPrimaryKey(listTypeId);
1043
1044 return listType;
1045 }
1046
1047
1054 @Override
1055 public ListType remove(long listTypeId) throws NoSuchListTypeException {
1056 return remove((Serializable)listTypeId);
1057 }
1058
1059
1066 @Override
1067 public ListType remove(Serializable primaryKey)
1068 throws NoSuchListTypeException {
1069 Session session = null;
1070
1071 try {
1072 session = openSession();
1073
1074 ListType listType = (ListType)session.get(ListTypeImpl.class,
1075 primaryKey);
1076
1077 if (listType == null) {
1078 if (_log.isWarnEnabled()) {
1079 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1080 }
1081
1082 throw new NoSuchListTypeException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1083 primaryKey);
1084 }
1085
1086 return remove(listType);
1087 }
1088 catch (NoSuchListTypeException nsee) {
1089 throw nsee;
1090 }
1091 catch (Exception e) {
1092 throw processException(e);
1093 }
1094 finally {
1095 closeSession(session);
1096 }
1097 }
1098
1099 @Override
1100 protected ListType removeImpl(ListType listType) {
1101 listType = toUnwrappedModel(listType);
1102
1103 Session session = null;
1104
1105 try {
1106 session = openSession();
1107
1108 if (!session.contains(listType)) {
1109 listType = (ListType)session.get(ListTypeImpl.class,
1110 listType.getPrimaryKeyObj());
1111 }
1112
1113 if (listType != null) {
1114 session.delete(listType);
1115 }
1116 }
1117 catch (Exception e) {
1118 throw processException(e);
1119 }
1120 finally {
1121 closeSession(session);
1122 }
1123
1124 if (listType != null) {
1125 clearCache(listType);
1126 }
1127
1128 return listType;
1129 }
1130
1131 @Override
1132 public ListType updateImpl(ListType listType) {
1133 listType = toUnwrappedModel(listType);
1134
1135 boolean isNew = listType.isNew();
1136
1137 ListTypeModelImpl listTypeModelImpl = (ListTypeModelImpl)listType;
1138
1139 Session session = null;
1140
1141 try {
1142 session = openSession();
1143
1144 if (listType.isNew()) {
1145 session.save(listType);
1146
1147 listType.setNew(false);
1148 }
1149 else {
1150 session.merge(listType);
1151 }
1152 }
1153 catch (Exception e) {
1154 throw processException(e);
1155 }
1156 finally {
1157 closeSession(session);
1158 }
1159
1160 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1161
1162 if (isNew || !ListTypeModelImpl.COLUMN_BITMASK_ENABLED) {
1163 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1164 }
1165
1166 else {
1167 if ((listTypeModelImpl.getColumnBitmask() &
1168 FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE.getColumnBitmask()) != 0) {
1169 Object[] args = new Object[] { listTypeModelImpl.getOriginalType() };
1170
1171 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TYPE, args);
1172 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE,
1173 args);
1174
1175 args = new Object[] { listTypeModelImpl.getType() };
1176
1177 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TYPE, args);
1178 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TYPE,
1179 args);
1180 }
1181 }
1182
1183 EntityCacheUtil.putResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
1184 ListTypeImpl.class, listType.getPrimaryKey(), listType, false);
1185
1186 clearUniqueFindersCache(listType);
1187 cacheUniqueFindersCache(listType);
1188
1189 listType.resetOriginalValues();
1190
1191 return listType;
1192 }
1193
1194 protected ListType toUnwrappedModel(ListType listType) {
1195 if (listType instanceof ListTypeImpl) {
1196 return listType;
1197 }
1198
1199 ListTypeImpl listTypeImpl = new ListTypeImpl();
1200
1201 listTypeImpl.setNew(listType.isNew());
1202 listTypeImpl.setPrimaryKey(listType.getPrimaryKey());
1203
1204 listTypeImpl.setMvccVersion(listType.getMvccVersion());
1205 listTypeImpl.setListTypeId(listType.getListTypeId());
1206 listTypeImpl.setName(listType.getName());
1207 listTypeImpl.setType(listType.getType());
1208
1209 return listTypeImpl;
1210 }
1211
1212
1219 @Override
1220 public ListType findByPrimaryKey(Serializable primaryKey)
1221 throws NoSuchListTypeException {
1222 ListType listType = fetchByPrimaryKey(primaryKey);
1223
1224 if (listType == null) {
1225 if (_log.isWarnEnabled()) {
1226 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1227 }
1228
1229 throw new NoSuchListTypeException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1230 primaryKey);
1231 }
1232
1233 return listType;
1234 }
1235
1236
1243 @Override
1244 public ListType findByPrimaryKey(long listTypeId)
1245 throws NoSuchListTypeException {
1246 return findByPrimaryKey((Serializable)listTypeId);
1247 }
1248
1249
1255 @Override
1256 public ListType fetchByPrimaryKey(Serializable primaryKey) {
1257 ListType listType = (ListType)EntityCacheUtil.getResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
1258 ListTypeImpl.class, primaryKey);
1259
1260 if (listType == _nullListType) {
1261 return null;
1262 }
1263
1264 if (listType == null) {
1265 Session session = null;
1266
1267 try {
1268 session = openSession();
1269
1270 listType = (ListType)session.get(ListTypeImpl.class, primaryKey);
1271
1272 if (listType != null) {
1273 cacheResult(listType);
1274 }
1275 else {
1276 EntityCacheUtil.putResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
1277 ListTypeImpl.class, primaryKey, _nullListType);
1278 }
1279 }
1280 catch (Exception e) {
1281 EntityCacheUtil.removeResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
1282 ListTypeImpl.class, primaryKey);
1283
1284 throw processException(e);
1285 }
1286 finally {
1287 closeSession(session);
1288 }
1289 }
1290
1291 return listType;
1292 }
1293
1294
1300 @Override
1301 public ListType fetchByPrimaryKey(long listTypeId) {
1302 return fetchByPrimaryKey((Serializable)listTypeId);
1303 }
1304
1305 @Override
1306 public Map<Serializable, ListType> fetchByPrimaryKeys(
1307 Set<Serializable> primaryKeys) {
1308 if (primaryKeys.isEmpty()) {
1309 return Collections.emptyMap();
1310 }
1311
1312 Map<Serializable, ListType> map = new HashMap<Serializable, ListType>();
1313
1314 if (primaryKeys.size() == 1) {
1315 Iterator<Serializable> iterator = primaryKeys.iterator();
1316
1317 Serializable primaryKey = iterator.next();
1318
1319 ListType listType = fetchByPrimaryKey(primaryKey);
1320
1321 if (listType != null) {
1322 map.put(primaryKey, listType);
1323 }
1324
1325 return map;
1326 }
1327
1328 Set<Serializable> uncachedPrimaryKeys = null;
1329
1330 for (Serializable primaryKey : primaryKeys) {
1331 ListType listType = (ListType)EntityCacheUtil.getResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
1332 ListTypeImpl.class, primaryKey);
1333
1334 if (listType == null) {
1335 if (uncachedPrimaryKeys == null) {
1336 uncachedPrimaryKeys = new HashSet<Serializable>();
1337 }
1338
1339 uncachedPrimaryKeys.add(primaryKey);
1340 }
1341 else {
1342 map.put(primaryKey, listType);
1343 }
1344 }
1345
1346 if (uncachedPrimaryKeys == null) {
1347 return map;
1348 }
1349
1350 StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
1351 1);
1352
1353 query.append(_SQL_SELECT_LISTTYPE_WHERE_PKS_IN);
1354
1355 for (Serializable primaryKey : uncachedPrimaryKeys) {
1356 query.append(String.valueOf(primaryKey));
1357
1358 query.append(StringPool.COMMA);
1359 }
1360
1361 query.setIndex(query.index() - 1);
1362
1363 query.append(StringPool.CLOSE_PARENTHESIS);
1364
1365 String sql = query.toString();
1366
1367 Session session = null;
1368
1369 try {
1370 session = openSession();
1371
1372 Query q = session.createQuery(sql);
1373
1374 for (ListType listType : (List<ListType>)q.list()) {
1375 map.put(listType.getPrimaryKeyObj(), listType);
1376
1377 cacheResult(listType);
1378
1379 uncachedPrimaryKeys.remove(listType.getPrimaryKeyObj());
1380 }
1381
1382 for (Serializable primaryKey : uncachedPrimaryKeys) {
1383 EntityCacheUtil.putResult(ListTypeModelImpl.ENTITY_CACHE_ENABLED,
1384 ListTypeImpl.class, primaryKey, _nullListType);
1385 }
1386 }
1387 catch (Exception e) {
1388 throw processException(e);
1389 }
1390 finally {
1391 closeSession(session);
1392 }
1393
1394 return map;
1395 }
1396
1397
1402 @Override
1403 public List<ListType> findAll() {
1404 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1405 }
1406
1407
1418 @Override
1419 public List<ListType> findAll(int start, int end) {
1420 return findAll(start, end, null);
1421 }
1422
1423
1435 @Override
1436 public List<ListType> findAll(int start, int end,
1437 OrderByComparator<ListType> orderByComparator) {
1438 boolean pagination = true;
1439 FinderPath finderPath = null;
1440 Object[] finderArgs = null;
1441
1442 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1443 (orderByComparator == null)) {
1444 pagination = false;
1445 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1446 finderArgs = FINDER_ARGS_EMPTY;
1447 }
1448 else {
1449 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1450 finderArgs = new Object[] { start, end, orderByComparator };
1451 }
1452
1453 List<ListType> list = (List<ListType>)FinderCacheUtil.getResult(finderPath,
1454 finderArgs, this);
1455
1456 if (list == null) {
1457 StringBundler query = null;
1458 String sql = null;
1459
1460 if (orderByComparator != null) {
1461 query = new StringBundler(2 +
1462 (orderByComparator.getOrderByFields().length * 3));
1463
1464 query.append(_SQL_SELECT_LISTTYPE);
1465
1466 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1467 orderByComparator);
1468
1469 sql = query.toString();
1470 }
1471 else {
1472 sql = _SQL_SELECT_LISTTYPE;
1473
1474 if (pagination) {
1475 sql = sql.concat(ListTypeModelImpl.ORDER_BY_JPQL);
1476 }
1477 }
1478
1479 Session session = null;
1480
1481 try {
1482 session = openSession();
1483
1484 Query q = session.createQuery(sql);
1485
1486 if (!pagination) {
1487 list = (List<ListType>)QueryUtil.list(q, getDialect(),
1488 start, end, false);
1489
1490 Collections.sort(list);
1491
1492 list = Collections.unmodifiableList(list);
1493 }
1494 else {
1495 list = (List<ListType>)QueryUtil.list(q, getDialect(),
1496 start, end);
1497 }
1498
1499 cacheResult(list);
1500
1501 FinderCacheUtil.putResult(finderPath, finderArgs, list);
1502 }
1503 catch (Exception e) {
1504 FinderCacheUtil.removeResult(finderPath, finderArgs);
1505
1506 throw processException(e);
1507 }
1508 finally {
1509 closeSession(session);
1510 }
1511 }
1512
1513 return list;
1514 }
1515
1516
1520 @Override
1521 public void removeAll() {
1522 for (ListType listType : findAll()) {
1523 remove(listType);
1524 }
1525 }
1526
1527
1532 @Override
1533 public int countAll() {
1534 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1535 FINDER_ARGS_EMPTY, this);
1536
1537 if (count == null) {
1538 Session session = null;
1539
1540 try {
1541 session = openSession();
1542
1543 Query q = session.createQuery(_SQL_COUNT_LISTTYPE);
1544
1545 count = (Long)q.uniqueResult();
1546
1547 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1548 FINDER_ARGS_EMPTY, count);
1549 }
1550 catch (Exception e) {
1551 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1552 FINDER_ARGS_EMPTY);
1553
1554 throw processException(e);
1555 }
1556 finally {
1557 closeSession(session);
1558 }
1559 }
1560
1561 return count.intValue();
1562 }
1563
1564 @Override
1565 protected Set<String> getBadColumnNames() {
1566 return _badColumnNames;
1567 }
1568
1569
1572 public void afterPropertiesSet() {
1573 }
1574
1575 public void destroy() {
1576 EntityCacheUtil.removeCache(ListTypeImpl.class.getName());
1577 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1578 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1579 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1580 }
1581
1582 private static final String _SQL_SELECT_LISTTYPE = "SELECT listType FROM ListType listType";
1583 private static final String _SQL_SELECT_LISTTYPE_WHERE_PKS_IN = "SELECT listType FROM ListType listType WHERE listTypeId IN (";
1584 private static final String _SQL_SELECT_LISTTYPE_WHERE = "SELECT listType FROM ListType listType WHERE ";
1585 private static final String _SQL_COUNT_LISTTYPE = "SELECT COUNT(listType) FROM ListType listType";
1586 private static final String _SQL_COUNT_LISTTYPE_WHERE = "SELECT COUNT(listType) FROM ListType listType WHERE ";
1587 private static final String _ORDER_BY_ENTITY_ALIAS = "listType.";
1588 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ListType exists with the primary key ";
1589 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ListType exists with the key {";
1590 private static final Log _log = LogFactoryUtil.getLog(ListTypePersistenceImpl.class);
1591 private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
1592 "type"
1593 });
1594 private static final ListType _nullListType = new ListTypeImpl() {
1595 @Override
1596 public Object clone() {
1597 return this;
1598 }
1599
1600 @Override
1601 public CacheModel<ListType> toCacheModel() {
1602 return _nullListTypeCacheModel;
1603 }
1604 };
1605
1606 private static final CacheModel<ListType> _nullListTypeCacheModel = new NullCacheModel();
1607
1608 private static class NullCacheModel implements CacheModel<ListType>,
1609 MVCCModel {
1610 @Override
1611 public long getMvccVersion() {
1612 return -1;
1613 }
1614
1615 @Override
1616 public void setMvccVersion(long mvccVersion) {
1617 }
1618
1619 @Override
1620 public ListType toEntityModel() {
1621 return _nullListType;
1622 }
1623 }
1624 }