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