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