001
014
015 package com.liferay.portal.service.persistence;
016
017 import com.liferay.portal.NoSuchModelException;
018 import com.liferay.portal.NoSuchResourceException;
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.model.CacheModel;
038 import com.liferay.portal.model.ModelListener;
039 import com.liferay.portal.model.Resource;
040 import com.liferay.portal.model.impl.ResourceImpl;
041 import com.liferay.portal.model.impl.ResourceModelImpl;
042 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
043
044 import java.io.Serializable;
045
046 import java.util.ArrayList;
047 import java.util.Collections;
048 import java.util.List;
049
050
062 public class ResourcePersistenceImpl extends BasePersistenceImpl<Resource>
063 implements ResourcePersistence {
064
069 public static final String FINDER_CLASS_NAME_ENTITY = ResourceImpl.class.getName();
070 public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
071 ".List1";
072 public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073 ".List2";
074 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_CODEID = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
075 ResourceModelImpl.FINDER_CACHE_ENABLED, ResourceImpl.class,
076 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCodeId",
077 new String[] {
078 Long.class.getName(),
079
080 "java.lang.Integer", "java.lang.Integer",
081 "com.liferay.portal.kernel.util.OrderByComparator"
082 });
083 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CODEID =
084 new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
085 ResourceModelImpl.FINDER_CACHE_ENABLED, ResourceImpl.class,
086 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCodeId",
087 new String[] { Long.class.getName() },
088 ResourceModelImpl.CODEID_COLUMN_BITMASK);
089 public static final FinderPath FINDER_PATH_COUNT_BY_CODEID = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
090 ResourceModelImpl.FINDER_CACHE_ENABLED, Long.class,
091 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCodeId",
092 new String[] { Long.class.getName() });
093 public static final FinderPath FINDER_PATH_FETCH_BY_C_P = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
094 ResourceModelImpl.FINDER_CACHE_ENABLED, ResourceImpl.class,
095 FINDER_CLASS_NAME_ENTITY, "fetchByC_P",
096 new String[] { Long.class.getName(), String.class.getName() },
097 ResourceModelImpl.CODEID_COLUMN_BITMASK |
098 ResourceModelImpl.PRIMKEY_COLUMN_BITMASK);
099 public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
100 ResourceModelImpl.FINDER_CACHE_ENABLED, Long.class,
101 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_P",
102 new String[] { Long.class.getName(), String.class.getName() });
103 public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
104 ResourceModelImpl.FINDER_CACHE_ENABLED, ResourceImpl.class,
105 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
106 public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
107 ResourceModelImpl.FINDER_CACHE_ENABLED, ResourceImpl.class,
108 FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
109 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
110 ResourceModelImpl.FINDER_CACHE_ENABLED, Long.class,
111 FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
112
113
118 public void cacheResult(Resource resource) {
119 EntityCacheUtil.putResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
120 ResourceImpl.class, resource.getPrimaryKey(), resource);
121
122 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
123 new Object[] {
124 Long.valueOf(resource.getCodeId()),
125
126 resource.getPrimKey()
127 }, resource);
128
129 resource.resetOriginalValues();
130 }
131
132
137 public void cacheResult(List<Resource> resources) {
138 for (Resource resource : resources) {
139 if (EntityCacheUtil.getResult(
140 ResourceModelImpl.ENTITY_CACHE_ENABLED,
141 ResourceImpl.class, resource.getPrimaryKey()) == null) {
142 cacheResult(resource);
143 }
144 else {
145 resource.resetOriginalValues();
146 }
147 }
148 }
149
150
157 @Override
158 public void clearCache() {
159 if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
160 CacheRegistryUtil.clear(ResourceImpl.class.getName());
161 }
162
163 EntityCacheUtil.clearCache(ResourceImpl.class.getName());
164
165 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
166 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
167 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
168 }
169
170
177 @Override
178 public void clearCache(Resource resource) {
179 EntityCacheUtil.removeResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
180 ResourceImpl.class, resource.getPrimaryKey());
181
182 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
183 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
184
185 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
186 new Object[] {
187 Long.valueOf(resource.getCodeId()),
188
189 resource.getPrimKey()
190 });
191 }
192
193
199 public Resource create(long resourceId) {
200 Resource resource = new ResourceImpl();
201
202 resource.setNew(true);
203 resource.setPrimaryKey(resourceId);
204
205 return resource;
206 }
207
208
216 @Override
217 public Resource remove(Serializable primaryKey)
218 throws NoSuchModelException, SystemException {
219 return remove(((Long)primaryKey).longValue());
220 }
221
222
230 public Resource remove(long resourceId)
231 throws NoSuchResourceException, SystemException {
232 Session session = null;
233
234 try {
235 session = openSession();
236
237 Resource resource = (Resource)session.get(ResourceImpl.class,
238 Long.valueOf(resourceId));
239
240 if (resource == null) {
241 if (_log.isWarnEnabled()) {
242 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + resourceId);
243 }
244
245 throw new NoSuchResourceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
246 resourceId);
247 }
248
249 return resourcePersistence.remove(resource);
250 }
251 catch (NoSuchResourceException nsee) {
252 throw nsee;
253 }
254 catch (Exception e) {
255 throw processException(e);
256 }
257 finally {
258 closeSession(session);
259 }
260 }
261
262
269 @Override
270 public Resource remove(Resource resource) throws SystemException {
271 return super.remove(resource);
272 }
273
274 @Override
275 protected Resource removeImpl(Resource resource) throws SystemException {
276 resource = toUnwrappedModel(resource);
277
278 Session session = null;
279
280 try {
281 session = openSession();
282
283 BatchSessionUtil.delete(session, resource);
284 }
285 catch (Exception e) {
286 throw processException(e);
287 }
288 finally {
289 closeSession(session);
290 }
291
292 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
293 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
294
295 ResourceModelImpl resourceModelImpl = (ResourceModelImpl)resource;
296
297 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
298 new Object[] {
299 Long.valueOf(resourceModelImpl.getCodeId()),
300
301 resourceModelImpl.getPrimKey()
302 });
303
304 EntityCacheUtil.removeResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
305 ResourceImpl.class, resource.getPrimaryKey());
306
307 return resource;
308 }
309
310 @Override
311 public Resource updateImpl(com.liferay.portal.model.Resource resource,
312 boolean merge) throws SystemException {
313 resource = toUnwrappedModel(resource);
314
315 boolean isNew = resource.isNew();
316
317 ResourceModelImpl resourceModelImpl = (ResourceModelImpl)resource;
318
319 Session session = null;
320
321 try {
322 session = openSession();
323
324 BatchSessionUtil.update(session, resource, merge);
325
326 resource.setNew(false);
327 }
328 catch (Exception e) {
329 throw processException(e);
330 }
331 finally {
332 closeSession(session);
333 }
334
335 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
336
337 if (isNew || !ResourceModelImpl.COLUMN_BITMASK_ENABLED) {
338 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
339 }
340
341 else {
342 if ((resourceModelImpl.getColumnBitmask() &
343 FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CODEID.getColumnBitmask()) != 0) {
344 Object[] args = new Object[] {
345 Long.valueOf(resourceModelImpl.getOriginalCodeId())
346 };
347
348 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CODEID, args);
349 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CODEID,
350 args);
351
352 args = new Object[] { Long.valueOf(resourceModelImpl.getCodeId()) };
353
354 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_CODEID, args);
355 FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CODEID,
356 args);
357 }
358 }
359
360 EntityCacheUtil.putResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
361 ResourceImpl.class, resource.getPrimaryKey(), resource);
362
363 if (isNew) {
364 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
365 new Object[] {
366 Long.valueOf(resource.getCodeId()),
367
368 resource.getPrimKey()
369 }, resource);
370 }
371 else {
372 if ((resourceModelImpl.getColumnBitmask() &
373 FINDER_PATH_FETCH_BY_C_P.getColumnBitmask()) != 0) {
374 Object[] args = new Object[] {
375 Long.valueOf(resourceModelImpl.getOriginalCodeId()),
376
377 resourceModelImpl.getOriginalPrimKey()
378 };
379
380 FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_P, args);
381 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P, args);
382
383 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
384 new Object[] {
385 Long.valueOf(resource.getCodeId()),
386
387 resource.getPrimKey()
388 }, resource);
389 }
390 }
391
392 return resource;
393 }
394
395 protected Resource toUnwrappedModel(Resource resource) {
396 if (resource instanceof ResourceImpl) {
397 return resource;
398 }
399
400 ResourceImpl resourceImpl = new ResourceImpl();
401
402 resourceImpl.setNew(resource.isNew());
403 resourceImpl.setPrimaryKey(resource.getPrimaryKey());
404
405 resourceImpl.setResourceId(resource.getResourceId());
406 resourceImpl.setCodeId(resource.getCodeId());
407 resourceImpl.setPrimKey(resource.getPrimKey());
408
409 return resourceImpl;
410 }
411
412
420 @Override
421 public Resource findByPrimaryKey(Serializable primaryKey)
422 throws NoSuchModelException, SystemException {
423 return findByPrimaryKey(((Long)primaryKey).longValue());
424 }
425
426
434 public Resource findByPrimaryKey(long resourceId)
435 throws NoSuchResourceException, SystemException {
436 Resource resource = fetchByPrimaryKey(resourceId);
437
438 if (resource == null) {
439 if (_log.isWarnEnabled()) {
440 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + resourceId);
441 }
442
443 throw new NoSuchResourceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
444 resourceId);
445 }
446
447 return resource;
448 }
449
450
457 @Override
458 public Resource fetchByPrimaryKey(Serializable primaryKey)
459 throws SystemException {
460 return fetchByPrimaryKey(((Long)primaryKey).longValue());
461 }
462
463
470 public Resource fetchByPrimaryKey(long resourceId)
471 throws SystemException {
472 Resource resource = (Resource)EntityCacheUtil.getResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
473 ResourceImpl.class, resourceId);
474
475 if (resource == _nullResource) {
476 return null;
477 }
478
479 if (resource == null) {
480 Session session = null;
481
482 boolean hasException = false;
483
484 try {
485 session = openSession();
486
487 resource = (Resource)session.get(ResourceImpl.class,
488 Long.valueOf(resourceId));
489 }
490 catch (Exception e) {
491 hasException = true;
492
493 throw processException(e);
494 }
495 finally {
496 if (resource != null) {
497 cacheResult(resource);
498 }
499 else if (!hasException) {
500 EntityCacheUtil.putResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
501 ResourceImpl.class, resourceId, _nullResource);
502 }
503
504 closeSession(session);
505 }
506 }
507
508 return resource;
509 }
510
511
518 public List<Resource> findByCodeId(long codeId) throws SystemException {
519 return findByCodeId(codeId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
520 }
521
522
535 public List<Resource> findByCodeId(long codeId, int start, int end)
536 throws SystemException {
537 return findByCodeId(codeId, start, end, null);
538 }
539
540
554 public List<Resource> findByCodeId(long codeId, int start, int end,
555 OrderByComparator orderByComparator) throws SystemException {
556 FinderPath finderPath = null;
557 Object[] finderArgs = null;
558
559 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
560 (orderByComparator == null)) {
561 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_CODEID;
562 finderArgs = new Object[] { codeId };
563 }
564 else {
565 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_CODEID;
566 finderArgs = new Object[] { codeId, start, end, orderByComparator };
567 }
568
569 List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(finderPath,
570 finderArgs, this);
571
572 if (list == null) {
573 StringBundler query = null;
574
575 if (orderByComparator != null) {
576 query = new StringBundler(3 +
577 (orderByComparator.getOrderByFields().length * 3));
578 }
579 else {
580 query = new StringBundler(2);
581 }
582
583 query.append(_SQL_SELECT_RESOURCE_WHERE);
584
585 query.append(_FINDER_COLUMN_CODEID_CODEID_2);
586
587 if (orderByComparator != null) {
588 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
589 orderByComparator);
590 }
591
592 String sql = query.toString();
593
594 Session session = null;
595
596 try {
597 session = openSession();
598
599 Query q = session.createQuery(sql);
600
601 QueryPos qPos = QueryPos.getInstance(q);
602
603 qPos.add(codeId);
604
605 list = (List<Resource>)QueryUtil.list(q, getDialect(), start,
606 end);
607 }
608 catch (Exception e) {
609 throw processException(e);
610 }
611 finally {
612 if (list == null) {
613 FinderCacheUtil.removeResult(finderPath, finderArgs);
614 }
615 else {
616 cacheResult(list);
617
618 FinderCacheUtil.putResult(finderPath, finderArgs, list);
619 }
620
621 closeSession(session);
622 }
623 }
624
625 return list;
626 }
627
628
641 public Resource findByCodeId_First(long codeId,
642 OrderByComparator orderByComparator)
643 throws NoSuchResourceException, SystemException {
644 List<Resource> list = findByCodeId(codeId, 0, 1, orderByComparator);
645
646 if (list.isEmpty()) {
647 StringBundler msg = new StringBundler(4);
648
649 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
650
651 msg.append("codeId=");
652 msg.append(codeId);
653
654 msg.append(StringPool.CLOSE_CURLY_BRACE);
655
656 throw new NoSuchResourceException(msg.toString());
657 }
658 else {
659 return list.get(0);
660 }
661 }
662
663
676 public Resource findByCodeId_Last(long codeId,
677 OrderByComparator orderByComparator)
678 throws NoSuchResourceException, SystemException {
679 int count = countByCodeId(codeId);
680
681 List<Resource> list = findByCodeId(codeId, count - 1, count,
682 orderByComparator);
683
684 if (list.isEmpty()) {
685 StringBundler msg = new StringBundler(4);
686
687 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
688
689 msg.append("codeId=");
690 msg.append(codeId);
691
692 msg.append(StringPool.CLOSE_CURLY_BRACE);
693
694 throw new NoSuchResourceException(msg.toString());
695 }
696 else {
697 return list.get(0);
698 }
699 }
700
701
715 public Resource[] findByCodeId_PrevAndNext(long resourceId, long codeId,
716 OrderByComparator orderByComparator)
717 throws NoSuchResourceException, SystemException {
718 Resource resource = findByPrimaryKey(resourceId);
719
720 Session session = null;
721
722 try {
723 session = openSession();
724
725 Resource[] array = new ResourceImpl[3];
726
727 array[0] = getByCodeId_PrevAndNext(session, resource, codeId,
728 orderByComparator, true);
729
730 array[1] = resource;
731
732 array[2] = getByCodeId_PrevAndNext(session, resource, codeId,
733 orderByComparator, false);
734
735 return array;
736 }
737 catch (Exception e) {
738 throw processException(e);
739 }
740 finally {
741 closeSession(session);
742 }
743 }
744
745 protected Resource getByCodeId_PrevAndNext(Session session,
746 Resource resource, long codeId, OrderByComparator orderByComparator,
747 boolean previous) {
748 StringBundler query = null;
749
750 if (orderByComparator != null) {
751 query = new StringBundler(6 +
752 (orderByComparator.getOrderByFields().length * 6));
753 }
754 else {
755 query = new StringBundler(3);
756 }
757
758 query.append(_SQL_SELECT_RESOURCE_WHERE);
759
760 query.append(_FINDER_COLUMN_CODEID_CODEID_2);
761
762 if (orderByComparator != null) {
763 String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
764
765 if (orderByConditionFields.length > 0) {
766 query.append(WHERE_AND);
767 }
768
769 for (int i = 0; i < orderByConditionFields.length; i++) {
770 query.append(_ORDER_BY_ENTITY_ALIAS);
771 query.append(orderByConditionFields[i]);
772
773 if ((i + 1) < orderByConditionFields.length) {
774 if (orderByComparator.isAscending() ^ previous) {
775 query.append(WHERE_GREATER_THAN_HAS_NEXT);
776 }
777 else {
778 query.append(WHERE_LESSER_THAN_HAS_NEXT);
779 }
780 }
781 else {
782 if (orderByComparator.isAscending() ^ previous) {
783 query.append(WHERE_GREATER_THAN);
784 }
785 else {
786 query.append(WHERE_LESSER_THAN);
787 }
788 }
789 }
790
791 query.append(ORDER_BY_CLAUSE);
792
793 String[] orderByFields = orderByComparator.getOrderByFields();
794
795 for (int i = 0; i < orderByFields.length; i++) {
796 query.append(_ORDER_BY_ENTITY_ALIAS);
797 query.append(orderByFields[i]);
798
799 if ((i + 1) < orderByFields.length) {
800 if (orderByComparator.isAscending() ^ previous) {
801 query.append(ORDER_BY_ASC_HAS_NEXT);
802 }
803 else {
804 query.append(ORDER_BY_DESC_HAS_NEXT);
805 }
806 }
807 else {
808 if (orderByComparator.isAscending() ^ previous) {
809 query.append(ORDER_BY_ASC);
810 }
811 else {
812 query.append(ORDER_BY_DESC);
813 }
814 }
815 }
816 }
817
818 String sql = query.toString();
819
820 Query q = session.createQuery(sql);
821
822 q.setFirstResult(0);
823 q.setMaxResults(2);
824
825 QueryPos qPos = QueryPos.getInstance(q);
826
827 qPos.add(codeId);
828
829 if (orderByComparator != null) {
830 Object[] values = orderByComparator.getOrderByConditionValues(resource);
831
832 for (Object value : values) {
833 qPos.add(value);
834 }
835 }
836
837 List<Resource> list = q.list();
838
839 if (list.size() == 2) {
840 return list.get(1);
841 }
842 else {
843 return null;
844 }
845 }
846
847
856 public Resource findByC_P(long codeId, String primKey)
857 throws NoSuchResourceException, SystemException {
858 Resource resource = fetchByC_P(codeId, primKey);
859
860 if (resource == null) {
861 StringBundler msg = new StringBundler(6);
862
863 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
864
865 msg.append("codeId=");
866 msg.append(codeId);
867
868 msg.append(", primKey=");
869 msg.append(primKey);
870
871 msg.append(StringPool.CLOSE_CURLY_BRACE);
872
873 if (_log.isWarnEnabled()) {
874 _log.warn(msg.toString());
875 }
876
877 throw new NoSuchResourceException(msg.toString());
878 }
879
880 return resource;
881 }
882
883
891 public Resource fetchByC_P(long codeId, String primKey)
892 throws SystemException {
893 return fetchByC_P(codeId, primKey, true);
894 }
895
896
905 public Resource fetchByC_P(long codeId, String primKey,
906 boolean retrieveFromCache) throws SystemException {
907 Object[] finderArgs = new Object[] { codeId, primKey };
908
909 Object result = null;
910
911 if (retrieveFromCache) {
912 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
913 finderArgs, this);
914 }
915
916 if (result == null) {
917 StringBundler query = new StringBundler(3);
918
919 query.append(_SQL_SELECT_RESOURCE_WHERE);
920
921 query.append(_FINDER_COLUMN_C_P_CODEID_2);
922
923 if (primKey == null) {
924 query.append(_FINDER_COLUMN_C_P_PRIMKEY_1);
925 }
926 else {
927 if (primKey.equals(StringPool.BLANK)) {
928 query.append(_FINDER_COLUMN_C_P_PRIMKEY_3);
929 }
930 else {
931 query.append(_FINDER_COLUMN_C_P_PRIMKEY_2);
932 }
933 }
934
935 String sql = query.toString();
936
937 Session session = null;
938
939 try {
940 session = openSession();
941
942 Query q = session.createQuery(sql);
943
944 QueryPos qPos = QueryPos.getInstance(q);
945
946 qPos.add(codeId);
947
948 if (primKey != null) {
949 qPos.add(primKey);
950 }
951
952 List<Resource> list = q.list();
953
954 result = list;
955
956 Resource resource = null;
957
958 if (list.isEmpty()) {
959 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
960 finderArgs, list);
961 }
962 else {
963 resource = list.get(0);
964
965 cacheResult(resource);
966
967 if ((resource.getCodeId() != codeId) ||
968 (resource.getPrimKey() == null) ||
969 !resource.getPrimKey().equals(primKey)) {
970 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
971 finderArgs, resource);
972 }
973 }
974
975 return resource;
976 }
977 catch (Exception e) {
978 throw processException(e);
979 }
980 finally {
981 if (result == null) {
982 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
983 finderArgs);
984 }
985
986 closeSession(session);
987 }
988 }
989 else {
990 if (result instanceof List<?>) {
991 return null;
992 }
993 else {
994 return (Resource)result;
995 }
996 }
997 }
998
999
1005 public List<Resource> findAll() throws SystemException {
1006 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1007 }
1008
1009
1021 public List<Resource> findAll(int start, int end) throws SystemException {
1022 return findAll(start, end, null);
1023 }
1024
1025
1038 public List<Resource> findAll(int start, int end,
1039 OrderByComparator orderByComparator) throws SystemException {
1040 FinderPath finderPath = null;
1041 Object[] finderArgs = new Object[] { start, end, orderByComparator };
1042
1043 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1044 (orderByComparator == null)) {
1045 finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1046 finderArgs = FINDER_ARGS_EMPTY;
1047 }
1048 else {
1049 finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1050 finderArgs = new Object[] { start, end, orderByComparator };
1051 }
1052
1053 List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(finderPath,
1054 finderArgs, this);
1055
1056 if (list == null) {
1057 StringBundler query = null;
1058 String sql = null;
1059
1060 if (orderByComparator != null) {
1061 query = new StringBundler(2 +
1062 (orderByComparator.getOrderByFields().length * 3));
1063
1064 query.append(_SQL_SELECT_RESOURCE);
1065
1066 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1067 orderByComparator);
1068
1069 sql = query.toString();
1070 }
1071 else {
1072 sql = _SQL_SELECT_RESOURCE;
1073 }
1074
1075 Session session = null;
1076
1077 try {
1078 session = openSession();
1079
1080 Query q = session.createQuery(sql);
1081
1082 if (orderByComparator == null) {
1083 list = (List<Resource>)QueryUtil.list(q, getDialect(),
1084 start, end, false);
1085
1086 Collections.sort(list);
1087 }
1088 else {
1089 list = (List<Resource>)QueryUtil.list(q, getDialect(),
1090 start, end);
1091 }
1092 }
1093 catch (Exception e) {
1094 throw processException(e);
1095 }
1096 finally {
1097 if (list == null) {
1098 FinderCacheUtil.removeResult(finderPath, finderArgs);
1099 }
1100 else {
1101 cacheResult(list);
1102
1103 FinderCacheUtil.putResult(finderPath, finderArgs, list);
1104 }
1105
1106 closeSession(session);
1107 }
1108 }
1109
1110 return list;
1111 }
1112
1113
1119 public void removeByCodeId(long codeId) throws SystemException {
1120 for (Resource resource : findByCodeId(codeId)) {
1121 resourcePersistence.remove(resource);
1122 }
1123 }
1124
1125
1132 public void removeByC_P(long codeId, String primKey)
1133 throws NoSuchResourceException, SystemException {
1134 Resource resource = findByC_P(codeId, primKey);
1135
1136 resourcePersistence.remove(resource);
1137 }
1138
1139
1144 public void removeAll() throws SystemException {
1145 for (Resource resource : findAll()) {
1146 resourcePersistence.remove(resource);
1147 }
1148 }
1149
1150
1157 public int countByCodeId(long codeId) throws SystemException {
1158 Object[] finderArgs = new Object[] { codeId };
1159
1160 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODEID,
1161 finderArgs, this);
1162
1163 if (count == null) {
1164 StringBundler query = new StringBundler(2);
1165
1166 query.append(_SQL_COUNT_RESOURCE_WHERE);
1167
1168 query.append(_FINDER_COLUMN_CODEID_CODEID_2);
1169
1170 String sql = query.toString();
1171
1172 Session session = null;
1173
1174 try {
1175 session = openSession();
1176
1177 Query q = session.createQuery(sql);
1178
1179 QueryPos qPos = QueryPos.getInstance(q);
1180
1181 qPos.add(codeId);
1182
1183 count = (Long)q.uniqueResult();
1184 }
1185 catch (Exception e) {
1186 throw processException(e);
1187 }
1188 finally {
1189 if (count == null) {
1190 count = Long.valueOf(0);
1191 }
1192
1193 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODEID,
1194 finderArgs, count);
1195
1196 closeSession(session);
1197 }
1198 }
1199
1200 return count.intValue();
1201 }
1202
1203
1211 public int countByC_P(long codeId, String primKey)
1212 throws SystemException {
1213 Object[] finderArgs = new Object[] { codeId, primKey };
1214
1215 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
1216 finderArgs, this);
1217
1218 if (count == null) {
1219 StringBundler query = new StringBundler(3);
1220
1221 query.append(_SQL_COUNT_RESOURCE_WHERE);
1222
1223 query.append(_FINDER_COLUMN_C_P_CODEID_2);
1224
1225 if (primKey == null) {
1226 query.append(_FINDER_COLUMN_C_P_PRIMKEY_1);
1227 }
1228 else {
1229 if (primKey.equals(StringPool.BLANK)) {
1230 query.append(_FINDER_COLUMN_C_P_PRIMKEY_3);
1231 }
1232 else {
1233 query.append(_FINDER_COLUMN_C_P_PRIMKEY_2);
1234 }
1235 }
1236
1237 String sql = query.toString();
1238
1239 Session session = null;
1240
1241 try {
1242 session = openSession();
1243
1244 Query q = session.createQuery(sql);
1245
1246 QueryPos qPos = QueryPos.getInstance(q);
1247
1248 qPos.add(codeId);
1249
1250 if (primKey != null) {
1251 qPos.add(primKey);
1252 }
1253
1254 count = (Long)q.uniqueResult();
1255 }
1256 catch (Exception e) {
1257 throw processException(e);
1258 }
1259 finally {
1260 if (count == null) {
1261 count = Long.valueOf(0);
1262 }
1263
1264 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
1265 count);
1266
1267 closeSession(session);
1268 }
1269 }
1270
1271 return count.intValue();
1272 }
1273
1274
1280 public int countAll() throws SystemException {
1281 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1282 FINDER_ARGS_EMPTY, this);
1283
1284 if (count == null) {
1285 Session session = null;
1286
1287 try {
1288 session = openSession();
1289
1290 Query q = session.createQuery(_SQL_COUNT_RESOURCE);
1291
1292 count = (Long)q.uniqueResult();
1293 }
1294 catch (Exception e) {
1295 throw processException(e);
1296 }
1297 finally {
1298 if (count == null) {
1299 count = Long.valueOf(0);
1300 }
1301
1302 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1303 FINDER_ARGS_EMPTY, count);
1304
1305 closeSession(session);
1306 }
1307 }
1308
1309 return count.intValue();
1310 }
1311
1312
1315 public void afterPropertiesSet() {
1316 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1317 com.liferay.portal.util.PropsUtil.get(
1318 "value.object.listener.com.liferay.portal.model.Resource")));
1319
1320 if (listenerClassNames.length > 0) {
1321 try {
1322 List<ModelListener<Resource>> listenersList = new ArrayList<ModelListener<Resource>>();
1323
1324 for (String listenerClassName : listenerClassNames) {
1325 listenersList.add((ModelListener<Resource>)InstanceFactory.newInstance(
1326 listenerClassName));
1327 }
1328
1329 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1330 }
1331 catch (Exception e) {
1332 _log.error(e);
1333 }
1334 }
1335 }
1336
1337 public void destroy() {
1338 EntityCacheUtil.removeCache(ResourceImpl.class.getName());
1339 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1340 FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1341 }
1342
1343 @BeanReference(type = AccountPersistence.class)
1344 protected AccountPersistence accountPersistence;
1345 @BeanReference(type = AddressPersistence.class)
1346 protected AddressPersistence addressPersistence;
1347 @BeanReference(type = BrowserTrackerPersistence.class)
1348 protected BrowserTrackerPersistence browserTrackerPersistence;
1349 @BeanReference(type = ClassNamePersistence.class)
1350 protected ClassNamePersistence classNamePersistence;
1351 @BeanReference(type = ClusterGroupPersistence.class)
1352 protected ClusterGroupPersistence clusterGroupPersistence;
1353 @BeanReference(type = CompanyPersistence.class)
1354 protected CompanyPersistence companyPersistence;
1355 @BeanReference(type = ContactPersistence.class)
1356 protected ContactPersistence contactPersistence;
1357 @BeanReference(type = CountryPersistence.class)
1358 protected CountryPersistence countryPersistence;
1359 @BeanReference(type = EmailAddressPersistence.class)
1360 protected EmailAddressPersistence emailAddressPersistence;
1361 @BeanReference(type = GroupPersistence.class)
1362 protected GroupPersistence groupPersistence;
1363 @BeanReference(type = ImagePersistence.class)
1364 protected ImagePersistence imagePersistence;
1365 @BeanReference(type = LayoutPersistence.class)
1366 protected LayoutPersistence layoutPersistence;
1367 @BeanReference(type = LayoutBranchPersistence.class)
1368 protected LayoutBranchPersistence layoutBranchPersistence;
1369 @BeanReference(type = LayoutPrototypePersistence.class)
1370 protected LayoutPrototypePersistence layoutPrototypePersistence;
1371 @BeanReference(type = LayoutRevisionPersistence.class)
1372 protected LayoutRevisionPersistence layoutRevisionPersistence;
1373 @BeanReference(type = LayoutSetPersistence.class)
1374 protected LayoutSetPersistence layoutSetPersistence;
1375 @BeanReference(type = LayoutSetBranchPersistence.class)
1376 protected LayoutSetBranchPersistence layoutSetBranchPersistence;
1377 @BeanReference(type = LayoutSetPrototypePersistence.class)
1378 protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1379 @BeanReference(type = ListTypePersistence.class)
1380 protected ListTypePersistence listTypePersistence;
1381 @BeanReference(type = LockPersistence.class)
1382 protected LockPersistence lockPersistence;
1383 @BeanReference(type = MembershipRequestPersistence.class)
1384 protected MembershipRequestPersistence membershipRequestPersistence;
1385 @BeanReference(type = OrganizationPersistence.class)
1386 protected OrganizationPersistence organizationPersistence;
1387 @BeanReference(type = OrgGroupPermissionPersistence.class)
1388 protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1389 @BeanReference(type = OrgGroupRolePersistence.class)
1390 protected OrgGroupRolePersistence orgGroupRolePersistence;
1391 @BeanReference(type = OrgLaborPersistence.class)
1392 protected OrgLaborPersistence orgLaborPersistence;
1393 @BeanReference(type = PasswordPolicyPersistence.class)
1394 protected PasswordPolicyPersistence passwordPolicyPersistence;
1395 @BeanReference(type = PasswordPolicyRelPersistence.class)
1396 protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1397 @BeanReference(type = PasswordTrackerPersistence.class)
1398 protected PasswordTrackerPersistence passwordTrackerPersistence;
1399 @BeanReference(type = PermissionPersistence.class)
1400 protected PermissionPersistence permissionPersistence;
1401 @BeanReference(type = PhonePersistence.class)
1402 protected PhonePersistence phonePersistence;
1403 @BeanReference(type = PluginSettingPersistence.class)
1404 protected PluginSettingPersistence pluginSettingPersistence;
1405 @BeanReference(type = PortalPreferencesPersistence.class)
1406 protected PortalPreferencesPersistence portalPreferencesPersistence;
1407 @BeanReference(type = PortletPersistence.class)
1408 protected PortletPersistence portletPersistence;
1409 @BeanReference(type = PortletItemPersistence.class)
1410 protected PortletItemPersistence portletItemPersistence;
1411 @BeanReference(type = PortletPreferencesPersistence.class)
1412 protected PortletPreferencesPersistence portletPreferencesPersistence;
1413 @BeanReference(type = RegionPersistence.class)
1414 protected RegionPersistence regionPersistence;
1415 @BeanReference(type = ReleasePersistence.class)
1416 protected ReleasePersistence releasePersistence;
1417 @BeanReference(type = RepositoryPersistence.class)
1418 protected RepositoryPersistence repositoryPersistence;
1419 @BeanReference(type = RepositoryEntryPersistence.class)
1420 protected RepositoryEntryPersistence repositoryEntryPersistence;
1421 @BeanReference(type = ResourcePersistence.class)
1422 protected ResourcePersistence resourcePersistence;
1423 @BeanReference(type = ResourceActionPersistence.class)
1424 protected ResourceActionPersistence resourceActionPersistence;
1425 @BeanReference(type = ResourceBlockPersistence.class)
1426 protected ResourceBlockPersistence resourceBlockPersistence;
1427 @BeanReference(type = ResourceBlockPermissionPersistence.class)
1428 protected ResourceBlockPermissionPersistence resourceBlockPermissionPersistence;
1429 @BeanReference(type = ResourceCodePersistence.class)
1430 protected ResourceCodePersistence resourceCodePersistence;
1431 @BeanReference(type = ResourcePermissionPersistence.class)
1432 protected ResourcePermissionPersistence resourcePermissionPersistence;
1433 @BeanReference(type = ResourceTypePermissionPersistence.class)
1434 protected ResourceTypePermissionPersistence resourceTypePermissionPersistence;
1435 @BeanReference(type = RolePersistence.class)
1436 protected RolePersistence rolePersistence;
1437 @BeanReference(type = ServiceComponentPersistence.class)
1438 protected ServiceComponentPersistence serviceComponentPersistence;
1439 @BeanReference(type = ShardPersistence.class)
1440 protected ShardPersistence shardPersistence;
1441 @BeanReference(type = SubscriptionPersistence.class)
1442 protected SubscriptionPersistence subscriptionPersistence;
1443 @BeanReference(type = TeamPersistence.class)
1444 protected TeamPersistence teamPersistence;
1445 @BeanReference(type = TicketPersistence.class)
1446 protected TicketPersistence ticketPersistence;
1447 @BeanReference(type = UserPersistence.class)
1448 protected UserPersistence userPersistence;
1449 @BeanReference(type = UserGroupPersistence.class)
1450 protected UserGroupPersistence userGroupPersistence;
1451 @BeanReference(type = UserGroupGroupRolePersistence.class)
1452 protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1453 @BeanReference(type = UserGroupRolePersistence.class)
1454 protected UserGroupRolePersistence userGroupRolePersistence;
1455 @BeanReference(type = UserIdMapperPersistence.class)
1456 protected UserIdMapperPersistence userIdMapperPersistence;
1457 @BeanReference(type = UserNotificationEventPersistence.class)
1458 protected UserNotificationEventPersistence userNotificationEventPersistence;
1459 @BeanReference(type = UserTrackerPersistence.class)
1460 protected UserTrackerPersistence userTrackerPersistence;
1461 @BeanReference(type = UserTrackerPathPersistence.class)
1462 protected UserTrackerPathPersistence userTrackerPathPersistence;
1463 @BeanReference(type = VirtualHostPersistence.class)
1464 protected VirtualHostPersistence virtualHostPersistence;
1465 @BeanReference(type = WebDAVPropsPersistence.class)
1466 protected WebDAVPropsPersistence webDAVPropsPersistence;
1467 @BeanReference(type = WebsitePersistence.class)
1468 protected WebsitePersistence websitePersistence;
1469 @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1470 protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1471 @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1472 protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1473 private static final String _SQL_SELECT_RESOURCE = "SELECT resource FROM Resource resource";
1474 private static final String _SQL_SELECT_RESOURCE_WHERE = "SELECT resource FROM Resource resource WHERE ";
1475 private static final String _SQL_COUNT_RESOURCE = "SELECT COUNT(resource) FROM Resource resource";
1476 private static final String _SQL_COUNT_RESOURCE_WHERE = "SELECT COUNT(resource) FROM Resource resource WHERE ";
1477 private static final String _FINDER_COLUMN_CODEID_CODEID_2 = "resource.codeId = ?";
1478 private static final String _FINDER_COLUMN_C_P_CODEID_2 = "resource.codeId = ? AND ";
1479 private static final String _FINDER_COLUMN_C_P_PRIMKEY_1 = "resource.primKey IS NULL";
1480 private static final String _FINDER_COLUMN_C_P_PRIMKEY_2 = "resource.primKey = ?";
1481 private static final String _FINDER_COLUMN_C_P_PRIMKEY_3 = "(resource.primKey IS NULL OR resource.primKey = ?)";
1482 private static final String _ORDER_BY_ENTITY_ALIAS = "resource.";
1483 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Resource exists with the primary key ";
1484 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Resource exists with the key {";
1485 private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1486 private static Log _log = LogFactoryUtil.getLog(ResourcePersistenceImpl.class);
1487 private static Resource _nullResource = new ResourceImpl() {
1488 @Override
1489 public Object clone() {
1490 return this;
1491 }
1492
1493 @Override
1494 public CacheModel<Resource> toCacheModel() {
1495 return _nullResourceCacheModel;
1496 }
1497 };
1498
1499 private static CacheModel<Resource> _nullResourceCacheModel = new CacheModel<Resource>() {
1500 public Resource toEntityModel() {
1501 return _nullResource;
1502 }
1503 };
1504 }