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