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.impl;
016    
017    import com.liferay.portal.DuplicateTeamException;
018    import com.liferay.portal.TeamNameException;
019    import com.liferay.portal.kernel.dao.orm.QueryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.OrderByComparator;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.ResourceConstants;
025    import com.liferay.portal.model.Role;
026    import com.liferay.portal.model.RoleConstants;
027    import com.liferay.portal.model.Team;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.service.base.TeamLocalServiceBaseImpl;
031    
032    import java.util.LinkedHashMap;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class TeamLocalServiceImpl extends TeamLocalServiceBaseImpl {
039    
040            /**
041             * @throws     PortalException
042             * @deprecated As of 7.0.0, replaced by {@link #addTeam(long, long, String,
043             *             String, ServiceContext)}
044             */
045            @Deprecated
046            @Override
047            public Team addTeam(
048                            long userId, long groupId, String name, String description)
049                    throws PortalException {
050    
051                    return addTeam(
052                            userId, groupId, name, description, new ServiceContext());
053            }
054    
055            @Override
056            public Team addTeam(
057                            long userId, long groupId, String name, String description,
058                            ServiceContext serviceContext)
059                    throws PortalException {
060    
061                    // Team
062    
063                    User user = userPersistence.findByPrimaryKey(userId);
064    
065                    validate(0, groupId, name);
066    
067                    long teamId = counterLocalService.increment();
068    
069                    Team team = teamPersistence.create(teamId);
070    
071                    team.setUuid(serviceContext.getUuid());
072                    team.setUserId(userId);
073                    team.setCompanyId(user.getCompanyId());
074                    team.setUserName(user.getFullName());
075                    team.setGroupId(groupId);
076                    team.setName(name);
077                    team.setDescription(description);
078    
079                    teamPersistence.update(team);
080    
081                    // Resources
082    
083                    resourceLocalService.addResources(
084                            user.getCompanyId(), groupId, userId, Team.class.getName(),
085                            team.getTeamId(), false, true, true);
086    
087                    // Role
088    
089                    roleLocalService.addRole(
090                            userId, Team.class.getName(), teamId, String.valueOf(teamId), null,
091                            null, RoleConstants.TYPE_PROVIDER, null, null);
092    
093                    return team;
094            }
095    
096            @Override
097            public Team deleteTeam(long teamId) throws PortalException {
098                    Team team = teamPersistence.findByPrimaryKey(teamId);
099    
100                    return deleteTeam(team);
101            }
102    
103            @Override
104            public Team deleteTeam(Team team) throws PortalException {
105    
106                    // Team
107    
108                    teamPersistence.remove(team);
109    
110                    // Resources
111    
112                    resourceLocalService.deleteResource(
113                            team.getCompanyId(), Team.class.getName(),
114                            ResourceConstants.SCOPE_INDIVIDUAL, team.getTeamId());
115    
116                    // Role
117    
118                    Role role = team.getRole();
119    
120                    roleLocalService.deleteRole(role);
121    
122                    return team;
123            }
124    
125            @Override
126            public void deleteTeams(long groupId) throws PortalException {
127                    List<Team> teams = teamPersistence.findByGroupId(groupId);
128    
129                    for (Team team : teams) {
130                            deleteTeam(team.getTeamId());
131                    }
132            }
133    
134            @Override
135            public Team fetchTeam(long groupId, String name) {
136                    return teamPersistence.fetchByG_N(groupId, name);
137            }
138    
139            @Override
140            public List<Team> getGroupTeams(long groupId) {
141                    return teamPersistence.findByGroupId(groupId);
142            }
143    
144            @Override
145            public Team getTeam(long groupId, String name) throws PortalException {
146                    return teamPersistence.findByG_N(groupId, name);
147            }
148    
149            @Override
150            public List<Team> getUserTeams(long userId, long groupId) {
151                    LinkedHashMap<String, Object> params = new LinkedHashMap<>();
152    
153                    params.put("usersTeams", userId);
154    
155                    return search(
156                            groupId, null, null, params, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
157                            null);
158            }
159    
160            @Override
161            public List<Team> search(
162                    long groupId, String name, String description,
163                    LinkedHashMap<String, Object> params, int start, int end,
164                    OrderByComparator<Team> obc) {
165    
166                    return teamFinder.findByG_N_D(
167                            groupId, name, description, params, start, end, obc);
168            }
169    
170            @Override
171            public int searchCount(
172                    long groupId, String name, String description,
173                    LinkedHashMap<String, Object> params) {
174    
175                    return teamFinder.countByG_N_D(groupId, name, description, params);
176            }
177    
178            @Override
179            public Team updateTeam(long teamId, String name, String description)
180                    throws PortalException {
181    
182                    Team team = teamPersistence.findByPrimaryKey(teamId);
183    
184                    validate(teamId, team.getGroupId(), name);
185    
186                    team.setName(name);
187                    team.setDescription(description);
188    
189                    teamPersistence.update(team);
190    
191                    return team;
192            }
193    
194            protected void validate(long teamId, long groupId, String name)
195                    throws PortalException {
196    
197                    if (Validator.isNull(name) || Validator.isNumber(name) ||
198                            (name.indexOf(CharPool.COMMA) != -1) ||
199                            (name.indexOf(CharPool.STAR) != -1)) {
200    
201                            throw new TeamNameException();
202                    }
203    
204                    Team team = teamPersistence.fetchByG_N(groupId, name);
205    
206                    if ((team != null) && (team.getTeamId() != teamId)) {
207                            throw new DuplicateTeamException("{teamId=" + teamId + "}");
208                    }
209            }
210    
211    }