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