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