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