PageRenderTime 52ms CodeModel.GetById 36ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 0ms

/portlets/so-portlet/docroot/WEB-INF/src/com/liferay/so/service/impl/MemberRequestLocalServiceImpl.java

https://github.com/l15k4/liferay-plugins
Java | 362 lines | 260 code | 82 blank | 20 comment | 20 complexity | 758d0e814f1f0767998d4434da4af76e MD5 | raw file
  1/**
  2 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3 *
  4 * This file is part of Liferay Social Office. Liferay Social Office is free
  5 * software: you can redistribute it and/or modify it under the terms of the GNU
  6 * Affero General Public License as published by the Free Software Foundation,
  7 * either version 3 of the License, or (at your option) any later version.
  8 *
  9 * Liferay Social Office is distributed in the hope that it will be useful, but
 10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
 12 * for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License along with
 15 * Liferay Social Office. If not, see http://www.gnu.org/licenses/agpl-3.0.html.
 16 */
 17
 18package com.liferay.so.service.impl;
 19
 20import com.liferay.mail.service.MailServiceUtil;
 21import com.liferay.portal.kernel.exception.PortalException;
 22import com.liferay.portal.kernel.exception.SystemException;
 23import com.liferay.portal.kernel.mail.MailMessage;
 24import com.liferay.portal.kernel.util.PrefsPropsUtil;
 25import com.liferay.portal.kernel.util.PropsKeys;
 26import com.liferay.portal.kernel.util.StringPool;
 27import com.liferay.portal.kernel.util.StringUtil;
 28import com.liferay.portal.kernel.util.Validator;
 29import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
 30import com.liferay.portal.model.Group;
 31import com.liferay.portal.model.GroupConstants;
 32import com.liferay.portal.model.Layout;
 33import com.liferay.portal.model.User;
 34import com.liferay.portal.theme.ThemeDisplay;
 35import com.liferay.portal.util.PortalUtil;
 36import com.liferay.portal.util.PortletKeys;
 37import com.liferay.so.MemberRequestAlreadyUsedException;
 38import com.liferay.so.MemberRequestInvalidUserException;
 39import com.liferay.so.invitemembers.util.InviteMembersConstants;
 40import com.liferay.so.model.MemberRequest;
 41import com.liferay.so.service.base.MemberRequestLocalServiceBaseImpl;
 42
 43import java.util.Date;
 44import java.util.List;
 45
 46import javax.mail.internet.InternetAddress;
 47
 48/**
 49 * @author Ryan	Park
 50 */
 51public class MemberRequestLocalServiceImpl
 52	extends MemberRequestLocalServiceBaseImpl {
 53
 54	public MemberRequest addMemberRequest(
 55			long userId, long groupId, long receiverUserId,
 56			String receiverEmailAddress, long invitedRoleId, long invitedTeamId,
 57			ThemeDisplay themeDisplay)
 58		throws PortalException, SystemException {
 59
 60		User user = userLocalService.getUserById(userId);
 61
 62		Date now = new Date();
 63
 64		long memberRequestId = counterLocalService.increment();
 65
 66		MemberRequest memberRequest = memberRequestPersistence.create(
 67			memberRequestId);
 68
 69		memberRequest.setGroupId(groupId);
 70		memberRequest.setCompanyId(user.getCompanyId());
 71		memberRequest.setUserId(userId);
 72		memberRequest.setUserName(user.getFullName());
 73		memberRequest.setCreateDate(now);
 74		memberRequest.setModifiedDate(now);
 75		memberRequest.setKey(PortalUUIDUtil.generate());
 76		memberRequest.setReceiverUserId(receiverUserId);
 77		memberRequest.setInvitedRoleId(invitedRoleId);
 78		memberRequest.setInvitedTeamId(invitedTeamId);
 79		memberRequest.setStatus(InviteMembersConstants.STATUS_PENDING);
 80
 81		memberRequestPersistence.update(memberRequest, false);
 82
 83		// Email
 84
 85		try {
 86			sendEmail(receiverEmailAddress, memberRequest, themeDisplay);
 87		}
 88		catch (Exception e) {
 89			throw new SystemException(e);
 90		}
 91
 92		return memberRequest;
 93	}
 94
 95	public void addMemberRequests(
 96			long userId, long groupId, long[] receiverUserIds,
 97			long invitedRoleId, long invitedTeamId, ThemeDisplay themeDisplay)
 98		throws PortalException, SystemException {
 99
100		for (long receiverUserId : receiverUserIds) {
101			if (hasPendingMemberRequest(groupId, receiverUserId)) {
102				continue;
103			}
104
105			User user = userLocalService.getUser(receiverUserId);
106
107			String emailAddress = user.getEmailAddress();
108
109			addMemberRequest(
110				userId, groupId, receiverUserId, emailAddress, invitedRoleId,
111				invitedTeamId, themeDisplay);
112		}
113	}
114
115	public void addMemberRequests(
116			long userId, long groupId, String[] emailAddresses,
117			long invitedRoleId, long invitedTeamId, ThemeDisplay themeDisplay)
118		throws PortalException, SystemException {
119
120		for (String emailAddress : emailAddresses) {
121			if (!Validator.isEmailAddress(emailAddress)) {
122				continue;
123			}
124
125			addMemberRequest(
126				userId, groupId, 0, emailAddress, invitedRoleId, invitedTeamId,
127				themeDisplay);
128		}
129	}
130
131	public MemberRequest getMemberRequest(
132			long groupId, long receiverUserId, int status)
133		throws PortalException, SystemException {
134
135		return memberRequestPersistence.findByG_R_S(
136			groupId, receiverUserId, status);
137	}
138
139	public List<MemberRequest> getReceiverMemberRequest(
140			long receiverUserId, int start, int end)
141		throws SystemException {
142
143		return memberRequestPersistence.findByReceiverUserId(receiverUserId);
144	}
145
146	public int getReceiverMemberRequestCount(long receiverUserId)
147		throws SystemException {
148
149		return memberRequestPersistence.countByReceiverUserId(receiverUserId);
150	}
151
152	public List<MemberRequest> getReceiverStatusMemberRequest(
153			long receiverUserId, int status, int start, int end)
154		throws SystemException {
155
156		return memberRequestPersistence.findByR_S(
157			receiverUserId, status, start, end);
158	}
159
160	public int getReceiverStatusMemberRequestCount(
161			long receiverUserId, int status)
162		throws SystemException {
163
164		return memberRequestPersistence.countByR_S(receiverUserId, status);
165	}
166
167	public boolean hasPendingMemberRequest(long groupId, long receiverUserId)
168		throws SystemException {
169
170		MemberRequest memberRequest = memberRequestPersistence.fetchByG_R_S(
171			groupId, receiverUserId, InviteMembersConstants.STATUS_PENDING);
172
173		if (memberRequest != null) {
174			return true;
175		}
176		else {
177			return false;
178		}
179	}
180
181	public MemberRequest updateMemberRequest(
182			long userId, long memberRequestId, int status)
183		throws Exception {
184
185		MemberRequest memberRequest = memberRequestPersistence.findByPrimaryKey(
186			memberRequestId);
187
188		validate(memberRequest, userId);
189
190		memberRequest.setModifiedDate(new Date());
191		memberRequest.setStatus(status);
192
193		memberRequestPersistence.update(memberRequest, false);
194
195		if (status == InviteMembersConstants.STATUS_ACCEPTED) {
196			userLocalService.addGroupUsers(
197				memberRequest.getGroupId(),
198				new long[] {memberRequest.getReceiverUserId()});
199
200			if (memberRequest.getInvitedRoleId() > 0) {
201				userGroupRoleLocalService.addUserGroupRoles(
202					new long[] {memberRequest.getReceiverUserId()},
203					memberRequest.getGroupId(),
204					memberRequest.getInvitedRoleId());
205			}
206
207			if (memberRequest.getInvitedTeamId() > 0) {
208				userLocalService.addTeamUsers(
209					memberRequest.getInvitedTeamId(),
210					new long[] {memberRequest.getReceiverUserId()});
211			}
212		}
213
214		return memberRequest;
215	}
216
217	public MemberRequest updateMemberRequest(String key, long receiverUserId)
218		throws PortalException, SystemException {
219
220		MemberRequest memberRequest = memberRequestPersistence.findByKey(key);
221
222		validate(memberRequest, 0);
223
224		memberRequest.setModifiedDate(new Date());
225		memberRequest.setReceiverUserId(receiverUserId);
226
227		memberRequestPersistence.update(memberRequest, false);
228
229		return memberRequest;
230	}
231
232	protected String getCreateAccountURL(String key, ThemeDisplay themeDisplay)
233		throws Exception {
234
235		return getLoginURL(key, themeDisplay) +
236			"&p_p_id=58&_58_struts_action=%2Flogin%2Fcreate_account";
237	}
238
239	protected String getLoginURL(String key, ThemeDisplay themeDisplay)
240		throws Exception {
241
242		Group group = groupLocalService.getGroup(
243			themeDisplay.getCompanyId(), GroupConstants.GUEST);
244
245		long plid = PortalUtil.getPlidFromPortletId(
246			group.getGroupId(), PortletKeys.LOGIN);
247
248		Layout layout = layoutLocalService.getLayout(plid);
249
250		return PortalUtil.getLayoutFullURL(layout, themeDisplay, false) +
251			"?key=" + key;
252	}
253
254	protected void sendEmail(
255			String emailAddress, MemberRequest memberRequest,
256			ThemeDisplay themeDisplay)
257		throws Exception {
258
259		long companyId = memberRequest.getCompanyId();
260
261		Group group = groupLocalService.getGroup(memberRequest.getGroupId());
262
263		User user = userLocalService.getUser(memberRequest.getUserId());
264
265		User receiverUser = null;
266
267		if (memberRequest.getReceiverUserId() > 0) {
268			receiverUser = userLocalService.getUser(
269				memberRequest.getReceiverUserId());
270		}
271
272		String fromName = PrefsPropsUtil.getString(
273			companyId, PropsKeys.ADMIN_EMAIL_FROM_NAME);
274		String fromAddress = PrefsPropsUtil.getString(
275			companyId, PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
276
277		String toName = StringPool.BLANK;
278		String toAddress = emailAddress;
279
280		if (receiverUser != null) {
281			toName = receiverUser.getFullName();
282		}
283
284		ClassLoader classLoader = getClass().getClassLoader();
285
286		String subject = StringUtil.read(
287			classLoader,
288			"com/liferay/so/invitemembers/dependencies/subject.tmpl");
289
290		String body = StringPool.BLANK;
291
292		if (memberRequest.getReceiverUserId() > 0) {
293			body = StringUtil.read(
294				classLoader,
295				"com/liferay/so/invitemembers/dependencies/" +
296					"existing_user_body.tmpl");
297		}
298		else {
299			body = StringUtil.read(
300				classLoader,
301				"com/liferay/so/invitemembers/dependencies/" +
302					"new_user_body.tmpl");
303		}
304
305		String createAccountURL = getCreateAccountURL(
306			memberRequest.getKey(), themeDisplay);
307		String loginURL = getLoginURL(memberRequest.getKey(), themeDisplay);
308
309		subject = StringUtil.replace(
310			subject,
311			new String[] {
312				"[$MEMBER_REQUEST_GROUP$]",
313				"[$MEMBER_REQUEST_USER$]"
314			},
315			new String[] {
316				group.getDescriptiveName(),
317				user.getFullName()
318			});
319
320		body = StringUtil.replace(
321			body,
322			new String[] {
323				"[$ADMIN_ADDRESS$]",
324				"[$ADMIN_NAME$]",
325				"[$MEMBER_REQUEST_CREATE_ACCOUNT_URL$]",
326				"[$MEMBER_REQUEST_GROUP$]",
327				"[$MEMBER_REQUEST_LOGIN_URL$]",
328				"[$MEMBER_REQUEST_USER$]"
329			},
330			new String[] {
331				fromAddress,
332				fromName,
333				createAccountURL,
334				group.getDescriptiveName(),
335				loginURL,
336				user.getFullName()
337			});
338
339		InternetAddress from = new InternetAddress(fromAddress, fromName);
340
341		InternetAddress to = new InternetAddress(toAddress, toName);
342
343		MailMessage mailMessage = new MailMessage(
344			from, to, subject, body, true);
345
346		MailServiceUtil.sendEmail(mailMessage);
347	}
348
349	protected void validate(MemberRequest memberRequest, long userId)
350		throws PortalException {
351
352		if (memberRequest.getStatus() !=
353				InviteMembersConstants.STATUS_PENDING) {
354
355			throw new MemberRequestAlreadyUsedException();
356		}
357		else if (memberRequest.getReceiverUserId() != userId) {
358			throw new MemberRequestInvalidUserException();
359		}
360	}
361
362}