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