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