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