1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderPath;
24  import com.liferay.portal.kernel.dao.orm.Query;
25  import com.liferay.portal.kernel.dao.orm.QueryPos;
26  import com.liferay.portal.kernel.dao.orm.QueryUtil;
27  import com.liferay.portal.kernel.dao.orm.Session;
28  import com.liferay.portal.kernel.exception.SystemException;
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     public Resource updateImpl(com.liferay.portal.model.Resource resource,
229         boolean merge) throws SystemException {
230         resource = toUnwrappedModel(resource);
231 
232         boolean isNew = resource.isNew();
233 
234         ResourceModelImpl resourceModelImpl = (ResourceModelImpl)resource;
235 
236         Session session = null;
237 
238         try {
239             session = openSession();
240 
241             BatchSessionUtil.update(session, resource, merge);
242 
243             resource.setNew(false);
244         }
245         catch (Exception e) {
246             throw processException(e);
247         }
248         finally {
249             closeSession(session);
250         }
251 
252         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
253 
254         EntityCacheUtil.putResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
255             ResourceImpl.class, resource.getPrimaryKey(), resource);
256 
257         if (!isNew &&
258                 ((resource.getCodeId() != resourceModelImpl.getOriginalCodeId()) ||
259                 !Validator.equals(resource.getPrimKey(),
260                     resourceModelImpl.getOriginalPrimKey()))) {
261             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_P,
262                 new Object[] {
263                     new Long(resourceModelImpl.getOriginalCodeId()),
264                     
265                 resourceModelImpl.getOriginalPrimKey()
266                 });
267         }
268 
269         if (isNew ||
270                 ((resource.getCodeId() != resourceModelImpl.getOriginalCodeId()) ||
271                 !Validator.equals(resource.getPrimKey(),
272                     resourceModelImpl.getOriginalPrimKey()))) {
273             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
274                 new Object[] {
275                     new Long(resource.getCodeId()),
276                     
277                 resource.getPrimKey()
278                 }, resource);
279         }
280 
281         return resource;
282     }
283 
284     protected Resource toUnwrappedModel(Resource resource) {
285         if (resource instanceof ResourceImpl) {
286             return resource;
287         }
288 
289         ResourceImpl resourceImpl = new ResourceImpl();
290 
291         resourceImpl.setNew(resource.isNew());
292         resourceImpl.setPrimaryKey(resource.getPrimaryKey());
293 
294         resourceImpl.setResourceId(resource.getResourceId());
295         resourceImpl.setCodeId(resource.getCodeId());
296         resourceImpl.setPrimKey(resource.getPrimKey());
297 
298         return resourceImpl;
299     }
300 
301     public Resource findByPrimaryKey(Serializable primaryKey)
302         throws NoSuchModelException, SystemException {
303         return findByPrimaryKey(((Long)primaryKey).longValue());
304     }
305 
306     public Resource findByPrimaryKey(long resourceId)
307         throws NoSuchResourceException, SystemException {
308         Resource resource = fetchByPrimaryKey(resourceId);
309 
310         if (resource == null) {
311             if (_log.isWarnEnabled()) {
312                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + resourceId);
313             }
314 
315             throw new NoSuchResourceException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
316                 resourceId);
317         }
318 
319         return resource;
320     }
321 
322     public Resource fetchByPrimaryKey(Serializable primaryKey)
323         throws SystemException {
324         return fetchByPrimaryKey(((Long)primaryKey).longValue());
325     }
326 
327     public Resource fetchByPrimaryKey(long resourceId)
328         throws SystemException {
329         Resource resource = (Resource)EntityCacheUtil.getResult(ResourceModelImpl.ENTITY_CACHE_ENABLED,
330                 ResourceImpl.class, resourceId, this);
331 
332         if (resource == null) {
333             Session session = null;
334 
335             try {
336                 session = openSession();
337 
338                 resource = (Resource)session.get(ResourceImpl.class,
339                         new Long(resourceId));
340             }
341             catch (Exception e) {
342                 throw processException(e);
343             }
344             finally {
345                 if (resource != null) {
346                     cacheResult(resource);
347                 }
348 
349                 closeSession(session);
350             }
351         }
352 
353         return resource;
354     }
355 
356     public List<Resource> findByCodeId(long codeId) throws SystemException {
357         Object[] finderArgs = new Object[] { new Long(codeId) };
358 
359         List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CODEID,
360                 finderArgs, this);
361 
362         if (list == null) {
363             Session session = null;
364 
365             try {
366                 session = openSession();
367 
368                 StringBundler query = new StringBundler(2);
369 
370                 query.append(_SQL_SELECT_RESOURCE_WHERE);
371 
372                 query.append(_FINDER_COLUMN_CODEID_CODEID_2);
373 
374                 String sql = query.toString();
375 
376                 Query q = session.createQuery(sql);
377 
378                 QueryPos qPos = QueryPos.getInstance(q);
379 
380                 qPos.add(codeId);
381 
382                 list = q.list();
383             }
384             catch (Exception e) {
385                 throw processException(e);
386             }
387             finally {
388                 if (list == null) {
389                     list = new ArrayList<Resource>();
390                 }
391 
392                 cacheResult(list);
393 
394                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CODEID,
395                     finderArgs, list);
396 
397                 closeSession(session);
398             }
399         }
400 
401         return list;
402     }
403 
404     public List<Resource> findByCodeId(long codeId, int start, int end)
405         throws SystemException {
406         return findByCodeId(codeId, start, end, null);
407     }
408 
409     public List<Resource> findByCodeId(long codeId, int start, int end,
410         OrderByComparator orderByComparator) throws SystemException {
411         Object[] finderArgs = new Object[] {
412                 new Long(codeId),
413                 
414                 String.valueOf(start), String.valueOf(end),
415                 String.valueOf(orderByComparator)
416             };
417 
418         List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_CODEID,
419                 finderArgs, this);
420 
421         if (list == null) {
422             Session session = null;
423 
424             try {
425                 session = openSession();
426 
427                 StringBundler query = null;
428 
429                 if (orderByComparator != null) {
430                     query = new StringBundler(3 +
431                             (orderByComparator.getOrderByFields().length * 3));
432                 }
433                 else {
434                     query = new StringBundler(2);
435                 }
436 
437                 query.append(_SQL_SELECT_RESOURCE_WHERE);
438 
439                 query.append(_FINDER_COLUMN_CODEID_CODEID_2);
440 
441                 if (orderByComparator != null) {
442                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
443                         orderByComparator);
444                 }
445 
446                 String sql = query.toString();
447 
448                 Query q = session.createQuery(sql);
449 
450                 QueryPos qPos = QueryPos.getInstance(q);
451 
452                 qPos.add(codeId);
453 
454                 list = (List<Resource>)QueryUtil.list(q, getDialect(), start,
455                         end);
456             }
457             catch (Exception e) {
458                 throw processException(e);
459             }
460             finally {
461                 if (list == null) {
462                     list = new ArrayList<Resource>();
463                 }
464 
465                 cacheResult(list);
466 
467                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_CODEID,
468                     finderArgs, list);
469 
470                 closeSession(session);
471             }
472         }
473 
474         return list;
475     }
476 
477     public Resource findByCodeId_First(long codeId,
478         OrderByComparator orderByComparator)
479         throws NoSuchResourceException, SystemException {
480         List<Resource> list = findByCodeId(codeId, 0, 1, orderByComparator);
481 
482         if (list.isEmpty()) {
483             StringBundler msg = new StringBundler(4);
484 
485             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
486 
487             msg.append("codeId=");
488             msg.append(codeId);
489 
490             msg.append(StringPool.CLOSE_CURLY_BRACE);
491 
492             throw new NoSuchResourceException(msg.toString());
493         }
494         else {
495             return list.get(0);
496         }
497     }
498 
499     public Resource findByCodeId_Last(long codeId,
500         OrderByComparator orderByComparator)
501         throws NoSuchResourceException, SystemException {
502         int count = countByCodeId(codeId);
503 
504         List<Resource> list = findByCodeId(codeId, count - 1, count,
505                 orderByComparator);
506 
507         if (list.isEmpty()) {
508             StringBundler msg = new StringBundler(4);
509 
510             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
511 
512             msg.append("codeId=");
513             msg.append(codeId);
514 
515             msg.append(StringPool.CLOSE_CURLY_BRACE);
516 
517             throw new NoSuchResourceException(msg.toString());
518         }
519         else {
520             return list.get(0);
521         }
522     }
523 
524     public Resource[] findByCodeId_PrevAndNext(long resourceId, long codeId,
525         OrderByComparator orderByComparator)
526         throws NoSuchResourceException, SystemException {
527         Resource resource = findByPrimaryKey(resourceId);
528 
529         int count = countByCodeId(codeId);
530 
531         Session session = null;
532 
533         try {
534             session = openSession();
535 
536             StringBundler query = null;
537 
538             if (orderByComparator != null) {
539                 query = new StringBundler(3 +
540                         (orderByComparator.getOrderByFields().length * 3));
541             }
542             else {
543                 query = new StringBundler(2);
544             }
545 
546             query.append(_SQL_SELECT_RESOURCE_WHERE);
547 
548             query.append(_FINDER_COLUMN_CODEID_CODEID_2);
549 
550             if (orderByComparator != null) {
551                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
552                     orderByComparator);
553             }
554 
555             String sql = query.toString();
556 
557             Query q = session.createQuery(sql);
558 
559             QueryPos qPos = QueryPos.getInstance(q);
560 
561             qPos.add(codeId);
562 
563             Object[] objArray = QueryUtil.getPrevAndNext(q, count,
564                     orderByComparator, resource);
565 
566             Resource[] array = new ResourceImpl[3];
567 
568             array[0] = (Resource)objArray[0];
569             array[1] = (Resource)objArray[1];
570             array[2] = (Resource)objArray[2];
571 
572             return array;
573         }
574         catch (Exception e) {
575             throw processException(e);
576         }
577         finally {
578             closeSession(session);
579         }
580     }
581 
582     public Resource findByC_P(long codeId, String primKey)
583         throws NoSuchResourceException, SystemException {
584         Resource resource = fetchByC_P(codeId, primKey);
585 
586         if (resource == null) {
587             StringBundler msg = new StringBundler(6);
588 
589             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
590 
591             msg.append("codeId=");
592             msg.append(codeId);
593 
594             msg.append(", primKey=");
595             msg.append(primKey);
596 
597             msg.append(StringPool.CLOSE_CURLY_BRACE);
598 
599             if (_log.isWarnEnabled()) {
600                 _log.warn(msg.toString());
601             }
602 
603             throw new NoSuchResourceException(msg.toString());
604         }
605 
606         return resource;
607     }
608 
609     public Resource fetchByC_P(long codeId, String primKey)
610         throws SystemException {
611         return fetchByC_P(codeId, primKey, true);
612     }
613 
614     public Resource fetchByC_P(long codeId, String primKey,
615         boolean retrieveFromCache) throws SystemException {
616         Object[] finderArgs = new Object[] { new Long(codeId), primKey };
617 
618         Object result = null;
619 
620         if (retrieveFromCache) {
621             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_P,
622                     finderArgs, this);
623         }
624 
625         if (result == null) {
626             Session session = null;
627 
628             try {
629                 session = openSession();
630 
631                 StringBundler query = new StringBundler(3);
632 
633                 query.append(_SQL_SELECT_RESOURCE_WHERE);
634 
635                 query.append(_FINDER_COLUMN_C_P_CODEID_2);
636 
637                 if (primKey == null) {
638                     query.append(_FINDER_COLUMN_C_P_PRIMKEY_1);
639                 }
640                 else {
641                     if (primKey.equals(StringPool.BLANK)) {
642                         query.append(_FINDER_COLUMN_C_P_PRIMKEY_3);
643                     }
644                     else {
645                         query.append(_FINDER_COLUMN_C_P_PRIMKEY_2);
646                     }
647                 }
648 
649                 String sql = query.toString();
650 
651                 Query q = session.createQuery(sql);
652 
653                 QueryPos qPos = QueryPos.getInstance(q);
654 
655                 qPos.add(codeId);
656 
657                 if (primKey != null) {
658                     qPos.add(primKey);
659                 }
660 
661                 List<Resource> list = q.list();
662 
663                 result = list;
664 
665                 Resource resource = null;
666 
667                 if (list.isEmpty()) {
668                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
669                         finderArgs, list);
670                 }
671                 else {
672                     resource = list.get(0);
673 
674                     cacheResult(resource);
675 
676                     if ((resource.getCodeId() != codeId) ||
677                             (resource.getPrimKey() == null) ||
678                             !resource.getPrimKey().equals(primKey)) {
679                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
680                             finderArgs, resource);
681                     }
682                 }
683 
684                 return resource;
685             }
686             catch (Exception e) {
687                 throw processException(e);
688             }
689             finally {
690                 if (result == null) {
691                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_P,
692                         finderArgs, new ArrayList<Resource>());
693                 }
694 
695                 closeSession(session);
696             }
697         }
698         else {
699             if (result instanceof List<?>) {
700                 return null;
701             }
702             else {
703                 return (Resource)result;
704             }
705         }
706     }
707 
708     public List<Resource> findAll() throws SystemException {
709         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
710     }
711 
712     public List<Resource> findAll(int start, int end) throws SystemException {
713         return findAll(start, end, null);
714     }
715 
716     public List<Resource> findAll(int start, int end,
717         OrderByComparator orderByComparator) throws SystemException {
718         Object[] finderArgs = new Object[] {
719                 String.valueOf(start), String.valueOf(end),
720                 String.valueOf(orderByComparator)
721             };
722 
723         List<Resource> list = (List<Resource>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
724                 finderArgs, this);
725 
726         if (list == null) {
727             Session session = null;
728 
729             try {
730                 session = openSession();
731 
732                 StringBundler query = null;
733                 String sql = null;
734 
735                 if (orderByComparator != null) {
736                     query = new StringBundler(2 +
737                             (orderByComparator.getOrderByFields().length * 3));
738 
739                     query.append(_SQL_SELECT_RESOURCE);
740 
741                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
742                         orderByComparator);
743 
744                     sql = query.toString();
745                 }
746 
747                 sql = _SQL_SELECT_RESOURCE;
748 
749                 Query q = session.createQuery(sql);
750 
751                 if (orderByComparator == null) {
752                     list = (List<Resource>)QueryUtil.list(q, getDialect(),
753                             start, end, false);
754 
755                     Collections.sort(list);
756                 }
757                 else {
758                     list = (List<Resource>)QueryUtil.list(q, getDialect(),
759                             start, end);
760                 }
761             }
762             catch (Exception e) {
763                 throw processException(e);
764             }
765             finally {
766                 if (list == null) {
767                     list = new ArrayList<Resource>();
768                 }
769 
770                 cacheResult(list);
771 
772                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
773 
774                 closeSession(session);
775             }
776         }
777 
778         return list;
779     }
780 
781     public void removeByCodeId(long codeId) throws SystemException {
782         for (Resource resource : findByCodeId(codeId)) {
783             remove(resource);
784         }
785     }
786 
787     public void removeByC_P(long codeId, String primKey)
788         throws NoSuchResourceException, SystemException {
789         Resource resource = findByC_P(codeId, primKey);
790 
791         remove(resource);
792     }
793 
794     public void removeAll() throws SystemException {
795         for (Resource resource : findAll()) {
796             remove(resource);
797         }
798     }
799 
800     public int countByCodeId(long codeId) throws SystemException {
801         Object[] finderArgs = new Object[] { new Long(codeId) };
802 
803         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CODEID,
804                 finderArgs, this);
805 
806         if (count == null) {
807             Session session = null;
808 
809             try {
810                 session = openSession();
811 
812                 StringBundler query = new StringBundler(2);
813 
814                 query.append(_SQL_COUNT_RESOURCE_WHERE);
815 
816                 query.append(_FINDER_COLUMN_CODEID_CODEID_2);
817 
818                 String sql = query.toString();
819 
820                 Query q = session.createQuery(sql);
821 
822                 QueryPos qPos = QueryPos.getInstance(q);
823 
824                 qPos.add(codeId);
825 
826                 count = (Long)q.uniqueResult();
827             }
828             catch (Exception e) {
829                 throw processException(e);
830             }
831             finally {
832                 if (count == null) {
833                     count = Long.valueOf(0);
834                 }
835 
836                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CODEID,
837                     finderArgs, count);
838 
839                 closeSession(session);
840             }
841         }
842 
843         return count.intValue();
844     }
845 
846     public int countByC_P(long codeId, String primKey)
847         throws SystemException {
848         Object[] finderArgs = new Object[] { new Long(codeId), primKey };
849 
850         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
851                 finderArgs, this);
852 
853         if (count == null) {
854             Session session = null;
855 
856             try {
857                 session = openSession();
858 
859                 StringBundler query = new StringBundler(3);
860 
861                 query.append(_SQL_COUNT_RESOURCE_WHERE);
862 
863                 query.append(_FINDER_COLUMN_C_P_CODEID_2);
864 
865                 if (primKey == null) {
866                     query.append(_FINDER_COLUMN_C_P_PRIMKEY_1);
867                 }
868                 else {
869                     if (primKey.equals(StringPool.BLANK)) {
870                         query.append(_FINDER_COLUMN_C_P_PRIMKEY_3);
871                     }
872                     else {
873                         query.append(_FINDER_COLUMN_C_P_PRIMKEY_2);
874                     }
875                 }
876 
877                 String sql = query.toString();
878 
879                 Query q = session.createQuery(sql);
880 
881                 QueryPos qPos = QueryPos.getInstance(q);
882 
883                 qPos.add(codeId);
884 
885                 if (primKey != null) {
886                     qPos.add(primKey);
887                 }
888 
889                 count = (Long)q.uniqueResult();
890             }
891             catch (Exception e) {
892                 throw processException(e);
893             }
894             finally {
895                 if (count == null) {
896                     count = Long.valueOf(0);
897                 }
898 
899                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
900                     count);
901 
902                 closeSession(session);
903             }
904         }
905 
906         return count.intValue();
907     }
908 
909     public int countAll() throws SystemException {
910         Object[] finderArgs = new Object[0];
911 
912         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
913                 finderArgs, this);
914 
915         if (count == null) {
916             Session session = null;
917 
918             try {
919                 session = openSession();
920 
921                 Query q = session.createQuery(_SQL_COUNT_RESOURCE);
922 
923                 count = (Long)q.uniqueResult();
924             }
925             catch (Exception e) {
926                 throw processException(e);
927             }
928             finally {
929                 if (count == null) {
930                     count = Long.valueOf(0);
931                 }
932 
933                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
934                     count);
935 
936                 closeSession(session);
937             }
938         }
939 
940         return count.intValue();
941     }
942 
943     public void afterPropertiesSet() {
944         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
945                     com.liferay.portal.util.PropsUtil.get(
946                         "value.object.listener.com.liferay.portal.model.Resource")));
947 
948         if (listenerClassNames.length > 0) {
949             try {
950                 List<ModelListener<Resource>> listenersList = new ArrayList<ModelListener<Resource>>();
951 
952                 for (String listenerClassName : listenerClassNames) {
953                     listenersList.add((ModelListener<Resource>)Class.forName(
954                             listenerClassName).newInstance());
955                 }
956 
957                 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
958             }
959             catch (Exception e) {
960                 _log.error(e);
961             }
962         }
963     }
964 
965     @BeanReference(type = AccountPersistence.class)
966     protected AccountPersistence accountPersistence;
967     @BeanReference(type = AddressPersistence.class)
968     protected AddressPersistence addressPersistence;
969     @BeanReference(type = BrowserTrackerPersistence.class)
970     protected BrowserTrackerPersistence browserTrackerPersistence;
971     @BeanReference(type = ClassNamePersistence.class)
972     protected ClassNamePersistence classNamePersistence;
973     @BeanReference(type = CompanyPersistence.class)
974     protected CompanyPersistence companyPersistence;
975     @BeanReference(type = ContactPersistence.class)
976     protected ContactPersistence contactPersistence;
977     @BeanReference(type = CountryPersistence.class)
978     protected CountryPersistence countryPersistence;
979     @BeanReference(type = EmailAddressPersistence.class)
980     protected EmailAddressPersistence emailAddressPersistence;
981     @BeanReference(type = GroupPersistence.class)
982     protected GroupPersistence groupPersistence;
983     @BeanReference(type = ImagePersistence.class)
984     protected ImagePersistence imagePersistence;
985     @BeanReference(type = LayoutPersistence.class)
986     protected LayoutPersistence layoutPersistence;
987     @BeanReference(type = LayoutPrototypePersistence.class)
988     protected LayoutPrototypePersistence layoutPrototypePersistence;
989     @BeanReference(type = LayoutSetPersistence.class)
990     protected LayoutSetPersistence layoutSetPersistence;
991     @BeanReference(type = LayoutSetPrototypePersistence.class)
992     protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
993     @BeanReference(type = ListTypePersistence.class)
994     protected ListTypePersistence listTypePersistence;
995     @BeanReference(type = LockPersistence.class)
996     protected LockPersistence lockPersistence;
997     @BeanReference(type = MembershipRequestPersistence.class)
998     protected MembershipRequestPersistence membershipRequestPersistence;
999     @BeanReference(type = OrganizationPersistence.class)
1000    protected OrganizationPersistence organizationPersistence;
1001    @BeanReference(type = OrgGroupPermissionPersistence.class)
1002    protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1003    @BeanReference(type = OrgGroupRolePersistence.class)
1004    protected OrgGroupRolePersistence orgGroupRolePersistence;
1005    @BeanReference(type = OrgLaborPersistence.class)
1006    protected OrgLaborPersistence orgLaborPersistence;
1007    @BeanReference(type = PasswordPolicyPersistence.class)
1008    protected PasswordPolicyPersistence passwordPolicyPersistence;
1009    @BeanReference(type = PasswordPolicyRelPersistence.class)
1010    protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1011    @BeanReference(type = PasswordTrackerPersistence.class)
1012    protected PasswordTrackerPersistence passwordTrackerPersistence;
1013    @BeanReference(type = PermissionPersistence.class)
1014    protected PermissionPersistence permissionPersistence;
1015    @BeanReference(type = PhonePersistence.class)
1016    protected PhonePersistence phonePersistence;
1017    @BeanReference(type = PluginSettingPersistence.class)
1018    protected PluginSettingPersistence pluginSettingPersistence;
1019    @BeanReference(type = PortletPersistence.class)
1020    protected PortletPersistence portletPersistence;
1021    @BeanReference(type = PortletItemPersistence.class)
1022    protected PortletItemPersistence portletItemPersistence;
1023    @BeanReference(type = PortletPreferencesPersistence.class)
1024    protected PortletPreferencesPersistence portletPreferencesPersistence;
1025    @BeanReference(type = RegionPersistence.class)
1026    protected RegionPersistence regionPersistence;
1027    @BeanReference(type = ReleasePersistence.class)
1028    protected ReleasePersistence releasePersistence;
1029    @BeanReference(type = ResourcePersistence.class)
1030    protected ResourcePersistence resourcePersistence;
1031    @BeanReference(type = ResourceActionPersistence.class)
1032    protected ResourceActionPersistence resourceActionPersistence;
1033    @BeanReference(type = ResourceCodePersistence.class)
1034    protected ResourceCodePersistence resourceCodePersistence;
1035    @BeanReference(type = ResourcePermissionPersistence.class)
1036    protected ResourcePermissionPersistence resourcePermissionPersistence;
1037    @BeanReference(type = RolePersistence.class)
1038    protected RolePersistence rolePersistence;
1039    @BeanReference(type = ServiceComponentPersistence.class)
1040    protected ServiceComponentPersistence serviceComponentPersistence;
1041    @BeanReference(type = ShardPersistence.class)
1042    protected ShardPersistence shardPersistence;
1043    @BeanReference(type = SubscriptionPersistence.class)
1044    protected SubscriptionPersistence subscriptionPersistence;
1045    @BeanReference(type = TeamPersistence.class)
1046    protected TeamPersistence teamPersistence;
1047    @BeanReference(type = UserPersistence.class)
1048    protected UserPersistence userPersistence;
1049    @BeanReference(type = UserGroupPersistence.class)
1050    protected UserGroupPersistence userGroupPersistence;
1051    @BeanReference(type = UserGroupGroupRolePersistence.class)
1052    protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1053    @BeanReference(type = UserGroupRolePersistence.class)
1054    protected UserGroupRolePersistence userGroupRolePersistence;
1055    @BeanReference(type = UserIdMapperPersistence.class)
1056    protected UserIdMapperPersistence userIdMapperPersistence;
1057    @BeanReference(type = UserTrackerPersistence.class)
1058    protected UserTrackerPersistence userTrackerPersistence;
1059    @BeanReference(type = UserTrackerPathPersistence.class)
1060    protected UserTrackerPathPersistence userTrackerPathPersistence;
1061    @BeanReference(type = WebDAVPropsPersistence.class)
1062    protected WebDAVPropsPersistence webDAVPropsPersistence;
1063    @BeanReference(type = WebsitePersistence.class)
1064    protected WebsitePersistence websitePersistence;
1065    @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1066    protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1067    @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1068    protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1069    private static final String _SQL_SELECT_RESOURCE = "SELECT resource FROM Resource resource";
1070    private static final String _SQL_SELECT_RESOURCE_WHERE = "SELECT resource FROM Resource resource WHERE ";
1071    private static final String _SQL_COUNT_RESOURCE = "SELECT COUNT(resource) FROM Resource resource";
1072    private static final String _SQL_COUNT_RESOURCE_WHERE = "SELECT COUNT(resource) FROM Resource resource WHERE ";
1073    private static final String _FINDER_COLUMN_CODEID_CODEID_2 = "resource.codeId = ?";
1074    private static final String _FINDER_COLUMN_C_P_CODEID_2 = "resource.codeId = ? AND ";
1075    private static final String _FINDER_COLUMN_C_P_PRIMKEY_1 = "resource.primKey IS NULL";
1076    private static final String _FINDER_COLUMN_C_P_PRIMKEY_2 = "resource.primKey = ?";
1077    private static final String _FINDER_COLUMN_C_P_PRIMKEY_3 = "(resource.primKey IS NULL OR resource.primKey = ?)";
1078    private static final String _ORDER_BY_ENTITY_ALIAS = "resource.";
1079    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Resource exists with the primary key ";
1080    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Resource exists with the key {";
1081    private static Log _log = LogFactoryUtil.getLog(ResourcePersistenceImpl.class);
1082}