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