001    /**
002     * Copyright (c) 2000-2012 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            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);
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);
078    
079                    return team;
080            }
081    
082            @Override
083            public Team deleteTeam(long teamId)
084                    throws PortalException, SystemException {
085    
086                    Team team = teamPersistence.findByPrimaryKey(teamId);
087    
088                    return deleteTeam(team);
089            }
090    
091            @Override
092            public Team deleteTeam(Team team) throws PortalException, SystemException {
093    
094                    // Team
095    
096                    teamPersistence.remove(team);
097    
098                    // Resources
099    
100                    resourceLocalService.deleteResource(
101                            team.getCompanyId(), Team.class.getName(),
102                            ResourceConstants.SCOPE_INDIVIDUAL, team.getTeamId());
103    
104                    // Role
105    
106                    Role role = team.getRole();
107    
108                    roleLocalService.deleteRole(role);
109    
110                    return team;
111            }
112    
113            public void deleteTeams(long groupId)
114                    throws PortalException, SystemException {
115    
116                    List<Team> teams = teamPersistence.findByGroupId(groupId);
117    
118                    for (Team team : teams) {
119                            deleteTeam(team.getTeamId());
120                    }
121            }
122    
123            public List<Team> getGroupTeams(long groupId) throws SystemException {
124                    return teamPersistence.findByGroupId(groupId);
125            }
126    
127            public Team getTeam(long groupId, String name)
128                    throws PortalException, SystemException {
129    
130                    return teamPersistence.findByG_N(groupId, name);
131            }
132    
133            public List<Team> getUserTeams(long userId) throws SystemException {
134                    return userPersistence.getTeams(userId);
135            }
136    
137            public List<Team> getUserTeams(long userId, long groupId)
138                    throws SystemException {
139    
140                    LinkedHashMap<String, Object> params =
141                            new LinkedHashMap<String, Object>();
142    
143                    params.put("usersTeams", userId);
144    
145                    return search(
146                            groupId, null, null, params, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
147                            null);
148            }
149    
150            public boolean hasUserTeam(long userId, long teamId)
151                    throws SystemException {
152    
153                    return userPersistence.containsTeam(userId, teamId);
154            }
155    
156            public List<Team> search(
157                            long groupId, String name, String description,
158                            LinkedHashMap<String, Object> params, int start, int end,
159                            OrderByComparator obc)
160                    throws SystemException {
161    
162                    return teamFinder.findByG_N_D(
163                            groupId, name, description, params, start, end, obc);
164            }
165    
166            public int searchCount(
167                            long groupId, String name, String description,
168                            LinkedHashMap<String, Object> params)
169                    throws SystemException {
170    
171                    return teamFinder.countByG_N_D(groupId, name, description, params);
172            }
173    
174            public Team updateTeam(long teamId, String name, String description)
175                    throws PortalException, SystemException {
176    
177                    Date now = new Date();
178    
179                    Team team = teamPersistence.findByPrimaryKey(teamId);
180    
181                    validate(teamId, team.getGroupId(), name);
182    
183                    team.setModifiedDate(now);
184                    team.setName(name);
185                    team.setDescription(description);
186    
187                    teamPersistence.update(team);
188    
189                    return team;
190            }
191    
192            protected void validate(long teamId, long groupId, String name)
193                    throws PortalException, SystemException {
194    
195                    if (Validator.isNull(name) || Validator.isNumber(name) ||
196                            (name.indexOf(CharPool.COMMA) != -1) ||
197                            (name.indexOf(CharPool.STAR) != -1)) {
198    
199                            throw new TeamNameException();
200                    }
201    
202                    Team team = teamPersistence.fetchByG_N(groupId, name);
203    
204                    if (team != null) {
205                            if ((teamId <= 0) || (team.getTeamId() != teamId)) {
206                                    throw new DuplicateTeamException();
207                            }
208                    }
209            }
210    
211    }