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