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