001    /**
002     * Copyright (c) 2000-2013 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.NoSuchTeamException;
018    import com.liferay.portal.kernel.bean.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.SQLQuery;
027    import com.liferay.portal.kernel.dao.orm.Session;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.GetterUtil;
032    import com.liferay.portal.kernel.util.InstanceFactory;
033    import com.liferay.portal.kernel.util.OrderByComparator;
034    import com.liferay.portal.kernel.util.StringBundler;
035    import com.liferay.portal.kernel.util.StringPool;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.kernel.util.UnmodifiableList;
038    import com.liferay.portal.kernel.util.Validator;
039    import com.liferay.portal.model.CacheModel;
040    import com.liferay.portal.model.ModelListener;
041    import com.liferay.portal.model.Team;
042    import com.liferay.portal.model.impl.TeamImpl;
043    import com.liferay.portal.model.impl.TeamModelImpl;
044    import com.liferay.portal.security.permission.InlineSQLHelperUtil;
045    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
046    import com.liferay.portal.service.persistence.impl.TableMapper;
047    import com.liferay.portal.service.persistence.impl.TableMapperFactory;
048    
049    import java.io.Serializable;
050    
051    import java.util.ArrayList;
052    import java.util.Collections;
053    import java.util.List;
054    
055    /**
056     * The persistence implementation for the team service.
057     *
058     * <p>
059     * Caching information and settings can be found in <code>portal.properties</code>
060     * </p>
061     *
062     * @author Brian Wing Shun Chan
063     * @see TeamPersistence
064     * @see TeamUtil
065     * @generated
066     */
067    public class TeamPersistenceImpl extends BasePersistenceImpl<Team>
068            implements TeamPersistence {
069            /*
070             * NOTE FOR DEVELOPERS:
071             *
072             * Never modify or reference this class directly. Always use {@link TeamUtil} to access the team persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
073             */
074            public static final String FINDER_CLASS_NAME_ENTITY = TeamImpl.class.getName();
075            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
076                    ".List1";
077            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
078                    ".List2";
079            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(TeamModelImpl.ENTITY_CACHE_ENABLED,
080                            TeamModelImpl.FINDER_CACHE_ENABLED, TeamImpl.class,
081                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
082            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(TeamModelImpl.ENTITY_CACHE_ENABLED,
083                            TeamModelImpl.FINDER_CACHE_ENABLED, TeamImpl.class,
084                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
085            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(TeamModelImpl.ENTITY_CACHE_ENABLED,
086                            TeamModelImpl.FINDER_CACHE_ENABLED, Long.class,
087                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
088            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID = new FinderPath(TeamModelImpl.ENTITY_CACHE_ENABLED,
089                            TeamModelImpl.FINDER_CACHE_ENABLED, TeamImpl.class,
090                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByGroupId",
091                            new String[] {
092                                    Long.class.getName(),
093                                    
094                            Integer.class.getName(), Integer.class.getName(),
095                                    OrderByComparator.class.getName()
096                            });
097            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID =
098                    new FinderPath(TeamModelImpl.ENTITY_CACHE_ENABLED,
099                            TeamModelImpl.FINDER_CACHE_ENABLED, TeamImpl.class,
100                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByGroupId",
101                            new String[] { Long.class.getName() },
102                            TeamModelImpl.GROUPID_COLUMN_BITMASK |
103                            TeamModelImpl.NAME_COLUMN_BITMASK);
104            public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(TeamModelImpl.ENTITY_CACHE_ENABLED,
105                            TeamModelImpl.FINDER_CACHE_ENABLED, Long.class,
106                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByGroupId",
107                            new String[] { Long.class.getName() });
108    
109            /**
110             * Returns all the teams where groupId = &#63;.
111             *
112             * @param groupId the group ID
113             * @return the matching teams
114             * @throws SystemException if a system exception occurred
115             */
116            @Override
117            public List<Team> findByGroupId(long groupId) throws SystemException {
118                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
119            }
120    
121            /**
122             * Returns a range of all the teams where groupId = &#63;.
123             *
124             * <p>
125             * 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.TeamModelImpl}. 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.
126             * </p>
127             *
128             * @param groupId the group ID
129             * @param start the lower bound of the range of teams
130             * @param end the upper bound of the range of teams (not inclusive)
131             * @return the range of matching teams
132             * @throws SystemException if a system exception occurred
133             */
134            @Override
135            public List<Team> findByGroupId(long groupId, int start, int end)
136                    throws SystemException {
137                    return findByGroupId(groupId, start, end, null);
138            }
139    
140            /**
141             * Returns an ordered range of all the teams where groupId = &#63;.
142             *
143             * <p>
144             * 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.TeamModelImpl}. 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.
145             * </p>
146             *
147             * @param groupId the group ID
148             * @param start the lower bound of the range of teams
149             * @param end the upper bound of the range of teams (not inclusive)
150             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
151             * @return the ordered range of matching teams
152             * @throws SystemException if a system exception occurred
153             */
154            @Override
155            public List<Team> findByGroupId(long groupId, int start, int end,
156                    OrderByComparator orderByComparator) throws SystemException {
157                    boolean pagination = true;
158                    FinderPath finderPath = null;
159                    Object[] finderArgs = null;
160    
161                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
162                                    (orderByComparator == null)) {
163                            pagination = false;
164                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID;
165                            finderArgs = new Object[] { groupId };
166                    }
167                    else {
168                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID;
169                            finderArgs = new Object[] { groupId, start, end, orderByComparator };
170                    }
171    
172                    List<Team> list = (List<Team>)FinderCacheUtil.getResult(finderPath,
173                                    finderArgs, this);
174    
175                    if ((list != null) && !list.isEmpty()) {
176                            for (Team team : list) {
177                                    if ((groupId != team.getGroupId())) {
178                                            list = null;
179    
180                                            break;
181                                    }
182                            }
183                    }
184    
185                    if (list == null) {
186                            StringBundler query = null;
187    
188                            if (orderByComparator != null) {
189                                    query = new StringBundler(3 +
190                                                    (orderByComparator.getOrderByFields().length * 3));
191                            }
192                            else {
193                                    query = new StringBundler(3);
194                            }
195    
196                            query.append(_SQL_SELECT_TEAM_WHERE);
197    
198                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
199    
200                            if (orderByComparator != null) {
201                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
202                                            orderByComparator);
203                            }
204                            else
205                             if (pagination) {
206                                    query.append(TeamModelImpl.ORDER_BY_JPQL);
207                            }
208    
209                            String sql = query.toString();
210    
211                            Session session = null;
212    
213                            try {
214                                    session = openSession();
215    
216                                    Query q = session.createQuery(sql);
217    
218                                    QueryPos qPos = QueryPos.getInstance(q);
219    
220                                    qPos.add(groupId);
221    
222                                    if (!pagination) {
223                                            list = (List<Team>)QueryUtil.list(q, getDialect(), start,
224                                                            end, false);
225    
226                                            Collections.sort(list);
227    
228                                            list = new UnmodifiableList<Team>(list);
229                                    }
230                                    else {
231                                            list = (List<Team>)QueryUtil.list(q, getDialect(), start,
232                                                            end);
233                                    }
234    
235                                    cacheResult(list);
236    
237                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
238                            }
239                            catch (Exception e) {
240                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
241    
242                                    throw processException(e);
243                            }
244                            finally {
245                                    closeSession(session);
246                            }
247                    }
248    
249                    return list;
250            }
251    
252            /**
253             * Returns the first team in the ordered set where groupId = &#63;.
254             *
255             * @param groupId the group ID
256             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
257             * @return the first matching team
258             * @throws com.liferay.portal.NoSuchTeamException if a matching team could not be found
259             * @throws SystemException if a system exception occurred
260             */
261            @Override
262            public Team findByGroupId_First(long groupId,
263                    OrderByComparator orderByComparator)
264                    throws NoSuchTeamException, SystemException {
265                    Team team = fetchByGroupId_First(groupId, orderByComparator);
266    
267                    if (team != null) {
268                            return team;
269                    }
270    
271                    StringBundler msg = new StringBundler(4);
272    
273                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
274    
275                    msg.append("groupId=");
276                    msg.append(groupId);
277    
278                    msg.append(StringPool.CLOSE_CURLY_BRACE);
279    
280                    throw new NoSuchTeamException(msg.toString());
281            }
282    
283            /**
284             * Returns the first team in the ordered set where groupId = &#63;.
285             *
286             * @param groupId the group ID
287             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
288             * @return the first matching team, or <code>null</code> if a matching team could not be found
289             * @throws SystemException if a system exception occurred
290             */
291            @Override
292            public Team fetchByGroupId_First(long groupId,
293                    OrderByComparator orderByComparator) throws SystemException {
294                    List<Team> list = findByGroupId(groupId, 0, 1, orderByComparator);
295    
296                    if (!list.isEmpty()) {
297                            return list.get(0);
298                    }
299    
300                    return null;
301            }
302    
303            /**
304             * Returns the last team in the ordered set where groupId = &#63;.
305             *
306             * @param groupId the group ID
307             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
308             * @return the last matching team
309             * @throws com.liferay.portal.NoSuchTeamException if a matching team could not be found
310             * @throws SystemException if a system exception occurred
311             */
312            @Override
313            public Team findByGroupId_Last(long groupId,
314                    OrderByComparator orderByComparator)
315                    throws NoSuchTeamException, SystemException {
316                    Team team = fetchByGroupId_Last(groupId, orderByComparator);
317    
318                    if (team != null) {
319                            return team;
320                    }
321    
322                    StringBundler msg = new StringBundler(4);
323    
324                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
325    
326                    msg.append("groupId=");
327                    msg.append(groupId);
328    
329                    msg.append(StringPool.CLOSE_CURLY_BRACE);
330    
331                    throw new NoSuchTeamException(msg.toString());
332            }
333    
334            /**
335             * Returns the last team in the ordered set where groupId = &#63;.
336             *
337             * @param groupId the group ID
338             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
339             * @return the last matching team, or <code>null</code> if a matching team could not be found
340             * @throws SystemException if a system exception occurred
341             */
342            @Override
343            public Team fetchByGroupId_Last(long groupId,
344                    OrderByComparator orderByComparator) throws SystemException {
345                    int count = countByGroupId(groupId);
346    
347                    if (count == 0) {
348                            return null;
349                    }
350    
351                    List<Team> list = findByGroupId(groupId, count - 1, count,
352                                    orderByComparator);
353    
354                    if (!list.isEmpty()) {
355                            return list.get(0);
356                    }
357    
358                    return null;
359            }
360    
361            /**
362             * Returns the teams before and after the current team in the ordered set where groupId = &#63;.
363             *
364             * @param teamId the primary key of the current team
365             * @param groupId the group ID
366             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
367             * @return the previous, current, and next team
368             * @throws com.liferay.portal.NoSuchTeamException if a team with the primary key could not be found
369             * @throws SystemException if a system exception occurred
370             */
371            @Override
372            public Team[] findByGroupId_PrevAndNext(long teamId, long groupId,
373                    OrderByComparator orderByComparator)
374                    throws NoSuchTeamException, SystemException {
375                    Team team = findByPrimaryKey(teamId);
376    
377                    Session session = null;
378    
379                    try {
380                            session = openSession();
381    
382                            Team[] array = new TeamImpl[3];
383    
384                            array[0] = getByGroupId_PrevAndNext(session, team, groupId,
385                                            orderByComparator, true);
386    
387                            array[1] = team;
388    
389                            array[2] = getByGroupId_PrevAndNext(session, team, groupId,
390                                            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 Team getByGroupId_PrevAndNext(Session session, Team team,
403                    long groupId, OrderByComparator orderByComparator, boolean previous) {
404                    StringBundler query = null;
405    
406                    if (orderByComparator != null) {
407                            query = new StringBundler(6 +
408                                            (orderByComparator.getOrderByFields().length * 6));
409                    }
410                    else {
411                            query = new StringBundler(3);
412                    }
413    
414                    query.append(_SQL_SELECT_TEAM_WHERE);
415    
416                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
417    
418                    if (orderByComparator != null) {
419                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
420    
421                            if (orderByConditionFields.length > 0) {
422                                    query.append(WHERE_AND);
423                            }
424    
425                            for (int i = 0; i < orderByConditionFields.length; i++) {
426                                    query.append(_ORDER_BY_ENTITY_ALIAS);
427                                    query.append(orderByConditionFields[i]);
428    
429                                    if ((i + 1) < orderByConditionFields.length) {
430                                            if (orderByComparator.isAscending() ^ previous) {
431                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
432                                            }
433                                            else {
434                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
435                                            }
436                                    }
437                                    else {
438                                            if (orderByComparator.isAscending() ^ previous) {
439                                                    query.append(WHERE_GREATER_THAN);
440                                            }
441                                            else {
442                                                    query.append(WHERE_LESSER_THAN);
443                                            }
444                                    }
445                            }
446    
447                            query.append(ORDER_BY_CLAUSE);
448    
449                            String[] orderByFields = orderByComparator.getOrderByFields();
450    
451                            for (int i = 0; i < orderByFields.length; i++) {
452                                    query.append(_ORDER_BY_ENTITY_ALIAS);
453                                    query.append(orderByFields[i]);
454    
455                                    if ((i + 1) < orderByFields.length) {
456                                            if (orderByComparator.isAscending() ^ previous) {
457                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
458                                            }
459                                            else {
460                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
461                                            }
462                                    }
463                                    else {
464                                            if (orderByComparator.isAscending() ^ previous) {
465                                                    query.append(ORDER_BY_ASC);
466                                            }
467                                            else {
468                                                    query.append(ORDER_BY_DESC);
469                                            }
470                                    }
471                            }
472                    }
473                    else {
474                            query.append(TeamModelImpl.ORDER_BY_JPQL);
475                    }
476    
477                    String sql = query.toString();
478    
479                    Query q = session.createQuery(sql);
480    
481                    q.setFirstResult(0);
482                    q.setMaxResults(2);
483    
484                    QueryPos qPos = QueryPos.getInstance(q);
485    
486                    qPos.add(groupId);
487    
488                    if (orderByComparator != null) {
489                            Object[] values = orderByComparator.getOrderByConditionValues(team);
490    
491                            for (Object value : values) {
492                                    qPos.add(value);
493                            }
494                    }
495    
496                    List<Team> list = q.list();
497    
498                    if (list.size() == 2) {
499                            return list.get(1);
500                    }
501                    else {
502                            return null;
503                    }
504            }
505    
506            /**
507             * Returns all the teams that the user has permission to view where groupId = &#63;.
508             *
509             * @param groupId the group ID
510             * @return the matching teams that the user has permission to view
511             * @throws SystemException if a system exception occurred
512             */
513            @Override
514            public List<Team> filterFindByGroupId(long groupId)
515                    throws SystemException {
516                    return filterFindByGroupId(groupId, QueryUtil.ALL_POS,
517                            QueryUtil.ALL_POS, null);
518            }
519    
520            /**
521             * Returns a range of all the teams that the user has permission to view where groupId = &#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.TeamModelImpl}. 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 groupId the group ID
528             * @param start the lower bound of the range of teams
529             * @param end the upper bound of the range of teams (not inclusive)
530             * @return the range of matching teams that the user has permission to view
531             * @throws SystemException if a system exception occurred
532             */
533            @Override
534            public List<Team> filterFindByGroupId(long groupId, int start, int end)
535                    throws SystemException {
536                    return filterFindByGroupId(groupId, start, end, null);
537            }
538    
539            /**
540             * Returns an ordered range of all the teams that the user has permissions to view where groupId = &#63;.
541             *
542             * <p>
543             * 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.TeamModelImpl}. 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.
544             * </p>
545             *
546             * @param groupId the group ID
547             * @param start the lower bound of the range of teams
548             * @param end the upper bound of the range of teams (not inclusive)
549             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
550             * @return the ordered range of matching teams that the user has permission to view
551             * @throws SystemException if a system exception occurred
552             */
553            @Override
554            public List<Team> filterFindByGroupId(long groupId, int start, int end,
555                    OrderByComparator orderByComparator) throws SystemException {
556                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
557                            return findByGroupId(groupId, start, end, orderByComparator);
558                    }
559    
560                    StringBundler query = null;
561    
562                    if (orderByComparator != null) {
563                            query = new StringBundler(3 +
564                                            (orderByComparator.getOrderByFields().length * 3));
565                    }
566                    else {
567                            query = new StringBundler(3);
568                    }
569    
570                    if (getDB().isSupportsInlineDistinct()) {
571                            query.append(_FILTER_SQL_SELECT_TEAM_WHERE);
572                    }
573                    else {
574                            query.append(_FILTER_SQL_SELECT_TEAM_NO_INLINE_DISTINCT_WHERE_1);
575                    }
576    
577                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
578    
579                    if (!getDB().isSupportsInlineDistinct()) {
580                            query.append(_FILTER_SQL_SELECT_TEAM_NO_INLINE_DISTINCT_WHERE_2);
581                    }
582    
583                    if (orderByComparator != null) {
584                            if (getDB().isSupportsInlineDistinct()) {
585                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
586                                            orderByComparator, true);
587                            }
588                            else {
589                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_TABLE,
590                                            orderByComparator, true);
591                            }
592                    }
593                    else {
594                            if (getDB().isSupportsInlineDistinct()) {
595                                    query.append(TeamModelImpl.ORDER_BY_JPQL);
596                            }
597                            else {
598                                    query.append(TeamModelImpl.ORDER_BY_SQL);
599                            }
600                    }
601    
602                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
603                                    Team.class.getName(), _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN,
604                                    groupId);
605    
606                    Session session = null;
607    
608                    try {
609                            session = openSession();
610    
611                            SQLQuery q = session.createSQLQuery(sql);
612    
613                            if (getDB().isSupportsInlineDistinct()) {
614                                    q.addEntity(_FILTER_ENTITY_ALIAS, TeamImpl.class);
615                            }
616                            else {
617                                    q.addEntity(_FILTER_ENTITY_TABLE, TeamImpl.class);
618                            }
619    
620                            QueryPos qPos = QueryPos.getInstance(q);
621    
622                            qPos.add(groupId);
623    
624                            return (List<Team>)QueryUtil.list(q, getDialect(), start, end);
625                    }
626                    catch (Exception e) {
627                            throw processException(e);
628                    }
629                    finally {
630                            closeSession(session);
631                    }
632            }
633    
634            /**
635             * Returns the teams before and after the current team in the ordered set of teams that the user has permission to view where groupId = &#63;.
636             *
637             * @param teamId the primary key of the current team
638             * @param groupId the group ID
639             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
640             * @return the previous, current, and next team
641             * @throws com.liferay.portal.NoSuchTeamException if a team with the primary key could not be found
642             * @throws SystemException if a system exception occurred
643             */
644            @Override
645            public Team[] filterFindByGroupId_PrevAndNext(long teamId, long groupId,
646                    OrderByComparator orderByComparator)
647                    throws NoSuchTeamException, SystemException {
648                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
649                            return findByGroupId_PrevAndNext(teamId, groupId, orderByComparator);
650                    }
651    
652                    Team team = findByPrimaryKey(teamId);
653    
654                    Session session = null;
655    
656                    try {
657                            session = openSession();
658    
659                            Team[] array = new TeamImpl[3];
660    
661                            array[0] = filterGetByGroupId_PrevAndNext(session, team, groupId,
662                                            orderByComparator, true);
663    
664                            array[1] = team;
665    
666                            array[2] = filterGetByGroupId_PrevAndNext(session, team, groupId,
667                                            orderByComparator, false);
668    
669                            return array;
670                    }
671                    catch (Exception e) {
672                            throw processException(e);
673                    }
674                    finally {
675                            closeSession(session);
676                    }
677            }
678    
679            protected Team filterGetByGroupId_PrevAndNext(Session session, Team team,
680                    long groupId, 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_TEAM_WHERE);
693                    }
694                    else {
695                            query.append(_FILTER_SQL_SELECT_TEAM_NO_INLINE_DISTINCT_WHERE_1);
696                    }
697    
698                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
699    
700                    if (!getDB().isSupportsInlineDistinct()) {
701                            query.append(_FILTER_SQL_SELECT_TEAM_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(TeamModelImpl.ORDER_BY_JPQL);
774                            }
775                            else {
776                                    query.append(TeamModelImpl.ORDER_BY_SQL);
777                            }
778                    }
779    
780                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
781                                    Team.class.getName(), _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN,
782                                    groupId);
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, TeamImpl.class);
791                    }
792                    else {
793                            q.addEntity(_FILTER_ENTITY_TABLE, TeamImpl.class);
794                    }
795    
796                    QueryPos qPos = QueryPos.getInstance(q);
797    
798                    qPos.add(groupId);
799    
800                    if (orderByComparator != null) {
801                            Object[] values = orderByComparator.getOrderByConditionValues(team);
802    
803                            for (Object value : values) {
804                                    qPos.add(value);
805                            }
806                    }
807    
808                    List<Team> 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 teams where groupId = &#63; from the database.
820             *
821             * @param groupId the group ID
822             * @throws SystemException if a system exception occurred
823             */
824            @Override
825            public void removeByGroupId(long groupId) throws SystemException {
826                    for (Team team : findByGroupId(groupId, QueryUtil.ALL_POS,
827                                    QueryUtil.ALL_POS, null)) {
828                            remove(team);
829                    }
830            }
831    
832            /**
833             * Returns the number of teams where groupId = &#63;.
834             *
835             * @param groupId the group ID
836             * @return the number of matching teams
837             * @throws SystemException if a system exception occurred
838             */
839            @Override
840            public int countByGroupId(long groupId) throws SystemException {
841                    FinderPath finderPath = FINDER_PATH_COUNT_BY_GROUPID;
842    
843                    Object[] finderArgs = new Object[] { groupId };
844    
845                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
846                                    this);
847    
848                    if (count == null) {
849                            StringBundler query = new StringBundler(2);
850    
851                            query.append(_SQL_COUNT_TEAM_WHERE);
852    
853                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
854    
855                            String sql = query.toString();
856    
857                            Session session = null;
858    
859                            try {
860                                    session = openSession();
861    
862                                    Query q = session.createQuery(sql);
863    
864                                    QueryPos qPos = QueryPos.getInstance(q);
865    
866                                    qPos.add(groupId);
867    
868                                    count = (Long)q.uniqueResult();
869    
870                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
871                            }
872                            catch (Exception e) {
873                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
874    
875                                    throw processException(e);
876                            }
877                            finally {
878                                    closeSession(session);
879                            }
880                    }
881    
882                    return count.intValue();
883            }
884    
885            /**
886             * Returns the number of teams that the user has permission to view where groupId = &#63;.
887             *
888             * @param groupId the group ID
889             * @return the number of matching teams that the user has permission to view
890             * @throws SystemException if a system exception occurred
891             */
892            @Override
893            public int filterCountByGroupId(long groupId) throws SystemException {
894                    if (!InlineSQLHelperUtil.isEnabled(groupId)) {
895                            return countByGroupId(groupId);
896                    }
897    
898                    StringBundler query = new StringBundler(2);
899    
900                    query.append(_FILTER_SQL_COUNT_TEAM_WHERE);
901    
902                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
903    
904                    String sql = InlineSQLHelperUtil.replacePermissionCheck(query.toString(),
905                                    Team.class.getName(), _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN,
906                                    groupId);
907    
908                    Session session = null;
909    
910                    try {
911                            session = openSession();
912    
913                            SQLQuery q = session.createSQLQuery(sql);
914    
915                            q.addScalar(COUNT_COLUMN_NAME,
916                                    com.liferay.portal.kernel.dao.orm.Type.LONG);
917    
918                            QueryPos qPos = QueryPos.getInstance(q);
919    
920                            qPos.add(groupId);
921    
922                            Long count = (Long)q.uniqueResult();
923    
924                            return count.intValue();
925                    }
926                    catch (Exception e) {
927                            throw processException(e);
928                    }
929                    finally {
930                            closeSession(session);
931                    }
932            }
933    
934            private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "team.groupId = ?";
935            public static final FinderPath FINDER_PATH_FETCH_BY_G_N = new FinderPath(TeamModelImpl.ENTITY_CACHE_ENABLED,
936                            TeamModelImpl.FINDER_CACHE_ENABLED, TeamImpl.class,
937                            FINDER_CLASS_NAME_ENTITY, "fetchByG_N",
938                            new String[] { Long.class.getName(), String.class.getName() },
939                            TeamModelImpl.GROUPID_COLUMN_BITMASK |
940                            TeamModelImpl.NAME_COLUMN_BITMASK);
941            public static final FinderPath FINDER_PATH_COUNT_BY_G_N = new FinderPath(TeamModelImpl.ENTITY_CACHE_ENABLED,
942                            TeamModelImpl.FINDER_CACHE_ENABLED, Long.class,
943                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByG_N",
944                            new String[] { Long.class.getName(), String.class.getName() });
945    
946            /**
947             * Returns the team where groupId = &#63; and name = &#63; or throws a {@link com.liferay.portal.NoSuchTeamException} if it could not be found.
948             *
949             * @param groupId the group ID
950             * @param name the name
951             * @return the matching team
952             * @throws com.liferay.portal.NoSuchTeamException if a matching team could not be found
953             * @throws SystemException if a system exception occurred
954             */
955            @Override
956            public Team findByG_N(long groupId, String name)
957                    throws NoSuchTeamException, SystemException {
958                    Team team = fetchByG_N(groupId, name);
959    
960                    if (team == null) {
961                            StringBundler msg = new StringBundler(6);
962    
963                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
964    
965                            msg.append("groupId=");
966                            msg.append(groupId);
967    
968                            msg.append(", name=");
969                            msg.append(name);
970    
971                            msg.append(StringPool.CLOSE_CURLY_BRACE);
972    
973                            if (_log.isWarnEnabled()) {
974                                    _log.warn(msg.toString());
975                            }
976    
977                            throw new NoSuchTeamException(msg.toString());
978                    }
979    
980                    return team;
981            }
982    
983            /**
984             * Returns the team where groupId = &#63; and name = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
985             *
986             * @param groupId the group ID
987             * @param name the name
988             * @return the matching team, or <code>null</code> if a matching team could not be found
989             * @throws SystemException if a system exception occurred
990             */
991            @Override
992            public Team fetchByG_N(long groupId, String name) throws SystemException {
993                    return fetchByG_N(groupId, name, true);
994            }
995    
996            /**
997             * Returns the team where groupId = &#63; and name = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
998             *
999             * @param groupId the group ID
1000             * @param name the name
1001             * @param retrieveFromCache whether to use the finder cache
1002             * @return the matching team, or <code>null</code> if a matching team could not be found
1003             * @throws SystemException if a system exception occurred
1004             */
1005            @Override
1006            public Team fetchByG_N(long groupId, String name, boolean retrieveFromCache)
1007                    throws SystemException {
1008                    Object[] finderArgs = new Object[] { groupId, name };
1009    
1010                    Object result = null;
1011    
1012                    if (retrieveFromCache) {
1013                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_G_N,
1014                                            finderArgs, this);
1015                    }
1016    
1017                    if (result instanceof Team) {
1018                            Team team = (Team)result;
1019    
1020                            if ((groupId != team.getGroupId()) ||
1021                                            !Validator.equals(name, team.getName())) {
1022                                    result = null;
1023                            }
1024                    }
1025    
1026                    if (result == null) {
1027                            StringBundler query = new StringBundler(4);
1028    
1029                            query.append(_SQL_SELECT_TEAM_WHERE);
1030    
1031                            query.append(_FINDER_COLUMN_G_N_GROUPID_2);
1032    
1033                            boolean bindName = false;
1034    
1035                            if (name == null) {
1036                                    query.append(_FINDER_COLUMN_G_N_NAME_1);
1037                            }
1038                            else if (name.equals(StringPool.BLANK)) {
1039                                    query.append(_FINDER_COLUMN_G_N_NAME_3);
1040                            }
1041                            else {
1042                                    bindName = true;
1043    
1044                                    query.append(_FINDER_COLUMN_G_N_NAME_2);
1045                            }
1046    
1047                            String sql = query.toString();
1048    
1049                            Session session = null;
1050    
1051                            try {
1052                                    session = openSession();
1053    
1054                                    Query q = session.createQuery(sql);
1055    
1056                                    QueryPos qPos = QueryPos.getInstance(q);
1057    
1058                                    qPos.add(groupId);
1059    
1060                                    if (bindName) {
1061                                            qPos.add(name);
1062                                    }
1063    
1064                                    List<Team> list = q.list();
1065    
1066                                    if (list.isEmpty()) {
1067                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N,
1068                                                    finderArgs, list);
1069                                    }
1070                                    else {
1071                                            Team team = list.get(0);
1072    
1073                                            result = team;
1074    
1075                                            cacheResult(team);
1076    
1077                                            if ((team.getGroupId() != groupId) ||
1078                                                            (team.getName() == null) ||
1079                                                            !team.getName().equals(name)) {
1080                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N,
1081                                                            finderArgs, team);
1082                                            }
1083                                    }
1084                            }
1085                            catch (Exception e) {
1086                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_N,
1087                                            finderArgs);
1088    
1089                                    throw processException(e);
1090                            }
1091                            finally {
1092                                    closeSession(session);
1093                            }
1094                    }
1095    
1096                    if (result instanceof List<?>) {
1097                            return null;
1098                    }
1099                    else {
1100                            return (Team)result;
1101                    }
1102            }
1103    
1104            /**
1105             * Removes the team where groupId = &#63; and name = &#63; from the database.
1106             *
1107             * @param groupId the group ID
1108             * @param name the name
1109             * @return the team that was removed
1110             * @throws SystemException if a system exception occurred
1111             */
1112            @Override
1113            public Team removeByG_N(long groupId, String name)
1114                    throws NoSuchTeamException, SystemException {
1115                    Team team = findByG_N(groupId, name);
1116    
1117                    return remove(team);
1118            }
1119    
1120            /**
1121             * Returns the number of teams where groupId = &#63; and name = &#63;.
1122             *
1123             * @param groupId the group ID
1124             * @param name the name
1125             * @return the number of matching teams
1126             * @throws SystemException if a system exception occurred
1127             */
1128            @Override
1129            public int countByG_N(long groupId, String name) throws SystemException {
1130                    FinderPath finderPath = FINDER_PATH_COUNT_BY_G_N;
1131    
1132                    Object[] finderArgs = new Object[] { groupId, name };
1133    
1134                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1135                                    this);
1136    
1137                    if (count == null) {
1138                            StringBundler query = new StringBundler(3);
1139    
1140                            query.append(_SQL_COUNT_TEAM_WHERE);
1141    
1142                            query.append(_FINDER_COLUMN_G_N_GROUPID_2);
1143    
1144                            boolean bindName = false;
1145    
1146                            if (name == null) {
1147                                    query.append(_FINDER_COLUMN_G_N_NAME_1);
1148                            }
1149                            else if (name.equals(StringPool.BLANK)) {
1150                                    query.append(_FINDER_COLUMN_G_N_NAME_3);
1151                            }
1152                            else {
1153                                    bindName = true;
1154    
1155                                    query.append(_FINDER_COLUMN_G_N_NAME_2);
1156                            }
1157    
1158                            String sql = query.toString();
1159    
1160                            Session session = null;
1161    
1162                            try {
1163                                    session = openSession();
1164    
1165                                    Query q = session.createQuery(sql);
1166    
1167                                    QueryPos qPos = QueryPos.getInstance(q);
1168    
1169                                    qPos.add(groupId);
1170    
1171                                    if (bindName) {
1172                                            qPos.add(name);
1173                                    }
1174    
1175                                    count = (Long)q.uniqueResult();
1176    
1177                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1178                            }
1179                            catch (Exception e) {
1180                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1181    
1182                                    throw processException(e);
1183                            }
1184                            finally {
1185                                    closeSession(session);
1186                            }
1187                    }
1188    
1189                    return count.intValue();
1190            }
1191    
1192            private static final String _FINDER_COLUMN_G_N_GROUPID_2 = "team.groupId = ? AND ";
1193            private static final String _FINDER_COLUMN_G_N_NAME_1 = "team.name IS NULL";
1194            private static final String _FINDER_COLUMN_G_N_NAME_2 = "team.name = ?";
1195            private static final String _FINDER_COLUMN_G_N_NAME_3 = "(team.name IS NULL OR team.name = '')";
1196    
1197            public TeamPersistenceImpl() {
1198                    setModelClass(Team.class);
1199            }
1200    
1201            /**
1202             * Caches the team in the entity cache if it is enabled.
1203             *
1204             * @param team the team
1205             */
1206            @Override
1207            public void cacheResult(Team team) {
1208                    EntityCacheUtil.putResult(TeamModelImpl.ENTITY_CACHE_ENABLED,
1209                            TeamImpl.class, team.getPrimaryKey(), team);
1210    
1211                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N,
1212                            new Object[] { team.getGroupId(), team.getName() }, team);
1213    
1214                    team.resetOriginalValues();
1215            }
1216    
1217            /**
1218             * Caches the teams in the entity cache if it is enabled.
1219             *
1220             * @param teams the teams
1221             */
1222            @Override
1223            public void cacheResult(List<Team> teams) {
1224                    for (Team team : teams) {
1225                            if (EntityCacheUtil.getResult(TeamModelImpl.ENTITY_CACHE_ENABLED,
1226                                                    TeamImpl.class, team.getPrimaryKey()) == null) {
1227                                    cacheResult(team);
1228                            }
1229                            else {
1230                                    team.resetOriginalValues();
1231                            }
1232                    }
1233            }
1234    
1235            /**
1236             * Clears the cache for all teams.
1237             *
1238             * <p>
1239             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1240             * </p>
1241             */
1242            @Override
1243            public void clearCache() {
1244                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
1245                            CacheRegistryUtil.clear(TeamImpl.class.getName());
1246                    }
1247    
1248                    EntityCacheUtil.clearCache(TeamImpl.class.getName());
1249    
1250                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
1251                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1252                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1253            }
1254    
1255            /**
1256             * Clears the cache for the team.
1257             *
1258             * <p>
1259             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
1260             * </p>
1261             */
1262            @Override
1263            public void clearCache(Team team) {
1264                    EntityCacheUtil.removeResult(TeamModelImpl.ENTITY_CACHE_ENABLED,
1265                            TeamImpl.class, team.getPrimaryKey());
1266    
1267                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1268                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1269    
1270                    clearUniqueFindersCache(team);
1271            }
1272    
1273            @Override
1274            public void clearCache(List<Team> teams) {
1275                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1276                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1277    
1278                    for (Team team : teams) {
1279                            EntityCacheUtil.removeResult(TeamModelImpl.ENTITY_CACHE_ENABLED,
1280                                    TeamImpl.class, team.getPrimaryKey());
1281    
1282                            clearUniqueFindersCache(team);
1283                    }
1284            }
1285    
1286            protected void cacheUniqueFindersCache(Team team) {
1287                    if (team.isNew()) {
1288                            Object[] args = new Object[] { team.getGroupId(), team.getName() };
1289    
1290                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_N, args,
1291                                    Long.valueOf(1));
1292                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N, args, team);
1293                    }
1294                    else {
1295                            TeamModelImpl teamModelImpl = (TeamModelImpl)team;
1296    
1297                            if ((teamModelImpl.getColumnBitmask() &
1298                                            FINDER_PATH_FETCH_BY_G_N.getColumnBitmask()) != 0) {
1299                                    Object[] args = new Object[] { team.getGroupId(), team.getName() };
1300    
1301                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_N, args,
1302                                            Long.valueOf(1));
1303                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_N, args, team);
1304                            }
1305                    }
1306            }
1307    
1308            protected void clearUniqueFindersCache(Team team) {
1309                    TeamModelImpl teamModelImpl = (TeamModelImpl)team;
1310    
1311                    Object[] args = new Object[] { team.getGroupId(), team.getName() };
1312    
1313                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_N, args);
1314                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_N, args);
1315    
1316                    if ((teamModelImpl.getColumnBitmask() &
1317                                    FINDER_PATH_FETCH_BY_G_N.getColumnBitmask()) != 0) {
1318                            args = new Object[] {
1319                                            teamModelImpl.getOriginalGroupId(),
1320                                            teamModelImpl.getOriginalName()
1321                                    };
1322    
1323                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_N, args);
1324                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_N, args);
1325                    }
1326            }
1327    
1328            /**
1329             * Creates a new team with the primary key. Does not add the team to the database.
1330             *
1331             * @param teamId the primary key for the new team
1332             * @return the new team
1333             */
1334            @Override
1335            public Team create(long teamId) {
1336                    Team team = new TeamImpl();
1337    
1338                    team.setNew(true);
1339                    team.setPrimaryKey(teamId);
1340    
1341                    return team;
1342            }
1343    
1344            /**
1345             * Removes the team with the primary key from the database. Also notifies the appropriate model listeners.
1346             *
1347             * @param teamId the primary key of the team
1348             * @return the team that was removed
1349             * @throws com.liferay.portal.NoSuchTeamException if a team with the primary key could not be found
1350             * @throws SystemException if a system exception occurred
1351             */
1352            @Override
1353            public Team remove(long teamId) throws NoSuchTeamException, SystemException {
1354                    return remove((Serializable)teamId);
1355            }
1356    
1357            /**
1358             * Removes the team with the primary key from the database. Also notifies the appropriate model listeners.
1359             *
1360             * @param primaryKey the primary key of the team
1361             * @return the team that was removed
1362             * @throws com.liferay.portal.NoSuchTeamException if a team with the primary key could not be found
1363             * @throws SystemException if a system exception occurred
1364             */
1365            @Override
1366            public Team remove(Serializable primaryKey)
1367                    throws NoSuchTeamException, SystemException {
1368                    Session session = null;
1369    
1370                    try {
1371                            session = openSession();
1372    
1373                            Team team = (Team)session.get(TeamImpl.class, primaryKey);
1374    
1375                            if (team == null) {
1376                                    if (_log.isWarnEnabled()) {
1377                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1378                                    }
1379    
1380                                    throw new NoSuchTeamException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1381                                            primaryKey);
1382                            }
1383    
1384                            return remove(team);
1385                    }
1386                    catch (NoSuchTeamException nsee) {
1387                            throw nsee;
1388                    }
1389                    catch (Exception e) {
1390                            throw processException(e);
1391                    }
1392                    finally {
1393                            closeSession(session);
1394                    }
1395            }
1396    
1397            @Override
1398            protected Team removeImpl(Team team) throws SystemException {
1399                    team = toUnwrappedModel(team);
1400    
1401                    teamToUserTableMapper.deleteLeftPrimaryKeyTableMappings(team.getPrimaryKey());
1402    
1403                    teamToUserGroupTableMapper.deleteLeftPrimaryKeyTableMappings(team.getPrimaryKey());
1404    
1405                    Session session = null;
1406    
1407                    try {
1408                            session = openSession();
1409    
1410                            if (!session.contains(team)) {
1411                                    team = (Team)session.get(TeamImpl.class, team.getPrimaryKeyObj());
1412                            }
1413    
1414                            if (team != null) {
1415                                    session.delete(team);
1416                            }
1417                    }
1418                    catch (Exception e) {
1419                            throw processException(e);
1420                    }
1421                    finally {
1422                            closeSession(session);
1423                    }
1424    
1425                    if (team != null) {
1426                            clearCache(team);
1427                    }
1428    
1429                    return team;
1430            }
1431    
1432            @Override
1433            public Team updateImpl(com.liferay.portal.model.Team team)
1434                    throws SystemException {
1435                    team = toUnwrappedModel(team);
1436    
1437                    boolean isNew = team.isNew();
1438    
1439                    TeamModelImpl teamModelImpl = (TeamModelImpl)team;
1440    
1441                    Session session = null;
1442    
1443                    try {
1444                            session = openSession();
1445    
1446                            if (team.isNew()) {
1447                                    session.save(team);
1448    
1449                                    team.setNew(false);
1450                            }
1451                            else {
1452                                    session.merge(team);
1453                            }
1454                    }
1455                    catch (Exception e) {
1456                            throw processException(e);
1457                    }
1458                    finally {
1459                            closeSession(session);
1460                    }
1461    
1462                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
1463    
1464                    if (isNew || !TeamModelImpl.COLUMN_BITMASK_ENABLED) {
1465                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1466                    }
1467    
1468                    else {
1469                            if ((teamModelImpl.getColumnBitmask() &
1470                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
1471                                    Object[] args = new Object[] { teamModelImpl.getOriginalGroupId() };
1472    
1473                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
1474                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
1475                                            args);
1476    
1477                                    args = new Object[] { teamModelImpl.getGroupId() };
1478    
1479                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
1480                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
1481                                            args);
1482                            }
1483                    }
1484    
1485                    EntityCacheUtil.putResult(TeamModelImpl.ENTITY_CACHE_ENABLED,
1486                            TeamImpl.class, team.getPrimaryKey(), team);
1487    
1488                    clearUniqueFindersCache(team);
1489                    cacheUniqueFindersCache(team);
1490    
1491                    return team;
1492            }
1493    
1494            protected Team toUnwrappedModel(Team team) {
1495                    if (team instanceof TeamImpl) {
1496                            return team;
1497                    }
1498    
1499                    TeamImpl teamImpl = new TeamImpl();
1500    
1501                    teamImpl.setNew(team.isNew());
1502                    teamImpl.setPrimaryKey(team.getPrimaryKey());
1503    
1504                    teamImpl.setTeamId(team.getTeamId());
1505                    teamImpl.setCompanyId(team.getCompanyId());
1506                    teamImpl.setUserId(team.getUserId());
1507                    teamImpl.setUserName(team.getUserName());
1508                    teamImpl.setCreateDate(team.getCreateDate());
1509                    teamImpl.setModifiedDate(team.getModifiedDate());
1510                    teamImpl.setGroupId(team.getGroupId());
1511                    teamImpl.setName(team.getName());
1512                    teamImpl.setDescription(team.getDescription());
1513    
1514                    return teamImpl;
1515            }
1516    
1517            /**
1518             * Returns the team with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
1519             *
1520             * @param primaryKey the primary key of the team
1521             * @return the team
1522             * @throws com.liferay.portal.NoSuchTeamException if a team with the primary key could not be found
1523             * @throws SystemException if a system exception occurred
1524             */
1525            @Override
1526            public Team findByPrimaryKey(Serializable primaryKey)
1527                    throws NoSuchTeamException, SystemException {
1528                    Team team = fetchByPrimaryKey(primaryKey);
1529    
1530                    if (team == null) {
1531                            if (_log.isWarnEnabled()) {
1532                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
1533                            }
1534    
1535                            throw new NoSuchTeamException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
1536                                    primaryKey);
1537                    }
1538    
1539                    return team;
1540            }
1541    
1542            /**
1543             * Returns the team with the primary key or throws a {@link com.liferay.portal.NoSuchTeamException} if it could not be found.
1544             *
1545             * @param teamId the primary key of the team
1546             * @return the team
1547             * @throws com.liferay.portal.NoSuchTeamException if a team with the primary key could not be found
1548             * @throws SystemException if a system exception occurred
1549             */
1550            @Override
1551            public Team findByPrimaryKey(long teamId)
1552                    throws NoSuchTeamException, SystemException {
1553                    return findByPrimaryKey((Serializable)teamId);
1554            }
1555    
1556            /**
1557             * Returns the team with the primary key or returns <code>null</code> if it could not be found.
1558             *
1559             * @param primaryKey the primary key of the team
1560             * @return the team, or <code>null</code> if a team with the primary key could not be found
1561             * @throws SystemException if a system exception occurred
1562             */
1563            @Override
1564            public Team fetchByPrimaryKey(Serializable primaryKey)
1565                    throws SystemException {
1566                    Team team = (Team)EntityCacheUtil.getResult(TeamModelImpl.ENTITY_CACHE_ENABLED,
1567                                    TeamImpl.class, primaryKey);
1568    
1569                    if (team == _nullTeam) {
1570                            return null;
1571                    }
1572    
1573                    if (team == null) {
1574                            Session session = null;
1575    
1576                            try {
1577                                    session = openSession();
1578    
1579                                    team = (Team)session.get(TeamImpl.class, primaryKey);
1580    
1581                                    if (team != null) {
1582                                            cacheResult(team);
1583                                    }
1584                                    else {
1585                                            EntityCacheUtil.putResult(TeamModelImpl.ENTITY_CACHE_ENABLED,
1586                                                    TeamImpl.class, primaryKey, _nullTeam);
1587                                    }
1588                            }
1589                            catch (Exception e) {
1590                                    EntityCacheUtil.removeResult(TeamModelImpl.ENTITY_CACHE_ENABLED,
1591                                            TeamImpl.class, primaryKey);
1592    
1593                                    throw processException(e);
1594                            }
1595                            finally {
1596                                    closeSession(session);
1597                            }
1598                    }
1599    
1600                    return team;
1601            }
1602    
1603            /**
1604             * Returns the team with the primary key or returns <code>null</code> if it could not be found.
1605             *
1606             * @param teamId the primary key of the team
1607             * @return the team, or <code>null</code> if a team with the primary key could not be found
1608             * @throws SystemException if a system exception occurred
1609             */
1610            @Override
1611            public Team fetchByPrimaryKey(long teamId) throws SystemException {
1612                    return fetchByPrimaryKey((Serializable)teamId);
1613            }
1614    
1615            /**
1616             * Returns all the teams.
1617             *
1618             * @return the teams
1619             * @throws SystemException if a system exception occurred
1620             */
1621            @Override
1622            public List<Team> findAll() throws SystemException {
1623                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1624            }
1625    
1626            /**
1627             * Returns a range of all the teams.
1628             *
1629             * <p>
1630             * 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.TeamModelImpl}. 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.
1631             * </p>
1632             *
1633             * @param start the lower bound of the range of teams
1634             * @param end the upper bound of the range of teams (not inclusive)
1635             * @return the range of teams
1636             * @throws SystemException if a system exception occurred
1637             */
1638            @Override
1639            public List<Team> findAll(int start, int end) throws SystemException {
1640                    return findAll(start, end, null);
1641            }
1642    
1643            /**
1644             * Returns an ordered range of all the teams.
1645             *
1646             * <p>
1647             * 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.TeamModelImpl}. 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.
1648             * </p>
1649             *
1650             * @param start the lower bound of the range of teams
1651             * @param end the upper bound of the range of teams (not inclusive)
1652             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1653             * @return the ordered range of teams
1654             * @throws SystemException if a system exception occurred
1655             */
1656            @Override
1657            public List<Team> findAll(int start, int end,
1658                    OrderByComparator orderByComparator) throws SystemException {
1659                    boolean pagination = true;
1660                    FinderPath finderPath = null;
1661                    Object[] finderArgs = null;
1662    
1663                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1664                                    (orderByComparator == null)) {
1665                            pagination = false;
1666                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1667                            finderArgs = FINDER_ARGS_EMPTY;
1668                    }
1669                    else {
1670                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1671                            finderArgs = new Object[] { start, end, orderByComparator };
1672                    }
1673    
1674                    List<Team> list = (List<Team>)FinderCacheUtil.getResult(finderPath,
1675                                    finderArgs, this);
1676    
1677                    if (list == null) {
1678                            StringBundler query = null;
1679                            String sql = null;
1680    
1681                            if (orderByComparator != null) {
1682                                    query = new StringBundler(2 +
1683                                                    (orderByComparator.getOrderByFields().length * 3));
1684    
1685                                    query.append(_SQL_SELECT_TEAM);
1686    
1687                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1688                                            orderByComparator);
1689    
1690                                    sql = query.toString();
1691                            }
1692                            else {
1693                                    sql = _SQL_SELECT_TEAM;
1694    
1695                                    if (pagination) {
1696                                            sql = sql.concat(TeamModelImpl.ORDER_BY_JPQL);
1697                                    }
1698                            }
1699    
1700                            Session session = null;
1701    
1702                            try {
1703                                    session = openSession();
1704    
1705                                    Query q = session.createQuery(sql);
1706    
1707                                    if (!pagination) {
1708                                            list = (List<Team>)QueryUtil.list(q, getDialect(), start,
1709                                                            end, false);
1710    
1711                                            Collections.sort(list);
1712    
1713                                            list = new UnmodifiableList<Team>(list);
1714                                    }
1715                                    else {
1716                                            list = (List<Team>)QueryUtil.list(q, getDialect(), start,
1717                                                            end);
1718                                    }
1719    
1720                                    cacheResult(list);
1721    
1722                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1723                            }
1724                            catch (Exception e) {
1725                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1726    
1727                                    throw processException(e);
1728                            }
1729                            finally {
1730                                    closeSession(session);
1731                            }
1732                    }
1733    
1734                    return list;
1735            }
1736    
1737            /**
1738             * Removes all the teams from the database.
1739             *
1740             * @throws SystemException if a system exception occurred
1741             */
1742            @Override
1743            public void removeAll() throws SystemException {
1744                    for (Team team : findAll()) {
1745                            remove(team);
1746                    }
1747            }
1748    
1749            /**
1750             * Returns the number of teams.
1751             *
1752             * @return the number of teams
1753             * @throws SystemException if a system exception occurred
1754             */
1755            @Override
1756            public int countAll() throws SystemException {
1757                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1758                                    FINDER_ARGS_EMPTY, this);
1759    
1760                    if (count == null) {
1761                            Session session = null;
1762    
1763                            try {
1764                                    session = openSession();
1765    
1766                                    Query q = session.createQuery(_SQL_COUNT_TEAM);
1767    
1768                                    count = (Long)q.uniqueResult();
1769    
1770                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1771                                            FINDER_ARGS_EMPTY, count);
1772                            }
1773                            catch (Exception e) {
1774                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
1775                                            FINDER_ARGS_EMPTY);
1776    
1777                                    throw processException(e);
1778                            }
1779                            finally {
1780                                    closeSession(session);
1781                            }
1782                    }
1783    
1784                    return count.intValue();
1785            }
1786    
1787            /**
1788             * Returns all the users associated with the team.
1789             *
1790             * @param pk the primary key of the team
1791             * @return the users associated with the team
1792             * @throws SystemException if a system exception occurred
1793             */
1794            @Override
1795            public List<com.liferay.portal.model.User> getUsers(long pk)
1796                    throws SystemException {
1797                    return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1798            }
1799    
1800            /**
1801             * Returns a range of all the users associated with the team.
1802             *
1803             * <p>
1804             * 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.TeamModelImpl}. 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.
1805             * </p>
1806             *
1807             * @param pk the primary key of the team
1808             * @param start the lower bound of the range of teams
1809             * @param end the upper bound of the range of teams (not inclusive)
1810             * @return the range of users associated with the team
1811             * @throws SystemException if a system exception occurred
1812             */
1813            @Override
1814            public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1815                    int end) throws SystemException {
1816                    return getUsers(pk, start, end, null);
1817            }
1818    
1819            /**
1820             * Returns an ordered range of all the users associated with the team.
1821             *
1822             * <p>
1823             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. 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.TeamModelImpl}. 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.
1824             * </p>
1825             *
1826             * @param pk the primary key of the team
1827             * @param start the lower bound of the range of teams
1828             * @param end the upper bound of the range of teams (not inclusive)
1829             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1830             * @return the ordered range of users associated with the team
1831             * @throws SystemException if a system exception occurred
1832             */
1833            @Override
1834            public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1835                    int end, OrderByComparator orderByComparator) throws SystemException {
1836                    return teamToUserTableMapper.getRightBaseModels(pk, start, end,
1837                            orderByComparator);
1838            }
1839    
1840            /**
1841             * Returns the number of users associated with the team.
1842             *
1843             * @param pk the primary key of the team
1844             * @return the number of users associated with the team
1845             * @throws SystemException if a system exception occurred
1846             */
1847            @Override
1848            public int getUsersSize(long pk) throws SystemException {
1849                    long[] pks = teamToUserTableMapper.getRightPrimaryKeys(pk);
1850    
1851                    return pks.length;
1852            }
1853    
1854            /**
1855             * Returns <code>true</code> if the user is associated with the team.
1856             *
1857             * @param pk the primary key of the team
1858             * @param userPK the primary key of the user
1859             * @return <code>true</code> if the user is associated with the team; <code>false</code> otherwise
1860             * @throws SystemException if a system exception occurred
1861             */
1862            @Override
1863            public boolean containsUser(long pk, long userPK) throws SystemException {
1864                    return teamToUserTableMapper.containsTableMapping(pk, userPK);
1865            }
1866    
1867            /**
1868             * Returns <code>true</code> if the team has any users associated with it.
1869             *
1870             * @param pk the primary key of the team to check for associations with users
1871             * @return <code>true</code> if the team has any users associated with it; <code>false</code> otherwise
1872             * @throws SystemException if a system exception occurred
1873             */
1874            @Override
1875            public boolean containsUsers(long pk) throws SystemException {
1876                    if (getUsersSize(pk) > 0) {
1877                            return true;
1878                    }
1879                    else {
1880                            return false;
1881                    }
1882            }
1883    
1884            /**
1885             * Adds an association between the team and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1886             *
1887             * @param pk the primary key of the team
1888             * @param userPK the primary key of the user
1889             * @throws SystemException if a system exception occurred
1890             */
1891            @Override
1892            public void addUser(long pk, long userPK) throws SystemException {
1893                    teamToUserTableMapper.addTableMapping(pk, userPK);
1894            }
1895    
1896            /**
1897             * Adds an association between the team and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1898             *
1899             * @param pk the primary key of the team
1900             * @param user the user
1901             * @throws SystemException if a system exception occurred
1902             */
1903            @Override
1904            public void addUser(long pk, com.liferay.portal.model.User user)
1905                    throws SystemException {
1906                    teamToUserTableMapper.addTableMapping(pk, user.getPrimaryKey());
1907            }
1908    
1909            /**
1910             * Adds an association between the team and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1911             *
1912             * @param pk the primary key of the team
1913             * @param userPKs the primary keys of the users
1914             * @throws SystemException if a system exception occurred
1915             */
1916            @Override
1917            public void addUsers(long pk, long[] userPKs) throws SystemException {
1918                    for (long userPK : userPKs) {
1919                            teamToUserTableMapper.addTableMapping(pk, userPK);
1920                    }
1921            }
1922    
1923            /**
1924             * Adds an association between the team and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1925             *
1926             * @param pk the primary key of the team
1927             * @param users the users
1928             * @throws SystemException if a system exception occurred
1929             */
1930            @Override
1931            public void addUsers(long pk, List<com.liferay.portal.model.User> users)
1932                    throws SystemException {
1933                    for (com.liferay.portal.model.User user : users) {
1934                            teamToUserTableMapper.addTableMapping(pk, user.getPrimaryKey());
1935                    }
1936            }
1937    
1938            /**
1939             * Clears all associations between the team and its users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1940             *
1941             * @param pk the primary key of the team to clear the associated users from
1942             * @throws SystemException if a system exception occurred
1943             */
1944            @Override
1945            public void clearUsers(long pk) throws SystemException {
1946                    teamToUserTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
1947            }
1948    
1949            /**
1950             * Removes the association between the team and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1951             *
1952             * @param pk the primary key of the team
1953             * @param userPK the primary key of the user
1954             * @throws SystemException if a system exception occurred
1955             */
1956            @Override
1957            public void removeUser(long pk, long userPK) throws SystemException {
1958                    teamToUserTableMapper.deleteTableMapping(pk, userPK);
1959            }
1960    
1961            /**
1962             * Removes the association between the team and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1963             *
1964             * @param pk the primary key of the team
1965             * @param user the user
1966             * @throws SystemException if a system exception occurred
1967             */
1968            @Override
1969            public void removeUser(long pk, com.liferay.portal.model.User user)
1970                    throws SystemException {
1971                    teamToUserTableMapper.deleteTableMapping(pk, user.getPrimaryKey());
1972            }
1973    
1974            /**
1975             * Removes the association between the team and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1976             *
1977             * @param pk the primary key of the team
1978             * @param userPKs the primary keys of the users
1979             * @throws SystemException if a system exception occurred
1980             */
1981            @Override
1982            public void removeUsers(long pk, long[] userPKs) throws SystemException {
1983                    for (long userPK : userPKs) {
1984                            teamToUserTableMapper.deleteTableMapping(pk, userPK);
1985                    }
1986            }
1987    
1988            /**
1989             * Removes the association between the team and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1990             *
1991             * @param pk the primary key of the team
1992             * @param users the users
1993             * @throws SystemException if a system exception occurred
1994             */
1995            @Override
1996            public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
1997                    throws SystemException {
1998                    for (com.liferay.portal.model.User user : users) {
1999                            teamToUserTableMapper.deleteTableMapping(pk, user.getPrimaryKey());
2000                    }
2001            }
2002    
2003            /**
2004             * Sets the users associated with the team, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2005             *
2006             * @param pk the primary key of the team
2007             * @param userPKs the primary keys of the users to be associated with the team
2008             * @throws SystemException if a system exception occurred
2009             */
2010            @Override
2011            public void setUsers(long pk, long[] userPKs) throws SystemException {
2012                    teamToUserTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
2013    
2014                    for (Long userPK : userPKs) {
2015                            teamToUserTableMapper.addTableMapping(pk, userPK);
2016                    }
2017            }
2018    
2019            /**
2020             * Sets the users associated with the team, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2021             *
2022             * @param pk the primary key of the team
2023             * @param users the users to be associated with the team
2024             * @throws SystemException if a system exception occurred
2025             */
2026            @Override
2027            public void setUsers(long pk, List<com.liferay.portal.model.User> users)
2028                    throws SystemException {
2029                    try {
2030                            long[] userPKs = new long[users.size()];
2031    
2032                            for (int i = 0; i < users.size(); i++) {
2033                                    com.liferay.portal.model.User user = users.get(i);
2034    
2035                                    userPKs[i] = user.getPrimaryKey();
2036                            }
2037    
2038                            setUsers(pk, userPKs);
2039                    }
2040                    catch (Exception e) {
2041                            throw processException(e);
2042                    }
2043                    finally {
2044                            FinderCacheUtil.clearCache(TeamModelImpl.MAPPING_TABLE_USERS_TEAMS_NAME);
2045                    }
2046            }
2047    
2048            /**
2049             * Returns all the user groups associated with the team.
2050             *
2051             * @param pk the primary key of the team
2052             * @return the user groups associated with the team
2053             * @throws SystemException if a system exception occurred
2054             */
2055            @Override
2056            public List<com.liferay.portal.model.UserGroup> getUserGroups(long pk)
2057                    throws SystemException {
2058                    return getUserGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
2059            }
2060    
2061            /**
2062             * Returns a range of all the user groups associated with the team.
2063             *
2064             * <p>
2065             * 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.TeamModelImpl}. 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.
2066             * </p>
2067             *
2068             * @param pk the primary key of the team
2069             * @param start the lower bound of the range of teams
2070             * @param end the upper bound of the range of teams (not inclusive)
2071             * @return the range of user groups associated with the team
2072             * @throws SystemException if a system exception occurred
2073             */
2074            @Override
2075            public List<com.liferay.portal.model.UserGroup> getUserGroups(long pk,
2076                    int start, int end) throws SystemException {
2077                    return getUserGroups(pk, start, end, null);
2078            }
2079    
2080            /**
2081             * Returns an ordered range of all the user groups associated with the team.
2082             *
2083             * <p>
2084             * 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.TeamModelImpl}. 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.
2085             * </p>
2086             *
2087             * @param pk the primary key of the team
2088             * @param start the lower bound of the range of teams
2089             * @param end the upper bound of the range of teams (not inclusive)
2090             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2091             * @return the ordered range of user groups associated with the team
2092             * @throws SystemException if a system exception occurred
2093             */
2094            @Override
2095            public List<com.liferay.portal.model.UserGroup> getUserGroups(long pk,
2096                    int start, int end, OrderByComparator orderByComparator)
2097                    throws SystemException {
2098                    return teamToUserGroupTableMapper.getRightBaseModels(pk, start, end,
2099                            orderByComparator);
2100            }
2101    
2102            /**
2103             * Returns the number of user groups associated with the team.
2104             *
2105             * @param pk the primary key of the team
2106             * @return the number of user groups associated with the team
2107             * @throws SystemException if a system exception occurred
2108             */
2109            @Override
2110            public int getUserGroupsSize(long pk) throws SystemException {
2111                    long[] pks = teamToUserGroupTableMapper.getRightPrimaryKeys(pk);
2112    
2113                    return pks.length;
2114            }
2115    
2116            /**
2117             * Returns <code>true</code> if the user group is associated with the team.
2118             *
2119             * @param pk the primary key of the team
2120             * @param userGroupPK the primary key of the user group
2121             * @return <code>true</code> if the user group is associated with the team; <code>false</code> otherwise
2122             * @throws SystemException if a system exception occurred
2123             */
2124            @Override
2125            public boolean containsUserGroup(long pk, long userGroupPK)
2126                    throws SystemException {
2127                    return teamToUserGroupTableMapper.containsTableMapping(pk, userGroupPK);
2128            }
2129    
2130            /**
2131             * Returns <code>true</code> if the team has any user groups associated with it.
2132             *
2133             * @param pk the primary key of the team to check for associations with user groups
2134             * @return <code>true</code> if the team has any user groups associated with it; <code>false</code> otherwise
2135             * @throws SystemException if a system exception occurred
2136             */
2137            @Override
2138            public boolean containsUserGroups(long pk) throws SystemException {
2139                    if (getUserGroupsSize(pk) > 0) {
2140                            return true;
2141                    }
2142                    else {
2143                            return false;
2144                    }
2145            }
2146    
2147            /**
2148             * Adds an association between the team and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2149             *
2150             * @param pk the primary key of the team
2151             * @param userGroupPK the primary key of the user group
2152             * @throws SystemException if a system exception occurred
2153             */
2154            @Override
2155            public void addUserGroup(long pk, long userGroupPK)
2156                    throws SystemException {
2157                    teamToUserGroupTableMapper.addTableMapping(pk, userGroupPK);
2158            }
2159    
2160            /**
2161             * Adds an association between the team and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2162             *
2163             * @param pk the primary key of the team
2164             * @param userGroup the user group
2165             * @throws SystemException if a system exception occurred
2166             */
2167            @Override
2168            public void addUserGroup(long pk,
2169                    com.liferay.portal.model.UserGroup userGroup) throws SystemException {
2170                    teamToUserGroupTableMapper.addTableMapping(pk, userGroup.getPrimaryKey());
2171            }
2172    
2173            /**
2174             * Adds an association between the team and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2175             *
2176             * @param pk the primary key of the team
2177             * @param userGroupPKs the primary keys of the user groups
2178             * @throws SystemException if a system exception occurred
2179             */
2180            @Override
2181            public void addUserGroups(long pk, long[] userGroupPKs)
2182                    throws SystemException {
2183                    for (long userGroupPK : userGroupPKs) {
2184                            teamToUserGroupTableMapper.addTableMapping(pk, userGroupPK);
2185                    }
2186            }
2187    
2188            /**
2189             * Adds an association between the team and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2190             *
2191             * @param pk the primary key of the team
2192             * @param userGroups the user groups
2193             * @throws SystemException if a system exception occurred
2194             */
2195            @Override
2196            public void addUserGroups(long pk,
2197                    List<com.liferay.portal.model.UserGroup> userGroups)
2198                    throws SystemException {
2199                    for (com.liferay.portal.model.UserGroup userGroup : userGroups) {
2200                            teamToUserGroupTableMapper.addTableMapping(pk,
2201                                    userGroup.getPrimaryKey());
2202                    }
2203            }
2204    
2205            /**
2206             * Clears all associations between the team and its user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2207             *
2208             * @param pk the primary key of the team to clear the associated user groups from
2209             * @throws SystemException if a system exception occurred
2210             */
2211            @Override
2212            public void clearUserGroups(long pk) throws SystemException {
2213                    teamToUserGroupTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
2214            }
2215    
2216            /**
2217             * Removes the association between the team and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2218             *
2219             * @param pk the primary key of the team
2220             * @param userGroupPK the primary key of the user group
2221             * @throws SystemException if a system exception occurred
2222             */
2223            @Override
2224            public void removeUserGroup(long pk, long userGroupPK)
2225                    throws SystemException {
2226                    teamToUserGroupTableMapper.deleteTableMapping(pk, userGroupPK);
2227            }
2228    
2229            /**
2230             * Removes the association between the team and the user group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2231             *
2232             * @param pk the primary key of the team
2233             * @param userGroup the user group
2234             * @throws SystemException if a system exception occurred
2235             */
2236            @Override
2237            public void removeUserGroup(long pk,
2238                    com.liferay.portal.model.UserGroup userGroup) throws SystemException {
2239                    teamToUserGroupTableMapper.deleteTableMapping(pk,
2240                            userGroup.getPrimaryKey());
2241            }
2242    
2243            /**
2244             * Removes the association between the team and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2245             *
2246             * @param pk the primary key of the team
2247             * @param userGroupPKs the primary keys of the user groups
2248             * @throws SystemException if a system exception occurred
2249             */
2250            @Override
2251            public void removeUserGroups(long pk, long[] userGroupPKs)
2252                    throws SystemException {
2253                    for (long userGroupPK : userGroupPKs) {
2254                            teamToUserGroupTableMapper.deleteTableMapping(pk, userGroupPK);
2255                    }
2256            }
2257    
2258            /**
2259             * Removes the association between the team and the user groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2260             *
2261             * @param pk the primary key of the team
2262             * @param userGroups the user groups
2263             * @throws SystemException if a system exception occurred
2264             */
2265            @Override
2266            public void removeUserGroups(long pk,
2267                    List<com.liferay.portal.model.UserGroup> userGroups)
2268                    throws SystemException {
2269                    for (com.liferay.portal.model.UserGroup userGroup : userGroups) {
2270                            teamToUserGroupTableMapper.deleteTableMapping(pk,
2271                                    userGroup.getPrimaryKey());
2272                    }
2273            }
2274    
2275            /**
2276             * Sets the user groups associated with the team, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2277             *
2278             * @param pk the primary key of the team
2279             * @param userGroupPKs the primary keys of the user groups to be associated with the team
2280             * @throws SystemException if a system exception occurred
2281             */
2282            @Override
2283            public void setUserGroups(long pk, long[] userGroupPKs)
2284                    throws SystemException {
2285                    teamToUserGroupTableMapper.deleteLeftPrimaryKeyTableMappings(pk);
2286    
2287                    for (Long userGroupPK : userGroupPKs) {
2288                            teamToUserGroupTableMapper.addTableMapping(pk, userGroupPK);
2289                    }
2290            }
2291    
2292            /**
2293             * Sets the user groups associated with the team, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2294             *
2295             * @param pk the primary key of the team
2296             * @param userGroups the user groups to be associated with the team
2297             * @throws SystemException if a system exception occurred
2298             */
2299            @Override
2300            public void setUserGroups(long pk,
2301                    List<com.liferay.portal.model.UserGroup> userGroups)
2302                    throws SystemException {
2303                    try {
2304                            long[] userGroupPKs = new long[userGroups.size()];
2305    
2306                            for (int i = 0; i < userGroups.size(); i++) {
2307                                    com.liferay.portal.model.UserGroup userGroup = userGroups.get(i);
2308    
2309                                    userGroupPKs[i] = userGroup.getPrimaryKey();
2310                            }
2311    
2312                            setUserGroups(pk, userGroupPKs);
2313                    }
2314                    catch (Exception e) {
2315                            throw processException(e);
2316                    }
2317                    finally {
2318                            FinderCacheUtil.clearCache(TeamModelImpl.MAPPING_TABLE_USERGROUPS_TEAMS_NAME);
2319                    }
2320            }
2321    
2322            /**
2323             * Initializes the team persistence.
2324             */
2325            public void afterPropertiesSet() {
2326                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2327                                            com.liferay.portal.util.PropsUtil.get(
2328                                                    "value.object.listener.com.liferay.portal.model.Team")));
2329    
2330                    if (listenerClassNames.length > 0) {
2331                            try {
2332                                    List<ModelListener<Team>> listenersList = new ArrayList<ModelListener<Team>>();
2333    
2334                                    for (String listenerClassName : listenerClassNames) {
2335                                            listenersList.add((ModelListener<Team>)InstanceFactory.newInstance(
2336                                                            getClassLoader(), listenerClassName));
2337                                    }
2338    
2339                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2340                            }
2341                            catch (Exception e) {
2342                                    _log.error(e);
2343                            }
2344                    }
2345    
2346                    teamToUserTableMapper = TableMapperFactory.getTableMapper("Users_Teams",
2347                                    "teamId", "userId", this, userPersistence);
2348    
2349                    teamToUserGroupTableMapper = TableMapperFactory.getTableMapper("UserGroups_Teams",
2350                                    "teamId", "userGroupId", this, userGroupPersistence);
2351            }
2352    
2353            public void destroy() {
2354                    EntityCacheUtil.removeCache(TeamImpl.class.getName());
2355                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
2356                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2357                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2358            }
2359    
2360            @BeanReference(type = UserPersistence.class)
2361            protected UserPersistence userPersistence;
2362            protected TableMapper<Team, com.liferay.portal.model.User> teamToUserTableMapper;
2363            @BeanReference(type = UserGroupPersistence.class)
2364            protected UserGroupPersistence userGroupPersistence;
2365            protected TableMapper<Team, com.liferay.portal.model.UserGroup> teamToUserGroupTableMapper;
2366            private static final String _SQL_SELECT_TEAM = "SELECT team FROM Team team";
2367            private static final String _SQL_SELECT_TEAM_WHERE = "SELECT team FROM Team team WHERE ";
2368            private static final String _SQL_COUNT_TEAM = "SELECT COUNT(team) FROM Team team";
2369            private static final String _SQL_COUNT_TEAM_WHERE = "SELECT COUNT(team) FROM Team team WHERE ";
2370            private static final String _FILTER_ENTITY_TABLE_FILTER_PK_COLUMN = "team.teamId";
2371            private static final String _FILTER_SQL_SELECT_TEAM_WHERE = "SELECT DISTINCT {team.*} FROM Team team WHERE ";
2372            private static final String _FILTER_SQL_SELECT_TEAM_NO_INLINE_DISTINCT_WHERE_1 =
2373                    "SELECT {Team.*} FROM (SELECT DISTINCT team.teamId FROM Team team WHERE ";
2374            private static final String _FILTER_SQL_SELECT_TEAM_NO_INLINE_DISTINCT_WHERE_2 =
2375                    ") TEMP_TABLE INNER JOIN Team ON TEMP_TABLE.teamId = Team.teamId";
2376            private static final String _FILTER_SQL_COUNT_TEAM_WHERE = "SELECT COUNT(DISTINCT team.teamId) AS COUNT_VALUE FROM Team team WHERE ";
2377            private static final String _FILTER_ENTITY_ALIAS = "team";
2378            private static final String _FILTER_ENTITY_TABLE = "Team";
2379            private static final String _ORDER_BY_ENTITY_ALIAS = "team.";
2380            private static final String _ORDER_BY_ENTITY_TABLE = "Team.";
2381            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Team exists with the primary key ";
2382            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Team exists with the key {";
2383            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
2384            private static Log _log = LogFactoryUtil.getLog(TeamPersistenceImpl.class);
2385            private static Team _nullTeam = new TeamImpl() {
2386                            @Override
2387                            public Object clone() {
2388                                    return this;
2389                            }
2390    
2391                            @Override
2392                            public CacheModel<Team> toCacheModel() {
2393                                    return _nullTeamCacheModel;
2394                            }
2395                    };
2396    
2397            private static CacheModel<Team> _nullTeamCacheModel = new CacheModel<Team>() {
2398                            @Override
2399                            public Team toEntityModel() {
2400                                    return _nullTeam;
2401                            }
2402                    };
2403    }