1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.DuplicateTeamException;
18  import com.liferay.portal.TeamNameException;
19  import com.liferay.portal.kernel.dao.orm.QueryUtil;
20  import com.liferay.portal.kernel.exception.PortalException;
21  import com.liferay.portal.kernel.exception.SystemException;
22  import com.liferay.portal.kernel.util.OrderByComparator;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.ResourceConstants;
26  import com.liferay.portal.model.Role;
27  import com.liferay.portal.model.RoleConstants;
28  import com.liferay.portal.model.Team;
29  import com.liferay.portal.model.User;
30  import com.liferay.portal.service.base.TeamLocalServiceBaseImpl;
31  
32  import java.util.Date;
33  import java.util.LinkedHashMap;
34  import java.util.List;
35  
36  /**
37   * <a href="TeamLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class TeamLocalServiceImpl extends TeamLocalServiceBaseImpl {
42  
43      public Team addTeam(
44              long userId, long groupId, String name, String description)
45          throws PortalException, SystemException {
46  
47          // Team
48  
49          User user = userPersistence.findByPrimaryKey(userId);
50          Date now = new Date();
51  
52          validate(0, groupId, name);
53  
54          long teamId = counterLocalService.increment();
55  
56          Team team = teamPersistence.create(teamId);
57  
58          team.setUserId(userId);
59          team.setCompanyId(user.getCompanyId());
60          team.setUserName(user.getFullName());
61          team.setCreateDate(now);
62          team.setModifiedDate(now);
63          team.setGroupId(groupId);
64          team.setName(name);
65          team.setDescription(description);
66  
67          teamPersistence.update(team, false);
68  
69          // Resources
70  
71          resourceLocalService.addResources(
72              user.getCompanyId(), groupId, userId, Team.class.getName(),
73              team.getTeamId(), false, true, true);
74  
75          // Role
76  
77          roleLocalService.addRole(
78              userId, user.getCompanyId(), String.valueOf(teamId), null, null,
79              RoleConstants.TYPE_PROVIDER, Team.class.getName(), teamId);
80  
81          return team;
82      }
83  
84      public void deleteTeam(long teamId)
85          throws PortalException, SystemException {
86  
87          // Team
88  
89          Team team = teamPersistence.findByPrimaryKey(teamId);
90  
91          teamPersistence.remove(team);
92  
93          // Resources
94  
95          resourceLocalService.deleteResource(
96              team.getCompanyId(), Team.class.getName(),
97              ResourceConstants.SCOPE_INDIVIDUAL, team.getTeamId());
98  
99          // Role
100 
101         Role role = team.getRole();
102 
103         roleLocalService.deleteRole(role.getRoleId());
104     }
105 
106     public void deleteTeams(long groupId)
107         throws PortalException, SystemException {
108 
109         List<Team> teams = teamPersistence.findByGroupId(groupId);
110 
111         for (Team team : teams) {
112             deleteTeam(team.getTeamId());
113         }
114     }
115 
116     public List<Team> getGroupTeams(long groupId) throws SystemException {
117         return teamPersistence.findByGroupId(groupId);
118     }
119 
120     public Team getTeam(long teamId)
121         throws PortalException, SystemException {
122 
123         return teamPersistence.findByPrimaryKey(teamId);
124     }
125 
126     public List<Team> getUserTeams(long userId) throws SystemException {
127         return userPersistence.getTeams(userId);
128     }
129 
130     public List<Team> getUserTeams(long userId, long groupId)
131         throws SystemException {
132 
133         LinkedHashMap<String, Object> params =
134             new LinkedHashMap<String, Object>();
135 
136         params.put("usersTeams", userId);
137 
138         return search(
139             groupId, null, null, params, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
140             null);
141     }
142 
143     public boolean hasUserTeam(long userId, long teamId)
144         throws SystemException {
145 
146         return userPersistence.containsTeam(userId, teamId);
147     }
148 
149     public List<Team> search(
150             long groupId, String name, String description,
151             LinkedHashMap<String, Object> params, int start, int end,
152             OrderByComparator obc)
153         throws SystemException {
154 
155         return teamFinder.findByG_N_D(
156             groupId, name, description, params, start, end, obc);
157     }
158 
159     public int searchCount(
160             long groupId, String name, String description,
161             LinkedHashMap<String, Object> params)
162         throws SystemException {
163 
164         return teamFinder.countByG_N_D(groupId, name, description, params);
165     }
166 
167     public Team updateTeam(
168             long teamId, String name, String description)
169         throws PortalException, SystemException {
170 
171         Date now = new Date();
172 
173         Team team = teamPersistence.findByPrimaryKey(teamId);
174 
175         validate(teamId, team.getGroupId(), name);
176 
177         team.setModifiedDate(now);
178         team.setName(name);
179         team.setDescription(description);
180 
181         teamPersistence.update(team, false);
182 
183         return team;
184     }
185 
186     protected void validate(long teamId, long groupId, String name)
187         throws PortalException, SystemException {
188 
189         if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
190             (name.indexOf(StringPool.COMMA) != -1) ||
191             (name.indexOf(StringPool.STAR) != -1)) {
192 
193             throw new TeamNameException();
194         }
195 
196         Team team = teamPersistence.fetchByG_N(groupId, name);
197 
198         if (team != null) {
199             if ((teamId <= 0) || (team.getTeamId() != teamId)) {
200                 throw new DuplicateTeamException();
201             }
202         }
203     }
204 
205 }