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