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