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