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