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