1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.annotation.BeanReference;
28  import com.liferay.portal.kernel.cache.CacheRegistry;
29  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
30  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
32  import com.liferay.portal.kernel.dao.orm.FinderPath;
33  import com.liferay.portal.kernel.dao.orm.Query;
34  import com.liferay.portal.kernel.dao.orm.QueryPos;
35  import com.liferay.portal.kernel.dao.orm.QueryUtil;
36  import com.liferay.portal.kernel.dao.orm.Session;
37  import com.liferay.portal.kernel.log.Log;
38  import com.liferay.portal.kernel.log.LogFactoryUtil;
39  import com.liferay.portal.kernel.util.GetterUtil;
40  import com.liferay.portal.kernel.util.OrderByComparator;
41  import com.liferay.portal.kernel.util.StringPool;
42  import com.liferay.portal.kernel.util.StringUtil;
43  import com.liferay.portal.kernel.util.Validator;
44  import com.liferay.portal.model.ModelListener;
45  import com.liferay.portal.model.Resource;
46  import com.liferay.portal.model.impl.ResourceImpl;
47  import com.liferay.portal.model.impl.ResourceModelImpl;
48  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
49  
50  import java.util.ArrayList;
51  import java.util.Collections;
52  import java.util.List;
53  
54  /**
55   * <a href="ResourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class ResourcePersistenceImpl extends BasePersistenceImpl
61      implements ResourcePersistence {
62      public static final String FINDER_CLASS_NAME_ENTITY = ResourceImpl.class.getName();
63      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
64          ".List";
65      public static final FinderPath FINDER_PATH_FIND_BY_CODEID = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
66              ResourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
67              "findByCodeId", new String[] { Long.class.getName() });
68      public static final FinderPath FINDER_PATH_FIND_BY_OBC_CODEID = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
69              ResourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
70              "findByCodeId",
71              new String[] {
72                  Long.class.getName(),
73                  
74              "java.lang.Integer", "java.lang.Integer",
75                  "com.liferay.portal.kernel.util.OrderByComparator"
76              });
77      public static final FinderPath FINDER_PATH_COUNT_BY_CODEID = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
78              ResourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
79              "countByCodeId", new String[] { Long.class.getName() });
80      public static final FinderPath FINDER_PATH_FETCH_BY_C_P = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
81              ResourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
82              "fetchByC_P",
83              new String[] { Long.class.getName(), String.class.getName() });
84      public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
85              ResourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
86              "countByC_P",
87              new String[] { Long.class.getName(), String.class.getName() });
88      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
89              ResourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
90              "findAll", new String[0]);
91      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ResourceModelImpl.ENTITY_CACHE_ENABLED,
92              ResourceModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
93              "countAll", new String[0]);
94  
95      public void cacheResult(Resource resource) {
96          EntityCacheUtil.putResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
97              ResourceImpl.class, resource.getPrimaryKey(), resource);
98  
99          FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
100             new Object[] { new Long(resource.getCodeId()), resource.getPrimKey() },
101             resource);
102     }
103 
104     public void cacheResult(List<Resource> resources) {
105         for (Resource resource : resources) {
106             if (EntityCacheUtil.getResult(
107                         ResourceModelImpl.ENTITY_CACHE_ENABLED,
108                         ResourceImpl.class, resource.getPrimaryKey(), this) == null) {
109                 cacheResult(resource);
110             }
111         }
112     }
113 
114     public void clearCache() {
115         CacheRegistry.clear(ResourceImpl.class.getName());
116         EntityCacheUtil.clearCache(ResourceImpl.class.getName());
117         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
118         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
119     }
120 
121     public Resource create(long resourceId) {
122         Resource resource = new ResourceImpl();
123 
124         resource.setNew(true);
125         resource.setPrimaryKey(resourceId);
126 
127         return resource;
128     }
129 
130     public Resource remove(long resourceId)
131         throws NoSuchResourceException, SystemException {
132         Session session = null;
133 
134         try {
135             session = openSession();
136 
137             Resource resource = (Resource)session.get(ResourceImpl.class,
138                     new Long(resourceId));
139 
140             if (resource == null) {
141                 if (_log.isWarnEnabled()) {
142                     _log.warn("No Resource exists with the primary key " +
143                         resourceId);
144                 }
145 
146                 throw new NoSuchResourceException(
147                     "No Resource exists with the primary key " + resourceId);
148             }
149 
150             return remove(resource);
151         }
152         catch (NoSuchResourceException nsee) {
153             throw nsee;
154         }
155         catch (Exception e) {
156             throw processException(e);
157         }
158         finally {
159             closeSession(session);
160         }
161     }
162 
163     public Resource remove(Resource resource) throws SystemException {
164         for (ModelListener<Resource> listener : listeners) {
165             listener.onBeforeRemove(resource);
166         }
167 
168         resource = removeImpl(resource);
169 
170         for (ModelListener<Resource> listener : listeners) {
171             listener.onAfterRemove(resource);
172         }
173 
174         return resource;
175     }
176 
177     protected Resource removeImpl(Resource resource) throws SystemException {
178         Session session = null;
179 
180         try {
181             session = openSession();
182 
183             if (resource.isCachedModel() || BatchSessionUtil.isEnabled()) {
184                 Object staleObject = session.get(ResourceImpl.class,
185                         resource.getPrimaryKeyObj());
186 
187                 if (staleObject != null) {
188                     session.evict(staleObject);
189                 }
190             }
191 
192             session.delete(resource);
193 
194             session.flush();
195         }
196         catch (Exception e) {
197             throw processException(e);
198         }
199         finally {
200             closeSession(session);
201         }
202 
203         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
204 
205         ResourceModelImpl resourceModelImpl = (ResourceModelImpl)resource;
206 
207         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
208             new Object[] {
209                 new Long(resourceModelImpl.getOriginalCodeId()),
210                 
211             resourceModelImpl.getOriginalPrimKey()
212             });
213 
214         EntityCacheUtil.removeResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
215             ResourceImpl.class, resource.getPrimaryKey());
216 
217         return resource;
218     }
219 
220     /**
221      * @deprecated Use <code>update(Resource resource, boolean merge)</code>.
222      */
223     public Resource update(Resource resource) throws SystemException {
224         if (_log.isWarnEnabled()) {
225             _log.warn(
226                 "Using the deprecated update(Resource resource) method. Use update(Resource resource, boolean merge) instead.");
227         }
228 
229         return update(resource, false);
230     }
231 
232     /**
233      * Add, update, or merge, the entity. This method also calls the model
234      * listeners to trigger the proper events associated with adding, deleting,
235      * or updating an entity.
236      *
237      * @param        resource the entity to add, update, or merge
238      * @param        merge boolean value for whether to merge the entity. The
239      *                default value is false. Setting merge to true is more
240      *                expensive and should only be true when resource is
241      *                transient. See LEP-5473 for a detailed discussion of this
242      *                method.
243      * @return        true if the portlet can be displayed via Ajax
244      */
245     public Resource update(Resource resource, boolean merge)
246         throws SystemException {
247         boolean isNew = resource.isNew();
248 
249         for (ModelListener<Resource> listener : listeners) {
250             if (isNew) {
251                 listener.onBeforeCreate(resource);
252             }
253             else {
254                 listener.onBeforeUpdate(resource);
255             }
256         }
257 
258         resource = updateImpl(resource, merge);
259 
260         for (ModelListener<Resource> listener : listeners) {
261             if (isNew) {
262                 listener.onAfterCreate(resource);
263             }
264             else {
265                 listener.onAfterUpdate(resource);
266             }
267         }
268 
269         return resource;
270     }
271 
272     public Resource updateImpl(com.liferay.portal.model.Resource resource,
273         boolean merge) throws SystemException {
274         boolean isNew = resource.isNew();
275 
276         ResourceModelImpl resourceModelImpl = (ResourceModelImpl)resource;
277 
278         Session session = null;
279 
280         try {
281             session = openSession();
282 
283             BatchSessionUtil.update(session, resource, merge);
284 
285             resource.setNew(false);
286         }
287         catch (Exception e) {
288             throw processException(e);
289         }
290         finally {
291             closeSession(session);
292         }
293 
294         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
295 
296         EntityCacheUtil.putResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
297             ResourceImpl.class, resource.getPrimaryKey(), resource);
298 
299         if (!isNew &&
300                 ((resource.getCodeId() != resourceModelImpl.getOriginalCodeId()) ||
301                 !Validator.equals(resource.getPrimKey(),
302                     resourceModelImpl.getOriginalPrimKey()))) {
303             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
304                 new Object[] {
305                     new Long(resourceModelImpl.getOriginalCodeId()),
306                     
307                 resourceModelImpl.getOriginalPrimKey()
308                 });
309         }
310 
311         if (isNew ||
312                 ((resource.getCodeId() != resourceModelImpl.getOriginalCodeId()) ||
313                 !Validator.equals(resource.getPrimKey(),
314                     resourceModelImpl.getOriginalPrimKey()))) {
315             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
316                 new Object[] {
317                     new Long(resource.getCodeId()),
318                     
319                 resource.getPrimKey()
320                 }, resource);
321         }
322 
323         return resource;
324     }
325 
326     public Resource findByPrimaryKey(long resourceId)
327         throws NoSuchResourceException, SystemException {
328         Resource resource = fetchByPrimaryKey(resourceId);
329 
330         if (resource == null) {
331             if (_log.isWarnEnabled()) {
332                 _log.warn("No Resource exists with the primary key " +
333                     resourceId);
334             }
335 
336             throw new NoSuchResourceException(
337                 "No Resource exists with the primary key " + resourceId);
338         }
339 
340         return resource;
341     }
342 
343     public Resource fetchByPrimaryKey(long resourceId)
344         throws SystemException {
345         Resource resource = (Resource)EntityCacheUtil.getResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
346                 ResourceImpl.class, resourceId, this);
347 
348         if (resource == null) {
349             Session session = null;
350 
351             try {
352                 session = openSession();
353 
354                 resource = (Resource)session.get(ResourceImpl.class,
355                         new Long(resourceId));
356             }
357             catch (Exception e) {
358                 throw processException(e);
359             }
360             finally {
361                 if (resource != null) {
362                     cacheResult(resource);
363                 }
364 
365                 closeSession(session);
366             }
367         }
368 
369         return resource;
370     }
371 
372     public List<Resource> findByCodeId(long codeId) throws SystemException {
373         Object[] finderArgs = new Object[] { new Long(codeId) };
374 
375         List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CODEID,
376                 finderArgs, this);
377 
378         if (list == null) {
379             Session session = null;
380 
381             try {
382                 session = openSession();
383 
384                 StringBuilder query = new StringBuilder();
385 
386                 query.append("SELECT resource FROM Resource resource WHERE ");
387 
388                 query.append("resource.codeId = ?");
389 
390                 query.append(" ");
391 
392                 Query q = session.createQuery(query.toString());
393 
394                 QueryPos qPos = QueryPos.getInstance(q);
395 
396                 qPos.add(codeId);
397 
398                 list = q.list();
399             }
400             catch (Exception e) {
401                 throw processException(e);
402             }
403             finally {
404                 if (list == null) {
405                     list = new ArrayList<Resource>();
406                 }
407 
408                 cacheResult(list);
409 
410                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CODEID,
411                     finderArgs, list);
412 
413                 closeSession(session);
414             }
415         }
416 
417         return list;
418     }
419 
420     public List<Resource> findByCodeId(long codeId, int start, int end)
421         throws SystemException {
422         return findByCodeId(codeId, start, end, null);
423     }
424 
425     public List<Resource> findByCodeId(long codeId, int start, int end,
426         OrderByComparator obc) throws SystemException {
427         Object[] finderArgs = new Object[] {
428                 new Long(codeId),
429                 
430                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
431             };
432 
433         List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_CODEID,
434                 finderArgs, this);
435 
436         if (list == null) {
437             Session session = null;
438 
439             try {
440                 session = openSession();
441 
442                 StringBuilder query = new StringBuilder();
443 
444                 query.append("SELECT resource FROM Resource resource WHERE ");
445 
446                 query.append("resource.codeId = ?");
447 
448                 query.append(" ");
449 
450                 if (obc != null) {
451                     query.append("ORDER BY ");
452 
453                     String[] orderByFields = obc.getOrderByFields();
454 
455                     for (int i = 0; i < orderByFields.length; i++) {
456                         query.append("resource.");
457                         query.append(orderByFields[i]);
458 
459                         if (obc.isAscending()) {
460                             query.append(" ASC");
461                         }
462                         else {
463                             query.append(" DESC");
464                         }
465 
466                         if ((i + 1) < orderByFields.length) {
467                             query.append(", ");
468                         }
469                     }
470                 }
471 
472                 Query q = session.createQuery(query.toString());
473 
474                 QueryPos qPos = QueryPos.getInstance(q);
475 
476                 qPos.add(codeId);
477 
478                 list = (List<Resource>)QueryUtil.list(q, getDialect(), start,
479                         end);
480             }
481             catch (Exception e) {
482                 throw processException(e);
483             }
484             finally {
485                 if (list == null) {
486                     list = new ArrayList<Resource>();
487                 }
488 
489                 cacheResult(list);
490 
491                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_CODEID,
492                     finderArgs, list);
493 
494                 closeSession(session);
495             }
496         }
497 
498         return list;
499     }
500 
501     public Resource findByCodeId_First(long codeId, OrderByComparator obc)
502         throws NoSuchResourceException, SystemException {
503         List<Resource> list = findByCodeId(codeId, 0, 1, obc);
504 
505         if (list.isEmpty()) {
506             StringBuilder msg = new StringBuilder();
507 
508             msg.append("No Resource exists with the key {");
509 
510             msg.append("codeId=" + codeId);
511 
512             msg.append(StringPool.CLOSE_CURLY_BRACE);
513 
514             throw new NoSuchResourceException(msg.toString());
515         }
516         else {
517             return list.get(0);
518         }
519     }
520 
521     public Resource findByCodeId_Last(long codeId, OrderByComparator obc)
522         throws NoSuchResourceException, SystemException {
523         int count = countByCodeId(codeId);
524 
525         List<Resource> list = findByCodeId(codeId, count - 1, count, obc);
526 
527         if (list.isEmpty()) {
528             StringBuilder msg = new StringBuilder();
529 
530             msg.append("No Resource exists with the key {");
531 
532             msg.append("codeId=" + codeId);
533 
534             msg.append(StringPool.CLOSE_CURLY_BRACE);
535 
536             throw new NoSuchResourceException(msg.toString());
537         }
538         else {
539             return list.get(0);
540         }
541     }
542 
543     public Resource[] findByCodeId_PrevAndNext(long resourceId, long codeId,
544         OrderByComparator obc) throws NoSuchResourceException, SystemException {
545         Resource resource = findByPrimaryKey(resourceId);
546 
547         int count = countByCodeId(codeId);
548 
549         Session session = null;
550 
551         try {
552             session = openSession();
553 
554             StringBuilder query = new StringBuilder();
555 
556             query.append("SELECT resource FROM Resource resource WHERE ");
557 
558             query.append("resource.codeId = ?");
559 
560             query.append(" ");
561 
562             if (obc != null) {
563                 query.append("ORDER BY ");
564 
565                 String[] orderByFields = obc.getOrderByFields();
566 
567                 for (int i = 0; i < orderByFields.length; i++) {
568                     query.append("resource.");
569                     query.append(orderByFields[i]);
570 
571                     if (obc.isAscending()) {
572                         query.append(" ASC");
573                     }
574                     else {
575                         query.append(" DESC");
576                     }
577 
578                     if ((i + 1) < orderByFields.length) {
579                         query.append(", ");
580                     }
581                 }
582             }
583 
584             Query q = session.createQuery(query.toString());
585 
586             QueryPos qPos = QueryPos.getInstance(q);
587 
588             qPos.add(codeId);
589 
590             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, resource);
591 
592             Resource[] array = new ResourceImpl[3];
593 
594             array[0] = (Resource)objArray[0];
595             array[1] = (Resource)objArray[1];
596             array[2] = (Resource)objArray[2];
597 
598             return array;
599         }
600         catch (Exception e) {
601             throw processException(e);
602         }
603         finally {
604             closeSession(session);
605         }
606     }
607 
608     public Resource findByC_P(long codeId, String primKey)
609         throws NoSuchResourceException, SystemException {
610         Resource resource = fetchByC_P(codeId, primKey);
611 
612         if (resource == null) {
613             StringBuilder msg = new StringBuilder();
614 
615             msg.append("No Resource exists with the key {");
616 
617             msg.append("codeId=" + codeId);
618 
619             msg.append(", ");
620             msg.append("primKey=" + primKey);
621 
622             msg.append(StringPool.CLOSE_CURLY_BRACE);
623 
624             if (_log.isWarnEnabled()) {
625                 _log.warn(msg.toString());
626             }
627 
628             throw new NoSuchResourceException(msg.toString());
629         }
630 
631         return resource;
632     }
633 
634     public Resource fetchByC_P(long codeId, String primKey)
635         throws SystemException {
636         return fetchByC_P(codeId, primKey, true);
637     }
638 
639     public Resource fetchByC_P(long codeId, String primKey,
640         boolean retrieveFromCache) throws SystemException {
641         Object[] finderArgs = new Object[] { new Long(codeId), primKey };
642 
643         Object result = null;
644 
645         if (retrieveFromCache) {
646             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
647                     finderArgs, this);
648         }
649 
650         if (result == null) {
651             Session session = null;
652 
653             try {
654                 session = openSession();
655 
656                 StringBuilder query = new StringBuilder();
657 
658                 query.append("SELECT resource FROM Resource resource WHERE ");
659 
660                 query.append("resource.codeId = ?");
661 
662                 query.append(" AND ");
663 
664                 if (primKey == null) {
665                     query.append("resource.primKey IS NULL");
666                 }
667                 else {
668                     query.append("resource.primKey = ?");
669                 }
670 
671                 query.append(" ");
672 
673                 Query q = session.createQuery(query.toString());
674 
675                 QueryPos qPos = QueryPos.getInstance(q);
676 
677                 qPos.add(codeId);
678 
679                 if (primKey != null) {
680                     qPos.add(primKey);
681                 }
682 
683                 List<Resource> list = q.list();
684 
685                 result = list;
686 
687                 Resource resource = null;
688 
689                 if (list.isEmpty()) {
690                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
691                         finderArgs, list);
692                 }
693                 else {
694                     resource = list.get(0);
695 
696                     cacheResult(resource);
697 
698                     if ((resource.getCodeId() != codeId) ||
699                             (resource.getPrimKey() == null) ||
700                             !resource.getPrimKey().equals(primKey)) {
701                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
702                             finderArgs, resource);
703                     }
704                 }
705 
706                 return resource;
707             }
708             catch (Exception e) {
709                 throw processException(e);
710             }
711             finally {
712                 if (result == null) {
713                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
714                         finderArgs, new ArrayList<Resource>());
715                 }
716 
717                 closeSession(session);
718             }
719         }
720         else {
721             if (result instanceof List) {
722                 return null;
723             }
724             else {
725                 return (Resource)result;
726             }
727         }
728     }
729 
730     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
731         throws SystemException {
732         Session session = null;
733 
734         try {
735             session = openSession();
736 
737             dynamicQuery.compile(session);
738 
739             return dynamicQuery.list();
740         }
741         catch (Exception e) {
742             throw processException(e);
743         }
744         finally {
745             closeSession(session);
746         }
747     }
748 
749     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
750         int start, int end) throws SystemException {
751         Session session = null;
752 
753         try {
754             session = openSession();
755 
756             dynamicQuery.setLimit(start, end);
757 
758             dynamicQuery.compile(session);
759 
760             return dynamicQuery.list();
761         }
762         catch (Exception e) {
763             throw processException(e);
764         }
765         finally {
766             closeSession(session);
767         }
768     }
769 
770     public List<Resource> findAll() throws SystemException {
771         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
772     }
773 
774     public List<Resource> findAll(int start, int end) throws SystemException {
775         return findAll(start, end, null);
776     }
777 
778     public List<Resource> findAll(int start, int end, OrderByComparator obc)
779         throws SystemException {
780         Object[] finderArgs = new Object[] {
781                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
782             };
783 
784         List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
785                 finderArgs, this);
786 
787         if (list == null) {
788             Session session = null;
789 
790             try {
791                 session = openSession();
792 
793                 StringBuilder query = new StringBuilder();
794 
795                 query.append("SELECT resource FROM Resource resource ");
796 
797                 if (obc != null) {
798                     query.append("ORDER BY ");
799 
800                     String[] orderByFields = obc.getOrderByFields();
801 
802                     for (int i = 0; i < orderByFields.length; i++) {
803                         query.append("resource.");
804                         query.append(orderByFields[i]);
805 
806                         if (obc.isAscending()) {
807                             query.append(" ASC");
808                         }
809                         else {
810                             query.append(" DESC");
811                         }
812 
813                         if ((i + 1) < orderByFields.length) {
814                             query.append(", ");
815                         }
816                     }
817                 }
818 
819                 Query q = session.createQuery(query.toString());
820 
821                 if (obc == null) {
822                     list = (List<Resource>)QueryUtil.list(q, getDialect(),
823                             start, end, false);
824 
825                     Collections.sort(list);
826                 }
827                 else {
828                     list = (List<Resource>)QueryUtil.list(q, getDialect(),
829                             start, end);
830                 }
831             }
832             catch (Exception e) {
833                 throw processException(e);
834             }
835             finally {
836                 if (list == null) {
837                     list = new ArrayList<Resource>();
838                 }
839 
840                 cacheResult(list);
841 
842                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
843 
844                 closeSession(session);
845             }
846         }
847 
848         return list;
849     }
850 
851     public void removeByCodeId(long codeId) throws SystemException {
852         for (Resource resource : findByCodeId(codeId)) {
853             remove(resource);
854         }
855     }
856 
857     public void removeByC_P(long codeId, String primKey)
858         throws NoSuchResourceException, SystemException {
859         Resource resource = findByC_P(codeId, primKey);
860 
861         remove(resource);
862     }
863 
864     public void removeAll() throws SystemException {
865         for (Resource resource : findAll()) {
866             remove(resource);
867         }
868     }
869 
870     public int countByCodeId(long codeId) throws SystemException {
871         Object[] finderArgs = new Object[] { new Long(codeId) };
872 
873         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODEID,
874                 finderArgs, this);
875 
876         if (count == null) {
877             Session session = null;
878 
879             try {
880                 session = openSession();
881 
882                 StringBuilder query = new StringBuilder();
883 
884                 query.append("SELECT COUNT(resource) ");
885                 query.append("FROM Resource resource WHERE ");
886 
887                 query.append("resource.codeId = ?");
888 
889                 query.append(" ");
890 
891                 Query q = session.createQuery(query.toString());
892 
893                 QueryPos qPos = QueryPos.getInstance(q);
894 
895                 qPos.add(codeId);
896 
897                 count = (Long)q.uniqueResult();
898             }
899             catch (Exception e) {
900                 throw processException(e);
901             }
902             finally {
903                 if (count == null) {
904                     count = Long.valueOf(0);
905                 }
906 
907                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODEID,
908                     finderArgs, count);
909 
910                 closeSession(session);
911             }
912         }
913 
914         return count.intValue();
915     }
916 
917     public int countByC_P(long codeId, String primKey)
918         throws SystemException {
919         Object[] finderArgs = new Object[] { new Long(codeId), primKey };
920 
921         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
922                 finderArgs, this);
923 
924         if (count == null) {
925             Session session = null;
926 
927             try {
928                 session = openSession();
929 
930                 StringBuilder query = new StringBuilder();
931 
932                 query.append("SELECT COUNT(resource) ");
933                 query.append("FROM Resource resource WHERE ");
934 
935                 query.append("resource.codeId = ?");
936 
937                 query.append(" AND ");
938 
939                 if (primKey == null) {
940                     query.append("resource.primKey IS NULL");
941                 }
942                 else {
943                     query.append("resource.primKey = ?");
944                 }
945 
946                 query.append(" ");
947 
948                 Query q = session.createQuery(query.toString());
949 
950                 QueryPos qPos = QueryPos.getInstance(q);
951 
952                 qPos.add(codeId);
953 
954                 if (primKey != null) {
955                     qPos.add(primKey);
956                 }
957 
958                 count = (Long)q.uniqueResult();
959             }
960             catch (Exception e) {
961                 throw processException(e);
962             }
963             finally {
964                 if (count == null) {
965                     count = Long.valueOf(0);
966                 }
967 
968                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
969                     count);
970 
971                 closeSession(session);
972             }
973         }
974 
975         return count.intValue();
976     }
977 
978     public int countAll() throws SystemException {
979         Object[] finderArgs = new Object[0];
980 
981         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
982                 finderArgs, this);
983 
984         if (count == null) {
985             Session session = null;
986 
987             try {
988                 session = openSession();
989 
990                 Query q = session.createQuery(
991                         "SELECT COUNT(resource) FROM Resource resource");
992 
993                 count = (Long)q.uniqueResult();
994             }
995             catch (Exception e) {
996                 throw processException(e);
997             }
998             finally {
999                 if (count == null) {
1000                    count = Long.valueOf(0);
1001                }
1002
1003                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1004                    count);
1005
1006                closeSession(session);
1007            }
1008        }
1009
1010        return count.intValue();
1011    }
1012
1013    public void afterPropertiesSet() {
1014        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1015                    com.liferay.portal.util.PropsUtil.get(
1016                        "value.object.listener.com.liferay.portal.model.Resource")));
1017
1018        if (listenerClassNames.length > 0) {
1019            try {
1020                List<ModelListener<Resource>> listenersList = new ArrayList<ModelListener<Resource>>();
1021
1022                for (String listenerClassName : listenerClassNames) {
1023                    listenersList.add((ModelListener<Resource>)Class.forName(
1024                            listenerClassName).newInstance());
1025                }
1026
1027                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1028            }
1029            catch (Exception e) {
1030                _log.error(e);
1031            }
1032        }
1033    }
1034
1035    @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
1036    protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
1037    @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
1038    protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
1039    @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
1040    protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
1041    @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
1042    protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
1043    @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
1044    protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
1045    @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
1046    protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
1047    @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
1048    protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
1049    @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
1050    protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
1051    @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
1052    protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
1053    @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
1054    protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
1055    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
1056    protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
1057    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
1058    protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
1059    @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
1060    protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
1061    @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
1062    protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
1063    @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
1064    protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
1065    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
1066    protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1067    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
1068    protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
1069    @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
1070    protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
1071    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
1072    protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
1073    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
1074    protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1075    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
1076    protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
1077    @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
1078    protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
1079    @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
1080    protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
1081    @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
1082    protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
1083    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
1084    protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
1085    @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
1086    protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
1087    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
1088    protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
1089    @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
1090    protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
1091    @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
1092    protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
1093    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1094    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1095    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
1096    protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
1097    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
1098    protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
1099    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
1100    protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
1101    @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
1102    protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
1103    @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
1104    protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
1105    @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
1106    protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
1107    @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
1108    protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
1109    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1110    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1111    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
1112    protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
1113    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
1114    protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
1115    @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
1116    protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
1117    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
1118    protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
1119    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
1120    protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
1121    @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
1122    protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
1123    @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
1124    protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
1125    private static Log _log = LogFactoryUtil.getLog(ResourcePersistenceImpl.class);
1126}