1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.NoSuchOrgGroupPermissionException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.annotation.BeanReference;
21  import com.liferay.portal.kernel.cache.CacheRegistry;
22  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
24  import com.liferay.portal.kernel.dao.orm.FinderPath;
25  import com.liferay.portal.kernel.dao.orm.Query;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.InstanceFactory;
33  import com.liferay.portal.kernel.util.OrderByComparator;
34  import com.liferay.portal.kernel.util.StringBundler;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.model.OrgGroupPermission;
39  import com.liferay.portal.model.impl.OrgGroupPermissionImpl;
40  import com.liferay.portal.model.impl.OrgGroupPermissionModelImpl;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import java.io.Serializable;
44  
45  import java.util.ArrayList;
46  import java.util.Collections;
47  import java.util.List;
48  
49  /**
50   * <a href="OrgGroupPermissionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * <p>
53   * ServiceBuilder generated this class. Modifications in this class will be
54   * overwritten the next time is generated.
55   * </p>
56   *
57   * @author    Brian Wing Shun Chan
58   * @see       OrgGroupPermissionPersistence
59   * @see       OrgGroupPermissionUtil
60   * @generated
61   */
62  public class OrgGroupPermissionPersistenceImpl extends BasePersistenceImpl<OrgGroupPermission>
63      implements OrgGroupPermissionPersistence {
64      public static final String FINDER_CLASS_NAME_ENTITY = OrgGroupPermissionImpl.class.getName();
65      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
66          ".List";
67      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
68              OrgGroupPermissionModelImpl.FINDER_CACHE_ENABLED,
69              FINDER_CLASS_NAME_LIST, "findByGroupId",
70              new String[] {
71                  Long.class.getName(),
72                  
73              "java.lang.Integer", "java.lang.Integer",
74                  "com.liferay.portal.kernel.util.OrderByComparator"
75              });
76      public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
77              OrgGroupPermissionModelImpl.FINDER_CACHE_ENABLED,
78              FINDER_CLASS_NAME_LIST, "countByGroupId",
79              new String[] { Long.class.getName() });
80      public static final FinderPath FINDER_PATH_FIND_BY_PERMISSIONID = new FinderPath(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
81              OrgGroupPermissionModelImpl.FINDER_CACHE_ENABLED,
82              FINDER_CLASS_NAME_LIST, "findByPermissionId",
83              new String[] {
84                  Long.class.getName(),
85                  
86              "java.lang.Integer", "java.lang.Integer",
87                  "com.liferay.portal.kernel.util.OrderByComparator"
88              });
89      public static final FinderPath FINDER_PATH_COUNT_BY_PERMISSIONID = new FinderPath(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
90              OrgGroupPermissionModelImpl.FINDER_CACHE_ENABLED,
91              FINDER_CLASS_NAME_LIST, "countByPermissionId",
92              new String[] { Long.class.getName() });
93      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
94              OrgGroupPermissionModelImpl.FINDER_CACHE_ENABLED,
95              FINDER_CLASS_NAME_LIST, "findAll", new String[0]);
96      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
97              OrgGroupPermissionModelImpl.FINDER_CACHE_ENABLED,
98              FINDER_CLASS_NAME_LIST, "countAll", new String[0]);
99  
100     public void cacheResult(OrgGroupPermission orgGroupPermission) {
101         EntityCacheUtil.putResult(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
102             OrgGroupPermissionImpl.class, orgGroupPermission.getPrimaryKey(),
103             orgGroupPermission);
104     }
105 
106     public void cacheResult(List<OrgGroupPermission> orgGroupPermissions) {
107         for (OrgGroupPermission orgGroupPermission : orgGroupPermissions) {
108             if (EntityCacheUtil.getResult(
109                         OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
110                         OrgGroupPermissionImpl.class,
111                         orgGroupPermission.getPrimaryKey(), this) == null) {
112                 cacheResult(orgGroupPermission);
113             }
114         }
115     }
116 
117     public void clearCache() {
118         CacheRegistry.clear(OrgGroupPermissionImpl.class.getName());
119         EntityCacheUtil.clearCache(OrgGroupPermissionImpl.class.getName());
120         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
121         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
122     }
123 
124     public void clearCache(OrgGroupPermission orgGroupPermission) {
125         EntityCacheUtil.removeResult(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
126             OrgGroupPermissionImpl.class, orgGroupPermission.getPrimaryKey());
127     }
128 
129     public OrgGroupPermission create(OrgGroupPermissionPK orgGroupPermissionPK) {
130         OrgGroupPermission orgGroupPermission = new OrgGroupPermissionImpl();
131 
132         orgGroupPermission.setNew(true);
133         orgGroupPermission.setPrimaryKey(orgGroupPermissionPK);
134 
135         return orgGroupPermission;
136     }
137 
138     public OrgGroupPermission remove(Serializable primaryKey)
139         throws NoSuchModelException, SystemException {
140         return remove((OrgGroupPermissionPK)primaryKey);
141     }
142 
143     public OrgGroupPermission remove(OrgGroupPermissionPK orgGroupPermissionPK)
144         throws NoSuchOrgGroupPermissionException, SystemException {
145         Session session = null;
146 
147         try {
148             session = openSession();
149 
150             OrgGroupPermission orgGroupPermission = (OrgGroupPermission)session.get(OrgGroupPermissionImpl.class,
151                     orgGroupPermissionPK);
152 
153             if (orgGroupPermission == null) {
154                 if (_log.isWarnEnabled()) {
155                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
156                         orgGroupPermissionPK);
157                 }
158 
159                 throw new NoSuchOrgGroupPermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
160                     orgGroupPermissionPK);
161             }
162 
163             return remove(orgGroupPermission);
164         }
165         catch (NoSuchOrgGroupPermissionException nsee) {
166             throw nsee;
167         }
168         catch (Exception e) {
169             throw processException(e);
170         }
171         finally {
172             closeSession(session);
173         }
174     }
175 
176     protected OrgGroupPermission removeImpl(
177         OrgGroupPermission orgGroupPermission) throws SystemException {
178         orgGroupPermission = toUnwrappedModel(orgGroupPermission);
179 
180         Session session = null;
181 
182         try {
183             session = openSession();
184 
185             BatchSessionUtil.delete(session, orgGroupPermission);
186         }
187         catch (Exception e) {
188             throw processException(e);
189         }
190         finally {
191             closeSession(session);
192         }
193 
194         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
195 
196         EntityCacheUtil.removeResult(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
197             OrgGroupPermissionImpl.class, orgGroupPermission.getPrimaryKey());
198 
199         return orgGroupPermission;
200     }
201 
202     /**
203      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
204      */
205     public OrgGroupPermission update(OrgGroupPermission orgGroupPermission)
206         throws SystemException {
207         if (_log.isWarnEnabled()) {
208             _log.warn(
209                 "Using the deprecated update(OrgGroupPermission orgGroupPermission) method. Use update(OrgGroupPermission orgGroupPermission, boolean merge) instead.");
210         }
211 
212         return update(orgGroupPermission, false);
213     }
214 
215     public OrgGroupPermission updateImpl(
216         com.liferay.portal.model.OrgGroupPermission orgGroupPermission,
217         boolean merge) throws SystemException {
218         orgGroupPermission = toUnwrappedModel(orgGroupPermission);
219 
220         Session session = null;
221 
222         try {
223             session = openSession();
224 
225             BatchSessionUtil.update(session, orgGroupPermission, merge);
226 
227             orgGroupPermission.setNew(false);
228         }
229         catch (Exception e) {
230             throw processException(e);
231         }
232         finally {
233             closeSession(session);
234         }
235 
236         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
237 
238         EntityCacheUtil.putResult(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
239             OrgGroupPermissionImpl.class, orgGroupPermission.getPrimaryKey(),
240             orgGroupPermission);
241 
242         return orgGroupPermission;
243     }
244 
245     protected OrgGroupPermission toUnwrappedModel(
246         OrgGroupPermission orgGroupPermission) {
247         if (orgGroupPermission instanceof OrgGroupPermissionImpl) {
248             return orgGroupPermission;
249         }
250 
251         OrgGroupPermissionImpl orgGroupPermissionImpl = new OrgGroupPermissionImpl();
252 
253         orgGroupPermissionImpl.setNew(orgGroupPermission.isNew());
254         orgGroupPermissionImpl.setPrimaryKey(orgGroupPermission.getPrimaryKey());
255 
256         orgGroupPermissionImpl.setOrganizationId(orgGroupPermission.getOrganizationId());
257         orgGroupPermissionImpl.setGroupId(orgGroupPermission.getGroupId());
258         orgGroupPermissionImpl.setPermissionId(orgGroupPermission.getPermissionId());
259 
260         return orgGroupPermissionImpl;
261     }
262 
263     public OrgGroupPermission findByPrimaryKey(Serializable primaryKey)
264         throws NoSuchModelException, SystemException {
265         return findByPrimaryKey((OrgGroupPermissionPK)primaryKey);
266     }
267 
268     public OrgGroupPermission findByPrimaryKey(
269         OrgGroupPermissionPK orgGroupPermissionPK)
270         throws NoSuchOrgGroupPermissionException, SystemException {
271         OrgGroupPermission orgGroupPermission = fetchByPrimaryKey(orgGroupPermissionPK);
272 
273         if (orgGroupPermission == null) {
274             if (_log.isWarnEnabled()) {
275                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
276                     orgGroupPermissionPK);
277             }
278 
279             throw new NoSuchOrgGroupPermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
280                 orgGroupPermissionPK);
281         }
282 
283         return orgGroupPermission;
284     }
285 
286     public OrgGroupPermission fetchByPrimaryKey(Serializable primaryKey)
287         throws SystemException {
288         return fetchByPrimaryKey((OrgGroupPermissionPK)primaryKey);
289     }
290 
291     public OrgGroupPermission fetchByPrimaryKey(
292         OrgGroupPermissionPK orgGroupPermissionPK) throws SystemException {
293         OrgGroupPermission orgGroupPermission = (OrgGroupPermission)EntityCacheUtil.getResult(OrgGroupPermissionModelImpl.ENTITY_CACHE_ENABLED,
294                 OrgGroupPermissionImpl.class, orgGroupPermissionPK, this);
295 
296         if (orgGroupPermission == null) {
297             Session session = null;
298 
299             try {
300                 session = openSession();
301 
302                 orgGroupPermission = (OrgGroupPermission)session.get(OrgGroupPermissionImpl.class,
303                         orgGroupPermissionPK);
304             }
305             catch (Exception e) {
306                 throw processException(e);
307             }
308             finally {
309                 if (orgGroupPermission != null) {
310                     cacheResult(orgGroupPermission);
311                 }
312 
313                 closeSession(session);
314             }
315         }
316 
317         return orgGroupPermission;
318     }
319 
320     public List<OrgGroupPermission> findByGroupId(long groupId)
321         throws SystemException {
322         return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
323     }
324 
325     public List<OrgGroupPermission> findByGroupId(long groupId, int start,
326         int end) throws SystemException {
327         return findByGroupId(groupId, start, end, null);
328     }
329 
330     public List<OrgGroupPermission> findByGroupId(long groupId, int start,
331         int end, OrderByComparator orderByComparator) throws SystemException {
332         Object[] finderArgs = new Object[] {
333                 groupId,
334                 
335                 String.valueOf(start), String.valueOf(end),
336                 String.valueOf(orderByComparator)
337             };
338 
339         List<OrgGroupPermission> list = (List<OrgGroupPermission>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
340                 finderArgs, this);
341 
342         if (list == null) {
343             StringBundler query = null;
344 
345             if (orderByComparator != null) {
346                 query = new StringBundler(3 +
347                         (orderByComparator.getOrderByFields().length * 3));
348             }
349             else {
350                 query = new StringBundler(2);
351             }
352 
353             query.append(_SQL_SELECT_ORGGROUPPERMISSION_WHERE);
354 
355             query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
356 
357             if (orderByComparator != null) {
358                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
359                     orderByComparator);
360             }
361 
362             String sql = query.toString();
363 
364             Session session = null;
365 
366             try {
367                 session = openSession();
368 
369                 Query q = session.createQuery(sql);
370 
371                 QueryPos qPos = QueryPos.getInstance(q);
372 
373                 qPos.add(groupId);
374 
375                 list = (List<OrgGroupPermission>)QueryUtil.list(q,
376                         getDialect(), start, end);
377             }
378             catch (Exception e) {
379                 throw processException(e);
380             }
381             finally {
382                 if (list == null) {
383                     list = new ArrayList<OrgGroupPermission>();
384                 }
385 
386                 cacheResult(list);
387 
388                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
389                     finderArgs, list);
390 
391                 closeSession(session);
392             }
393         }
394 
395         return list;
396     }
397 
398     public OrgGroupPermission findByGroupId_First(long groupId,
399         OrderByComparator orderByComparator)
400         throws NoSuchOrgGroupPermissionException, SystemException {
401         List<OrgGroupPermission> list = findByGroupId(groupId, 0, 1,
402                 orderByComparator);
403 
404         if (list.isEmpty()) {
405             StringBundler msg = new StringBundler(4);
406 
407             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
408 
409             msg.append("groupId=");
410             msg.append(groupId);
411 
412             msg.append(StringPool.CLOSE_CURLY_BRACE);
413 
414             throw new NoSuchOrgGroupPermissionException(msg.toString());
415         }
416         else {
417             return list.get(0);
418         }
419     }
420 
421     public OrgGroupPermission findByGroupId_Last(long groupId,
422         OrderByComparator orderByComparator)
423         throws NoSuchOrgGroupPermissionException, SystemException {
424         int count = countByGroupId(groupId);
425 
426         List<OrgGroupPermission> list = findByGroupId(groupId, count - 1,
427                 count, orderByComparator);
428 
429         if (list.isEmpty()) {
430             StringBundler msg = new StringBundler(4);
431 
432             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
433 
434             msg.append("groupId=");
435             msg.append(groupId);
436 
437             msg.append(StringPool.CLOSE_CURLY_BRACE);
438 
439             throw new NoSuchOrgGroupPermissionException(msg.toString());
440         }
441         else {
442             return list.get(0);
443         }
444     }
445 
446     public OrgGroupPermission[] findByGroupId_PrevAndNext(
447         OrgGroupPermissionPK orgGroupPermissionPK, long groupId,
448         OrderByComparator orderByComparator)
449         throws NoSuchOrgGroupPermissionException, SystemException {
450         OrgGroupPermission orgGroupPermission = findByPrimaryKey(orgGroupPermissionPK);
451 
452         Session session = null;
453 
454         try {
455             session = openSession();
456 
457             OrgGroupPermission[] array = new OrgGroupPermissionImpl[3];
458 
459             array[0] = getByGroupId_PrevAndNext(session, orgGroupPermission,
460                     groupId, orderByComparator, true);
461 
462             array[1] = orgGroupPermission;
463 
464             array[2] = getByGroupId_PrevAndNext(session, orgGroupPermission,
465                     groupId, orderByComparator, false);
466 
467             return array;
468         }
469         catch (Exception e) {
470             throw processException(e);
471         }
472         finally {
473             closeSession(session);
474         }
475     }
476 
477     protected OrgGroupPermission getByGroupId_PrevAndNext(Session session,
478         OrgGroupPermission orgGroupPermission, long groupId,
479         OrderByComparator orderByComparator, boolean previous) {
480         StringBundler query = null;
481 
482         if (orderByComparator != null) {
483             query = new StringBundler(6 +
484                     (orderByComparator.getOrderByFields().length * 6));
485         }
486         else {
487             query = new StringBundler(3);
488         }
489 
490         query.append(_SQL_SELECT_ORGGROUPPERMISSION_WHERE);
491 
492         query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
493 
494         if (orderByComparator != null) {
495             String[] orderByFields = orderByComparator.getOrderByFields();
496 
497             if (orderByFields.length > 0) {
498                 query.append(WHERE_AND);
499             }
500 
501             for (int i = 0; i < orderByFields.length; i++) {
502                 query.append(_ORDER_BY_ENTITY_ALIAS);
503                 query.append(orderByFields[i]);
504 
505                 if ((i + 1) < orderByFields.length) {
506                     if (orderByComparator.isAscending() ^ previous) {
507                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
508                     }
509                     else {
510                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
511                     }
512                 }
513                 else {
514                     if (orderByComparator.isAscending() ^ previous) {
515                         query.append(WHERE_GREATER_THAN);
516                     }
517                     else {
518                         query.append(WHERE_LESSER_THAN);
519                     }
520                 }
521             }
522 
523             query.append(ORDER_BY_CLAUSE);
524 
525             for (int i = 0; i < orderByFields.length; i++) {
526                 query.append(_ORDER_BY_ENTITY_ALIAS);
527                 query.append(orderByFields[i]);
528 
529                 if ((i + 1) < orderByFields.length) {
530                     if (orderByComparator.isAscending() ^ previous) {
531                         query.append(ORDER_BY_ASC_HAS_NEXT);
532                     }
533                     else {
534                         query.append(ORDER_BY_DESC_HAS_NEXT);
535                     }
536                 }
537                 else {
538                     if (orderByComparator.isAscending() ^ previous) {
539                         query.append(ORDER_BY_ASC);
540                     }
541                     else {
542                         query.append(ORDER_BY_DESC);
543                     }
544                 }
545             }
546         }
547 
548         String sql = query.toString();
549 
550         Query q = session.createQuery(sql);
551 
552         q.setFirstResult(0);
553         q.setMaxResults(2);
554 
555         QueryPos qPos = QueryPos.getInstance(q);
556 
557         qPos.add(groupId);
558 
559         if (orderByComparator != null) {
560             Object[] values = orderByComparator.getOrderByValues(orgGroupPermission);
561 
562             for (Object value : values) {
563                 qPos.add(value);
564             }
565         }
566 
567         List<OrgGroupPermission> list = q.list();
568 
569         if (list.size() == 2) {
570             return list.get(1);
571         }
572         else {
573             return null;
574         }
575     }
576 
577     public List<OrgGroupPermission> findByPermissionId(long permissionId)
578         throws SystemException {
579         return findByPermissionId(permissionId, QueryUtil.ALL_POS,
580             QueryUtil.ALL_POS, null);
581     }
582 
583     public List<OrgGroupPermission> findByPermissionId(long permissionId,
584         int start, int end) throws SystemException {
585         return findByPermissionId(permissionId, start, end, null);
586     }
587 
588     public List<OrgGroupPermission> findByPermissionId(long permissionId,
589         int start, int end, OrderByComparator orderByComparator)
590         throws SystemException {
591         Object[] finderArgs = new Object[] {
592                 permissionId,
593                 
594                 String.valueOf(start), String.valueOf(end),
595                 String.valueOf(orderByComparator)
596             };
597 
598         List<OrgGroupPermission> list = (List<OrgGroupPermission>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_PERMISSIONID,
599                 finderArgs, this);
600 
601         if (list == null) {
602             StringBundler query = null;
603 
604             if (orderByComparator != null) {
605                 query = new StringBundler(3 +
606                         (orderByComparator.getOrderByFields().length * 3));
607             }
608             else {
609                 query = new StringBundler(2);
610             }
611 
612             query.append(_SQL_SELECT_ORGGROUPPERMISSION_WHERE);
613 
614             query.append(_FINDER_COLUMN_PERMISSIONID_PERMISSIONID_2);
615 
616             if (orderByComparator != null) {
617                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
618                     orderByComparator);
619             }
620 
621             String sql = query.toString();
622 
623             Session session = null;
624 
625             try {
626                 session = openSession();
627 
628                 Query q = session.createQuery(sql);
629 
630                 QueryPos qPos = QueryPos.getInstance(q);
631 
632                 qPos.add(permissionId);
633 
634                 list = (List<OrgGroupPermission>)QueryUtil.list(q,
635                         getDialect(), start, end);
636             }
637             catch (Exception e) {
638                 throw processException(e);
639             }
640             finally {
641                 if (list == null) {
642                     list = new ArrayList<OrgGroupPermission>();
643                 }
644 
645                 cacheResult(list);
646 
647                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_PERMISSIONID,
648                     finderArgs, list);
649 
650                 closeSession(session);
651             }
652         }
653 
654         return list;
655     }
656 
657     public OrgGroupPermission findByPermissionId_First(long permissionId,
658         OrderByComparator orderByComparator)
659         throws NoSuchOrgGroupPermissionException, SystemException {
660         List<OrgGroupPermission> list = findByPermissionId(permissionId, 0, 1,
661                 orderByComparator);
662 
663         if (list.isEmpty()) {
664             StringBundler msg = new StringBundler(4);
665 
666             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
667 
668             msg.append("permissionId=");
669             msg.append(permissionId);
670 
671             msg.append(StringPool.CLOSE_CURLY_BRACE);
672 
673             throw new NoSuchOrgGroupPermissionException(msg.toString());
674         }
675         else {
676             return list.get(0);
677         }
678     }
679 
680     public OrgGroupPermission findByPermissionId_Last(long permissionId,
681         OrderByComparator orderByComparator)
682         throws NoSuchOrgGroupPermissionException, SystemException {
683         int count = countByPermissionId(permissionId);
684 
685         List<OrgGroupPermission> list = findByPermissionId(permissionId,
686                 count - 1, count, orderByComparator);
687 
688         if (list.isEmpty()) {
689             StringBundler msg = new StringBundler(4);
690 
691             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
692 
693             msg.append("permissionId=");
694             msg.append(permissionId);
695 
696             msg.append(StringPool.CLOSE_CURLY_BRACE);
697 
698             throw new NoSuchOrgGroupPermissionException(msg.toString());
699         }
700         else {
701             return list.get(0);
702         }
703     }
704 
705     public OrgGroupPermission[] findByPermissionId_PrevAndNext(
706         OrgGroupPermissionPK orgGroupPermissionPK, long permissionId,
707         OrderByComparator orderByComparator)
708         throws NoSuchOrgGroupPermissionException, SystemException {
709         OrgGroupPermission orgGroupPermission = findByPrimaryKey(orgGroupPermissionPK);
710 
711         Session session = null;
712 
713         try {
714             session = openSession();
715 
716             OrgGroupPermission[] array = new OrgGroupPermissionImpl[3];
717 
718             array[0] = getByPermissionId_PrevAndNext(session,
719                     orgGroupPermission, permissionId, orderByComparator, true);
720 
721             array[1] = orgGroupPermission;
722 
723             array[2] = getByPermissionId_PrevAndNext(session,
724                     orgGroupPermission, permissionId, orderByComparator, false);
725 
726             return array;
727         }
728         catch (Exception e) {
729             throw processException(e);
730         }
731         finally {
732             closeSession(session);
733         }
734     }
735 
736     protected OrgGroupPermission getByPermissionId_PrevAndNext(
737         Session session, OrgGroupPermission orgGroupPermission,
738         long permissionId, OrderByComparator orderByComparator, boolean previous) {
739         StringBundler query = null;
740 
741         if (orderByComparator != null) {
742             query = new StringBundler(6 +
743                     (orderByComparator.getOrderByFields().length * 6));
744         }
745         else {
746             query = new StringBundler(3);
747         }
748 
749         query.append(_SQL_SELECT_ORGGROUPPERMISSION_WHERE);
750 
751         query.append(_FINDER_COLUMN_PERMISSIONID_PERMISSIONID_2);
752 
753         if (orderByComparator != null) {
754             String[] orderByFields = orderByComparator.getOrderByFields();
755 
756             if (orderByFields.length > 0) {
757                 query.append(WHERE_AND);
758             }
759 
760             for (int i = 0; i < orderByFields.length; i++) {
761                 query.append(_ORDER_BY_ENTITY_ALIAS);
762                 query.append(orderByFields[i]);
763 
764                 if ((i + 1) < orderByFields.length) {
765                     if (orderByComparator.isAscending() ^ previous) {
766                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
767                     }
768                     else {
769                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
770                     }
771                 }
772                 else {
773                     if (orderByComparator.isAscending() ^ previous) {
774                         query.append(WHERE_GREATER_THAN);
775                     }
776                     else {
777                         query.append(WHERE_LESSER_THAN);
778                     }
779                 }
780             }
781 
782             query.append(ORDER_BY_CLAUSE);
783 
784             for (int i = 0; i < orderByFields.length; i++) {
785                 query.append(_ORDER_BY_ENTITY_ALIAS);
786                 query.append(orderByFields[i]);
787 
788                 if ((i + 1) < orderByFields.length) {
789                     if (orderByComparator.isAscending() ^ previous) {
790                         query.append(ORDER_BY_ASC_HAS_NEXT);
791                     }
792                     else {
793                         query.append(ORDER_BY_DESC_HAS_NEXT);
794                     }
795                 }
796                 else {
797                     if (orderByComparator.isAscending() ^ previous) {
798                         query.append(ORDER_BY_ASC);
799                     }
800                     else {
801                         query.append(ORDER_BY_DESC);
802                     }
803                 }
804             }
805         }
806 
807         String sql = query.toString();
808 
809         Query q = session.createQuery(sql);
810 
811         q.setFirstResult(0);
812         q.setMaxResults(2);
813 
814         QueryPos qPos = QueryPos.getInstance(q);
815 
816         qPos.add(permissionId);
817 
818         if (orderByComparator != null) {
819             Object[] values = orderByComparator.getOrderByValues(orgGroupPermission);
820 
821             for (Object value : values) {
822                 qPos.add(value);
823             }
824         }
825 
826         List<OrgGroupPermission> list = q.list();
827 
828         if (list.size() == 2) {
829             return list.get(1);
830         }
831         else {
832             return null;
833         }
834     }
835 
836     public List<OrgGroupPermission> findAll() throws SystemException {
837         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
838     }
839 
840     public List<OrgGroupPermission> findAll(int start, int end)
841         throws SystemException {
842         return findAll(start, end, null);
843     }
844 
845     public List<OrgGroupPermission> findAll(int start, int end,
846         OrderByComparator orderByComparator) throws SystemException {
847         Object[] finderArgs = new Object[] {
848                 String.valueOf(start), String.valueOf(end),
849                 String.valueOf(orderByComparator)
850             };
851 
852         List<OrgGroupPermission> list = (List<OrgGroupPermission>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
853                 finderArgs, this);
854 
855         if (list == null) {
856             StringBundler query = null;
857             String sql = null;
858 
859             if (orderByComparator != null) {
860                 query = new StringBundler(2 +
861                         (orderByComparator.getOrderByFields().length * 3));
862 
863                 query.append(_SQL_SELECT_ORGGROUPPERMISSION);
864 
865                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
866                     orderByComparator);
867 
868                 sql = query.toString();
869             }
870             else {
871                 sql = _SQL_SELECT_ORGGROUPPERMISSION;
872             }
873 
874             Session session = null;
875 
876             try {
877                 session = openSession();
878 
879                 Query q = session.createQuery(sql);
880 
881                 if (orderByComparator == null) {
882                     list = (List<OrgGroupPermission>)QueryUtil.list(q,
883                             getDialect(), start, end, false);
884 
885                     Collections.sort(list);
886                 }
887                 else {
888                     list = (List<OrgGroupPermission>)QueryUtil.list(q,
889                             getDialect(), start, end);
890                 }
891             }
892             catch (Exception e) {
893                 throw processException(e);
894             }
895             finally {
896                 if (list == null) {
897                     list = new ArrayList<OrgGroupPermission>();
898                 }
899 
900                 cacheResult(list);
901 
902                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
903 
904                 closeSession(session);
905             }
906         }
907 
908         return list;
909     }
910 
911     public void removeByGroupId(long groupId) throws SystemException {
912         for (OrgGroupPermission orgGroupPermission : findByGroupId(groupId)) {
913             remove(orgGroupPermission);
914         }
915     }
916 
917     public void removeByPermissionId(long permissionId)
918         throws SystemException {
919         for (OrgGroupPermission orgGroupPermission : findByPermissionId(
920                 permissionId)) {
921             remove(orgGroupPermission);
922         }
923     }
924 
925     public void removeAll() throws SystemException {
926         for (OrgGroupPermission orgGroupPermission : findAll()) {
927             remove(orgGroupPermission);
928         }
929     }
930 
931     public int countByGroupId(long groupId) throws SystemException {
932         Object[] finderArgs = new Object[] { groupId };
933 
934         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
935                 finderArgs, this);
936 
937         if (count == null) {
938             StringBundler query = new StringBundler(2);
939 
940             query.append(_SQL_COUNT_ORGGROUPPERMISSION_WHERE);
941 
942             query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
943 
944             String sql = query.toString();
945 
946             Session session = null;
947 
948             try {
949                 session = openSession();
950 
951                 Query q = session.createQuery(sql);
952 
953                 QueryPos qPos = QueryPos.getInstance(q);
954 
955                 qPos.add(groupId);
956 
957                 count = (Long)q.uniqueResult();
958             }
959             catch (Exception e) {
960                 throw processException(e);
961             }
962             finally {
963                 if (count == null) {
964                     count = Long.valueOf(0);
965                 }
966 
967                 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
968                     finderArgs, count);
969 
970                 closeSession(session);
971             }
972         }
973 
974         return count.intValue();
975     }
976 
977     public int countByPermissionId(long permissionId) throws SystemException {
978         Object[] finderArgs = new Object[] { permissionId };
979 
980         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_PERMISSIONID,
981                 finderArgs, this);
982 
983         if (count == null) {
984             StringBundler query = new StringBundler(2);
985 
986             query.append(_SQL_COUNT_ORGGROUPPERMISSION_WHERE);
987 
988             query.append(_FINDER_COLUMN_PERMISSIONID_PERMISSIONID_2);
989 
990             String sql = query.toString();
991 
992             Session session = null;
993 
994             try {
995                 session = openSession();
996 
997                 Query q = session.createQuery(sql);
998 
999                 QueryPos qPos = QueryPos.getInstance(q);
1000
1001                qPos.add(permissionId);
1002
1003                count = (Long)q.uniqueResult();
1004            }
1005            catch (Exception e) {
1006                throw processException(e);
1007            }
1008            finally {
1009                if (count == null) {
1010                    count = Long.valueOf(0);
1011                }
1012
1013                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_PERMISSIONID,
1014                    finderArgs, count);
1015
1016                closeSession(session);
1017            }
1018        }
1019
1020        return count.intValue();
1021    }
1022
1023    public int countAll() throws SystemException {
1024        Object[] finderArgs = new Object[0];
1025
1026        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1027                finderArgs, this);
1028
1029        if (count == null) {
1030            Session session = null;
1031
1032            try {
1033                session = openSession();
1034
1035                Query q = session.createQuery(_SQL_COUNT_ORGGROUPPERMISSION);
1036
1037                count = (Long)q.uniqueResult();
1038            }
1039            catch (Exception e) {
1040                throw processException(e);
1041            }
1042            finally {
1043                if (count == null) {
1044                    count = Long.valueOf(0);
1045                }
1046
1047                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1048                    count);
1049
1050                closeSession(session);
1051            }
1052        }
1053
1054        return count.intValue();
1055    }
1056
1057    public void afterPropertiesSet() {
1058        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1059                    com.liferay.portal.util.PropsUtil.get(
1060                        "value.object.listener.com.liferay.portal.model.OrgGroupPermission")));
1061
1062        if (listenerClassNames.length > 0) {
1063            try {
1064                List<ModelListener<OrgGroupPermission>> listenersList = new ArrayList<ModelListener<OrgGroupPermission>>();
1065
1066                for (String listenerClassName : listenerClassNames) {
1067                    listenersList.add((ModelListener<OrgGroupPermission>)InstanceFactory.newInstance(
1068                            listenerClassName));
1069                }
1070
1071                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1072            }
1073            catch (Exception e) {
1074                _log.error(e);
1075            }
1076        }
1077    }
1078
1079    public void destroy() {
1080        EntityCacheUtil.removeCache(OrgGroupPermissionImpl.class.getName());
1081        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1082        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
1083    }
1084
1085    @BeanReference(type = AccountPersistence.class)
1086    protected AccountPersistence accountPersistence;
1087    @BeanReference(type = AddressPersistence.class)
1088    protected AddressPersistence addressPersistence;
1089    @BeanReference(type = BrowserTrackerPersistence.class)
1090    protected BrowserTrackerPersistence browserTrackerPersistence;
1091    @BeanReference(type = ClassNamePersistence.class)
1092    protected ClassNamePersistence classNamePersistence;
1093    @BeanReference(type = CompanyPersistence.class)
1094    protected CompanyPersistence companyPersistence;
1095    @BeanReference(type = ContactPersistence.class)
1096    protected ContactPersistence contactPersistence;
1097    @BeanReference(type = CountryPersistence.class)
1098    protected CountryPersistence countryPersistence;
1099    @BeanReference(type = EmailAddressPersistence.class)
1100    protected EmailAddressPersistence emailAddressPersistence;
1101    @BeanReference(type = GroupPersistence.class)
1102    protected GroupPersistence groupPersistence;
1103    @BeanReference(type = ImagePersistence.class)
1104    protected ImagePersistence imagePersistence;
1105    @BeanReference(type = LayoutPersistence.class)
1106    protected LayoutPersistence layoutPersistence;
1107    @BeanReference(type = LayoutSetPersistence.class)
1108    protected LayoutSetPersistence layoutSetPersistence;
1109    @BeanReference(type = ListTypePersistence.class)
1110    protected ListTypePersistence listTypePersistence;
1111    @BeanReference(type = LockPersistence.class)
1112    protected LockPersistence lockPersistence;
1113    @BeanReference(type = MembershipRequestPersistence.class)
1114    protected MembershipRequestPersistence membershipRequestPersistence;
1115    @BeanReference(type = OrganizationPersistence.class)
1116    protected OrganizationPersistence organizationPersistence;
1117    @BeanReference(type = OrgGroupPermissionPersistence.class)
1118    protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1119    @BeanReference(type = OrgGroupRolePersistence.class)
1120    protected OrgGroupRolePersistence orgGroupRolePersistence;
1121    @BeanReference(type = OrgLaborPersistence.class)
1122    protected OrgLaborPersistence orgLaborPersistence;
1123    @BeanReference(type = PasswordPolicyPersistence.class)
1124    protected PasswordPolicyPersistence passwordPolicyPersistence;
1125    @BeanReference(type = PasswordPolicyRelPersistence.class)
1126    protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1127    @BeanReference(type = PasswordTrackerPersistence.class)
1128    protected PasswordTrackerPersistence passwordTrackerPersistence;
1129    @BeanReference(type = PermissionPersistence.class)
1130    protected PermissionPersistence permissionPersistence;
1131    @BeanReference(type = PhonePersistence.class)
1132    protected PhonePersistence phonePersistence;
1133    @BeanReference(type = PluginSettingPersistence.class)
1134    protected PluginSettingPersistence pluginSettingPersistence;
1135    @BeanReference(type = PortletPersistence.class)
1136    protected PortletPersistence portletPersistence;
1137    @BeanReference(type = PortletItemPersistence.class)
1138    protected PortletItemPersistence portletItemPersistence;
1139    @BeanReference(type = PortletPreferencesPersistence.class)
1140    protected PortletPreferencesPersistence portletPreferencesPersistence;
1141    @BeanReference(type = RegionPersistence.class)
1142    protected RegionPersistence regionPersistence;
1143    @BeanReference(type = ReleasePersistence.class)
1144    protected ReleasePersistence releasePersistence;
1145    @BeanReference(type = ResourcePersistence.class)
1146    protected ResourcePersistence resourcePersistence;
1147    @BeanReference(type = ResourceActionPersistence.class)
1148    protected ResourceActionPersistence resourceActionPersistence;
1149    @BeanReference(type = ResourceCodePersistence.class)
1150    protected ResourceCodePersistence resourceCodePersistence;
1151    @BeanReference(type = ResourcePermissionPersistence.class)
1152    protected ResourcePermissionPersistence resourcePermissionPersistence;
1153    @BeanReference(type = RolePersistence.class)
1154    protected RolePersistence rolePersistence;
1155    @BeanReference(type = ServiceComponentPersistence.class)
1156    protected ServiceComponentPersistence serviceComponentPersistence;
1157    @BeanReference(type = ShardPersistence.class)
1158    protected ShardPersistence shardPersistence;
1159    @BeanReference(type = SubscriptionPersistence.class)
1160    protected SubscriptionPersistence subscriptionPersistence;
1161    @BeanReference(type = UserPersistence.class)
1162    protected UserPersistence userPersistence;
1163    @BeanReference(type = UserGroupPersistence.class)
1164    protected UserGroupPersistence userGroupPersistence;
1165    @BeanReference(type = UserGroupGroupRolePersistence.class)
1166    protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1167    @BeanReference(type = UserGroupRolePersistence.class)
1168    protected UserGroupRolePersistence userGroupRolePersistence;
1169    @BeanReference(type = UserIdMapperPersistence.class)
1170    protected UserIdMapperPersistence userIdMapperPersistence;
1171    @BeanReference(type = UserTrackerPersistence.class)
1172    protected UserTrackerPersistence userTrackerPersistence;
1173    @BeanReference(type = UserTrackerPathPersistence.class)
1174    protected UserTrackerPathPersistence userTrackerPathPersistence;
1175    @BeanReference(type = WebDAVPropsPersistence.class)
1176    protected WebDAVPropsPersistence webDAVPropsPersistence;
1177    @BeanReference(type = WebsitePersistence.class)
1178    protected WebsitePersistence websitePersistence;
1179    private static final String _SQL_SELECT_ORGGROUPPERMISSION = "SELECT orgGroupPermission FROM OrgGroupPermission orgGroupPermission";
1180    private static final String _SQL_SELECT_ORGGROUPPERMISSION_WHERE = "SELECT orgGroupPermission FROM OrgGroupPermission orgGroupPermission WHERE ";
1181    private static final String _SQL_COUNT_ORGGROUPPERMISSION = "SELECT COUNT(orgGroupPermission) FROM OrgGroupPermission orgGroupPermission";
1182    private static final String _SQL_COUNT_ORGGROUPPERMISSION_WHERE = "SELECT COUNT(orgGroupPermission) FROM OrgGroupPermission orgGroupPermission WHERE ";
1183    private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "orgGroupPermission.id.groupId = ?";
1184    private static final String _FINDER_COLUMN_PERMISSIONID_PERMISSIONID_2 = "orgGroupPermission.id.permissionId = ?";
1185    private static final String _ORDER_BY_ENTITY_ALIAS = "orgGroupPermission.";
1186    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No OrgGroupPermission exists with the primary key ";
1187    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No OrgGroupPermission exists with the key {";
1188    private static Log _log = LogFactoryUtil.getLog(OrgGroupPermissionPersistenceImpl.class);
1189}