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