1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.model.Resource;
40  import com.liferay.portal.model.impl.ResourceImpl;
41  import com.liferay.portal.model.impl.ResourceModelImpl;
42  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.Iterator;
50  import java.util.List;
51  
52  /**
53   * <a href="ResourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class ResourcePersistenceImpl extends BasePersistenceImpl
59      implements ResourcePersistence {
60      public Resource create(long resourceId) {
61          Resource resource = new ResourceImpl();
62  
63          resource.setNew(true);
64          resource.setPrimaryKey(resourceId);
65  
66          return resource;
67      }
68  
69      public Resource remove(long resourceId)
70          throws NoSuchResourceException, SystemException {
71          Session session = null;
72  
73          try {
74              session = openSession();
75  
76              Resource resource = (Resource)session.get(ResourceImpl.class,
77                      new Long(resourceId));
78  
79              if (resource == null) {
80                  if (_log.isWarnEnabled()) {
81                      _log.warn("No Resource exists with the primary key " +
82                          resourceId);
83                  }
84  
85                  throw new NoSuchResourceException(
86                      "No Resource exists with the primary key " + resourceId);
87              }
88  
89              return remove(resource);
90          }
91          catch (NoSuchResourceException nsee) {
92              throw nsee;
93          }
94          catch (Exception e) {
95              throw processException(e);
96          }
97          finally {
98              closeSession(session);
99          }
100     }
101 
102     public Resource remove(Resource resource) throws SystemException {
103         if (_listeners.length > 0) {
104             for (ModelListener listener : _listeners) {
105                 listener.onBeforeRemove(resource);
106             }
107         }
108 
109         resource = removeImpl(resource);
110 
111         if (_listeners.length > 0) {
112             for (ModelListener listener : _listeners) {
113                 listener.onAfterRemove(resource);
114             }
115         }
116 
117         return resource;
118     }
119 
120     protected Resource removeImpl(Resource resource) throws SystemException {
121         Session session = null;
122 
123         try {
124             session = openSession();
125 
126             if (BatchSessionUtil.isEnabled()) {
127                 Object staleObject = session.get(ResourceImpl.class,
128                         resource.getPrimaryKeyObj());
129 
130                 if (staleObject != null) {
131                     session.evict(staleObject);
132                 }
133             }
134 
135             session.delete(resource);
136 
137             session.flush();
138 
139             return resource;
140         }
141         catch (Exception e) {
142             throw processException(e);
143         }
144         finally {
145             closeSession(session);
146 
147             FinderCacheUtil.clearCache(Resource.class.getName());
148         }
149     }
150 
151     /**
152      * @deprecated Use <code>update(Resource resource, boolean merge)</code>.
153      */
154     public Resource update(Resource resource) throws SystemException {
155         if (_log.isWarnEnabled()) {
156             _log.warn(
157                 "Using the deprecated update(Resource resource) method. Use update(Resource resource, boolean merge) instead.");
158         }
159 
160         return update(resource, false);
161     }
162 
163     /**
164      * Add, update, or merge, the entity. This method also calls the model
165      * listeners to trigger the proper events associated with adding, deleting,
166      * or updating an entity.
167      *
168      * @param        resource the entity to add, update, or merge
169      * @param        merge boolean value for whether to merge the entity. The
170      *                default value is false. Setting merge to true is more
171      *                expensive and should only be true when resource is
172      *                transient. See LEP-5473 for a detailed discussion of this
173      *                method.
174      * @return        true if the portlet can be displayed via Ajax
175      */
176     public Resource update(Resource resource, boolean merge)
177         throws SystemException {
178         boolean isNew = resource.isNew();
179 
180         if (_listeners.length > 0) {
181             for (ModelListener listener : _listeners) {
182                 if (isNew) {
183                     listener.onBeforeCreate(resource);
184                 }
185                 else {
186                     listener.onBeforeUpdate(resource);
187                 }
188             }
189         }
190 
191         resource = updateImpl(resource, merge);
192 
193         if (_listeners.length > 0) {
194             for (ModelListener listener : _listeners) {
195                 if (isNew) {
196                     listener.onAfterCreate(resource);
197                 }
198                 else {
199                     listener.onAfterUpdate(resource);
200                 }
201             }
202         }
203 
204         return resource;
205     }
206 
207     public Resource updateImpl(com.liferay.portal.model.Resource resource,
208         boolean merge) throws SystemException {
209         Session session = null;
210 
211         try {
212             session = openSession();
213 
214             BatchSessionUtil.update(session, resource, merge);
215 
216             resource.setNew(false);
217 
218             return resource;
219         }
220         catch (Exception e) {
221             throw processException(e);
222         }
223         finally {
224             closeSession(session);
225 
226             FinderCacheUtil.clearCache(Resource.class.getName());
227         }
228     }
229 
230     public Resource findByPrimaryKey(long resourceId)
231         throws NoSuchResourceException, SystemException {
232         Resource resource = fetchByPrimaryKey(resourceId);
233 
234         if (resource == null) {
235             if (_log.isWarnEnabled()) {
236                 _log.warn("No Resource exists with the primary key " +
237                     resourceId);
238             }
239 
240             throw new NoSuchResourceException(
241                 "No Resource exists with the primary key " + resourceId);
242         }
243 
244         return resource;
245     }
246 
247     public Resource fetchByPrimaryKey(long resourceId)
248         throws SystemException {
249         Session session = null;
250 
251         try {
252             session = openSession();
253 
254             return (Resource)session.get(ResourceImpl.class,
255                 new Long(resourceId));
256         }
257         catch (Exception e) {
258             throw processException(e);
259         }
260         finally {
261             closeSession(session);
262         }
263     }
264 
265     public List<Resource> findByCodeId(long codeId) throws SystemException {
266         boolean finderClassNameCacheEnabled = ResourceModelImpl.CACHE_ENABLED;
267         String finderClassName = Resource.class.getName();
268         String finderMethodName = "findByCodeId";
269         String[] finderParams = new String[] { Long.class.getName() };
270         Object[] finderArgs = new Object[] { new Long(codeId) };
271 
272         Object result = null;
273 
274         if (finderClassNameCacheEnabled) {
275             result = FinderCacheUtil.getResult(finderClassName,
276                     finderMethodName, finderParams, finderArgs, this);
277         }
278 
279         if (result == null) {
280             Session session = null;
281 
282             try {
283                 session = openSession();
284 
285                 StringBuilder query = new StringBuilder();
286 
287                 query.append("FROM com.liferay.portal.model.Resource WHERE ");
288 
289                 query.append("codeId = ?");
290 
291                 query.append(" ");
292 
293                 Query q = session.createQuery(query.toString());
294 
295                 QueryPos qPos = QueryPos.getInstance(q);
296 
297                 qPos.add(codeId);
298 
299                 List<Resource> list = q.list();
300 
301                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
302                     finderClassName, finderMethodName, finderParams,
303                     finderArgs, list);
304 
305                 return list;
306             }
307             catch (Exception e) {
308                 throw processException(e);
309             }
310             finally {
311                 closeSession(session);
312             }
313         }
314         else {
315             return (List<Resource>)result;
316         }
317     }
318 
319     public List<Resource> findByCodeId(long codeId, int start, int end)
320         throws SystemException {
321         return findByCodeId(codeId, start, end, null);
322     }
323 
324     public List<Resource> findByCodeId(long codeId, int start, int end,
325         OrderByComparator obc) throws SystemException {
326         boolean finderClassNameCacheEnabled = ResourceModelImpl.CACHE_ENABLED;
327         String finderClassName = Resource.class.getName();
328         String finderMethodName = "findByCodeId";
329         String[] finderParams = new String[] {
330                 Long.class.getName(),
331                 
332                 "java.lang.Integer", "java.lang.Integer",
333                 "com.liferay.portal.kernel.util.OrderByComparator"
334             };
335         Object[] finderArgs = new Object[] {
336                 new Long(codeId),
337                 
338                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
339             };
340 
341         Object result = null;
342 
343         if (finderClassNameCacheEnabled) {
344             result = FinderCacheUtil.getResult(finderClassName,
345                     finderMethodName, finderParams, finderArgs, this);
346         }
347 
348         if (result == null) {
349             Session session = null;
350 
351             try {
352                 session = openSession();
353 
354                 StringBuilder query = new StringBuilder();
355 
356                 query.append("FROM com.liferay.portal.model.Resource WHERE ");
357 
358                 query.append("codeId = ?");
359 
360                 query.append(" ");
361 
362                 if (obc != null) {
363                     query.append("ORDER BY ");
364                     query.append(obc.getOrderBy());
365                 }
366 
367                 Query q = session.createQuery(query.toString());
368 
369                 QueryPos qPos = QueryPos.getInstance(q);
370 
371                 qPos.add(codeId);
372 
373                 List<Resource> list = (List<Resource>)QueryUtil.list(q,
374                         getDialect(), start, end);
375 
376                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
377                     finderClassName, finderMethodName, finderParams,
378                     finderArgs, list);
379 
380                 return list;
381             }
382             catch (Exception e) {
383                 throw processException(e);
384             }
385             finally {
386                 closeSession(session);
387             }
388         }
389         else {
390             return (List<Resource>)result;
391         }
392     }
393 
394     public Resource findByCodeId_First(long codeId, OrderByComparator obc)
395         throws NoSuchResourceException, SystemException {
396         List<Resource> list = findByCodeId(codeId, 0, 1, obc);
397 
398         if (list.size() == 0) {
399             StringBuilder msg = new StringBuilder();
400 
401             msg.append("No Resource exists with the key {");
402 
403             msg.append("codeId=" + codeId);
404 
405             msg.append(StringPool.CLOSE_CURLY_BRACE);
406 
407             throw new NoSuchResourceException(msg.toString());
408         }
409         else {
410             return list.get(0);
411         }
412     }
413 
414     public Resource findByCodeId_Last(long codeId, OrderByComparator obc)
415         throws NoSuchResourceException, SystemException {
416         int count = countByCodeId(codeId);
417 
418         List<Resource> list = findByCodeId(codeId, count - 1, count, obc);
419 
420         if (list.size() == 0) {
421             StringBuilder msg = new StringBuilder();
422 
423             msg.append("No Resource exists with the key {");
424 
425             msg.append("codeId=" + codeId);
426 
427             msg.append(StringPool.CLOSE_CURLY_BRACE);
428 
429             throw new NoSuchResourceException(msg.toString());
430         }
431         else {
432             return list.get(0);
433         }
434     }
435 
436     public Resource[] findByCodeId_PrevAndNext(long resourceId, long codeId,
437         OrderByComparator obc) throws NoSuchResourceException, SystemException {
438         Resource resource = findByPrimaryKey(resourceId);
439 
440         int count = countByCodeId(codeId);
441 
442         Session session = null;
443 
444         try {
445             session = openSession();
446 
447             StringBuilder query = new StringBuilder();
448 
449             query.append("FROM com.liferay.portal.model.Resource WHERE ");
450 
451             query.append("codeId = ?");
452 
453             query.append(" ");
454 
455             if (obc != null) {
456                 query.append("ORDER BY ");
457                 query.append(obc.getOrderBy());
458             }
459 
460             Query q = session.createQuery(query.toString());
461 
462             QueryPos qPos = QueryPos.getInstance(q);
463 
464             qPos.add(codeId);
465 
466             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, resource);
467 
468             Resource[] array = new ResourceImpl[3];
469 
470             array[0] = (Resource)objArray[0];
471             array[1] = (Resource)objArray[1];
472             array[2] = (Resource)objArray[2];
473 
474             return array;
475         }
476         catch (Exception e) {
477             throw processException(e);
478         }
479         finally {
480             closeSession(session);
481         }
482     }
483 
484     public Resource findByC_P(long codeId, String primKey)
485         throws NoSuchResourceException, SystemException {
486         Resource resource = fetchByC_P(codeId, primKey);
487 
488         if (resource == null) {
489             StringBuilder msg = new StringBuilder();
490 
491             msg.append("No Resource exists with the key {");
492 
493             msg.append("codeId=" + codeId);
494 
495             msg.append(", ");
496             msg.append("primKey=" + primKey);
497 
498             msg.append(StringPool.CLOSE_CURLY_BRACE);
499 
500             if (_log.isWarnEnabled()) {
501                 _log.warn(msg.toString());
502             }
503 
504             throw new NoSuchResourceException(msg.toString());
505         }
506 
507         return resource;
508     }
509 
510     public Resource fetchByC_P(long codeId, String primKey)
511         throws SystemException {
512         boolean finderClassNameCacheEnabled = ResourceModelImpl.CACHE_ENABLED;
513         String finderClassName = Resource.class.getName();
514         String finderMethodName = "fetchByC_P";
515         String[] finderParams = new String[] {
516                 Long.class.getName(), String.class.getName()
517             };
518         Object[] finderArgs = new Object[] { new Long(codeId), primKey };
519 
520         Object result = null;
521 
522         if (finderClassNameCacheEnabled) {
523             result = FinderCacheUtil.getResult(finderClassName,
524                     finderMethodName, finderParams, finderArgs, this);
525         }
526 
527         if (result == null) {
528             Session session = null;
529 
530             try {
531                 session = openSession();
532 
533                 StringBuilder query = new StringBuilder();
534 
535                 query.append("FROM com.liferay.portal.model.Resource WHERE ");
536 
537                 query.append("codeId = ?");
538 
539                 query.append(" AND ");
540 
541                 if (primKey == null) {
542                     query.append("primKey IS NULL");
543                 }
544                 else {
545                     query.append("primKey = ?");
546                 }
547 
548                 query.append(" ");
549 
550                 Query q = session.createQuery(query.toString());
551 
552                 QueryPos qPos = QueryPos.getInstance(q);
553 
554                 qPos.add(codeId);
555 
556                 if (primKey != null) {
557                     qPos.add(primKey);
558                 }
559 
560                 List<Resource> list = q.list();
561 
562                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
563                     finderClassName, finderMethodName, finderParams,
564                     finderArgs, list);
565 
566                 if (list.size() == 0) {
567                     return null;
568                 }
569                 else {
570                     return list.get(0);
571                 }
572             }
573             catch (Exception e) {
574                 throw processException(e);
575             }
576             finally {
577                 closeSession(session);
578             }
579         }
580         else {
581             List<Resource> list = (List<Resource>)result;
582 
583             if (list.size() == 0) {
584                 return null;
585             }
586             else {
587                 return list.get(0);
588             }
589         }
590     }
591 
592     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
593         throws SystemException {
594         Session session = null;
595 
596         try {
597             session = openSession();
598 
599             dynamicQuery.compile(session);
600 
601             return dynamicQuery.list();
602         }
603         catch (Exception e) {
604             throw processException(e);
605         }
606         finally {
607             closeSession(session);
608         }
609     }
610 
611     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
612         int start, int end) throws SystemException {
613         Session session = null;
614 
615         try {
616             session = openSession();
617 
618             dynamicQuery.setLimit(start, end);
619 
620             dynamicQuery.compile(session);
621 
622             return dynamicQuery.list();
623         }
624         catch (Exception e) {
625             throw processException(e);
626         }
627         finally {
628             closeSession(session);
629         }
630     }
631 
632     public List<Resource> findAll() throws SystemException {
633         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
634     }
635 
636     public List<Resource> findAll(int start, int end) throws SystemException {
637         return findAll(start, end, null);
638     }
639 
640     public List<Resource> findAll(int start, int end, OrderByComparator obc)
641         throws SystemException {
642         boolean finderClassNameCacheEnabled = ResourceModelImpl.CACHE_ENABLED;
643         String finderClassName = Resource.class.getName();
644         String finderMethodName = "findAll";
645         String[] finderParams = new String[] {
646                 "java.lang.Integer", "java.lang.Integer",
647                 "com.liferay.portal.kernel.util.OrderByComparator"
648             };
649         Object[] finderArgs = new Object[] {
650                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
651             };
652 
653         Object result = null;
654 
655         if (finderClassNameCacheEnabled) {
656             result = FinderCacheUtil.getResult(finderClassName,
657                     finderMethodName, finderParams, finderArgs, this);
658         }
659 
660         if (result == null) {
661             Session session = null;
662 
663             try {
664                 session = openSession();
665 
666                 StringBuilder query = new StringBuilder();
667 
668                 query.append("FROM com.liferay.portal.model.Resource ");
669 
670                 if (obc != null) {
671                     query.append("ORDER BY ");
672                     query.append(obc.getOrderBy());
673                 }
674 
675                 Query q = session.createQuery(query.toString());
676 
677                 List<Resource> list = null;
678 
679                 if (obc == null) {
680                     list = (List<Resource>)QueryUtil.list(q, getDialect(),
681                             start, end, false);
682 
683                     Collections.sort(list);
684                 }
685                 else {
686                     list = (List<Resource>)QueryUtil.list(q, getDialect(),
687                             start, end);
688                 }
689 
690                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
691                     finderClassName, finderMethodName, finderParams,
692                     finderArgs, list);
693 
694                 return list;
695             }
696             catch (Exception e) {
697                 throw processException(e);
698             }
699             finally {
700                 closeSession(session);
701             }
702         }
703         else {
704             return (List<Resource>)result;
705         }
706     }
707 
708     public void removeByCodeId(long codeId) throws SystemException {
709         for (Resource resource : findByCodeId(codeId)) {
710             remove(resource);
711         }
712     }
713 
714     public void removeByC_P(long codeId, String primKey)
715         throws NoSuchResourceException, SystemException {
716         Resource resource = findByC_P(codeId, primKey);
717 
718         remove(resource);
719     }
720 
721     public void removeAll() throws SystemException {
722         for (Resource resource : findAll()) {
723             remove(resource);
724         }
725     }
726 
727     public int countByCodeId(long codeId) throws SystemException {
728         boolean finderClassNameCacheEnabled = ResourceModelImpl.CACHE_ENABLED;
729         String finderClassName = Resource.class.getName();
730         String finderMethodName = "countByCodeId";
731         String[] finderParams = new String[] { Long.class.getName() };
732         Object[] finderArgs = new Object[] { new Long(codeId) };
733 
734         Object result = null;
735 
736         if (finderClassNameCacheEnabled) {
737             result = FinderCacheUtil.getResult(finderClassName,
738                     finderMethodName, finderParams, finderArgs, this);
739         }
740 
741         if (result == null) {
742             Session session = null;
743 
744             try {
745                 session = openSession();
746 
747                 StringBuilder query = new StringBuilder();
748 
749                 query.append("SELECT COUNT(*) ");
750                 query.append("FROM com.liferay.portal.model.Resource WHERE ");
751 
752                 query.append("codeId = ?");
753 
754                 query.append(" ");
755 
756                 Query q = session.createQuery(query.toString());
757 
758                 QueryPos qPos = QueryPos.getInstance(q);
759 
760                 qPos.add(codeId);
761 
762                 Long count = null;
763 
764                 Iterator<Long> itr = q.list().iterator();
765 
766                 if (itr.hasNext()) {
767                     count = itr.next();
768                 }
769 
770                 if (count == null) {
771                     count = new Long(0);
772                 }
773 
774                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
775                     finderClassName, finderMethodName, finderParams,
776                     finderArgs, count);
777 
778                 return count.intValue();
779             }
780             catch (Exception e) {
781                 throw processException(e);
782             }
783             finally {
784                 closeSession(session);
785             }
786         }
787         else {
788             return ((Long)result).intValue();
789         }
790     }
791 
792     public int countByC_P(long codeId, String primKey)
793         throws SystemException {
794         boolean finderClassNameCacheEnabled = ResourceModelImpl.CACHE_ENABLED;
795         String finderClassName = Resource.class.getName();
796         String finderMethodName = "countByC_P";
797         String[] finderParams = new String[] {
798                 Long.class.getName(), String.class.getName()
799             };
800         Object[] finderArgs = new Object[] { new Long(codeId), primKey };
801 
802         Object result = null;
803 
804         if (finderClassNameCacheEnabled) {
805             result = FinderCacheUtil.getResult(finderClassName,
806                     finderMethodName, finderParams, finderArgs, this);
807         }
808 
809         if (result == null) {
810             Session session = null;
811 
812             try {
813                 session = openSession();
814 
815                 StringBuilder query = new StringBuilder();
816 
817                 query.append("SELECT COUNT(*) ");
818                 query.append("FROM com.liferay.portal.model.Resource WHERE ");
819 
820                 query.append("codeId = ?");
821 
822                 query.append(" AND ");
823 
824                 if (primKey == null) {
825                     query.append("primKey IS NULL");
826                 }
827                 else {
828                     query.append("primKey = ?");
829                 }
830 
831                 query.append(" ");
832 
833                 Query q = session.createQuery(query.toString());
834 
835                 QueryPos qPos = QueryPos.getInstance(q);
836 
837                 qPos.add(codeId);
838 
839                 if (primKey != null) {
840                     qPos.add(primKey);
841                 }
842 
843                 Long count = null;
844 
845                 Iterator<Long> itr = q.list().iterator();
846 
847                 if (itr.hasNext()) {
848                     count = itr.next();
849                 }
850 
851                 if (count == null) {
852                     count = new Long(0);
853                 }
854 
855                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
856                     finderClassName, finderMethodName, finderParams,
857                     finderArgs, count);
858 
859                 return count.intValue();
860             }
861             catch (Exception e) {
862                 throw processException(e);
863             }
864             finally {
865                 closeSession(session);
866             }
867         }
868         else {
869             return ((Long)result).intValue();
870         }
871     }
872 
873     public int countAll() throws SystemException {
874         boolean finderClassNameCacheEnabled = ResourceModelImpl.CACHE_ENABLED;
875         String finderClassName = Resource.class.getName();
876         String finderMethodName = "countAll";
877         String[] finderParams = new String[] {  };
878         Object[] finderArgs = new Object[] {  };
879 
880         Object result = null;
881 
882         if (finderClassNameCacheEnabled) {
883             result = FinderCacheUtil.getResult(finderClassName,
884                     finderMethodName, finderParams, finderArgs, this);
885         }
886 
887         if (result == null) {
888             Session session = null;
889 
890             try {
891                 session = openSession();
892 
893                 Query q = session.createQuery(
894                         "SELECT COUNT(*) FROM com.liferay.portal.model.Resource");
895 
896                 Long count = null;
897 
898                 Iterator<Long> itr = q.list().iterator();
899 
900                 if (itr.hasNext()) {
901                     count = itr.next();
902                 }
903 
904                 if (count == null) {
905                     count = new Long(0);
906                 }
907 
908                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
909                     finderClassName, finderMethodName, finderParams,
910                     finderArgs, count);
911 
912                 return count.intValue();
913             }
914             catch (Exception e) {
915                 throw processException(e);
916             }
917             finally {
918                 closeSession(session);
919             }
920         }
921         else {
922             return ((Long)result).intValue();
923         }
924     }
925 
926     public void registerListener(ModelListener listener) {
927         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
928 
929         listeners.add(listener);
930 
931         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
932     }
933 
934     public void unregisterListener(ModelListener listener) {
935         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
936 
937         listeners.remove(listener);
938 
939         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
940     }
941 
942     public void afterPropertiesSet() {
943         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
944                     com.liferay.portal.util.PropsUtil.get(
945                         "value.object.listener.com.liferay.portal.model.Resource")));
946 
947         if (listenerClassNames.length > 0) {
948             try {
949                 List<ModelListener> listeners = new ArrayList<ModelListener>();
950 
951                 for (String listenerClassName : listenerClassNames) {
952                     listeners.add((ModelListener)Class.forName(
953                             listenerClassName).newInstance());
954                 }
955 
956                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
957             }
958             catch (Exception e) {
959                 _log.error(e);
960             }
961         }
962     }
963 
964     private static Log _log = LogFactory.getLog(ResourcePersistenceImpl.class);
965     private ModelListener[] _listeners = new ModelListener[0];
966 }