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