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