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.NoSuchPermissionException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
28  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
29  import com.liferay.portal.kernel.dao.jdbc.RowMapper;
30  import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
31  import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
32  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
33  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
34  import com.liferay.portal.kernel.dao.orm.Query;
35  import com.liferay.portal.kernel.dao.orm.QueryPos;
36  import com.liferay.portal.kernel.dao.orm.QueryUtil;
37  import com.liferay.portal.kernel.dao.orm.SQLQuery;
38  import com.liferay.portal.kernel.dao.orm.Session;
39  import com.liferay.portal.kernel.dao.orm.Type;
40  import com.liferay.portal.kernel.util.GetterUtil;
41  import com.liferay.portal.kernel.util.ListUtil;
42  import com.liferay.portal.kernel.util.OrderByComparator;
43  import com.liferay.portal.kernel.util.StringPool;
44  import com.liferay.portal.kernel.util.StringUtil;
45  import com.liferay.portal.model.ModelListener;
46  import com.liferay.portal.model.Permission;
47  import com.liferay.portal.model.impl.PermissionImpl;
48  import com.liferay.portal.model.impl.PermissionModelImpl;
49  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
50  
51  import org.apache.commons.logging.Log;
52  import org.apache.commons.logging.LogFactory;
53  
54  import java.sql.Types;
55  
56  import java.util.ArrayList;
57  import java.util.Collections;
58  import java.util.Iterator;
59  import java.util.List;
60  
61  /**
62   * <a href="PermissionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   *
66   */
67  public class PermissionPersistenceImpl extends BasePersistenceImpl
68      implements PermissionPersistence {
69      public Permission create(long permissionId) {
70          Permission permission = new PermissionImpl();
71  
72          permission.setNew(true);
73          permission.setPrimaryKey(permissionId);
74  
75          return permission;
76      }
77  
78      public Permission remove(long permissionId)
79          throws NoSuchPermissionException, SystemException {
80          Session session = null;
81  
82          try {
83              session = openSession();
84  
85              Permission permission = (Permission)session.get(PermissionImpl.class,
86                      new Long(permissionId));
87  
88              if (permission == null) {
89                  if (_log.isWarnEnabled()) {
90                      _log.warn("No Permission exists with the primary key " +
91                          permissionId);
92                  }
93  
94                  throw new NoSuchPermissionException(
95                      "No Permission exists with the primary key " +
96                      permissionId);
97              }
98  
99              return remove(permission);
100         }
101         catch (NoSuchPermissionException nsee) {
102             throw nsee;
103         }
104         catch (Exception e) {
105             throw processException(e);
106         }
107         finally {
108             closeSession(session);
109         }
110     }
111 
112     public Permission remove(Permission permission) throws SystemException {
113         if (_listeners.length > 0) {
114             for (ModelListener listener : _listeners) {
115                 listener.onBeforeRemove(permission);
116             }
117         }
118 
119         permission = removeImpl(permission);
120 
121         if (_listeners.length > 0) {
122             for (ModelListener listener : _listeners) {
123                 listener.onAfterRemove(permission);
124             }
125         }
126 
127         return permission;
128     }
129 
130     protected Permission removeImpl(Permission permission)
131         throws SystemException {
132         try {
133             clearGroups.clear(permission.getPrimaryKey());
134         }
135         catch (Exception e) {
136             throw processException(e);
137         }
138         finally {
139             FinderCacheUtil.clearCache("Groups_Permissions");
140         }
141 
142         try {
143             clearRoles.clear(permission.getPrimaryKey());
144         }
145         catch (Exception e) {
146             throw processException(e);
147         }
148         finally {
149             FinderCacheUtil.clearCache("Roles_Permissions");
150         }
151 
152         try {
153             clearUsers.clear(permission.getPrimaryKey());
154         }
155         catch (Exception e) {
156             throw processException(e);
157         }
158         finally {
159             FinderCacheUtil.clearCache("Users_Permissions");
160         }
161 
162         Session session = null;
163 
164         try {
165             session = openSession();
166 
167             session.delete(permission);
168 
169             session.flush();
170 
171             return permission;
172         }
173         catch (Exception e) {
174             throw processException(e);
175         }
176         finally {
177             closeSession(session);
178 
179             FinderCacheUtil.clearCache(Permission.class.getName());
180         }
181     }
182 
183     /**
184      * @deprecated Use <code>update(Permission permission, boolean merge)</code>.
185      */
186     public Permission update(Permission permission) throws SystemException {
187         if (_log.isWarnEnabled()) {
188             _log.warn(
189                 "Using the deprecated update(Permission permission) method. Use update(Permission permission, boolean merge) instead.");
190         }
191 
192         return update(permission, false);
193     }
194 
195     /**
196      * Add, update, or merge, the entity. This method also calls the model
197      * listeners to trigger the proper events associated with adding, deleting,
198      * or updating an entity.
199      *
200      * @param        permission the entity to add, update, or merge
201      * @param        merge boolean value for whether to merge the entity. The
202      *                default value is false. Setting merge to true is more
203      *                expensive and should only be true when permission is
204      *                transient. See LEP-5473 for a detailed discussion of this
205      *                method.
206      * @return        true if the portlet can be displayed via Ajax
207      */
208     public Permission update(Permission permission, boolean merge)
209         throws SystemException {
210         boolean isNew = permission.isNew();
211 
212         if (_listeners.length > 0) {
213             for (ModelListener listener : _listeners) {
214                 if (isNew) {
215                     listener.onBeforeCreate(permission);
216                 }
217                 else {
218                     listener.onBeforeUpdate(permission);
219                 }
220             }
221         }
222 
223         permission = updateImpl(permission, merge);
224 
225         if (_listeners.length > 0) {
226             for (ModelListener listener : _listeners) {
227                 if (isNew) {
228                     listener.onAfterCreate(permission);
229                 }
230                 else {
231                     listener.onAfterUpdate(permission);
232                 }
233             }
234         }
235 
236         return permission;
237     }
238 
239     public Permission updateImpl(
240         com.liferay.portal.model.Permission permission, boolean merge)
241         throws SystemException {
242         FinderCacheUtil.clearCache("Groups_Permissions");
243         FinderCacheUtil.clearCache("Roles_Permissions");
244         FinderCacheUtil.clearCache("Users_Permissions");
245 
246         Session session = null;
247 
248         try {
249             session = openSession();
250 
251             if (merge) {
252                 session.merge(permission);
253             }
254             else {
255                 if (permission.isNew()) {
256                     session.save(permission);
257                 }
258             }
259 
260             session.flush();
261 
262             permission.setNew(false);
263 
264             return permission;
265         }
266         catch (Exception e) {
267             throw processException(e);
268         }
269         finally {
270             closeSession(session);
271 
272             FinderCacheUtil.clearCache(Permission.class.getName());
273         }
274     }
275 
276     public Permission findByPrimaryKey(long permissionId)
277         throws NoSuchPermissionException, SystemException {
278         Permission permission = fetchByPrimaryKey(permissionId);
279 
280         if (permission == null) {
281             if (_log.isWarnEnabled()) {
282                 _log.warn("No Permission exists with the primary key " +
283                     permissionId);
284             }
285 
286             throw new NoSuchPermissionException(
287                 "No Permission exists with the primary key " + permissionId);
288         }
289 
290         return permission;
291     }
292 
293     public Permission fetchByPrimaryKey(long permissionId)
294         throws SystemException {
295         Session session = null;
296 
297         try {
298             session = openSession();
299 
300             return (Permission)session.get(PermissionImpl.class,
301                 new Long(permissionId));
302         }
303         catch (Exception e) {
304             throw processException(e);
305         }
306         finally {
307             closeSession(session);
308         }
309     }
310 
311     public List<Permission> findByResourceId(long resourceId)
312         throws SystemException {
313         boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED;
314         String finderClassName = Permission.class.getName();
315         String finderMethodName = "findByResourceId";
316         String[] finderParams = new String[] { Long.class.getName() };
317         Object[] finderArgs = new Object[] { new Long(resourceId) };
318 
319         Object result = null;
320 
321         if (finderClassNameCacheEnabled) {
322             result = FinderCacheUtil.getResult(finderClassName,
323                     finderMethodName, finderParams, finderArgs, this);
324         }
325 
326         if (result == null) {
327             Session session = null;
328 
329             try {
330                 session = openSession();
331 
332                 StringBuilder query = new StringBuilder();
333 
334                 query.append("FROM com.liferay.portal.model.Permission WHERE ");
335 
336                 query.append("resourceId = ?");
337 
338                 query.append(" ");
339 
340                 Query q = session.createQuery(query.toString());
341 
342                 QueryPos qPos = QueryPos.getInstance(q);
343 
344                 qPos.add(resourceId);
345 
346                 List<Permission> list = q.list();
347 
348                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
349                     finderClassName, finderMethodName, finderParams,
350                     finderArgs, list);
351 
352                 return list;
353             }
354             catch (Exception e) {
355                 throw processException(e);
356             }
357             finally {
358                 closeSession(session);
359             }
360         }
361         else {
362             return (List<Permission>)result;
363         }
364     }
365 
366     public List<Permission> findByResourceId(long resourceId, int start, int end)
367         throws SystemException {
368         return findByResourceId(resourceId, start, end, null);
369     }
370 
371     public List<Permission> findByResourceId(long resourceId, int start,
372         int end, OrderByComparator obc) throws SystemException {
373         boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED;
374         String finderClassName = Permission.class.getName();
375         String finderMethodName = "findByResourceId";
376         String[] finderParams = new String[] {
377                 Long.class.getName(),
378                 
379                 "java.lang.Integer", "java.lang.Integer",
380                 "com.liferay.portal.kernel.util.OrderByComparator"
381             };
382         Object[] finderArgs = new Object[] {
383                 new Long(resourceId),
384                 
385                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
386             };
387 
388         Object result = null;
389 
390         if (finderClassNameCacheEnabled) {
391             result = FinderCacheUtil.getResult(finderClassName,
392                     finderMethodName, finderParams, finderArgs, this);
393         }
394 
395         if (result == null) {
396             Session session = null;
397 
398             try {
399                 session = openSession();
400 
401                 StringBuilder query = new StringBuilder();
402 
403                 query.append("FROM com.liferay.portal.model.Permission WHERE ");
404 
405                 query.append("resourceId = ?");
406 
407                 query.append(" ");
408 
409                 if (obc != null) {
410                     query.append("ORDER BY ");
411                     query.append(obc.getOrderBy());
412                 }
413 
414                 Query q = session.createQuery(query.toString());
415 
416                 QueryPos qPos = QueryPos.getInstance(q);
417 
418                 qPos.add(resourceId);
419 
420                 List<Permission> list = (List<Permission>)QueryUtil.list(q,
421                         getDialect(), start, end);
422 
423                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
424                     finderClassName, finderMethodName, finderParams,
425                     finderArgs, list);
426 
427                 return list;
428             }
429             catch (Exception e) {
430                 throw processException(e);
431             }
432             finally {
433                 closeSession(session);
434             }
435         }
436         else {
437             return (List<Permission>)result;
438         }
439     }
440 
441     public Permission findByResourceId_First(long resourceId,
442         OrderByComparator obc)
443         throws NoSuchPermissionException, SystemException {
444         List<Permission> list = findByResourceId(resourceId, 0, 1, obc);
445 
446         if (list.size() == 0) {
447             StringBuilder msg = new StringBuilder();
448 
449             msg.append("No Permission exists with the key {");
450 
451             msg.append("resourceId=" + resourceId);
452 
453             msg.append(StringPool.CLOSE_CURLY_BRACE);
454 
455             throw new NoSuchPermissionException(msg.toString());
456         }
457         else {
458             return list.get(0);
459         }
460     }
461 
462     public Permission findByResourceId_Last(long resourceId,
463         OrderByComparator obc)
464         throws NoSuchPermissionException, SystemException {
465         int count = countByResourceId(resourceId);
466 
467         List<Permission> list = findByResourceId(resourceId, count - 1, count,
468                 obc);
469 
470         if (list.size() == 0) {
471             StringBuilder msg = new StringBuilder();
472 
473             msg.append("No Permission exists with the key {");
474 
475             msg.append("resourceId=" + resourceId);
476 
477             msg.append(StringPool.CLOSE_CURLY_BRACE);
478 
479             throw new NoSuchPermissionException(msg.toString());
480         }
481         else {
482             return list.get(0);
483         }
484     }
485 
486     public Permission[] findByResourceId_PrevAndNext(long permissionId,
487         long resourceId, OrderByComparator obc)
488         throws NoSuchPermissionException, SystemException {
489         Permission permission = findByPrimaryKey(permissionId);
490 
491         int count = countByResourceId(resourceId);
492 
493         Session session = null;
494 
495         try {
496             session = openSession();
497 
498             StringBuilder query = new StringBuilder();
499 
500             query.append("FROM com.liferay.portal.model.Permission WHERE ");
501 
502             query.append("resourceId = ?");
503 
504             query.append(" ");
505 
506             if (obc != null) {
507                 query.append("ORDER BY ");
508                 query.append(obc.getOrderBy());
509             }
510 
511             Query q = session.createQuery(query.toString());
512 
513             QueryPos qPos = QueryPos.getInstance(q);
514 
515             qPos.add(resourceId);
516 
517             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
518                     permission);
519 
520             Permission[] array = new PermissionImpl[3];
521 
522             array[0] = (Permission)objArray[0];
523             array[1] = (Permission)objArray[1];
524             array[2] = (Permission)objArray[2];
525 
526             return array;
527         }
528         catch (Exception e) {
529             throw processException(e);
530         }
531         finally {
532             closeSession(session);
533         }
534     }
535 
536     public Permission findByA_R(String actionId, long resourceId)
537         throws NoSuchPermissionException, SystemException {
538         Permission permission = fetchByA_R(actionId, resourceId);
539 
540         if (permission == null) {
541             StringBuilder msg = new StringBuilder();
542 
543             msg.append("No Permission exists with the key {");
544 
545             msg.append("actionId=" + actionId);
546 
547             msg.append(", ");
548             msg.append("resourceId=" + resourceId);
549 
550             msg.append(StringPool.CLOSE_CURLY_BRACE);
551 
552             if (_log.isWarnEnabled()) {
553                 _log.warn(msg.toString());
554             }
555 
556             throw new NoSuchPermissionException(msg.toString());
557         }
558 
559         return permission;
560     }
561 
562     public Permission fetchByA_R(String actionId, long resourceId)
563         throws SystemException {
564         boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED;
565         String finderClassName = Permission.class.getName();
566         String finderMethodName = "fetchByA_R";
567         String[] finderParams = new String[] {
568                 String.class.getName(), Long.class.getName()
569             };
570         Object[] finderArgs = new Object[] { actionId, new Long(resourceId) };
571 
572         Object result = null;
573 
574         if (finderClassNameCacheEnabled) {
575             result = FinderCacheUtil.getResult(finderClassName,
576                     finderMethodName, finderParams, finderArgs, this);
577         }
578 
579         if (result == null) {
580             Session session = null;
581 
582             try {
583                 session = openSession();
584 
585                 StringBuilder query = new StringBuilder();
586 
587                 query.append("FROM com.liferay.portal.model.Permission WHERE ");
588 
589                 if (actionId == null) {
590                     query.append("actionId IS NULL");
591                 }
592                 else {
593                     query.append("actionId = ?");
594                 }
595 
596                 query.append(" AND ");
597 
598                 query.append("resourceId = ?");
599 
600                 query.append(" ");
601 
602                 Query q = session.createQuery(query.toString());
603 
604                 QueryPos qPos = QueryPos.getInstance(q);
605 
606                 if (actionId != null) {
607                     qPos.add(actionId);
608                 }
609 
610                 qPos.add(resourceId);
611 
612                 List<Permission> list = q.list();
613 
614                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
615                     finderClassName, finderMethodName, finderParams,
616                     finderArgs, list);
617 
618                 if (list.size() == 0) {
619                     return null;
620                 }
621                 else {
622                     return list.get(0);
623                 }
624             }
625             catch (Exception e) {
626                 throw processException(e);
627             }
628             finally {
629                 closeSession(session);
630             }
631         }
632         else {
633             List<Permission> list = (List<Permission>)result;
634 
635             if (list.size() == 0) {
636                 return null;
637             }
638             else {
639                 return list.get(0);
640             }
641         }
642     }
643 
644     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
645         throws SystemException {
646         Session session = null;
647 
648         try {
649             session = openSession();
650 
651             dynamicQuery.compile(session);
652 
653             return dynamicQuery.list();
654         }
655         catch (Exception e) {
656             throw processException(e);
657         }
658         finally {
659             closeSession(session);
660         }
661     }
662 
663     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
664         int start, int end) throws SystemException {
665         Session session = null;
666 
667         try {
668             session = openSession();
669 
670             dynamicQuery.setLimit(start, end);
671 
672             dynamicQuery.compile(session);
673 
674             return dynamicQuery.list();
675         }
676         catch (Exception e) {
677             throw processException(e);
678         }
679         finally {
680             closeSession(session);
681         }
682     }
683 
684     public List<Permission> findAll() throws SystemException {
685         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
686     }
687 
688     public List<Permission> findAll(int start, int end)
689         throws SystemException {
690         return findAll(start, end, null);
691     }
692 
693     public List<Permission> findAll(int start, int end, OrderByComparator obc)
694         throws SystemException {
695         boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED;
696         String finderClassName = Permission.class.getName();
697         String finderMethodName = "findAll";
698         String[] finderParams = new String[] {
699                 "java.lang.Integer", "java.lang.Integer",
700                 "com.liferay.portal.kernel.util.OrderByComparator"
701             };
702         Object[] finderArgs = new Object[] {
703                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
704             };
705 
706         Object result = null;
707 
708         if (finderClassNameCacheEnabled) {
709             result = FinderCacheUtil.getResult(finderClassName,
710                     finderMethodName, finderParams, finderArgs, this);
711         }
712 
713         if (result == null) {
714             Session session = null;
715 
716             try {
717                 session = openSession();
718 
719                 StringBuilder query = new StringBuilder();
720 
721                 query.append("FROM com.liferay.portal.model.Permission ");
722 
723                 if (obc != null) {
724                     query.append("ORDER BY ");
725                     query.append(obc.getOrderBy());
726                 }
727 
728                 Query q = session.createQuery(query.toString());
729 
730                 List<Permission> list = (List<Permission>)QueryUtil.list(q,
731                         getDialect(), start, end);
732 
733                 if (obc == null) {
734                     Collections.sort(list);
735                 }
736 
737                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
738                     finderClassName, finderMethodName, finderParams,
739                     finderArgs, list);
740 
741                 return list;
742             }
743             catch (Exception e) {
744                 throw processException(e);
745             }
746             finally {
747                 closeSession(session);
748             }
749         }
750         else {
751             return (List<Permission>)result;
752         }
753     }
754 
755     public void removeByResourceId(long resourceId) throws SystemException {
756         for (Permission permission : findByResourceId(resourceId)) {
757             remove(permission);
758         }
759     }
760 
761     public void removeByA_R(String actionId, long resourceId)
762         throws NoSuchPermissionException, SystemException {
763         Permission permission = findByA_R(actionId, resourceId);
764 
765         remove(permission);
766     }
767 
768     public void removeAll() throws SystemException {
769         for (Permission permission : findAll()) {
770             remove(permission);
771         }
772     }
773 
774     public int countByResourceId(long resourceId) throws SystemException {
775         boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED;
776         String finderClassName = Permission.class.getName();
777         String finderMethodName = "countByResourceId";
778         String[] finderParams = new String[] { Long.class.getName() };
779         Object[] finderArgs = new Object[] { new Long(resourceId) };
780 
781         Object result = null;
782 
783         if (finderClassNameCacheEnabled) {
784             result = FinderCacheUtil.getResult(finderClassName,
785                     finderMethodName, finderParams, finderArgs, this);
786         }
787 
788         if (result == null) {
789             Session session = null;
790 
791             try {
792                 session = openSession();
793 
794                 StringBuilder query = new StringBuilder();
795 
796                 query.append("SELECT COUNT(*) ");
797                 query.append("FROM com.liferay.portal.model.Permission WHERE ");
798 
799                 query.append("resourceId = ?");
800 
801                 query.append(" ");
802 
803                 Query q = session.createQuery(query.toString());
804 
805                 QueryPos qPos = QueryPos.getInstance(q);
806 
807                 qPos.add(resourceId);
808 
809                 Long count = null;
810 
811                 Iterator<Long> itr = q.list().iterator();
812 
813                 if (itr.hasNext()) {
814                     count = itr.next();
815                 }
816 
817                 if (count == null) {
818                     count = new Long(0);
819                 }
820 
821                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
822                     finderClassName, finderMethodName, finderParams,
823                     finderArgs, count);
824 
825                 return count.intValue();
826             }
827             catch (Exception e) {
828                 throw processException(e);
829             }
830             finally {
831                 closeSession(session);
832             }
833         }
834         else {
835             return ((Long)result).intValue();
836         }
837     }
838 
839     public int countByA_R(String actionId, long resourceId)
840         throws SystemException {
841         boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED;
842         String finderClassName = Permission.class.getName();
843         String finderMethodName = "countByA_R";
844         String[] finderParams = new String[] {
845                 String.class.getName(), Long.class.getName()
846             };
847         Object[] finderArgs = new Object[] { actionId, new Long(resourceId) };
848 
849         Object result = null;
850 
851         if (finderClassNameCacheEnabled) {
852             result = FinderCacheUtil.getResult(finderClassName,
853                     finderMethodName, finderParams, finderArgs, this);
854         }
855 
856         if (result == null) {
857             Session session = null;
858 
859             try {
860                 session = openSession();
861 
862                 StringBuilder query = new StringBuilder();
863 
864                 query.append("SELECT COUNT(*) ");
865                 query.append("FROM com.liferay.portal.model.Permission WHERE ");
866 
867                 if (actionId == null) {
868                     query.append("actionId IS NULL");
869                 }
870                 else {
871                     query.append("actionId = ?");
872                 }
873 
874                 query.append(" AND ");
875 
876                 query.append("resourceId = ?");
877 
878                 query.append(" ");
879 
880                 Query q = session.createQuery(query.toString());
881 
882                 QueryPos qPos = QueryPos.getInstance(q);
883 
884                 if (actionId != null) {
885                     qPos.add(actionId);
886                 }
887 
888                 qPos.add(resourceId);
889 
890                 Long count = null;
891 
892                 Iterator<Long> itr = q.list().iterator();
893 
894                 if (itr.hasNext()) {
895                     count = itr.next();
896                 }
897 
898                 if (count == null) {
899                     count = new Long(0);
900                 }
901 
902                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
903                     finderClassName, finderMethodName, finderParams,
904                     finderArgs, count);
905 
906                 return count.intValue();
907             }
908             catch (Exception e) {
909                 throw processException(e);
910             }
911             finally {
912                 closeSession(session);
913             }
914         }
915         else {
916             return ((Long)result).intValue();
917         }
918     }
919 
920     public int countAll() throws SystemException {
921         boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED;
922         String finderClassName = Permission.class.getName();
923         String finderMethodName = "countAll";
924         String[] finderParams = new String[] {  };
925         Object[] finderArgs = new Object[] {  };
926 
927         Object result = null;
928 
929         if (finderClassNameCacheEnabled) {
930             result = FinderCacheUtil.getResult(finderClassName,
931                     finderMethodName, finderParams, finderArgs, this);
932         }
933 
934         if (result == null) {
935             Session session = null;
936 
937             try {
938                 session = openSession();
939 
940                 Query q = session.createQuery(
941                         "SELECT COUNT(*) FROM com.liferay.portal.model.Permission");
942 
943                 Long count = null;
944 
945                 Iterator<Long> itr = q.list().iterator();
946 
947                 if (itr.hasNext()) {
948                     count = itr.next();
949                 }
950 
951                 if (count == null) {
952                     count = new Long(0);
953                 }
954 
955                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
956                     finderClassName, finderMethodName, finderParams,
957                     finderArgs, count);
958 
959                 return count.intValue();
960             }
961             catch (Exception e) {
962                 throw processException(e);
963             }
964             finally {
965                 closeSession(session);
966             }
967         }
968         else {
969             return ((Long)result).intValue();
970         }
971     }
972 
973     public List<com.liferay.portal.model.Group> getGroups(long pk)
974         throws SystemException {
975         return getGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
976     }
977 
978     public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
979         int end) throws SystemException {
980         return getGroups(pk, start, end, null);
981     }
982 
983     public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
984         int end, OrderByComparator obc) throws SystemException {
985         boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_GROUPS_PERMISSIONS;
986 
987         String finderClassName = "Groups_Permissions";
988 
989         String finderMethodName = "getGroups";
990         String[] finderParams = new String[] {
991                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
992                 "com.liferay.portal.kernel.util.OrderByComparator"
993             };
994         Object[] finderArgs = new Object[] {
995                 new Long(pk), String.valueOf(start), String.valueOf(end),
996                 String.valueOf(obc)
997             };
998 
999         Object result = null;
1000
1001        if (finderClassNameCacheEnabled) {
1002            result = FinderCacheUtil.getResult(finderClassName,
1003                    finderMethodName, finderParams, finderArgs, this);
1004        }
1005
1006        if (result == null) {
1007            Session session = null;
1008
1009            try {
1010                session = openSession();
1011
1012                StringBuilder sb = new StringBuilder();
1013
1014                sb.append(_SQL_GETGROUPS);
1015
1016                if (obc != null) {
1017                    sb.append("ORDER BY ");
1018                    sb.append(obc.getOrderBy());
1019                }
1020
1021                else {
1022                    sb.append("ORDER BY ");
1023
1024                    sb.append("Group_.name ASC");
1025                }
1026
1027                String sql = sb.toString();
1028
1029                SQLQuery q = session.createSQLQuery(sql);
1030
1031                q.addEntity("Group_",
1032                    com.liferay.portal.model.impl.GroupImpl.class);
1033
1034                QueryPos qPos = QueryPos.getInstance(q);
1035
1036                qPos.add(pk);
1037
1038                List<com.liferay.portal.model.Group> list = (List<com.liferay.portal.model.Group>)QueryUtil.list(q,
1039                        getDialect(), start, end);
1040
1041                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1042                    finderClassName, finderMethodName, finderParams,
1043                    finderArgs, list);
1044
1045                return list;
1046            }
1047            catch (Exception e) {
1048                throw processException(e);
1049            }
1050            finally {
1051                closeSession(session);
1052            }
1053        }
1054        else {
1055            return (List<com.liferay.portal.model.Group>)result;
1056        }
1057    }
1058
1059    public int getGroupsSize(long pk) throws SystemException {
1060        boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_GROUPS_PERMISSIONS;
1061
1062        String finderClassName = "Groups_Permissions";
1063
1064        String finderMethodName = "getGroupsSize";
1065        String[] finderParams = new String[] { Long.class.getName() };
1066        Object[] finderArgs = new Object[] { new Long(pk) };
1067
1068        Object result = null;
1069
1070        if (finderClassNameCacheEnabled) {
1071            result = FinderCacheUtil.getResult(finderClassName,
1072                    finderMethodName, finderParams, finderArgs, this);
1073        }
1074
1075        if (result == null) {
1076            Session session = null;
1077
1078            try {
1079                session = openSession();
1080
1081                SQLQuery q = session.createSQLQuery(_SQL_GETGROUPSSIZE);
1082
1083                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1084
1085                QueryPos qPos = QueryPos.getInstance(q);
1086
1087                qPos.add(pk);
1088
1089                Long count = null;
1090
1091                Iterator<Long> itr = q.list().iterator();
1092
1093                if (itr.hasNext()) {
1094                    count = itr.next();
1095                }
1096
1097                if (count == null) {
1098                    count = new Long(0);
1099                }
1100
1101                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1102                    finderClassName, finderMethodName, finderParams,
1103                    finderArgs, count);
1104
1105                return count.intValue();
1106            }
1107            catch (Exception e) {
1108                throw processException(e);
1109            }
1110            finally {
1111                closeSession(session);
1112            }
1113        }
1114        else {
1115            return ((Long)result).intValue();
1116        }
1117    }
1118
1119    public boolean containsGroup(long pk, long groupPK)
1120        throws SystemException {
1121        boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_GROUPS_PERMISSIONS;
1122
1123        String finderClassName = "Groups_Permissions";
1124
1125        String finderMethodName = "containsGroups";
1126        String[] finderParams = new String[] {
1127                Long.class.getName(),
1128                
1129                Long.class.getName()
1130            };
1131        Object[] finderArgs = new Object[] { new Long(pk), new Long(groupPK) };
1132
1133        Object result = null;
1134
1135        if (finderClassNameCacheEnabled) {
1136            result = FinderCacheUtil.getResult(finderClassName,
1137                    finderMethodName, finderParams, finderArgs, this);
1138        }
1139
1140        if (result == null) {
1141            try {
1142                Boolean value = Boolean.valueOf(containsGroup.contains(pk,
1143                            groupPK));
1144
1145                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1146                    finderClassName, finderMethodName, finderParams,
1147                    finderArgs, value);
1148
1149                return value.booleanValue();
1150            }
1151            catch (Exception e) {
1152                throw processException(e);
1153            }
1154        }
1155        else {
1156            return ((Boolean)result).booleanValue();
1157        }
1158    }
1159
1160    public boolean containsGroups(long pk) throws SystemException {
1161        if (getGroupsSize(pk) > 0) {
1162            return true;
1163        }
1164        else {
1165            return false;
1166        }
1167    }
1168
1169    public void addGroup(long pk, long groupPK) throws SystemException {
1170        try {
1171            addGroup.add(pk, groupPK);
1172        }
1173        catch (Exception e) {
1174            throw processException(e);
1175        }
1176        finally {
1177            FinderCacheUtil.clearCache("Groups_Permissions");
1178        }
1179    }
1180
1181    public void addGroup(long pk, com.liferay.portal.model.Group group)
1182        throws SystemException {
1183        try {
1184            addGroup.add(pk, group.getPrimaryKey());
1185        }
1186        catch (Exception e) {
1187            throw processException(e);
1188        }
1189        finally {
1190            FinderCacheUtil.clearCache("Groups_Permissions");
1191        }
1192    }
1193
1194    public void addGroups(long pk, long[] groupPKs) throws SystemException {
1195        try {
1196            for (long groupPK : groupPKs) {
1197                addGroup.add(pk, groupPK);
1198            }
1199        }
1200        catch (Exception e) {
1201            throw processException(e);
1202        }
1203        finally {
1204            FinderCacheUtil.clearCache("Groups_Permissions");
1205        }
1206    }
1207
1208    public void addGroups(long pk, List<com.liferay.portal.model.Group> groups)
1209        throws SystemException {
1210        try {
1211            for (com.liferay.portal.model.Group group : groups) {
1212                addGroup.add(pk, group.getPrimaryKey());
1213            }
1214        }
1215        catch (Exception e) {
1216            throw processException(e);
1217        }
1218        finally {
1219            FinderCacheUtil.clearCache("Groups_Permissions");
1220        }
1221    }
1222
1223    public void clearGroups(long pk) throws SystemException {
1224        try {
1225            clearGroups.clear(pk);
1226        }
1227        catch (Exception e) {
1228            throw processException(e);
1229        }
1230        finally {
1231            FinderCacheUtil.clearCache("Groups_Permissions");
1232        }
1233    }
1234
1235    public void removeGroup(long pk, long groupPK) throws SystemException {
1236        try {
1237            removeGroup.remove(pk, groupPK);
1238        }
1239        catch (Exception e) {
1240            throw processException(e);
1241        }
1242        finally {
1243            FinderCacheUtil.clearCache("Groups_Permissions");
1244        }
1245    }
1246
1247    public void removeGroup(long pk, com.liferay.portal.model.Group group)
1248        throws SystemException {
1249        try {
1250            removeGroup.remove(pk, group.getPrimaryKey());
1251        }
1252        catch (Exception e) {
1253            throw processException(e);
1254        }
1255        finally {
1256            FinderCacheUtil.clearCache("Groups_Permissions");
1257        }
1258    }
1259
1260    public void removeGroups(long pk, long[] groupPKs)
1261        throws SystemException {
1262        try {
1263            for (long groupPK : groupPKs) {
1264                removeGroup.remove(pk, groupPK);
1265            }
1266        }
1267        catch (Exception e) {
1268            throw processException(e);
1269        }
1270        finally {
1271            FinderCacheUtil.clearCache("Groups_Permissions");
1272        }
1273    }
1274
1275    public void removeGroups(long pk,
1276        List<com.liferay.portal.model.Group> groups) throws SystemException {
1277        try {
1278            for (com.liferay.portal.model.Group group : groups) {
1279                removeGroup.remove(pk, group.getPrimaryKey());
1280            }
1281        }
1282        catch (Exception e) {
1283            throw processException(e);
1284        }
1285        finally {
1286            FinderCacheUtil.clearCache("Groups_Permissions");
1287        }
1288    }
1289
1290    public void setGroups(long pk, long[] groupPKs) throws SystemException {
1291        try {
1292            clearGroups.clear(pk);
1293
1294            for (long groupPK : groupPKs) {
1295                addGroup.add(pk, groupPK);
1296            }
1297        }
1298        catch (Exception e) {
1299            throw processException(e);
1300        }
1301        finally {
1302            FinderCacheUtil.clearCache("Groups_Permissions");
1303        }
1304    }
1305
1306    public void setGroups(long pk, List<com.liferay.portal.model.Group> groups)
1307        throws SystemException {
1308        try {
1309            clearGroups.clear(pk);
1310
1311            for (com.liferay.portal.model.Group group : groups) {
1312                addGroup.add(pk, group.getPrimaryKey());
1313            }
1314        }
1315        catch (Exception e) {
1316            throw processException(e);
1317        }
1318        finally {
1319            FinderCacheUtil.clearCache("Groups_Permissions");
1320        }
1321    }
1322
1323    public List<com.liferay.portal.model.Role> getRoles(long pk)
1324        throws SystemException {
1325        return getRoles(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1326    }
1327
1328    public List<com.liferay.portal.model.Role> getRoles(long pk, int start,
1329        int end) throws SystemException {
1330        return getRoles(pk, start, end, null);
1331    }
1332
1333    public List<com.liferay.portal.model.Role> getRoles(long pk, int start,
1334        int end, OrderByComparator obc) throws SystemException {
1335        boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_ROLES_PERMISSIONS;
1336
1337        String finderClassName = "Roles_Permissions";
1338
1339        String finderMethodName = "getRoles";
1340        String[] finderParams = new String[] {
1341                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1342                "com.liferay.portal.kernel.util.OrderByComparator"
1343            };
1344        Object[] finderArgs = new Object[] {
1345                new Long(pk), String.valueOf(start), String.valueOf(end),
1346                String.valueOf(obc)
1347            };
1348
1349        Object result = null;
1350
1351        if (finderClassNameCacheEnabled) {
1352            result = FinderCacheUtil.getResult(finderClassName,
1353                    finderMethodName, finderParams, finderArgs, this);
1354        }
1355
1356        if (result == null) {
1357            Session session = null;
1358
1359            try {
1360                session = openSession();
1361
1362                StringBuilder sb = new StringBuilder();
1363
1364                sb.append(_SQL_GETROLES);
1365
1366                if (obc != null) {
1367                    sb.append("ORDER BY ");
1368                    sb.append(obc.getOrderBy());
1369                }
1370
1371                else {
1372                    sb.append("ORDER BY ");
1373
1374                    sb.append("Role_.name ASC");
1375                }
1376
1377                String sql = sb.toString();
1378
1379                SQLQuery q = session.createSQLQuery(sql);
1380
1381                q.addEntity("Role_",
1382                    com.liferay.portal.model.impl.RoleImpl.class);
1383
1384                QueryPos qPos = QueryPos.getInstance(q);
1385
1386                qPos.add(pk);
1387
1388                List<com.liferay.portal.model.Role> list = (List<com.liferay.portal.model.Role>)QueryUtil.list(q,
1389                        getDialect(), start, end);
1390
1391                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1392                    finderClassName, finderMethodName, finderParams,
1393                    finderArgs, list);
1394
1395                return list;
1396            }
1397            catch (Exception e) {
1398                throw processException(e);
1399            }
1400            finally {
1401                closeSession(session);
1402            }
1403        }
1404        else {
1405            return (List<com.liferay.portal.model.Role>)result;
1406        }
1407    }
1408
1409    public int getRolesSize(long pk) throws SystemException {
1410        boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_ROLES_PERMISSIONS;
1411
1412        String finderClassName = "Roles_Permissions";
1413
1414        String finderMethodName = "getRolesSize";
1415        String[] finderParams = new String[] { Long.class.getName() };
1416        Object[] finderArgs = new Object[] { new Long(pk) };
1417
1418        Object result = null;
1419
1420        if (finderClassNameCacheEnabled) {
1421            result = FinderCacheUtil.getResult(finderClassName,
1422                    finderMethodName, finderParams, finderArgs, this);
1423        }
1424
1425        if (result == null) {
1426            Session session = null;
1427
1428            try {
1429                session = openSession();
1430
1431                SQLQuery q = session.createSQLQuery(_SQL_GETROLESSIZE);
1432
1433                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1434
1435                QueryPos qPos = QueryPos.getInstance(q);
1436
1437                qPos.add(pk);
1438
1439                Long count = null;
1440
1441                Iterator<Long> itr = q.list().iterator();
1442
1443                if (itr.hasNext()) {
1444                    count = itr.next();
1445                }
1446
1447                if (count == null) {
1448                    count = new Long(0);
1449                }
1450
1451                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1452                    finderClassName, finderMethodName, finderParams,
1453                    finderArgs, count);
1454
1455                return count.intValue();
1456            }
1457            catch (Exception e) {
1458                throw processException(e);
1459            }
1460            finally {
1461                closeSession(session);
1462            }
1463        }
1464        else {
1465            return ((Long)result).intValue();
1466        }
1467    }
1468
1469    public boolean containsRole(long pk, long rolePK) throws SystemException {
1470        boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_ROLES_PERMISSIONS;
1471
1472        String finderClassName = "Roles_Permissions";
1473
1474        String finderMethodName = "containsRoles";
1475        String[] finderParams = new String[] {
1476                Long.class.getName(),
1477                
1478                Long.class.getName()
1479            };
1480        Object[] finderArgs = new Object[] { new Long(pk), new Long(rolePK) };
1481
1482        Object result = null;
1483
1484        if (finderClassNameCacheEnabled) {
1485            result = FinderCacheUtil.getResult(finderClassName,
1486                    finderMethodName, finderParams, finderArgs, this);
1487        }
1488
1489        if (result == null) {
1490            try {
1491                Boolean value = Boolean.valueOf(containsRole.contains(pk, rolePK));
1492
1493                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1494                    finderClassName, finderMethodName, finderParams,
1495                    finderArgs, value);
1496
1497                return value.booleanValue();
1498            }
1499            catch (Exception e) {
1500                throw processException(e);
1501            }
1502        }
1503        else {
1504            return ((Boolean)result).booleanValue();
1505        }
1506    }
1507
1508    public boolean containsRoles(long pk) throws SystemException {
1509        if (getRolesSize(pk) > 0) {
1510            return true;
1511        }
1512        else {
1513            return false;
1514        }
1515    }
1516
1517    public void addRole(long pk, long rolePK) throws SystemException {
1518        try {
1519            addRole.add(pk, rolePK);
1520        }
1521        catch (Exception e) {
1522            throw processException(e);
1523        }
1524        finally {
1525            FinderCacheUtil.clearCache("Roles_Permissions");
1526        }
1527    }
1528
1529    public void addRole(long pk, com.liferay.portal.model.Role role)
1530        throws SystemException {
1531        try {
1532            addRole.add(pk, role.getPrimaryKey());
1533        }
1534        catch (Exception e) {
1535            throw processException(e);
1536        }
1537        finally {
1538            FinderCacheUtil.clearCache("Roles_Permissions");
1539        }
1540    }
1541
1542    public void addRoles(long pk, long[] rolePKs) throws SystemException {
1543        try {
1544            for (long rolePK : rolePKs) {
1545                addRole.add(pk, rolePK);
1546            }
1547        }
1548        catch (Exception e) {
1549            throw processException(e);
1550        }
1551        finally {
1552            FinderCacheUtil.clearCache("Roles_Permissions");
1553        }
1554    }
1555
1556    public void addRoles(long pk, List<com.liferay.portal.model.Role> roles)
1557        throws SystemException {
1558        try {
1559            for (com.liferay.portal.model.Role role : roles) {
1560                addRole.add(pk, role.getPrimaryKey());
1561            }
1562        }
1563        catch (Exception e) {
1564            throw processException(e);
1565        }
1566        finally {
1567            FinderCacheUtil.clearCache("Roles_Permissions");
1568        }
1569    }
1570
1571    public void clearRoles(long pk) throws SystemException {
1572        try {
1573            clearRoles.clear(pk);
1574        }
1575        catch (Exception e) {
1576            throw processException(e);
1577        }
1578        finally {
1579            FinderCacheUtil.clearCache("Roles_Permissions");
1580        }
1581    }
1582
1583    public void removeRole(long pk, long rolePK) throws SystemException {
1584        try {
1585            removeRole.remove(pk, rolePK);
1586        }
1587        catch (Exception e) {
1588            throw processException(e);
1589        }
1590        finally {
1591            FinderCacheUtil.clearCache("Roles_Permissions");
1592        }
1593    }
1594
1595    public void removeRole(long pk, com.liferay.portal.model.Role role)
1596        throws SystemException {
1597        try {
1598            removeRole.remove(pk, role.getPrimaryKey());
1599        }
1600        catch (Exception e) {
1601            throw processException(e);
1602        }
1603        finally {
1604            FinderCacheUtil.clearCache("Roles_Permissions");
1605        }
1606    }
1607
1608    public void removeRoles(long pk, long[] rolePKs) throws SystemException {
1609        try {
1610            for (long rolePK : rolePKs) {
1611                removeRole.remove(pk, rolePK);
1612            }
1613        }
1614        catch (Exception e) {
1615            throw processException(e);
1616        }
1617        finally {
1618            FinderCacheUtil.clearCache("Roles_Permissions");
1619        }
1620    }
1621
1622    public void removeRoles(long pk, List<com.liferay.portal.model.Role> roles)
1623        throws SystemException {
1624        try {
1625            for (com.liferay.portal.model.Role role : roles) {
1626                removeRole.remove(pk, role.getPrimaryKey());
1627            }
1628        }
1629        catch (Exception e) {
1630            throw processException(e);
1631        }
1632        finally {
1633            FinderCacheUtil.clearCache("Roles_Permissions");
1634        }
1635    }
1636
1637    public void setRoles(long pk, long[] rolePKs) throws SystemException {
1638        try {
1639            clearRoles.clear(pk);
1640
1641            for (long rolePK : rolePKs) {
1642                addRole.add(pk, rolePK);
1643            }
1644        }
1645        catch (Exception e) {
1646            throw processException(e);
1647        }
1648        finally {
1649            FinderCacheUtil.clearCache("Roles_Permissions");
1650        }
1651    }
1652
1653    public void setRoles(long pk, List<com.liferay.portal.model.Role> roles)
1654        throws SystemException {
1655        try {
1656            clearRoles.clear(pk);
1657
1658            for (com.liferay.portal.model.Role role : roles) {
1659                addRole.add(pk, role.getPrimaryKey());
1660            }
1661        }
1662        catch (Exception e) {
1663            throw processException(e);
1664        }
1665        finally {
1666            FinderCacheUtil.clearCache("Roles_Permissions");
1667        }
1668    }
1669
1670    public List<com.liferay.portal.model.User> getUsers(long pk)
1671        throws SystemException {
1672        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1673    }
1674
1675    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1676        int end) throws SystemException {
1677        return getUsers(pk, start, end, null);
1678    }
1679
1680    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1681        int end, OrderByComparator obc) throws SystemException {
1682        boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_USERS_PERMISSIONS;
1683
1684        String finderClassName = "Users_Permissions";
1685
1686        String finderMethodName = "getUsers";
1687        String[] finderParams = new String[] {
1688                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1689                "com.liferay.portal.kernel.util.OrderByComparator"
1690            };
1691        Object[] finderArgs = new Object[] {
1692                new Long(pk), String.valueOf(start), String.valueOf(end),
1693                String.valueOf(obc)
1694            };
1695
1696        Object result = null;
1697
1698        if (finderClassNameCacheEnabled) {
1699            result = FinderCacheUtil.getResult(finderClassName,
1700                    finderMethodName, finderParams, finderArgs, this);
1701        }
1702
1703        if (result == null) {
1704            Session session = null;
1705
1706            try {
1707                session = openSession();
1708
1709                StringBuilder sb = new StringBuilder();
1710
1711                sb.append(_SQL_GETUSERS);
1712
1713                if (obc != null) {
1714                    sb.append("ORDER BY ");
1715                    sb.append(obc.getOrderBy());
1716                }
1717
1718                String sql = sb.toString();
1719
1720                SQLQuery q = session.createSQLQuery(sql);
1721
1722                q.addEntity("User_",
1723                    com.liferay.portal.model.impl.UserImpl.class);
1724
1725                QueryPos qPos = QueryPos.getInstance(q);
1726
1727                qPos.add(pk);
1728
1729                List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
1730                        getDialect(), start, end);
1731
1732                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1733                    finderClassName, finderMethodName, finderParams,
1734                    finderArgs, list);
1735
1736                return list;
1737            }
1738            catch (Exception e) {
1739                throw processException(e);
1740            }
1741            finally {
1742                closeSession(session);
1743            }
1744        }
1745        else {
1746            return (List<com.liferay.portal.model.User>)result;
1747        }
1748    }
1749
1750    public int getUsersSize(long pk) throws SystemException {
1751        boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_USERS_PERMISSIONS;
1752
1753        String finderClassName = "Users_Permissions";
1754
1755        String finderMethodName = "getUsersSize";
1756        String[] finderParams = new String[] { Long.class.getName() };
1757        Object[] finderArgs = new Object[] { new Long(pk) };
1758
1759        Object result = null;
1760
1761        if (finderClassNameCacheEnabled) {
1762            result = FinderCacheUtil.getResult(finderClassName,
1763                    finderMethodName, finderParams, finderArgs, this);
1764        }
1765
1766        if (result == null) {
1767            Session session = null;
1768
1769            try {
1770                session = openSession();
1771
1772                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
1773
1774                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1775
1776                QueryPos qPos = QueryPos.getInstance(q);
1777
1778                qPos.add(pk);
1779
1780                Long count = null;
1781
1782                Iterator<Long> itr = q.list().iterator();
1783
1784                if (itr.hasNext()) {
1785                    count = itr.next();
1786                }
1787
1788                if (count == null) {
1789                    count = new Long(0);
1790                }
1791
1792                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1793                    finderClassName, finderMethodName, finderParams,
1794                    finderArgs, count);
1795
1796                return count.intValue();
1797            }
1798            catch (Exception e) {
1799                throw processException(e);
1800            }
1801            finally {
1802                closeSession(session);
1803            }
1804        }
1805        else {
1806            return ((Long)result).intValue();
1807        }
1808    }
1809
1810    public boolean containsUser(long pk, long userPK) throws SystemException {
1811        boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED_USERS_PERMISSIONS;
1812
1813        String finderClassName = "Users_Permissions";
1814
1815        String finderMethodName = "containsUsers";
1816        String[] finderParams = new String[] {
1817                Long.class.getName(),
1818                
1819                Long.class.getName()
1820            };
1821        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
1822
1823        Object result = null;
1824
1825        if (finderClassNameCacheEnabled) {
1826            result = FinderCacheUtil.getResult(finderClassName,
1827                    finderMethodName, finderParams, finderArgs, this);
1828        }
1829
1830        if (result == null) {
1831            try {
1832                Boolean value = Boolean.valueOf(containsUser.contains(pk, userPK));
1833
1834                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1835                    finderClassName, finderMethodName, finderParams,
1836                    finderArgs, value);
1837
1838                return value.booleanValue();
1839            }
1840            catch (Exception e) {
1841                throw processException(e);
1842            }
1843        }
1844        else {
1845            return ((Boolean)result).booleanValue();
1846        }
1847    }
1848
1849    public boolean containsUsers(long pk) throws SystemException {
1850        if (getUsersSize(pk) > 0) {
1851            return true;
1852        }
1853        else {
1854            return false;
1855        }
1856    }
1857
1858    public void addUser(long pk, long userPK) throws SystemException {
1859        try {
1860            addUser.add(pk, userPK);
1861        }
1862        catch (Exception e) {
1863            throw processException(e);
1864        }
1865        finally {
1866            FinderCacheUtil.clearCache("Users_Permissions");
1867        }
1868    }
1869
1870    public void addUser(long pk, com.liferay.portal.model.User user)
1871        throws SystemException {
1872        try {
1873            addUser.add(pk, user.getPrimaryKey());
1874        }
1875        catch (Exception e) {
1876            throw processException(e);
1877        }
1878        finally {
1879            FinderCacheUtil.clearCache("Users_Permissions");
1880        }
1881    }
1882
1883    public void addUsers(long pk, long[] userPKs) throws SystemException {
1884        try {
1885            for (long userPK : userPKs) {
1886                addUser.add(pk, userPK);
1887            }
1888        }
1889        catch (Exception e) {
1890            throw processException(e);
1891        }
1892        finally {
1893            FinderCacheUtil.clearCache("Users_Permissions");
1894        }
1895    }
1896
1897    public void addUsers(long pk, List<com.liferay.portal.model.User> users)
1898        throws SystemException {
1899        try {
1900            for (com.liferay.portal.model.User user : users) {
1901                addUser.add(pk, user.getPrimaryKey());
1902            }
1903        }
1904        catch (Exception e) {
1905            throw processException(e);
1906        }
1907        finally {
1908            FinderCacheUtil.clearCache("Users_Permissions");
1909        }
1910    }
1911
1912    public void clearUsers(long pk) throws SystemException {
1913        try {
1914            clearUsers.clear(pk);
1915        }
1916        catch (Exception e) {
1917            throw processException(e);
1918        }
1919        finally {
1920            FinderCacheUtil.clearCache("Users_Permissions");
1921        }
1922    }
1923
1924    public void removeUser(long pk, long userPK) throws SystemException {
1925        try {
1926            removeUser.remove(pk, userPK);
1927        }
1928        catch (Exception e) {
1929            throw processException(e);
1930        }
1931        finally {
1932            FinderCacheUtil.clearCache("Users_Permissions");
1933        }
1934    }
1935
1936    public void removeUser(long pk, com.liferay.portal.model.User user)
1937        throws SystemException {
1938        try {
1939            removeUser.remove(pk, user.getPrimaryKey());
1940        }
1941        catch (Exception e) {
1942            throw processException(e);
1943        }
1944        finally {
1945            FinderCacheUtil.clearCache("Users_Permissions");
1946        }
1947    }
1948
1949    public void removeUsers(long pk, long[] userPKs) throws SystemException {
1950        try {
1951            for (long userPK : userPKs) {
1952                removeUser.remove(pk, userPK);
1953            }
1954        }
1955        catch (Exception e) {
1956            throw processException(e);
1957        }
1958        finally {
1959            FinderCacheUtil.clearCache("Users_Permissions");
1960        }
1961    }
1962
1963    public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
1964        throws SystemException {
1965        try {
1966            for (com.liferay.portal.model.User user : users) {
1967                removeUser.remove(pk, user.getPrimaryKey());
1968            }
1969        }
1970        catch (Exception e) {
1971            throw processException(e);
1972        }
1973        finally {
1974            FinderCacheUtil.clearCache("Users_Permissions");
1975        }
1976    }
1977
1978    public void setUsers(long pk, long[] userPKs) throws SystemException {
1979        try {
1980            clearUsers.clear(pk);
1981
1982            for (long userPK : userPKs) {
1983                addUser.add(pk, userPK);
1984            }
1985        }
1986        catch (Exception e) {
1987            throw processException(e);
1988        }
1989        finally {
1990            FinderCacheUtil.clearCache("Users_Permissions");
1991        }
1992    }
1993
1994    public void setUsers(long pk, List<com.liferay.portal.model.User> users)
1995        throws SystemException {
1996        try {
1997            clearUsers.clear(pk);
1998
1999            for (com.liferay.portal.model.User user : users) {
2000                addUser.add(pk, user.getPrimaryKey());
2001            }
2002        }
2003        catch (Exception e) {
2004            throw processException(e);
2005        }
2006        finally {
2007            FinderCacheUtil.clearCache("Users_Permissions");
2008        }
2009    }
2010
2011    public void registerListener(ModelListener listener) {
2012        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
2013
2014        listeners.add(listener);
2015
2016        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2017    }
2018
2019    public void unregisterListener(ModelListener listener) {
2020        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
2021
2022        listeners.remove(listener);
2023
2024        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2025    }
2026
2027    public void afterPropertiesSet() {
2028        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2029                    com.liferay.portal.util.PropsUtil.get(
2030                        "value.object.listener.com.liferay.portal.model.Permission")));
2031
2032        if (listenerClassNames.length > 0) {
2033            try {
2034                List<ModelListener> listeners = new ArrayList<ModelListener>();
2035
2036                for (String listenerClassName : listenerClassNames) {
2037                    listeners.add((ModelListener)Class.forName(
2038                            listenerClassName).newInstance());
2039                }
2040
2041                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2042            }
2043            catch (Exception e) {
2044                _log.error(e);
2045            }
2046        }
2047
2048        containsGroup = new ContainsGroup(this);
2049
2050        addGroup = new AddGroup(this);
2051        clearGroups = new ClearGroups(this);
2052        removeGroup = new RemoveGroup(this);
2053
2054        containsRole = new ContainsRole(this);
2055
2056        addRole = new AddRole(this);
2057        clearRoles = new ClearRoles(this);
2058        removeRole = new RemoveRole(this);
2059
2060        containsUser = new ContainsUser(this);
2061
2062        addUser = new AddUser(this);
2063        clearUsers = new ClearUsers(this);
2064        removeUser = new RemoveUser(this);
2065    }
2066
2067    protected ContainsGroup containsGroup;
2068    protected AddGroup addGroup;
2069    protected ClearGroups clearGroups;
2070    protected RemoveGroup removeGroup;
2071    protected ContainsRole containsRole;
2072    protected AddRole addRole;
2073    protected ClearRoles clearRoles;
2074    protected RemoveRole removeRole;
2075    protected ContainsUser containsUser;
2076    protected AddUser addUser;
2077    protected ClearUsers clearUsers;
2078    protected RemoveUser removeUser;
2079
2080    protected class ContainsGroup {
2081        protected ContainsGroup(PermissionPersistenceImpl persistenceImpl) {
2082            super();
2083
2084            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2085                    _SQL_CONTAINSGROUP,
2086                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2087        }
2088
2089        protected boolean contains(long permissionId, long groupId) {
2090            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2091                        new Long(permissionId), new Long(groupId)
2092                    });
2093
2094            if (results.size() > 0) {
2095                Integer count = results.get(0);
2096
2097                if (count.intValue() > 0) {
2098                    return true;
2099                }
2100            }
2101
2102            return false;
2103        }
2104
2105        private MappingSqlQuery _mappingSqlQuery;
2106    }
2107
2108    protected class AddGroup {
2109        protected AddGroup(PermissionPersistenceImpl persistenceImpl) {
2110            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2111                    "INSERT INTO Groups_Permissions (permissionId, groupId) VALUES (?, ?)",
2112                    new int[] { Types.BIGINT, Types.BIGINT });
2113            _persistenceImpl = persistenceImpl;
2114        }
2115
2116        protected void add(long permissionId, long groupId) {
2117            if (!_persistenceImpl.containsGroup.contains(permissionId, groupId)) {
2118                _sqlUpdate.update(new Object[] {
2119                        new Long(permissionId), new Long(groupId)
2120                    });
2121            }
2122        }
2123
2124        private SqlUpdate _sqlUpdate;
2125        private PermissionPersistenceImpl _persistenceImpl;
2126    }
2127
2128    protected class ClearGroups {
2129        protected ClearGroups(PermissionPersistenceImpl persistenceImpl) {
2130            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2131                    "DELETE FROM Groups_Permissions WHERE permissionId = ?",
2132                    new int[] { Types.BIGINT });
2133        }
2134
2135        protected void clear(long permissionId) {
2136            _sqlUpdate.update(new Object[] { new Long(permissionId) });
2137        }
2138
2139        private SqlUpdate _sqlUpdate;
2140    }
2141
2142    protected class RemoveGroup {
2143        protected RemoveGroup(PermissionPersistenceImpl persistenceImpl) {
2144            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2145                    "DELETE FROM Groups_Permissions WHERE permissionId = ? AND groupId = ?",
2146                    new int[] { Types.BIGINT, Types.BIGINT });
2147        }
2148
2149        protected void remove(long permissionId, long groupId) {
2150            _sqlUpdate.update(new Object[] {
2151                    new Long(permissionId), new Long(groupId)
2152                });
2153        }
2154
2155        private SqlUpdate _sqlUpdate;
2156    }
2157
2158    protected class ContainsRole {
2159        protected ContainsRole(PermissionPersistenceImpl persistenceImpl) {
2160            super();
2161
2162            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2163                    _SQL_CONTAINSROLE,
2164                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2165        }
2166
2167        protected boolean contains(long permissionId, long roleId) {
2168            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2169                        new Long(permissionId), new Long(roleId)
2170                    });
2171
2172            if (results.size() > 0) {
2173                Integer count = results.get(0);
2174
2175                if (count.intValue() > 0) {
2176                    return true;
2177                }
2178            }
2179
2180            return false;
2181        }
2182
2183        private MappingSqlQuery _mappingSqlQuery;
2184    }
2185
2186    protected class AddRole {
2187        protected AddRole(PermissionPersistenceImpl persistenceImpl) {
2188            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2189                    "INSERT INTO Roles_Permissions (permissionId, roleId) VALUES (?, ?)",
2190                    new int[] { Types.BIGINT, Types.BIGINT });
2191            _persistenceImpl = persistenceImpl;
2192        }
2193
2194        protected void add(long permissionId, long roleId) {
2195            if (!_persistenceImpl.containsRole.contains(permissionId, roleId)) {
2196                _sqlUpdate.update(new Object[] {
2197                        new Long(permissionId), new Long(roleId)
2198                    });
2199            }
2200        }
2201
2202        private SqlUpdate _sqlUpdate;
2203        private PermissionPersistenceImpl _persistenceImpl;
2204    }
2205
2206    protected class ClearRoles {
2207        protected ClearRoles(PermissionPersistenceImpl persistenceImpl) {
2208            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2209                    "DELETE FROM Roles_Permissions WHERE permissionId = ?",
2210                    new int[] { Types.BIGINT });
2211        }
2212
2213        protected void clear(long permissionId) {
2214            _sqlUpdate.update(new Object[] { new Long(permissionId) });
2215        }
2216
2217        private SqlUpdate _sqlUpdate;
2218    }
2219
2220    protected class RemoveRole {
2221        protected RemoveRole(PermissionPersistenceImpl persistenceImpl) {
2222            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2223                    "DELETE FROM Roles_Permissions WHERE permissionId = ? AND roleId = ?",
2224                    new int[] { Types.BIGINT, Types.BIGINT });
2225        }
2226
2227        protected void remove(long permissionId, long roleId) {
2228            _sqlUpdate.update(new Object[] {
2229                    new Long(permissionId), new Long(roleId)
2230                });
2231        }
2232
2233        private SqlUpdate _sqlUpdate;
2234    }
2235
2236    protected class ContainsUser {
2237        protected ContainsUser(PermissionPersistenceImpl persistenceImpl) {
2238            super();
2239
2240            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2241                    _SQL_CONTAINSUSER,
2242                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2243        }
2244
2245        protected boolean contains(long permissionId, long userId) {
2246            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2247                        new Long(permissionId), new Long(userId)
2248                    });
2249
2250            if (results.size() > 0) {
2251                Integer count = results.get(0);
2252
2253                if (count.intValue() > 0) {
2254                    return true;
2255                }
2256            }
2257
2258            return false;
2259        }
2260
2261        private MappingSqlQuery _mappingSqlQuery;
2262    }
2263
2264    protected class AddUser {
2265        protected AddUser(PermissionPersistenceImpl persistenceImpl) {
2266            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2267                    "INSERT INTO Users_Permissions (permissionId, userId) VALUES (?, ?)",
2268                    new int[] { Types.BIGINT, Types.BIGINT });
2269            _persistenceImpl = persistenceImpl;
2270        }
2271
2272        protected void add(long permissionId, long userId) {
2273            if (!_persistenceImpl.containsUser.contains(permissionId, userId)) {
2274                _sqlUpdate.update(new Object[] {
2275                        new Long(permissionId), new Long(userId)
2276                    });
2277            }
2278        }
2279
2280        private SqlUpdate _sqlUpdate;
2281        private PermissionPersistenceImpl _persistenceImpl;
2282    }
2283
2284    protected class ClearUsers {
2285        protected ClearUsers(PermissionPersistenceImpl persistenceImpl) {
2286            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2287                    "DELETE FROM Users_Permissions WHERE permissionId = ?",
2288                    new int[] { Types.BIGINT });
2289        }
2290
2291        protected void clear(long permissionId) {
2292            _sqlUpdate.update(new Object[] { new Long(permissionId) });
2293        }
2294
2295        private SqlUpdate _sqlUpdate;
2296    }
2297
2298    protected class RemoveUser {
2299        protected RemoveUser(PermissionPersistenceImpl persistenceImpl) {
2300            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2301                    "DELETE FROM Users_Permissions WHERE permissionId = ? AND userId = ?",
2302                    new int[] { Types.BIGINT, Types.BIGINT });
2303        }
2304
2305        protected void remove(long permissionId, long userId) {
2306            _sqlUpdate.update(new Object[] {
2307                    new Long(permissionId), new Long(userId)
2308                });
2309        }
2310
2311        private SqlUpdate _sqlUpdate;
2312    }
2313
2314    private static final String _SQL_GETGROUPS = "SELECT {Group_.*} FROM Group_ INNER JOIN Groups_Permissions ON (Groups_Permissions.groupId = Group_.groupId) WHERE (Groups_Permissions.permissionId = ?)";
2315    private static final String _SQL_GETGROUPSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Permissions WHERE permissionId = ?";
2316    private static final String _SQL_CONTAINSGROUP = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Permissions WHERE permissionId = ? AND groupId = ?";
2317    private static final String _SQL_GETROLES = "SELECT {Role_.*} FROM Role_ INNER JOIN Roles_Permissions ON (Roles_Permissions.roleId = Role_.roleId) WHERE (Roles_Permissions.permissionId = ?)";
2318    private static final String _SQL_GETROLESSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Roles_Permissions WHERE permissionId = ?";
2319    private static final String _SQL_CONTAINSROLE = "SELECT COUNT(*) AS COUNT_VALUE FROM Roles_Permissions WHERE permissionId = ? AND roleId = ?";
2320    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_Permissions ON (Users_Permissions.userId = User_.userId) WHERE (Users_Permissions.permissionId = ?)";
2321    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Permissions WHERE permissionId = ?";
2322    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Permissions WHERE permissionId = ? AND userId = ?";
2323    private static Log _log = LogFactory.getLog(PermissionPersistenceImpl.class);
2324    private ModelListener[] _listeners = new ModelListener[0];
2325}