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