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