1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.NoSuchOrganizationException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.annotation.BeanReference;
21  import com.liferay.portal.kernel.cache.CacheRegistry;
22  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
23  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
24  import com.liferay.portal.kernel.dao.jdbc.RowMapper;
25  import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
26  import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.FinderPath;
31  import com.liferay.portal.kernel.dao.orm.Query;
32  import com.liferay.portal.kernel.dao.orm.QueryPos;
33  import com.liferay.portal.kernel.dao.orm.QueryUtil;
34  import com.liferay.portal.kernel.dao.orm.SQLQuery;
35  import com.liferay.portal.kernel.dao.orm.Session;
36  import com.liferay.portal.kernel.dao.orm.Type;
37  import com.liferay.portal.kernel.log.Log;
38  import com.liferay.portal.kernel.log.LogFactoryUtil;
39  import com.liferay.portal.kernel.util.GetterUtil;
40  import com.liferay.portal.kernel.util.OrderByComparator;
41  import com.liferay.portal.kernel.util.SetUtil;
42  import com.liferay.portal.kernel.util.StringBundler;
43  import com.liferay.portal.kernel.util.StringPool;
44  import com.liferay.portal.kernel.util.StringUtil;
45  import com.liferay.portal.kernel.util.Validator;
46  import com.liferay.portal.model.ModelListener;
47  import com.liferay.portal.model.Organization;
48  import com.liferay.portal.model.impl.OrganizationImpl;
49  import com.liferay.portal.model.impl.OrganizationModelImpl;
50  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
51  
52  import java.io.Serializable;
53  
54  import java.sql.Types;
55  
56  import java.util.ArrayList;
57  import java.util.Collections;
58  import java.util.List;
59  import java.util.Set;
60  
61  /**
62   * <a href="OrganizationPersistenceImpl.java.html"><b><i>View Source</i></b></a>
63   *
64   * <p>
65   * ServiceBuilder generated this class. Modifications in this class will be
66   * overwritten the next time is generated.
67   * </p>
68   *
69   * @author    Brian Wing Shun Chan
70   * @see       OrganizationPersistence
71   * @see       OrganizationUtil
72   * @generated
73   */
74  public class OrganizationPersistenceImpl extends BasePersistenceImpl<Organization>
75      implements OrganizationPersistence {
76      public static final String FINDER_CLASS_NAME_ENTITY = OrganizationImpl.class.getName();
77      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
78          ".List";
79      public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
80              OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
81              "findByCompanyId", new String[] { Long.class.getName() });
82      public static final FinderPath FINDER_PATH_FIND_BY_OBC_COMPANYID = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
83              OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
84              "findByCompanyId",
85              new String[] {
86                  Long.class.getName(),
87                  
88              "java.lang.Integer", "java.lang.Integer",
89                  "com.liferay.portal.kernel.util.OrderByComparator"
90              });
91      public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
92              OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
93              "countByCompanyId", new String[] { Long.class.getName() });
94      public static final FinderPath FINDER_PATH_FIND_BY_LOCATIONS = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
95              OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
96              "findByLocations", new String[] { Long.class.getName() });
97      public static final FinderPath FINDER_PATH_FIND_BY_OBC_LOCATIONS = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
98              OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
99              "findByLocations",
100             new String[] {
101                 Long.class.getName(),
102                 
103             "java.lang.Integer", "java.lang.Integer",
104                 "com.liferay.portal.kernel.util.OrderByComparator"
105             });
106     public static final FinderPath FINDER_PATH_COUNT_BY_LOCATIONS = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
107             OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
108             "countByLocations", new String[] { Long.class.getName() });
109     public static final FinderPath FINDER_PATH_FIND_BY_C_P = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
110             OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
111             "findByC_P",
112             new String[] { Long.class.getName(), Long.class.getName() });
113     public static final FinderPath FINDER_PATH_FIND_BY_OBC_C_P = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
114             OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
115             "findByC_P",
116             new String[] {
117                 Long.class.getName(), Long.class.getName(),
118                 
119             "java.lang.Integer", "java.lang.Integer",
120                 "com.liferay.portal.kernel.util.OrderByComparator"
121             });
122     public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
123             OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
124             "countByC_P",
125             new String[] { Long.class.getName(), Long.class.getName() });
126     public static final FinderPath FINDER_PATH_FETCH_BY_C_N = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
127             OrganizationModelImpl.FINDER_CACHE_ENABLED,
128             FINDER_CLASS_NAME_ENTITY, "fetchByC_N",
129             new String[] { Long.class.getName(), String.class.getName() });
130     public static final FinderPath FINDER_PATH_COUNT_BY_C_N = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
131             OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
132             "countByC_N",
133             new String[] { Long.class.getName(), String.class.getName() });
134     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
135             OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
136             "findAll", new String[0]);
137     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
138             OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
139             "countAll", new String[0]);
140 
141     public void cacheResult(Organization organization) {
142         EntityCacheUtil.putResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
143             OrganizationImpl.class, organization.getPrimaryKey(), organization);
144 
145         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
146             new Object[] {
147                 new Long(organization.getCompanyId()),
148                 
149             organization.getName()
150             }, organization);
151     }
152 
153     public void cacheResult(List<Organization> organizations) {
154         for (Organization organization : organizations) {
155             if (EntityCacheUtil.getResult(
156                         OrganizationModelImpl.ENTITY_CACHE_ENABLED,
157                         OrganizationImpl.class, organization.getPrimaryKey(),
158                         this) == null) {
159                 cacheResult(organization);
160             }
161         }
162     }
163 
164     public void clearCache() {
165         CacheRegistry.clear(OrganizationImpl.class.getName());
166         EntityCacheUtil.clearCache(OrganizationImpl.class.getName());
167         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
168         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
169     }
170 
171     public Organization create(long organizationId) {
172         Organization organization = new OrganizationImpl();
173 
174         organization.setNew(true);
175         organization.setPrimaryKey(organizationId);
176 
177         return organization;
178     }
179 
180     public Organization remove(Serializable primaryKey)
181         throws NoSuchModelException, SystemException {
182         return remove(((Long)primaryKey).longValue());
183     }
184 
185     public Organization remove(long organizationId)
186         throws NoSuchOrganizationException, SystemException {
187         Session session = null;
188 
189         try {
190             session = openSession();
191 
192             Organization organization = (Organization)session.get(OrganizationImpl.class,
193                     new Long(organizationId));
194 
195             if (organization == null) {
196                 if (_log.isWarnEnabled()) {
197                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
198                         organizationId);
199                 }
200 
201                 throw new NoSuchOrganizationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
202                     organizationId);
203             }
204 
205             return remove(organization);
206         }
207         catch (NoSuchOrganizationException nsee) {
208             throw nsee;
209         }
210         catch (Exception e) {
211             throw processException(e);
212         }
213         finally {
214             closeSession(session);
215         }
216     }
217 
218     public Organization remove(Organization organization)
219         throws SystemException {
220         for (ModelListener<Organization> listener : listeners) {
221             listener.onBeforeRemove(organization);
222         }
223 
224         organization = removeImpl(organization);
225 
226         for (ModelListener<Organization> listener : listeners) {
227             listener.onAfterRemove(organization);
228         }
229 
230         return organization;
231     }
232 
233     protected Organization removeImpl(Organization organization)
234         throws SystemException {
235         organization = toUnwrappedModel(organization);
236 
237         try {
238             clearGroups.clear(organization.getPrimaryKey());
239         }
240         catch (Exception e) {
241             throw processException(e);
242         }
243         finally {
244             FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
245         }
246 
247         try {
248             clearUsers.clear(organization.getPrimaryKey());
249         }
250         catch (Exception e) {
251             throw processException(e);
252         }
253         finally {
254             FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
255         }
256 
257         shrinkTree(organization);
258 
259         Session session = null;
260 
261         try {
262             session = openSession();
263 
264             if (organization.isCachedModel() || BatchSessionUtil.isEnabled()) {
265                 Object staleObject = session.get(OrganizationImpl.class,
266                         organization.getPrimaryKeyObj());
267 
268                 if (staleObject != null) {
269                     session.evict(staleObject);
270                 }
271             }
272 
273             session.delete(organization);
274 
275             session.flush();
276         }
277         catch (Exception e) {
278             throw processException(e);
279         }
280         finally {
281             closeSession(session);
282         }
283 
284         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
285 
286         OrganizationModelImpl organizationModelImpl = (OrganizationModelImpl)organization;
287 
288         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
289             new Object[] {
290                 new Long(organizationModelImpl.getOriginalCompanyId()),
291                 
292             organizationModelImpl.getOriginalName()
293             });
294 
295         EntityCacheUtil.removeResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
296             OrganizationImpl.class, organization.getPrimaryKey());
297 
298         return organization;
299     }
300 
301     /**
302      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
303      */
304     public Organization update(Organization organization)
305         throws SystemException {
306         if (_log.isWarnEnabled()) {
307             _log.warn(
308                 "Using the deprecated update(Organization organization) method. Use update(Organization organization, boolean merge) instead.");
309         }
310 
311         return update(organization, false);
312     }
313 
314     public Organization updateImpl(
315         com.liferay.portal.model.Organization organization, boolean merge)
316         throws SystemException {
317         organization = toUnwrappedModel(organization);
318 
319         boolean isNew = organization.isNew();
320 
321         OrganizationModelImpl organizationModelImpl = (OrganizationModelImpl)organization;
322 
323         if (isNew) {
324             expandTree(organization);
325         }
326         else {
327             if (organization.getParentOrganizationId() != organizationModelImpl.getOriginalParentOrganizationId()) {
328                 shrinkTree(organization);
329                 expandTree(organization);
330             }
331         }
332 
333         Session session = null;
334 
335         try {
336             session = openSession();
337 
338             BatchSessionUtil.update(session, organization, merge);
339 
340             organization.setNew(false);
341         }
342         catch (Exception e) {
343             throw processException(e);
344         }
345         finally {
346             closeSession(session);
347         }
348 
349         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
350 
351         EntityCacheUtil.putResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
352             OrganizationImpl.class, organization.getPrimaryKey(), organization);
353 
354         if (!isNew &&
355                 ((organization.getCompanyId() != organizationModelImpl.getOriginalCompanyId()) ||
356                 !Validator.equals(organization.getName(),
357                     organizationModelImpl.getOriginalName()))) {
358             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
359                 new Object[] {
360                     new Long(organizationModelImpl.getOriginalCompanyId()),
361                     
362                 organizationModelImpl.getOriginalName()
363                 });
364         }
365 
366         if (isNew ||
367                 ((organization.getCompanyId() != organizationModelImpl.getOriginalCompanyId()) ||
368                 !Validator.equals(organization.getName(),
369                     organizationModelImpl.getOriginalName()))) {
370             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
371                 new Object[] {
372                     new Long(organization.getCompanyId()),
373                     
374                 organization.getName()
375                 }, organization);
376         }
377 
378         return organization;
379     }
380 
381     protected Organization toUnwrappedModel(Organization organization) {
382         if (organization instanceof OrganizationImpl) {
383             return organization;
384         }
385 
386         OrganizationImpl organizationImpl = new OrganizationImpl();
387 
388         organizationImpl.setNew(organization.isNew());
389         organizationImpl.setPrimaryKey(organization.getPrimaryKey());
390 
391         organizationImpl.setOrganizationId(organization.getOrganizationId());
392         organizationImpl.setCompanyId(organization.getCompanyId());
393         organizationImpl.setParentOrganizationId(organization.getParentOrganizationId());
394         organizationImpl.setLeftOrganizationId(organization.getLeftOrganizationId());
395         organizationImpl.setRightOrganizationId(organization.getRightOrganizationId());
396         organizationImpl.setName(organization.getName());
397         organizationImpl.setLocation(organization.isLocation());
398         organizationImpl.setRecursable(organization.isRecursable());
399         organizationImpl.setRegionId(organization.getRegionId());
400         organizationImpl.setCountryId(organization.getCountryId());
401         organizationImpl.setStatusId(organization.getStatusId());
402         organizationImpl.setComments(organization.getComments());
403 
404         return organizationImpl;
405     }
406 
407     public Organization findByPrimaryKey(Serializable primaryKey)
408         throws NoSuchModelException, SystemException {
409         return findByPrimaryKey(((Long)primaryKey).longValue());
410     }
411 
412     public Organization findByPrimaryKey(long organizationId)
413         throws NoSuchOrganizationException, SystemException {
414         Organization organization = fetchByPrimaryKey(organizationId);
415 
416         if (organization == null) {
417             if (_log.isWarnEnabled()) {
418                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + organizationId);
419             }
420 
421             throw new NoSuchOrganizationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
422                 organizationId);
423         }
424 
425         return organization;
426     }
427 
428     public Organization fetchByPrimaryKey(Serializable primaryKey)
429         throws SystemException {
430         return fetchByPrimaryKey(((Long)primaryKey).longValue());
431     }
432 
433     public Organization fetchByPrimaryKey(long organizationId)
434         throws SystemException {
435         Organization organization = (Organization)EntityCacheUtil.getResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
436                 OrganizationImpl.class, organizationId, this);
437 
438         if (organization == null) {
439             Session session = null;
440 
441             try {
442                 session = openSession();
443 
444                 organization = (Organization)session.get(OrganizationImpl.class,
445                         new Long(organizationId));
446             }
447             catch (Exception e) {
448                 throw processException(e);
449             }
450             finally {
451                 if (organization != null) {
452                     cacheResult(organization);
453                 }
454 
455                 closeSession(session);
456             }
457         }
458 
459         return organization;
460     }
461 
462     public List<Organization> findByCompanyId(long companyId)
463         throws SystemException {
464         Object[] finderArgs = new Object[] { new Long(companyId) };
465 
466         List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
467                 finderArgs, this);
468 
469         if (list == null) {
470             Session session = null;
471 
472             try {
473                 session = openSession();
474 
475                 StringBundler query = new StringBundler(3);
476 
477                 query.append(_SQL_SELECT_ORGANIZATION_WHERE);
478 
479                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
480 
481                 query.append(OrganizationModelImpl.ORDER_BY_JPQL);
482 
483                 String sql = query.toString();
484 
485                 Query q = session.createQuery(sql);
486 
487                 QueryPos qPos = QueryPos.getInstance(q);
488 
489                 qPos.add(companyId);
490 
491                 list = q.list();
492             }
493             catch (Exception e) {
494                 throw processException(e);
495             }
496             finally {
497                 if (list == null) {
498                     list = new ArrayList<Organization>();
499                 }
500 
501                 cacheResult(list);
502 
503                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
504                     finderArgs, list);
505 
506                 closeSession(session);
507             }
508         }
509 
510         return list;
511     }
512 
513     public List<Organization> findByCompanyId(long companyId, int start, int end)
514         throws SystemException {
515         return findByCompanyId(companyId, start, end, null);
516     }
517 
518     public List<Organization> findByCompanyId(long companyId, int start,
519         int end, OrderByComparator obc) throws SystemException {
520         Object[] finderArgs = new Object[] {
521                 new Long(companyId),
522                 
523                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
524             };
525 
526         List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
527                 finderArgs, this);
528 
529         if (list == null) {
530             Session session = null;
531 
532             try {
533                 session = openSession();
534 
535                 StringBundler query = null;
536 
537                 if (obc != null) {
538                     query = new StringBundler(3 +
539                             (obc.getOrderByFields().length * 3));
540                 }
541                 else {
542                     query = new StringBundler(3);
543                 }
544 
545                 query.append(_SQL_SELECT_ORGANIZATION_WHERE);
546 
547                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
548 
549                 if (obc != null) {
550                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
551                 }
552 
553                 else {
554                     query.append(OrganizationModelImpl.ORDER_BY_JPQL);
555                 }
556 
557                 String sql = query.toString();
558 
559                 Query q = session.createQuery(sql);
560 
561                 QueryPos qPos = QueryPos.getInstance(q);
562 
563                 qPos.add(companyId);
564 
565                 list = (List<Organization>)QueryUtil.list(q, getDialect(),
566                         start, end);
567             }
568             catch (Exception e) {
569                 throw processException(e);
570             }
571             finally {
572                 if (list == null) {
573                     list = new ArrayList<Organization>();
574                 }
575 
576                 cacheResult(list);
577 
578                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
579                     finderArgs, list);
580 
581                 closeSession(session);
582             }
583         }
584 
585         return list;
586     }
587 
588     public Organization findByCompanyId_First(long companyId,
589         OrderByComparator obc)
590         throws NoSuchOrganizationException, SystemException {
591         List<Organization> list = findByCompanyId(companyId, 0, 1, obc);
592 
593         if (list.isEmpty()) {
594             StringBundler msg = new StringBundler(4);
595 
596             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
597 
598             msg.append("companyId=");
599             msg.append(companyId);
600 
601             msg.append(StringPool.CLOSE_CURLY_BRACE);
602 
603             throw new NoSuchOrganizationException(msg.toString());
604         }
605         else {
606             return list.get(0);
607         }
608     }
609 
610     public Organization findByCompanyId_Last(long companyId,
611         OrderByComparator obc)
612         throws NoSuchOrganizationException, SystemException {
613         int count = countByCompanyId(companyId);
614 
615         List<Organization> list = findByCompanyId(companyId, count - 1, count,
616                 obc);
617 
618         if (list.isEmpty()) {
619             StringBundler msg = new StringBundler(4);
620 
621             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
622 
623             msg.append("companyId=");
624             msg.append(companyId);
625 
626             msg.append(StringPool.CLOSE_CURLY_BRACE);
627 
628             throw new NoSuchOrganizationException(msg.toString());
629         }
630         else {
631             return list.get(0);
632         }
633     }
634 
635     public Organization[] findByCompanyId_PrevAndNext(long organizationId,
636         long companyId, OrderByComparator obc)
637         throws NoSuchOrganizationException, SystemException {
638         Organization organization = findByPrimaryKey(organizationId);
639 
640         int count = countByCompanyId(companyId);
641 
642         Session session = null;
643 
644         try {
645             session = openSession();
646 
647             StringBundler query = null;
648 
649             if (obc != null) {
650                 query = new StringBundler(3 +
651                         (obc.getOrderByFields().length * 3));
652             }
653             else {
654                 query = new StringBundler(3);
655             }
656 
657             query.append(_SQL_SELECT_ORGANIZATION_WHERE);
658 
659             query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
660 
661             if (obc != null) {
662                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
663             }
664 
665             else {
666                 query.append(OrganizationModelImpl.ORDER_BY_JPQL);
667             }
668 
669             String sql = query.toString();
670 
671             Query q = session.createQuery(sql);
672 
673             QueryPos qPos = QueryPos.getInstance(q);
674 
675             qPos.add(companyId);
676 
677             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
678                     organization);
679 
680             Organization[] array = new OrganizationImpl[3];
681 
682             array[0] = (Organization)objArray[0];
683             array[1] = (Organization)objArray[1];
684             array[2] = (Organization)objArray[2];
685 
686             return array;
687         }
688         catch (Exception e) {
689             throw processException(e);
690         }
691         finally {
692             closeSession(session);
693         }
694     }
695 
696     public List<Organization> findByLocations(long companyId)
697         throws SystemException {
698         Object[] finderArgs = new Object[] { new Long(companyId) };
699 
700         List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_LOCATIONS,
701                 finderArgs, this);
702 
703         if (list == null) {
704             Session session = null;
705 
706             try {
707                 session = openSession();
708 
709                 StringBundler query = new StringBundler(3);
710 
711                 query.append(_SQL_SELECT_ORGANIZATION_WHERE);
712 
713                 query.append(_FINDER_COLUMN_LOCATIONS_COMPANYID_2);
714 
715                 query.append(OrganizationModelImpl.ORDER_BY_JPQL);
716 
717                 String sql = query.toString();
718 
719                 Query q = session.createQuery(sql);
720 
721                 QueryPos qPos = QueryPos.getInstance(q);
722 
723                 qPos.add(companyId);
724 
725                 list = q.list();
726             }
727             catch (Exception e) {
728                 throw processException(e);
729             }
730             finally {
731                 if (list == null) {
732                     list = new ArrayList<Organization>();
733                 }
734 
735                 cacheResult(list);
736 
737                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_LOCATIONS,
738                     finderArgs, list);
739 
740                 closeSession(session);
741             }
742         }
743 
744         return list;
745     }
746 
747     public List<Organization> findByLocations(long companyId, int start, int end)
748         throws SystemException {
749         return findByLocations(companyId, start, end, null);
750     }
751 
752     public List<Organization> findByLocations(long companyId, int start,
753         int end, OrderByComparator obc) throws SystemException {
754         Object[] finderArgs = new Object[] {
755                 new Long(companyId),
756                 
757                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
758             };
759 
760         List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_LOCATIONS,
761                 finderArgs, this);
762 
763         if (list == null) {
764             Session session = null;
765 
766             try {
767                 session = openSession();
768 
769                 StringBundler query = null;
770 
771                 if (obc != null) {
772                     query = new StringBundler(3 +
773                             (obc.getOrderByFields().length * 3));
774                 }
775                 else {
776                     query = new StringBundler(3);
777                 }
778 
779                 query.append(_SQL_SELECT_ORGANIZATION_WHERE);
780 
781                 query.append(_FINDER_COLUMN_LOCATIONS_COMPANYID_2);
782 
783                 if (obc != null) {
784                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
785                 }
786 
787                 else {
788                     query.append(OrganizationModelImpl.ORDER_BY_JPQL);
789                 }
790 
791                 String sql = query.toString();
792 
793                 Query q = session.createQuery(sql);
794 
795                 QueryPos qPos = QueryPos.getInstance(q);
796 
797                 qPos.add(companyId);
798 
799                 list = (List<Organization>)QueryUtil.list(q, getDialect(),
800                         start, end);
801             }
802             catch (Exception e) {
803                 throw processException(e);
804             }
805             finally {
806                 if (list == null) {
807                     list = new ArrayList<Organization>();
808                 }
809 
810                 cacheResult(list);
811 
812                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_LOCATIONS,
813                     finderArgs, list);
814 
815                 closeSession(session);
816             }
817         }
818 
819         return list;
820     }
821 
822     public Organization findByLocations_First(long companyId,
823         OrderByComparator obc)
824         throws NoSuchOrganizationException, SystemException {
825         List<Organization> list = findByLocations(companyId, 0, 1, obc);
826 
827         if (list.isEmpty()) {
828             StringBundler msg = new StringBundler(4);
829 
830             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
831 
832             msg.append("companyId=");
833             msg.append(companyId);
834 
835             msg.append(StringPool.CLOSE_CURLY_BRACE);
836 
837             throw new NoSuchOrganizationException(msg.toString());
838         }
839         else {
840             return list.get(0);
841         }
842     }
843 
844     public Organization findByLocations_Last(long companyId,
845         OrderByComparator obc)
846         throws NoSuchOrganizationException, SystemException {
847         int count = countByLocations(companyId);
848 
849         List<Organization> list = findByLocations(companyId, count - 1, count,
850                 obc);
851 
852         if (list.isEmpty()) {
853             StringBundler msg = new StringBundler(4);
854 
855             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
856 
857             msg.append("companyId=");
858             msg.append(companyId);
859 
860             msg.append(StringPool.CLOSE_CURLY_BRACE);
861 
862             throw new NoSuchOrganizationException(msg.toString());
863         }
864         else {
865             return list.get(0);
866         }
867     }
868 
869     public Organization[] findByLocations_PrevAndNext(long organizationId,
870         long companyId, OrderByComparator obc)
871         throws NoSuchOrganizationException, SystemException {
872         Organization organization = findByPrimaryKey(organizationId);
873 
874         int count = countByLocations(companyId);
875 
876         Session session = null;
877 
878         try {
879             session = openSession();
880 
881             StringBundler query = null;
882 
883             if (obc != null) {
884                 query = new StringBundler(3 +
885                         (obc.getOrderByFields().length * 3));
886             }
887             else {
888                 query = new StringBundler(3);
889             }
890 
891             query.append(_SQL_SELECT_ORGANIZATION_WHERE);
892 
893             query.append(_FINDER_COLUMN_LOCATIONS_COMPANYID_2);
894 
895             if (obc != null) {
896                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
897             }
898 
899             else {
900                 query.append(OrganizationModelImpl.ORDER_BY_JPQL);
901             }
902 
903             String sql = query.toString();
904 
905             Query q = session.createQuery(sql);
906 
907             QueryPos qPos = QueryPos.getInstance(q);
908 
909             qPos.add(companyId);
910 
911             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
912                     organization);
913 
914             Organization[] array = new OrganizationImpl[3];
915 
916             array[0] = (Organization)objArray[0];
917             array[1] = (Organization)objArray[1];
918             array[2] = (Organization)objArray[2];
919 
920             return array;
921         }
922         catch (Exception e) {
923             throw processException(e);
924         }
925         finally {
926             closeSession(session);
927         }
928     }
929 
930     public List<Organization> findByC_P(long companyId,
931         long parentOrganizationId) throws SystemException {
932         Object[] finderArgs = new Object[] {
933                 new Long(companyId), new Long(parentOrganizationId)
934             };
935 
936         List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_P,
937                 finderArgs, this);
938 
939         if (list == null) {
940             Session session = null;
941 
942             try {
943                 session = openSession();
944 
945                 StringBundler query = new StringBundler(4);
946 
947                 query.append(_SQL_SELECT_ORGANIZATION_WHERE);
948 
949                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
950 
951                 query.append(_FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2);
952 
953                 query.append(OrganizationModelImpl.ORDER_BY_JPQL);
954 
955                 String sql = query.toString();
956 
957                 Query q = session.createQuery(sql);
958 
959                 QueryPos qPos = QueryPos.getInstance(q);
960 
961                 qPos.add(companyId);
962 
963                 qPos.add(parentOrganizationId);
964 
965                 list = q.list();
966             }
967             catch (Exception e) {
968                 throw processException(e);
969             }
970             finally {
971                 if (list == null) {
972                     list = new ArrayList<Organization>();
973                 }
974 
975                 cacheResult(list);
976 
977                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_P, finderArgs,
978                     list);
979 
980                 closeSession(session);
981             }
982         }
983 
984         return list;
985     }
986 
987     public List<Organization> findByC_P(long companyId,
988         long parentOrganizationId, int start, int end)
989         throws SystemException {
990         return findByC_P(companyId, parentOrganizationId, start, end, null);
991     }
992 
993     public List<Organization> findByC_P(long companyId,
994         long parentOrganizationId, int start, int end, OrderByComparator obc)
995         throws SystemException {
996         Object[] finderArgs = new Object[] {
997                 new Long(companyId), new Long(parentOrganizationId),
998                 
999                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1000            };
1001
1002        List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_C_P,
1003                finderArgs, this);
1004
1005        if (list == null) {
1006            Session session = null;
1007
1008            try {
1009                session = openSession();
1010
1011                StringBundler query = null;
1012
1013                if (obc != null) {
1014                    query = new StringBundler(4 +
1015                            (obc.getOrderByFields().length * 3));
1016                }
1017                else {
1018                    query = new StringBundler(4);
1019                }
1020
1021                query.append(_SQL_SELECT_ORGANIZATION_WHERE);
1022
1023                query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1024
1025                query.append(_FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2);
1026
1027                if (obc != null) {
1028                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
1029                }
1030
1031                else {
1032                    query.append(OrganizationModelImpl.ORDER_BY_JPQL);
1033                }
1034
1035                String sql = query.toString();
1036
1037                Query q = session.createQuery(sql);
1038
1039                QueryPos qPos = QueryPos.getInstance(q);
1040
1041                qPos.add(companyId);
1042
1043                qPos.add(parentOrganizationId);
1044
1045                list = (List<Organization>)QueryUtil.list(q, getDialect(),
1046                        start, end);
1047            }
1048            catch (Exception e) {
1049                throw processException(e);
1050            }
1051            finally {
1052                if (list == null) {
1053                    list = new ArrayList<Organization>();
1054                }
1055
1056                cacheResult(list);
1057
1058                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_C_P,
1059                    finderArgs, list);
1060
1061                closeSession(session);
1062            }
1063        }
1064
1065        return list;
1066    }
1067
1068    public Organization findByC_P_First(long companyId,
1069        long parentOrganizationId, OrderByComparator obc)
1070        throws NoSuchOrganizationException, SystemException {
1071        List<Organization> list = findByC_P(companyId, parentOrganizationId, 0,
1072                1, obc);
1073
1074        if (list.isEmpty()) {
1075            StringBundler msg = new StringBundler(6);
1076
1077            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1078
1079            msg.append("companyId=");
1080            msg.append(companyId);
1081
1082            msg.append(", parentOrganizationId=");
1083            msg.append(parentOrganizationId);
1084
1085            msg.append(StringPool.CLOSE_CURLY_BRACE);
1086
1087            throw new NoSuchOrganizationException(msg.toString());
1088        }
1089        else {
1090            return list.get(0);
1091        }
1092    }
1093
1094    public Organization findByC_P_Last(long companyId,
1095        long parentOrganizationId, OrderByComparator obc)
1096        throws NoSuchOrganizationException, SystemException {
1097        int count = countByC_P(companyId, parentOrganizationId);
1098
1099        List<Organization> list = findByC_P(companyId, parentOrganizationId,
1100                count - 1, count, obc);
1101
1102        if (list.isEmpty()) {
1103            StringBundler msg = new StringBundler(6);
1104
1105            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1106
1107            msg.append("companyId=");
1108            msg.append(companyId);
1109
1110            msg.append(", parentOrganizationId=");
1111            msg.append(parentOrganizationId);
1112
1113            msg.append(StringPool.CLOSE_CURLY_BRACE);
1114
1115            throw new NoSuchOrganizationException(msg.toString());
1116        }
1117        else {
1118            return list.get(0);
1119        }
1120    }
1121
1122    public Organization[] findByC_P_PrevAndNext(long organizationId,
1123        long companyId, long parentOrganizationId, OrderByComparator obc)
1124        throws NoSuchOrganizationException, SystemException {
1125        Organization organization = findByPrimaryKey(organizationId);
1126
1127        int count = countByC_P(companyId, parentOrganizationId);
1128
1129        Session session = null;
1130
1131        try {
1132            session = openSession();
1133
1134            StringBundler query = null;
1135
1136            if (obc != null) {
1137                query = new StringBundler(4 +
1138                        (obc.getOrderByFields().length * 3));
1139            }
1140            else {
1141                query = new StringBundler(4);
1142            }
1143
1144            query.append(_SQL_SELECT_ORGANIZATION_WHERE);
1145
1146            query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1147
1148            query.append(_FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2);
1149
1150            if (obc != null) {
1151                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
1152            }
1153
1154            else {
1155                query.append(OrganizationModelImpl.ORDER_BY_JPQL);
1156            }
1157
1158            String sql = query.toString();
1159
1160            Query q = session.createQuery(sql);
1161
1162            QueryPos qPos = QueryPos.getInstance(q);
1163
1164            qPos.add(companyId);
1165
1166            qPos.add(parentOrganizationId);
1167
1168            Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
1169                    organization);
1170
1171            Organization[] array = new OrganizationImpl[3];
1172
1173            array[0] = (Organization)objArray[0];
1174            array[1] = (Organization)objArray[1];
1175            array[2] = (Organization)objArray[2];
1176
1177            return array;
1178        }
1179        catch (Exception e) {
1180            throw processException(e);
1181        }
1182        finally {
1183            closeSession(session);
1184        }
1185    }
1186
1187    public Organization findByC_N(long companyId, String name)
1188        throws NoSuchOrganizationException, SystemException {
1189        Organization organization = fetchByC_N(companyId, name);
1190
1191        if (organization == null) {
1192            StringBundler msg = new StringBundler(6);
1193
1194            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1195
1196            msg.append("companyId=");
1197            msg.append(companyId);
1198
1199            msg.append(", name=");
1200            msg.append(name);
1201
1202            msg.append(StringPool.CLOSE_CURLY_BRACE);
1203
1204            if (_log.isWarnEnabled()) {
1205                _log.warn(msg.toString());
1206            }
1207
1208            throw new NoSuchOrganizationException(msg.toString());
1209        }
1210
1211        return organization;
1212    }
1213
1214    public Organization fetchByC_N(long companyId, String name)
1215        throws SystemException {
1216        return fetchByC_N(companyId, name, true);
1217    }
1218
1219    public Organization fetchByC_N(long companyId, String name,
1220        boolean retrieveFromCache) throws SystemException {
1221        Object[] finderArgs = new Object[] { new Long(companyId), name };
1222
1223        Object result = null;
1224
1225        if (retrieveFromCache) {
1226            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N,
1227                    finderArgs, this);
1228        }
1229
1230        if (result == null) {
1231            Session session = null;
1232
1233            try {
1234                session = openSession();
1235
1236                StringBundler query = new StringBundler(4);
1237
1238                query.append(_SQL_SELECT_ORGANIZATION_WHERE);
1239
1240                query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
1241
1242                if (name == null) {
1243                    query.append(_FINDER_COLUMN_C_N_NAME_1);
1244                }
1245                else {
1246                    if (name.equals(StringPool.BLANK)) {
1247                        query.append(_FINDER_COLUMN_C_N_NAME_3);
1248                    }
1249                    else {
1250                        query.append(_FINDER_COLUMN_C_N_NAME_2);
1251                    }
1252                }
1253
1254                query.append(OrganizationModelImpl.ORDER_BY_JPQL);
1255
1256                String sql = query.toString();
1257
1258                Query q = session.createQuery(sql);
1259
1260                QueryPos qPos = QueryPos.getInstance(q);
1261
1262                qPos.add(companyId);
1263
1264                if (name != null) {
1265                    qPos.add(name);
1266                }
1267
1268                List<Organization> list = q.list();
1269
1270                result = list;
1271
1272                Organization organization = null;
1273
1274                if (list.isEmpty()) {
1275                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1276                        finderArgs, list);
1277                }
1278                else {
1279                    organization = list.get(0);
1280
1281                    cacheResult(organization);
1282
1283                    if ((organization.getCompanyId() != companyId) ||
1284                            (organization.getName() == null) ||
1285                            !organization.getName().equals(name)) {
1286                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1287                            finderArgs, organization);
1288                    }
1289                }
1290
1291                return organization;
1292            }
1293            catch (Exception e) {
1294                throw processException(e);
1295            }
1296            finally {
1297                if (result == null) {
1298                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1299                        finderArgs, new ArrayList<Organization>());
1300                }
1301
1302                closeSession(session);
1303            }
1304        }
1305        else {
1306            if (result instanceof List<?>) {
1307                return null;
1308            }
1309            else {
1310                return (Organization)result;
1311            }
1312        }
1313    }
1314
1315    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1316        throws SystemException {
1317        Session session = null;
1318
1319        try {
1320            session = openSession();
1321
1322            dynamicQuery.compile(session);
1323
1324            return dynamicQuery.list();
1325        }
1326        catch (Exception e) {
1327            throw processException(e);
1328        }
1329        finally {
1330            closeSession(session);
1331        }
1332    }
1333
1334    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1335        int start, int end) throws SystemException {
1336        Session session = null;
1337
1338        try {
1339            session = openSession();
1340
1341            dynamicQuery.setLimit(start, end);
1342
1343            dynamicQuery.compile(session);
1344
1345            return dynamicQuery.list();
1346        }
1347        catch (Exception e) {
1348            throw processException(e);
1349        }
1350        finally {
1351            closeSession(session);
1352        }
1353    }
1354
1355    public List<Organization> findAll() throws SystemException {
1356        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1357    }
1358
1359    public List<Organization> findAll(int start, int end)
1360        throws SystemException {
1361        return findAll(start, end, null);
1362    }
1363
1364    public List<Organization> findAll(int start, int end, OrderByComparator obc)
1365        throws SystemException {
1366        Object[] finderArgs = new Object[] {
1367                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1368            };
1369
1370        List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1371                finderArgs, this);
1372
1373        if (list == null) {
1374            Session session = null;
1375
1376            try {
1377                session = openSession();
1378
1379                StringBundler query = null;
1380                String sql = null;
1381
1382                if (obc != null) {
1383                    query = new StringBundler(2 +
1384                            (obc.getOrderByFields().length * 3));
1385
1386                    query.append(_SQL_SELECT_ORGANIZATION);
1387
1388                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
1389
1390                    sql = query.toString();
1391                }
1392
1393                else {
1394                    sql = _SQL_SELECT_ORGANIZATION.concat(OrganizationModelImpl.ORDER_BY_JPQL);
1395                }
1396
1397                Query q = session.createQuery(sql);
1398
1399                if (obc == null) {
1400                    list = (List<Organization>)QueryUtil.list(q, getDialect(),
1401                            start, end, false);
1402
1403                    Collections.sort(list);
1404                }
1405                else {
1406                    list = (List<Organization>)QueryUtil.list(q, getDialect(),
1407                            start, end);
1408                }
1409            }
1410            catch (Exception e) {
1411                throw processException(e);
1412            }
1413            finally {
1414                if (list == null) {
1415                    list = new ArrayList<Organization>();
1416                }
1417
1418                cacheResult(list);
1419
1420                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1421
1422                closeSession(session);
1423            }
1424        }
1425
1426        return list;
1427    }
1428
1429    public void removeByCompanyId(long companyId) throws SystemException {
1430        for (Organization organization : findByCompanyId(companyId)) {
1431            remove(organization);
1432        }
1433    }
1434
1435    public void removeByLocations(long companyId) throws SystemException {
1436        for (Organization organization : findByLocations(companyId)) {
1437            remove(organization);
1438        }
1439    }
1440
1441    public void removeByC_P(long companyId, long parentOrganizationId)
1442        throws SystemException {
1443        for (Organization organization : findByC_P(companyId,
1444                parentOrganizationId)) {
1445            remove(organization);
1446        }
1447    }
1448
1449    public void removeByC_N(long companyId, String name)
1450        throws NoSuchOrganizationException, SystemException {
1451        Organization organization = findByC_N(companyId, name);
1452
1453        remove(organization);
1454    }
1455
1456    public void removeAll() throws SystemException {
1457        for (Organization organization : findAll()) {
1458            remove(organization);
1459        }
1460    }
1461
1462    public int countByCompanyId(long companyId) throws SystemException {
1463        Object[] finderArgs = new Object[] { new Long(companyId) };
1464
1465        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
1466                finderArgs, this);
1467
1468        if (count == null) {
1469            Session session = null;
1470
1471            try {
1472                session = openSession();
1473
1474                StringBundler query = new StringBundler(2);
1475
1476                query.append(_SQL_COUNT_ORGANIZATION_WHERE);
1477
1478                query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1479
1480                String sql = query.toString();
1481
1482                Query q = session.createQuery(sql);
1483
1484                QueryPos qPos = QueryPos.getInstance(q);
1485
1486                qPos.add(companyId);
1487
1488                count = (Long)q.uniqueResult();
1489            }
1490            catch (Exception e) {
1491                throw processException(e);
1492            }
1493            finally {
1494                if (count == null) {
1495                    count = Long.valueOf(0);
1496                }
1497
1498                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
1499                    finderArgs, count);
1500
1501                closeSession(session);
1502            }
1503        }
1504
1505        return count.intValue();
1506    }
1507
1508    public int countByLocations(long companyId) throws SystemException {
1509        Object[] finderArgs = new Object[] { new Long(companyId) };
1510
1511        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_LOCATIONS,
1512                finderArgs, this);
1513
1514        if (count == null) {
1515            Session session = null;
1516
1517            try {
1518                session = openSession();
1519
1520                StringBundler query = new StringBundler(2);
1521
1522                query.append(_SQL_COUNT_ORGANIZATION_WHERE);
1523
1524                query.append(_FINDER_COLUMN_LOCATIONS_COMPANYID_2);
1525
1526                String sql = query.toString();
1527
1528                Query q = session.createQuery(sql);
1529
1530                QueryPos qPos = QueryPos.getInstance(q);
1531
1532                qPos.add(companyId);
1533
1534                count = (Long)q.uniqueResult();
1535            }
1536            catch (Exception e) {
1537                throw processException(e);
1538            }
1539            finally {
1540                if (count == null) {
1541                    count = Long.valueOf(0);
1542                }
1543
1544                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_LOCATIONS,
1545                    finderArgs, count);
1546
1547                closeSession(session);
1548            }
1549        }
1550
1551        return count.intValue();
1552    }
1553
1554    public int countByC_P(long companyId, long parentOrganizationId)
1555        throws SystemException {
1556        Object[] finderArgs = new Object[] {
1557                new Long(companyId), new Long(parentOrganizationId)
1558            };
1559
1560        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
1561                finderArgs, this);
1562
1563        if (count == null) {
1564            Session session = null;
1565
1566            try {
1567                session = openSession();
1568
1569                StringBundler query = new StringBundler(3);
1570
1571                query.append(_SQL_COUNT_ORGANIZATION_WHERE);
1572
1573                query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1574
1575                query.append(_FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2);
1576
1577                String sql = query.toString();
1578
1579                Query q = session.createQuery(sql);
1580
1581                QueryPos qPos = QueryPos.getInstance(q);
1582
1583                qPos.add(companyId);
1584
1585                qPos.add(parentOrganizationId);
1586
1587                count = (Long)q.uniqueResult();
1588            }
1589            catch (Exception e) {
1590                throw processException(e);
1591            }
1592            finally {
1593                if (count == null) {
1594                    count = Long.valueOf(0);
1595                }
1596
1597                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
1598                    count);
1599
1600                closeSession(session);
1601            }
1602        }
1603
1604        return count.intValue();
1605    }
1606
1607    public int countByC_N(long companyId, String name)
1608        throws SystemException {
1609        Object[] finderArgs = new Object[] { new Long(companyId), name };
1610
1611        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_N,
1612                finderArgs, this);
1613
1614        if (count == null) {
1615            Session session = null;
1616
1617            try {
1618                session = openSession();
1619
1620                StringBundler query = new StringBundler(3);
1621
1622                query.append(_SQL_COUNT_ORGANIZATION_WHERE);
1623
1624                query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
1625
1626                if (name == null) {
1627                    query.append(_FINDER_COLUMN_C_N_NAME_1);
1628                }
1629                else {
1630                    if (name.equals(StringPool.BLANK)) {
1631                        query.append(_FINDER_COLUMN_C_N_NAME_3);
1632                    }
1633                    else {
1634                        query.append(_FINDER_COLUMN_C_N_NAME_2);
1635                    }
1636                }
1637
1638                String sql = query.toString();
1639
1640                Query q = session.createQuery(sql);
1641
1642                QueryPos qPos = QueryPos.getInstance(q);
1643
1644                qPos.add(companyId);
1645
1646                if (name != null) {
1647                    qPos.add(name);
1648                }
1649
1650                count = (Long)q.uniqueResult();
1651            }
1652            catch (Exception e) {
1653                throw processException(e);
1654            }
1655            finally {
1656                if (count == null) {
1657                    count = Long.valueOf(0);
1658                }
1659
1660                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N, finderArgs,
1661                    count);
1662
1663                closeSession(session);
1664            }
1665        }
1666
1667        return count.intValue();
1668    }
1669
1670    public int countAll() throws SystemException {
1671        Object[] finderArgs = new Object[0];
1672
1673        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1674                finderArgs, this);
1675
1676        if (count == null) {
1677            Session session = null;
1678
1679            try {
1680                session = openSession();
1681
1682                Query q = session.createQuery(_SQL_COUNT_ORGANIZATION);
1683
1684                count = (Long)q.uniqueResult();
1685            }
1686            catch (Exception e) {
1687                throw processException(e);
1688            }
1689            finally {
1690                if (count == null) {
1691                    count = Long.valueOf(0);
1692                }
1693
1694                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1695                    count);
1696
1697                closeSession(session);
1698            }
1699        }
1700
1701        return count.intValue();
1702    }
1703
1704    public List<com.liferay.portal.model.Group> getGroups(long pk)
1705        throws SystemException {
1706        return getGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1707    }
1708
1709    public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
1710        int end) throws SystemException {
1711        return getGroups(pk, start, end, null);
1712    }
1713
1714    public static final FinderPath FINDER_PATH_GET_GROUPS = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1715            OrganizationModelImpl.FINDER_CACHE_ENABLED_GROUPS_ORGS,
1716            OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME, "getGroups",
1717            new String[] {
1718                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1719                "com.liferay.portal.kernel.util.OrderByComparator"
1720            });
1721
1722    public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
1723        int end, OrderByComparator obc) throws SystemException {
1724        Object[] finderArgs = new Object[] {
1725                new Long(pk), String.valueOf(start), String.valueOf(end),
1726                String.valueOf(obc)
1727            };
1728
1729        List<com.liferay.portal.model.Group> list = (List<com.liferay.portal.model.Group>)FinderCacheUtil.getResult(FINDER_PATH_GET_GROUPS,
1730                finderArgs, this);
1731
1732        if (list == null) {
1733            Session session = null;
1734
1735            try {
1736                session = openSession();
1737
1738                String sql = null;
1739
1740                if (obc != null) {
1741                    sql = _SQL_GETGROUPS.concat(ORDER_BY_CLAUSE)
1742                                        .concat(obc.getOrderBy());
1743                }
1744
1745                else {
1746                    sql = _SQL_GETGROUPS.concat(com.liferay.portal.model.impl.GroupModelImpl.ORDER_BY_SQL);
1747                }
1748
1749                SQLQuery q = session.createSQLQuery(sql);
1750
1751                q.addEntity("Group_",
1752                    com.liferay.portal.model.impl.GroupImpl.class);
1753
1754                QueryPos qPos = QueryPos.getInstance(q);
1755
1756                qPos.add(pk);
1757
1758                list = (List<com.liferay.portal.model.Group>)QueryUtil.list(q,
1759                        getDialect(), start, end);
1760            }
1761            catch (Exception e) {
1762                throw processException(e);
1763            }
1764            finally {
1765                if (list == null) {
1766                    list = new ArrayList<com.liferay.portal.model.Group>();
1767                }
1768
1769                groupPersistence.cacheResult(list);
1770
1771                FinderCacheUtil.putResult(FINDER_PATH_GET_GROUPS, finderArgs,
1772                    list);
1773
1774                closeSession(session);
1775            }
1776        }
1777
1778        return list;
1779    }
1780
1781    public static final FinderPath FINDER_PATH_GET_GROUPS_SIZE = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1782            OrganizationModelImpl.FINDER_CACHE_ENABLED_GROUPS_ORGS,
1783            OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME,
1784            "getGroupsSize", new String[] { Long.class.getName() });
1785
1786    public int getGroupsSize(long pk) throws SystemException {
1787        Object[] finderArgs = new Object[] { new Long(pk) };
1788
1789        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_GROUPS_SIZE,
1790                finderArgs, this);
1791
1792        if (count == null) {
1793            Session session = null;
1794
1795            try {
1796                session = openSession();
1797
1798                SQLQuery q = session.createSQLQuery(_SQL_GETGROUPSSIZE);
1799
1800                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1801
1802                QueryPos qPos = QueryPos.getInstance(q);
1803
1804                qPos.add(pk);
1805
1806                count = (Long)q.uniqueResult();
1807            }
1808            catch (Exception e) {
1809                throw processException(e);
1810            }
1811            finally {
1812                if (count == null) {
1813                    count = Long.valueOf(0);
1814                }
1815
1816                FinderCacheUtil.putResult(FINDER_PATH_GET_GROUPS_SIZE,
1817                    finderArgs, count);
1818
1819                closeSession(session);
1820            }
1821        }
1822
1823        return count.intValue();
1824    }
1825
1826    public static final FinderPath FINDER_PATH_CONTAINS_GROUP = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1827            OrganizationModelImpl.FINDER_CACHE_ENABLED_GROUPS_ORGS,
1828            OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME,
1829            "containsGroup",
1830            new String[] { Long.class.getName(), Long.class.getName() });
1831
1832    public boolean containsGroup(long pk, long groupPK)
1833        throws SystemException {
1834        Object[] finderArgs = new Object[] { new Long(pk), new Long(groupPK) };
1835
1836        Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_GROUP,
1837                finderArgs, this);
1838
1839        if (value == null) {
1840            try {
1841                value = Boolean.valueOf(containsGroup.contains(pk, groupPK));
1842            }
1843            catch (Exception e) {
1844                throw processException(e);
1845            }
1846            finally {
1847                if (value == null) {
1848                    value = Boolean.FALSE;
1849                }
1850
1851                FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_GROUP,
1852                    finderArgs, value);
1853            }
1854        }
1855
1856        return value.booleanValue();
1857    }
1858
1859    public boolean containsGroups(long pk) throws SystemException {
1860        if (getGroupsSize(pk) > 0) {
1861            return true;
1862        }
1863        else {
1864            return false;
1865        }
1866    }
1867
1868    public void addGroup(long pk, long groupPK) throws SystemException {
1869        try {
1870            addGroup.add(pk, groupPK);
1871        }
1872        catch (Exception e) {
1873            throw processException(e);
1874        }
1875        finally {
1876            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1877        }
1878    }
1879
1880    public void addGroup(long pk, com.liferay.portal.model.Group group)
1881        throws SystemException {
1882        try {
1883            addGroup.add(pk, group.getPrimaryKey());
1884        }
1885        catch (Exception e) {
1886            throw processException(e);
1887        }
1888        finally {
1889            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1890        }
1891    }
1892
1893    public void addGroups(long pk, long[] groupPKs) throws SystemException {
1894        try {
1895            for (long groupPK : groupPKs) {
1896                addGroup.add(pk, groupPK);
1897            }
1898        }
1899        catch (Exception e) {
1900            throw processException(e);
1901        }
1902        finally {
1903            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1904        }
1905    }
1906
1907    public void addGroups(long pk, List<com.liferay.portal.model.Group> groups)
1908        throws SystemException {
1909        try {
1910            for (com.liferay.portal.model.Group group : groups) {
1911                addGroup.add(pk, group.getPrimaryKey());
1912            }
1913        }
1914        catch (Exception e) {
1915            throw processException(e);
1916        }
1917        finally {
1918            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1919        }
1920    }
1921
1922    public void clearGroups(long pk) throws SystemException {
1923        try {
1924            clearGroups.clear(pk);
1925        }
1926        catch (Exception e) {
1927            throw processException(e);
1928        }
1929        finally {
1930            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1931        }
1932    }
1933
1934    public void removeGroup(long pk, long groupPK) throws SystemException {
1935        try {
1936            removeGroup.remove(pk, groupPK);
1937        }
1938        catch (Exception e) {
1939            throw processException(e);
1940        }
1941        finally {
1942            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1943        }
1944    }
1945
1946    public void removeGroup(long pk, com.liferay.portal.model.Group group)
1947        throws SystemException {
1948        try {
1949            removeGroup.remove(pk, group.getPrimaryKey());
1950        }
1951        catch (Exception e) {
1952            throw processException(e);
1953        }
1954        finally {
1955            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1956        }
1957    }
1958
1959    public void removeGroups(long pk, long[] groupPKs)
1960        throws SystemException {
1961        try {
1962            for (long groupPK : groupPKs) {
1963                removeGroup.remove(pk, groupPK);
1964            }
1965        }
1966        catch (Exception e) {
1967            throw processException(e);
1968        }
1969        finally {
1970            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1971        }
1972    }
1973
1974    public void removeGroups(long pk,
1975        List<com.liferay.portal.model.Group> groups) throws SystemException {
1976        try {
1977            for (com.liferay.portal.model.Group group : groups) {
1978                removeGroup.remove(pk, group.getPrimaryKey());
1979            }
1980        }
1981        catch (Exception e) {
1982            throw processException(e);
1983        }
1984        finally {
1985            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
1986        }
1987    }
1988
1989    public void setGroups(long pk, long[] groupPKs) throws SystemException {
1990        try {
1991            Set<Long> groupPKSet = SetUtil.fromArray(groupPKs);
1992
1993            List<com.liferay.portal.model.Group> groups = getGroups(pk);
1994
1995            for (com.liferay.portal.model.Group group : groups) {
1996                if (!groupPKSet.contains(group.getPrimaryKey())) {
1997                    removeGroup.remove(pk, group.getPrimaryKey());
1998                }
1999                else {
2000                    groupPKSet.remove(group.getPrimaryKey());
2001                }
2002            }
2003
2004            for (Long groupPK : groupPKSet) {
2005                addGroup.add(pk, groupPK);
2006            }
2007        }
2008        catch (Exception e) {
2009            throw processException(e);
2010        }
2011        finally {
2012            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2013        }
2014    }
2015
2016    public void setGroups(long pk, List<com.liferay.portal.model.Group> groups)
2017        throws SystemException {
2018        try {
2019            long[] groupPKs = new long[groups.size()];
2020
2021            for (int i = 0; i < groups.size(); i++) {
2022                com.liferay.portal.model.Group group = groups.get(i);
2023
2024                groupPKs[i] = group.getPrimaryKey();
2025            }
2026
2027            setGroups(pk, groupPKs);
2028        }
2029        catch (Exception e) {
2030            throw processException(e);
2031        }
2032        finally {
2033            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2034        }
2035    }
2036
2037    public List<com.liferay.portal.model.User> getUsers(long pk)
2038        throws SystemException {
2039        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
2040    }
2041
2042    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
2043        int end) throws SystemException {
2044        return getUsers(pk, start, end, null);
2045    }
2046
2047    public static final FinderPath FINDER_PATH_GET_USERS = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2048            OrganizationModelImpl.FINDER_CACHE_ENABLED_USERS_ORGS,
2049            OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME, "getUsers",
2050            new String[] {
2051                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
2052                "com.liferay.portal.kernel.util.OrderByComparator"
2053            });
2054
2055    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
2056        int end, OrderByComparator obc) throws SystemException {
2057        Object[] finderArgs = new Object[] {
2058                new Long(pk), String.valueOf(start), String.valueOf(end),
2059                String.valueOf(obc)
2060            };
2061
2062        List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS,
2063                finderArgs, this);
2064
2065        if (list == null) {
2066            Session session = null;
2067
2068            try {
2069                session = openSession();
2070
2071                String sql = null;
2072
2073                if (obc != null) {
2074                    sql = _SQL_GETUSERS.concat(ORDER_BY_CLAUSE)
2075                                       .concat(obc.getOrderBy());
2076                }
2077
2078                sql = _SQL_GETUSERS;
2079
2080                SQLQuery q = session.createSQLQuery(sql);
2081
2082                q.addEntity("User_",
2083                    com.liferay.portal.model.impl.UserImpl.class);
2084
2085                QueryPos qPos = QueryPos.getInstance(q);
2086
2087                qPos.add(pk);
2088
2089                list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
2090                        getDialect(), start, end);
2091            }
2092            catch (Exception e) {
2093                throw processException(e);
2094            }
2095            finally {
2096                if (list == null) {
2097                    list = new ArrayList<com.liferay.portal.model.User>();
2098                }
2099
2100                userPersistence.cacheResult(list);
2101
2102                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS, finderArgs,
2103                    list);
2104
2105                closeSession(session);
2106            }
2107        }
2108
2109        return list;
2110    }
2111
2112    public static final FinderPath FINDER_PATH_GET_USERS_SIZE = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2113            OrganizationModelImpl.FINDER_CACHE_ENABLED_USERS_ORGS,
2114            OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME,
2115            "getUsersSize", new String[] { Long.class.getName() });
2116
2117    public int getUsersSize(long pk) throws SystemException {
2118        Object[] finderArgs = new Object[] { new Long(pk) };
2119
2120        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS_SIZE,
2121                finderArgs, this);
2122
2123        if (count == null) {
2124            Session session = null;
2125
2126            try {
2127                session = openSession();
2128
2129                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
2130
2131                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
2132
2133                QueryPos qPos = QueryPos.getInstance(q);
2134
2135                qPos.add(pk);
2136
2137                count = (Long)q.uniqueResult();
2138            }
2139            catch (Exception e) {
2140                throw processException(e);
2141            }
2142            finally {
2143                if (count == null) {
2144                    count = Long.valueOf(0);
2145                }
2146
2147                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS_SIZE,
2148                    finderArgs, count);
2149
2150                closeSession(session);
2151            }
2152        }
2153
2154        return count.intValue();
2155    }
2156
2157    public static final FinderPath FINDER_PATH_CONTAINS_USER = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2158            OrganizationModelImpl.FINDER_CACHE_ENABLED_USERS_ORGS,
2159            OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME,
2160            "containsUser",
2161            new String[] { Long.class.getName(), Long.class.getName() });
2162
2163    public boolean containsUser(long pk, long userPK) throws SystemException {
2164        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
2165
2166        Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_USER,
2167                finderArgs, this);
2168
2169        if (value == null) {
2170            try {
2171                value = Boolean.valueOf(containsUser.contains(pk, userPK));
2172            }
2173            catch (Exception e) {
2174                throw processException(e);
2175            }
2176            finally {
2177                if (value == null) {
2178                    value = Boolean.FALSE;
2179                }
2180
2181                FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_USER,
2182                    finderArgs, value);
2183            }
2184        }
2185
2186        return value.booleanValue();
2187    }
2188
2189    public boolean containsUsers(long pk) throws SystemException {
2190        if (getUsersSize(pk) > 0) {
2191            return true;
2192        }
2193        else {
2194            return false;
2195        }
2196    }
2197
2198    public void addUser(long pk, long userPK) throws SystemException {
2199        try {
2200            addUser.add(pk, userPK);
2201        }
2202        catch (Exception e) {
2203            throw processException(e);
2204        }
2205        finally {
2206            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2207        }
2208    }
2209
2210    public void addUser(long pk, com.liferay.portal.model.User user)
2211        throws SystemException {
2212        try {
2213            addUser.add(pk, user.getPrimaryKey());
2214        }
2215        catch (Exception e) {
2216            throw processException(e);
2217        }
2218        finally {
2219            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2220        }
2221    }
2222
2223    public void addUsers(long pk, long[] userPKs) throws SystemException {
2224        try {
2225            for (long userPK : userPKs) {
2226                addUser.add(pk, userPK);
2227            }
2228        }
2229        catch (Exception e) {
2230            throw processException(e);
2231        }
2232        finally {
2233            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2234        }
2235    }
2236
2237    public void addUsers(long pk, List<com.liferay.portal.model.User> users)
2238        throws SystemException {
2239        try {
2240            for (com.liferay.portal.model.User user : users) {
2241                addUser.add(pk, user.getPrimaryKey());
2242            }
2243        }
2244        catch (Exception e) {
2245            throw processException(e);
2246        }
2247        finally {
2248            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2249        }
2250    }
2251
2252    public void clearUsers(long pk) throws SystemException {
2253        try {
2254            clearUsers.clear(pk);
2255        }
2256        catch (Exception e) {
2257            throw processException(e);
2258        }
2259        finally {
2260            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2261        }
2262    }
2263
2264    public void removeUser(long pk, long userPK) throws SystemException {
2265        try {
2266            removeUser.remove(pk, userPK);
2267        }
2268        catch (Exception e) {
2269            throw processException(e);
2270        }
2271        finally {
2272            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2273        }
2274    }
2275
2276    public void removeUser(long pk, com.liferay.portal.model.User user)
2277        throws SystemException {
2278        try {
2279            removeUser.remove(pk, user.getPrimaryKey());
2280        }
2281        catch (Exception e) {
2282            throw processException(e);
2283        }
2284        finally {
2285            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2286        }
2287    }
2288
2289    public void removeUsers(long pk, long[] userPKs) throws SystemException {
2290        try {
2291            for (long userPK : userPKs) {
2292                removeUser.remove(pk, userPK);
2293            }
2294        }
2295        catch (Exception e) {
2296            throw processException(e);
2297        }
2298        finally {
2299            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2300        }
2301    }
2302
2303    public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
2304        throws SystemException {
2305        try {
2306            for (com.liferay.portal.model.User user : users) {
2307                removeUser.remove(pk, user.getPrimaryKey());
2308            }
2309        }
2310        catch (Exception e) {
2311            throw processException(e);
2312        }
2313        finally {
2314            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2315        }
2316    }
2317
2318    public void setUsers(long pk, long[] userPKs) throws SystemException {
2319        try {
2320            Set<Long> userPKSet = SetUtil.fromArray(userPKs);
2321
2322            List<com.liferay.portal.model.User> users = getUsers(pk);
2323
2324            for (com.liferay.portal.model.User user : users) {
2325                if (!userPKSet.contains(user.getPrimaryKey())) {
2326                    removeUser.remove(pk, user.getPrimaryKey());
2327                }
2328                else {
2329                    userPKSet.remove(user.getPrimaryKey());
2330                }
2331            }
2332
2333            for (Long userPK : userPKSet) {
2334                addUser.add(pk, userPK);
2335            }
2336        }
2337        catch (Exception e) {
2338            throw processException(e);
2339        }
2340        finally {
2341            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2342        }
2343    }
2344
2345    public void setUsers(long pk, List<com.liferay.portal.model.User> users)
2346        throws SystemException {
2347        try {
2348            long[] userPKs = new long[users.size()];
2349
2350            for (int i = 0; i < users.size(); i++) {
2351                com.liferay.portal.model.User user = users.get(i);
2352
2353                userPKs[i] = user.getPrimaryKey();
2354            }
2355
2356            setUsers(pk, userPKs);
2357        }
2358        catch (Exception e) {
2359            throw processException(e);
2360        }
2361        finally {
2362            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2363        }
2364    }
2365
2366    public void rebuildTree(long companyId, boolean force)
2367        throws SystemException {
2368        if (force || (countOrphanTreeNodes(companyId) > 0)) {
2369            rebuildTree(companyId, 0, 1);
2370
2371            CacheRegistry.clear(OrganizationImpl.class.getName());
2372            EntityCacheUtil.clearCache(OrganizationImpl.class.getName());
2373            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
2374            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
2375        }
2376    }
2377
2378    protected long countOrphanTreeNodes(long companyId)
2379        throws SystemException {
2380        Session session = null;
2381
2382        try {
2383            session = openSession();
2384
2385            SQLQuery q = session.createSQLQuery(
2386                    "SELECT COUNT(*) AS COUNT_VALUE FROM Organization_ WHERE companyId = ? AND (leftOrganizationId = 0 OR leftOrganizationId IS NULL OR rightOrganizationId = 0 OR rightOrganizationId IS NULL)");
2387
2388            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
2389
2390            QueryPos qPos = QueryPos.getInstance(q);
2391
2392            qPos.add(companyId);
2393
2394            return (Long)q.uniqueResult();
2395        }
2396        catch (Exception e) {
2397            throw processException(e);
2398        }
2399        finally {
2400            closeSession(session);
2401        }
2402    }
2403
2404    protected void expandTree(Organization organization)
2405        throws SystemException {
2406        long companyId = organization.getCompanyId();
2407
2408        long lastRightOrganizationId = getLastRightOrganizationId(companyId,
2409                organization.getParentOrganizationId());
2410
2411        long leftOrganizationId = 2;
2412        long rightOrganizationId = 3;
2413
2414        if (lastRightOrganizationId > 0) {
2415            leftOrganizationId = lastRightOrganizationId + 1;
2416            rightOrganizationId = lastRightOrganizationId + 2;
2417
2418            expandTreeLeftOrganizationId.expand(companyId,
2419                lastRightOrganizationId);
2420            expandTreeRightOrganizationId.expand(companyId,
2421                lastRightOrganizationId);
2422
2423            CacheRegistry.clear(OrganizationImpl.class.getName());
2424            EntityCacheUtil.clearCache(OrganizationImpl.class.getName());
2425            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
2426            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
2427        }
2428
2429        organization.setLeftOrganizationId(leftOrganizationId);
2430        organization.setRightOrganizationId(rightOrganizationId);
2431    }
2432
2433    protected long getLastRightOrganizationId(long companyId,
2434        long parentOrganizationId) throws SystemException {
2435        Session session = null;
2436
2437        try {
2438            session = openSession();
2439
2440            SQLQuery q = session.createSQLQuery(
2441                    "SELECT rightOrganizationId FROM Organization_ WHERE (companyId = ?) AND (parentOrganizationId = ?) ORDER BY rightOrganizationId DESC");
2442
2443            q.addScalar("rightOrganizationId", Type.LONG);
2444
2445            QueryPos qPos = QueryPos.getInstance(q);
2446
2447            qPos.add(companyId);
2448            qPos.add(parentOrganizationId);
2449
2450            List<Long> list = (List<Long>)QueryUtil.list(q, getDialect(), 0, 1);
2451
2452            if (list.isEmpty()) {
2453                if (parentOrganizationId > 0) {
2454                    Organization parentOrganization = findByPrimaryKey(parentOrganizationId);
2455
2456                    return parentOrganization.getLeftOrganizationId();
2457                }
2458
2459                return 0;
2460            }
2461            else {
2462                return list.get(0);
2463            }
2464        }
2465        catch (Exception e) {
2466            throw processException(e);
2467        }
2468        finally {
2469            closeSession(session);
2470        }
2471    }
2472
2473    protected long rebuildTree(long companyId, long parentOrganizationId,
2474        long leftOrganizationId) throws SystemException {
2475        List<Long> organizationIds = null;
2476
2477        Session session = null;
2478
2479        try {
2480            session = openSession();
2481
2482            SQLQuery q = session.createSQLQuery(
2483                    "SELECT organizationId FROM Organization_ WHERE companyId = ? AND parentOrganizationId = ? ORDER BY organizationId ASC");
2484
2485            q.addScalar("organizationId", Type.LONG);
2486
2487            QueryPos qPos = QueryPos.getInstance(q);
2488
2489            qPos.add(companyId);
2490            qPos.add(parentOrganizationId);
2491
2492            organizationIds = q.list();
2493        }
2494        catch (Exception e) {
2495            throw processException(e);
2496        }
2497        finally {
2498            closeSession(session);
2499        }
2500
2501        long rightOrganizationId = leftOrganizationId + 1;
2502
2503        for (long organizationId : organizationIds) {
2504            rightOrganizationId = rebuildTree(companyId, organizationId,
2505                    rightOrganizationId);
2506        }
2507
2508        if (parentOrganizationId > 0) {
2509            updateTree.update(parentOrganizationId, leftOrganizationId,
2510                rightOrganizationId);
2511        }
2512
2513        return rightOrganizationId + 1;
2514    }
2515
2516    protected void shrinkTree(Organization organization) {
2517        long companyId = organization.getCompanyId();
2518
2519        long leftOrganizationId = organization.getLeftOrganizationId();
2520        long rightOrganizationId = organization.getRightOrganizationId();
2521
2522        long delta = (rightOrganizationId - leftOrganizationId) + 1;
2523
2524        shrinkTreeLeftOrganizationId.shrink(companyId, rightOrganizationId,
2525            delta);
2526        shrinkTreeRightOrganizationId.shrink(companyId, rightOrganizationId,
2527            delta);
2528
2529        CacheRegistry.clear(OrganizationImpl.class.getName());
2530        EntityCacheUtil.clearCache(OrganizationImpl.class.getName());
2531        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
2532        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
2533    }
2534
2535    public void afterPropertiesSet() {
2536        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2537                    com.liferay.portal.util.PropsUtil.get(
2538                        "value.object.listener.com.liferay.portal.model.Organization")));
2539
2540        if (listenerClassNames.length > 0) {
2541            try {
2542                List<ModelListener<Organization>> listenersList = new ArrayList<ModelListener<Organization>>();
2543
2544                for (String listenerClassName : listenerClassNames) {
2545                    listenersList.add((ModelListener<Organization>)Class.forName(
2546                            listenerClassName).newInstance());
2547                }
2548
2549                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2550            }
2551            catch (Exception e) {
2552                _log.error(e);
2553            }
2554        }
2555
2556        containsGroup = new ContainsGroup(this);
2557
2558        addGroup = new AddGroup(this);
2559        clearGroups = new ClearGroups(this);
2560        removeGroup = new RemoveGroup(this);
2561
2562        containsUser = new ContainsUser(this);
2563
2564        addUser = new AddUser(this);
2565        clearUsers = new ClearUsers(this);
2566        removeUser = new RemoveUser(this);
2567
2568        expandTreeLeftOrganizationId = new ExpandTreeLeftOrganizationId();
2569        expandTreeRightOrganizationId = new ExpandTreeRightOrganizationId();
2570        shrinkTreeLeftOrganizationId = new ShrinkTreeLeftOrganizationId();
2571        shrinkTreeRightOrganizationId = new ShrinkTreeRightOrganizationId();
2572        updateTree = new UpdateTree();
2573    }
2574
2575    @BeanReference(name = "com.liferay.portal.service.persistence.AccountPersistence")
2576    protected com.liferay.portal.service.persistence.AccountPersistence accountPersistence;
2577    @BeanReference(name = "com.liferay.portal.service.persistence.AddressPersistence")
2578    protected com.liferay.portal.service.persistence.AddressPersistence addressPersistence;
2579    @BeanReference(name = "com.liferay.portal.service.persistence.BrowserTrackerPersistence")
2580    protected com.liferay.portal.service.persistence.BrowserTrackerPersistence browserTrackerPersistence;
2581    @BeanReference(name = "com.liferay.portal.service.persistence.ClassNamePersistence")
2582    protected com.liferay.portal.service.persistence.ClassNamePersistence classNamePersistence;
2583    @BeanReference(name = "com.liferay.portal.service.persistence.CompanyPersistence")
2584    protected com.liferay.portal.service.persistence.CompanyPersistence companyPersistence;
2585    @BeanReference(name = "com.liferay.portal.service.persistence.ContactPersistence")
2586    protected com.liferay.portal.service.persistence.ContactPersistence contactPersistence;
2587    @BeanReference(name = "com.liferay.portal.service.persistence.CountryPersistence")
2588    protected com.liferay.portal.service.persistence.CountryPersistence countryPersistence;
2589    @BeanReference(name = "com.liferay.portal.service.persistence.EmailAddressPersistence")
2590    protected com.liferay.portal.service.persistence.EmailAddressPersistence emailAddressPersistence;
2591    @BeanReference(name = "com.liferay.portal.service.persistence.GroupPersistence")
2592    protected com.liferay.portal.service.persistence.GroupPersistence groupPersistence;
2593    @BeanReference(name = "com.liferay.portal.service.persistence.ImagePersistence")
2594    protected com.liferay.portal.service.persistence.ImagePersistence imagePersistence;
2595    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutPersistence")
2596    protected com.liferay.portal.service.persistence.LayoutPersistence layoutPersistence;
2597    @BeanReference(name = "com.liferay.portal.service.persistence.LayoutSetPersistence")
2598    protected com.liferay.portal.service.persistence.LayoutSetPersistence layoutSetPersistence;
2599    @BeanReference(name = "com.liferay.portal.service.persistence.ListTypePersistence")
2600    protected com.liferay.portal.service.persistence.ListTypePersistence listTypePersistence;
2601    @BeanReference(name = "com.liferay.portal.service.persistence.LockPersistence")
2602    protected com.liferay.portal.service.persistence.LockPersistence lockPersistence;
2603    @BeanReference(name = "com.liferay.portal.service.persistence.MembershipRequestPersistence")
2604    protected com.liferay.portal.service.persistence.MembershipRequestPersistence membershipRequestPersistence;
2605    @BeanReference(name = "com.liferay.portal.service.persistence.OrganizationPersistence")
2606    protected com.liferay.portal.service.persistence.OrganizationPersistence organizationPersistence;
2607    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupPermissionPersistence")
2608    protected com.liferay.portal.service.persistence.OrgGroupPermissionPersistence orgGroupPermissionPersistence;
2609    @BeanReference(name = "com.liferay.portal.service.persistence.OrgGroupRolePersistence")
2610    protected com.liferay.portal.service.persistence.OrgGroupRolePersistence orgGroupRolePersistence;
2611    @BeanReference(name = "com.liferay.portal.service.persistence.OrgLaborPersistence")
2612    protected com.liferay.portal.service.persistence.OrgLaborPersistence orgLaborPersistence;
2613    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyPersistence")
2614    protected com.liferay.portal.service.persistence.PasswordPolicyPersistence passwordPolicyPersistence;
2615    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordPolicyRelPersistence")
2616    protected com.liferay.portal.service.persistence.PasswordPolicyRelPersistence passwordPolicyRelPersistence;
2617    @BeanReference(name = "com.liferay.portal.service.persistence.PasswordTrackerPersistence")
2618    protected com.liferay.portal.service.persistence.PasswordTrackerPersistence passwordTrackerPersistence;
2619    @BeanReference(name = "com.liferay.portal.service.persistence.PermissionPersistence")
2620    protected com.liferay.portal.service.persistence.PermissionPersistence permissionPersistence;
2621    @BeanReference(name = "com.liferay.portal.service.persistence.PhonePersistence")
2622    protected com.liferay.portal.service.persistence.PhonePersistence phonePersistence;
2623    @BeanReference(name = "com.liferay.portal.service.persistence.PluginSettingPersistence")
2624    protected com.liferay.portal.service.persistence.PluginSettingPersistence pluginSettingPersistence;
2625    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPersistence")
2626    protected com.liferay.portal.service.persistence.PortletPersistence portletPersistence;
2627    @BeanReference(name = "com.liferay.portal.service.persistence.PortletItemPersistence")
2628    protected com.liferay.portal.service.persistence.PortletItemPersistence portletItemPersistence;
2629    @BeanReference(name = "com.liferay.portal.service.persistence.PortletPreferencesPersistence")
2630    protected com.liferay.portal.service.persistence.PortletPreferencesPersistence portletPreferencesPersistence;
2631    @BeanReference(name = "com.liferay.portal.service.persistence.RegionPersistence")
2632    protected com.liferay.portal.service.persistence.RegionPersistence regionPersistence;
2633    @BeanReference(name = "com.liferay.portal.service.persistence.ReleasePersistence")
2634    protected com.liferay.portal.service.persistence.ReleasePersistence releasePersistence;
2635    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence")
2636    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
2637    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceActionPersistence")
2638    protected com.liferay.portal.service.persistence.ResourceActionPersistence resourceActionPersistence;
2639    @BeanReference(name = "com.liferay.portal.service.persistence.ResourceCodePersistence")
2640    protected com.liferay.portal.service.persistence.ResourceCodePersistence resourceCodePersistence;
2641    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePermissionPersistence")
2642    protected com.liferay.portal.service.persistence.ResourcePermissionPersistence resourcePermissionPersistence;
2643    @BeanReference(name = "com.liferay.portal.service.persistence.RolePersistence")
2644    protected com.liferay.portal.service.persistence.RolePersistence rolePersistence;
2645    @BeanReference(name = "com.liferay.portal.service.persistence.ServiceComponentPersistence")
2646    protected com.liferay.portal.service.persistence.ServiceComponentPersistence serviceComponentPersistence;
2647    @BeanReference(name = "com.liferay.portal.service.persistence.ShardPersistence")
2648    protected com.liferay.portal.service.persistence.ShardPersistence shardPersistence;
2649    @BeanReference(name = "com.liferay.portal.service.persistence.SubscriptionPersistence")
2650    protected com.liferay.portal.service.persistence.SubscriptionPersistence subscriptionPersistence;
2651    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence")
2652    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
2653    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupPersistence")
2654    protected com.liferay.portal.service.persistence.UserGroupPersistence userGroupPersistence;
2655    @BeanReference(name = "com.liferay.portal.service.persistence.UserGroupRolePersistence")
2656    protected com.liferay.portal.service.persistence.UserGroupRolePersistence userGroupRolePersistence;
2657    @BeanReference(name = "com.liferay.portal.service.persistence.UserIdMapperPersistence")
2658    protected com.liferay.portal.service.persistence.UserIdMapperPersistence userIdMapperPersistence;
2659    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPersistence")
2660    protected com.liferay.portal.service.persistence.UserTrackerPersistence userTrackerPersistence;
2661    @BeanReference(name = "com.liferay.portal.service.persistence.UserTrackerPathPersistence")
2662    protected com.liferay.portal.service.persistence.UserTrackerPathPersistence userTrackerPathPersistence;
2663    @BeanReference(name = "com.liferay.portal.service.persistence.WebDAVPropsPersistence")
2664    protected com.liferay.portal.service.persistence.WebDAVPropsPersistence webDAVPropsPersistence;
2665    @BeanReference(name = "com.liferay.portal.service.persistence.WebsitePersistence")
2666    protected com.liferay.portal.service.persistence.WebsitePersistence websitePersistence;
2667    protected ContainsGroup containsGroup;
2668    protected AddGroup addGroup;
2669    protected ClearGroups clearGroups;
2670    protected RemoveGroup removeGroup;
2671    protected ContainsUser containsUser;
2672    protected AddUser addUser;
2673    protected ClearUsers clearUsers;
2674    protected RemoveUser removeUser;
2675
2676    protected class ContainsGroup {
2677        protected ContainsGroup(OrganizationPersistenceImpl persistenceImpl) {
2678            super();
2679
2680            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2681                    _SQL_CONTAINSGROUP,
2682                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2683        }
2684
2685        protected boolean contains(long organizationId, long groupId) {
2686            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2687                        new Long(organizationId), new Long(groupId)
2688                    });
2689
2690            if (results.size() > 0) {
2691                Integer count = results.get(0);
2692
2693                if (count.intValue() > 0) {
2694                    return true;
2695                }
2696            }
2697
2698            return false;
2699        }
2700
2701        private MappingSqlQuery _mappingSqlQuery;
2702    }
2703
2704    protected class AddGroup {
2705        protected AddGroup(OrganizationPersistenceImpl persistenceImpl) {
2706            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2707                    "INSERT INTO Groups_Orgs (organizationId, groupId) VALUES (?, ?)",
2708                    new int[] { Types.BIGINT, Types.BIGINT });
2709            _persistenceImpl = persistenceImpl;
2710        }
2711
2712        protected void add(long organizationId, long groupId) {
2713            if (!_persistenceImpl.containsGroup.contains(organizationId, groupId)) {
2714                _sqlUpdate.update(new Object[] {
2715                        new Long(organizationId), new Long(groupId)
2716                    });
2717            }
2718        }
2719
2720        private SqlUpdate _sqlUpdate;
2721        private OrganizationPersistenceImpl _persistenceImpl;
2722    }
2723
2724    protected class ClearGroups {
2725        protected ClearGroups(OrganizationPersistenceImpl persistenceImpl) {
2726            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2727                    "DELETE FROM Groups_Orgs WHERE organizationId = ?",
2728                    new int[] { Types.BIGINT });
2729        }
2730
2731        protected void clear(long organizationId) {
2732            _sqlUpdate.update(new Object[] { new Long(organizationId) });
2733        }
2734
2735        private SqlUpdate _sqlUpdate;
2736    }
2737
2738    protected class RemoveGroup {
2739        protected RemoveGroup(OrganizationPersistenceImpl persistenceImpl) {
2740            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2741                    "DELETE FROM Groups_Orgs WHERE organizationId = ? AND groupId = ?",
2742                    new int[] { Types.BIGINT, Types.BIGINT });
2743        }
2744
2745        protected void remove(long organizationId, long groupId) {
2746            _sqlUpdate.update(new Object[] {
2747                    new Long(organizationId), new Long(groupId)
2748                });
2749        }
2750
2751        private SqlUpdate _sqlUpdate;
2752    }
2753
2754    protected class ContainsUser {
2755        protected ContainsUser(OrganizationPersistenceImpl persistenceImpl) {
2756            super();
2757
2758            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2759                    _SQL_CONTAINSUSER,
2760                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2761        }
2762
2763        protected boolean contains(long organizationId, long userId) {
2764            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2765                        new Long(organizationId), new Long(userId)
2766                    });
2767
2768            if (results.size() > 0) {
2769                Integer count = results.get(0);
2770
2771                if (count.intValue() > 0) {
2772                    return true;
2773                }
2774            }
2775
2776            return false;
2777        }
2778
2779        private MappingSqlQuery _mappingSqlQuery;
2780    }
2781
2782    protected class AddUser {
2783        protected AddUser(OrganizationPersistenceImpl persistenceImpl) {
2784            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2785                    "INSERT INTO Users_Orgs (organizationId, userId) VALUES (?, ?)",
2786                    new int[] { Types.BIGINT, Types.BIGINT });
2787            _persistenceImpl = persistenceImpl;
2788        }
2789
2790        protected void add(long organizationId, long userId) {
2791            if (!_persistenceImpl.containsUser.contains(organizationId, userId)) {
2792                _sqlUpdate.update(new Object[] {
2793                        new Long(organizationId), new Long(userId)
2794                    });
2795            }
2796        }
2797
2798        private SqlUpdate _sqlUpdate;
2799        private OrganizationPersistenceImpl _persistenceImpl;
2800    }
2801
2802    protected class ClearUsers {
2803        protected ClearUsers(OrganizationPersistenceImpl persistenceImpl) {
2804            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2805                    "DELETE FROM Users_Orgs WHERE organizationId = ?",
2806                    new int[] { Types.BIGINT });
2807        }
2808
2809        protected void clear(long organizationId) {
2810            _sqlUpdate.update(new Object[] { new Long(organizationId) });
2811        }
2812
2813        private SqlUpdate _sqlUpdate;
2814    }
2815
2816    protected class RemoveUser {
2817        protected RemoveUser(OrganizationPersistenceImpl persistenceImpl) {
2818            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2819                    "DELETE FROM Users_Orgs WHERE organizationId = ? AND userId = ?",
2820                    new int[] { Types.BIGINT, Types.BIGINT });
2821        }
2822
2823        protected void remove(long organizationId, long userId) {
2824            _sqlUpdate.update(new Object[] {
2825                    new Long(organizationId), new Long(userId)
2826                });
2827        }
2828
2829        private SqlUpdate _sqlUpdate;
2830    }
2831
2832    protected ExpandTreeLeftOrganizationId expandTreeLeftOrganizationId;
2833    protected ExpandTreeRightOrganizationId expandTreeRightOrganizationId;
2834    protected ShrinkTreeLeftOrganizationId shrinkTreeLeftOrganizationId;
2835    protected ShrinkTreeRightOrganizationId shrinkTreeRightOrganizationId;
2836    protected UpdateTree updateTree;
2837
2838    protected class ExpandTreeLeftOrganizationId {
2839        protected ExpandTreeLeftOrganizationId() {
2840            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2841                    "UPDATE Organization_ SET leftOrganizationId = (leftOrganizationId + 2) WHERE (companyId = ?) AND (leftOrganizationId > ?)",
2842                    new int[] { Types.BIGINT, Types.BIGINT });
2843        }
2844
2845        protected void expand(long companyId, long leftOrganizationId) {
2846            _sqlUpdate.update(new Object[] { companyId, leftOrganizationId });
2847        }
2848
2849        private SqlUpdate _sqlUpdate;
2850    }
2851
2852    protected class ExpandTreeRightOrganizationId {
2853        protected ExpandTreeRightOrganizationId() {
2854            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2855                    "UPDATE Organization_ SET rightOrganizationId = (rightOrganizationId + 2) WHERE (companyId = ?) AND (rightOrganizationId > ?)",
2856                    new int[] { Types.BIGINT, Types.BIGINT });
2857        }
2858
2859        protected void expand(long companyId, long rightOrganizationId) {
2860            _sqlUpdate.update(new Object[] { companyId, rightOrganizationId });
2861        }
2862
2863        private SqlUpdate _sqlUpdate;
2864    }
2865
2866    protected class ShrinkTreeLeftOrganizationId {
2867        protected ShrinkTreeLeftOrganizationId() {
2868            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2869                    "UPDATE Organization_ SET leftOrganizationId = (leftOrganizationId - ?) WHERE (companyId = ?) AND (leftOrganizationId > ?)",
2870                    new int[] { Types.BIGINT, Types.BIGINT, Types.BIGINT });
2871        }
2872
2873        protected void shrink(long companyId, long leftOrganizationId,
2874            long delta) {
2875            _sqlUpdate.update(new Object[] { delta, companyId, leftOrganizationId });
2876        }
2877
2878        private SqlUpdate _sqlUpdate;
2879    }
2880
2881    protected class ShrinkTreeRightOrganizationId {
2882        protected ShrinkTreeRightOrganizationId() {
2883            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2884                    "UPDATE Organization_ SET rightOrganizationId = (rightOrganizationId - ?) WHERE (companyId = ?) AND (rightOrganizationId > ?)",
2885                    new int[] { Types.BIGINT, Types.BIGINT, Types.BIGINT });
2886        }
2887
2888        protected void shrink(long companyId, long rightOrganizationId,
2889            long delta) {
2890            _sqlUpdate.update(new Object[] { delta, companyId, rightOrganizationId });
2891        }
2892
2893        private SqlUpdate _sqlUpdate;
2894    }
2895
2896    protected class UpdateTree {
2897        protected UpdateTree() {
2898            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2899                    "UPDATE Organization_ SET leftOrganizationId = ?, rightOrganizationId = ? WHERE organizationId = ?",
2900                    new int[] { Types.BIGINT, Types.BIGINT, Types.BIGINT });
2901        }
2902
2903        protected void update(long organizationId, long leftOrganizationId,
2904            long rightOrganizationId) {
2905            _sqlUpdate.update(new Object[] {
2906                    leftOrganizationId, rightOrganizationId, organizationId
2907                });
2908        }
2909
2910        private SqlUpdate _sqlUpdate;
2911    }
2912
2913    private static final String _SQL_SELECT_ORGANIZATION = "SELECT organization FROM Organization organization";
2914    private static final String _SQL_SELECT_ORGANIZATION_WHERE = "SELECT organization FROM Organization organization WHERE ";
2915    private static final String _SQL_COUNT_ORGANIZATION = "SELECT COUNT(organization) FROM Organization organization";
2916    private static final String _SQL_COUNT_ORGANIZATION_WHERE = "SELECT COUNT(organization) FROM Organization organization WHERE ";
2917    private static final String _SQL_GETGROUPS = "SELECT {Group_.*} FROM Group_ INNER JOIN Groups_Orgs ON (Groups_Orgs.groupId = Group_.groupId) WHERE (Groups_Orgs.organizationId = ?)";
2918    private static final String _SQL_GETGROUPSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Orgs WHERE organizationId = ?";
2919    private static final String _SQL_CONTAINSGROUP = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Orgs WHERE organizationId = ? AND groupId = ?";
2920    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_Orgs ON (Users_Orgs.userId = User_.userId) WHERE (Users_Orgs.organizationId = ?)";
2921    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Orgs WHERE organizationId = ?";
2922    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Orgs WHERE organizationId = ? AND userId = ?";
2923    private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "organization.companyId = ?";
2924    private static final String _FINDER_COLUMN_LOCATIONS_COMPANYID_2 = "organization.companyId = ? AND organization.parentOrganizationId != 0";
2925    private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "organization.companyId = ? AND ";
2926    private static final String _FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2 = "organization.parentOrganizationId = ?";
2927    private static final String _FINDER_COLUMN_C_N_COMPANYID_2 = "organization.companyId = ? AND ";
2928    private static final String _FINDER_COLUMN_C_N_NAME_1 = "organization.name IS NULL";
2929    private static final String _FINDER_COLUMN_C_N_NAME_2 = "organization.name = ?";
2930    private static final String _FINDER_COLUMN_C_N_NAME_3 = "(organization.name IS NULL OR organization.name = ?)";
2931    private static final String _ORDER_BY_ENTITY_ALIAS = "organization.";
2932    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Organization exists with the primary key ";
2933    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Organization exists with the key {";
2934    private static Log _log = LogFactoryUtil.getLog(OrganizationPersistenceImpl.class);
2935}