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.NoSuchRoleException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.annotation.BeanReference;
28  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
29  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
30  import com.liferay.portal.kernel.dao.jdbc.RowMapper;
31  import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
32  import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
33  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
34  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
35  import com.liferay.portal.kernel.dao.orm.Query;
36  import com.liferay.portal.kernel.dao.orm.QueryPos;
37  import com.liferay.portal.kernel.dao.orm.QueryUtil;
38  import com.liferay.portal.kernel.dao.orm.SQLQuery;
39  import com.liferay.portal.kernel.dao.orm.Session;
40  import com.liferay.portal.kernel.dao.orm.Type;
41  import com.liferay.portal.kernel.log.Log;
42  import com.liferay.portal.kernel.log.LogFactoryUtil;
43  import com.liferay.portal.kernel.util.GetterUtil;
44  import com.liferay.portal.kernel.util.OrderByComparator;
45  import com.liferay.portal.kernel.util.StringPool;
46  import com.liferay.portal.kernel.util.StringUtil;
47  import com.liferay.portal.model.ModelListener;
48  import com.liferay.portal.model.Role;
49  import com.liferay.portal.model.impl.RoleImpl;
50  import com.liferay.portal.model.impl.RoleModelImpl;
51  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
52  
53  import java.sql.Types;
54  
55  import java.util.ArrayList;
56  import java.util.Collections;
57  import java.util.Iterator;
58  import java.util.List;
59  
60  /**
61   * <a href="RolePersistenceImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class RolePersistenceImpl extends BasePersistenceImpl
67      implements RolePersistence {
68      public Role create(long roleId) {
69          Role role = new RoleImpl();
70  
71          role.setNew(true);
72          role.setPrimaryKey(roleId);
73  
74          return role;
75      }
76  
77      public Role remove(long roleId) throws NoSuchRoleException, SystemException {
78          Session session = null;
79  
80          try {
81              session = openSession();
82  
83              Role role = (Role)session.get(RoleImpl.class, new Long(roleId));
84  
85              if (role == null) {
86                  if (_log.isWarnEnabled()) {
87                      _log.warn("No Role exists with the primary key " + roleId);
88                  }
89  
90                  throw new NoSuchRoleException(
91                      "No Role exists with the primary key " + roleId);
92              }
93  
94              return remove(role);
95          }
96          catch (NoSuchRoleException nsee) {
97              throw nsee;
98          }
99          catch (Exception e) {
100             throw processException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public Role remove(Role role) throws SystemException {
108         for (ModelListener listener : listeners) {
109             listener.onBeforeRemove(role);
110         }
111 
112         role = removeImpl(role);
113 
114         for (ModelListener listener : listeners) {
115             listener.onAfterRemove(role);
116         }
117 
118         return role;
119     }
120 
121     protected Role removeImpl(Role role) throws SystemException {
122         try {
123             clearGroups.clear(role.getPrimaryKey());
124         }
125         catch (Exception e) {
126             throw processException(e);
127         }
128         finally {
129             FinderCacheUtil.clearCache("Groups_Roles");
130         }
131 
132         try {
133             clearPermissions.clear(role.getPrimaryKey());
134         }
135         catch (Exception e) {
136             throw processException(e);
137         }
138         finally {
139             FinderCacheUtil.clearCache("Roles_Permissions");
140         }
141 
142         try {
143             clearUsers.clear(role.getPrimaryKey());
144         }
145         catch (Exception e) {
146             throw processException(e);
147         }
148         finally {
149             FinderCacheUtil.clearCache("Users_Roles");
150         }
151 
152         Session session = null;
153 
154         try {
155             session = openSession();
156 
157             if (BatchSessionUtil.isEnabled()) {
158                 Object staleObject = session.get(RoleImpl.class,
159                         role.getPrimaryKeyObj());
160 
161                 if (staleObject != null) {
162                     session.evict(staleObject);
163                 }
164             }
165 
166             session.delete(role);
167 
168             session.flush();
169 
170             return role;
171         }
172         catch (Exception e) {
173             throw processException(e);
174         }
175         finally {
176             closeSession(session);
177 
178             FinderCacheUtil.clearCache(Role.class.getName());
179         }
180     }
181 
182     /**
183      * @deprecated Use <code>update(Role role, boolean merge)</code>.
184      */
185     public Role update(Role role) throws SystemException {
186         if (_log.isWarnEnabled()) {
187             _log.warn(
188                 "Using the deprecated update(Role role) method. Use update(Role role, boolean merge) instead.");
189         }
190 
191         return update(role, false);
192     }
193 
194     /**
195      * Add, update, or merge, the entity. This method also calls the model
196      * listeners to trigger the proper events associated with adding, deleting,
197      * or updating an entity.
198      *
199      * @param        role the entity to add, update, or merge
200      * @param        merge boolean value for whether to merge the entity. The
201      *                default value is false. Setting merge to true is more
202      *                expensive and should only be true when role is
203      *                transient. See LEP-5473 for a detailed discussion of this
204      *                method.
205      * @return        true if the portlet can be displayed via Ajax
206      */
207     public Role update(Role role, boolean merge) throws SystemException {
208         boolean isNew = role.isNew();
209 
210         for (ModelListener listener : listeners) {
211             if (isNew) {
212                 listener.onBeforeCreate(role);
213             }
214             else {
215                 listener.onBeforeUpdate(role);
216             }
217         }
218 
219         role = updateImpl(role, merge);
220 
221         for (ModelListener listener : listeners) {
222             if (isNew) {
223                 listener.onAfterCreate(role);
224             }
225             else {
226                 listener.onAfterUpdate(role);
227             }
228         }
229 
230         return role;
231     }
232 
233     public Role updateImpl(com.liferay.portal.model.Role role, boolean merge)
234         throws SystemException {
235         FinderCacheUtil.clearCache("Groups_Roles");
236         FinderCacheUtil.clearCache("Roles_Permissions");
237         FinderCacheUtil.clearCache("Users_Roles");
238 
239         Session session = null;
240 
241         try {
242             session = openSession();
243 
244             BatchSessionUtil.update(session, role, merge);
245 
246             role.setNew(false);
247 
248             return role;
249         }
250         catch (Exception e) {
251             throw processException(e);
252         }
253         finally {
254             closeSession(session);
255 
256             FinderCacheUtil.clearCache(Role.class.getName());
257         }
258     }
259 
260     public Role findByPrimaryKey(long roleId)
261         throws NoSuchRoleException, SystemException {
262         Role role = fetchByPrimaryKey(roleId);
263 
264         if (role == null) {
265             if (_log.isWarnEnabled()) {
266                 _log.warn("No Role exists with the primary key " + roleId);
267             }
268 
269             throw new NoSuchRoleException(
270                 "No Role exists with the primary key " + roleId);
271         }
272 
273         return role;
274     }
275 
276     public Role fetchByPrimaryKey(long roleId) throws SystemException {
277         Session session = null;
278 
279         try {
280             session = openSession();
281 
282             return (Role)session.get(RoleImpl.class, new Long(roleId));
283         }
284         catch (Exception e) {
285             throw processException(e);
286         }
287         finally {
288             closeSession(session);
289         }
290     }
291 
292     public List<Role> findByCompanyId(long companyId) throws SystemException {
293         boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
294         String finderClassName = Role.class.getName();
295         String finderMethodName = "findByCompanyId";
296         String[] finderParams = new String[] { Long.class.getName() };
297         Object[] finderArgs = new Object[] { new Long(companyId) };
298 
299         Object result = null;
300 
301         if (finderClassNameCacheEnabled) {
302             result = FinderCacheUtil.getResult(finderClassName,
303                     finderMethodName, finderParams, finderArgs, this);
304         }
305 
306         if (result == null) {
307             Session session = null;
308 
309             try {
310                 session = openSession();
311 
312                 StringBuilder query = new StringBuilder();
313 
314                 query.append("FROM com.liferay.portal.model.Role WHERE ");
315 
316                 query.append("companyId = ?");
317 
318                 query.append(" ");
319 
320                 query.append("ORDER BY ");
321 
322                 query.append("name ASC");
323 
324                 Query q = session.createQuery(query.toString());
325 
326                 QueryPos qPos = QueryPos.getInstance(q);
327 
328                 qPos.add(companyId);
329 
330                 List<Role> list = q.list();
331 
332                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
333                     finderClassName, finderMethodName, finderParams,
334                     finderArgs, list);
335 
336                 return list;
337             }
338             catch (Exception e) {
339                 throw processException(e);
340             }
341             finally {
342                 closeSession(session);
343             }
344         }
345         else {
346             return (List<Role>)result;
347         }
348     }
349 
350     public List<Role> findByCompanyId(long companyId, int start, int end)
351         throws SystemException {
352         return findByCompanyId(companyId, start, end, null);
353     }
354 
355     public List<Role> findByCompanyId(long companyId, int start, int end,
356         OrderByComparator obc) throws SystemException {
357         boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
358         String finderClassName = Role.class.getName();
359         String finderMethodName = "findByCompanyId";
360         String[] finderParams = new String[] {
361                 Long.class.getName(),
362                 
363                 "java.lang.Integer", "java.lang.Integer",
364                 "com.liferay.portal.kernel.util.OrderByComparator"
365             };
366         Object[] finderArgs = new Object[] {
367                 new Long(companyId),
368                 
369                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
370             };
371 
372         Object result = null;
373 
374         if (finderClassNameCacheEnabled) {
375             result = FinderCacheUtil.getResult(finderClassName,
376                     finderMethodName, finderParams, finderArgs, this);
377         }
378 
379         if (result == null) {
380             Session session = null;
381 
382             try {
383                 session = openSession();
384 
385                 StringBuilder query = new StringBuilder();
386 
387                 query.append("FROM com.liferay.portal.model.Role WHERE ");
388 
389                 query.append("companyId = ?");
390 
391                 query.append(" ");
392 
393                 if (obc != null) {
394                     query.append("ORDER BY ");
395                     query.append(obc.getOrderBy());
396                 }
397 
398                 else {
399                     query.append("ORDER BY ");
400 
401                     query.append("name ASC");
402                 }
403 
404                 Query q = session.createQuery(query.toString());
405 
406                 QueryPos qPos = QueryPos.getInstance(q);
407 
408                 qPos.add(companyId);
409 
410                 List<Role> list = (List<Role>)QueryUtil.list(q, getDialect(),
411                         start, end);
412 
413                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
414                     finderClassName, finderMethodName, finderParams,
415                     finderArgs, list);
416 
417                 return list;
418             }
419             catch (Exception e) {
420                 throw processException(e);
421             }
422             finally {
423                 closeSession(session);
424             }
425         }
426         else {
427             return (List<Role>)result;
428         }
429     }
430 
431     public Role findByCompanyId_First(long companyId, OrderByComparator obc)
432         throws NoSuchRoleException, SystemException {
433         List<Role> list = findByCompanyId(companyId, 0, 1, obc);
434 
435         if (list.size() == 0) {
436             StringBuilder msg = new StringBuilder();
437 
438             msg.append("No Role exists with the key {");
439 
440             msg.append("companyId=" + companyId);
441 
442             msg.append(StringPool.CLOSE_CURLY_BRACE);
443 
444             throw new NoSuchRoleException(msg.toString());
445         }
446         else {
447             return list.get(0);
448         }
449     }
450 
451     public Role findByCompanyId_Last(long companyId, OrderByComparator obc)
452         throws NoSuchRoleException, SystemException {
453         int count = countByCompanyId(companyId);
454 
455         List<Role> list = findByCompanyId(companyId, count - 1, count, obc);
456 
457         if (list.size() == 0) {
458             StringBuilder msg = new StringBuilder();
459 
460             msg.append("No Role exists with the key {");
461 
462             msg.append("companyId=" + companyId);
463 
464             msg.append(StringPool.CLOSE_CURLY_BRACE);
465 
466             throw new NoSuchRoleException(msg.toString());
467         }
468         else {
469             return list.get(0);
470         }
471     }
472 
473     public Role[] findByCompanyId_PrevAndNext(long roleId, long companyId,
474         OrderByComparator obc) throws NoSuchRoleException, SystemException {
475         Role role = findByPrimaryKey(roleId);
476 
477         int count = countByCompanyId(companyId);
478 
479         Session session = null;
480 
481         try {
482             session = openSession();
483 
484             StringBuilder query = new StringBuilder();
485 
486             query.append("FROM com.liferay.portal.model.Role WHERE ");
487 
488             query.append("companyId = ?");
489 
490             query.append(" ");
491 
492             if (obc != null) {
493                 query.append("ORDER BY ");
494                 query.append(obc.getOrderBy());
495             }
496 
497             else {
498                 query.append("ORDER BY ");
499 
500                 query.append("name ASC");
501             }
502 
503             Query q = session.createQuery(query.toString());
504 
505             QueryPos qPos = QueryPos.getInstance(q);
506 
507             qPos.add(companyId);
508 
509             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, role);
510 
511             Role[] array = new RoleImpl[3];
512 
513             array[0] = (Role)objArray[0];
514             array[1] = (Role)objArray[1];
515             array[2] = (Role)objArray[2];
516 
517             return array;
518         }
519         catch (Exception e) {
520             throw processException(e);
521         }
522         finally {
523             closeSession(session);
524         }
525     }
526 
527     public Role findByC_N(long companyId, String name)
528         throws NoSuchRoleException, SystemException {
529         Role role = fetchByC_N(companyId, name);
530 
531         if (role == null) {
532             StringBuilder msg = new StringBuilder();
533 
534             msg.append("No Role exists with the key {");
535 
536             msg.append("companyId=" + companyId);
537 
538             msg.append(", ");
539             msg.append("name=" + name);
540 
541             msg.append(StringPool.CLOSE_CURLY_BRACE);
542 
543             if (_log.isWarnEnabled()) {
544                 _log.warn(msg.toString());
545             }
546 
547             throw new NoSuchRoleException(msg.toString());
548         }
549 
550         return role;
551     }
552 
553     public Role fetchByC_N(long companyId, String name)
554         throws SystemException {
555         boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
556         String finderClassName = Role.class.getName();
557         String finderMethodName = "fetchByC_N";
558         String[] finderParams = new String[] {
559                 Long.class.getName(), String.class.getName()
560             };
561         Object[] finderArgs = new Object[] { new Long(companyId), name };
562 
563         Object result = null;
564 
565         if (finderClassNameCacheEnabled) {
566             result = FinderCacheUtil.getResult(finderClassName,
567                     finderMethodName, finderParams, finderArgs, this);
568         }
569 
570         if (result == null) {
571             Session session = null;
572 
573             try {
574                 session = openSession();
575 
576                 StringBuilder query = new StringBuilder();
577 
578                 query.append("FROM com.liferay.portal.model.Role WHERE ");
579 
580                 query.append("companyId = ?");
581 
582                 query.append(" AND ");
583 
584                 if (name == null) {
585                     query.append("name IS NULL");
586                 }
587                 else {
588                     query.append("name = ?");
589                 }
590 
591                 query.append(" ");
592 
593                 query.append("ORDER BY ");
594 
595                 query.append("name ASC");
596 
597                 Query q = session.createQuery(query.toString());
598 
599                 QueryPos qPos = QueryPos.getInstance(q);
600 
601                 qPos.add(companyId);
602 
603                 if (name != null) {
604                     qPos.add(name);
605                 }
606 
607                 List<Role> list = q.list();
608 
609                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
610                     finderClassName, finderMethodName, finderParams,
611                     finderArgs, list);
612 
613                 if (list.size() == 0) {
614                     return null;
615                 }
616                 else {
617                     return list.get(0);
618                 }
619             }
620             catch (Exception e) {
621                 throw processException(e);
622             }
623             finally {
624                 closeSession(session);
625             }
626         }
627         else {
628             List<Role> list = (List<Role>)result;
629 
630             if (list.size() == 0) {
631                 return null;
632             }
633             else {
634                 return list.get(0);
635             }
636         }
637     }
638 
639     public Role findByC_C_C(long companyId, long classNameId, long classPK)
640         throws NoSuchRoleException, SystemException {
641         Role role = fetchByC_C_C(companyId, classNameId, classPK);
642 
643         if (role == null) {
644             StringBuilder msg = new StringBuilder();
645 
646             msg.append("No Role exists with the key {");
647 
648             msg.append("companyId=" + companyId);
649 
650             msg.append(", ");
651             msg.append("classNameId=" + classNameId);
652 
653             msg.append(", ");
654             msg.append("classPK=" + classPK);
655 
656             msg.append(StringPool.CLOSE_CURLY_BRACE);
657 
658             if (_log.isWarnEnabled()) {
659                 _log.warn(msg.toString());
660             }
661 
662             throw new NoSuchRoleException(msg.toString());
663         }
664 
665         return role;
666     }
667 
668     public Role fetchByC_C_C(long companyId, long classNameId, long classPK)
669         throws SystemException {
670         boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
671         String finderClassName = Role.class.getName();
672         String finderMethodName = "fetchByC_C_C";
673         String[] finderParams = new String[] {
674                 Long.class.getName(), Long.class.getName(), Long.class.getName()
675             };
676         Object[] finderArgs = new Object[] {
677                 new Long(companyId), new Long(classNameId), new Long(classPK)
678             };
679 
680         Object result = null;
681 
682         if (finderClassNameCacheEnabled) {
683             result = FinderCacheUtil.getResult(finderClassName,
684                     finderMethodName, finderParams, finderArgs, this);
685         }
686 
687         if (result == null) {
688             Session session = null;
689 
690             try {
691                 session = openSession();
692 
693                 StringBuilder query = new StringBuilder();
694 
695                 query.append("FROM com.liferay.portal.model.Role WHERE ");
696 
697                 query.append("companyId = ?");
698 
699                 query.append(" AND ");
700 
701                 query.append("classNameId = ?");
702 
703                 query.append(" AND ");
704 
705                 query.append("classPK = ?");
706 
707                 query.append(" ");
708 
709                 query.append("ORDER BY ");
710 
711                 query.append("name ASC");
712 
713                 Query q = session.createQuery(query.toString());
714 
715                 QueryPos qPos = QueryPos.getInstance(q);
716 
717                 qPos.add(companyId);
718 
719                 qPos.add(classNameId);
720 
721                 qPos.add(classPK);
722 
723                 List<Role> list = q.list();
724 
725                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
726                     finderClassName, finderMethodName, finderParams,
727                     finderArgs, list);
728 
729                 if (list.size() == 0) {
730                     return null;
731                 }
732                 else {
733                     return list.get(0);
734                 }
735             }
736             catch (Exception e) {
737                 throw processException(e);
738             }
739             finally {
740                 closeSession(session);
741             }
742         }
743         else {
744             List<Role> list = (List<Role>)result;
745 
746             if (list.size() == 0) {
747                 return null;
748             }
749             else {
750                 return list.get(0);
751             }
752         }
753     }
754 
755     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
756         throws SystemException {
757         Session session = null;
758 
759         try {
760             session = openSession();
761 
762             dynamicQuery.compile(session);
763 
764             return dynamicQuery.list();
765         }
766         catch (Exception e) {
767             throw processException(e);
768         }
769         finally {
770             closeSession(session);
771         }
772     }
773 
774     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
775         int start, int end) throws SystemException {
776         Session session = null;
777 
778         try {
779             session = openSession();
780 
781             dynamicQuery.setLimit(start, end);
782 
783             dynamicQuery.compile(session);
784 
785             return dynamicQuery.list();
786         }
787         catch (Exception e) {
788             throw processException(e);
789         }
790         finally {
791             closeSession(session);
792         }
793     }
794 
795     public List<Role> findAll() throws SystemException {
796         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
797     }
798 
799     public List<Role> findAll(int start, int end) throws SystemException {
800         return findAll(start, end, null);
801     }
802 
803     public List<Role> findAll(int start, int end, OrderByComparator obc)
804         throws SystemException {
805         boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
806         String finderClassName = Role.class.getName();
807         String finderMethodName = "findAll";
808         String[] finderParams = new String[] {
809                 "java.lang.Integer", "java.lang.Integer",
810                 "com.liferay.portal.kernel.util.OrderByComparator"
811             };
812         Object[] finderArgs = new Object[] {
813                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
814             };
815 
816         Object result = null;
817 
818         if (finderClassNameCacheEnabled) {
819             result = FinderCacheUtil.getResult(finderClassName,
820                     finderMethodName, finderParams, finderArgs, this);
821         }
822 
823         if (result == null) {
824             Session session = null;
825 
826             try {
827                 session = openSession();
828 
829                 StringBuilder query = new StringBuilder();
830 
831                 query.append("FROM com.liferay.portal.model.Role ");
832 
833                 if (obc != null) {
834                     query.append("ORDER BY ");
835                     query.append(obc.getOrderBy());
836                 }
837 
838                 else {
839                     query.append("ORDER BY ");
840 
841                     query.append("name ASC");
842                 }
843 
844                 Query q = session.createQuery(query.toString());
845 
846                 List<Role> list = null;
847 
848                 if (obc == null) {
849                     list = (List<Role>)QueryUtil.list(q, getDialect(), start,
850                             end, false);
851 
852                     Collections.sort(list);
853                 }
854                 else {
855                     list = (List<Role>)QueryUtil.list(q, getDialect(), start,
856                             end);
857                 }
858 
859                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
860                     finderClassName, finderMethodName, finderParams,
861                     finderArgs, list);
862 
863                 return list;
864             }
865             catch (Exception e) {
866                 throw processException(e);
867             }
868             finally {
869                 closeSession(session);
870             }
871         }
872         else {
873             return (List<Role>)result;
874         }
875     }
876 
877     public void removeByCompanyId(long companyId) throws SystemException {
878         for (Role role : findByCompanyId(companyId)) {
879             remove(role);
880         }
881     }
882 
883     public void removeByC_N(long companyId, String name)
884         throws NoSuchRoleException, SystemException {
885         Role role = findByC_N(companyId, name);
886 
887         remove(role);
888     }
889 
890     public void removeByC_C_C(long companyId, long classNameId, long classPK)
891         throws NoSuchRoleException, SystemException {
892         Role role = findByC_C_C(companyId, classNameId, classPK);
893 
894         remove(role);
895     }
896 
897     public void removeAll() throws SystemException {
898         for (Role role : findAll()) {
899             remove(role);
900         }
901     }
902 
903     public int countByCompanyId(long companyId) throws SystemException {
904         boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
905         String finderClassName = Role.class.getName();
906         String finderMethodName = "countByCompanyId";
907         String[] finderParams = new String[] { Long.class.getName() };
908         Object[] finderArgs = new Object[] { new Long(companyId) };
909 
910         Object result = null;
911 
912         if (finderClassNameCacheEnabled) {
913             result = FinderCacheUtil.getResult(finderClassName,
914                     finderMethodName, finderParams, finderArgs, this);
915         }
916 
917         if (result == null) {
918             Session session = null;
919 
920             try {
921                 session = openSession();
922 
923                 StringBuilder query = new StringBuilder();
924 
925                 query.append("SELECT COUNT(*) ");
926                 query.append("FROM com.liferay.portal.model.Role WHERE ");
927 
928                 query.append("companyId = ?");
929 
930                 query.append(" ");
931 
932                 Query q = session.createQuery(query.toString());
933 
934                 QueryPos qPos = QueryPos.getInstance(q);
935 
936                 qPos.add(companyId);
937 
938                 Long count = null;
939 
940                 Iterator<Long> itr = q.list().iterator();
941 
942                 if (itr.hasNext()) {
943                     count = itr.next();
944                 }
945 
946                 if (count == null) {
947                     count = new Long(0);
948                 }
949 
950                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
951                     finderClassName, finderMethodName, finderParams,
952                     finderArgs, count);
953 
954                 return count.intValue();
955             }
956             catch (Exception e) {
957                 throw processException(e);
958             }
959             finally {
960                 closeSession(session);
961             }
962         }
963         else {
964             return ((Long)result).intValue();
965         }
966     }
967 
968     public int countByC_N(long companyId, String name)
969         throws SystemException {
970         boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
971         String finderClassName = Role.class.getName();
972         String finderMethodName = "countByC_N";
973         String[] finderParams = new String[] {
974                 Long.class.getName(), String.class.getName()
975             };
976         Object[] finderArgs = new Object[] { new Long(companyId), name };
977 
978         Object result = null;
979 
980         if (finderClassNameCacheEnabled) {
981             result = FinderCacheUtil.getResult(finderClassName,
982                     finderMethodName, finderParams, finderArgs, this);
983         }
984 
985         if (result == null) {
986             Session session = null;
987 
988             try {
989                 session = openSession();
990 
991                 StringBuilder query = new StringBuilder();
992 
993                 query.append("SELECT COUNT(*) ");
994                 query.append("FROM com.liferay.portal.model.Role WHERE ");
995 
996                 query.append("companyId = ?");
997 
998                 query.append(" AND ");
999 
1000                if (name == null) {
1001                    query.append("name IS NULL");
1002                }
1003                else {
1004                    query.append("name = ?");
1005                }
1006
1007                query.append(" ");
1008
1009                Query q = session.createQuery(query.toString());
1010
1011                QueryPos qPos = QueryPos.getInstance(q);
1012
1013                qPos.add(companyId);
1014
1015                if (name != null) {
1016                    qPos.add(name);
1017                }
1018
1019                Long count = null;
1020
1021                Iterator<Long> itr = q.list().iterator();
1022
1023                if (itr.hasNext()) {
1024                    count = itr.next();
1025                }
1026
1027                if (count == null) {
1028                    count = new Long(0);
1029                }
1030
1031                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1032                    finderClassName, finderMethodName, finderParams,
1033                    finderArgs, count);
1034
1035                return count.intValue();
1036            }
1037            catch (Exception e) {
1038                throw processException(e);
1039            }
1040            finally {
1041                closeSession(session);
1042            }
1043        }
1044        else {
1045            return ((Long)result).intValue();
1046        }
1047    }
1048
1049    public int countByC_C_C(long companyId, long classNameId, long classPK)
1050        throws SystemException {
1051        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
1052        String finderClassName = Role.class.getName();
1053        String finderMethodName = "countByC_C_C";
1054        String[] finderParams = new String[] {
1055                Long.class.getName(), Long.class.getName(), Long.class.getName()
1056            };
1057        Object[] finderArgs = new Object[] {
1058                new Long(companyId), new Long(classNameId), new Long(classPK)
1059            };
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                StringBuilder query = new StringBuilder();
1075
1076                query.append("SELECT COUNT(*) ");
1077                query.append("FROM com.liferay.portal.model.Role WHERE ");
1078
1079                query.append("companyId = ?");
1080
1081                query.append(" AND ");
1082
1083                query.append("classNameId = ?");
1084
1085                query.append(" AND ");
1086
1087                query.append("classPK = ?");
1088
1089                query.append(" ");
1090
1091                Query q = session.createQuery(query.toString());
1092
1093                QueryPos qPos = QueryPos.getInstance(q);
1094
1095                qPos.add(companyId);
1096
1097                qPos.add(classNameId);
1098
1099                qPos.add(classPK);
1100
1101                Long count = null;
1102
1103                Iterator<Long> itr = q.list().iterator();
1104
1105                if (itr.hasNext()) {
1106                    count = itr.next();
1107                }
1108
1109                if (count == null) {
1110                    count = new Long(0);
1111                }
1112
1113                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1114                    finderClassName, finderMethodName, finderParams,
1115                    finderArgs, count);
1116
1117                return count.intValue();
1118            }
1119            catch (Exception e) {
1120                throw processException(e);
1121            }
1122            finally {
1123                closeSession(session);
1124            }
1125        }
1126        else {
1127            return ((Long)result).intValue();
1128        }
1129    }
1130
1131    public int countAll() throws SystemException {
1132        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
1133        String finderClassName = Role.class.getName();
1134        String finderMethodName = "countAll";
1135        String[] finderParams = new String[] {  };
1136        Object[] finderArgs = new Object[] {  };
1137
1138        Object result = null;
1139
1140        if (finderClassNameCacheEnabled) {
1141            result = FinderCacheUtil.getResult(finderClassName,
1142                    finderMethodName, finderParams, finderArgs, this);
1143        }
1144
1145        if (result == null) {
1146            Session session = null;
1147
1148            try {
1149                session = openSession();
1150
1151                Query q = session.createQuery(
1152                        "SELECT COUNT(*) FROM com.liferay.portal.model.Role");
1153
1154                Long count = null;
1155
1156                Iterator<Long> itr = q.list().iterator();
1157
1158                if (itr.hasNext()) {
1159                    count = itr.next();
1160                }
1161
1162                if (count == null) {
1163                    count = new Long(0);
1164                }
1165
1166                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1167                    finderClassName, finderMethodName, finderParams,
1168                    finderArgs, count);
1169
1170                return count.intValue();
1171            }
1172            catch (Exception e) {
1173                throw processException(e);
1174            }
1175            finally {
1176                closeSession(session);
1177            }
1178        }
1179        else {
1180            return ((Long)result).intValue();
1181        }
1182    }
1183
1184    public List<com.liferay.portal.model.Group> getGroups(long pk)
1185        throws SystemException {
1186        return getGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1187    }
1188
1189    public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
1190        int end) throws SystemException {
1191        return getGroups(pk, start, end, null);
1192    }
1193
1194    public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
1195        int end, OrderByComparator obc) throws SystemException {
1196        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_GROUPS_ROLES;
1197
1198        String finderClassName = "Groups_Roles";
1199
1200        String finderMethodName = "getGroups";
1201        String[] finderParams = new String[] {
1202                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1203                "com.liferay.portal.kernel.util.OrderByComparator"
1204            };
1205        Object[] finderArgs = new Object[] {
1206                new Long(pk), String.valueOf(start), String.valueOf(end),
1207                String.valueOf(obc)
1208            };
1209
1210        Object result = null;
1211
1212        if (finderClassNameCacheEnabled) {
1213            result = FinderCacheUtil.getResult(finderClassName,
1214                    finderMethodName, finderParams, finderArgs, this);
1215        }
1216
1217        if (result == null) {
1218            Session session = null;
1219
1220            try {
1221                session = openSession();
1222
1223                StringBuilder sb = new StringBuilder();
1224
1225                sb.append(_SQL_GETGROUPS);
1226
1227                if (obc != null) {
1228                    sb.append("ORDER BY ");
1229                    sb.append(obc.getOrderBy());
1230                }
1231
1232                else {
1233                    sb.append("ORDER BY ");
1234
1235                    sb.append("Group_.name ASC");
1236                }
1237
1238                String sql = sb.toString();
1239
1240                SQLQuery q = session.createSQLQuery(sql);
1241
1242                q.addEntity("Group_",
1243                    com.liferay.portal.model.impl.GroupImpl.class);
1244
1245                QueryPos qPos = QueryPos.getInstance(q);
1246
1247                qPos.add(pk);
1248
1249                List<com.liferay.portal.model.Group> list = (List<com.liferay.portal.model.Group>)QueryUtil.list(q,
1250                        getDialect(), start, end);
1251
1252                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1253                    finderClassName, finderMethodName, finderParams,
1254                    finderArgs, list);
1255
1256                return list;
1257            }
1258            catch (Exception e) {
1259                throw processException(e);
1260            }
1261            finally {
1262                closeSession(session);
1263            }
1264        }
1265        else {
1266            return (List<com.liferay.portal.model.Group>)result;
1267        }
1268    }
1269
1270    public int getGroupsSize(long pk) throws SystemException {
1271        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_GROUPS_ROLES;
1272
1273        String finderClassName = "Groups_Roles";
1274
1275        String finderMethodName = "getGroupsSize";
1276        String[] finderParams = new String[] { Long.class.getName() };
1277        Object[] finderArgs = new Object[] { new Long(pk) };
1278
1279        Object result = null;
1280
1281        if (finderClassNameCacheEnabled) {
1282            result = FinderCacheUtil.getResult(finderClassName,
1283                    finderMethodName, finderParams, finderArgs, this);
1284        }
1285
1286        if (result == null) {
1287            Session session = null;
1288
1289            try {
1290                session = openSession();
1291
1292                SQLQuery q = session.createSQLQuery(_SQL_GETGROUPSSIZE);
1293
1294                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1295
1296                QueryPos qPos = QueryPos.getInstance(q);
1297
1298                qPos.add(pk);
1299
1300                Long count = null;
1301
1302                Iterator<Long> itr = q.list().iterator();
1303
1304                if (itr.hasNext()) {
1305                    count = itr.next();
1306                }
1307
1308                if (count == null) {
1309                    count = new Long(0);
1310                }
1311
1312                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1313                    finderClassName, finderMethodName, finderParams,
1314                    finderArgs, count);
1315
1316                return count.intValue();
1317            }
1318            catch (Exception e) {
1319                throw processException(e);
1320            }
1321            finally {
1322                closeSession(session);
1323            }
1324        }
1325        else {
1326            return ((Long)result).intValue();
1327        }
1328    }
1329
1330    public boolean containsGroup(long pk, long groupPK)
1331        throws SystemException {
1332        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_GROUPS_ROLES;
1333
1334        String finderClassName = "Groups_Roles";
1335
1336        String finderMethodName = "containsGroups";
1337        String[] finderParams = new String[] {
1338                Long.class.getName(),
1339                
1340                Long.class.getName()
1341            };
1342        Object[] finderArgs = new Object[] { new Long(pk), new Long(groupPK) };
1343
1344        Object result = null;
1345
1346        if (finderClassNameCacheEnabled) {
1347            result = FinderCacheUtil.getResult(finderClassName,
1348                    finderMethodName, finderParams, finderArgs, this);
1349        }
1350
1351        if (result == null) {
1352            try {
1353                Boolean value = Boolean.valueOf(containsGroup.contains(pk,
1354                            groupPK));
1355
1356                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1357                    finderClassName, finderMethodName, finderParams,
1358                    finderArgs, value);
1359
1360                return value.booleanValue();
1361            }
1362            catch (Exception e) {
1363                throw processException(e);
1364            }
1365        }
1366        else {
1367            return ((Boolean)result).booleanValue();
1368        }
1369    }
1370
1371    public boolean containsGroups(long pk) throws SystemException {
1372        if (getGroupsSize(pk) > 0) {
1373            return true;
1374        }
1375        else {
1376            return false;
1377        }
1378    }
1379
1380    public void addGroup(long pk, long groupPK) throws SystemException {
1381        try {
1382            addGroup.add(pk, groupPK);
1383        }
1384        catch (Exception e) {
1385            throw processException(e);
1386        }
1387        finally {
1388            FinderCacheUtil.clearCache("Groups_Roles");
1389        }
1390    }
1391
1392    public void addGroup(long pk, com.liferay.portal.model.Group group)
1393        throws SystemException {
1394        try {
1395            addGroup.add(pk, group.getPrimaryKey());
1396        }
1397        catch (Exception e) {
1398            throw processException(e);
1399        }
1400        finally {
1401            FinderCacheUtil.clearCache("Groups_Roles");
1402        }
1403    }
1404
1405    public void addGroups(long pk, long[] groupPKs) throws SystemException {
1406        try {
1407            for (long groupPK : groupPKs) {
1408                addGroup.add(pk, groupPK);
1409            }
1410        }
1411        catch (Exception e) {
1412            throw processException(e);
1413        }
1414        finally {
1415            FinderCacheUtil.clearCache("Groups_Roles");
1416        }
1417    }
1418
1419    public void addGroups(long pk, List<com.liferay.portal.model.Group> groups)
1420        throws SystemException {
1421        try {
1422            for (com.liferay.portal.model.Group group : groups) {
1423                addGroup.add(pk, group.getPrimaryKey());
1424            }
1425        }
1426        catch (Exception e) {
1427            throw processException(e);
1428        }
1429        finally {
1430            FinderCacheUtil.clearCache("Groups_Roles");
1431        }
1432    }
1433
1434    public void clearGroups(long pk) throws SystemException {
1435        try {
1436            clearGroups.clear(pk);
1437        }
1438        catch (Exception e) {
1439            throw processException(e);
1440        }
1441        finally {
1442            FinderCacheUtil.clearCache("Groups_Roles");
1443        }
1444    }
1445
1446    public void removeGroup(long pk, long groupPK) throws SystemException {
1447        try {
1448            removeGroup.remove(pk, groupPK);
1449        }
1450        catch (Exception e) {
1451            throw processException(e);
1452        }
1453        finally {
1454            FinderCacheUtil.clearCache("Groups_Roles");
1455        }
1456    }
1457
1458    public void removeGroup(long pk, com.liferay.portal.model.Group group)
1459        throws SystemException {
1460        try {
1461            removeGroup.remove(pk, group.getPrimaryKey());
1462        }
1463        catch (Exception e) {
1464            throw processException(e);
1465        }
1466        finally {
1467            FinderCacheUtil.clearCache("Groups_Roles");
1468        }
1469    }
1470
1471    public void removeGroups(long pk, long[] groupPKs)
1472        throws SystemException {
1473        try {
1474            for (long groupPK : groupPKs) {
1475                removeGroup.remove(pk, groupPK);
1476            }
1477        }
1478        catch (Exception e) {
1479            throw processException(e);
1480        }
1481        finally {
1482            FinderCacheUtil.clearCache("Groups_Roles");
1483        }
1484    }
1485
1486    public void removeGroups(long pk,
1487        List<com.liferay.portal.model.Group> groups) throws SystemException {
1488        try {
1489            for (com.liferay.portal.model.Group group : groups) {
1490                removeGroup.remove(pk, group.getPrimaryKey());
1491            }
1492        }
1493        catch (Exception e) {
1494            throw processException(e);
1495        }
1496        finally {
1497            FinderCacheUtil.clearCache("Groups_Roles");
1498        }
1499    }
1500
1501    public void setGroups(long pk, long[] groupPKs) throws SystemException {
1502        try {
1503            clearGroups.clear(pk);
1504
1505            for (long groupPK : groupPKs) {
1506                addGroup.add(pk, groupPK);
1507            }
1508        }
1509        catch (Exception e) {
1510            throw processException(e);
1511        }
1512        finally {
1513            FinderCacheUtil.clearCache("Groups_Roles");
1514        }
1515    }
1516
1517    public void setGroups(long pk, List<com.liferay.portal.model.Group> groups)
1518        throws SystemException {
1519        try {
1520            clearGroups.clear(pk);
1521
1522            for (com.liferay.portal.model.Group group : groups) {
1523                addGroup.add(pk, group.getPrimaryKey());
1524            }
1525        }
1526        catch (Exception e) {
1527            throw processException(e);
1528        }
1529        finally {
1530            FinderCacheUtil.clearCache("Groups_Roles");
1531        }
1532    }
1533
1534    public List<com.liferay.portal.model.Permission> getPermissions(long pk)
1535        throws SystemException {
1536        return getPermissions(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1537    }
1538
1539    public List<com.liferay.portal.model.Permission> getPermissions(long pk,
1540        int start, int end) throws SystemException {
1541        return getPermissions(pk, start, end, null);
1542    }
1543
1544    public List<com.liferay.portal.model.Permission> getPermissions(long pk,
1545        int start, int end, OrderByComparator obc) throws SystemException {
1546        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_ROLES_PERMISSIONS;
1547
1548        String finderClassName = "Roles_Permissions";
1549
1550        String finderMethodName = "getPermissions";
1551        String[] finderParams = new String[] {
1552                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1553                "com.liferay.portal.kernel.util.OrderByComparator"
1554            };
1555        Object[] finderArgs = new Object[] {
1556                new Long(pk), String.valueOf(start), String.valueOf(end),
1557                String.valueOf(obc)
1558            };
1559
1560        Object result = null;
1561
1562        if (finderClassNameCacheEnabled) {
1563            result = FinderCacheUtil.getResult(finderClassName,
1564                    finderMethodName, finderParams, finderArgs, this);
1565        }
1566
1567        if (result == null) {
1568            Session session = null;
1569
1570            try {
1571                session = openSession();
1572
1573                StringBuilder sb = new StringBuilder();
1574
1575                sb.append(_SQL_GETPERMISSIONS);
1576
1577                if (obc != null) {
1578                    sb.append("ORDER BY ");
1579                    sb.append(obc.getOrderBy());
1580                }
1581
1582                String sql = sb.toString();
1583
1584                SQLQuery q = session.createSQLQuery(sql);
1585
1586                q.addEntity("Permission_",
1587                    com.liferay.portal.model.impl.PermissionImpl.class);
1588
1589                QueryPos qPos = QueryPos.getInstance(q);
1590
1591                qPos.add(pk);
1592
1593                List<com.liferay.portal.model.Permission> list = (List<com.liferay.portal.model.Permission>)QueryUtil.list(q,
1594                        getDialect(), start, end);
1595
1596                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1597                    finderClassName, finderMethodName, finderParams,
1598                    finderArgs, list);
1599
1600                return list;
1601            }
1602            catch (Exception e) {
1603                throw processException(e);
1604            }
1605            finally {
1606                closeSession(session);
1607            }
1608        }
1609        else {
1610            return (List<com.liferay.portal.model.Permission>)result;
1611        }
1612    }
1613
1614    public int getPermissionsSize(long pk) throws SystemException {
1615        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_ROLES_PERMISSIONS;
1616
1617        String finderClassName = "Roles_Permissions";
1618
1619        String finderMethodName = "getPermissionsSize";
1620        String[] finderParams = new String[] { Long.class.getName() };
1621        Object[] finderArgs = new Object[] { new Long(pk) };
1622
1623        Object result = null;
1624
1625        if (finderClassNameCacheEnabled) {
1626            result = FinderCacheUtil.getResult(finderClassName,
1627                    finderMethodName, finderParams, finderArgs, this);
1628        }
1629
1630        if (result == null) {
1631            Session session = null;
1632
1633            try {
1634                session = openSession();
1635
1636                SQLQuery q = session.createSQLQuery(_SQL_GETPERMISSIONSSIZE);
1637
1638                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1639
1640                QueryPos qPos = QueryPos.getInstance(q);
1641
1642                qPos.add(pk);
1643
1644                Long count = null;
1645
1646                Iterator<Long> itr = q.list().iterator();
1647
1648                if (itr.hasNext()) {
1649                    count = itr.next();
1650                }
1651
1652                if (count == null) {
1653                    count = new Long(0);
1654                }
1655
1656                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1657                    finderClassName, finderMethodName, finderParams,
1658                    finderArgs, count);
1659
1660                return count.intValue();
1661            }
1662            catch (Exception e) {
1663                throw processException(e);
1664            }
1665            finally {
1666                closeSession(session);
1667            }
1668        }
1669        else {
1670            return ((Long)result).intValue();
1671        }
1672    }
1673
1674    public boolean containsPermission(long pk, long permissionPK)
1675        throws SystemException {
1676        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_ROLES_PERMISSIONS;
1677
1678        String finderClassName = "Roles_Permissions";
1679
1680        String finderMethodName = "containsPermissions";
1681        String[] finderParams = new String[] {
1682                Long.class.getName(),
1683                
1684                Long.class.getName()
1685            };
1686        Object[] finderArgs = new Object[] { new Long(pk), new Long(permissionPK) };
1687
1688        Object result = null;
1689
1690        if (finderClassNameCacheEnabled) {
1691            result = FinderCacheUtil.getResult(finderClassName,
1692                    finderMethodName, finderParams, finderArgs, this);
1693        }
1694
1695        if (result == null) {
1696            try {
1697                Boolean value = Boolean.valueOf(containsPermission.contains(
1698                            pk, permissionPK));
1699
1700                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1701                    finderClassName, finderMethodName, finderParams,
1702                    finderArgs, value);
1703
1704                return value.booleanValue();
1705            }
1706            catch (Exception e) {
1707                throw processException(e);
1708            }
1709        }
1710        else {
1711            return ((Boolean)result).booleanValue();
1712        }
1713    }
1714
1715    public boolean containsPermissions(long pk) throws SystemException {
1716        if (getPermissionsSize(pk) > 0) {
1717            return true;
1718        }
1719        else {
1720            return false;
1721        }
1722    }
1723
1724    public void addPermission(long pk, long permissionPK)
1725        throws SystemException {
1726        try {
1727            addPermission.add(pk, permissionPK);
1728        }
1729        catch (Exception e) {
1730            throw processException(e);
1731        }
1732        finally {
1733            FinderCacheUtil.clearCache("Roles_Permissions");
1734        }
1735    }
1736
1737    public void addPermission(long pk,
1738        com.liferay.portal.model.Permission permission)
1739        throws SystemException {
1740        try {
1741            addPermission.add(pk, permission.getPrimaryKey());
1742        }
1743        catch (Exception e) {
1744            throw processException(e);
1745        }
1746        finally {
1747            FinderCacheUtil.clearCache("Roles_Permissions");
1748        }
1749    }
1750
1751    public void addPermissions(long pk, long[] permissionPKs)
1752        throws SystemException {
1753        try {
1754            for (long permissionPK : permissionPKs) {
1755                addPermission.add(pk, permissionPK);
1756            }
1757        }
1758        catch (Exception e) {
1759            throw processException(e);
1760        }
1761        finally {
1762            FinderCacheUtil.clearCache("Roles_Permissions");
1763        }
1764    }
1765
1766    public void addPermissions(long pk,
1767        List<com.liferay.portal.model.Permission> permissions)
1768        throws SystemException {
1769        try {
1770            for (com.liferay.portal.model.Permission permission : permissions) {
1771                addPermission.add(pk, permission.getPrimaryKey());
1772            }
1773        }
1774        catch (Exception e) {
1775            throw processException(e);
1776        }
1777        finally {
1778            FinderCacheUtil.clearCache("Roles_Permissions");
1779        }
1780    }
1781
1782    public void clearPermissions(long pk) throws SystemException {
1783        try {
1784            clearPermissions.clear(pk);
1785        }
1786        catch (Exception e) {
1787            throw processException(e);
1788        }
1789        finally {
1790            FinderCacheUtil.clearCache("Roles_Permissions");
1791        }
1792    }
1793
1794    public void removePermission(long pk, long permissionPK)
1795        throws SystemException {
1796        try {
1797            removePermission.remove(pk, permissionPK);
1798        }
1799        catch (Exception e) {
1800            throw processException(e);
1801        }
1802        finally {
1803            FinderCacheUtil.clearCache("Roles_Permissions");
1804        }
1805    }
1806
1807    public void removePermission(long pk,
1808        com.liferay.portal.model.Permission permission)
1809        throws SystemException {
1810        try {
1811            removePermission.remove(pk, permission.getPrimaryKey());
1812        }
1813        catch (Exception e) {
1814            throw processException(e);
1815        }
1816        finally {
1817            FinderCacheUtil.clearCache("Roles_Permissions");
1818        }
1819    }
1820
1821    public void removePermissions(long pk, long[] permissionPKs)
1822        throws SystemException {
1823        try {
1824            for (long permissionPK : permissionPKs) {
1825                removePermission.remove(pk, permissionPK);
1826            }
1827        }
1828        catch (Exception e) {
1829            throw processException(e);
1830        }
1831        finally {
1832            FinderCacheUtil.clearCache("Roles_Permissions");
1833        }
1834    }
1835
1836    public void removePermissions(long pk,
1837        List<com.liferay.portal.model.Permission> permissions)
1838        throws SystemException {
1839        try {
1840            for (com.liferay.portal.model.Permission permission : permissions) {
1841                removePermission.remove(pk, permission.getPrimaryKey());
1842            }
1843        }
1844        catch (Exception e) {
1845            throw processException(e);
1846        }
1847        finally {
1848            FinderCacheUtil.clearCache("Roles_Permissions");
1849        }
1850    }
1851
1852    public void setPermissions(long pk, long[] permissionPKs)
1853        throws SystemException {
1854        try {
1855            clearPermissions.clear(pk);
1856
1857            for (long permissionPK : permissionPKs) {
1858                addPermission.add(pk, permissionPK);
1859            }
1860        }
1861        catch (Exception e) {
1862            throw processException(e);
1863        }
1864        finally {
1865            FinderCacheUtil.clearCache("Roles_Permissions");
1866        }
1867    }
1868
1869    public void setPermissions(long pk,
1870        List<com.liferay.portal.model.Permission> permissions)
1871        throws SystemException {
1872        try {
1873            clearPermissions.clear(pk);
1874
1875            for (com.liferay.portal.model.Permission permission : permissions) {
1876                addPermission.add(pk, permission.getPrimaryKey());
1877            }
1878        }
1879        catch (Exception e) {
1880            throw processException(e);
1881        }
1882        finally {
1883            FinderCacheUtil.clearCache("Roles_Permissions");
1884        }
1885    }
1886
1887    public List<com.liferay.portal.model.User> getUsers(long pk)
1888        throws SystemException {
1889        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1890    }
1891
1892    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1893        int end) throws SystemException {
1894        return getUsers(pk, start, end, null);
1895    }
1896
1897    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1898        int end, OrderByComparator obc) throws SystemException {
1899        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_USERS_ROLES;
1900
1901        String finderClassName = "Users_Roles";
1902
1903        String finderMethodName = "getUsers";
1904        String[] finderParams = new String[] {
1905                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1906                "com.liferay.portal.kernel.util.OrderByComparator"
1907            };
1908        Object[] finderArgs = new Object[] {
1909                new Long(pk), String.valueOf(start), String.valueOf(end),
1910                String.valueOf(obc)
1911            };
1912
1913        Object result = null;
1914
1915        if (finderClassNameCacheEnabled) {
1916            result = FinderCacheUtil.getResult(finderClassName,
1917                    finderMethodName, finderParams, finderArgs, this);
1918        }
1919
1920        if (result == null) {
1921            Session session = null;
1922
1923            try {
1924                session = openSession();
1925
1926                StringBuilder sb = new StringBuilder();
1927
1928                sb.append(_SQL_GETUSERS);
1929
1930                if (obc != null) {
1931                    sb.append("ORDER BY ");
1932                    sb.append(obc.getOrderBy());
1933                }
1934
1935                String sql = sb.toString();
1936
1937                SQLQuery q = session.createSQLQuery(sql);
1938
1939                q.addEntity("User_",
1940                    com.liferay.portal.model.impl.UserImpl.class);
1941
1942                QueryPos qPos = QueryPos.getInstance(q);
1943
1944                qPos.add(pk);
1945
1946                List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
1947                        getDialect(), start, end);
1948
1949                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1950                    finderClassName, finderMethodName, finderParams,
1951                    finderArgs, list);
1952
1953                return list;
1954            }
1955            catch (Exception e) {
1956                throw processException(e);
1957            }
1958            finally {
1959                closeSession(session);
1960            }
1961        }
1962        else {
1963            return (List<com.liferay.portal.model.User>)result;
1964        }
1965    }
1966
1967    public int getUsersSize(long pk) throws SystemException {
1968        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_USERS_ROLES;
1969
1970        String finderClassName = "Users_Roles";
1971
1972        String finderMethodName = "getUsersSize";
1973        String[] finderParams = new String[] { Long.class.getName() };
1974        Object[] finderArgs = new Object[] { new Long(pk) };
1975
1976        Object result = null;
1977
1978        if (finderClassNameCacheEnabled) {
1979            result = FinderCacheUtil.getResult(finderClassName,
1980                    finderMethodName, finderParams, finderArgs, this);
1981        }
1982
1983        if (result == null) {
1984            Session session = null;
1985
1986            try {
1987                session = openSession();
1988
1989                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
1990
1991                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1992
1993                QueryPos qPos = QueryPos.getInstance(q);
1994
1995                qPos.add(pk);
1996
1997                Long count = null;
1998
1999                Iterator<Long> itr = q.list().iterator();
2000
2001                if (itr.hasNext()) {
2002                    count = itr.next();
2003                }
2004
2005                if (count == null) {
2006                    count = new Long(0);
2007                }
2008
2009                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2010                    finderClassName, finderMethodName, finderParams,
2011                    finderArgs, count);
2012
2013                return count.intValue();
2014            }
2015            catch (Exception e) {
2016                throw processException(e);
2017            }
2018            finally {
2019                closeSession(session);
2020            }
2021        }
2022        else {
2023            return ((Long)result).intValue();
2024        }
2025    }
2026
2027    public boolean containsUser(long pk, long userPK) throws SystemException {
2028        boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED_USERS_ROLES;
2029
2030        String finderClassName = "Users_Roles";
2031
2032        String finderMethodName = "containsUsers";
2033        String[] finderParams = new String[] {
2034                Long.class.getName(),
2035                
2036                Long.class.getName()
2037            };
2038        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
2039
2040        Object result = null;
2041
2042        if (finderClassNameCacheEnabled) {
2043            result = FinderCacheUtil.getResult(finderClassName,
2044                    finderMethodName, finderParams, finderArgs, this);
2045        }
2046
2047        if (result == null) {
2048            try {
2049                Boolean value = Boolean.valueOf(containsUser.contains(pk, userPK));
2050
2051                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2052                    finderClassName, finderMethodName, finderParams,
2053                    finderArgs, value);
2054
2055                return value.booleanValue();
2056            }
2057            catch (Exception e) {
2058                throw processException(e);
2059            }
2060        }
2061        else {
2062            return ((Boolean)result).booleanValue();
2063        }
2064    }
2065
2066    public boolean containsUsers(long pk) throws SystemException {
2067        if (getUsersSize(pk) > 0) {
2068            return true;
2069        }
2070        else {
2071            return false;
2072        }
2073    }
2074
2075    public void addUser(long pk, long userPK) throws SystemException {
2076        try {
2077            addUser.add(pk, userPK);
2078        }
2079        catch (Exception e) {
2080            throw processException(e);
2081        }
2082        finally {
2083            FinderCacheUtil.clearCache("Users_Roles");
2084        }
2085    }
2086
2087    public void addUser(long pk, com.liferay.portal.model.User user)
2088        throws SystemException {
2089        try {
2090            addUser.add(pk, user.getPrimaryKey());
2091        }
2092        catch (Exception e) {
2093            throw processException(e);
2094        }
2095        finally {
2096            FinderCacheUtil.clearCache("Users_Roles");
2097        }
2098    }
2099
2100    public void addUsers(long pk, long[] userPKs) throws SystemException {
2101        try {
2102            for (long userPK : userPKs) {
2103                addUser.add(pk, userPK);
2104            }
2105        }
2106        catch (Exception e) {
2107            throw processException(e);
2108        }
2109        finally {
2110            FinderCacheUtil.clearCache("Users_Roles");
2111        }
2112    }
2113
2114    public void addUsers(long pk, List<com.liferay.portal.model.User> users)
2115        throws SystemException {
2116        try {
2117            for (com.liferay.portal.model.User user : users) {
2118                addUser.add(pk, user.getPrimaryKey());
2119            }
2120        }
2121        catch (Exception e) {
2122            throw processException(e);
2123        }
2124        finally {
2125            FinderCacheUtil.clearCache("Users_Roles");
2126        }
2127    }
2128
2129    public void clearUsers(long pk) throws SystemException {
2130        try {
2131            clearUsers.clear(pk);
2132        }
2133        catch (Exception e) {
2134            throw processException(e);
2135        }
2136        finally {
2137            FinderCacheUtil.clearCache("Users_Roles");
2138        }
2139    }
2140
2141    public void removeUser(long pk, long userPK) throws SystemException {
2142        try {
2143            removeUser.remove(pk, userPK);
2144        }
2145        catch (Exception e) {
2146            throw processException(e);
2147        }
2148        finally {
2149            FinderCacheUtil.clearCache("Users_Roles");
2150        }
2151    }
2152
2153    public void removeUser(long pk, com.liferay.portal.model.User user)
2154        throws SystemException {
2155        try {
2156            removeUser.remove(pk, user.getPrimaryKey());
2157        }
2158        catch (Exception e) {
2159            throw processException(e);
2160        }
2161        finally {
2162            FinderCacheUtil.clearCache("Users_Roles");
2163        }
2164    }
2165
2166    public void removeUsers(long pk, long[] userPKs) throws SystemException {
2167        try {
2168            for (long userPK : userPKs) {
2169                removeUser.remove(pk, userPK);
2170            }
2171        }
2172        catch (Exception e) {
2173            throw processException(e);
2174        }
2175        finally {
2176            FinderCacheUtil.clearCache("Users_Roles");
2177        }
2178    }
2179
2180    public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
2181        throws SystemException {
2182        try {
2183            for (com.liferay.portal.model.User user : users) {
2184                removeUser.remove(pk, user.getPrimaryKey());
2185            }
2186        }
2187        catch (Exception e) {
2188            throw processException(e);
2189        }
2190        finally {
2191            FinderCacheUtil.clearCache("Users_Roles");
2192        }
2193    }
2194
2195    public void setUsers(long pk, long[] userPKs) throws SystemException {
2196        try {
2197            clearUsers.clear(pk);
2198
2199            for (long userPK : userPKs) {
2200                addUser.add(pk, userPK);
2201            }
2202        }
2203        catch (Exception e) {
2204            throw processException(e);
2205        }
2206        finally {
2207            FinderCacheUtil.clearCache("Users_Roles");
2208        }
2209    }
2210
2211    public void setUsers(long pk, List<com.liferay.portal.model.User> users)
2212        throws SystemException {
2213        try {
2214            clearUsers.clear(pk);
2215
2216            for (com.liferay.portal.model.User user : users) {
2217                addUser.add(pk, user.getPrimaryKey());
2218            }
2219        }
2220        catch (Exception e) {
2221            throw processException(e);
2222        }
2223        finally {
2224            FinderCacheUtil.clearCache("Users_Roles");
2225        }
2226    }
2227
2228    public void afterPropertiesSet() {
2229        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2230                    com.liferay.portal.util.PropsUtil.get(
2231                        "value.object.listener.com.liferay.portal.model.Role")));
2232
2233        if (listenerClassNames.length > 0) {
2234            try {
2235                List<ModelListener> listenersList = new ArrayList<ModelListener>();
2236
2237                for (String listenerClassName : listenerClassNames) {
2238                    listenersList.add((ModelListener)Class.forName(
2239                            listenerClassName).newInstance());
2240                }
2241
2242                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2243            }
2244            catch (Exception e) {
2245                _log.error(e);
2246            }
2247        }
2248
2249        containsGroup = new ContainsGroup(this);
2250
2251        addGroup = new AddGroup(this);
2252        clearGroups = new ClearGroups(this);
2253        removeGroup = new RemoveGroup(this);
2254
2255        containsPermission = new ContainsPermission(this);
2256
2257        addPermission = new AddPermission(this);
2258        clearPermissions = new ClearPermissions(this);
2259        removePermission = new RemovePermission(this);
2260
2261        containsUser = new ContainsUser(this);
2262
2263        addUser = new AddUser(this);
2264        clearUsers = new ClearUsers(this);
2265        removeUser = new RemoveUser(this);
2266    }
2267
2268    @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
2269    protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
2270    @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
2271    protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
2272    @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
2273    protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
2274    @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
2275    protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
2276    @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
2277    protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
2278    @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
2279    protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
2280    @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
2281    protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
2282    @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
2283    protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
2284    @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
2285    protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
2286    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
2287    protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
2288    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
2289    protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
2290    @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
2291    protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
2292    @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
2293    protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
2294    @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
2295    protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
2296    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
2297    protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
2298    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
2299    protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
2300    @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
2301    protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
2302    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
2303    protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
2304    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
2305    protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
2306    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
2307    protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
2308    @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
2309    protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
2310    @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
2311    protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
2312    @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
2313    protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
2314    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
2315    protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
2316    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
2317    protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
2318    @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
2319    protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
2320    @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
2321    protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
2322    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
2323    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
2324    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
2325    protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
2326    @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
2327    protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
2328    @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
2329    protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
2330    @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
2331    protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
2332    @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
2333    protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
2334    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
2335    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
2336    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
2337    protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
2338    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
2339    protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
2340    @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
2341    protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
2342    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
2343    protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
2344    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
2345    protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
2346    @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
2347    protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
2348    @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
2349    protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
2350    protected ContainsGroup containsGroup;
2351    protected AddGroup addGroup;
2352    protected ClearGroups clearGroups;
2353    protected RemoveGroup removeGroup;
2354    protected ContainsPermission containsPermission;
2355    protected AddPermission addPermission;
2356    protected ClearPermissions clearPermissions;
2357    protected RemovePermission removePermission;
2358    protected ContainsUser containsUser;
2359    protected AddUser addUser;
2360    protected ClearUsers clearUsers;
2361    protected RemoveUser removeUser;
2362
2363    protected class ContainsGroup {
2364        protected ContainsGroup(RolePersistenceImpl persistenceImpl) {
2365            super();
2366
2367            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2368                    _SQL_CONTAINSGROUP,
2369                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2370        }
2371
2372        protected boolean contains(long roleId, long groupId) {
2373            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2374                        new Long(roleId), new Long(groupId)
2375                    });
2376
2377            if (results.size() > 0) {
2378                Integer count = results.get(0);
2379
2380                if (count.intValue() > 0) {
2381                    return true;
2382                }
2383            }
2384
2385            return false;
2386        }
2387
2388        private MappingSqlQuery _mappingSqlQuery;
2389    }
2390
2391    protected class AddGroup {
2392        protected AddGroup(RolePersistenceImpl persistenceImpl) {
2393            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2394                    "INSERT INTO Groups_Roles (roleId, groupId) VALUES (?, ?)",
2395                    new int[] { Types.BIGINT, Types.BIGINT });
2396            _persistenceImpl = persistenceImpl;
2397        }
2398
2399        protected void add(long roleId, long groupId) throws SystemException {
2400            if (!_persistenceImpl.containsGroup.contains(roleId, groupId)) {
2401                ModelListener[] groupListeners = groupPersistence.getListeners();
2402
2403                for (ModelListener listener : listeners) {
2404                    listener.onBeforeAddAssociation(roleId,
2405                        com.liferay.portal.model.Group.class.getName(), groupId);
2406                }
2407
2408                for (ModelListener listener : groupListeners) {
2409                    listener.onBeforeAddAssociation(groupId,
2410                        Role.class.getName(), roleId);
2411                }
2412
2413                _sqlUpdate.update(new Object[] {
2414                        new Long(roleId), new Long(groupId)
2415                    });
2416
2417                for (ModelListener listener : listeners) {
2418                    listener.onAfterAddAssociation(roleId,
2419                        com.liferay.portal.model.Group.class.getName(), groupId);
2420                }
2421
2422                for (ModelListener listener : groupListeners) {
2423                    listener.onAfterAddAssociation(groupId,
2424                        Role.class.getName(), roleId);
2425                }
2426            }
2427        }
2428
2429        private SqlUpdate _sqlUpdate;
2430        private RolePersistenceImpl _persistenceImpl;
2431    }
2432
2433    protected class ClearGroups {
2434        protected ClearGroups(RolePersistenceImpl persistenceImpl) {
2435            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2436                    "DELETE FROM Groups_Roles WHERE roleId = ?",
2437                    new int[] { Types.BIGINT });
2438        }
2439
2440        protected void clear(long roleId) throws SystemException {
2441            ModelListener[] groupListeners = groupPersistence.getListeners();
2442
2443            List<com.liferay.portal.model.Group> groups = null;
2444
2445            if ((listeners.length > 0) || (groupListeners.length > 0)) {
2446                groups = getGroups(roleId);
2447
2448                for (com.liferay.portal.model.Group group : groups) {
2449                    for (ModelListener listener : listeners) {
2450                        listener.onBeforeRemoveAssociation(roleId,
2451                            com.liferay.portal.model.Group.class.getName(),
2452                            group.getPrimaryKey());
2453                    }
2454
2455                    for (ModelListener listener : groupListeners) {
2456                        listener.onBeforeRemoveAssociation(group.getPrimaryKey(),
2457                            Role.class.getName(), roleId);
2458                    }
2459                }
2460            }
2461
2462            _sqlUpdate.update(new Object[] { new Long(roleId) });
2463
2464            if ((listeners.length > 0) || (groupListeners.length > 0)) {
2465                for (com.liferay.portal.model.Group group : groups) {
2466                    for (ModelListener listener : listeners) {
2467                        listener.onAfterRemoveAssociation(roleId,
2468                            com.liferay.portal.model.Group.class.getName(),
2469                            group.getPrimaryKey());
2470                    }
2471
2472                    for (ModelListener listener : groupListeners) {
2473                        listener.onBeforeRemoveAssociation(group.getPrimaryKey(),
2474                            Role.class.getName(), roleId);
2475                    }
2476                }
2477            }
2478        }
2479
2480        private SqlUpdate _sqlUpdate;
2481    }
2482
2483    protected class RemoveGroup {
2484        protected RemoveGroup(RolePersistenceImpl persistenceImpl) {
2485            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2486                    "DELETE FROM Groups_Roles WHERE roleId = ? AND groupId = ?",
2487                    new int[] { Types.BIGINT, Types.BIGINT });
2488            _persistenceImpl = persistenceImpl;
2489        }
2490
2491        protected void remove(long roleId, long groupId)
2492            throws SystemException {
2493            if (_persistenceImpl.containsGroup.contains(roleId, groupId)) {
2494                ModelListener[] groupListeners = groupPersistence.getListeners();
2495
2496                for (ModelListener listener : listeners) {
2497                    listener.onBeforeRemoveAssociation(roleId,
2498                        com.liferay.portal.model.Group.class.getName(), groupId);
2499                }
2500
2501                for (ModelListener listener : groupListeners) {
2502                    listener.onBeforeRemoveAssociation(groupId,
2503                        Role.class.getName(), roleId);
2504                }
2505
2506                _sqlUpdate.update(new Object[] {
2507                        new Long(roleId), new Long(groupId)
2508                    });
2509
2510                for (ModelListener listener : listeners) {
2511                    listener.onAfterRemoveAssociation(roleId,
2512                        com.liferay.portal.model.Group.class.getName(), groupId);
2513                }
2514
2515                for (ModelListener listener : groupListeners) {
2516                    listener.onAfterRemoveAssociation(groupId,
2517                        Role.class.getName(), roleId);
2518                }
2519            }
2520        }
2521
2522        private SqlUpdate _sqlUpdate;
2523        private RolePersistenceImpl _persistenceImpl;
2524    }
2525
2526    protected class ContainsPermission {
2527        protected ContainsPermission(RolePersistenceImpl persistenceImpl) {
2528            super();
2529
2530            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2531                    _SQL_CONTAINSPERMISSION,
2532                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2533        }
2534
2535        protected boolean contains(long roleId, long permissionId) {
2536            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2537                        new Long(roleId), new Long(permissionId)
2538                    });
2539
2540            if (results.size() > 0) {
2541                Integer count = results.get(0);
2542
2543                if (count.intValue() > 0) {
2544                    return true;
2545                }
2546            }
2547
2548            return false;
2549        }
2550
2551        private MappingSqlQuery _mappingSqlQuery;
2552    }
2553
2554    protected class AddPermission {
2555        protected AddPermission(RolePersistenceImpl persistenceImpl) {
2556            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2557                    "INSERT INTO Roles_Permissions (roleId, permissionId) VALUES (?, ?)",
2558                    new int[] { Types.BIGINT, Types.BIGINT });
2559            _persistenceImpl = persistenceImpl;
2560        }
2561
2562        protected void add(long roleId, long permissionId)
2563            throws SystemException {
2564            if (!_persistenceImpl.containsPermission.contains(roleId,
2565                        permissionId)) {
2566                ModelListener[] permissionListeners = permissionPersistence.getListeners();
2567
2568                for (ModelListener listener : listeners) {
2569                    listener.onBeforeAddAssociation(roleId,
2570                        com.liferay.portal.model.Permission.class.getName(),
2571                        permissionId);
2572                }
2573
2574                for (ModelListener listener : permissionListeners) {
2575                    listener.onBeforeAddAssociation(permissionId,
2576                        Role.class.getName(), roleId);
2577                }
2578
2579                _sqlUpdate.update(new Object[] {
2580                        new Long(roleId), new Long(permissionId)
2581                    });
2582
2583                for (ModelListener listener : listeners) {
2584                    listener.onAfterAddAssociation(roleId,
2585                        com.liferay.portal.model.Permission.class.getName(),
2586                        permissionId);
2587                }
2588
2589                for (ModelListener listener : permissionListeners) {
2590                    listener.onAfterAddAssociation(permissionId,
2591                        Role.class.getName(), roleId);
2592                }
2593            }
2594        }
2595
2596        private SqlUpdate _sqlUpdate;
2597        private RolePersistenceImpl _persistenceImpl;
2598    }
2599
2600    protected class ClearPermissions {
2601        protected ClearPermissions(RolePersistenceImpl persistenceImpl) {
2602            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2603                    "DELETE FROM Roles_Permissions WHERE roleId = ?",
2604                    new int[] { Types.BIGINT });
2605        }
2606
2607        protected void clear(long roleId) throws SystemException {
2608            ModelListener[] permissionListeners = permissionPersistence.getListeners();
2609
2610            List<com.liferay.portal.model.Permission> permissions = null;
2611
2612            if ((listeners.length > 0) || (permissionListeners.length > 0)) {
2613                permissions = getPermissions(roleId);
2614
2615                for (com.liferay.portal.model.Permission permission : permissions) {
2616                    for (ModelListener listener : listeners) {
2617                        listener.onBeforeRemoveAssociation(roleId,
2618                            com.liferay.portal.model.Permission.class.getName(),
2619                            permission.getPrimaryKey());
2620                    }
2621
2622                    for (ModelListener listener : permissionListeners) {
2623                        listener.onBeforeRemoveAssociation(permission.getPrimaryKey(),
2624                            Role.class.getName(), roleId);
2625                    }
2626                }
2627            }
2628
2629            _sqlUpdate.update(new Object[] { new Long(roleId) });
2630
2631            if ((listeners.length > 0) || (permissionListeners.length > 0)) {
2632                for (com.liferay.portal.model.Permission permission : permissions) {
2633                    for (ModelListener listener : listeners) {
2634                        listener.onAfterRemoveAssociation(roleId,
2635                            com.liferay.portal.model.Permission.class.getName(),
2636                            permission.getPrimaryKey());
2637                    }
2638
2639                    for (ModelListener listener : permissionListeners) {
2640                        listener.onBeforeRemoveAssociation(permission.getPrimaryKey(),
2641                            Role.class.getName(), roleId);
2642                    }
2643                }
2644            }
2645        }
2646
2647        private SqlUpdate _sqlUpdate;
2648    }
2649
2650    protected class RemovePermission {
2651        protected RemovePermission(RolePersistenceImpl persistenceImpl) {
2652            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2653                    "DELETE FROM Roles_Permissions WHERE roleId = ? AND permissionId = ?",
2654                    new int[] { Types.BIGINT, Types.BIGINT });
2655            _persistenceImpl = persistenceImpl;
2656        }
2657
2658        protected void remove(long roleId, long permissionId)
2659            throws SystemException {
2660            if (_persistenceImpl.containsPermission.contains(roleId,
2661                        permissionId)) {
2662                ModelListener[] permissionListeners = permissionPersistence.getListeners();
2663
2664                for (ModelListener listener : listeners) {
2665                    listener.onBeforeRemoveAssociation(roleId,
2666                        com.liferay.portal.model.Permission.class.getName(),
2667                        permissionId);
2668                }
2669
2670                for (ModelListener listener : permissionListeners) {
2671                    listener.onBeforeRemoveAssociation(permissionId,
2672                        Role.class.getName(), roleId);
2673                }
2674
2675                _sqlUpdate.update(new Object[] {
2676                        new Long(roleId), new Long(permissionId)
2677                    });
2678
2679                for (ModelListener listener : listeners) {
2680                    listener.onAfterRemoveAssociation(roleId,
2681                        com.liferay.portal.model.Permission.class.getName(),
2682                        permissionId);
2683                }
2684
2685                for (ModelListener listener : permissionListeners) {
2686                    listener.onAfterRemoveAssociation(permissionId,
2687                        Role.class.getName(), roleId);
2688                }
2689            }
2690        }
2691
2692        private SqlUpdate _sqlUpdate;
2693        private RolePersistenceImpl _persistenceImpl;
2694    }
2695
2696    protected class ContainsUser {
2697        protected ContainsUser(RolePersistenceImpl persistenceImpl) {
2698            super();
2699
2700            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2701                    _SQL_CONTAINSUSER,
2702                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2703        }
2704
2705        protected boolean contains(long roleId, long userId) {
2706            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2707                        new Long(roleId), new Long(userId)
2708                    });
2709
2710            if (results.size() > 0) {
2711                Integer count = results.get(0);
2712
2713                if (count.intValue() > 0) {
2714                    return true;
2715                }
2716            }
2717
2718            return false;
2719        }
2720
2721        private MappingSqlQuery _mappingSqlQuery;
2722    }
2723
2724    protected class AddUser {
2725        protected AddUser(RolePersistenceImpl persistenceImpl) {
2726            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2727                    "INSERT INTO Users_Roles (roleId, userId) VALUES (?, ?)",
2728                    new int[] { Types.BIGINT, Types.BIGINT });
2729            _persistenceImpl = persistenceImpl;
2730        }
2731
2732        protected void add(long roleId, long userId) throws SystemException {
2733            if (!_persistenceImpl.containsUser.contains(roleId, userId)) {
2734                ModelListener[] userListeners = userPersistence.getListeners();
2735
2736                for (ModelListener listener : listeners) {
2737                    listener.onBeforeAddAssociation(roleId,
2738                        com.liferay.portal.model.User.class.getName(), userId);
2739                }
2740
2741                for (ModelListener listener : userListeners) {
2742                    listener.onBeforeAddAssociation(userId,
2743                        Role.class.getName(), roleId);
2744                }
2745
2746                _sqlUpdate.update(new Object[] {
2747                        new Long(roleId), new Long(userId)
2748                    });
2749
2750                for (ModelListener listener : listeners) {
2751                    listener.onAfterAddAssociation(roleId,
2752                        com.liferay.portal.model.User.class.getName(), userId);
2753                }
2754
2755                for (ModelListener listener : userListeners) {
2756                    listener.onAfterAddAssociation(userId,
2757                        Role.class.getName(), roleId);
2758                }
2759            }
2760        }
2761
2762        private SqlUpdate _sqlUpdate;
2763        private RolePersistenceImpl _persistenceImpl;
2764    }
2765
2766    protected class ClearUsers {
2767        protected ClearUsers(RolePersistenceImpl persistenceImpl) {
2768            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2769                    "DELETE FROM Users_Roles WHERE roleId = ?",
2770                    new int[] { Types.BIGINT });
2771        }
2772
2773        protected void clear(long roleId) throws SystemException {
2774            ModelListener[] userListeners = userPersistence.getListeners();
2775
2776            List<com.liferay.portal.model.User> users = null;
2777
2778            if ((listeners.length > 0) || (userListeners.length > 0)) {
2779                users = getUsers(roleId);
2780
2781                for (com.liferay.portal.model.User user : users) {
2782                    for (ModelListener listener : listeners) {
2783                        listener.onBeforeRemoveAssociation(roleId,
2784                            com.liferay.portal.model.User.class.getName(),
2785                            user.getPrimaryKey());
2786                    }
2787
2788                    for (ModelListener listener : userListeners) {
2789                        listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
2790                            Role.class.getName(), roleId);
2791                    }
2792                }
2793            }
2794
2795            _sqlUpdate.update(new Object[] { new Long(roleId) });
2796
2797            if ((listeners.length > 0) || (userListeners.length > 0)) {
2798                for (com.liferay.portal.model.User user : users) {
2799                    for (ModelListener listener : listeners) {
2800                        listener.onAfterRemoveAssociation(roleId,
2801                            com.liferay.portal.model.User.class.getName(),
2802                            user.getPrimaryKey());
2803                    }
2804
2805                    for (ModelListener listener : userListeners) {
2806                        listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
2807                            Role.class.getName(), roleId);
2808                    }
2809                }
2810            }
2811        }
2812
2813        private SqlUpdate _sqlUpdate;
2814    }
2815
2816    protected class RemoveUser {
2817        protected RemoveUser(RolePersistenceImpl persistenceImpl) {
2818            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2819                    "DELETE FROM Users_Roles WHERE roleId = ? AND userId = ?",
2820                    new int[] { Types.BIGINT, Types.BIGINT });
2821            _persistenceImpl = persistenceImpl;
2822        }
2823
2824        protected void remove(long roleId, long userId)
2825            throws SystemException {
2826            if (_persistenceImpl.containsUser.contains(roleId, userId)) {
2827                ModelListener[] userListeners = userPersistence.getListeners();
2828
2829                for (ModelListener listener : listeners) {
2830                    listener.onBeforeRemoveAssociation(roleId,
2831                        com.liferay.portal.model.User.class.getName(), userId);
2832                }
2833
2834                for (ModelListener listener : userListeners) {
2835                    listener.onBeforeRemoveAssociation(userId,
2836                        Role.class.getName(), roleId);
2837                }
2838
2839                _sqlUpdate.update(new Object[] {
2840                        new Long(roleId), new Long(userId)
2841                    });
2842
2843                for (ModelListener listener : listeners) {
2844                    listener.onAfterRemoveAssociation(roleId,
2845                        com.liferay.portal.model.User.class.getName(), userId);
2846                }
2847
2848                for (ModelListener listener : userListeners) {
2849                    listener.onAfterRemoveAssociation(userId,
2850                        Role.class.getName(), roleId);
2851                }
2852            }
2853        }
2854
2855        private SqlUpdate _sqlUpdate;
2856        private RolePersistenceImpl _persistenceImpl;
2857    }
2858
2859    private static final String _SQL_GETGROUPS = "SELECT {Group_.*} FROM Group_ INNER JOIN Groups_Roles ON (Groups_Roles.groupId = Group_.groupId) WHERE (Groups_Roles.roleId = ?)";
2860    private static final String _SQL_GETGROUPSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Roles WHERE roleId = ?";
2861    private static final String _SQL_CONTAINSGROUP = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Roles WHERE roleId = ? AND groupId = ?";
2862    private static final String _SQL_GETPERMISSIONS = "SELECT {Permission_.*} FROM Permission_ INNER JOIN Roles_Permissions ON (Roles_Permissions.permissionId = Permission_.permissionId) WHERE (Roles_Permissions.roleId = ?)";
2863    private static final String _SQL_GETPERMISSIONSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Roles_Permissions WHERE roleId = ?";
2864    private static final String _SQL_CONTAINSPERMISSION = "SELECT COUNT(*) AS COUNT_VALUE FROM Roles_Permissions WHERE roleId = ? AND permissionId = ?";
2865    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_Roles ON (Users_Roles.userId = User_.userId) WHERE (Users_Roles.roleId = ?)";
2866    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Roles WHERE roleId = ?";
2867    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Roles WHERE roleId = ? AND userId = ?";
2868    private static Log _log = LogFactoryUtil.getLog(RolePersistenceImpl.class);
2869}