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("FROM com.liferay.portal.model.Resource WHERE ");
387 
388                 query.append("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("FROM com.liferay.portal.model.Resource WHERE ");
445 
446                 query.append("codeId = ?");
447 
448                 query.append(" ");
449 
450                 if (obc != null) {
451                     query.append("ORDER BY ");
452                     query.append(obc.getOrderBy());
453                 }
454 
455                 Query q = session.createQuery(query.toString());
456 
457                 QueryPos qPos = QueryPos.getInstance(q);
458 
459                 qPos.add(codeId);
460 
461                 list = (List<Resource>)QueryUtil.list(q, getDialect(), start,
462                         end);
463             }
464             catch (Exception e) {
465                 throw processException(e);
466             }
467             finally {
468                 if (list == null) {
469                     list = new ArrayList<Resource>();
470                 }
471 
472                 cacheResult(list);
473 
474                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_CODEID,
475                     finderArgs, list);
476 
477                 closeSession(session);
478             }
479         }
480 
481         return list;
482     }
483 
484     public Resource findByCodeId_First(long codeId, OrderByComparator obc)
485         throws NoSuchResourceException, SystemException {
486         List<Resource> list = findByCodeId(codeId, 0, 1, obc);
487 
488         if (list.isEmpty()) {
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(StringPool.CLOSE_CURLY_BRACE);
496 
497             throw new NoSuchResourceException(msg.toString());
498         }
499         else {
500             return list.get(0);
501         }
502     }
503 
504     public Resource findByCodeId_Last(long codeId, OrderByComparator obc)
505         throws NoSuchResourceException, SystemException {
506         int count = countByCodeId(codeId);
507 
508         List<Resource> list = findByCodeId(codeId, count - 1, count, obc);
509 
510         if (list.isEmpty()) {
511             StringBuilder msg = new StringBuilder();
512 
513             msg.append("No Resource exists with the key {");
514 
515             msg.append("codeId=" + codeId);
516 
517             msg.append(StringPool.CLOSE_CURLY_BRACE);
518 
519             throw new NoSuchResourceException(msg.toString());
520         }
521         else {
522             return list.get(0);
523         }
524     }
525 
526     public Resource[] findByCodeId_PrevAndNext(long resourceId, long codeId,
527         OrderByComparator obc) throws NoSuchResourceException, SystemException {
528         Resource resource = findByPrimaryKey(resourceId);
529 
530         int count = countByCodeId(codeId);
531 
532         Session session = null;
533 
534         try {
535             session = openSession();
536 
537             StringBuilder query = new StringBuilder();
538 
539             query.append("FROM com.liferay.portal.model.Resource WHERE ");
540 
541             query.append("codeId = ?");
542 
543             query.append(" ");
544 
545             if (obc != null) {
546                 query.append("ORDER BY ");
547                 query.append(obc.getOrderBy());
548             }
549 
550             Query q = session.createQuery(query.toString());
551 
552             QueryPos qPos = QueryPos.getInstance(q);
553 
554             qPos.add(codeId);
555 
556             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, resource);
557 
558             Resource[] array = new ResourceImpl[3];
559 
560             array[0] = (Resource)objArray[0];
561             array[1] = (Resource)objArray[1];
562             array[2] = (Resource)objArray[2];
563 
564             return array;
565         }
566         catch (Exception e) {
567             throw processException(e);
568         }
569         finally {
570             closeSession(session);
571         }
572     }
573 
574     public Resource findByC_P(long codeId, String primKey)
575         throws NoSuchResourceException, SystemException {
576         Resource resource = fetchByC_P(codeId, primKey);
577 
578         if (resource == null) {
579             StringBuilder msg = new StringBuilder();
580 
581             msg.append("No Resource exists with the key {");
582 
583             msg.append("codeId=" + codeId);
584 
585             msg.append(", ");
586             msg.append("primKey=" + primKey);
587 
588             msg.append(StringPool.CLOSE_CURLY_BRACE);
589 
590             if (_log.isWarnEnabled()) {
591                 _log.warn(msg.toString());
592             }
593 
594             throw new NoSuchResourceException(msg.toString());
595         }
596 
597         return resource;
598     }
599 
600     public Resource fetchByC_P(long codeId, String primKey)
601         throws SystemException {
602         return fetchByC_P(codeId, primKey, true);
603     }
604 
605     public Resource fetchByC_P(long codeId, String primKey,
606         boolean retrieveFromCache) throws SystemException {
607         Object[] finderArgs = new Object[] { new Long(codeId), primKey };
608 
609         Object result = null;
610 
611         if (retrieveFromCache) {
612             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
613                     finderArgs, this);
614         }
615 
616         if (result == null) {
617             Session session = null;
618 
619             try {
620                 session = openSession();
621 
622                 StringBuilder query = new StringBuilder();
623 
624                 query.append("FROM com.liferay.portal.model.Resource WHERE ");
625 
626                 query.append("codeId = ?");
627 
628                 query.append(" AND ");
629 
630                 if (primKey == null) {
631                     query.append("primKey IS NULL");
632                 }
633                 else {
634                     query.append("primKey = ?");
635                 }
636 
637                 query.append(" ");
638 
639                 Query q = session.createQuery(query.toString());
640 
641                 QueryPos qPos = QueryPos.getInstance(q);
642 
643                 qPos.add(codeId);
644 
645                 if (primKey != null) {
646                     qPos.add(primKey);
647                 }
648 
649                 List<Resource> list = q.list();
650 
651                 result = list;
652 
653                 Resource resource = null;
654 
655                 if (list.isEmpty()) {
656                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
657                         finderArgs, list);
658                 }
659                 else {
660                     resource = list.get(0);
661 
662                     cacheResult(resource);
663 
664                     if ((resource.getCodeId() != codeId) ||
665                             (resource.getPrimKey() == null) ||
666                             !resource.getPrimKey().equals(primKey)) {
667                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
668                             finderArgs, resource);
669                     }
670                 }
671 
672                 return resource;
673             }
674             catch (Exception e) {
675                 throw processException(e);
676             }
677             finally {
678                 if (result == null) {
679                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
680                         finderArgs, new ArrayList<Resource>());
681                 }
682 
683                 closeSession(session);
684             }
685         }
686         else {
687             if (result instanceof List) {
688                 return null;
689             }
690             else {
691                 return (Resource)result;
692             }
693         }
694     }
695 
696     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
697         throws SystemException {
698         Session session = null;
699 
700         try {
701             session = openSession();
702 
703             dynamicQuery.compile(session);
704 
705             return dynamicQuery.list();
706         }
707         catch (Exception e) {
708             throw processException(e);
709         }
710         finally {
711             closeSession(session);
712         }
713     }
714 
715     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
716         int start, int end) throws SystemException {
717         Session session = null;
718 
719         try {
720             session = openSession();
721 
722             dynamicQuery.setLimit(start, end);
723 
724             dynamicQuery.compile(session);
725 
726             return dynamicQuery.list();
727         }
728         catch (Exception e) {
729             throw processException(e);
730         }
731         finally {
732             closeSession(session);
733         }
734     }
735 
736     public List<Resource> findAll() throws SystemException {
737         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
738     }
739 
740     public List<Resource> findAll(int start, int end) throws SystemException {
741         return findAll(start, end, null);
742     }
743 
744     public List<Resource> findAll(int start, int end, OrderByComparator obc)
745         throws SystemException {
746         Object[] finderArgs = new Object[] {
747                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
748             };
749 
750         List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
751                 finderArgs, this);
752 
753         if (list == null) {
754             Session session = null;
755 
756             try {
757                 session = openSession();
758 
759                 StringBuilder query = new StringBuilder();
760 
761                 query.append("FROM com.liferay.portal.model.Resource ");
762 
763                 if (obc != null) {
764                     query.append("ORDER BY ");
765                     query.append(obc.getOrderBy());
766                 }
767 
768                 Query q = session.createQuery(query.toString());
769 
770                 if (obc == null) {
771                     list = (List<Resource>)QueryUtil.list(q, getDialect(),
772                             start, end, false);
773 
774                     Collections.sort(list);
775                 }
776                 else {
777                     list = (List<Resource>)QueryUtil.list(q, getDialect(),
778                             start, end);
779                 }
780             }
781             catch (Exception e) {
782                 throw processException(e);
783             }
784             finally {
785                 if (list == null) {
786                     list = new ArrayList<Resource>();
787                 }
788 
789                 cacheResult(list);
790 
791                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
792 
793                 closeSession(session);
794             }
795         }
796 
797         return list;
798     }
799 
800     public void removeByCodeId(long codeId) throws SystemException {
801         for (Resource resource : findByCodeId(codeId)) {
802             remove(resource);
803         }
804     }
805 
806     public void removeByC_P(long codeId, String primKey)
807         throws NoSuchResourceException, SystemException {
808         Resource resource = findByC_P(codeId, primKey);
809 
810         remove(resource);
811     }
812 
813     public void removeAll() throws SystemException {
814         for (Resource resource : findAll()) {
815             remove(resource);
816         }
817     }
818 
819     public int countByCodeId(long codeId) throws SystemException {
820         Object[] finderArgs = new Object[] { new Long(codeId) };
821 
822         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODEID,
823                 finderArgs, this);
824 
825         if (count == null) {
826             Session session = null;
827 
828             try {
829                 session = openSession();
830 
831                 StringBuilder query = new StringBuilder();
832 
833                 query.append("SELECT COUNT(*) ");
834                 query.append("FROM com.liferay.portal.model.Resource WHERE ");
835 
836                 query.append("codeId = ?");
837 
838                 query.append(" ");
839 
840                 Query q = session.createQuery(query.toString());
841 
842                 QueryPos qPos = QueryPos.getInstance(q);
843 
844                 qPos.add(codeId);
845 
846                 count = (Long)q.uniqueResult();
847             }
848             catch (Exception e) {
849                 throw processException(e);
850             }
851             finally {
852                 if (count == null) {
853                     count = Long.valueOf(0);
854                 }
855 
856                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODEID,
857                     finderArgs, count);
858 
859                 closeSession(session);
860             }
861         }
862 
863         return count.intValue();
864     }
865 
866     public int countByC_P(long codeId, String primKey)
867         throws SystemException {
868         Object[] finderArgs = new Object[] { new Long(codeId), primKey };
869 
870         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
871                 finderArgs, this);
872 
873         if (count == null) {
874             Session session = null;
875 
876             try {
877                 session = openSession();
878 
879                 StringBuilder query = new StringBuilder();
880 
881                 query.append("SELECT COUNT(*) ");
882                 query.append("FROM com.liferay.portal.model.Resource WHERE ");
883 
884                 query.append("codeId = ?");
885 
886                 query.append(" AND ");
887 
888                 if (primKey == null) {
889                     query.append("primKey IS NULL");
890                 }
891                 else {
892                     query.append("primKey = ?");
893                 }
894 
895                 query.append(" ");
896 
897                 Query q = session.createQuery(query.toString());
898 
899                 QueryPos qPos = QueryPos.getInstance(q);
900 
901                 qPos.add(codeId);
902 
903                 if (primKey != null) {
904                     qPos.add(primKey);
905                 }
906 
907                 count = (Long)q.uniqueResult();
908             }
909             catch (Exception e) {
910                 throw processException(e);
911             }
912             finally {
913                 if (count == null) {
914                     count = Long.valueOf(0);
915                 }
916 
917                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
918                     count);
919 
920                 closeSession(session);
921             }
922         }
923 
924         return count.intValue();
925     }
926 
927     public int countAll() throws SystemException {
928         Object[] finderArgs = new Object[0];
929 
930         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
931                 finderArgs, this);
932 
933         if (count == null) {
934             Session session = null;
935 
936             try {
937                 session = openSession();
938 
939                 Query q = session.createQuery(
940                         "SELECT COUNT(*) FROM com.liferay.portal.model.Resource");
941 
942                 count = (Long)q.uniqueResult();
943             }
944             catch (Exception e) {
945                 throw processException(e);
946             }
947             finally {
948                 if (count == null) {
949                     count = Long.valueOf(0);
950                 }
951 
952                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
953                     count);
954 
955                 closeSession(session);
956             }
957         }
958 
959         return count.intValue();
960     }
961 
962     public void afterPropertiesSet() {
963         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
964                     com.liferay.portal.util.PropsUtil.get(
965                         "value.object.listener.com.liferay.portal.model.Resource")));
966 
967         if (listenerClassNames.length > 0) {
968             try {
969                 List<ModelListener<Resource>> listenersList = new ArrayList<ModelListener<Resource>>();
970 
971                 for (String listenerClassName : listenerClassNames) {
972                     listenersList.add((ModelListener<Resource>)Class.forName(
973                             listenerClassName).newInstance());
974                 }
975 
976                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
977             }
978             catch (Exception e) {
979                 _log.error(e);
980             }
981         }
982     }
983 
984     @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
985     protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
986     @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
987     protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
988     @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
989     protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
990     @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
991     protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
992     @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
993     protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
994     @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
995     protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
996     @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
997     protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
998     @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
999     protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
1000    @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
1001    protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
1002    @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
1003    protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
1004    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
1005    protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
1006    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
1007    protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
1008    @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
1009    protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
1010    @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
1011    protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
1012    @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
1013    protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
1014    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
1015    protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1016    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
1017    protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
1018    @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
1019    protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
1020    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
1021    protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
1022    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
1023    protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1024    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
1025    protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
1026    @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
1027    protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
1028    @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
1029    protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
1030    @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
1031    protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
1032    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
1033    protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
1034    @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
1035    protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
1036    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
1037    protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
1038    @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
1039    protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
1040    @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
1041    protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
1042    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1043    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1044    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
1045    protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
1046    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
1047    protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
1048    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
1049    protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
1050    @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
1051    protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
1052    @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
1053    protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
1054    @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
1055    protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
1056    @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
1057    protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
1058    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1059    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1060    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
1061    protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
1062    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
1063    protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
1064    @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
1065    protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
1066    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
1067    protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
1068    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
1069    protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
1070    @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
1071    protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
1072    @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
1073    protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
1074    private static Log _log = LogFactoryUtil.getLog(ResourcePersistenceImpl.class);
1075}