PageRenderTime 55ms CodeModel.GetById 40ms app.highlight 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/christine-huang/liferay-plugins
Java | 426 lines | 304 code | 99 blank | 23 comment | 25 complexity | 1da97709a129bc6c725b8f38863433e9 MD5 | raw file
  1/**
  2 * Copyright (c) 2000-2012 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.NoSuchUserException;
 22import com.liferay.portal.kernel.exception.PortalException;
 23import com.liferay.portal.kernel.exception.SystemException;
 24import com.liferay.portal.kernel.json.JSONFactoryUtil;
 25import com.liferay.portal.kernel.json.JSONObject;
 26import com.liferay.portal.kernel.mail.MailMessage;
 27import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
 28import com.liferay.portal.kernel.notifications.NotificationEvent;
 29import com.liferay.portal.kernel.notifications.NotificationEventFactoryUtil;
 30import com.liferay.portal.kernel.util.HttpUtil;
 31import com.liferay.portal.kernel.util.PrefsPropsUtil;
 32import com.liferay.portal.kernel.util.PropsKeys;
 33import com.liferay.portal.kernel.util.StringPool;
 34import com.liferay.portal.kernel.util.StringUtil;
 35import com.liferay.portal.kernel.util.Validator;
 36import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
 37import com.liferay.portal.model.Group;
 38import com.liferay.portal.model.User;
 39import com.liferay.portal.service.ServiceContext;
 40import com.liferay.so.MemberRequestAlreadyUsedException;
 41import com.liferay.so.MemberRequestInvalidUserException;
 42import com.liferay.so.invitemembers.util.InviteMembersConstants;
 43import com.liferay.so.model.MemberRequest;
 44import com.liferay.so.service.base.MemberRequestLocalServiceBaseImpl;
 45import com.liferay.so.util.PortletKeys;
 46
 47import java.util.Date;
 48import java.util.List;
 49
 50import javax.mail.internet.InternetAddress;
 51
 52/**
 53 * @author Ryan Park
 54 * @author Jonathan Lee
 55 */
 56public class MemberRequestLocalServiceImpl
 57	extends MemberRequestLocalServiceBaseImpl {
 58
 59	public MemberRequest addMemberRequest(
 60			long userId, long groupId, long receiverUserId,
 61			String receiverEmailAddress, long invitedRoleId, long invitedTeamId,
 62			ServiceContext serviceContext)
 63		throws PortalException, SystemException {
 64
 65		// Member request
 66
 67		User user = userLocalService.getUserById(userId);
 68
 69		try {
 70			User receiverUser = userLocalService.getUserByEmailAddress(
 71				serviceContext.getCompanyId(), receiverEmailAddress);
 72
 73			receiverUserId = receiverUser.getUserId();
 74		}
 75		catch (NoSuchUserException nsue) {
 76		}
 77
 78		Date now = new Date();
 79
 80		long memberRequestId = counterLocalService.increment();
 81
 82		MemberRequest memberRequest = memberRequestPersistence.create(
 83			memberRequestId);
 84
 85		memberRequest.setGroupId(groupId);
 86		memberRequest.setCompanyId(user.getCompanyId());
 87		memberRequest.setUserId(userId);
 88		memberRequest.setUserName(user.getFullName());
 89		memberRequest.setCreateDate(now);
 90		memberRequest.setModifiedDate(now);
 91		memberRequest.setKey(PortalUUIDUtil.generate());
 92		memberRequest.setReceiverUserId(receiverUserId);
 93		memberRequest.setInvitedRoleId(invitedRoleId);
 94		memberRequest.setInvitedTeamId(invitedTeamId);
 95		memberRequest.setStatus(InviteMembersConstants.STATUS_PENDING);
 96
 97		memberRequestPersistence.update(memberRequest);
 98
 99		// Email
100
101		try {
102			sendEmail(receiverEmailAddress, memberRequest, serviceContext);
103		}
104		catch (Exception e) {
105			throw new SystemException(e);
106		}
107
108		// Notifications
109
110		if (receiverUserId > 0) {
111			sendNotificationEvent(memberRequest);
112		}
113
114		return memberRequest;
115	}
116
117	public void addMemberRequests(
118			long userId, long groupId, long[] receiverUserIds,
119			long invitedRoleId, long invitedTeamId,
120			ServiceContext serviceContext)
121		throws PortalException, SystemException {
122
123		for (long receiverUserId : receiverUserIds) {
124			if (hasPendingMemberRequest(groupId, receiverUserId)) {
125				continue;
126			}
127
128			User user = userLocalService.getUser(receiverUserId);
129
130			String emailAddress = user.getEmailAddress();
131
132			addMemberRequest(
133				userId, groupId, receiverUserId, emailAddress, invitedRoleId,
134				invitedTeamId, serviceContext);
135		}
136	}
137
138	public void addMemberRequests(
139			long userId, long groupId, String[] emailAddresses,
140			long invitedRoleId, long invitedTeamId,
141			ServiceContext serviceContext)
142		throws PortalException, SystemException {
143
144		for (String emailAddress : emailAddresses) {
145			if (!Validator.isEmailAddress(emailAddress)) {
146				continue;
147			}
148
149			addMemberRequest(
150				userId, groupId, 0, emailAddress, invitedRoleId, invitedTeamId,
151				serviceContext);
152		}
153	}
154
155	public MemberRequest getMemberRequest(
156			long groupId, long receiverUserId, int status)
157		throws PortalException, SystemException {
158
159		return memberRequestPersistence.findByG_R_S(
160			groupId, receiverUserId, status);
161	}
162
163	public List<MemberRequest> getReceiverMemberRequest(
164			long receiverUserId, int start, int end)
165		throws SystemException {
166
167		return memberRequestPersistence.findByReceiverUserId(receiverUserId);
168	}
169
170	public int getReceiverMemberRequestCount(long receiverUserId)
171		throws SystemException {
172
173		return memberRequestPersistence.countByReceiverUserId(receiverUserId);
174	}
175
176	public List<MemberRequest> getReceiverStatusMemberRequest(
177			long receiverUserId, int status, int start, int end)
178		throws SystemException {
179
180		return memberRequestPersistence.findByR_S(
181			receiverUserId, status, start, end);
182	}
183
184	public int getReceiverStatusMemberRequestCount(
185			long receiverUserId, int status)
186		throws SystemException {
187
188		return memberRequestPersistence.countByR_S(receiverUserId, status);
189	}
190
191	public boolean hasPendingMemberRequest(long groupId, long receiverUserId)
192		throws SystemException {
193
194		MemberRequest memberRequest = memberRequestPersistence.fetchByG_R_S(
195			groupId, receiverUserId, InviteMembersConstants.STATUS_PENDING);
196
197		if (memberRequest != null) {
198			return true;
199		}
200		else {
201			return false;
202		}
203	}
204
205	public MemberRequest updateMemberRequest(
206			long userId, long memberRequestId, int status)
207		throws Exception {
208
209		MemberRequest memberRequest = memberRequestPersistence.findByPrimaryKey(
210			memberRequestId);
211
212		validate(memberRequest, userId);
213
214		memberRequest.setModifiedDate(new Date());
215		memberRequest.setStatus(status);
216
217		memberRequestPersistence.update(memberRequest);
218
219		if (status == InviteMembersConstants.STATUS_ACCEPTED) {
220			userLocalService.addGroupUsers(
221				memberRequest.getGroupId(),
222				new long[] {memberRequest.getReceiverUserId()});
223
224			if (memberRequest.getInvitedRoleId() > 0) {
225				userGroupRoleLocalService.addUserGroupRoles(
226					new long[] {memberRequest.getReceiverUserId()},
227					memberRequest.getGroupId(),
228					memberRequest.getInvitedRoleId());
229			}
230
231			if (memberRequest.getInvitedTeamId() > 0) {
232				userLocalService.addTeamUsers(
233					memberRequest.getInvitedTeamId(),
234					new long[] {memberRequest.getReceiverUserId()});
235			}
236		}
237
238		return memberRequest;
239	}
240
241	public MemberRequest updateMemberRequest(String key, long receiverUserId)
242		throws PortalException, SystemException {
243
244		MemberRequest memberRequest = memberRequestPersistence.findByKey(key);
245
246		validate(memberRequest, 0);
247
248		memberRequest.setModifiedDate(new Date());
249		memberRequest.setReceiverUserId(receiverUserId);
250
251		memberRequestPersistence.update(memberRequest);
252
253		if (receiverUserId > 0) {
254			sendNotificationEvent(memberRequest);
255		}
256
257		return memberRequest;
258	}
259
260	protected String getCreateAccountURL(
261		MemberRequest memberRequest, ServiceContext serviceContext) {
262
263		String createAccountURL = (String)serviceContext.getAttribute(
264			"createAccountURL");
265
266		if (Validator.isNull(createAccountURL)) {
267			createAccountURL = serviceContext.getPortalURL();
268		}
269
270		String redirectURL = getRedirectURL(serviceContext);
271
272		redirectURL = HttpUtil.addParameter(
273			redirectURL, "key", memberRequest.getKey());
274
275		createAccountURL = HttpUtil.addParameter(
276			createAccountURL, "redirect", redirectURL);
277
278		return createAccountURL;
279	}
280
281	protected String getLoginURL(ServiceContext serviceContext) {
282		String loginURL = (String)serviceContext.getAttribute("loginURL");
283
284		if (Validator.isNull(loginURL)) {
285			loginURL = serviceContext.getPortalURL();
286		}
287
288		String redirectURL = getRedirectURL(serviceContext);
289
290		return HttpUtil.addParameter(loginURL, "redirect", redirectURL);
291	}
292
293	protected String getRedirectURL(ServiceContext serviceContext) {
294		String redirectURL = (String)serviceContext.getAttribute("redirectURL");
295
296		if (Validator.isNull(redirectURL)) {
297			redirectURL = serviceContext.getCurrentURL();
298		}
299
300		return redirectURL;
301	}
302
303	protected void sendEmail(
304			String emailAddress, MemberRequest memberRequest,
305			ServiceContext serviceContext)
306		throws Exception {
307
308		long companyId = memberRequest.getCompanyId();
309
310		Group group = groupLocalService.getGroup(memberRequest.getGroupId());
311
312		User user = userLocalService.getUser(memberRequest.getUserId());
313
314		User receiverUser = null;
315
316		if (memberRequest.getReceiverUserId() > 0) {
317			receiverUser = userLocalService.getUser(
318				memberRequest.getReceiverUserId());
319		}
320
321		String fromName = PrefsPropsUtil.getString(
322			companyId, PropsKeys.ADMIN_EMAIL_FROM_NAME);
323		String fromAddress = PrefsPropsUtil.getString(
324			companyId, PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
325
326		String toName = StringPool.BLANK;
327		String toAddress = emailAddress;
328
329		if (receiverUser != null) {
330			toName = receiverUser.getFullName();
331		}
332
333		String subject = StringUtil.read(
334			getClassLoader(),
335			"com/liferay/so/invitemembers/dependencies/subject.tmpl");
336
337		String body = StringPool.BLANK;
338
339		if (memberRequest.getReceiverUserId() > 0) {
340			body = StringUtil.read(
341				getClassLoader(),
342				"com/liferay/so/invitemembers/dependencies/" +
343					"existing_user_body.tmpl");
344		}
345		else {
346			body = StringUtil.read(
347				getClassLoader(),
348				"com/liferay/so/invitemembers/dependencies/" +
349					"new_user_body.tmpl");
350		}
351
352		subject = StringUtil.replace(
353			subject,
354			new String[] {
355				"[$MEMBER_REQUEST_GROUP$]", "[$MEMBER_REQUEST_USER$]"
356			},
357			new String[] {
358				group.getDescriptiveName(serviceContext.getLocale()),
359				user.getFullName()
360			});
361
362		body = StringUtil.replace(
363			body,
364			new String[] {
365				"[$ADMIN_ADDRESS$]", "[$ADMIN_NAME$]",
366				"[$MEMBER_REQUEST_CREATE_ACCOUNT_URL$]",
367				"[$MEMBER_REQUEST_GROUP$]", "[$MEMBER_REQUEST_LOGIN_URL$]",
368				"[$MEMBER_REQUEST_USER$]"
369			},
370			new String[] {
371				fromAddress, fromName,
372				getCreateAccountURL(memberRequest, serviceContext),
373				group.getDescriptiveName(serviceContext.getLocale()),
374				getLoginURL(serviceContext), user.getFullName()
375			});
376
377		InternetAddress from = new InternetAddress(fromAddress, fromName);
378
379		InternetAddress to = new InternetAddress(toAddress, toName);
380
381		MailMessage mailMessage = new MailMessage(
382			from, to, subject, body, true);
383
384		MailServiceUtil.sendEmail(mailMessage);
385	}
386
387	protected void sendNotificationEvent(MemberRequest memberRequest)
388		throws PortalException {
389
390		JSONObject notificationEventJSONObject =
391			JSONFactoryUtil.createJSONObject();
392
393		notificationEventJSONObject.put("groupId", memberRequest.getGroupId());
394		notificationEventJSONObject.put(
395			"memberRequestId", memberRequest.getMemberRequestId());
396		notificationEventJSONObject.put(
397			"portletId", PortletKeys.SO_INVITE_MEMBERS);
398		notificationEventJSONObject.put("title", "x-invited-you-to-join-x");
399		notificationEventJSONObject.put("userId", memberRequest.getUserId());
400
401		NotificationEvent notificationEvent =
402			NotificationEventFactoryUtil.createNotificationEvent(
403				System.currentTimeMillis(), PortletKeys.SO_NOTIFICATION,
404				notificationEventJSONObject);
405
406		notificationEvent.setDeliveryRequired(0);
407
408		ChannelHubManagerUtil.sendNotificationEvent(
409			memberRequest.getCompanyId(), memberRequest.getReceiverUserId(),
410			notificationEvent);
411	}
412
413	protected void validate(MemberRequest memberRequest, long userId)
414		throws PortalException {
415
416		if (memberRequest.getStatus() !=
417				InviteMembersConstants.STATUS_PENDING) {
418
419			throw new MemberRequestAlreadyUsedException();
420		}
421		else if (memberRequest.getReceiverUserId() != userId) {
422			throw new MemberRequestInvalidUserException();
423		}
424	}
425
426}