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