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