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