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