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