1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence;
21  
22  import com.liferay.portal.NoSuchUserGroupException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.annotation.BeanReference;
25  import com.liferay.portal.kernel.cache.CacheRegistry;
26  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
27  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
28  import com.liferay.portal.kernel.dao.jdbc.RowMapper;
29  import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
30  import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
31  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
32  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
33  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
34  import com.liferay.portal.kernel.dao.orm.FinderPath;
35  import com.liferay.portal.kernel.dao.orm.Query;
36  import com.liferay.portal.kernel.dao.orm.QueryPos;
37  import com.liferay.portal.kernel.dao.orm.QueryUtil;
38  import com.liferay.portal.kernel.dao.orm.SQLQuery;
39  import com.liferay.portal.kernel.dao.orm.Session;
40  import com.liferay.portal.kernel.dao.orm.Type;
41  import com.liferay.portal.kernel.log.Log;
42  import com.liferay.portal.kernel.log.LogFactoryUtil;
43  import com.liferay.portal.kernel.util.GetterUtil;
44  import com.liferay.portal.kernel.util.OrderByComparator;
45  import com.liferay.portal.kernel.util.StringPool;
46  import com.liferay.portal.kernel.util.StringUtil;
47  import com.liferay.portal.kernel.util.Validator;
48  import com.liferay.portal.model.ModelListener;
49  import com.liferay.portal.model.UserGroup;
50  import com.liferay.portal.model.impl.UserGroupImpl;
51  import com.liferay.portal.model.impl.UserGroupModelImpl;
52  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
53  
54  import java.sql.Types;
55  
56  import java.util.ArrayList;
57  import java.util.Collections;
58  import java.util.List;
59  
60  /**
61   * <a href="UserGroupPersistenceImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Brian Wing Shun Chan
64   *
65   */
66  public class UserGroupPersistenceImpl extends BasePersistenceImpl
67      implements UserGroupPersistence {
68      public static final String FINDER_CLASS_NAME_ENTITY = UserGroupImpl.class.getName();
69      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
70          ".List";
71      public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
72              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
73              "findByCompanyId", new String[] { Long.class.getName() });
74      public static final FinderPath FINDER_PATH_FIND_BY_OBC_COMPANYID = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
75              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
76              "findByCompanyId",
77              new String[] {
78                  Long.class.getName(),
79                  
80              "java.lang.Integer", "java.lang.Integer",
81                  "com.liferay.portal.kernel.util.OrderByComparator"
82              });
83      public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
84              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
85              "countByCompanyId", new String[] { Long.class.getName() });
86      public static final FinderPath FINDER_PATH_FIND_BY_C_P = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
87              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
88              "findByC_P",
89              new String[] { Long.class.getName(), Long.class.getName() });
90      public static final FinderPath FINDER_PATH_FIND_BY_OBC_C_P = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
91              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
92              "findByC_P",
93              new String[] {
94                  Long.class.getName(), Long.class.getName(),
95                  
96              "java.lang.Integer", "java.lang.Integer",
97                  "com.liferay.portal.kernel.util.OrderByComparator"
98              });
99      public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
100             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
101             "countByC_P",
102             new String[] { Long.class.getName(), Long.class.getName() });
103     public static final FinderPath FINDER_PATH_FETCH_BY_C_N = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
104             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
105             "fetchByC_N",
106             new String[] { Long.class.getName(), String.class.getName() });
107     public static final FinderPath FINDER_PATH_COUNT_BY_C_N = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
108             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
109             "countByC_N",
110             new String[] { Long.class.getName(), String.class.getName() });
111     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
112             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
113             "findAll", new String[0]);
114     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
115             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
116             "countAll", new String[0]);
117 
118     public void cacheResult(UserGroup userGroup) {
119         EntityCacheUtil.putResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
120             UserGroupImpl.class, userGroup.getPrimaryKey(), userGroup);
121 
122         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
123             new Object[] { new Long(userGroup.getCompanyId()), userGroup.getName() },
124             userGroup);
125     }
126 
127     public void cacheResult(List<UserGroup> userGroups) {
128         for (UserGroup userGroup : userGroups) {
129             if (EntityCacheUtil.getResult(
130                         UserGroupModelImpl.ENTITY_CACHE_ENABLED,
131                         UserGroupImpl.class, userGroup.getPrimaryKey(), this) == null) {
132                 cacheResult(userGroup);
133             }
134         }
135     }
136 
137     public void clearCache() {
138         CacheRegistry.clear(UserGroupImpl.class.getName());
139         EntityCacheUtil.clearCache(UserGroupImpl.class.getName());
140         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
141         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
142     }
143 
144     public UserGroup create(long userGroupId) {
145         UserGroup userGroup = new UserGroupImpl();
146 
147         userGroup.setNew(true);
148         userGroup.setPrimaryKey(userGroupId);
149 
150         return userGroup;
151     }
152 
153     public UserGroup remove(long userGroupId)
154         throws NoSuchUserGroupException, SystemException {
155         Session session = null;
156 
157         try {
158             session = openSession();
159 
160             UserGroup userGroup = (UserGroup)session.get(UserGroupImpl.class,
161                     new Long(userGroupId));
162 
163             if (userGroup == null) {
164                 if (_log.isWarnEnabled()) {
165                     _log.warn("No UserGroup exists with the primary key " +
166                         userGroupId);
167                 }
168 
169                 throw new NoSuchUserGroupException(
170                     "No UserGroup exists with the primary key " + userGroupId);
171             }
172 
173             return remove(userGroup);
174         }
175         catch (NoSuchUserGroupException nsee) {
176             throw nsee;
177         }
178         catch (Exception e) {
179             throw processException(e);
180         }
181         finally {
182             closeSession(session);
183         }
184     }
185 
186     public UserGroup remove(UserGroup userGroup) throws SystemException {
187         for (ModelListener<UserGroup> listener : listeners) {
188             listener.onBeforeRemove(userGroup);
189         }
190 
191         userGroup = removeImpl(userGroup);
192 
193         for (ModelListener<UserGroup> listener : listeners) {
194             listener.onAfterRemove(userGroup);
195         }
196 
197         return userGroup;
198     }
199 
200     protected UserGroup removeImpl(UserGroup userGroup)
201         throws SystemException {
202         try {
203             clearUsers.clear(userGroup.getPrimaryKey());
204         }
205         catch (Exception e) {
206             throw processException(e);
207         }
208         finally {
209             FinderCacheUtil.clearCache("Users_UserGroups");
210         }
211 
212         Session session = null;
213 
214         try {
215             session = openSession();
216 
217             if (userGroup.isCachedModel() || BatchSessionUtil.isEnabled()) {
218                 Object staleObject = session.get(UserGroupImpl.class,
219                         userGroup.getPrimaryKeyObj());
220 
221                 if (staleObject != null) {
222                     session.evict(staleObject);
223                 }
224             }
225 
226             session.delete(userGroup);
227 
228             session.flush();
229         }
230         catch (Exception e) {
231             throw processException(e);
232         }
233         finally {
234             closeSession(session);
235         }
236 
237         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
238 
239         UserGroupModelImpl userGroupModelImpl = (UserGroupModelImpl)userGroup;
240 
241         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
242             new Object[] {
243                 new Long(userGroupModelImpl.getOriginalCompanyId()),
244                 
245             userGroupModelImpl.getOriginalName()
246             });
247 
248         EntityCacheUtil.removeResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
249             UserGroupImpl.class, userGroup.getPrimaryKey());
250 
251         return userGroup;
252     }
253 
254     /**
255      * @deprecated Use <code>update(UserGroup userGroup, boolean merge)</code>.
256      */
257     public UserGroup update(UserGroup userGroup) throws SystemException {
258         if (_log.isWarnEnabled()) {
259             _log.warn(
260                 "Using the deprecated update(UserGroup userGroup) method. Use update(UserGroup userGroup, boolean merge) instead.");
261         }
262 
263         return update(userGroup, false);
264     }
265 
266     /**
267      * Add, update, or merge, the entity. This method also calls the model
268      * listeners to trigger the proper events associated with adding, deleting,
269      * or updating an entity.
270      *
271      * @param        userGroup the entity to add, update, or merge
272      * @param        merge boolean value for whether to merge the entity. The
273      *                default value is false. Setting merge to true is more
274      *                expensive and should only be true when userGroup is
275      *                transient. See LEP-5473 for a detailed discussion of this
276      *                method.
277      * @return        true if the portlet can be displayed via Ajax
278      */
279     public UserGroup update(UserGroup userGroup, boolean merge)
280         throws SystemException {
281         boolean isNew = userGroup.isNew();
282 
283         for (ModelListener<UserGroup> listener : listeners) {
284             if (isNew) {
285                 listener.onBeforeCreate(userGroup);
286             }
287             else {
288                 listener.onBeforeUpdate(userGroup);
289             }
290         }
291 
292         userGroup = updateImpl(userGroup, merge);
293 
294         for (ModelListener<UserGroup> listener : listeners) {
295             if (isNew) {
296                 listener.onAfterCreate(userGroup);
297             }
298             else {
299                 listener.onAfterUpdate(userGroup);
300             }
301         }
302 
303         return userGroup;
304     }
305 
306     public UserGroup updateImpl(com.liferay.portal.model.UserGroup userGroup,
307         boolean merge) throws SystemException {
308         boolean isNew = userGroup.isNew();
309 
310         UserGroupModelImpl userGroupModelImpl = (UserGroupModelImpl)userGroup;
311 
312         Session session = null;
313 
314         try {
315             session = openSession();
316 
317             BatchSessionUtil.update(session, userGroup, merge);
318 
319             userGroup.setNew(false);
320         }
321         catch (Exception e) {
322             throw processException(e);
323         }
324         finally {
325             closeSession(session);
326         }
327 
328         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
329 
330         EntityCacheUtil.putResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
331             UserGroupImpl.class, userGroup.getPrimaryKey(), userGroup);
332 
333         if (!isNew &&
334                 ((userGroup.getCompanyId() != userGroupModelImpl.getOriginalCompanyId()) ||
335                 !Validator.equals(userGroup.getName(),
336                     userGroupModelImpl.getOriginalName()))) {
337             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
338                 new Object[] {
339                     new Long(userGroupModelImpl.getOriginalCompanyId()),
340                     
341                 userGroupModelImpl.getOriginalName()
342                 });
343         }
344 
345         if (isNew ||
346                 ((userGroup.getCompanyId() != userGroupModelImpl.getOriginalCompanyId()) ||
347                 !Validator.equals(userGroup.getName(),
348                     userGroupModelImpl.getOriginalName()))) {
349             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
350                 new Object[] {
351                     new Long(userGroup.getCompanyId()),
352                     
353                 userGroup.getName()
354                 }, userGroup);
355         }
356 
357         return userGroup;
358     }
359 
360     public UserGroup findByPrimaryKey(long userGroupId)
361         throws NoSuchUserGroupException, SystemException {
362         UserGroup userGroup = fetchByPrimaryKey(userGroupId);
363 
364         if (userGroup == null) {
365             if (_log.isWarnEnabled()) {
366                 _log.warn("No UserGroup exists with the primary key " +
367                     userGroupId);
368             }
369 
370             throw new NoSuchUserGroupException(
371                 "No UserGroup exists with the primary key " + userGroupId);
372         }
373 
374         return userGroup;
375     }
376 
377     public UserGroup fetchByPrimaryKey(long userGroupId)
378         throws SystemException {
379         UserGroup userGroup = (UserGroup)EntityCacheUtil.getResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
380                 UserGroupImpl.class, userGroupId, this);
381 
382         if (userGroup == null) {
383             Session session = null;
384 
385             try {
386                 session = openSession();
387 
388                 userGroup = (UserGroup)session.get(UserGroupImpl.class,
389                         new Long(userGroupId));
390             }
391             catch (Exception e) {
392                 throw processException(e);
393             }
394             finally {
395                 if (userGroup != null) {
396                     cacheResult(userGroup);
397                 }
398 
399                 closeSession(session);
400             }
401         }
402 
403         return userGroup;
404     }
405 
406     public List<UserGroup> findByCompanyId(long companyId)
407         throws SystemException {
408         Object[] finderArgs = new Object[] { new Long(companyId) };
409 
410         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
411                 finderArgs, this);
412 
413         if (list == null) {
414             Session session = null;
415 
416             try {
417                 session = openSession();
418 
419                 StringBuilder query = new StringBuilder();
420 
421                 query.append("SELECT userGroup FROM UserGroup userGroup WHERE ");
422 
423                 query.append("userGroup.companyId = ?");
424 
425                 query.append(" ");
426 
427                 query.append("ORDER BY ");
428 
429                 query.append("userGroup.name ASC");
430 
431                 Query q = session.createQuery(query.toString());
432 
433                 QueryPos qPos = QueryPos.getInstance(q);
434 
435                 qPos.add(companyId);
436 
437                 list = q.list();
438             }
439             catch (Exception e) {
440                 throw processException(e);
441             }
442             finally {
443                 if (list == null) {
444                     list = new ArrayList<UserGroup>();
445                 }
446 
447                 cacheResult(list);
448 
449                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
450                     finderArgs, list);
451 
452                 closeSession(session);
453             }
454         }
455 
456         return list;
457     }
458 
459     public List<UserGroup> findByCompanyId(long companyId, int start, int end)
460         throws SystemException {
461         return findByCompanyId(companyId, start, end, null);
462     }
463 
464     public List<UserGroup> findByCompanyId(long companyId, int start, int end,
465         OrderByComparator obc) throws SystemException {
466         Object[] finderArgs = new Object[] {
467                 new Long(companyId),
468                 
469                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
470             };
471 
472         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
473                 finderArgs, this);
474 
475         if (list == null) {
476             Session session = null;
477 
478             try {
479                 session = openSession();
480 
481                 StringBuilder query = new StringBuilder();
482 
483                 query.append("SELECT userGroup FROM UserGroup userGroup WHERE ");
484 
485                 query.append("userGroup.companyId = ?");
486 
487                 query.append(" ");
488 
489                 if (obc != null) {
490                     query.append("ORDER BY ");
491 
492                     String[] orderByFields = obc.getOrderByFields();
493 
494                     for (int i = 0; i < orderByFields.length; i++) {
495                         query.append("userGroup.");
496                         query.append(orderByFields[i]);
497 
498                         if (obc.isAscending()) {
499                             query.append(" ASC");
500                         }
501                         else {
502                             query.append(" DESC");
503                         }
504 
505                         if ((i + 1) < orderByFields.length) {
506                             query.append(", ");
507                         }
508                     }
509                 }
510 
511                 else {
512                     query.append("ORDER BY ");
513 
514                     query.append("userGroup.name ASC");
515                 }
516 
517                 Query q = session.createQuery(query.toString());
518 
519                 QueryPos qPos = QueryPos.getInstance(q);
520 
521                 qPos.add(companyId);
522 
523                 list = (List<UserGroup>)QueryUtil.list(q, getDialect(), start,
524                         end);
525             }
526             catch (Exception e) {
527                 throw processException(e);
528             }
529             finally {
530                 if (list == null) {
531                     list = new ArrayList<UserGroup>();
532                 }
533 
534                 cacheResult(list);
535 
536                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
537                     finderArgs, list);
538 
539                 closeSession(session);
540             }
541         }
542 
543         return list;
544     }
545 
546     public UserGroup findByCompanyId_First(long companyId, OrderByComparator obc)
547         throws NoSuchUserGroupException, SystemException {
548         List<UserGroup> list = findByCompanyId(companyId, 0, 1, obc);
549 
550         if (list.isEmpty()) {
551             StringBuilder msg = new StringBuilder();
552 
553             msg.append("No UserGroup exists with the key {");
554 
555             msg.append("companyId=" + companyId);
556 
557             msg.append(StringPool.CLOSE_CURLY_BRACE);
558 
559             throw new NoSuchUserGroupException(msg.toString());
560         }
561         else {
562             return list.get(0);
563         }
564     }
565 
566     public UserGroup findByCompanyId_Last(long companyId, OrderByComparator obc)
567         throws NoSuchUserGroupException, SystemException {
568         int count = countByCompanyId(companyId);
569 
570         List<UserGroup> list = findByCompanyId(companyId, count - 1, count, obc);
571 
572         if (list.isEmpty()) {
573             StringBuilder msg = new StringBuilder();
574 
575             msg.append("No UserGroup exists with the key {");
576 
577             msg.append("companyId=" + companyId);
578 
579             msg.append(StringPool.CLOSE_CURLY_BRACE);
580 
581             throw new NoSuchUserGroupException(msg.toString());
582         }
583         else {
584             return list.get(0);
585         }
586     }
587 
588     public UserGroup[] findByCompanyId_PrevAndNext(long userGroupId,
589         long companyId, OrderByComparator obc)
590         throws NoSuchUserGroupException, SystemException {
591         UserGroup userGroup = findByPrimaryKey(userGroupId);
592 
593         int count = countByCompanyId(companyId);
594 
595         Session session = null;
596 
597         try {
598             session = openSession();
599 
600             StringBuilder query = new StringBuilder();
601 
602             query.append("SELECT userGroup FROM UserGroup userGroup WHERE ");
603 
604             query.append("userGroup.companyId = ?");
605 
606             query.append(" ");
607 
608             if (obc != null) {
609                 query.append("ORDER BY ");
610 
611                 String[] orderByFields = obc.getOrderByFields();
612 
613                 for (int i = 0; i < orderByFields.length; i++) {
614                     query.append("userGroup.");
615                     query.append(orderByFields[i]);
616 
617                     if (obc.isAscending()) {
618                         query.append(" ASC");
619                     }
620                     else {
621                         query.append(" DESC");
622                     }
623 
624                     if ((i + 1) < orderByFields.length) {
625                         query.append(", ");
626                     }
627                 }
628             }
629 
630             else {
631                 query.append("ORDER BY ");
632 
633                 query.append("userGroup.name ASC");
634             }
635 
636             Query q = session.createQuery(query.toString());
637 
638             QueryPos qPos = QueryPos.getInstance(q);
639 
640             qPos.add(companyId);
641 
642             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
643                     userGroup);
644 
645             UserGroup[] array = new UserGroupImpl[3];
646 
647             array[0] = (UserGroup)objArray[0];
648             array[1] = (UserGroup)objArray[1];
649             array[2] = (UserGroup)objArray[2];
650 
651             return array;
652         }
653         catch (Exception e) {
654             throw processException(e);
655         }
656         finally {
657             closeSession(session);
658         }
659     }
660 
661     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId)
662         throws SystemException {
663         Object[] finderArgs = new Object[] {
664                 new Long(companyId), new Long(parentUserGroupId)
665             };
666 
667         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_P,
668                 finderArgs, this);
669 
670         if (list == null) {
671             Session session = null;
672 
673             try {
674                 session = openSession();
675 
676                 StringBuilder query = new StringBuilder();
677 
678                 query.append("SELECT userGroup FROM UserGroup userGroup WHERE ");
679 
680                 query.append("userGroup.companyId = ?");
681 
682                 query.append(" AND ");
683 
684                 query.append("userGroup.parentUserGroupId = ?");
685 
686                 query.append(" ");
687 
688                 query.append("ORDER BY ");
689 
690                 query.append("userGroup.name ASC");
691 
692                 Query q = session.createQuery(query.toString());
693 
694                 QueryPos qPos = QueryPos.getInstance(q);
695 
696                 qPos.add(companyId);
697 
698                 qPos.add(parentUserGroupId);
699 
700                 list = q.list();
701             }
702             catch (Exception e) {
703                 throw processException(e);
704             }
705             finally {
706                 if (list == null) {
707                     list = new ArrayList<UserGroup>();
708                 }
709 
710                 cacheResult(list);
711 
712                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_P, finderArgs,
713                     list);
714 
715                 closeSession(session);
716             }
717         }
718 
719         return list;
720     }
721 
722     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId,
723         int start, int end) throws SystemException {
724         return findByC_P(companyId, parentUserGroupId, start, end, null);
725     }
726 
727     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId,
728         int start, int end, OrderByComparator obc) throws SystemException {
729         Object[] finderArgs = new Object[] {
730                 new Long(companyId), new Long(parentUserGroupId),
731                 
732                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
733             };
734 
735         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_C_P,
736                 finderArgs, this);
737 
738         if (list == null) {
739             Session session = null;
740 
741             try {
742                 session = openSession();
743 
744                 StringBuilder query = new StringBuilder();
745 
746                 query.append("SELECT userGroup FROM UserGroup userGroup WHERE ");
747 
748                 query.append("userGroup.companyId = ?");
749 
750                 query.append(" AND ");
751 
752                 query.append("userGroup.parentUserGroupId = ?");
753 
754                 query.append(" ");
755 
756                 if (obc != null) {
757                     query.append("ORDER BY ");
758 
759                     String[] orderByFields = obc.getOrderByFields();
760 
761                     for (int i = 0; i < orderByFields.length; i++) {
762                         query.append("userGroup.");
763                         query.append(orderByFields[i]);
764 
765                         if (obc.isAscending()) {
766                             query.append(" ASC");
767                         }
768                         else {
769                             query.append(" DESC");
770                         }
771 
772                         if ((i + 1) < orderByFields.length) {
773                             query.append(", ");
774                         }
775                     }
776                 }
777 
778                 else {
779                     query.append("ORDER BY ");
780 
781                     query.append("userGroup.name ASC");
782                 }
783 
784                 Query q = session.createQuery(query.toString());
785 
786                 QueryPos qPos = QueryPos.getInstance(q);
787 
788                 qPos.add(companyId);
789 
790                 qPos.add(parentUserGroupId);
791 
792                 list = (List<UserGroup>)QueryUtil.list(q, getDialect(), start,
793                         end);
794             }
795             catch (Exception e) {
796                 throw processException(e);
797             }
798             finally {
799                 if (list == null) {
800                     list = new ArrayList<UserGroup>();
801                 }
802 
803                 cacheResult(list);
804 
805                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_C_P,
806                     finderArgs, list);
807 
808                 closeSession(session);
809             }
810         }
811 
812         return list;
813     }
814 
815     public UserGroup findByC_P_First(long companyId, long parentUserGroupId,
816         OrderByComparator obc) throws NoSuchUserGroupException, SystemException {
817         List<UserGroup> list = findByC_P(companyId, parentUserGroupId, 0, 1, obc);
818 
819         if (list.isEmpty()) {
820             StringBuilder msg = new StringBuilder();
821 
822             msg.append("No UserGroup exists with the key {");
823 
824             msg.append("companyId=" + companyId);
825 
826             msg.append(", ");
827             msg.append("parentUserGroupId=" + parentUserGroupId);
828 
829             msg.append(StringPool.CLOSE_CURLY_BRACE);
830 
831             throw new NoSuchUserGroupException(msg.toString());
832         }
833         else {
834             return list.get(0);
835         }
836     }
837 
838     public UserGroup findByC_P_Last(long companyId, long parentUserGroupId,
839         OrderByComparator obc) throws NoSuchUserGroupException, SystemException {
840         int count = countByC_P(companyId, parentUserGroupId);
841 
842         List<UserGroup> list = findByC_P(companyId, parentUserGroupId,
843                 count - 1, count, obc);
844 
845         if (list.isEmpty()) {
846             StringBuilder msg = new StringBuilder();
847 
848             msg.append("No UserGroup exists with the key {");
849 
850             msg.append("companyId=" + companyId);
851 
852             msg.append(", ");
853             msg.append("parentUserGroupId=" + parentUserGroupId);
854 
855             msg.append(StringPool.CLOSE_CURLY_BRACE);
856 
857             throw new NoSuchUserGroupException(msg.toString());
858         }
859         else {
860             return list.get(0);
861         }
862     }
863 
864     public UserGroup[] findByC_P_PrevAndNext(long userGroupId, long companyId,
865         long parentUserGroupId, OrderByComparator obc)
866         throws NoSuchUserGroupException, SystemException {
867         UserGroup userGroup = findByPrimaryKey(userGroupId);
868 
869         int count = countByC_P(companyId, parentUserGroupId);
870 
871         Session session = null;
872 
873         try {
874             session = openSession();
875 
876             StringBuilder query = new StringBuilder();
877 
878             query.append("SELECT userGroup FROM UserGroup userGroup WHERE ");
879 
880             query.append("userGroup.companyId = ?");
881 
882             query.append(" AND ");
883 
884             query.append("userGroup.parentUserGroupId = ?");
885 
886             query.append(" ");
887 
888             if (obc != null) {
889                 query.append("ORDER BY ");
890 
891                 String[] orderByFields = obc.getOrderByFields();
892 
893                 for (int i = 0; i < orderByFields.length; i++) {
894                     query.append("userGroup.");
895                     query.append(orderByFields[i]);
896 
897                     if (obc.isAscending()) {
898                         query.append(" ASC");
899                     }
900                     else {
901                         query.append(" DESC");
902                     }
903 
904                     if ((i + 1) < orderByFields.length) {
905                         query.append(", ");
906                     }
907                 }
908             }
909 
910             else {
911                 query.append("ORDER BY ");
912 
913                 query.append("userGroup.name ASC");
914             }
915 
916             Query q = session.createQuery(query.toString());
917 
918             QueryPos qPos = QueryPos.getInstance(q);
919 
920             qPos.add(companyId);
921 
922             qPos.add(parentUserGroupId);
923 
924             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
925                     userGroup);
926 
927             UserGroup[] array = new UserGroupImpl[3];
928 
929             array[0] = (UserGroup)objArray[0];
930             array[1] = (UserGroup)objArray[1];
931             array[2] = (UserGroup)objArray[2];
932 
933             return array;
934         }
935         catch (Exception e) {
936             throw processException(e);
937         }
938         finally {
939             closeSession(session);
940         }
941     }
942 
943     public UserGroup findByC_N(long companyId, String name)
944         throws NoSuchUserGroupException, SystemException {
945         UserGroup userGroup = fetchByC_N(companyId, name);
946 
947         if (userGroup == null) {
948             StringBuilder msg = new StringBuilder();
949 
950             msg.append("No UserGroup exists with the key {");
951 
952             msg.append("companyId=" + companyId);
953 
954             msg.append(", ");
955             msg.append("name=" + name);
956 
957             msg.append(StringPool.CLOSE_CURLY_BRACE);
958 
959             if (_log.isWarnEnabled()) {
960                 _log.warn(msg.toString());
961             }
962 
963             throw new NoSuchUserGroupException(msg.toString());
964         }
965 
966         return userGroup;
967     }
968 
969     public UserGroup fetchByC_N(long companyId, String name)
970         throws SystemException {
971         return fetchByC_N(companyId, name, true);
972     }
973 
974     public UserGroup fetchByC_N(long companyId, String name,
975         boolean retrieveFromCache) throws SystemException {
976         Object[] finderArgs = new Object[] { new Long(companyId), name };
977 
978         Object result = null;
979 
980         if (retrieveFromCache) {
981             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N,
982                     finderArgs, this);
983         }
984 
985         if (result == null) {
986             Session session = null;
987 
988             try {
989                 session = openSession();
990 
991                 StringBuilder query = new StringBuilder();
992 
993                 query.append("SELECT userGroup FROM UserGroup userGroup WHERE ");
994 
995                 query.append("userGroup.companyId = ?");
996 
997                 query.append(" AND ");
998 
999                 if (name == null) {
1000                    query.append("userGroup.name IS NULL");
1001                }
1002                else {
1003                    query.append("userGroup.name = ?");
1004                }
1005
1006                query.append(" ");
1007
1008                query.append("ORDER BY ");
1009
1010                query.append("userGroup.name ASC");
1011
1012                Query q = session.createQuery(query.toString());
1013
1014                QueryPos qPos = QueryPos.getInstance(q);
1015
1016                qPos.add(companyId);
1017
1018                if (name != null) {
1019                    qPos.add(name);
1020                }
1021
1022                List<UserGroup> list = q.list();
1023
1024                result = list;
1025
1026                UserGroup userGroup = null;
1027
1028                if (list.isEmpty()) {
1029                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1030                        finderArgs, list);
1031                }
1032                else {
1033                    userGroup = list.get(0);
1034
1035                    cacheResult(userGroup);
1036
1037                    if ((userGroup.getCompanyId() != companyId) ||
1038                            (userGroup.getName() == null) ||
1039                            !userGroup.getName().equals(name)) {
1040                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1041                            finderArgs, userGroup);
1042                    }
1043                }
1044
1045                return userGroup;
1046            }
1047            catch (Exception e) {
1048                throw processException(e);
1049            }
1050            finally {
1051                if (result == null) {
1052                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1053                        finderArgs, new ArrayList<UserGroup>());
1054                }
1055
1056                closeSession(session);
1057            }
1058        }
1059        else {
1060            if (result instanceof List) {
1061                return null;
1062            }
1063            else {
1064                return (UserGroup)result;
1065            }
1066        }
1067    }
1068
1069    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1070        throws SystemException {
1071        Session session = null;
1072
1073        try {
1074            session = openSession();
1075
1076            dynamicQuery.compile(session);
1077
1078            return dynamicQuery.list();
1079        }
1080        catch (Exception e) {
1081            throw processException(e);
1082        }
1083        finally {
1084            closeSession(session);
1085        }
1086    }
1087
1088    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1089        int start, int end) throws SystemException {
1090        Session session = null;
1091
1092        try {
1093            session = openSession();
1094
1095            dynamicQuery.setLimit(start, end);
1096
1097            dynamicQuery.compile(session);
1098
1099            return dynamicQuery.list();
1100        }
1101        catch (Exception e) {
1102            throw processException(e);
1103        }
1104        finally {
1105            closeSession(session);
1106        }
1107    }
1108
1109    public List<UserGroup> findAll() throws SystemException {
1110        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1111    }
1112
1113    public List<UserGroup> findAll(int start, int end)
1114        throws SystemException {
1115        return findAll(start, end, null);
1116    }
1117
1118    public List<UserGroup> findAll(int start, int end, OrderByComparator obc)
1119        throws SystemException {
1120        Object[] finderArgs = new Object[] {
1121                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1122            };
1123
1124        List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1125                finderArgs, this);
1126
1127        if (list == null) {
1128            Session session = null;
1129
1130            try {
1131                session = openSession();
1132
1133                StringBuilder query = new StringBuilder();
1134
1135                query.append("SELECT userGroup FROM UserGroup userGroup ");
1136
1137                if (obc != null) {
1138                    query.append("ORDER BY ");
1139
1140                    String[] orderByFields = obc.getOrderByFields();
1141
1142                    for (int i = 0; i < orderByFields.length; i++) {
1143                        query.append("userGroup.");
1144                        query.append(orderByFields[i]);
1145
1146                        if (obc.isAscending()) {
1147                            query.append(" ASC");
1148                        }
1149                        else {
1150                            query.append(" DESC");
1151                        }
1152
1153                        if ((i + 1) < orderByFields.length) {
1154                            query.append(", ");
1155                        }
1156                    }
1157                }
1158
1159                else {
1160                    query.append("ORDER BY ");
1161
1162                    query.append("userGroup.name ASC");
1163                }
1164
1165                Query q = session.createQuery(query.toString());
1166
1167                if (obc == null) {
1168                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1169                            start, end, false);
1170
1171                    Collections.sort(list);
1172                }
1173                else {
1174                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1175                            start, end);
1176                }
1177            }
1178            catch (Exception e) {
1179                throw processException(e);
1180            }
1181            finally {
1182                if (list == null) {
1183                    list = new ArrayList<UserGroup>();
1184                }
1185
1186                cacheResult(list);
1187
1188                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1189
1190                closeSession(session);
1191            }
1192        }
1193
1194        return list;
1195    }
1196
1197    public void removeByCompanyId(long companyId) throws SystemException {
1198        for (UserGroup userGroup : findByCompanyId(companyId)) {
1199            remove(userGroup);
1200        }
1201    }
1202
1203    public void removeByC_P(long companyId, long parentUserGroupId)
1204        throws SystemException {
1205        for (UserGroup userGroup : findByC_P(companyId, parentUserGroupId)) {
1206            remove(userGroup);
1207        }
1208    }
1209
1210    public void removeByC_N(long companyId, String name)
1211        throws NoSuchUserGroupException, SystemException {
1212        UserGroup userGroup = findByC_N(companyId, name);
1213
1214        remove(userGroup);
1215    }
1216
1217    public void removeAll() throws SystemException {
1218        for (UserGroup userGroup : findAll()) {
1219            remove(userGroup);
1220        }
1221    }
1222
1223    public int countByCompanyId(long companyId) throws SystemException {
1224        Object[] finderArgs = new Object[] { new Long(companyId) };
1225
1226        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
1227                finderArgs, this);
1228
1229        if (count == null) {
1230            Session session = null;
1231
1232            try {
1233                session = openSession();
1234
1235                StringBuilder query = new StringBuilder();
1236
1237                query.append("SELECT COUNT(userGroup) ");
1238                query.append("FROM UserGroup userGroup WHERE ");
1239
1240                query.append("userGroup.companyId = ?");
1241
1242                query.append(" ");
1243
1244                Query q = session.createQuery(query.toString());
1245
1246                QueryPos qPos = QueryPos.getInstance(q);
1247
1248                qPos.add(companyId);
1249
1250                count = (Long)q.uniqueResult();
1251            }
1252            catch (Exception e) {
1253                throw processException(e);
1254            }
1255            finally {
1256                if (count == null) {
1257                    count = Long.valueOf(0);
1258                }
1259
1260                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
1261                    finderArgs, count);
1262
1263                closeSession(session);
1264            }
1265        }
1266
1267        return count.intValue();
1268    }
1269
1270    public int countByC_P(long companyId, long parentUserGroupId)
1271        throws SystemException {
1272        Object[] finderArgs = new Object[] {
1273                new Long(companyId), new Long(parentUserGroupId)
1274            };
1275
1276        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
1277                finderArgs, this);
1278
1279        if (count == null) {
1280            Session session = null;
1281
1282            try {
1283                session = openSession();
1284
1285                StringBuilder query = new StringBuilder();
1286
1287                query.append("SELECT COUNT(userGroup) ");
1288                query.append("FROM UserGroup userGroup WHERE ");
1289
1290                query.append("userGroup.companyId = ?");
1291
1292                query.append(" AND ");
1293
1294                query.append("userGroup.parentUserGroupId = ?");
1295
1296                query.append(" ");
1297
1298                Query q = session.createQuery(query.toString());
1299
1300                QueryPos qPos = QueryPos.getInstance(q);
1301
1302                qPos.add(companyId);
1303
1304                qPos.add(parentUserGroupId);
1305
1306                count = (Long)q.uniqueResult();
1307            }
1308            catch (Exception e) {
1309                throw processException(e);
1310            }
1311            finally {
1312                if (count == null) {
1313                    count = Long.valueOf(0);
1314                }
1315
1316                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
1317                    count);
1318
1319                closeSession(session);
1320            }
1321        }
1322
1323        return count.intValue();
1324    }
1325
1326    public int countByC_N(long companyId, String name)
1327        throws SystemException {
1328        Object[] finderArgs = new Object[] { new Long(companyId), name };
1329
1330        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_N,
1331                finderArgs, this);
1332
1333        if (count == null) {
1334            Session session = null;
1335
1336            try {
1337                session = openSession();
1338
1339                StringBuilder query = new StringBuilder();
1340
1341                query.append("SELECT COUNT(userGroup) ");
1342                query.append("FROM UserGroup userGroup WHERE ");
1343
1344                query.append("userGroup.companyId = ?");
1345
1346                query.append(" AND ");
1347
1348                if (name == null) {
1349                    query.append("userGroup.name IS NULL");
1350                }
1351                else {
1352                    query.append("userGroup.name = ?");
1353                }
1354
1355                query.append(" ");
1356
1357                Query q = session.createQuery(query.toString());
1358
1359                QueryPos qPos = QueryPos.getInstance(q);
1360
1361                qPos.add(companyId);
1362
1363                if (name != null) {
1364                    qPos.add(name);
1365                }
1366
1367                count = (Long)q.uniqueResult();
1368            }
1369            catch (Exception e) {
1370                throw processException(e);
1371            }
1372            finally {
1373                if (count == null) {
1374                    count = Long.valueOf(0);
1375                }
1376
1377                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N, finderArgs,
1378                    count);
1379
1380                closeSession(session);
1381            }
1382        }
1383
1384        return count.intValue();
1385    }
1386
1387    public int countAll() throws SystemException {
1388        Object[] finderArgs = new Object[0];
1389
1390        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1391                finderArgs, this);
1392
1393        if (count == null) {
1394            Session session = null;
1395
1396            try {
1397                session = openSession();
1398
1399                Query q = session.createQuery(
1400                        "SELECT COUNT(userGroup) FROM UserGroup userGroup");
1401
1402                count = (Long)q.uniqueResult();
1403            }
1404            catch (Exception e) {
1405                throw processException(e);
1406            }
1407            finally {
1408                if (count == null) {
1409                    count = Long.valueOf(0);
1410                }
1411
1412                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1413                    count);
1414
1415                closeSession(session);
1416            }
1417        }
1418
1419        return count.intValue();
1420    }
1421
1422    public List<com.liferay.portal.model.User> getUsers(long pk)
1423        throws SystemException {
1424        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1425    }
1426
1427    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1428        int end) throws SystemException {
1429        return getUsers(pk, start, end, null);
1430    }
1431
1432    public static final FinderPath FINDER_PATH_GET_USERS = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1433            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1434            "Users_UserGroups", "getUsers",
1435            new String[] {
1436                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1437                "com.liferay.portal.kernel.util.OrderByComparator"
1438            });
1439
1440    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1441        int end, OrderByComparator obc) throws SystemException {
1442        Object[] finderArgs = new Object[] {
1443                new Long(pk), String.valueOf(start), String.valueOf(end),
1444                String.valueOf(obc)
1445            };
1446
1447        List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS,
1448                finderArgs, this);
1449
1450        if (list == null) {
1451            Session session = null;
1452
1453            try {
1454                session = openSession();
1455
1456                StringBuilder sb = new StringBuilder();
1457
1458                sb.append(_SQL_GETUSERS);
1459
1460                if (obc != null) {
1461                    sb.append("ORDER BY ");
1462                    sb.append(obc.getOrderBy());
1463                }
1464
1465                String sql = sb.toString();
1466
1467                SQLQuery q = session.createSQLQuery(sql);
1468
1469                q.addEntity("User_",
1470                    com.liferay.portal.model.impl.UserImpl.class);
1471
1472                QueryPos qPos = QueryPos.getInstance(q);
1473
1474                qPos.add(pk);
1475
1476                list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
1477                        getDialect(), start, end);
1478            }
1479            catch (Exception e) {
1480                throw processException(e);
1481            }
1482            finally {
1483                if (list == null) {
1484                    list = new ArrayList<com.liferay.portal.model.User>();
1485                }
1486
1487                userPersistence.cacheResult(list);
1488
1489                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS, finderArgs,
1490                    list);
1491
1492                closeSession(session);
1493            }
1494        }
1495
1496        return list;
1497    }
1498
1499    public static final FinderPath FINDER_PATH_GET_USERS_SIZE = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1500            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1501            "Users_UserGroups", "getUsersSize",
1502            new String[] { Long.class.getName() });
1503
1504    public int getUsersSize(long pk) throws SystemException {
1505        Object[] finderArgs = new Object[] { new Long(pk) };
1506
1507        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS_SIZE,
1508                finderArgs, this);
1509
1510        if (count == null) {
1511            Session session = null;
1512
1513            try {
1514                session = openSession();
1515
1516                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
1517
1518                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1519
1520                QueryPos qPos = QueryPos.getInstance(q);
1521
1522                qPos.add(pk);
1523
1524                count = (Long)q.uniqueResult();
1525            }
1526            catch (Exception e) {
1527                throw processException(e);
1528            }
1529            finally {
1530                if (count == null) {
1531                    count = Long.valueOf(0);
1532                }
1533
1534                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS_SIZE,
1535                    finderArgs, count);
1536
1537                closeSession(session);
1538            }
1539        }
1540
1541        return count.intValue();
1542    }
1543
1544    public static final FinderPath FINDER_PATH_CONTAINS_USER = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1545            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1546            "Users_UserGroups", "containsUser",
1547            new String[] { Long.class.getName(), Long.class.getName() });
1548
1549    public boolean containsUser(long pk, long userPK) throws SystemException {
1550        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
1551
1552        Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_USER,
1553                finderArgs, this);
1554
1555        if (value == null) {
1556            try {
1557                value = Boolean.valueOf(containsUser.contains(pk, userPK));
1558            }
1559            catch (Exception e) {
1560                throw processException(e);
1561            }
1562            finally {
1563                if (value == null) {
1564                    value = Boolean.FALSE;
1565                }
1566
1567                FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_USER,
1568                    finderArgs, value);
1569            }
1570        }
1571
1572        return value.booleanValue();
1573    }
1574
1575    public boolean containsUsers(long pk) throws SystemException {
1576        if (getUsersSize(pk) > 0) {
1577            return true;
1578        }
1579        else {
1580            return false;
1581        }
1582    }
1583
1584    public void addUser(long pk, long userPK) throws SystemException {
1585        try {
1586            addUser.add(pk, userPK);
1587        }
1588        catch (Exception e) {
1589            throw processException(e);
1590        }
1591        finally {
1592            FinderCacheUtil.clearCache("Users_UserGroups");
1593        }
1594    }
1595
1596    public void addUser(long pk, com.liferay.portal.model.User user)
1597        throws SystemException {
1598        try {
1599            addUser.add(pk, user.getPrimaryKey());
1600        }
1601        catch (Exception e) {
1602            throw processException(e);
1603        }
1604        finally {
1605            FinderCacheUtil.clearCache("Users_UserGroups");
1606        }
1607    }
1608
1609    public void addUsers(long pk, long[] userPKs) throws SystemException {
1610        try {
1611            for (long userPK : userPKs) {
1612                addUser.add(pk, userPK);
1613            }
1614        }
1615        catch (Exception e) {
1616            throw processException(e);
1617        }
1618        finally {
1619            FinderCacheUtil.clearCache("Users_UserGroups");
1620        }
1621    }
1622
1623    public void addUsers(long pk, List<com.liferay.portal.model.User> users)
1624        throws SystemException {
1625        try {
1626            for (com.liferay.portal.model.User user : users) {
1627                addUser.add(pk, user.getPrimaryKey());
1628            }
1629        }
1630        catch (Exception e) {
1631            throw processException(e);
1632        }
1633        finally {
1634            FinderCacheUtil.clearCache("Users_UserGroups");
1635        }
1636    }
1637
1638    public void clearUsers(long pk) throws SystemException {
1639        try {
1640            clearUsers.clear(pk);
1641        }
1642        catch (Exception e) {
1643            throw processException(e);
1644        }
1645        finally {
1646            FinderCacheUtil.clearCache("Users_UserGroups");
1647        }
1648    }
1649
1650    public void removeUser(long pk, long userPK) throws SystemException {
1651        try {
1652            removeUser.remove(pk, userPK);
1653        }
1654        catch (Exception e) {
1655            throw processException(e);
1656        }
1657        finally {
1658            FinderCacheUtil.clearCache("Users_UserGroups");
1659        }
1660    }
1661
1662    public void removeUser(long pk, com.liferay.portal.model.User user)
1663        throws SystemException {
1664        try {
1665            removeUser.remove(pk, user.getPrimaryKey());
1666        }
1667        catch (Exception e) {
1668            throw processException(e);
1669        }
1670        finally {
1671            FinderCacheUtil.clearCache("Users_UserGroups");
1672        }
1673    }
1674
1675    public void removeUsers(long pk, long[] userPKs) throws SystemException {
1676        try {
1677            for (long userPK : userPKs) {
1678                removeUser.remove(pk, userPK);
1679            }
1680        }
1681        catch (Exception e) {
1682            throw processException(e);
1683        }
1684        finally {
1685            FinderCacheUtil.clearCache("Users_UserGroups");
1686        }
1687    }
1688
1689    public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
1690        throws SystemException {
1691        try {
1692            for (com.liferay.portal.model.User user : users) {
1693                removeUser.remove(pk, user.getPrimaryKey());
1694            }
1695        }
1696        catch (Exception e) {
1697            throw processException(e);
1698        }
1699        finally {
1700            FinderCacheUtil.clearCache("Users_UserGroups");
1701        }
1702    }
1703
1704    public void setUsers(long pk, long[] userPKs) throws SystemException {
1705        try {
1706            clearUsers.clear(pk);
1707
1708            for (long userPK : userPKs) {
1709                addUser.add(pk, userPK);
1710            }
1711        }
1712        catch (Exception e) {
1713            throw processException(e);
1714        }
1715        finally {
1716            FinderCacheUtil.clearCache("Users_UserGroups");
1717        }
1718    }
1719
1720    public void setUsers(long pk, List<com.liferay.portal.model.User> users)
1721        throws SystemException {
1722        try {
1723            clearUsers.clear(pk);
1724
1725            for (com.liferay.portal.model.User user : users) {
1726                addUser.add(pk, user.getPrimaryKey());
1727            }
1728        }
1729        catch (Exception e) {
1730            throw processException(e);
1731        }
1732        finally {
1733            FinderCacheUtil.clearCache("Users_UserGroups");
1734        }
1735    }
1736
1737    public void afterPropertiesSet() {
1738        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1739                    com.liferay.portal.util.PropsUtil.get(
1740                        "value.object.listener.com.liferay.portal.model.UserGroup")));
1741
1742        if (listenerClassNames.length > 0) {
1743            try {
1744                List<ModelListener<UserGroup>> listenersList = new ArrayList<ModelListener<UserGroup>>();
1745
1746                for (String listenerClassName : listenerClassNames) {
1747                    listenersList.add((ModelListener<UserGroup>)Class.forName(
1748                            listenerClassName).newInstance());
1749                }
1750
1751                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1752            }
1753            catch (Exception e) {
1754                _log.error(e);
1755            }
1756        }
1757
1758        containsUser = new ContainsUser(this);
1759
1760        addUser = new AddUser(this);
1761        clearUsers = new ClearUsers(this);
1762        removeUser = new RemoveUser(this);
1763    }
1764
1765    @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence.impl")
1766    protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
1767    @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence.impl")
1768    protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
1769    @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence.impl")
1770    protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
1771    @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence.impl")
1772    protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
1773    @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence.impl")
1774    protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
1775    @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence.impl")
1776    protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
1777    @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence.impl")
1778    protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
1779    @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence.impl")
1780    protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
1781    @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence.impl")
1782    protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
1783    @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence.impl")
1784    protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
1785    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence.impl")
1786    protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
1787    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence.impl")
1788    protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
1789    @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence.impl")
1790    protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
1791    @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence.impl")
1792    protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
1793    @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence.impl")
1794    protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
1795    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence.impl")
1796    protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1797    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence.impl")
1798    protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
1799    @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence.impl")
1800    protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
1801    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence.impl")
1802    protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
1803    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence.impl")
1804    protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1805    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence.impl")
1806    protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
1807    @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence.impl")
1808    protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
1809    @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence.impl")
1810    protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
1811    @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence.impl")
1812    protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
1813    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence.impl")
1814    protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
1815    @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence.impl")
1816    protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
1817    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence.impl")
1818    protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
1819    @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence.impl")
1820    protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
1821    @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence.impl")
1822    protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
1823    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1824    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1825    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence.impl")
1826    protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
1827    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence.impl")
1828    protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
1829    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence.impl")
1830    protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
1831    @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence.impl")
1832    protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
1833    @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence.impl")
1834    protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
1835    @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence.impl")
1836    protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
1837    @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence.impl")
1838    protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
1839    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1840    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1841    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence.impl")
1842    protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
1843    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence.impl")
1844    protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
1845    @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence.impl")
1846    protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
1847    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence.impl")
1848    protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
1849    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence.impl")
1850    protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
1851    @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence.impl")
1852    protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
1853    @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence.impl")
1854    protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
1855    protected ContainsUser containsUser;
1856    protected AddUser addUser;
1857    protected ClearUsers clearUsers;
1858    protected RemoveUser removeUser;
1859
1860    protected class ContainsUser {
1861        protected ContainsUser(UserGroupPersistenceImpl persistenceImpl) {
1862            super();
1863
1864            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
1865                    _SQL_CONTAINSUSER,
1866                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
1867        }
1868
1869        protected boolean contains(long userGroupId, long userId) {
1870            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
1871                        new Long(userGroupId), new Long(userId)
1872                    });
1873
1874            if (results.size() > 0) {
1875                Integer count = results.get(0);
1876
1877                if (count.intValue() > 0) {
1878                    return true;
1879                }
1880            }
1881
1882            return false;
1883        }
1884
1885        private MappingSqlQuery _mappingSqlQuery;
1886    }
1887
1888    protected class AddUser {
1889        protected AddUser(UserGroupPersistenceImpl persistenceImpl) {
1890            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1891                    "INSERT INTO Users_UserGroups (userGroupId, userId) VALUES (?, ?)",
1892                    new int[] { Types.BIGINT, Types.BIGINT });
1893            _persistenceImpl = persistenceImpl;
1894        }
1895
1896        protected void add(long userGroupId, long userId)
1897            throws SystemException {
1898            if (!_persistenceImpl.containsUser.contains(userGroupId, userId)) {
1899                ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1900
1901                for (ModelListener<UserGroup> listener : listeners) {
1902                    listener.onBeforeAddAssociation(userGroupId,
1903                        com.liferay.portal.model.User.class.getName(), userId);
1904                }
1905
1906                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1907                    listener.onBeforeAddAssociation(userId,
1908                        UserGroup.class.getName(), userGroupId);
1909                }
1910
1911                _sqlUpdate.update(new Object[] {
1912                        new Long(userGroupId), new Long(userId)
1913                    });
1914
1915                for (ModelListener<UserGroup> listener : listeners) {
1916                    listener.onAfterAddAssociation(userGroupId,
1917                        com.liferay.portal.model.User.class.getName(), userId);
1918                }
1919
1920                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1921                    listener.onAfterAddAssociation(userId,
1922                        UserGroup.class.getName(), userGroupId);
1923                }
1924            }
1925        }
1926
1927        private SqlUpdate _sqlUpdate;
1928        private UserGroupPersistenceImpl _persistenceImpl;
1929    }
1930
1931    protected class ClearUsers {
1932        protected ClearUsers(UserGroupPersistenceImpl persistenceImpl) {
1933            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1934                    "DELETE FROM Users_UserGroups WHERE userGroupId = ?",
1935                    new int[] { Types.BIGINT });
1936        }
1937
1938        protected void clear(long userGroupId) throws SystemException {
1939            ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1940
1941            List<com.liferay.portal.model.User> users = null;
1942
1943            if ((listeners.length > 0) || (userListeners.length > 0)) {
1944                users = getUsers(userGroupId);
1945
1946                for (com.liferay.portal.model.User user : users) {
1947                    for (ModelListener<UserGroup> listener : listeners) {
1948                        listener.onBeforeRemoveAssociation(userGroupId,
1949                            com.liferay.portal.model.User.class.getName(),
1950                            user.getPrimaryKey());
1951                    }
1952
1953                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1954                        listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
1955                            UserGroup.class.getName(), userGroupId);
1956                    }
1957                }
1958            }
1959
1960            _sqlUpdate.update(new Object[] { new Long(userGroupId) });
1961
1962            if ((listeners.length > 0) || (userListeners.length > 0)) {
1963                for (com.liferay.portal.model.User user : users) {
1964                    for (ModelListener<UserGroup> listener : listeners) {
1965                        listener.onAfterRemoveAssociation(userGroupId,
1966                            com.liferay.portal.model.User.class.getName(),
1967                            user.getPrimaryKey());
1968                    }
1969
1970                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1971                        listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
1972                            UserGroup.class.getName(), userGroupId);
1973                    }
1974                }
1975            }
1976        }
1977
1978        private SqlUpdate _sqlUpdate;
1979    }
1980
1981    protected class RemoveUser {
1982        protected RemoveUser(UserGroupPersistenceImpl persistenceImpl) {
1983            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1984                    "DELETE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?",
1985                    new int[] { Types.BIGINT, Types.BIGINT });
1986            _persistenceImpl = persistenceImpl;
1987        }
1988
1989        protected void remove(long userGroupId, long userId)
1990            throws SystemException {
1991            if (_persistenceImpl.containsUser.contains(userGroupId, userId)) {
1992                ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1993
1994                for (ModelListener<UserGroup> listener : listeners) {
1995                    listener.onBeforeRemoveAssociation(userGroupId,
1996                        com.liferay.portal.model.User.class.getName(), userId);
1997                }
1998
1999                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
2000                    listener.onBeforeRemoveAssociation(userId,
2001                        UserGroup.class.getName(), userGroupId);
2002                }
2003
2004                _sqlUpdate.update(new Object[] {
2005                        new Long(userGroupId), new Long(userId)
2006                    });
2007
2008                for (ModelListener<UserGroup> listener : listeners) {
2009                    listener.onAfterRemoveAssociation(userGroupId,
2010                        com.liferay.portal.model.User.class.getName(), userId);
2011                }
2012
2013                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
2014                    listener.onAfterRemoveAssociation(userId,
2015                        UserGroup.class.getName(), userGroupId);
2016                }
2017            }
2018        }
2019
2020        private SqlUpdate _sqlUpdate;
2021        private UserGroupPersistenceImpl _persistenceImpl;
2022    }
2023
2024    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_UserGroups ON (Users_UserGroups.userId = User_.userId) WHERE (Users_UserGroups.userGroupId = ?)";
2025    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ?";
2026    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?";
2027    private static Log _log = LogFactoryUtil.getLog(UserGroupPersistenceImpl.class);
2028}