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