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