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