001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.NoSuchOrganizationException;
019    import com.liferay.portal.kernel.annotation.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
022    import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
023    import com.liferay.portal.kernel.dao.jdbc.RowMapper;
024    import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
025    import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
026    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
027    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
028    import com.liferay.portal.kernel.dao.orm.FinderPath;
029    import com.liferay.portal.kernel.dao.orm.Query;
030    import com.liferay.portal.kernel.dao.orm.QueryPos;
031    import com.liferay.portal.kernel.dao.orm.QueryUtil;
032    import com.liferay.portal.kernel.dao.orm.SQLQuery;
033    import com.liferay.portal.kernel.dao.orm.Session;
034    import com.liferay.portal.kernel.exception.SystemException;
035    import com.liferay.portal.kernel.log.Log;
036    import com.liferay.portal.kernel.log.LogFactoryUtil;
037    import com.liferay.portal.kernel.util.GetterUtil;
038    import com.liferay.portal.kernel.util.InstanceFactory;
039    import com.liferay.portal.kernel.util.OrderByComparator;
040    import com.liferay.portal.kernel.util.SetUtil;
041    import com.liferay.portal.kernel.util.StringBundler;
042    import com.liferay.portal.kernel.util.StringPool;
043    import com.liferay.portal.kernel.util.StringUtil;
044    import com.liferay.portal.kernel.util.Validator;
045    import com.liferay.portal.model.ModelListener;
046    import com.liferay.portal.model.Organization;
047    import com.liferay.portal.model.impl.OrganizationImpl;
048    import com.liferay.portal.model.impl.OrganizationModelImpl;
049    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
050    
051    import com.liferay.portlet.asset.service.persistence.AssetEntryPersistence;
052    import com.liferay.portlet.expando.service.persistence.ExpandoValuePersistence;
053    
054    import java.io.Serializable;
055    
056    import java.util.ArrayList;
057    import java.util.Collections;
058    import java.util.List;
059    import java.util.Set;
060    
061    /**
062     * The persistence implementation for the organization service.
063     *
064     * <p>
065     * Never modify or reference this class directly. Always use {@link OrganizationUtil} to access the organization persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
066     * </p>
067     *
068     * <p>
069     * Caching information and settings can be found in <code>portal.properties</code>
070     * </p>
071     *
072     * @author Brian Wing Shun Chan
073     * @see OrganizationPersistence
074     * @see OrganizationUtil
075     * @generated
076     */
077    public class OrganizationPersistenceImpl extends BasePersistenceImpl<Organization>
078            implements OrganizationPersistence {
079            public static final String FINDER_CLASS_NAME_ENTITY = OrganizationImpl.class.getName();
080            public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
081                    ".List";
082            public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
083                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
084                            "findByCompanyId",
085                            new String[] {
086                                    Long.class.getName(),
087                                    
088                            "java.lang.Integer", "java.lang.Integer",
089                                    "com.liferay.portal.kernel.util.OrderByComparator"
090                            });
091            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
092                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
093                            "countByCompanyId", new String[] { Long.class.getName() });
094            public static final FinderPath FINDER_PATH_FIND_BY_LOCATIONS = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
095                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
096                            "findByLocations",
097                            new String[] {
098                                    Long.class.getName(),
099                                    
100                            "java.lang.Integer", "java.lang.Integer",
101                                    "com.liferay.portal.kernel.util.OrderByComparator"
102                            });
103            public static final FinderPath FINDER_PATH_COUNT_BY_LOCATIONS = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
104                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
105                            "countByLocations", new String[] { Long.class.getName() });
106            public static final FinderPath FINDER_PATH_FIND_BY_C_P = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
107                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
108                            "findByC_P",
109                            new String[] {
110                                    Long.class.getName(), Long.class.getName(),
111                                    
112                            "java.lang.Integer", "java.lang.Integer",
113                                    "com.liferay.portal.kernel.util.OrderByComparator"
114                            });
115            public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
116                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
117                            "countByC_P",
118                            new String[] { Long.class.getName(), Long.class.getName() });
119            public static final FinderPath FINDER_PATH_FETCH_BY_C_N = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
120                            OrganizationModelImpl.FINDER_CACHE_ENABLED,
121                            FINDER_CLASS_NAME_ENTITY, "fetchByC_N",
122                            new String[] { Long.class.getName(), String.class.getName() });
123            public static final FinderPath FINDER_PATH_COUNT_BY_C_N = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
124                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
125                            "countByC_N",
126                            new String[] { Long.class.getName(), String.class.getName() });
127            public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
128                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
129                            "findAll", new String[0]);
130            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
131                            OrganizationModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
132                            "countAll", new String[0]);
133    
134            /**
135             * Caches the organization in the entity cache if it is enabled.
136             *
137             * @param organization the organization to cache
138             */
139            public void cacheResult(Organization organization) {
140                    EntityCacheUtil.putResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
141                            OrganizationImpl.class, organization.getPrimaryKey(), organization);
142    
143                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
144                            new Object[] {
145                                    new Long(organization.getCompanyId()),
146                                    
147                            organization.getName()
148                            }, organization);
149            }
150    
151            /**
152             * Caches the organizations in the entity cache if it is enabled.
153             *
154             * @param organizations the organizations to cache
155             */
156            public void cacheResult(List<Organization> organizations) {
157                    for (Organization organization : organizations) {
158                            if (EntityCacheUtil.getResult(
159                                                    OrganizationModelImpl.ENTITY_CACHE_ENABLED,
160                                                    OrganizationImpl.class, organization.getPrimaryKey(),
161                                                    this) == null) {
162                                    cacheResult(organization);
163                            }
164                    }
165            }
166    
167            /**
168             * Clears the cache for all organizations.
169             *
170             * <p>
171             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
172             * </p>
173             */
174            public void clearCache() {
175                    CacheRegistryUtil.clear(OrganizationImpl.class.getName());
176                    EntityCacheUtil.clearCache(OrganizationImpl.class.getName());
177                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
178                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
179            }
180    
181            /**
182             * Clears the cache for the organization.
183             *
184             * <p>
185             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
186             * </p>
187             */
188            public void clearCache(Organization organization) {
189                    EntityCacheUtil.removeResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
190                            OrganizationImpl.class, organization.getPrimaryKey());
191    
192                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
193                            new Object[] {
194                                    new Long(organization.getCompanyId()),
195                                    
196                            organization.getName()
197                            });
198            }
199    
200            /**
201             * Creates a new organization with the primary key. Does not add the organization to the database.
202             *
203             * @param organizationId the primary key for the new organization
204             * @return the new organization
205             */
206            public Organization create(long organizationId) {
207                    Organization organization = new OrganizationImpl();
208    
209                    organization.setNew(true);
210                    organization.setPrimaryKey(organizationId);
211    
212                    return organization;
213            }
214    
215            /**
216             * Removes the organization with the primary key from the database. Also notifies the appropriate model listeners.
217             *
218             * @param primaryKey the primary key of the organization to remove
219             * @return the organization that was removed
220             * @throws com.liferay.portal.NoSuchModelException if a organization with the primary key could not be found
221             * @throws SystemException if a system exception occurred
222             */
223            public Organization remove(Serializable primaryKey)
224                    throws NoSuchModelException, SystemException {
225                    return remove(((Long)primaryKey).longValue());
226            }
227    
228            /**
229             * Removes the organization with the primary key from the database. Also notifies the appropriate model listeners.
230             *
231             * @param organizationId the primary key of the organization to remove
232             * @return the organization that was removed
233             * @throws com.liferay.portal.NoSuchOrganizationException if a organization with the primary key could not be found
234             * @throws SystemException if a system exception occurred
235             */
236            public Organization remove(long organizationId)
237                    throws NoSuchOrganizationException, SystemException {
238                    Session session = null;
239    
240                    try {
241                            session = openSession();
242    
243                            Organization organization = (Organization)session.get(OrganizationImpl.class,
244                                            new Long(organizationId));
245    
246                            if (organization == null) {
247                                    if (_log.isWarnEnabled()) {
248                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
249                                                    organizationId);
250                                    }
251    
252                                    throw new NoSuchOrganizationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
253                                            organizationId);
254                            }
255    
256                            return remove(organization);
257                    }
258                    catch (NoSuchOrganizationException nsee) {
259                            throw nsee;
260                    }
261                    catch (Exception e) {
262                            throw processException(e);
263                    }
264                    finally {
265                            closeSession(session);
266                    }
267            }
268    
269            protected Organization removeImpl(Organization organization)
270                    throws SystemException {
271                    organization = toUnwrappedModel(organization);
272    
273                    try {
274                            clearGroups.clear(organization.getPrimaryKey());
275                    }
276                    catch (Exception e) {
277                            throw processException(e);
278                    }
279                    finally {
280                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
281                    }
282    
283                    try {
284                            clearUsers.clear(organization.getPrimaryKey());
285                    }
286                    catch (Exception e) {
287                            throw processException(e);
288                    }
289                    finally {
290                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
291                    }
292    
293                    shrinkTree(organization);
294    
295                    Session session = null;
296    
297                    try {
298                            session = openSession();
299    
300                            if (organization.isCachedModel() || BatchSessionUtil.isEnabled()) {
301                                    Object staleObject = session.get(OrganizationImpl.class,
302                                                    organization.getPrimaryKeyObj());
303    
304                                    if (staleObject != null) {
305                                            session.evict(staleObject);
306                                    }
307                            }
308    
309                            session.delete(organization);
310    
311                            session.flush();
312                    }
313                    catch (Exception e) {
314                            throw processException(e);
315                    }
316                    finally {
317                            closeSession(session);
318                    }
319    
320                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
321    
322                    OrganizationModelImpl organizationModelImpl = (OrganizationModelImpl)organization;
323    
324                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
325                            new Object[] {
326                                    new Long(organizationModelImpl.getOriginalCompanyId()),
327                                    
328                            organizationModelImpl.getOriginalName()
329                            });
330    
331                    EntityCacheUtil.removeResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
332                            OrganizationImpl.class, organization.getPrimaryKey());
333    
334                    return organization;
335            }
336    
337            public Organization updateImpl(
338                    com.liferay.portal.model.Organization organization, boolean merge)
339                    throws SystemException {
340                    organization = toUnwrappedModel(organization);
341    
342                    boolean isNew = organization.isNew();
343    
344                    OrganizationModelImpl organizationModelImpl = (OrganizationModelImpl)organization;
345    
346                    if (isNew) {
347                            expandTree(organization);
348                    }
349                    else {
350                            if (organization.getParentOrganizationId() != organizationModelImpl.getOriginalParentOrganizationId()) {
351                                    shrinkTree(organization);
352                                    expandTree(organization);
353                            }
354                    }
355    
356                    Session session = null;
357    
358                    try {
359                            session = openSession();
360    
361                            BatchSessionUtil.update(session, organization, merge);
362    
363                            organization.setNew(false);
364                    }
365                    catch (Exception e) {
366                            throw processException(e);
367                    }
368                    finally {
369                            closeSession(session);
370                    }
371    
372                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
373    
374                    EntityCacheUtil.putResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
375                            OrganizationImpl.class, organization.getPrimaryKey(), organization);
376    
377                    if (!isNew &&
378                                    ((organization.getCompanyId() != organizationModelImpl.getOriginalCompanyId()) ||
379                                    !Validator.equals(organization.getName(),
380                                            organizationModelImpl.getOriginalName()))) {
381                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
382                                    new Object[] {
383                                            new Long(organizationModelImpl.getOriginalCompanyId()),
384                                            
385                                    organizationModelImpl.getOriginalName()
386                                    });
387                    }
388    
389                    if (isNew ||
390                                    ((organization.getCompanyId() != organizationModelImpl.getOriginalCompanyId()) ||
391                                    !Validator.equals(organization.getName(),
392                                            organizationModelImpl.getOriginalName()))) {
393                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
394                                    new Object[] {
395                                            new Long(organization.getCompanyId()),
396                                            
397                                    organization.getName()
398                                    }, organization);
399                    }
400    
401                    return organization;
402            }
403    
404            protected Organization toUnwrappedModel(Organization organization) {
405                    if (organization instanceof OrganizationImpl) {
406                            return organization;
407                    }
408    
409                    OrganizationImpl organizationImpl = new OrganizationImpl();
410    
411                    organizationImpl.setNew(organization.isNew());
412                    organizationImpl.setPrimaryKey(organization.getPrimaryKey());
413    
414                    organizationImpl.setOrganizationId(organization.getOrganizationId());
415                    organizationImpl.setCompanyId(organization.getCompanyId());
416                    organizationImpl.setParentOrganizationId(organization.getParentOrganizationId());
417                    organizationImpl.setLeftOrganizationId(organization.getLeftOrganizationId());
418                    organizationImpl.setRightOrganizationId(organization.getRightOrganizationId());
419                    organizationImpl.setName(organization.getName());
420                    organizationImpl.setType(organization.getType());
421                    organizationImpl.setRecursable(organization.isRecursable());
422                    organizationImpl.setRegionId(organization.getRegionId());
423                    organizationImpl.setCountryId(organization.getCountryId());
424                    organizationImpl.setStatusId(organization.getStatusId());
425                    organizationImpl.setComments(organization.getComments());
426    
427                    return organizationImpl;
428            }
429    
430            /**
431             * Finds the organization with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
432             *
433             * @param primaryKey the primary key of the organization to find
434             * @return the organization
435             * @throws com.liferay.portal.NoSuchModelException if a organization with the primary key could not be found
436             * @throws SystemException if a system exception occurred
437             */
438            public Organization findByPrimaryKey(Serializable primaryKey)
439                    throws NoSuchModelException, SystemException {
440                    return findByPrimaryKey(((Long)primaryKey).longValue());
441            }
442    
443            /**
444             * Finds the organization with the primary key or throws a {@link com.liferay.portal.NoSuchOrganizationException} if it could not be found.
445             *
446             * @param organizationId the primary key of the organization to find
447             * @return the organization
448             * @throws com.liferay.portal.NoSuchOrganizationException if a organization with the primary key could not be found
449             * @throws SystemException if a system exception occurred
450             */
451            public Organization findByPrimaryKey(long organizationId)
452                    throws NoSuchOrganizationException, SystemException {
453                    Organization organization = fetchByPrimaryKey(organizationId);
454    
455                    if (organization == null) {
456                            if (_log.isWarnEnabled()) {
457                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + organizationId);
458                            }
459    
460                            throw new NoSuchOrganizationException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
461                                    organizationId);
462                    }
463    
464                    return organization;
465            }
466    
467            /**
468             * Finds the organization with the primary key or returns <code>null</code> if it could not be found.
469             *
470             * @param primaryKey the primary key of the organization to find
471             * @return the organization, or <code>null</code> if a organization with the primary key could not be found
472             * @throws SystemException if a system exception occurred
473             */
474            public Organization fetchByPrimaryKey(Serializable primaryKey)
475                    throws SystemException {
476                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
477            }
478    
479            /**
480             * Finds the organization with the primary key or returns <code>null</code> if it could not be found.
481             *
482             * @param organizationId the primary key of the organization to find
483             * @return the organization, or <code>null</code> if a organization with the primary key could not be found
484             * @throws SystemException if a system exception occurred
485             */
486            public Organization fetchByPrimaryKey(long organizationId)
487                    throws SystemException {
488                    Organization organization = (Organization)EntityCacheUtil.getResult(OrganizationModelImpl.ENTITY_CACHE_ENABLED,
489                                    OrganizationImpl.class, organizationId, this);
490    
491                    if (organization == null) {
492                            Session session = null;
493    
494                            try {
495                                    session = openSession();
496    
497                                    organization = (Organization)session.get(OrganizationImpl.class,
498                                                    new Long(organizationId));
499                            }
500                            catch (Exception e) {
501                                    throw processException(e);
502                            }
503                            finally {
504                                    if (organization != null) {
505                                            cacheResult(organization);
506                                    }
507    
508                                    closeSession(session);
509                            }
510                    }
511    
512                    return organization;
513            }
514    
515            /**
516             * Finds all the organizations where companyId = &#63;.
517             *
518             * @param companyId the company id to search with
519             * @return the matching organizations
520             * @throws SystemException if a system exception occurred
521             */
522            public List<Organization> findByCompanyId(long companyId)
523                    throws SystemException {
524                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
525                            null);
526            }
527    
528            /**
529             * Finds a range of all the organizations where companyId = &#63;.
530             *
531             * <p>
532             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
533             * </p>
534             *
535             * @param companyId the company id to search with
536             * @param start the lower bound of the range of organizations to return
537             * @param end the upper bound of the range of organizations to return (not inclusive)
538             * @return the range of matching organizations
539             * @throws SystemException if a system exception occurred
540             */
541            public List<Organization> findByCompanyId(long companyId, int start, int end)
542                    throws SystemException {
543                    return findByCompanyId(companyId, start, end, null);
544            }
545    
546            /**
547             * Finds an ordered range of all the organizations where companyId = &#63;.
548             *
549             * <p>
550             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
551             * </p>
552             *
553             * @param companyId the company id to search with
554             * @param start the lower bound of the range of organizations to return
555             * @param end the upper bound of the range of organizations to return (not inclusive)
556             * @param orderByComparator the comparator to order the results by
557             * @return the ordered range of matching organizations
558             * @throws SystemException if a system exception occurred
559             */
560            public List<Organization> findByCompanyId(long companyId, int start,
561                    int end, OrderByComparator orderByComparator) throws SystemException {
562                    Object[] finderArgs = new Object[] {
563                                    companyId,
564                                    
565                                    String.valueOf(start), String.valueOf(end),
566                                    String.valueOf(orderByComparator)
567                            };
568    
569                    List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
570                                    finderArgs, this);
571    
572                    if (list == null) {
573                            Session session = null;
574    
575                            try {
576                                    session = openSession();
577    
578                                    StringBundler query = null;
579    
580                                    if (orderByComparator != null) {
581                                            query = new StringBundler(3 +
582                                                            (orderByComparator.getOrderByFields().length * 3));
583                                    }
584                                    else {
585                                            query = new StringBundler(3);
586                                    }
587    
588                                    query.append(_SQL_SELECT_ORGANIZATION_WHERE);
589    
590                                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
591    
592                                    if (orderByComparator != null) {
593                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
594                                                    orderByComparator);
595                                    }
596    
597                                    else {
598                                            query.append(OrganizationModelImpl.ORDER_BY_JPQL);
599                                    }
600    
601                                    String sql = query.toString();
602    
603                                    Query q = session.createQuery(sql);
604    
605                                    QueryPos qPos = QueryPos.getInstance(q);
606    
607                                    qPos.add(companyId);
608    
609                                    list = (List<Organization>)QueryUtil.list(q, getDialect(),
610                                                    start, end);
611                            }
612                            catch (Exception e) {
613                                    throw processException(e);
614                            }
615                            finally {
616                                    if (list == null) {
617                                            list = new ArrayList<Organization>();
618                                    }
619    
620                                    cacheResult(list);
621    
622                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
623                                            finderArgs, list);
624    
625                                    closeSession(session);
626                            }
627                    }
628    
629                    return list;
630            }
631    
632            /**
633             * Finds the first organization in the ordered set where companyId = &#63;.
634             *
635             * <p>
636             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
637             * </p>
638             *
639             * @param companyId the company id to search with
640             * @param orderByComparator the comparator to order the set by
641             * @return the first matching organization
642             * @throws com.liferay.portal.NoSuchOrganizationException if a matching organization could not be found
643             * @throws SystemException if a system exception occurred
644             */
645            public Organization findByCompanyId_First(long companyId,
646                    OrderByComparator orderByComparator)
647                    throws NoSuchOrganizationException, SystemException {
648                    List<Organization> list = findByCompanyId(companyId, 0, 1,
649                                    orderByComparator);
650    
651                    if (list.isEmpty()) {
652                            StringBundler msg = new StringBundler(4);
653    
654                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
655    
656                            msg.append("companyId=");
657                            msg.append(companyId);
658    
659                            msg.append(StringPool.CLOSE_CURLY_BRACE);
660    
661                            throw new NoSuchOrganizationException(msg.toString());
662                    }
663                    else {
664                            return list.get(0);
665                    }
666            }
667    
668            /**
669             * Finds the last organization in the ordered set where companyId = &#63;.
670             *
671             * <p>
672             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
673             * </p>
674             *
675             * @param companyId the company id to search with
676             * @param orderByComparator the comparator to order the set by
677             * @return the last matching organization
678             * @throws com.liferay.portal.NoSuchOrganizationException if a matching organization could not be found
679             * @throws SystemException if a system exception occurred
680             */
681            public Organization findByCompanyId_Last(long companyId,
682                    OrderByComparator orderByComparator)
683                    throws NoSuchOrganizationException, SystemException {
684                    int count = countByCompanyId(companyId);
685    
686                    List<Organization> list = findByCompanyId(companyId, count - 1, count,
687                                    orderByComparator);
688    
689                    if (list.isEmpty()) {
690                            StringBundler msg = new StringBundler(4);
691    
692                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
693    
694                            msg.append("companyId=");
695                            msg.append(companyId);
696    
697                            msg.append(StringPool.CLOSE_CURLY_BRACE);
698    
699                            throw new NoSuchOrganizationException(msg.toString());
700                    }
701                    else {
702                            return list.get(0);
703                    }
704            }
705    
706            /**
707             * Finds the organizations before and after the current organization in the ordered set where companyId = &#63;.
708             *
709             * <p>
710             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
711             * </p>
712             *
713             * @param organizationId the primary key of the current organization
714             * @param companyId the company id to search with
715             * @param orderByComparator the comparator to order the set by
716             * @return the previous, current, and next organization
717             * @throws com.liferay.portal.NoSuchOrganizationException if a organization with the primary key could not be found
718             * @throws SystemException if a system exception occurred
719             */
720            public Organization[] findByCompanyId_PrevAndNext(long organizationId,
721                    long companyId, OrderByComparator orderByComparator)
722                    throws NoSuchOrganizationException, SystemException {
723                    Organization organization = findByPrimaryKey(organizationId);
724    
725                    Session session = null;
726    
727                    try {
728                            session = openSession();
729    
730                            Organization[] array = new OrganizationImpl[3];
731    
732                            array[0] = getByCompanyId_PrevAndNext(session, organization,
733                                            companyId, orderByComparator, true);
734    
735                            array[1] = organization;
736    
737                            array[2] = getByCompanyId_PrevAndNext(session, organization,
738                                            companyId, orderByComparator, false);
739    
740                            return array;
741                    }
742                    catch (Exception e) {
743                            throw processException(e);
744                    }
745                    finally {
746                            closeSession(session);
747                    }
748            }
749    
750            protected Organization getByCompanyId_PrevAndNext(Session session,
751                    Organization organization, long companyId,
752                    OrderByComparator orderByComparator, boolean previous) {
753                    StringBundler query = null;
754    
755                    if (orderByComparator != null) {
756                            query = new StringBundler(6 +
757                                            (orderByComparator.getOrderByFields().length * 6));
758                    }
759                    else {
760                            query = new StringBundler(3);
761                    }
762    
763                    query.append(_SQL_SELECT_ORGANIZATION_WHERE);
764    
765                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
766    
767                    if (orderByComparator != null) {
768                            String[] orderByFields = orderByComparator.getOrderByFields();
769    
770                            if (orderByFields.length > 0) {
771                                    query.append(WHERE_AND);
772                            }
773    
774                            for (int i = 0; i < orderByFields.length; i++) {
775                                    query.append(_ORDER_BY_ENTITY_ALIAS);
776                                    query.append(orderByFields[i]);
777    
778                                    if ((i + 1) < orderByFields.length) {
779                                            if (orderByComparator.isAscending() ^ previous) {
780                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
781                                            }
782                                            else {
783                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
784                                            }
785                                    }
786                                    else {
787                                            if (orderByComparator.isAscending() ^ previous) {
788                                                    query.append(WHERE_GREATER_THAN);
789                                            }
790                                            else {
791                                                    query.append(WHERE_LESSER_THAN);
792                                            }
793                                    }
794                            }
795    
796                            query.append(ORDER_BY_CLAUSE);
797    
798                            for (int i = 0; i < orderByFields.length; i++) {
799                                    query.append(_ORDER_BY_ENTITY_ALIAS);
800                                    query.append(orderByFields[i]);
801    
802                                    if ((i + 1) < orderByFields.length) {
803                                            if (orderByComparator.isAscending() ^ previous) {
804                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
805                                            }
806                                            else {
807                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
808                                            }
809                                    }
810                                    else {
811                                            if (orderByComparator.isAscending() ^ previous) {
812                                                    query.append(ORDER_BY_ASC);
813                                            }
814                                            else {
815                                                    query.append(ORDER_BY_DESC);
816                                            }
817                                    }
818                            }
819                    }
820    
821                    else {
822                            query.append(OrganizationModelImpl.ORDER_BY_JPQL);
823                    }
824    
825                    String sql = query.toString();
826    
827                    Query q = session.createQuery(sql);
828    
829                    q.setFirstResult(0);
830                    q.setMaxResults(2);
831    
832                    QueryPos qPos = QueryPos.getInstance(q);
833    
834                    qPos.add(companyId);
835    
836                    if (orderByComparator != null) {
837                            Object[] values = orderByComparator.getOrderByValues(organization);
838    
839                            for (Object value : values) {
840                                    qPos.add(value);
841                            }
842                    }
843    
844                    List<Organization> list = q.list();
845    
846                    if (list.size() == 2) {
847                            return list.get(1);
848                    }
849                    else {
850                            return null;
851                    }
852            }
853    
854            /**
855             * Finds all the organizations where companyId = &#63;.
856             *
857             * @param companyId the company id to search with
858             * @return the matching organizations
859             * @throws SystemException if a system exception occurred
860             */
861            public List<Organization> findByLocations(long companyId)
862                    throws SystemException {
863                    return findByLocations(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
864                            null);
865            }
866    
867            /**
868             * Finds a range of all the organizations where companyId = &#63;.
869             *
870             * <p>
871             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
872             * </p>
873             *
874             * @param companyId the company id to search with
875             * @param start the lower bound of the range of organizations to return
876             * @param end the upper bound of the range of organizations to return (not inclusive)
877             * @return the range of matching organizations
878             * @throws SystemException if a system exception occurred
879             */
880            public List<Organization> findByLocations(long companyId, int start, int end)
881                    throws SystemException {
882                    return findByLocations(companyId, start, end, null);
883            }
884    
885            /**
886             * Finds an ordered range of all the organizations where companyId = &#63;.
887             *
888             * <p>
889             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
890             * </p>
891             *
892             * @param companyId the company id to search with
893             * @param start the lower bound of the range of organizations to return
894             * @param end the upper bound of the range of organizations to return (not inclusive)
895             * @param orderByComparator the comparator to order the results by
896             * @return the ordered range of matching organizations
897             * @throws SystemException if a system exception occurred
898             */
899            public List<Organization> findByLocations(long companyId, int start,
900                    int end, OrderByComparator orderByComparator) throws SystemException {
901                    Object[] finderArgs = new Object[] {
902                                    companyId,
903                                    
904                                    String.valueOf(start), String.valueOf(end),
905                                    String.valueOf(orderByComparator)
906                            };
907    
908                    List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_LOCATIONS,
909                                    finderArgs, this);
910    
911                    if (list == null) {
912                            Session session = null;
913    
914                            try {
915                                    session = openSession();
916    
917                                    StringBundler query = null;
918    
919                                    if (orderByComparator != null) {
920                                            query = new StringBundler(3 +
921                                                            (orderByComparator.getOrderByFields().length * 3));
922                                    }
923                                    else {
924                                            query = new StringBundler(3);
925                                    }
926    
927                                    query.append(_SQL_SELECT_ORGANIZATION_WHERE);
928    
929                                    query.append(_FINDER_COLUMN_LOCATIONS_COMPANYID_2);
930    
931                                    if (orderByComparator != null) {
932                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
933                                                    orderByComparator);
934                                    }
935    
936                                    else {
937                                            query.append(OrganizationModelImpl.ORDER_BY_JPQL);
938                                    }
939    
940                                    String sql = query.toString();
941    
942                                    Query q = session.createQuery(sql);
943    
944                                    QueryPos qPos = QueryPos.getInstance(q);
945    
946                                    qPos.add(companyId);
947    
948                                    list = (List<Organization>)QueryUtil.list(q, getDialect(),
949                                                    start, end);
950                            }
951                            catch (Exception e) {
952                                    throw processException(e);
953                            }
954                            finally {
955                                    if (list == null) {
956                                            list = new ArrayList<Organization>();
957                                    }
958    
959                                    cacheResult(list);
960    
961                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_LOCATIONS,
962                                            finderArgs, list);
963    
964                                    closeSession(session);
965                            }
966                    }
967    
968                    return list;
969            }
970    
971            /**
972             * Finds the first organization in the ordered set where companyId = &#63;.
973             *
974             * <p>
975             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
976             * </p>
977             *
978             * @param companyId the company id to search with
979             * @param orderByComparator the comparator to order the set by
980             * @return the first matching organization
981             * @throws com.liferay.portal.NoSuchOrganizationException if a matching organization could not be found
982             * @throws SystemException if a system exception occurred
983             */
984            public Organization findByLocations_First(long companyId,
985                    OrderByComparator orderByComparator)
986                    throws NoSuchOrganizationException, SystemException {
987                    List<Organization> list = findByLocations(companyId, 0, 1,
988                                    orderByComparator);
989    
990                    if (list.isEmpty()) {
991                            StringBundler msg = new StringBundler(4);
992    
993                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
994    
995                            msg.append("companyId=");
996                            msg.append(companyId);
997    
998                            msg.append(StringPool.CLOSE_CURLY_BRACE);
999    
1000                            throw new NoSuchOrganizationException(msg.toString());
1001                    }
1002                    else {
1003                            return list.get(0);
1004                    }
1005            }
1006    
1007            /**
1008             * Finds the last organization in the ordered set where companyId = &#63;.
1009             *
1010             * <p>
1011             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1012             * </p>
1013             *
1014             * @param companyId the company id to search with
1015             * @param orderByComparator the comparator to order the set by
1016             * @return the last matching organization
1017             * @throws com.liferay.portal.NoSuchOrganizationException if a matching organization could not be found
1018             * @throws SystemException if a system exception occurred
1019             */
1020            public Organization findByLocations_Last(long companyId,
1021                    OrderByComparator orderByComparator)
1022                    throws NoSuchOrganizationException, SystemException {
1023                    int count = countByLocations(companyId);
1024    
1025                    List<Organization> list = findByLocations(companyId, count - 1, count,
1026                                    orderByComparator);
1027    
1028                    if (list.isEmpty()) {
1029                            StringBundler msg = new StringBundler(4);
1030    
1031                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1032    
1033                            msg.append("companyId=");
1034                            msg.append(companyId);
1035    
1036                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1037    
1038                            throw new NoSuchOrganizationException(msg.toString());
1039                    }
1040                    else {
1041                            return list.get(0);
1042                    }
1043            }
1044    
1045            /**
1046             * Finds the organizations before and after the current organization in the ordered set where companyId = &#63;.
1047             *
1048             * <p>
1049             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1050             * </p>
1051             *
1052             * @param organizationId the primary key of the current organization
1053             * @param companyId the company id to search with
1054             * @param orderByComparator the comparator to order the set by
1055             * @return the previous, current, and next organization
1056             * @throws com.liferay.portal.NoSuchOrganizationException if a organization with the primary key could not be found
1057             * @throws SystemException if a system exception occurred
1058             */
1059            public Organization[] findByLocations_PrevAndNext(long organizationId,
1060                    long companyId, OrderByComparator orderByComparator)
1061                    throws NoSuchOrganizationException, SystemException {
1062                    Organization organization = findByPrimaryKey(organizationId);
1063    
1064                    Session session = null;
1065    
1066                    try {
1067                            session = openSession();
1068    
1069                            Organization[] array = new OrganizationImpl[3];
1070    
1071                            array[0] = getByLocations_PrevAndNext(session, organization,
1072                                            companyId, orderByComparator, true);
1073    
1074                            array[1] = organization;
1075    
1076                            array[2] = getByLocations_PrevAndNext(session, organization,
1077                                            companyId, orderByComparator, false);
1078    
1079                            return array;
1080                    }
1081                    catch (Exception e) {
1082                            throw processException(e);
1083                    }
1084                    finally {
1085                            closeSession(session);
1086                    }
1087            }
1088    
1089            protected Organization getByLocations_PrevAndNext(Session session,
1090                    Organization organization, long companyId,
1091                    OrderByComparator orderByComparator, boolean previous) {
1092                    StringBundler query = null;
1093    
1094                    if (orderByComparator != null) {
1095                            query = new StringBundler(6 +
1096                                            (orderByComparator.getOrderByFields().length * 6));
1097                    }
1098                    else {
1099                            query = new StringBundler(3);
1100                    }
1101    
1102                    query.append(_SQL_SELECT_ORGANIZATION_WHERE);
1103    
1104                    query.append(_FINDER_COLUMN_LOCATIONS_COMPANYID_2);
1105    
1106                    if (orderByComparator != null) {
1107                            String[] orderByFields = orderByComparator.getOrderByFields();
1108    
1109                            if (orderByFields.length > 0) {
1110                                    query.append(WHERE_AND);
1111                            }
1112    
1113                            for (int i = 0; i < orderByFields.length; i++) {
1114                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1115                                    query.append(orderByFields[i]);
1116    
1117                                    if ((i + 1) < orderByFields.length) {
1118                                            if (orderByComparator.isAscending() ^ previous) {
1119                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1120                                            }
1121                                            else {
1122                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1123                                            }
1124                                    }
1125                                    else {
1126                                            if (orderByComparator.isAscending() ^ previous) {
1127                                                    query.append(WHERE_GREATER_THAN);
1128                                            }
1129                                            else {
1130                                                    query.append(WHERE_LESSER_THAN);
1131                                            }
1132                                    }
1133                            }
1134    
1135                            query.append(ORDER_BY_CLAUSE);
1136    
1137                            for (int i = 0; i < orderByFields.length; i++) {
1138                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1139                                    query.append(orderByFields[i]);
1140    
1141                                    if ((i + 1) < orderByFields.length) {
1142                                            if (orderByComparator.isAscending() ^ previous) {
1143                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1144                                            }
1145                                            else {
1146                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1147                                            }
1148                                    }
1149                                    else {
1150                                            if (orderByComparator.isAscending() ^ previous) {
1151                                                    query.append(ORDER_BY_ASC);
1152                                            }
1153                                            else {
1154                                                    query.append(ORDER_BY_DESC);
1155                                            }
1156                                    }
1157                            }
1158                    }
1159    
1160                    else {
1161                            query.append(OrganizationModelImpl.ORDER_BY_JPQL);
1162                    }
1163    
1164                    String sql = query.toString();
1165    
1166                    Query q = session.createQuery(sql);
1167    
1168                    q.setFirstResult(0);
1169                    q.setMaxResults(2);
1170    
1171                    QueryPos qPos = QueryPos.getInstance(q);
1172    
1173                    qPos.add(companyId);
1174    
1175                    if (orderByComparator != null) {
1176                            Object[] values = orderByComparator.getOrderByValues(organization);
1177    
1178                            for (Object value : values) {
1179                                    qPos.add(value);
1180                            }
1181                    }
1182    
1183                    List<Organization> list = q.list();
1184    
1185                    if (list.size() == 2) {
1186                            return list.get(1);
1187                    }
1188                    else {
1189                            return null;
1190                    }
1191            }
1192    
1193            /**
1194             * Finds all the organizations where companyId = &#63; and parentOrganizationId = &#63;.
1195             *
1196             * @param companyId the company id to search with
1197             * @param parentOrganizationId the parent organization id to search with
1198             * @return the matching organizations
1199             * @throws SystemException if a system exception occurred
1200             */
1201            public List<Organization> findByC_P(long companyId,
1202                    long parentOrganizationId) throws SystemException {
1203                    return findByC_P(companyId, parentOrganizationId, QueryUtil.ALL_POS,
1204                            QueryUtil.ALL_POS, null);
1205            }
1206    
1207            /**
1208             * Finds a range of all the organizations where companyId = &#63; and parentOrganizationId = &#63;.
1209             *
1210             * <p>
1211             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1212             * </p>
1213             *
1214             * @param companyId the company id to search with
1215             * @param parentOrganizationId the parent organization id to search with
1216             * @param start the lower bound of the range of organizations to return
1217             * @param end the upper bound of the range of organizations to return (not inclusive)
1218             * @return the range of matching organizations
1219             * @throws SystemException if a system exception occurred
1220             */
1221            public List<Organization> findByC_P(long companyId,
1222                    long parentOrganizationId, int start, int end)
1223                    throws SystemException {
1224                    return findByC_P(companyId, parentOrganizationId, start, end, null);
1225            }
1226    
1227            /**
1228             * Finds an ordered range of all the organizations where companyId = &#63; and parentOrganizationId = &#63;.
1229             *
1230             * <p>
1231             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1232             * </p>
1233             *
1234             * @param companyId the company id to search with
1235             * @param parentOrganizationId the parent organization id to search with
1236             * @param start the lower bound of the range of organizations to return
1237             * @param end the upper bound of the range of organizations to return (not inclusive)
1238             * @param orderByComparator the comparator to order the results by
1239             * @return the ordered range of matching organizations
1240             * @throws SystemException if a system exception occurred
1241             */
1242            public List<Organization> findByC_P(long companyId,
1243                    long parentOrganizationId, int start, int end,
1244                    OrderByComparator orderByComparator) throws SystemException {
1245                    Object[] finderArgs = new Object[] {
1246                                    companyId, parentOrganizationId,
1247                                    
1248                                    String.valueOf(start), String.valueOf(end),
1249                                    String.valueOf(orderByComparator)
1250                            };
1251    
1252                    List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_P,
1253                                    finderArgs, this);
1254    
1255                    if (list == null) {
1256                            Session session = null;
1257    
1258                            try {
1259                                    session = openSession();
1260    
1261                                    StringBundler query = null;
1262    
1263                                    if (orderByComparator != null) {
1264                                            query = new StringBundler(4 +
1265                                                            (orderByComparator.getOrderByFields().length * 3));
1266                                    }
1267                                    else {
1268                                            query = new StringBundler(4);
1269                                    }
1270    
1271                                    query.append(_SQL_SELECT_ORGANIZATION_WHERE);
1272    
1273                                    query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1274    
1275                                    query.append(_FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2);
1276    
1277                                    if (orderByComparator != null) {
1278                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1279                                                    orderByComparator);
1280                                    }
1281    
1282                                    else {
1283                                            query.append(OrganizationModelImpl.ORDER_BY_JPQL);
1284                                    }
1285    
1286                                    String sql = query.toString();
1287    
1288                                    Query q = session.createQuery(sql);
1289    
1290                                    QueryPos qPos = QueryPos.getInstance(q);
1291    
1292                                    qPos.add(companyId);
1293    
1294                                    qPos.add(parentOrganizationId);
1295    
1296                                    list = (List<Organization>)QueryUtil.list(q, getDialect(),
1297                                                    start, end);
1298                            }
1299                            catch (Exception e) {
1300                                    throw processException(e);
1301                            }
1302                            finally {
1303                                    if (list == null) {
1304                                            list = new ArrayList<Organization>();
1305                                    }
1306    
1307                                    cacheResult(list);
1308    
1309                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_P, finderArgs,
1310                                            list);
1311    
1312                                    closeSession(session);
1313                            }
1314                    }
1315    
1316                    return list;
1317            }
1318    
1319            /**
1320             * Finds the first organization in the ordered set where companyId = &#63; and parentOrganizationId = &#63;.
1321             *
1322             * <p>
1323             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1324             * </p>
1325             *
1326             * @param companyId the company id to search with
1327             * @param parentOrganizationId the parent organization id to search with
1328             * @param orderByComparator the comparator to order the set by
1329             * @return the first matching organization
1330             * @throws com.liferay.portal.NoSuchOrganizationException if a matching organization could not be found
1331             * @throws SystemException if a system exception occurred
1332             */
1333            public Organization findByC_P_First(long companyId,
1334                    long parentOrganizationId, OrderByComparator orderByComparator)
1335                    throws NoSuchOrganizationException, SystemException {
1336                    List<Organization> list = findByC_P(companyId, parentOrganizationId, 0,
1337                                    1, orderByComparator);
1338    
1339                    if (list.isEmpty()) {
1340                            StringBundler msg = new StringBundler(6);
1341    
1342                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1343    
1344                            msg.append("companyId=");
1345                            msg.append(companyId);
1346    
1347                            msg.append(", parentOrganizationId=");
1348                            msg.append(parentOrganizationId);
1349    
1350                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1351    
1352                            throw new NoSuchOrganizationException(msg.toString());
1353                    }
1354                    else {
1355                            return list.get(0);
1356                    }
1357            }
1358    
1359            /**
1360             * Finds the last organization in the ordered set where companyId = &#63; and parentOrganizationId = &#63;.
1361             *
1362             * <p>
1363             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1364             * </p>
1365             *
1366             * @param companyId the company id to search with
1367             * @param parentOrganizationId the parent organization id to search with
1368             * @param orderByComparator the comparator to order the set by
1369             * @return the last matching organization
1370             * @throws com.liferay.portal.NoSuchOrganizationException if a matching organization could not be found
1371             * @throws SystemException if a system exception occurred
1372             */
1373            public Organization findByC_P_Last(long companyId,
1374                    long parentOrganizationId, OrderByComparator orderByComparator)
1375                    throws NoSuchOrganizationException, SystemException {
1376                    int count = countByC_P(companyId, parentOrganizationId);
1377    
1378                    List<Organization> list = findByC_P(companyId, parentOrganizationId,
1379                                    count - 1, count, orderByComparator);
1380    
1381                    if (list.isEmpty()) {
1382                            StringBundler msg = new StringBundler(6);
1383    
1384                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1385    
1386                            msg.append("companyId=");
1387                            msg.append(companyId);
1388    
1389                            msg.append(", parentOrganizationId=");
1390                            msg.append(parentOrganizationId);
1391    
1392                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1393    
1394                            throw new NoSuchOrganizationException(msg.toString());
1395                    }
1396                    else {
1397                            return list.get(0);
1398                    }
1399            }
1400    
1401            /**
1402             * Finds the organizations before and after the current organization in the ordered set where companyId = &#63; and parentOrganizationId = &#63;.
1403             *
1404             * <p>
1405             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1406             * </p>
1407             *
1408             * @param organizationId the primary key of the current organization
1409             * @param companyId the company id to search with
1410             * @param parentOrganizationId the parent organization id to search with
1411             * @param orderByComparator the comparator to order the set by
1412             * @return the previous, current, and next organization
1413             * @throws com.liferay.portal.NoSuchOrganizationException if a organization with the primary key could not be found
1414             * @throws SystemException if a system exception occurred
1415             */
1416            public Organization[] findByC_P_PrevAndNext(long organizationId,
1417                    long companyId, long parentOrganizationId,
1418                    OrderByComparator orderByComparator)
1419                    throws NoSuchOrganizationException, SystemException {
1420                    Organization organization = findByPrimaryKey(organizationId);
1421    
1422                    Session session = null;
1423    
1424                    try {
1425                            session = openSession();
1426    
1427                            Organization[] array = new OrganizationImpl[3];
1428    
1429                            array[0] = getByC_P_PrevAndNext(session, organization, companyId,
1430                                            parentOrganizationId, orderByComparator, true);
1431    
1432                            array[1] = organization;
1433    
1434                            array[2] = getByC_P_PrevAndNext(session, organization, companyId,
1435                                            parentOrganizationId, orderByComparator, false);
1436    
1437                            return array;
1438                    }
1439                    catch (Exception e) {
1440                            throw processException(e);
1441                    }
1442                    finally {
1443                            closeSession(session);
1444                    }
1445            }
1446    
1447            protected Organization getByC_P_PrevAndNext(Session session,
1448                    Organization organization, long companyId, long parentOrganizationId,
1449                    OrderByComparator orderByComparator, boolean previous) {
1450                    StringBundler query = null;
1451    
1452                    if (orderByComparator != null) {
1453                            query = new StringBundler(6 +
1454                                            (orderByComparator.getOrderByFields().length * 6));
1455                    }
1456                    else {
1457                            query = new StringBundler(3);
1458                    }
1459    
1460                    query.append(_SQL_SELECT_ORGANIZATION_WHERE);
1461    
1462                    query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1463    
1464                    query.append(_FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2);
1465    
1466                    if (orderByComparator != null) {
1467                            String[] orderByFields = orderByComparator.getOrderByFields();
1468    
1469                            if (orderByFields.length > 0) {
1470                                    query.append(WHERE_AND);
1471                            }
1472    
1473                            for (int i = 0; i < orderByFields.length; i++) {
1474                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1475                                    query.append(orderByFields[i]);
1476    
1477                                    if ((i + 1) < orderByFields.length) {
1478                                            if (orderByComparator.isAscending() ^ previous) {
1479                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1480                                            }
1481                                            else {
1482                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1483                                            }
1484                                    }
1485                                    else {
1486                                            if (orderByComparator.isAscending() ^ previous) {
1487                                                    query.append(WHERE_GREATER_THAN);
1488                                            }
1489                                            else {
1490                                                    query.append(WHERE_LESSER_THAN);
1491                                            }
1492                                    }
1493                            }
1494    
1495                            query.append(ORDER_BY_CLAUSE);
1496    
1497                            for (int i = 0; i < orderByFields.length; i++) {
1498                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1499                                    query.append(orderByFields[i]);
1500    
1501                                    if ((i + 1) < orderByFields.length) {
1502                                            if (orderByComparator.isAscending() ^ previous) {
1503                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1504                                            }
1505                                            else {
1506                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1507                                            }
1508                                    }
1509                                    else {
1510                                            if (orderByComparator.isAscending() ^ previous) {
1511                                                    query.append(ORDER_BY_ASC);
1512                                            }
1513                                            else {
1514                                                    query.append(ORDER_BY_DESC);
1515                                            }
1516                                    }
1517                            }
1518                    }
1519    
1520                    else {
1521                            query.append(OrganizationModelImpl.ORDER_BY_JPQL);
1522                    }
1523    
1524                    String sql = query.toString();
1525    
1526                    Query q = session.createQuery(sql);
1527    
1528                    q.setFirstResult(0);
1529                    q.setMaxResults(2);
1530    
1531                    QueryPos qPos = QueryPos.getInstance(q);
1532    
1533                    qPos.add(companyId);
1534    
1535                    qPos.add(parentOrganizationId);
1536    
1537                    if (orderByComparator != null) {
1538                            Object[] values = orderByComparator.getOrderByValues(organization);
1539    
1540                            for (Object value : values) {
1541                                    qPos.add(value);
1542                            }
1543                    }
1544    
1545                    List<Organization> list = q.list();
1546    
1547                    if (list.size() == 2) {
1548                            return list.get(1);
1549                    }
1550                    else {
1551                            return null;
1552                    }
1553            }
1554    
1555            /**
1556             * Finds the organization where companyId = &#63; and name = &#63; or throws a {@link com.liferay.portal.NoSuchOrganizationException} if it could not be found.
1557             *
1558             * @param companyId the company id to search with
1559             * @param name the name to search with
1560             * @return the matching organization
1561             * @throws com.liferay.portal.NoSuchOrganizationException if a matching organization could not be found
1562             * @throws SystemException if a system exception occurred
1563             */
1564            public Organization findByC_N(long companyId, String name)
1565                    throws NoSuchOrganizationException, SystemException {
1566                    Organization organization = fetchByC_N(companyId, name);
1567    
1568                    if (organization == null) {
1569                            StringBundler msg = new StringBundler(6);
1570    
1571                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1572    
1573                            msg.append("companyId=");
1574                            msg.append(companyId);
1575    
1576                            msg.append(", name=");
1577                            msg.append(name);
1578    
1579                            msg.append(StringPool.CLOSE_CURLY_BRACE);
1580    
1581                            if (_log.isWarnEnabled()) {
1582                                    _log.warn(msg.toString());
1583                            }
1584    
1585                            throw new NoSuchOrganizationException(msg.toString());
1586                    }
1587    
1588                    return organization;
1589            }
1590    
1591            /**
1592             * Finds the organization where companyId = &#63; and name = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
1593             *
1594             * @param companyId the company id to search with
1595             * @param name the name to search with
1596             * @return the matching organization, or <code>null</code> if a matching organization could not be found
1597             * @throws SystemException if a system exception occurred
1598             */
1599            public Organization fetchByC_N(long companyId, String name)
1600                    throws SystemException {
1601                    return fetchByC_N(companyId, name, true);
1602            }
1603    
1604            /**
1605             * Finds the organization where companyId = &#63; and name = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
1606             *
1607             * @param companyId the company id to search with
1608             * @param name the name to search with
1609             * @return the matching organization, or <code>null</code> if a matching organization could not be found
1610             * @throws SystemException if a system exception occurred
1611             */
1612            public Organization fetchByC_N(long companyId, String name,
1613                    boolean retrieveFromCache) throws SystemException {
1614                    Object[] finderArgs = new Object[] { companyId, name };
1615    
1616                    Object result = null;
1617    
1618                    if (retrieveFromCache) {
1619                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N,
1620                                            finderArgs, this);
1621                    }
1622    
1623                    if (result == null) {
1624                            Session session = null;
1625    
1626                            try {
1627                                    session = openSession();
1628    
1629                                    StringBundler query = new StringBundler(4);
1630    
1631                                    query.append(_SQL_SELECT_ORGANIZATION_WHERE);
1632    
1633                                    query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
1634    
1635                                    if (name == null) {
1636                                            query.append(_FINDER_COLUMN_C_N_NAME_1);
1637                                    }
1638                                    else {
1639                                            if (name.equals(StringPool.BLANK)) {
1640                                                    query.append(_FINDER_COLUMN_C_N_NAME_3);
1641                                            }
1642                                            else {
1643                                                    query.append(_FINDER_COLUMN_C_N_NAME_2);
1644                                            }
1645                                    }
1646    
1647                                    query.append(OrganizationModelImpl.ORDER_BY_JPQL);
1648    
1649                                    String sql = query.toString();
1650    
1651                                    Query q = session.createQuery(sql);
1652    
1653                                    QueryPos qPos = QueryPos.getInstance(q);
1654    
1655                                    qPos.add(companyId);
1656    
1657                                    if (name != null) {
1658                                            qPos.add(name);
1659                                    }
1660    
1661                                    List<Organization> list = q.list();
1662    
1663                                    result = list;
1664    
1665                                    Organization organization = null;
1666    
1667                                    if (list.isEmpty()) {
1668                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1669                                                    finderArgs, list);
1670                                    }
1671                                    else {
1672                                            organization = list.get(0);
1673    
1674                                            cacheResult(organization);
1675    
1676                                            if ((organization.getCompanyId() != companyId) ||
1677                                                            (organization.getName() == null) ||
1678                                                            !organization.getName().equals(name)) {
1679                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1680                                                            finderArgs, organization);
1681                                            }
1682                                    }
1683    
1684                                    return organization;
1685                            }
1686                            catch (Exception e) {
1687                                    throw processException(e);
1688                            }
1689                            finally {
1690                                    if (result == null) {
1691                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1692                                                    finderArgs, new ArrayList<Organization>());
1693                                    }
1694    
1695                                    closeSession(session);
1696                            }
1697                    }
1698                    else {
1699                            if (result instanceof List<?>) {
1700                                    return null;
1701                            }
1702                            else {
1703                                    return (Organization)result;
1704                            }
1705                    }
1706            }
1707    
1708            /**
1709             * Finds all the organizations.
1710             *
1711             * @return the organizations
1712             * @throws SystemException if a system exception occurred
1713             */
1714            public List<Organization> findAll() throws SystemException {
1715                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1716            }
1717    
1718            /**
1719             * Finds a range of all the organizations.
1720             *
1721             * <p>
1722             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1723             * </p>
1724             *
1725             * @param start the lower bound of the range of organizations to return
1726             * @param end the upper bound of the range of organizations to return (not inclusive)
1727             * @return the range of organizations
1728             * @throws SystemException if a system exception occurred
1729             */
1730            public List<Organization> findAll(int start, int end)
1731                    throws SystemException {
1732                    return findAll(start, end, null);
1733            }
1734    
1735            /**
1736             * Finds an ordered range of all the organizations.
1737             *
1738             * <p>
1739             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1740             * </p>
1741             *
1742             * @param start the lower bound of the range of organizations to return
1743             * @param end the upper bound of the range of organizations to return (not inclusive)
1744             * @param orderByComparator the comparator to order the results by
1745             * @return the ordered range of organizations
1746             * @throws SystemException if a system exception occurred
1747             */
1748            public List<Organization> findAll(int start, int end,
1749                    OrderByComparator orderByComparator) throws SystemException {
1750                    Object[] finderArgs = new Object[] {
1751                                    String.valueOf(start), String.valueOf(end),
1752                                    String.valueOf(orderByComparator)
1753                            };
1754    
1755                    List<Organization> list = (List<Organization>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1756                                    finderArgs, this);
1757    
1758                    if (list == null) {
1759                            Session session = null;
1760    
1761                            try {
1762                                    session = openSession();
1763    
1764                                    StringBundler query = null;
1765                                    String sql = null;
1766    
1767                                    if (orderByComparator != null) {
1768                                            query = new StringBundler(2 +
1769                                                            (orderByComparator.getOrderByFields().length * 3));
1770    
1771                                            query.append(_SQL_SELECT_ORGANIZATION);
1772    
1773                                            appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1774                                                    orderByComparator);
1775    
1776                                            sql = query.toString();
1777                                    }
1778                                    else {
1779                                            sql = _SQL_SELECT_ORGANIZATION.concat(OrganizationModelImpl.ORDER_BY_JPQL);
1780                                    }
1781    
1782                                    Query q = session.createQuery(sql);
1783    
1784                                    if (orderByComparator == null) {
1785                                            list = (List<Organization>)QueryUtil.list(q, getDialect(),
1786                                                            start, end, false);
1787    
1788                                            Collections.sort(list);
1789                                    }
1790                                    else {
1791                                            list = (List<Organization>)QueryUtil.list(q, getDialect(),
1792                                                            start, end);
1793                                    }
1794                            }
1795                            catch (Exception e) {
1796                                    throw processException(e);
1797                            }
1798                            finally {
1799                                    if (list == null) {
1800                                            list = new ArrayList<Organization>();
1801                                    }
1802    
1803                                    cacheResult(list);
1804    
1805                                    FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1806    
1807                                    closeSession(session);
1808                            }
1809                    }
1810    
1811                    return list;
1812            }
1813    
1814            /**
1815             * Removes all the organizations where companyId = &#63; from the database.
1816             *
1817             * @param companyId the company id to search with
1818             * @throws SystemException if a system exception occurred
1819             */
1820            public void removeByCompanyId(long companyId) throws SystemException {
1821                    for (Organization organization : findByCompanyId(companyId)) {
1822                            remove(organization);
1823                    }
1824            }
1825    
1826            /**
1827             * Removes all the organizations where companyId = &#63; from the database.
1828             *
1829             * @param companyId the company id to search with
1830             * @throws SystemException if a system exception occurred
1831             */
1832            public void removeByLocations(long companyId) throws SystemException {
1833                    for (Organization organization : findByLocations(companyId)) {
1834                            remove(organization);
1835                    }
1836            }
1837    
1838            /**
1839             * Removes all the organizations where companyId = &#63; and parentOrganizationId = &#63; from the database.
1840             *
1841             * @param companyId the company id to search with
1842             * @param parentOrganizationId the parent organization id to search with
1843             * @throws SystemException if a system exception occurred
1844             */
1845            public void removeByC_P(long companyId, long parentOrganizationId)
1846                    throws SystemException {
1847                    for (Organization organization : findByC_P(companyId,
1848                                    parentOrganizationId)) {
1849                            remove(organization);
1850                    }
1851            }
1852    
1853            /**
1854             * Removes the organization where companyId = &#63; and name = &#63; from the database.
1855             *
1856             * @param companyId the company id to search with
1857             * @param name the name to search with
1858             * @throws SystemException if a system exception occurred
1859             */
1860            public void removeByC_N(long companyId, String name)
1861                    throws NoSuchOrganizationException, SystemException {
1862                    Organization organization = findByC_N(companyId, name);
1863    
1864                    remove(organization);
1865            }
1866    
1867            /**
1868             * Removes all the organizations from the database.
1869             *
1870             * @throws SystemException if a system exception occurred
1871             */
1872            public void removeAll() throws SystemException {
1873                    for (Organization organization : findAll()) {
1874                            remove(organization);
1875                    }
1876            }
1877    
1878            /**
1879             * Counts all the organizations where companyId = &#63;.
1880             *
1881             * @param companyId the company id to search with
1882             * @return the number of matching organizations
1883             * @throws SystemException if a system exception occurred
1884             */
1885            public int countByCompanyId(long companyId) throws SystemException {
1886                    Object[] finderArgs = new Object[] { companyId };
1887    
1888                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
1889                                    finderArgs, this);
1890    
1891                    if (count == null) {
1892                            Session session = null;
1893    
1894                            try {
1895                                    session = openSession();
1896    
1897                                    StringBundler query = new StringBundler(2);
1898    
1899                                    query.append(_SQL_COUNT_ORGANIZATION_WHERE);
1900    
1901                                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1902    
1903                                    String sql = query.toString();
1904    
1905                                    Query q = session.createQuery(sql);
1906    
1907                                    QueryPos qPos = QueryPos.getInstance(q);
1908    
1909                                    qPos.add(companyId);
1910    
1911                                    count = (Long)q.uniqueResult();
1912                            }
1913                            catch (Exception e) {
1914                                    throw processException(e);
1915                            }
1916                            finally {
1917                                    if (count == null) {
1918                                            count = Long.valueOf(0);
1919                                    }
1920    
1921                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
1922                                            finderArgs, count);
1923    
1924                                    closeSession(session);
1925                            }
1926                    }
1927    
1928                    return count.intValue();
1929            }
1930    
1931            /**
1932             * Counts all the organizations where companyId = &#63;.
1933             *
1934             * @param companyId the company id to search with
1935             * @return the number of matching organizations
1936             * @throws SystemException if a system exception occurred
1937             */
1938            public int countByLocations(long companyId) throws SystemException {
1939                    Object[] finderArgs = new Object[] { companyId };
1940    
1941                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_LOCATIONS,
1942                                    finderArgs, this);
1943    
1944                    if (count == null) {
1945                            Session session = null;
1946    
1947                            try {
1948                                    session = openSession();
1949    
1950                                    StringBundler query = new StringBundler(2);
1951    
1952                                    query.append(_SQL_COUNT_ORGANIZATION_WHERE);
1953    
1954                                    query.append(_FINDER_COLUMN_LOCATIONS_COMPANYID_2);
1955    
1956                                    String sql = query.toString();
1957    
1958                                    Query q = session.createQuery(sql);
1959    
1960                                    QueryPos qPos = QueryPos.getInstance(q);
1961    
1962                                    qPos.add(companyId);
1963    
1964                                    count = (Long)q.uniqueResult();
1965                            }
1966                            catch (Exception e) {
1967                                    throw processException(e);
1968                            }
1969                            finally {
1970                                    if (count == null) {
1971                                            count = Long.valueOf(0);
1972                                    }
1973    
1974                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_LOCATIONS,
1975                                            finderArgs, count);
1976    
1977                                    closeSession(session);
1978                            }
1979                    }
1980    
1981                    return count.intValue();
1982            }
1983    
1984            /**
1985             * Counts all the organizations where companyId = &#63; and parentOrganizationId = &#63;.
1986             *
1987             * @param companyId the company id to search with
1988             * @param parentOrganizationId the parent organization id to search with
1989             * @return the number of matching organizations
1990             * @throws SystemException if a system exception occurred
1991             */
1992            public int countByC_P(long companyId, long parentOrganizationId)
1993                    throws SystemException {
1994                    Object[] finderArgs = new Object[] { companyId, parentOrganizationId };
1995    
1996                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
1997                                    finderArgs, this);
1998    
1999                    if (count == null) {
2000                            Session session = null;
2001    
2002                            try {
2003                                    session = openSession();
2004    
2005                                    StringBundler query = new StringBundler(3);
2006    
2007                                    query.append(_SQL_COUNT_ORGANIZATION_WHERE);
2008    
2009                                    query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
2010    
2011                                    query.append(_FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2);
2012    
2013                                    String sql = query.toString();
2014    
2015                                    Query q = session.createQuery(sql);
2016    
2017                                    QueryPos qPos = QueryPos.getInstance(q);
2018    
2019                                    qPos.add(companyId);
2020    
2021                                    qPos.add(parentOrganizationId);
2022    
2023                                    count = (Long)q.uniqueResult();
2024                            }
2025                            catch (Exception e) {
2026                                    throw processException(e);
2027                            }
2028                            finally {
2029                                    if (count == null) {
2030                                            count = Long.valueOf(0);
2031                                    }
2032    
2033                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
2034                                            count);
2035    
2036                                    closeSession(session);
2037                            }
2038                    }
2039    
2040                    return count.intValue();
2041            }
2042    
2043            /**
2044             * Counts all the organizations where companyId = &#63; and name = &#63;.
2045             *
2046             * @param companyId the company id to search with
2047             * @param name the name to search with
2048             * @return the number of matching organizations
2049             * @throws SystemException if a system exception occurred
2050             */
2051            public int countByC_N(long companyId, String name)
2052                    throws SystemException {
2053                    Object[] finderArgs = new Object[] { companyId, name };
2054    
2055                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_N,
2056                                    finderArgs, this);
2057    
2058                    if (count == null) {
2059                            Session session = null;
2060    
2061                            try {
2062                                    session = openSession();
2063    
2064                                    StringBundler query = new StringBundler(3);
2065    
2066                                    query.append(_SQL_COUNT_ORGANIZATION_WHERE);
2067    
2068                                    query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
2069    
2070                                    if (name == null) {
2071                                            query.append(_FINDER_COLUMN_C_N_NAME_1);
2072                                    }
2073                                    else {
2074                                            if (name.equals(StringPool.BLANK)) {
2075                                                    query.append(_FINDER_COLUMN_C_N_NAME_3);
2076                                            }
2077                                            else {
2078                                                    query.append(_FINDER_COLUMN_C_N_NAME_2);
2079                                            }
2080                                    }
2081    
2082                                    String sql = query.toString();
2083    
2084                                    Query q = session.createQuery(sql);
2085    
2086                                    QueryPos qPos = QueryPos.getInstance(q);
2087    
2088                                    qPos.add(companyId);
2089    
2090                                    if (name != null) {
2091                                            qPos.add(name);
2092                                    }
2093    
2094                                    count = (Long)q.uniqueResult();
2095                            }
2096                            catch (Exception e) {
2097                                    throw processException(e);
2098                            }
2099                            finally {
2100                                    if (count == null) {
2101                                            count = Long.valueOf(0);
2102                                    }
2103    
2104                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N, finderArgs,
2105                                            count);
2106    
2107                                    closeSession(session);
2108                            }
2109                    }
2110    
2111                    return count.intValue();
2112            }
2113    
2114            /**
2115             * Counts all the organizations.
2116             *
2117             * @return the number of organizations
2118             * @throws SystemException if a system exception occurred
2119             */
2120            public int countAll() throws SystemException {
2121                    Object[] finderArgs = new Object[0];
2122    
2123                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
2124                                    finderArgs, this);
2125    
2126                    if (count == null) {
2127                            Session session = null;
2128    
2129                            try {
2130                                    session = openSession();
2131    
2132                                    Query q = session.createQuery(_SQL_COUNT_ORGANIZATION);
2133    
2134                                    count = (Long)q.uniqueResult();
2135                            }
2136                            catch (Exception e) {
2137                                    throw processException(e);
2138                            }
2139                            finally {
2140                                    if (count == null) {
2141                                            count = Long.valueOf(0);
2142                                    }
2143    
2144                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
2145                                            count);
2146    
2147                                    closeSession(session);
2148                            }
2149                    }
2150    
2151                    return count.intValue();
2152            }
2153    
2154            /**
2155             * Gets all the groups associated with the organization.
2156             *
2157             * @param pk the primary key of the organization to get the associated groups for
2158             * @return the groups associated with the organization
2159             * @throws SystemException if a system exception occurred
2160             */
2161            public List<com.liferay.portal.model.Group> getGroups(long pk)
2162                    throws SystemException {
2163                    return getGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
2164            }
2165    
2166            /**
2167             * Gets a range of all the groups associated with the organization.
2168             *
2169             * <p>
2170             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
2171             * </p>
2172             *
2173             * @param pk the primary key of the organization to get the associated groups for
2174             * @param start the lower bound of the range of organizations to return
2175             * @param end the upper bound of the range of organizations to return (not inclusive)
2176             * @return the range of groups associated with the organization
2177             * @throws SystemException if a system exception occurred
2178             */
2179            public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
2180                    int end) throws SystemException {
2181                    return getGroups(pk, start, end, null);
2182            }
2183    
2184            public static final FinderPath FINDER_PATH_GET_GROUPS = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
2185                            OrganizationModelImpl.FINDER_CACHE_ENABLED_GROUPS_ORGS,
2186                            OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME, "getGroups",
2187                            new String[] {
2188                                    Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
2189                                    "com.liferay.portal.kernel.util.OrderByComparator"
2190                            });
2191    
2192            /**
2193             * Gets an ordered range of all the groups associated with the organization.
2194             *
2195             * <p>
2196             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
2197             * </p>
2198             *
2199             * @param pk the primary key of the organization to get the associated groups for
2200             * @param start the lower bound of the range of organizations to return
2201             * @param end the upper bound of the range of organizations to return (not inclusive)
2202             * @param orderByComparator the comparator to order the results by
2203             * @return the ordered range of groups associated with the organization
2204             * @throws SystemException if a system exception occurred
2205             */
2206            public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
2207                    int end, OrderByComparator orderByComparator) throws SystemException {
2208                    Object[] finderArgs = new Object[] {
2209                                    pk, String.valueOf(start), String.valueOf(end),
2210                                    String.valueOf(orderByComparator)
2211                            };
2212    
2213                    List<com.liferay.portal.model.Group> list = (List<com.liferay.portal.model.Group>)FinderCacheUtil.getResult(FINDER_PATH_GET_GROUPS,
2214                                    finderArgs, this);
2215    
2216                    if (list == null) {
2217                            Session session = null;
2218    
2219                            try {
2220                                    session = openSession();
2221    
2222                                    String sql = null;
2223    
2224                                    if (orderByComparator != null) {
2225                                            sql = _SQL_GETGROUPS.concat(ORDER_BY_CLAUSE)
2226                                                                                    .concat(orderByComparator.getOrderBy());
2227                                    }
2228                                    else {
2229                                            sql = _SQL_GETGROUPS.concat(com.liferay.portal.model.impl.GroupModelImpl.ORDER_BY_SQL);
2230                                    }
2231    
2232                                    SQLQuery q = session.createSQLQuery(sql);
2233    
2234                                    q.addEntity("Group_",
2235                                            com.liferay.portal.model.impl.GroupImpl.class);
2236    
2237                                    QueryPos qPos = QueryPos.getInstance(q);
2238    
2239                                    qPos.add(pk);
2240    
2241                                    list = (List<com.liferay.portal.model.Group>)QueryUtil.list(q,
2242                                                    getDialect(), start, end);
2243                            }
2244                            catch (Exception e) {
2245                                    throw processException(e);
2246                            }
2247                            finally {
2248                                    if (list == null) {
2249                                            list = new ArrayList<com.liferay.portal.model.Group>();
2250                                    }
2251    
2252                                    groupPersistence.cacheResult(list);
2253    
2254                                    FinderCacheUtil.putResult(FINDER_PATH_GET_GROUPS, finderArgs,
2255                                            list);
2256    
2257                                    closeSession(session);
2258                            }
2259                    }
2260    
2261                    return list;
2262            }
2263    
2264            public static final FinderPath FINDER_PATH_GET_GROUPS_SIZE = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
2265                            OrganizationModelImpl.FINDER_CACHE_ENABLED_GROUPS_ORGS,
2266                            OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME,
2267                            "getGroupsSize", new String[] { Long.class.getName() });
2268    
2269            /**
2270             * Gets the number of groups associated with the organization.
2271             *
2272             * @param pk the primary key of the organization to get the number of associated groups for
2273             * @return the number of groups associated with the organization
2274             * @throws SystemException if a system exception occurred
2275             */
2276            public int getGroupsSize(long pk) throws SystemException {
2277                    Object[] finderArgs = new Object[] { pk };
2278    
2279                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_GROUPS_SIZE,
2280                                    finderArgs, this);
2281    
2282                    if (count == null) {
2283                            Session session = null;
2284    
2285                            try {
2286                                    session = openSession();
2287    
2288                                    SQLQuery q = session.createSQLQuery(_SQL_GETGROUPSSIZE);
2289    
2290                                    q.addScalar(COUNT_COLUMN_NAME,
2291                                            com.liferay.portal.kernel.dao.orm.Type.LONG);
2292    
2293                                    QueryPos qPos = QueryPos.getInstance(q);
2294    
2295                                    qPos.add(pk);
2296    
2297                                    count = (Long)q.uniqueResult();
2298                            }
2299                            catch (Exception e) {
2300                                    throw processException(e);
2301                            }
2302                            finally {
2303                                    if (count == null) {
2304                                            count = Long.valueOf(0);
2305                                    }
2306    
2307                                    FinderCacheUtil.putResult(FINDER_PATH_GET_GROUPS_SIZE,
2308                                            finderArgs, count);
2309    
2310                                    closeSession(session);
2311                            }
2312                    }
2313    
2314                    return count.intValue();
2315            }
2316    
2317            public static final FinderPath FINDER_PATH_CONTAINS_GROUP = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
2318                            OrganizationModelImpl.FINDER_CACHE_ENABLED_GROUPS_ORGS,
2319                            OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME,
2320                            "containsGroup",
2321                            new String[] { Long.class.getName(), Long.class.getName() });
2322    
2323            /**
2324             * Determines whether the group is associated with the organization.
2325             *
2326             * @param pk the primary key of the organization
2327             * @param groupPK the primary key of the group
2328             * @return whether the group is associated with the organization
2329             * @throws SystemException if a system exception occurred
2330             */
2331            public boolean containsGroup(long pk, long groupPK)
2332                    throws SystemException {
2333                    Object[] finderArgs = new Object[] { pk, groupPK };
2334    
2335                    Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_GROUP,
2336                                    finderArgs, this);
2337    
2338                    if (value == null) {
2339                            try {
2340                                    value = Boolean.valueOf(containsGroup.contains(pk, groupPK));
2341                            }
2342                            catch (Exception e) {
2343                                    throw processException(e);
2344                            }
2345                            finally {
2346                                    if (value == null) {
2347                                            value = Boolean.FALSE;
2348                                    }
2349    
2350                                    FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_GROUP,
2351                                            finderArgs, value);
2352                            }
2353                    }
2354    
2355                    return value.booleanValue();
2356            }
2357    
2358            /**
2359             * Determines whether the organization has any groups associated with it.
2360             *
2361             * @param pk the primary key of the organization to check for associations with groups
2362             * @return whether the organization has any groups associated with it
2363             * @throws SystemException if a system exception occurred
2364             */
2365            public boolean containsGroups(long pk) throws SystemException {
2366                    if (getGroupsSize(pk) > 0) {
2367                            return true;
2368                    }
2369                    else {
2370                            return false;
2371                    }
2372            }
2373    
2374            /**
2375             * Adds an association between the organization and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2376             *
2377             * @param pk the primary key of the organization
2378             * @param groupPK the primary key of the group
2379             * @throws SystemException if a system exception occurred
2380             */
2381            public void addGroup(long pk, long groupPK) throws SystemException {
2382                    try {
2383                            addGroup.add(pk, groupPK);
2384                    }
2385                    catch (Exception e) {
2386                            throw processException(e);
2387                    }
2388                    finally {
2389                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2390                    }
2391            }
2392    
2393            /**
2394             * Adds an association between the organization and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2395             *
2396             * @param pk the primary key of the organization
2397             * @param group the group
2398             * @throws SystemException if a system exception occurred
2399             */
2400            public void addGroup(long pk, com.liferay.portal.model.Group group)
2401                    throws SystemException {
2402                    try {
2403                            addGroup.add(pk, group.getPrimaryKey());
2404                    }
2405                    catch (Exception e) {
2406                            throw processException(e);
2407                    }
2408                    finally {
2409                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2410                    }
2411            }
2412    
2413            /**
2414             * Adds an association between the organization and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2415             *
2416             * @param pk the primary key of the organization
2417             * @param groupPKs the primary keys of the groups
2418             * @throws SystemException if a system exception occurred
2419             */
2420            public void addGroups(long pk, long[] groupPKs) throws SystemException {
2421                    try {
2422                            for (long groupPK : groupPKs) {
2423                                    addGroup.add(pk, groupPK);
2424                            }
2425                    }
2426                    catch (Exception e) {
2427                            throw processException(e);
2428                    }
2429                    finally {
2430                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2431                    }
2432            }
2433    
2434            /**
2435             * Adds an association between the organization and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2436             *
2437             * @param pk the primary key of the organization
2438             * @param groups the groups
2439             * @throws SystemException if a system exception occurred
2440             */
2441            public void addGroups(long pk, List<com.liferay.portal.model.Group> groups)
2442                    throws SystemException {
2443                    try {
2444                            for (com.liferay.portal.model.Group group : groups) {
2445                                    addGroup.add(pk, group.getPrimaryKey());
2446                            }
2447                    }
2448                    catch (Exception e) {
2449                            throw processException(e);
2450                    }
2451                    finally {
2452                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2453                    }
2454            }
2455    
2456            /**
2457             * Clears all associations between the organization and its groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2458             *
2459             * @param pk the primary key of the organization to clear the associated groups from
2460             * @throws SystemException if a system exception occurred
2461             */
2462            public void clearGroups(long pk) throws SystemException {
2463                    try {
2464                            clearGroups.clear(pk);
2465                    }
2466                    catch (Exception e) {
2467                            throw processException(e);
2468                    }
2469                    finally {
2470                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2471                    }
2472            }
2473    
2474            /**
2475             * Removes the association between the organization and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2476             *
2477             * @param pk the primary key of the organization
2478             * @param groupPK the primary key of the group
2479             * @throws SystemException if a system exception occurred
2480             */
2481            public void removeGroup(long pk, long groupPK) throws SystemException {
2482                    try {
2483                            removeGroup.remove(pk, groupPK);
2484                    }
2485                    catch (Exception e) {
2486                            throw processException(e);
2487                    }
2488                    finally {
2489                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2490                    }
2491            }
2492    
2493            /**
2494             * Removes the association between the organization and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2495             *
2496             * @param pk the primary key of the organization
2497             * @param group the group
2498             * @throws SystemException if a system exception occurred
2499             */
2500            public void removeGroup(long pk, com.liferay.portal.model.Group group)
2501                    throws SystemException {
2502                    try {
2503                            removeGroup.remove(pk, group.getPrimaryKey());
2504                    }
2505                    catch (Exception e) {
2506                            throw processException(e);
2507                    }
2508                    finally {
2509                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2510                    }
2511            }
2512    
2513            /**
2514             * Removes the association between the organization and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2515             *
2516             * @param pk the primary key of the organization
2517             * @param groupPKs the primary keys of the groups
2518             * @throws SystemException if a system exception occurred
2519             */
2520            public void removeGroups(long pk, long[] groupPKs)
2521                    throws SystemException {
2522                    try {
2523                            for (long groupPK : groupPKs) {
2524                                    removeGroup.remove(pk, groupPK);
2525                            }
2526                    }
2527                    catch (Exception e) {
2528                            throw processException(e);
2529                    }
2530                    finally {
2531                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2532                    }
2533            }
2534    
2535            /**
2536             * Removes the association between the organization and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2537             *
2538             * @param pk the primary key of the organization
2539             * @param groups the groups
2540             * @throws SystemException if a system exception occurred
2541             */
2542            public void removeGroups(long pk,
2543                    List<com.liferay.portal.model.Group> groups) throws SystemException {
2544                    try {
2545                            for (com.liferay.portal.model.Group group : groups) {
2546                                    removeGroup.remove(pk, group.getPrimaryKey());
2547                            }
2548                    }
2549                    catch (Exception e) {
2550                            throw processException(e);
2551                    }
2552                    finally {
2553                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2554                    }
2555            }
2556    
2557            /**
2558             * Sets the groups associated with the organization, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2559             *
2560             * @param pk the primary key of the organization to set the associations for
2561             * @param groupPKs the primary keys of the groups to be associated with the organization
2562             * @throws SystemException if a system exception occurred
2563             */
2564            public void setGroups(long pk, long[] groupPKs) throws SystemException {
2565                    try {
2566                            Set<Long> groupPKSet = SetUtil.fromArray(groupPKs);
2567    
2568                            List<com.liferay.portal.model.Group> groups = getGroups(pk);
2569    
2570                            for (com.liferay.portal.model.Group group : groups) {
2571                                    if (!groupPKSet.remove(group.getPrimaryKey())) {
2572                                            removeGroup.remove(pk, group.getPrimaryKey());
2573                                    }
2574                            }
2575    
2576                            for (Long groupPK : groupPKSet) {
2577                                    addGroup.add(pk, groupPK);
2578                            }
2579                    }
2580                    catch (Exception e) {
2581                            throw processException(e);
2582                    }
2583                    finally {
2584                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2585                    }
2586            }
2587    
2588            /**
2589             * Sets the groups associated with the organization, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2590             *
2591             * @param pk the primary key of the organization to set the associations for
2592             * @param groups the groups to be associated with the organization
2593             * @throws SystemException if a system exception occurred
2594             */
2595            public void setGroups(long pk, List<com.liferay.portal.model.Group> groups)
2596                    throws SystemException {
2597                    try {
2598                            long[] groupPKs = new long[groups.size()];
2599    
2600                            for (int i = 0; i < groups.size(); i++) {
2601                                    com.liferay.portal.model.Group group = groups.get(i);
2602    
2603                                    groupPKs[i] = group.getPrimaryKey();
2604                            }
2605    
2606                            setGroups(pk, groupPKs);
2607                    }
2608                    catch (Exception e) {
2609                            throw processException(e);
2610                    }
2611                    finally {
2612                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_GROUPS_ORGS_NAME);
2613                    }
2614            }
2615    
2616            /**
2617             * Gets all the users associated with the organization.
2618             *
2619             * @param pk the primary key of the organization to get the associated users for
2620             * @return the users associated with the organization
2621             * @throws SystemException if a system exception occurred
2622             */
2623            public List<com.liferay.portal.model.User> getUsers(long pk)
2624                    throws SystemException {
2625                    return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
2626            }
2627    
2628            /**
2629             * Gets a range of all the users associated with the organization.
2630             *
2631             * <p>
2632             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
2633             * </p>
2634             *
2635             * @param pk the primary key of the organization to get the associated users for
2636             * @param start the lower bound of the range of organizations to return
2637             * @param end the upper bound of the range of organizations to return (not inclusive)
2638             * @return the range of users associated with the organization
2639             * @throws SystemException if a system exception occurred
2640             */
2641            public List<com.liferay.portal.model.User> getUsers(long pk, int start,
2642                    int end) throws SystemException {
2643                    return getUsers(pk, start, end, null);
2644            }
2645    
2646            public static final FinderPath FINDER_PATH_GET_USERS = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2647                            OrganizationModelImpl.FINDER_CACHE_ENABLED_USERS_ORGS,
2648                            OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME, "getUsers",
2649                            new String[] {
2650                                    Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
2651                                    "com.liferay.portal.kernel.util.OrderByComparator"
2652                            });
2653    
2654            /**
2655             * Gets an ordered range of all the users associated with the organization.
2656             *
2657             * <p>
2658             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
2659             * </p>
2660             *
2661             * @param pk the primary key of the organization to get the associated users for
2662             * @param start the lower bound of the range of organizations to return
2663             * @param end the upper bound of the range of organizations to return (not inclusive)
2664             * @param orderByComparator the comparator to order the results by
2665             * @return the ordered range of users associated with the organization
2666             * @throws SystemException if a system exception occurred
2667             */
2668            public List<com.liferay.portal.model.User> getUsers(long pk, int start,
2669                    int end, OrderByComparator orderByComparator) throws SystemException {
2670                    Object[] finderArgs = new Object[] {
2671                                    pk, String.valueOf(start), String.valueOf(end),
2672                                    String.valueOf(orderByComparator)
2673                            };
2674    
2675                    List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS,
2676                                    finderArgs, this);
2677    
2678                    if (list == null) {
2679                            Session session = null;
2680    
2681                            try {
2682                                    session = openSession();
2683    
2684                                    String sql = null;
2685    
2686                                    if (orderByComparator != null) {
2687                                            sql = _SQL_GETUSERS.concat(ORDER_BY_CLAUSE)
2688                                                                               .concat(orderByComparator.getOrderBy());
2689                                    }
2690                                    else {
2691                                            sql = _SQL_GETUSERS;
2692                                    }
2693    
2694                                    SQLQuery q = session.createSQLQuery(sql);
2695    
2696                                    q.addEntity("User_",
2697                                            com.liferay.portal.model.impl.UserImpl.class);
2698    
2699                                    QueryPos qPos = QueryPos.getInstance(q);
2700    
2701                                    qPos.add(pk);
2702    
2703                                    list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
2704                                                    getDialect(), start, end);
2705                            }
2706                            catch (Exception e) {
2707                                    throw processException(e);
2708                            }
2709                            finally {
2710                                    if (list == null) {
2711                                            list = new ArrayList<com.liferay.portal.model.User>();
2712                                    }
2713    
2714                                    userPersistence.cacheResult(list);
2715    
2716                                    FinderCacheUtil.putResult(FINDER_PATH_GET_USERS, finderArgs,
2717                                            list);
2718    
2719                                    closeSession(session);
2720                            }
2721                    }
2722    
2723                    return list;
2724            }
2725    
2726            public static final FinderPath FINDER_PATH_GET_USERS_SIZE = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2727                            OrganizationModelImpl.FINDER_CACHE_ENABLED_USERS_ORGS,
2728                            OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME,
2729                            "getUsersSize", new String[] { Long.class.getName() });
2730    
2731            /**
2732             * Gets the number of users associated with the organization.
2733             *
2734             * @param pk the primary key of the organization to get the number of associated users for
2735             * @return the number of users associated with the organization
2736             * @throws SystemException if a system exception occurred
2737             */
2738            public int getUsersSize(long pk) throws SystemException {
2739                    Object[] finderArgs = new Object[] { pk };
2740    
2741                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS_SIZE,
2742                                    finderArgs, this);
2743    
2744                    if (count == null) {
2745                            Session session = null;
2746    
2747                            try {
2748                                    session = openSession();
2749    
2750                                    SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
2751    
2752                                    q.addScalar(COUNT_COLUMN_NAME,
2753                                            com.liferay.portal.kernel.dao.orm.Type.LONG);
2754    
2755                                    QueryPos qPos = QueryPos.getInstance(q);
2756    
2757                                    qPos.add(pk);
2758    
2759                                    count = (Long)q.uniqueResult();
2760                            }
2761                            catch (Exception e) {
2762                                    throw processException(e);
2763                            }
2764                            finally {
2765                                    if (count == null) {
2766                                            count = Long.valueOf(0);
2767                                    }
2768    
2769                                    FinderCacheUtil.putResult(FINDER_PATH_GET_USERS_SIZE,
2770                                            finderArgs, count);
2771    
2772                                    closeSession(session);
2773                            }
2774                    }
2775    
2776                    return count.intValue();
2777            }
2778    
2779            public static final FinderPath FINDER_PATH_CONTAINS_USER = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2780                            OrganizationModelImpl.FINDER_CACHE_ENABLED_USERS_ORGS,
2781                            OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME,
2782                            "containsUser",
2783                            new String[] { Long.class.getName(), Long.class.getName() });
2784    
2785            /**
2786             * Determines whether the user is associated with the organization.
2787             *
2788             * @param pk the primary key of the organization
2789             * @param userPK the primary key of the user
2790             * @return whether the user is associated with the organization
2791             * @throws SystemException if a system exception occurred
2792             */
2793            public boolean containsUser(long pk, long userPK) throws SystemException {
2794                    Object[] finderArgs = new Object[] { pk, userPK };
2795    
2796                    Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_USER,
2797                                    finderArgs, this);
2798    
2799                    if (value == null) {
2800                            try {
2801                                    value = Boolean.valueOf(containsUser.contains(pk, userPK));
2802                            }
2803                            catch (Exception e) {
2804                                    throw processException(e);
2805                            }
2806                            finally {
2807                                    if (value == null) {
2808                                            value = Boolean.FALSE;
2809                                    }
2810    
2811                                    FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_USER,
2812                                            finderArgs, value);
2813                            }
2814                    }
2815    
2816                    return value.booleanValue();
2817            }
2818    
2819            /**
2820             * Determines whether the organization has any users associated with it.
2821             *
2822             * @param pk the primary key of the organization to check for associations with users
2823             * @return whether the organization has any users associated with it
2824             * @throws SystemException if a system exception occurred
2825             */
2826            public boolean containsUsers(long pk) throws SystemException {
2827                    if (getUsersSize(pk) > 0) {
2828                            return true;
2829                    }
2830                    else {
2831                            return false;
2832                    }
2833            }
2834    
2835            /**
2836             * Adds an association between the organization and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2837             *
2838             * @param pk the primary key of the organization
2839             * @param userPK the primary key of the user
2840             * @throws SystemException if a system exception occurred
2841             */
2842            public void addUser(long pk, long userPK) throws SystemException {
2843                    try {
2844                            addUser.add(pk, userPK);
2845                    }
2846                    catch (Exception e) {
2847                            throw processException(e);
2848                    }
2849                    finally {
2850                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2851                    }
2852            }
2853    
2854            /**
2855             * Adds an association between the organization and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2856             *
2857             * @param pk the primary key of the organization
2858             * @param user the user
2859             * @throws SystemException if a system exception occurred
2860             */
2861            public void addUser(long pk, com.liferay.portal.model.User user)
2862                    throws SystemException {
2863                    try {
2864                            addUser.add(pk, user.getPrimaryKey());
2865                    }
2866                    catch (Exception e) {
2867                            throw processException(e);
2868                    }
2869                    finally {
2870                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2871                    }
2872            }
2873    
2874            /**
2875             * Adds an association between the organization and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2876             *
2877             * @param pk the primary key of the organization
2878             * @param userPKs the primary keys of the users
2879             * @throws SystemException if a system exception occurred
2880             */
2881            public void addUsers(long pk, long[] userPKs) throws SystemException {
2882                    try {
2883                            for (long userPK : userPKs) {
2884                                    addUser.add(pk, userPK);
2885                            }
2886                    }
2887                    catch (Exception e) {
2888                            throw processException(e);
2889                    }
2890                    finally {
2891                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2892                    }
2893            }
2894    
2895            /**
2896             * Adds an association between the organization and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2897             *
2898             * @param pk the primary key of the organization
2899             * @param users the users
2900             * @throws SystemException if a system exception occurred
2901             */
2902            public void addUsers(long pk, List<com.liferay.portal.model.User> users)
2903                    throws SystemException {
2904                    try {
2905                            for (com.liferay.portal.model.User user : users) {
2906                                    addUser.add(pk, user.getPrimaryKey());
2907                            }
2908                    }
2909                    catch (Exception e) {
2910                            throw processException(e);
2911                    }
2912                    finally {
2913                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2914                    }
2915            }
2916    
2917            /**
2918             * Clears all associations between the organization and its users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2919             *
2920             * @param pk the primary key of the organization to clear the associated users from
2921             * @throws SystemException if a system exception occurred
2922             */
2923            public void clearUsers(long pk) throws SystemException {
2924                    try {
2925                            clearUsers.clear(pk);
2926                    }
2927                    catch (Exception e) {
2928                            throw processException(e);
2929                    }
2930                    finally {
2931                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2932                    }
2933            }
2934    
2935            /**
2936             * Removes the association between the organization and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2937             *
2938             * @param pk the primary key of the organization
2939             * @param userPK the primary key of the user
2940             * @throws SystemException if a system exception occurred
2941             */
2942            public void removeUser(long pk, long userPK) throws SystemException {
2943                    try {
2944                            removeUser.remove(pk, userPK);
2945                    }
2946                    catch (Exception e) {
2947                            throw processException(e);
2948                    }
2949                    finally {
2950                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2951                    }
2952            }
2953    
2954            /**
2955             * Removes the association between the organization and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2956             *
2957             * @param pk the primary key of the organization
2958             * @param user the user
2959             * @throws SystemException if a system exception occurred
2960             */
2961            public void removeUser(long pk, com.liferay.portal.model.User user)
2962                    throws SystemException {
2963                    try {
2964                            removeUser.remove(pk, user.getPrimaryKey());
2965                    }
2966                    catch (Exception e) {
2967                            throw processException(e);
2968                    }
2969                    finally {
2970                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2971                    }
2972            }
2973    
2974            /**
2975             * Removes the association between the organization and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2976             *
2977             * @param pk the primary key of the organization
2978             * @param userPKs the primary keys of the users
2979             * @throws SystemException if a system exception occurred
2980             */
2981            public void removeUsers(long pk, long[] userPKs) throws SystemException {
2982                    try {
2983                            for (long userPK : userPKs) {
2984                                    removeUser.remove(pk, userPK);
2985                            }
2986                    }
2987                    catch (Exception e) {
2988                            throw processException(e);
2989                    }
2990                    finally {
2991                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
2992                    }
2993            }
2994    
2995            /**
2996             * Removes the association between the organization and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2997             *
2998             * @param pk the primary key of the organization
2999             * @param users the users
3000             * @throws SystemException if a system exception occurred
3001             */
3002            public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
3003                    throws SystemException {
3004                    try {
3005                            for (com.liferay.portal.model.User user : users) {
3006                                    removeUser.remove(pk, user.getPrimaryKey());
3007                            }
3008                    }
3009                    catch (Exception e) {
3010                            throw processException(e);
3011                    }
3012                    finally {
3013                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
3014                    }
3015            }
3016    
3017            /**
3018             * Sets the users associated with the organization, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
3019             *
3020             * @param pk the primary key of the organization to set the associations for
3021             * @param userPKs the primary keys of the users to be associated with the organization
3022             * @throws SystemException if a system exception occurred
3023             */
3024            public void setUsers(long pk, long[] userPKs) throws SystemException {
3025                    try {
3026                            Set<Long> userPKSet = SetUtil.fromArray(userPKs);
3027    
3028                            List<com.liferay.portal.model.User> users = getUsers(pk);
3029    
3030                            for (com.liferay.portal.model.User user : users) {
3031                                    if (!userPKSet.remove(user.getPrimaryKey())) {
3032                                            removeUser.remove(pk, user.getPrimaryKey());
3033                                    }
3034                            }
3035    
3036                            for (Long userPK : userPKSet) {
3037                                    addUser.add(pk, userPK);
3038                            }
3039                    }
3040                    catch (Exception e) {
3041                            throw processException(e);
3042                    }
3043                    finally {
3044                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
3045                    }
3046            }
3047    
3048            /**
3049             * Sets the users associated with the organization, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
3050             *
3051             * @param pk the primary key of the organization to set the associations for
3052             * @param users the users to be associated with the organization
3053             * @throws SystemException if a system exception occurred
3054             */
3055            public void setUsers(long pk, List<com.liferay.portal.model.User> users)
3056                    throws SystemException {
3057                    try {
3058                            long[] userPKs = new long[users.size()];
3059    
3060                            for (int i = 0; i < users.size(); i++) {
3061                                    com.liferay.portal.model.User user = users.get(i);
3062    
3063                                    userPKs[i] = user.getPrimaryKey();
3064                            }
3065    
3066                            setUsers(pk, userPKs);
3067                    }
3068                    catch (Exception e) {
3069                            throw processException(e);
3070                    }
3071                    finally {
3072                            FinderCacheUtil.clearCache(OrganizationModelImpl.MAPPING_TABLE_USERS_ORGS_NAME);
3073                    }
3074            }
3075    
3076            /**
3077             * Rebuilds the organizations tree for the scope using the modified pre-order tree traversal algorithm.
3078             *
3079             * <p>
3080             * Only call this method if the tree has become stale through operations other than normal CRUD. Under normal circumstances the tree is automatically rebuilt whenver necessary.
3081             * </p>
3082             *
3083             * @param companyId the id of the scope to rebuild the tree for
3084             * @param force whether to force the rebuild even if the tree is not stale
3085             */
3086            public void rebuildTree(long companyId, boolean force)
3087                    throws SystemException {
3088                    if (force || (countOrphanTreeNodes(companyId) > 0)) {
3089                            rebuildTree(companyId, 0, 1);
3090    
3091                            CacheRegistryUtil.clear(OrganizationImpl.class.getName());
3092                            EntityCacheUtil.clearCache(OrganizationImpl.class.getName());
3093                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
3094                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
3095                    }
3096            }
3097    
3098            protected long countOrphanTreeNodes(long companyId)
3099                    throws SystemException {
3100                    Session session = null;
3101    
3102                    try {
3103                            session = openSession();
3104    
3105                            SQLQuery q = session.createSQLQuery(
3106                                            "SELECT COUNT(*) AS COUNT_VALUE FROM Organization_ WHERE companyId = ? AND (leftOrganizationId = 0 OR leftOrganizationId IS NULL OR rightOrganizationId = 0 OR rightOrganizationId IS NULL)");
3107    
3108                            q.addScalar(COUNT_COLUMN_NAME,
3109                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
3110    
3111                            QueryPos qPos = QueryPos.getInstance(q);
3112    
3113                            qPos.add(companyId);
3114    
3115                            return (Long)q.uniqueResult();
3116                    }
3117                    catch (Exception e) {
3118                            throw processException(e);
3119                    }
3120                    finally {
3121                            closeSession(session);
3122                    }
3123            }
3124    
3125            protected void expandTree(Organization organization)
3126                    throws SystemException {
3127                    long companyId = organization.getCompanyId();
3128    
3129                    long lastRightOrganizationId = getLastRightOrganizationId(companyId,
3130                                    organization.getParentOrganizationId());
3131    
3132                    long leftOrganizationId = 2;
3133                    long rightOrganizationId = 3;
3134    
3135                    if (lastRightOrganizationId > 0) {
3136                            leftOrganizationId = lastRightOrganizationId + 1;
3137                            rightOrganizationId = lastRightOrganizationId + 2;
3138    
3139                            expandTreeLeftOrganizationId.expand(companyId,
3140                                    lastRightOrganizationId);
3141                            expandTreeRightOrganizationId.expand(companyId,
3142                                    lastRightOrganizationId);
3143    
3144                            CacheRegistryUtil.clear(OrganizationImpl.class.getName());
3145                            EntityCacheUtil.clearCache(OrganizationImpl.class.getName());
3146                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
3147                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
3148                    }
3149    
3150                    organization.setLeftOrganizationId(leftOrganizationId);
3151                    organization.setRightOrganizationId(rightOrganizationId);
3152            }
3153    
3154            protected long getLastRightOrganizationId(long companyId,
3155                    long parentOrganizationId) throws SystemException {
3156                    Session session = null;
3157    
3158                    try {
3159                            session = openSession();
3160    
3161                            SQLQuery q = session.createSQLQuery(
3162                                            "SELECT rightOrganizationId FROM Organization_ WHERE (companyId = ?) AND (parentOrganizationId = ?) ORDER BY rightOrganizationId DESC");
3163    
3164                            q.addScalar("rightOrganizationId",
3165                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
3166    
3167                            QueryPos qPos = QueryPos.getInstance(q);
3168    
3169                            qPos.add(companyId);
3170                            qPos.add(parentOrganizationId);
3171    
3172                            List<Long> list = (List<Long>)QueryUtil.list(q, getDialect(), 0, 1);
3173    
3174                            if (list.isEmpty()) {
3175                                    if (parentOrganizationId > 0) {
3176                                            Organization parentOrganization = findByPrimaryKey(parentOrganizationId);
3177    
3178                                            return parentOrganization.getLeftOrganizationId();
3179                                    }
3180    
3181                                    return 0;
3182                            }
3183                            else {
3184                                    return list.get(0);
3185                            }
3186                    }
3187                    catch (Exception e) {
3188                            throw processException(e);
3189                    }
3190                    finally {
3191                            closeSession(session);
3192                    }
3193            }
3194    
3195            protected long rebuildTree(long companyId, long parentOrganizationId,
3196                    long leftOrganizationId) throws SystemException {
3197                    List<Long> organizationIds = null;
3198    
3199                    Session session = null;
3200    
3201                    try {
3202                            session = openSession();
3203    
3204                            SQLQuery q = session.createSQLQuery(
3205                                            "SELECT organizationId FROM Organization_ WHERE companyId = ? AND parentOrganizationId = ? ORDER BY organizationId ASC");
3206    
3207                            q.addScalar("organizationId",
3208                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
3209    
3210                            QueryPos qPos = QueryPos.getInstance(q);
3211    
3212                            qPos.add(companyId);
3213                            qPos.add(parentOrganizationId);
3214    
3215                            organizationIds = q.list();
3216                    }
3217                    catch (Exception e) {
3218                            throw processException(e);
3219                    }
3220                    finally {
3221                            closeSession(session);
3222                    }
3223    
3224                    long rightOrganizationId = leftOrganizationId + 1;
3225    
3226                    for (long organizationId : organizationIds) {
3227                            rightOrganizationId = rebuildTree(companyId, organizationId,
3228                                            rightOrganizationId);
3229                    }
3230    
3231                    if (parentOrganizationId > 0) {
3232                            updateTree.update(parentOrganizationId, leftOrganizationId,
3233                                    rightOrganizationId);
3234                    }
3235    
3236                    return rightOrganizationId + 1;
3237            }
3238    
3239            protected void shrinkTree(Organization organization) {
3240                    long companyId = organization.getCompanyId();
3241    
3242                    long leftOrganizationId = organization.getLeftOrganizationId();
3243                    long rightOrganizationId = organization.getRightOrganizationId();
3244    
3245                    long delta = (rightOrganizationId - leftOrganizationId) + 1;
3246    
3247                    shrinkTreeLeftOrganizationId.shrink(companyId, rightOrganizationId,
3248                            delta);
3249                    shrinkTreeRightOrganizationId.shrink(companyId, rightOrganizationId,
3250                            delta);
3251    
3252                    CacheRegistryUtil.clear(OrganizationImpl.class.getName());
3253                    EntityCacheUtil.clearCache(OrganizationImpl.class.getName());
3254                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
3255                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
3256            }
3257    
3258            /**
3259             * Initializes the organization persistence.
3260             */
3261            public void afterPropertiesSet() {
3262                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
3263                                            com.liferay.portal.util.PropsUtil.get(
3264                                                    "value.object.listener.com.liferay.portal.model.Organization")));
3265    
3266                    if (listenerClassNames.length > 0) {
3267                            try {
3268                                    List<ModelListener<Organization>> listenersList = new ArrayList<ModelListener<Organization>>();
3269    
3270                                    for (String listenerClassName : listenerClassNames) {
3271                                            listenersList.add((ModelListener<Organization>)InstanceFactory.newInstance(
3272                                                            listenerClassName));
3273                                    }
3274    
3275                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
3276                            }
3277                            catch (Exception e) {
3278                                    _log.error(e);
3279                            }
3280                    }
3281    
3282                    containsGroup = new ContainsGroup(this);
3283    
3284                    addGroup = new AddGroup(this);
3285                    clearGroups = new ClearGroups(this);
3286                    removeGroup = new RemoveGroup(this);
3287    
3288                    containsUser = new ContainsUser(this);
3289    
3290                    addUser = new AddUser(this);
3291                    clearUsers = new ClearUsers(this);
3292                    removeUser = new RemoveUser(this);
3293    
3294                    expandTreeLeftOrganizationId = new ExpandTreeLeftOrganizationId();
3295                    expandTreeRightOrganizationId = new ExpandTreeRightOrganizationId();
3296                    shrinkTreeLeftOrganizationId = new ShrinkTreeLeftOrganizationId();
3297                    shrinkTreeRightOrganizationId = new ShrinkTreeRightOrganizationId();
3298                    updateTree = new UpdateTree();
3299            }
3300    
3301            @BeanReference(type = AccountPersistence.class)
3302            protected AccountPersistence accountPersistence;
3303            @BeanReference(type = AddressPersistence.class)
3304            protected AddressPersistence addressPersistence;
3305            @BeanReference(type = BrowserTrackerPersistence.class)
3306            protected BrowserTrackerPersistence browserTrackerPersistence;
3307            @BeanReference(type = ClassNamePersistence.class)
3308            protected ClassNamePersistence classNamePersistence;
3309            @BeanReference(type = ClusterGroupPersistence.class)
3310            protected ClusterGroupPersistence clusterGroupPersistence;
3311            @BeanReference(type = CompanyPersistence.class)
3312            protected CompanyPersistence companyPersistence;
3313            @BeanReference(type = ContactPersistence.class)
3314            protected ContactPersistence contactPersistence;
3315            @BeanReference(type = CountryPersistence.class)
3316            protected CountryPersistence countryPersistence;
3317            @BeanReference(type = EmailAddressPersistence.class)
3318            protected EmailAddressPersistence emailAddressPersistence;
3319            @BeanReference(type = GroupPersistence.class)
3320            protected GroupPersistence groupPersistence;
3321            @BeanReference(type = ImagePersistence.class)
3322            protected ImagePersistence imagePersistence;
3323            @BeanReference(type = LayoutPersistence.class)
3324            protected LayoutPersistence layoutPersistence;
3325            @BeanReference(type = LayoutPrototypePersistence.class)
3326            protected LayoutPrototypePersistence layoutPrototypePersistence;
3327            @BeanReference(type = LayoutSetPersistence.class)
3328            protected LayoutSetPersistence layoutSetPersistence;
3329            @BeanReference(type = LayoutSetPrototypePersistence.class)
3330            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
3331            @BeanReference(type = ListTypePersistence.class)
3332            protected ListTypePersistence listTypePersistence;
3333            @BeanReference(type = LockPersistence.class)
3334            protected LockPersistence lockPersistence;
3335            @BeanReference(type = MembershipRequestPersistence.class)
3336            protected MembershipRequestPersistence membershipRequestPersistence;
3337            @BeanReference(type = OrganizationPersistence.class)
3338            protected OrganizationPersistence organizationPersistence;
3339            @BeanReference(type = OrgGroupPermissionPersistence.class)
3340            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
3341            @BeanReference(type = OrgGroupRolePersistence.class)
3342            protected OrgGroupRolePersistence orgGroupRolePersistence;
3343            @BeanReference(type = OrgLaborPersistence.class)
3344            protected OrgLaborPersistence orgLaborPersistence;
3345            @BeanReference(type = PasswordPolicyPersistence.class)
3346            protected PasswordPolicyPersistence passwordPolicyPersistence;
3347            @BeanReference(type = PasswordPolicyRelPersistence.class)
3348            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
3349            @BeanReference(type = PasswordTrackerPersistence.class)
3350            protected PasswordTrackerPersistence passwordTrackerPersistence;
3351            @BeanReference(type = PermissionPersistence.class)
3352            protected PermissionPersistence permissionPersistence;
3353            @BeanReference(type = PhonePersistence.class)
3354            protected PhonePersistence phonePersistence;
3355            @BeanReference(type = PluginSettingPersistence.class)
3356            protected PluginSettingPersistence pluginSettingPersistence;
3357            @BeanReference(type = PortletPersistence.class)
3358            protected PortletPersistence portletPersistence;
3359            @BeanReference(type = PortletItemPersistence.class)
3360            protected PortletItemPersistence portletItemPersistence;
3361            @BeanReference(type = PortletPreferencesPersistence.class)
3362            protected PortletPreferencesPersistence portletPreferencesPersistence;
3363            @BeanReference(type = RegionPersistence.class)
3364            protected RegionPersistence regionPersistence;
3365            @BeanReference(type = ReleasePersistence.class)
3366            protected ReleasePersistence releasePersistence;
3367            @BeanReference(type = ResourcePersistence.class)
3368            protected ResourcePersistence resourcePersistence;
3369            @BeanReference(type = ResourceActionPersistence.class)
3370            protected ResourceActionPersistence resourceActionPersistence;
3371            @BeanReference(type = ResourceCodePersistence.class)
3372            protected ResourceCodePersistence resourceCodePersistence;
3373            @BeanReference(type = ResourcePermissionPersistence.class)
3374            protected ResourcePermissionPersistence resourcePermissionPersistence;
3375            @BeanReference(type = RolePersistence.class)
3376            protected RolePersistence rolePersistence;
3377            @BeanReference(type = ServiceComponentPersistence.class)
3378            protected ServiceComponentPersistence serviceComponentPersistence;
3379            @BeanReference(type = ShardPersistence.class)
3380            protected ShardPersistence shardPersistence;
3381            @BeanReference(type = SubscriptionPersistence.class)
3382            protected SubscriptionPersistence subscriptionPersistence;
3383            @BeanReference(type = TicketPersistence.class)
3384            protected TicketPersistence ticketPersistence;
3385            @BeanReference(type = TeamPersistence.class)
3386            protected TeamPersistence teamPersistence;
3387            @BeanReference(type = UserPersistence.class)
3388            protected UserPersistence userPersistence;
3389            @BeanReference(type = UserGroupPersistence.class)
3390            protected UserGroupPersistence userGroupPersistence;
3391            @BeanReference(type = UserGroupGroupRolePersistence.class)
3392            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
3393            @BeanReference(type = UserGroupRolePersistence.class)
3394            protected UserGroupRolePersistence userGroupRolePersistence;
3395            @BeanReference(type = UserIdMapperPersistence.class)
3396            protected UserIdMapperPersistence userIdMapperPersistence;
3397            @BeanReference(type = UserTrackerPersistence.class)
3398            protected UserTrackerPersistence userTrackerPersistence;
3399            @BeanReference(type = UserTrackerPathPersistence.class)
3400            protected UserTrackerPathPersistence userTrackerPathPersistence;
3401            @BeanReference(type = WebDAVPropsPersistence.class)
3402            protected WebDAVPropsPersistence webDAVPropsPersistence;
3403            @BeanReference(type = WebsitePersistence.class)
3404            protected WebsitePersistence websitePersistence;
3405            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
3406            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
3407            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
3408            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
3409            @BeanReference(type = AssetEntryPersistence.class)
3410            protected AssetEntryPersistence assetEntryPersistence;
3411            @BeanReference(type = ExpandoValuePersistence.class)
3412            protected ExpandoValuePersistence expandoValuePersistence;
3413            protected ContainsGroup containsGroup;
3414            protected AddGroup addGroup;
3415            protected ClearGroups clearGroups;
3416            protected RemoveGroup removeGroup;
3417            protected ContainsUser containsUser;
3418            protected AddUser addUser;
3419            protected ClearUsers clearUsers;
3420            protected RemoveUser removeUser;
3421    
3422            protected class ContainsGroup {
3423                    protected ContainsGroup(OrganizationPersistenceImpl persistenceImpl) {
3424                            super();
3425    
3426                            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
3427                                            _SQL_CONTAINSGROUP,
3428                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT },
3429                                            RowMapper.COUNT);
3430                    }
3431    
3432                    protected boolean contains(long organizationId, long groupId) {
3433                            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
3434                                                    new Long(organizationId), new Long(groupId)
3435                                            });
3436    
3437                            if (results.size() > 0) {
3438                                    Integer count = results.get(0);
3439    
3440                                    if (count.intValue() > 0) {
3441                                            return true;
3442                                    }
3443                            }
3444    
3445                            return false;
3446                    }
3447    
3448                    private MappingSqlQuery<Integer> _mappingSqlQuery;
3449            }
3450    
3451            protected class AddGroup {
3452                    protected AddGroup(OrganizationPersistenceImpl persistenceImpl) {
3453                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3454                                            "INSERT INTO Groups_Orgs (organizationId, groupId) VALUES (?, ?)",
3455                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3456                            _persistenceImpl = persistenceImpl;
3457                    }
3458    
3459                    protected void add(long organizationId, long groupId)
3460                            throws SystemException {
3461                            if (!_persistenceImpl.containsGroup.contains(organizationId, groupId)) {
3462                                    ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
3463    
3464                                    for (ModelListener<Organization> listener : listeners) {
3465                                            listener.onBeforeAddAssociation(organizationId,
3466                                                    com.liferay.portal.model.Group.class.getName(), groupId);
3467                                    }
3468    
3469                                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3470                                            listener.onBeforeAddAssociation(groupId,
3471                                                    Organization.class.getName(), organizationId);
3472                                    }
3473    
3474                                    _sqlUpdate.update(new Object[] {
3475                                                    new Long(organizationId), new Long(groupId)
3476                                            });
3477    
3478                                    for (ModelListener<Organization> listener : listeners) {
3479                                            listener.onAfterAddAssociation(organizationId,
3480                                                    com.liferay.portal.model.Group.class.getName(), groupId);
3481                                    }
3482    
3483                                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3484                                            listener.onAfterAddAssociation(groupId,
3485                                                    Organization.class.getName(), organizationId);
3486                                    }
3487                            }
3488                    }
3489    
3490                    private SqlUpdate _sqlUpdate;
3491                    private OrganizationPersistenceImpl _persistenceImpl;
3492            }
3493    
3494            protected class ClearGroups {
3495                    protected ClearGroups(OrganizationPersistenceImpl persistenceImpl) {
3496                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3497                                            "DELETE FROM Groups_Orgs WHERE organizationId = ?",
3498                                            new int[] { java.sql.Types.BIGINT });
3499                    }
3500    
3501                    protected void clear(long organizationId) throws SystemException {
3502                            ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
3503    
3504                            List<com.liferay.portal.model.Group> groups = null;
3505    
3506                            if ((listeners.length > 0) || (groupListeners.length > 0)) {
3507                                    groups = getGroups(organizationId);
3508    
3509                                    for (com.liferay.portal.model.Group group : groups) {
3510                                            for (ModelListener<Organization> listener : listeners) {
3511                                                    listener.onBeforeRemoveAssociation(organizationId,
3512                                                            com.liferay.portal.model.Group.class.getName(),
3513                                                            group.getPrimaryKey());
3514                                            }
3515    
3516                                            for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3517                                                    listener.onBeforeRemoveAssociation(group.getPrimaryKey(),
3518                                                            Organization.class.getName(), organizationId);
3519                                            }
3520                                    }
3521                            }
3522    
3523                            _sqlUpdate.update(new Object[] { new Long(organizationId) });
3524    
3525                            if ((listeners.length > 0) || (groupListeners.length > 0)) {
3526                                    for (com.liferay.portal.model.Group group : groups) {
3527                                            for (ModelListener<Organization> listener : listeners) {
3528                                                    listener.onAfterRemoveAssociation(organizationId,
3529                                                            com.liferay.portal.model.Group.class.getName(),
3530                                                            group.getPrimaryKey());
3531                                            }
3532    
3533                                            for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3534                                                    listener.onAfterRemoveAssociation(group.getPrimaryKey(),
3535                                                            Organization.class.getName(), organizationId);
3536                                            }
3537                                    }
3538                            }
3539                    }
3540    
3541                    private SqlUpdate _sqlUpdate;
3542            }
3543    
3544            protected class RemoveGroup {
3545                    protected RemoveGroup(OrganizationPersistenceImpl persistenceImpl) {
3546                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3547                                            "DELETE FROM Groups_Orgs WHERE organizationId = ? AND groupId = ?",
3548                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3549                            _persistenceImpl = persistenceImpl;
3550                    }
3551    
3552                    protected void remove(long organizationId, long groupId)
3553                            throws SystemException {
3554                            if (_persistenceImpl.containsGroup.contains(organizationId, groupId)) {
3555                                    ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
3556    
3557                                    for (ModelListener<Organization> listener : listeners) {
3558                                            listener.onBeforeRemoveAssociation(organizationId,
3559                                                    com.liferay.portal.model.Group.class.getName(), groupId);
3560                                    }
3561    
3562                                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3563                                            listener.onBeforeRemoveAssociation(groupId,
3564                                                    Organization.class.getName(), organizationId);
3565                                    }
3566    
3567                                    _sqlUpdate.update(new Object[] {
3568                                                    new Long(organizationId), new Long(groupId)
3569                                            });
3570    
3571                                    for (ModelListener<Organization> listener : listeners) {
3572                                            listener.onAfterRemoveAssociation(organizationId,
3573                                                    com.liferay.portal.model.Group.class.getName(), groupId);
3574                                    }
3575    
3576                                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3577                                            listener.onAfterRemoveAssociation(groupId,
3578                                                    Organization.class.getName(), organizationId);
3579                                    }
3580                            }
3581                    }
3582    
3583                    private SqlUpdate _sqlUpdate;
3584                    private OrganizationPersistenceImpl _persistenceImpl;
3585            }
3586    
3587            protected class ContainsUser {
3588                    protected ContainsUser(OrganizationPersistenceImpl persistenceImpl) {
3589                            super();
3590    
3591                            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
3592                                            _SQL_CONTAINSUSER,
3593                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT },
3594                                            RowMapper.COUNT);
3595                    }
3596    
3597                    protected boolean contains(long organizationId, long userId) {
3598                            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
3599                                                    new Long(organizationId), new Long(userId)
3600                                            });
3601    
3602                            if (results.size() > 0) {
3603                                    Integer count = results.get(0);
3604    
3605                                    if (count.intValue() > 0) {
3606                                            return true;
3607                                    }
3608                            }
3609    
3610                            return false;
3611                    }
3612    
3613                    private MappingSqlQuery<Integer> _mappingSqlQuery;
3614            }
3615    
3616            protected class AddUser {
3617                    protected AddUser(OrganizationPersistenceImpl persistenceImpl) {
3618                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3619                                            "INSERT INTO Users_Orgs (organizationId, userId) VALUES (?, ?)",
3620                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3621                            _persistenceImpl = persistenceImpl;
3622                    }
3623    
3624                    protected void add(long organizationId, long userId)
3625                            throws SystemException {
3626                            if (!_persistenceImpl.containsUser.contains(organizationId, userId)) {
3627                                    ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
3628    
3629                                    for (ModelListener<Organization> listener : listeners) {
3630                                            listener.onBeforeAddAssociation(organizationId,
3631                                                    com.liferay.portal.model.User.class.getName(), userId);
3632                                    }
3633    
3634                                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3635                                            listener.onBeforeAddAssociation(userId,
3636                                                    Organization.class.getName(), organizationId);
3637                                    }
3638    
3639                                    _sqlUpdate.update(new Object[] {
3640                                                    new Long(organizationId), new Long(userId)
3641                                            });
3642    
3643                                    for (ModelListener<Organization> listener : listeners) {
3644                                            listener.onAfterAddAssociation(organizationId,
3645                                                    com.liferay.portal.model.User.class.getName(), userId);
3646                                    }
3647    
3648                                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3649                                            listener.onAfterAddAssociation(userId,
3650                                                    Organization.class.getName(), organizationId);
3651                                    }
3652                            }
3653                    }
3654    
3655                    private SqlUpdate _sqlUpdate;
3656                    private OrganizationPersistenceImpl _persistenceImpl;
3657            }
3658    
3659            protected class ClearUsers {
3660                    protected ClearUsers(OrganizationPersistenceImpl persistenceImpl) {
3661                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3662                                            "DELETE FROM Users_Orgs WHERE organizationId = ?",
3663                                            new int[] { java.sql.Types.BIGINT });
3664                    }
3665    
3666                    protected void clear(long organizationId) throws SystemException {
3667                            ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
3668    
3669                            List<com.liferay.portal.model.User> users = null;
3670    
3671                            if ((listeners.length > 0) || (userListeners.length > 0)) {
3672                                    users = getUsers(organizationId);
3673    
3674                                    for (com.liferay.portal.model.User user : users) {
3675                                            for (ModelListener<Organization> listener : listeners) {
3676                                                    listener.onBeforeRemoveAssociation(organizationId,
3677                                                            com.liferay.portal.model.User.class.getName(),
3678                                                            user.getPrimaryKey());
3679                                            }
3680    
3681                                            for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3682                                                    listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
3683                                                            Organization.class.getName(), organizationId);
3684                                            }
3685                                    }
3686                            }
3687    
3688                            _sqlUpdate.update(new Object[] { new Long(organizationId) });
3689    
3690                            if ((listeners.length > 0) || (userListeners.length > 0)) {
3691                                    for (com.liferay.portal.model.User user : users) {
3692                                            for (ModelListener<Organization> listener : listeners) {
3693                                                    listener.onAfterRemoveAssociation(organizationId,
3694                                                            com.liferay.portal.model.User.class.getName(),
3695                                                            user.getPrimaryKey());
3696                                            }
3697    
3698                                            for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3699                                                    listener.onAfterRemoveAssociation(user.getPrimaryKey(),
3700                                                            Organization.class.getName(), organizationId);
3701                                            }
3702                                    }
3703                            }
3704                    }
3705    
3706                    private SqlUpdate _sqlUpdate;
3707            }
3708    
3709            protected class RemoveUser {
3710                    protected RemoveUser(OrganizationPersistenceImpl persistenceImpl) {
3711                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3712                                            "DELETE FROM Users_Orgs WHERE organizationId = ? AND userId = ?",
3713                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3714                            _persistenceImpl = persistenceImpl;
3715                    }
3716    
3717                    protected void remove(long organizationId, long userId)
3718                            throws SystemException {
3719                            if (_persistenceImpl.containsUser.contains(organizationId, userId)) {
3720                                    ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
3721    
3722                                    for (ModelListener<Organization> listener : listeners) {
3723                                            listener.onBeforeRemoveAssociation(organizationId,
3724                                                    com.liferay.portal.model.User.class.getName(), userId);
3725                                    }
3726    
3727                                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3728                                            listener.onBeforeRemoveAssociation(userId,
3729                                                    Organization.class.getName(), organizationId);
3730                                    }
3731    
3732                                    _sqlUpdate.update(new Object[] {
3733                                                    new Long(organizationId), new Long(userId)
3734                                            });
3735    
3736                                    for (ModelListener<Organization> listener : listeners) {
3737                                            listener.onAfterRemoveAssociation(organizationId,
3738                                                    com.liferay.portal.model.User.class.getName(), userId);
3739                                    }
3740    
3741                                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3742                                            listener.onAfterRemoveAssociation(userId,
3743                                                    Organization.class.getName(), organizationId);
3744                                    }
3745                            }
3746                    }
3747    
3748                    private SqlUpdate _sqlUpdate;
3749                    private OrganizationPersistenceImpl _persistenceImpl;
3750            }
3751    
3752            protected ExpandTreeLeftOrganizationId expandTreeLeftOrganizationId;
3753            protected ExpandTreeRightOrganizationId expandTreeRightOrganizationId;
3754            protected ShrinkTreeLeftOrganizationId shrinkTreeLeftOrganizationId;
3755            protected ShrinkTreeRightOrganizationId shrinkTreeRightOrganizationId;
3756            protected UpdateTree updateTree;
3757    
3758            protected class ExpandTreeLeftOrganizationId {
3759                    protected ExpandTreeLeftOrganizationId() {
3760                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3761                                            "UPDATE Organization_ SET leftOrganizationId = (leftOrganizationId + 2) WHERE (companyId = ?) AND (leftOrganizationId > ?)",
3762                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3763                    }
3764    
3765                    protected void expand(long companyId, long leftOrganizationId) {
3766                            _sqlUpdate.update(new Object[] { companyId, leftOrganizationId });
3767                    }
3768    
3769                    private SqlUpdate _sqlUpdate;
3770            }
3771    
3772            protected class ExpandTreeRightOrganizationId {
3773                    protected ExpandTreeRightOrganizationId() {
3774                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3775                                            "UPDATE Organization_ SET rightOrganizationId = (rightOrganizationId + 2) WHERE (companyId = ?) AND (rightOrganizationId > ?)",
3776                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3777                    }
3778    
3779                    protected void expand(long companyId, long rightOrganizationId) {
3780                            _sqlUpdate.update(new Object[] { companyId, rightOrganizationId });
3781                    }
3782    
3783                    private SqlUpdate _sqlUpdate;
3784            }
3785    
3786            protected class ShrinkTreeLeftOrganizationId {
3787                    protected ShrinkTreeLeftOrganizationId() {
3788                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3789                                            "UPDATE Organization_ SET leftOrganizationId = (leftOrganizationId - ?) WHERE (companyId = ?) AND (leftOrganizationId > ?)",
3790                                            new int[] {
3791                                                    java.sql.Types.BIGINT, java.sql.Types.BIGINT,
3792                                                    java.sql.Types.BIGINT
3793                                            });
3794                    }
3795    
3796                    protected void shrink(long companyId, long leftOrganizationId,
3797                            long delta) {
3798                            _sqlUpdate.update(new Object[] { delta, companyId, leftOrganizationId });
3799                    }
3800    
3801                    private SqlUpdate _sqlUpdate;
3802            }
3803    
3804            protected class ShrinkTreeRightOrganizationId {
3805                    protected ShrinkTreeRightOrganizationId() {
3806                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3807                                            "UPDATE Organization_ SET rightOrganizationId = (rightOrganizationId - ?) WHERE (companyId = ?) AND (rightOrganizationId > ?)",
3808                                            new int[] {
3809                                                    java.sql.Types.BIGINT, java.sql.Types.BIGINT,
3810                                                    java.sql.Types.BIGINT
3811                                            });
3812                    }
3813    
3814                    protected void shrink(long companyId, long rightOrganizationId,
3815                            long delta) {
3816                            _sqlUpdate.update(new Object[] { delta, companyId, rightOrganizationId });
3817                    }
3818    
3819                    private SqlUpdate _sqlUpdate;
3820            }
3821    
3822            protected class UpdateTree {
3823                    protected UpdateTree() {
3824                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3825                                            "UPDATE Organization_ SET leftOrganizationId = ?, rightOrganizationId = ? WHERE organizationId = ?",
3826                                            new int[] {
3827                                                    java.sql.Types.BIGINT, java.sql.Types.BIGINT,
3828                                                    java.sql.Types.BIGINT
3829                                            });
3830                    }
3831    
3832                    protected void update(long organizationId, long leftOrganizationId,
3833                            long rightOrganizationId) {
3834                            _sqlUpdate.update(new Object[] {
3835                                            leftOrganizationId, rightOrganizationId, organizationId
3836                                    });
3837                    }
3838    
3839                    private SqlUpdate _sqlUpdate;
3840            }
3841    
3842            private static final String _SQL_SELECT_ORGANIZATION = "SELECT organization FROM Organization organization";
3843            private static final String _SQL_SELECT_ORGANIZATION_WHERE = "SELECT organization FROM Organization organization WHERE ";
3844            private static final String _SQL_COUNT_ORGANIZATION = "SELECT COUNT(organization) FROM Organization organization";
3845            private static final String _SQL_COUNT_ORGANIZATION_WHERE = "SELECT COUNT(organization) FROM Organization organization WHERE ";
3846            private static final String _SQL_GETGROUPS = "SELECT {Group_.*} FROM Group_ INNER JOIN Groups_Orgs ON (Groups_Orgs.groupId = Group_.groupId) WHERE (Groups_Orgs.organizationId = ?)";
3847            private static final String _SQL_GETGROUPSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Orgs WHERE organizationId = ?";
3848            private static final String _SQL_CONTAINSGROUP = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Orgs WHERE organizationId = ? AND groupId = ?";
3849            private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_Orgs ON (Users_Orgs.userId = User_.userId) WHERE (Users_Orgs.organizationId = ?)";
3850            private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Orgs WHERE organizationId = ?";
3851            private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Orgs WHERE organizationId = ? AND userId = ?";
3852            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "organization.companyId = ?";
3853            private static final String _FINDER_COLUMN_LOCATIONS_COMPANYID_2 = "organization.companyId = ? AND organization.parentOrganizationId != 0";
3854            private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "organization.companyId = ? AND ";
3855            private static final String _FINDER_COLUMN_C_P_PARENTORGANIZATIONID_2 = "organization.parentOrganizationId = ?";
3856            private static final String _FINDER_COLUMN_C_N_COMPANYID_2 = "organization.companyId = ? AND ";
3857            private static final String _FINDER_COLUMN_C_N_NAME_1 = "organization.name IS NULL";
3858            private static final String _FINDER_COLUMN_C_N_NAME_2 = "organization.name = ?";
3859            private static final String _FINDER_COLUMN_C_N_NAME_3 = "(organization.name IS NULL OR organization.name = ?)";
3860            private static final String _ORDER_BY_ENTITY_ALIAS = "organization.";
3861            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Organization exists with the primary key ";
3862            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Organization exists with the key {";
3863            private static Log _log = LogFactoryUtil.getLog(OrganizationPersistenceImpl.class);
3864    }