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