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