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