001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public Team addTeam(
042                            long userId, long groupId, String name, String description)
043                    throws PortalException, SystemException {
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, false);
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, user.getCompanyId(), String.valueOf(teamId), null, null,
077                            RoleConstants.TYPE_PROVIDER, Team.class.getName(), teamId);
078    
079                    return team;
080            }
081    
082            @Override
083            public void deleteTeam(long teamId)
084                    throws PortalException, SystemException {
085    
086                    Team team = teamPersistence.findByPrimaryKey(teamId);
087    
088                    deleteTeam(team);
089            }
090    
091            @Override
092            public void deleteTeam(Team team)
093                    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    
112            public void deleteTeams(long groupId)
113                    throws PortalException, SystemException {
114    
115                    List<Team> teams = teamPersistence.findByGroupId(groupId);
116    
117                    for (Team team : teams) {
118                            deleteTeam(team.getTeamId());
119                    }
120            }
121    
122            public List<Team> getGroupTeams(long groupId) throws SystemException {
123                    return teamPersistence.findByGroupId(groupId);
124            }
125    
126            @Override
127            public Team getTeam(long teamId)
128                    throws PortalException, SystemException {
129    
130                    return teamPersistence.findByPrimaryKey(teamId);
131            }
132    
133            public Team getTeam(long groupId, String name)
134                    throws PortalException, SystemException {
135    
136                    return teamPersistence.findByG_N(groupId, name);
137            }
138    
139            public List<Team> getUserTeams(long userId) throws SystemException {
140                    return userPersistence.getTeams(userId);
141            }
142    
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            public boolean hasUserTeam(long userId, long teamId)
157                    throws SystemException {
158    
159                    return userPersistence.containsTeam(userId, teamId);
160            }
161    
162            public List<Team> search(
163                            long groupId, String name, String description,
164                            LinkedHashMap<String, Object> params, int start, int end,
165                            OrderByComparator obc)
166                    throws SystemException {
167    
168                    return teamFinder.findByG_N_D(
169                            groupId, name, description, params, start, end, obc);
170            }
171    
172            public int searchCount(
173                            long groupId, String name, String description,
174                            LinkedHashMap<String, Object> params)
175                    throws SystemException {
176    
177                    return teamFinder.countByG_N_D(groupId, name, description, params);
178            }
179    
180            public Team updateTeam(
181                            long teamId, String name, String description)
182                    throws PortalException, SystemException {
183    
184                    Date now = new Date();
185    
186                    Team team = teamPersistence.findByPrimaryKey(teamId);
187    
188                    validate(teamId, team.getGroupId(), name);
189    
190                    team.setModifiedDate(now);
191                    team.setName(name);
192                    team.setDescription(description);
193    
194                    teamPersistence.update(team, false);
195    
196                    return team;
197            }
198    
199            protected void validate(long teamId, long groupId, String name)
200                    throws PortalException, SystemException {
201    
202                    if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
203                            (name.indexOf(CharPool.COMMA) != -1) ||
204                            (name.indexOf(CharPool.STAR) != -1)) {
205    
206                            throw new TeamNameException();
207                    }
208    
209                    Team team = teamPersistence.fetchByG_N(groupId, name);
210    
211                    if (team != null) {
212                            if ((teamId <= 0) || (team.getTeamId() != teamId)) {
213                                    throw new DuplicateTeamException();
214                            }
215                    }
216            }
217    
218    }