001    /**
002     * Copyright (c) 2000-2013 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            @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);
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, 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, long groupId)
139                    throws SystemException {
140    
141                    LinkedHashMap<String, Object> params =
142                            new LinkedHashMap<String, Object>();
143    
144                    params.put("usersTeams", userId);
145    
146                    return search(
147                            groupId, null, null, params, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
148                            null);
149            }
150    
151            @Override
152            public List<Team> search(
153                            long groupId, String name, String description,
154                            LinkedHashMap<String, Object> params, int start, int end,
155                            OrderByComparator obc)
156                    throws SystemException {
157    
158                    return teamFinder.findByG_N_D(
159                            groupId, name, description, params, start, end, obc);
160            }
161    
162            @Override
163            public int searchCount(
164                            long groupId, String name, String description,
165                            LinkedHashMap<String, Object> params)
166                    throws SystemException {
167    
168                    return teamFinder.countByG_N_D(groupId, name, description, params);
169            }
170    
171            @Override
172            public Team updateTeam(long teamId, String name, String description)
173                    throws PortalException, SystemException {
174    
175                    Date now = new Date();
176    
177                    Team team = teamPersistence.findByPrimaryKey(teamId);
178    
179                    validate(teamId, team.getGroupId(), name);
180    
181                    team.setModifiedDate(now);
182                    team.setName(name);
183                    team.setDescription(description);
184    
185                    teamPersistence.update(team);
186    
187                    return team;
188            }
189    
190            protected void validate(long teamId, long groupId, String name)
191                    throws PortalException, SystemException {
192    
193                    if (Validator.isNull(name) || Validator.isNumber(name) ||
194                            (name.indexOf(CharPool.COMMA) != -1) ||
195                            (name.indexOf(CharPool.STAR) != -1)) {
196    
197                            throw new TeamNameException();
198                    }
199    
200                    Team team = teamPersistence.fetchByG_N(groupId, name);
201    
202                    if (team != null) {
203                            if ((teamId <= 0) || (team.getTeamId() != teamId)) {
204                                    throw new DuplicateTeamException();
205                            }
206                    }
207            }
208    
209    }