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.NoSuchResourceCodeException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.bean.InitializingBean;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.Query;
31  import com.liferay.portal.kernel.dao.orm.QueryPos;
32  import com.liferay.portal.kernel.dao.orm.QueryUtil;
33  import com.liferay.portal.kernel.dao.orm.Session;
34  import com.liferay.portal.kernel.util.GetterUtil;
35  import com.liferay.portal.kernel.util.ListUtil;
36  import com.liferay.portal.kernel.util.OrderByComparator;
37  import com.liferay.portal.kernel.util.StringPool;
38  import com.liferay.portal.kernel.util.StringUtil;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.model.ResourceCode;
41  import com.liferay.portal.model.impl.ResourceCodeImpl;
42  import com.liferay.portal.model.impl.ResourceCodeModelImpl;
43  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  import java.util.ArrayList;
49  import java.util.Collections;
50  import java.util.Iterator;
51  import java.util.List;
52  
53  /**
54   * <a href="ResourceCodePersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class ResourceCodePersistenceImpl extends BasePersistenceImpl
60      implements ResourceCodePersistence, InitializingBean {
61      public ResourceCode create(long codeId) {
62          ResourceCode resourceCode = new ResourceCodeImpl();
63  
64          resourceCode.setNew(true);
65          resourceCode.setPrimaryKey(codeId);
66  
67          return resourceCode;
68      }
69  
70      public ResourceCode remove(long codeId)
71          throws NoSuchResourceCodeException, SystemException {
72          Session session = null;
73  
74          try {
75              session = openSession();
76  
77              ResourceCode resourceCode = (ResourceCode)session.get(ResourceCodeImpl.class,
78                      new Long(codeId));
79  
80              if (resourceCode == null) {
81                  if (_log.isWarnEnabled()) {
82                      _log.warn("No ResourceCode exists with the primary key " +
83                          codeId);
84                  }
85  
86                  throw new NoSuchResourceCodeException(
87                      "No ResourceCode exists with the primary key " + codeId);
88              }
89  
90              return remove(resourceCode);
91          }
92          catch (NoSuchResourceCodeException nsee) {
93              throw nsee;
94          }
95          catch (Exception e) {
96              throw processException(e);
97          }
98          finally {
99              closeSession(session);
100         }
101     }
102 
103     public ResourceCode remove(ResourceCode resourceCode)
104         throws SystemException {
105         if (_listeners.length > 0) {
106             for (ModelListener listener : _listeners) {
107                 listener.onBeforeRemove(resourceCode);
108             }
109         }
110 
111         resourceCode = removeImpl(resourceCode);
112 
113         if (_listeners.length > 0) {
114             for (ModelListener listener : _listeners) {
115                 listener.onAfterRemove(resourceCode);
116             }
117         }
118 
119         return resourceCode;
120     }
121 
122     protected ResourceCode removeImpl(ResourceCode resourceCode)
123         throws SystemException {
124         Session session = null;
125 
126         try {
127             session = openSession();
128 
129             session.delete(resourceCode);
130 
131             session.flush();
132 
133             return resourceCode;
134         }
135         catch (Exception e) {
136             throw processException(e);
137         }
138         finally {
139             closeSession(session);
140 
141             FinderCacheUtil.clearCache(ResourceCode.class.getName());
142         }
143     }
144 
145     /**
146      * @deprecated Use <code>update(ResourceCode resourceCode, boolean merge)</code>.
147      */
148     public ResourceCode update(ResourceCode resourceCode)
149         throws SystemException {
150         if (_log.isWarnEnabled()) {
151             _log.warn(
152                 "Using the deprecated update(ResourceCode resourceCode) method. Use update(ResourceCode resourceCode, boolean merge) instead.");
153         }
154 
155         return update(resourceCode, false);
156     }
157 
158     /**
159      * Add, update, or merge, the entity. This method also calls the model
160      * listeners to trigger the proper events associated with adding, deleting,
161      * or updating an entity.
162      *
163      * @param        resourceCode the entity to add, update, or merge
164      * @param        merge boolean value for whether to merge the entity. The
165      *                default value is false. Setting merge to true is more
166      *                expensive and should only be true when resourceCode is
167      *                transient. See LEP-5473 for a detailed discussion of this
168      *                method.
169      * @return        true if the portlet can be displayed via Ajax
170      */
171     public ResourceCode update(ResourceCode resourceCode, boolean merge)
172         throws SystemException {
173         boolean isNew = resourceCode.isNew();
174 
175         if (_listeners.length > 0) {
176             for (ModelListener listener : _listeners) {
177                 if (isNew) {
178                     listener.onBeforeCreate(resourceCode);
179                 }
180                 else {
181                     listener.onBeforeUpdate(resourceCode);
182                 }
183             }
184         }
185 
186         resourceCode = updateImpl(resourceCode, merge);
187 
188         if (_listeners.length > 0) {
189             for (ModelListener listener : _listeners) {
190                 if (isNew) {
191                     listener.onAfterCreate(resourceCode);
192                 }
193                 else {
194                     listener.onAfterUpdate(resourceCode);
195                 }
196             }
197         }
198 
199         return resourceCode;
200     }
201 
202     public ResourceCode updateImpl(
203         com.liferay.portal.model.ResourceCode resourceCode, boolean merge)
204         throws SystemException {
205         Session session = null;
206 
207         try {
208             session = openSession();
209 
210             if (merge) {
211                 session.merge(resourceCode);
212             }
213             else {
214                 if (resourceCode.isNew()) {
215                     session.save(resourceCode);
216                 }
217             }
218 
219             session.flush();
220 
221             resourceCode.setNew(false);
222 
223             return resourceCode;
224         }
225         catch (Exception e) {
226             throw processException(e);
227         }
228         finally {
229             closeSession(session);
230 
231             FinderCacheUtil.clearCache(ResourceCode.class.getName());
232         }
233     }
234 
235     public ResourceCode findByPrimaryKey(long codeId)
236         throws NoSuchResourceCodeException, SystemException {
237         ResourceCode resourceCode = fetchByPrimaryKey(codeId);
238 
239         if (resourceCode == null) {
240             if (_log.isWarnEnabled()) {
241                 _log.warn("No ResourceCode exists with the primary key " +
242                     codeId);
243             }
244 
245             throw new NoSuchResourceCodeException(
246                 "No ResourceCode exists with the primary key " + codeId);
247         }
248 
249         return resourceCode;
250     }
251 
252     public ResourceCode fetchByPrimaryKey(long codeId)
253         throws SystemException {
254         Session session = null;
255 
256         try {
257             session = openSession();
258 
259             return (ResourceCode)session.get(ResourceCodeImpl.class,
260                 new Long(codeId));
261         }
262         catch (Exception e) {
263             throw processException(e);
264         }
265         finally {
266             closeSession(session);
267         }
268     }
269 
270     public List<ResourceCode> findByCompanyId(long companyId)
271         throws SystemException {
272         boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
273         String finderClassName = ResourceCode.class.getName();
274         String finderMethodName = "findByCompanyId";
275         String[] finderParams = new String[] { Long.class.getName() };
276         Object[] finderArgs = new Object[] { new Long(companyId) };
277 
278         Object result = null;
279 
280         if (finderClassNameCacheEnabled) {
281             result = FinderCacheUtil.getResult(finderClassName,
282                     finderMethodName, finderParams, finderArgs, this);
283         }
284 
285         if (result == null) {
286             Session session = null;
287 
288             try {
289                 session = openSession();
290 
291                 StringBuilder query = new StringBuilder();
292 
293                 query.append(
294                     "FROM com.liferay.portal.model.ResourceCode WHERE ");
295 
296                 query.append("companyId = ?");
297 
298                 query.append(" ");
299 
300                 Query q = session.createQuery(query.toString());
301 
302                 QueryPos qPos = QueryPos.getInstance(q);
303 
304                 qPos.add(companyId);
305 
306                 List<ResourceCode> list = q.list();
307 
308                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
309                     finderClassName, finderMethodName, finderParams,
310                     finderArgs, list);
311 
312                 return list;
313             }
314             catch (Exception e) {
315                 throw processException(e);
316             }
317             finally {
318                 closeSession(session);
319             }
320         }
321         else {
322             return (List<ResourceCode>)result;
323         }
324     }
325 
326     public List<ResourceCode> findByCompanyId(long companyId, int start, int end)
327         throws SystemException {
328         return findByCompanyId(companyId, start, end, null);
329     }
330 
331     public List<ResourceCode> findByCompanyId(long companyId, int start,
332         int end, OrderByComparator obc) throws SystemException {
333         boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
334         String finderClassName = ResourceCode.class.getName();
335         String finderMethodName = "findByCompanyId";
336         String[] finderParams = new String[] {
337                 Long.class.getName(),
338                 
339                 "java.lang.Integer", "java.lang.Integer",
340                 "com.liferay.portal.kernel.util.OrderByComparator"
341             };
342         Object[] finderArgs = new Object[] {
343                 new Long(companyId),
344                 
345                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
346             };
347 
348         Object result = null;
349 
350         if (finderClassNameCacheEnabled) {
351             result = FinderCacheUtil.getResult(finderClassName,
352                     finderMethodName, finderParams, finderArgs, this);
353         }
354 
355         if (result == null) {
356             Session session = null;
357 
358             try {
359                 session = openSession();
360 
361                 StringBuilder query = new StringBuilder();
362 
363                 query.append(
364                     "FROM com.liferay.portal.model.ResourceCode WHERE ");
365 
366                 query.append("companyId = ?");
367 
368                 query.append(" ");
369 
370                 if (obc != null) {
371                     query.append("ORDER BY ");
372                     query.append(obc.getOrderBy());
373                 }
374 
375                 Query q = session.createQuery(query.toString());
376 
377                 QueryPos qPos = QueryPos.getInstance(q);
378 
379                 qPos.add(companyId);
380 
381                 List<ResourceCode> list = (List<ResourceCode>)QueryUtil.list(q,
382                         getDialect(), start, end);
383 
384                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
385                     finderClassName, finderMethodName, finderParams,
386                     finderArgs, list);
387 
388                 return list;
389             }
390             catch (Exception e) {
391                 throw processException(e);
392             }
393             finally {
394                 closeSession(session);
395             }
396         }
397         else {
398             return (List<ResourceCode>)result;
399         }
400     }
401 
402     public ResourceCode findByCompanyId_First(long companyId,
403         OrderByComparator obc)
404         throws NoSuchResourceCodeException, SystemException {
405         List<ResourceCode> list = findByCompanyId(companyId, 0, 1, obc);
406 
407         if (list.size() == 0) {
408             StringBuilder msg = new StringBuilder();
409 
410             msg.append("No ResourceCode exists with the key {");
411 
412             msg.append("companyId=" + companyId);
413 
414             msg.append(StringPool.CLOSE_CURLY_BRACE);
415 
416             throw new NoSuchResourceCodeException(msg.toString());
417         }
418         else {
419             return list.get(0);
420         }
421     }
422 
423     public ResourceCode findByCompanyId_Last(long companyId,
424         OrderByComparator obc)
425         throws NoSuchResourceCodeException, SystemException {
426         int count = countByCompanyId(companyId);
427 
428         List<ResourceCode> list = findByCompanyId(companyId, count - 1, count,
429                 obc);
430 
431         if (list.size() == 0) {
432             StringBuilder msg = new StringBuilder();
433 
434             msg.append("No ResourceCode exists with the key {");
435 
436             msg.append("companyId=" + companyId);
437 
438             msg.append(StringPool.CLOSE_CURLY_BRACE);
439 
440             throw new NoSuchResourceCodeException(msg.toString());
441         }
442         else {
443             return list.get(0);
444         }
445     }
446 
447     public ResourceCode[] findByCompanyId_PrevAndNext(long codeId,
448         long companyId, OrderByComparator obc)
449         throws NoSuchResourceCodeException, SystemException {
450         ResourceCode resourceCode = findByPrimaryKey(codeId);
451 
452         int count = countByCompanyId(companyId);
453 
454         Session session = null;
455 
456         try {
457             session = openSession();
458 
459             StringBuilder query = new StringBuilder();
460 
461             query.append("FROM com.liferay.portal.model.ResourceCode WHERE ");
462 
463             query.append("companyId = ?");
464 
465             query.append(" ");
466 
467             if (obc != null) {
468                 query.append("ORDER BY ");
469                 query.append(obc.getOrderBy());
470             }
471 
472             Query q = session.createQuery(query.toString());
473 
474             QueryPos qPos = QueryPos.getInstance(q);
475 
476             qPos.add(companyId);
477 
478             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
479                     resourceCode);
480 
481             ResourceCode[] array = new ResourceCodeImpl[3];
482 
483             array[0] = (ResourceCode)objArray[0];
484             array[1] = (ResourceCode)objArray[1];
485             array[2] = (ResourceCode)objArray[2];
486 
487             return array;
488         }
489         catch (Exception e) {
490             throw processException(e);
491         }
492         finally {
493             closeSession(session);
494         }
495     }
496 
497     public List<ResourceCode> findByName(String name) throws SystemException {
498         boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
499         String finderClassName = ResourceCode.class.getName();
500         String finderMethodName = "findByName";
501         String[] finderParams = new String[] { String.class.getName() };
502         Object[] finderArgs = new Object[] { name };
503 
504         Object result = null;
505 
506         if (finderClassNameCacheEnabled) {
507             result = FinderCacheUtil.getResult(finderClassName,
508                     finderMethodName, finderParams, finderArgs, this);
509         }
510 
511         if (result == null) {
512             Session session = null;
513 
514             try {
515                 session = openSession();
516 
517                 StringBuilder query = new StringBuilder();
518 
519                 query.append(
520                     "FROM com.liferay.portal.model.ResourceCode WHERE ");
521 
522                 if (name == null) {
523                     query.append("name IS NULL");
524                 }
525                 else {
526                     query.append("name = ?");
527                 }
528 
529                 query.append(" ");
530 
531                 Query q = session.createQuery(query.toString());
532 
533                 QueryPos qPos = QueryPos.getInstance(q);
534 
535                 if (name != null) {
536                     qPos.add(name);
537                 }
538 
539                 List<ResourceCode> list = q.list();
540 
541                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
542                     finderClassName, finderMethodName, finderParams,
543                     finderArgs, list);
544 
545                 return list;
546             }
547             catch (Exception e) {
548                 throw processException(e);
549             }
550             finally {
551                 closeSession(session);
552             }
553         }
554         else {
555             return (List<ResourceCode>)result;
556         }
557     }
558 
559     public List<ResourceCode> findByName(String name, int start, int end)
560         throws SystemException {
561         return findByName(name, start, end, null);
562     }
563 
564     public List<ResourceCode> findByName(String name, int start, int end,
565         OrderByComparator obc) throws SystemException {
566         boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
567         String finderClassName = ResourceCode.class.getName();
568         String finderMethodName = "findByName";
569         String[] finderParams = new String[] {
570                 String.class.getName(),
571                 
572                 "java.lang.Integer", "java.lang.Integer",
573                 "com.liferay.portal.kernel.util.OrderByComparator"
574             };
575         Object[] finderArgs = new Object[] {
576                 name,
577                 
578                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
579             };
580 
581         Object result = null;
582 
583         if (finderClassNameCacheEnabled) {
584             result = FinderCacheUtil.getResult(finderClassName,
585                     finderMethodName, finderParams, finderArgs, this);
586         }
587 
588         if (result == null) {
589             Session session = null;
590 
591             try {
592                 session = openSession();
593 
594                 StringBuilder query = new StringBuilder();
595 
596                 query.append(
597                     "FROM com.liferay.portal.model.ResourceCode WHERE ");
598 
599                 if (name == null) {
600                     query.append("name IS NULL");
601                 }
602                 else {
603                     query.append("name = ?");
604                 }
605 
606                 query.append(" ");
607 
608                 if (obc != null) {
609                     query.append("ORDER BY ");
610                     query.append(obc.getOrderBy());
611                 }
612 
613                 Query q = session.createQuery(query.toString());
614 
615                 QueryPos qPos = QueryPos.getInstance(q);
616 
617                 if (name != null) {
618                     qPos.add(name);
619                 }
620 
621                 List<ResourceCode> list = (List<ResourceCode>)QueryUtil.list(q,
622                         getDialect(), start, end);
623 
624                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
625                     finderClassName, finderMethodName, finderParams,
626                     finderArgs, list);
627 
628                 return list;
629             }
630             catch (Exception e) {
631                 throw processException(e);
632             }
633             finally {
634                 closeSession(session);
635             }
636         }
637         else {
638             return (List<ResourceCode>)result;
639         }
640     }
641 
642     public ResourceCode findByName_First(String name, OrderByComparator obc)
643         throws NoSuchResourceCodeException, SystemException {
644         List<ResourceCode> list = findByName(name, 0, 1, obc);
645 
646         if (list.size() == 0) {
647             StringBuilder msg = new StringBuilder();
648 
649             msg.append("No ResourceCode exists with the key {");
650 
651             msg.append("name=" + name);
652 
653             msg.append(StringPool.CLOSE_CURLY_BRACE);
654 
655             throw new NoSuchResourceCodeException(msg.toString());
656         }
657         else {
658             return list.get(0);
659         }
660     }
661 
662     public ResourceCode findByName_Last(String name, OrderByComparator obc)
663         throws NoSuchResourceCodeException, SystemException {
664         int count = countByName(name);
665 
666         List<ResourceCode> list = findByName(name, count - 1, count, obc);
667 
668         if (list.size() == 0) {
669             StringBuilder msg = new StringBuilder();
670 
671             msg.append("No ResourceCode exists with the key {");
672 
673             msg.append("name=" + name);
674 
675             msg.append(StringPool.CLOSE_CURLY_BRACE);
676 
677             throw new NoSuchResourceCodeException(msg.toString());
678         }
679         else {
680             return list.get(0);
681         }
682     }
683 
684     public ResourceCode[] findByName_PrevAndNext(long codeId, String name,
685         OrderByComparator obc)
686         throws NoSuchResourceCodeException, SystemException {
687         ResourceCode resourceCode = findByPrimaryKey(codeId);
688 
689         int count = countByName(name);
690 
691         Session session = null;
692 
693         try {
694             session = openSession();
695 
696             StringBuilder query = new StringBuilder();
697 
698             query.append("FROM com.liferay.portal.model.ResourceCode WHERE ");
699 
700             if (name == null) {
701                 query.append("name IS NULL");
702             }
703             else {
704                 query.append("name = ?");
705             }
706 
707             query.append(" ");
708 
709             if (obc != null) {
710                 query.append("ORDER BY ");
711                 query.append(obc.getOrderBy());
712             }
713 
714             Query q = session.createQuery(query.toString());
715 
716             QueryPos qPos = QueryPos.getInstance(q);
717 
718             if (name != null) {
719                 qPos.add(name);
720             }
721 
722             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
723                     resourceCode);
724 
725             ResourceCode[] array = new ResourceCodeImpl[3];
726 
727             array[0] = (ResourceCode)objArray[0];
728             array[1] = (ResourceCode)objArray[1];
729             array[2] = (ResourceCode)objArray[2];
730 
731             return array;
732         }
733         catch (Exception e) {
734             throw processException(e);
735         }
736         finally {
737             closeSession(session);
738         }
739     }
740 
741     public ResourceCode findByC_N_S(long companyId, String name, int scope)
742         throws NoSuchResourceCodeException, SystemException {
743         ResourceCode resourceCode = fetchByC_N_S(companyId, name, scope);
744 
745         if (resourceCode == null) {
746             StringBuilder msg = new StringBuilder();
747 
748             msg.append("No ResourceCode exists with the key {");
749 
750             msg.append("companyId=" + companyId);
751 
752             msg.append(", ");
753             msg.append("name=" + name);
754 
755             msg.append(", ");
756             msg.append("scope=" + scope);
757 
758             msg.append(StringPool.CLOSE_CURLY_BRACE);
759 
760             if (_log.isWarnEnabled()) {
761                 _log.warn(msg.toString());
762             }
763 
764             throw new NoSuchResourceCodeException(msg.toString());
765         }
766 
767         return resourceCode;
768     }
769 
770     public ResourceCode fetchByC_N_S(long companyId, String name, int scope)
771         throws SystemException {
772         boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
773         String finderClassName = ResourceCode.class.getName();
774         String finderMethodName = "fetchByC_N_S";
775         String[] finderParams = new String[] {
776                 Long.class.getName(), String.class.getName(),
777                 Integer.class.getName()
778             };
779         Object[] finderArgs = new Object[] {
780                 new Long(companyId),
781                 
782                 name, new Integer(scope)
783             };
784 
785         Object result = null;
786 
787         if (finderClassNameCacheEnabled) {
788             result = FinderCacheUtil.getResult(finderClassName,
789                     finderMethodName, finderParams, finderArgs, this);
790         }
791 
792         if (result == null) {
793             Session session = null;
794 
795             try {
796                 session = openSession();
797 
798                 StringBuilder query = new StringBuilder();
799 
800                 query.append(
801                     "FROM com.liferay.portal.model.ResourceCode WHERE ");
802 
803                 query.append("companyId = ?");
804 
805                 query.append(" AND ");
806 
807                 if (name == null) {
808                     query.append("name IS NULL");
809                 }
810                 else {
811                     query.append("name = ?");
812                 }
813 
814                 query.append(" AND ");
815 
816                 query.append("scope = ?");
817 
818                 query.append(" ");
819 
820                 Query q = session.createQuery(query.toString());
821 
822                 QueryPos qPos = QueryPos.getInstance(q);
823 
824                 qPos.add(companyId);
825 
826                 if (name != null) {
827                     qPos.add(name);
828                 }
829 
830                 qPos.add(scope);
831 
832                 List<ResourceCode> list = q.list();
833 
834                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
835                     finderClassName, finderMethodName, finderParams,
836                     finderArgs, list);
837 
838                 if (list.size() == 0) {
839                     return null;
840                 }
841                 else {
842                     return list.get(0);
843                 }
844             }
845             catch (Exception e) {
846                 throw processException(e);
847             }
848             finally {
849                 closeSession(session);
850             }
851         }
852         else {
853             List<ResourceCode> list = (List<ResourceCode>)result;
854 
855             if (list.size() == 0) {
856                 return null;
857             }
858             else {
859                 return list.get(0);
860             }
861         }
862     }
863 
864     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
865         throws SystemException {
866         Session session = null;
867 
868         try {
869             session = openSession();
870 
871             dynamicQuery.compile(session);
872 
873             return dynamicQuery.list();
874         }
875         catch (Exception e) {
876             throw processException(e);
877         }
878         finally {
879             closeSession(session);
880         }
881     }
882 
883     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
884         int start, int end) throws SystemException {
885         Session session = null;
886 
887         try {
888             session = openSession();
889 
890             dynamicQuery.setLimit(start, end);
891 
892             dynamicQuery.compile(session);
893 
894             return dynamicQuery.list();
895         }
896         catch (Exception e) {
897             throw processException(e);
898         }
899         finally {
900             closeSession(session);
901         }
902     }
903 
904     public List<ResourceCode> findAll() throws SystemException {
905         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
906     }
907 
908     public List<ResourceCode> findAll(int start, int end)
909         throws SystemException {
910         return findAll(start, end, null);
911     }
912 
913     public List<ResourceCode> findAll(int start, int end, OrderByComparator obc)
914         throws SystemException {
915         boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
916         String finderClassName = ResourceCode.class.getName();
917         String finderMethodName = "findAll";
918         String[] finderParams = new String[] {
919                 "java.lang.Integer", "java.lang.Integer",
920                 "com.liferay.portal.kernel.util.OrderByComparator"
921             };
922         Object[] finderArgs = new Object[] {
923                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
924             };
925 
926         Object result = null;
927 
928         if (finderClassNameCacheEnabled) {
929             result = FinderCacheUtil.getResult(finderClassName,
930                     finderMethodName, finderParams, finderArgs, this);
931         }
932 
933         if (result == null) {
934             Session session = null;
935 
936             try {
937                 session = openSession();
938 
939                 StringBuilder query = new StringBuilder();
940 
941                 query.append("FROM com.liferay.portal.model.ResourceCode ");
942 
943                 if (obc != null) {
944                     query.append("ORDER BY ");
945                     query.append(obc.getOrderBy());
946                 }
947 
948                 Query q = session.createQuery(query.toString());
949 
950                 List<ResourceCode> list = (List<ResourceCode>)QueryUtil.list(q,
951                         getDialect(), start, end);
952 
953                 if (obc == null) {
954                     Collections.sort(list);
955                 }
956 
957                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
958                     finderClassName, finderMethodName, finderParams,
959                     finderArgs, list);
960 
961                 return list;
962             }
963             catch (Exception e) {
964                 throw processException(e);
965             }
966             finally {
967                 closeSession(session);
968             }
969         }
970         else {
971             return (List<ResourceCode>)result;
972         }
973     }
974 
975     public void removeByCompanyId(long companyId) throws SystemException {
976         for (ResourceCode resourceCode : findByCompanyId(companyId)) {
977             remove(resourceCode);
978         }
979     }
980 
981     public void removeByName(String name) throws SystemException {
982         for (ResourceCode resourceCode : findByName(name)) {
983             remove(resourceCode);
984         }
985     }
986 
987     public void removeByC_N_S(long companyId, String name, int scope)
988         throws NoSuchResourceCodeException, SystemException {
989         ResourceCode resourceCode = findByC_N_S(companyId, name, scope);
990 
991         remove(resourceCode);
992     }
993 
994     public void removeAll() throws SystemException {
995         for (ResourceCode resourceCode : findAll()) {
996             remove(resourceCode);
997         }
998     }
999 
1000    public int countByCompanyId(long companyId) throws SystemException {
1001        boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
1002        String finderClassName = ResourceCode.class.getName();
1003        String finderMethodName = "countByCompanyId";
1004        String[] finderParams = new String[] { Long.class.getName() };
1005        Object[] finderArgs = new Object[] { new Long(companyId) };
1006
1007        Object result = null;
1008
1009        if (finderClassNameCacheEnabled) {
1010            result = FinderCacheUtil.getResult(finderClassName,
1011                    finderMethodName, finderParams, finderArgs, this);
1012        }
1013
1014        if (result == null) {
1015            Session session = null;
1016
1017            try {
1018                session = openSession();
1019
1020                StringBuilder query = new StringBuilder();
1021
1022                query.append("SELECT COUNT(*) ");
1023                query.append(
1024                    "FROM com.liferay.portal.model.ResourceCode WHERE ");
1025
1026                query.append("companyId = ?");
1027
1028                query.append(" ");
1029
1030                Query q = session.createQuery(query.toString());
1031
1032                QueryPos qPos = QueryPos.getInstance(q);
1033
1034                qPos.add(companyId);
1035
1036                Long count = null;
1037
1038                Iterator<Long> itr = q.list().iterator();
1039
1040                if (itr.hasNext()) {
1041                    count = itr.next();
1042                }
1043
1044                if (count == null) {
1045                    count = new Long(0);
1046                }
1047
1048                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1049                    finderClassName, finderMethodName, finderParams,
1050                    finderArgs, count);
1051
1052                return count.intValue();
1053            }
1054            catch (Exception e) {
1055                throw processException(e);
1056            }
1057            finally {
1058                closeSession(session);
1059            }
1060        }
1061        else {
1062            return ((Long)result).intValue();
1063        }
1064    }
1065
1066    public int countByName(String name) throws SystemException {
1067        boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
1068        String finderClassName = ResourceCode.class.getName();
1069        String finderMethodName = "countByName";
1070        String[] finderParams = new String[] { String.class.getName() };
1071        Object[] finderArgs = new Object[] { name };
1072
1073        Object result = null;
1074
1075        if (finderClassNameCacheEnabled) {
1076            result = FinderCacheUtil.getResult(finderClassName,
1077                    finderMethodName, finderParams, finderArgs, this);
1078        }
1079
1080        if (result == null) {
1081            Session session = null;
1082
1083            try {
1084                session = openSession();
1085
1086                StringBuilder query = new StringBuilder();
1087
1088                query.append("SELECT COUNT(*) ");
1089                query.append(
1090                    "FROM com.liferay.portal.model.ResourceCode WHERE ");
1091
1092                if (name == null) {
1093                    query.append("name IS NULL");
1094                }
1095                else {
1096                    query.append("name = ?");
1097                }
1098
1099                query.append(" ");
1100
1101                Query q = session.createQuery(query.toString());
1102
1103                QueryPos qPos = QueryPos.getInstance(q);
1104
1105                if (name != null) {
1106                    qPos.add(name);
1107                }
1108
1109                Long count = null;
1110
1111                Iterator<Long> itr = q.list().iterator();
1112
1113                if (itr.hasNext()) {
1114                    count = itr.next();
1115                }
1116
1117                if (count == null) {
1118                    count = new Long(0);
1119                }
1120
1121                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1122                    finderClassName, finderMethodName, finderParams,
1123                    finderArgs, count);
1124
1125                return count.intValue();
1126            }
1127            catch (Exception e) {
1128                throw processException(e);
1129            }
1130            finally {
1131                closeSession(session);
1132            }
1133        }
1134        else {
1135            return ((Long)result).intValue();
1136        }
1137    }
1138
1139    public int countByC_N_S(long companyId, String name, int scope)
1140        throws SystemException {
1141        boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
1142        String finderClassName = ResourceCode.class.getName();
1143        String finderMethodName = "countByC_N_S";
1144        String[] finderParams = new String[] {
1145                Long.class.getName(), String.class.getName(),
1146                Integer.class.getName()
1147            };
1148        Object[] finderArgs = new Object[] {
1149                new Long(companyId),
1150                
1151                name, new Integer(scope)
1152            };
1153
1154        Object result = null;
1155
1156        if (finderClassNameCacheEnabled) {
1157            result = FinderCacheUtil.getResult(finderClassName,
1158                    finderMethodName, finderParams, finderArgs, this);
1159        }
1160
1161        if (result == null) {
1162            Session session = null;
1163
1164            try {
1165                session = openSession();
1166
1167                StringBuilder query = new StringBuilder();
1168
1169                query.append("SELECT COUNT(*) ");
1170                query.append(
1171                    "FROM com.liferay.portal.model.ResourceCode WHERE ");
1172
1173                query.append("companyId = ?");
1174
1175                query.append(" AND ");
1176
1177                if (name == null) {
1178                    query.append("name IS NULL");
1179                }
1180                else {
1181                    query.append("name = ?");
1182                }
1183
1184                query.append(" AND ");
1185
1186                query.append("scope = ?");
1187
1188                query.append(" ");
1189
1190                Query q = session.createQuery(query.toString());
1191
1192                QueryPos qPos = QueryPos.getInstance(q);
1193
1194                qPos.add(companyId);
1195
1196                if (name != null) {
1197                    qPos.add(name);
1198                }
1199
1200                qPos.add(scope);
1201
1202                Long count = null;
1203
1204                Iterator<Long> itr = q.list().iterator();
1205
1206                if (itr.hasNext()) {
1207                    count = itr.next();
1208                }
1209
1210                if (count == null) {
1211                    count = new Long(0);
1212                }
1213
1214                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1215                    finderClassName, finderMethodName, finderParams,
1216                    finderArgs, count);
1217
1218                return count.intValue();
1219            }
1220            catch (Exception e) {
1221                throw processException(e);
1222            }
1223            finally {
1224                closeSession(session);
1225            }
1226        }
1227        else {
1228            return ((Long)result).intValue();
1229        }
1230    }
1231
1232    public int countAll() throws SystemException {
1233        boolean finderClassNameCacheEnabled = ResourceCodeModelImpl.CACHE_ENABLED;
1234        String finderClassName = ResourceCode.class.getName();
1235        String finderMethodName = "countAll";
1236        String[] finderParams = new String[] {  };
1237        Object[] finderArgs = new Object[] {  };
1238
1239        Object result = null;
1240
1241        if (finderClassNameCacheEnabled) {
1242            result = FinderCacheUtil.getResult(finderClassName,
1243                    finderMethodName, finderParams, finderArgs, this);
1244        }
1245
1246        if (result == null) {
1247            Session session = null;
1248
1249            try {
1250                session = openSession();
1251
1252                Query q = session.createQuery(
1253                        "SELECT COUNT(*) FROM com.liferay.portal.model.ResourceCode");
1254
1255                Long count = null;
1256
1257                Iterator<Long> itr = q.list().iterator();
1258
1259                if (itr.hasNext()) {
1260                    count = itr.next();
1261                }
1262
1263                if (count == null) {
1264                    count = new Long(0);
1265                }
1266
1267                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1268                    finderClassName, finderMethodName, finderParams,
1269                    finderArgs, count);
1270
1271                return count.intValue();
1272            }
1273            catch (Exception e) {
1274                throw processException(e);
1275            }
1276            finally {
1277                closeSession(session);
1278            }
1279        }
1280        else {
1281            return ((Long)result).intValue();
1282        }
1283    }
1284
1285    public void registerListener(ModelListener listener) {
1286        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1287
1288        listeners.add(listener);
1289
1290        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1291    }
1292
1293    public void unregisterListener(ModelListener listener) {
1294        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1295
1296        listeners.remove(listener);
1297
1298        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1299    }
1300
1301    public void afterPropertiesSet() {
1302        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1303                    com.liferay.portal.util.PropsUtil.get(
1304                        "value.object.listener.com.liferay.portal.model.ResourceCode")));
1305
1306        if (listenerClassNames.length > 0) {
1307            try {
1308                List<ModelListener> listeners = new ArrayList<ModelListener>();
1309
1310                for (String listenerClassName : listenerClassNames) {
1311                    listeners.add((ModelListener)Class.forName(
1312                            listenerClassName).newInstance());
1313                }
1314
1315                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1316            }
1317            catch (Exception e) {
1318                _log.error(e);
1319            }
1320        }
1321    }
1322
1323    private static Log _log = LogFactory.getLog(ResourceCodePersistenceImpl.class);
1324    private ModelListener[] _listeners = new ModelListener[0];
1325}