/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This file is part of Liferay Social Office. Liferay Social Office is free
* software: you can redistribute it and/or modify it under the terms of the GNU
* Affero General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* Liferay Social Office is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* Liferay Social Office. If not, see http://www.gnu.org/licenses/agpl-3.0.html.
*/
package com.liferay.contacts.contactscenter.notifications;
import com.liferay.contacts.util.PortletKeys;
import com.liferay.contacts.util.SocialRelationConstants;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.model.UserNotificationEvent;
import com.liferay.portal.kernel.notifications.BaseUserNotificationHandler;
import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.UserLocalServiceUtil;
import com.liferay.portal.kernel.service.UserNotificationEventLocalServiceUtil;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.social.kernel.model.SocialRequest;
import com.liferay.social.kernel.model.SocialRequestConstants;
import com.liferay.social.kernel.service.SocialRequestLocalServiceUtil;
import javax.portlet.ActionRequest;
import javax.portlet.PortletURL;
import javax.portlet.WindowState;
/**
* @author Jonathan Lee
*/
public class ContactsCenterUserNotificationHandler
extends BaseUserNotificationHandler {
public ContactsCenterUserNotificationHandler() {
setActionable(true);
setPortletId(PortletKeys.CONTACTS_CENTER);
}
@Override
protected String getBody(
UserNotificationEvent userNotificationEvent,
ServiceContext serviceContext)
throws Exception {
JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
userNotificationEvent.getPayload());
long socialRequestId = jsonObject.getLong("classPK");
SocialRequest socialRequest =
SocialRequestLocalServiceUtil.fetchSocialRequest(socialRequestId);
if (socialRequest == null) {
UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
userNotificationEvent.getUserNotificationEventId());
return null;
}
String title = StringPool.BLANK;
if (socialRequest.getType() ==
SocialRelationConstants.TYPE_BI_CONNECTION) {
String creatorUserName = getUserNameLink(
socialRequest.getUserId(), serviceContext);
title = serviceContext.translate(
"request-social-networking-summary-add-connection",
creatorUserName);
}
LiferayPortletResponse liferayPortletResponse =
serviceContext.getLiferayPortletResponse();
PortletURL confirmURL = liferayPortletResponse.createActionURL(
PortletKeys.CONTACTS_CENTER);
confirmURL.setParameter(
ActionRequest.ACTION_NAME, "updateSocialRequest");
confirmURL.setParameter("redirect", serviceContext.getLayoutFullURL());
confirmURL.setParameter(
"socialRequestId", String.valueOf(socialRequestId));
confirmURL.setParameter(
"status", String.valueOf(SocialRequestConstants.STATUS_CONFIRM));
confirmURL.setParameter(
"userNotificationEventId",
String.valueOf(userNotificationEvent.getUserNotificationEventId()));
confirmURL.setWindowState(WindowState.NORMAL);
PortletURL ignoreURL = liferayPortletResponse.createActionURL(
PortletKeys.CONTACTS_CENTER);
ignoreURL.setParameter(
ActionRequest.ACTION_NAME, "updateSocialRequest");
ignoreURL.setParameter("redirect", serviceContext.getLayoutFullURL());
ignoreURL.setParameter(
"socialRequestId", String.valueOf(socialRequestId));
ignoreURL.setParameter(
"status", String.valueOf(SocialRequestConstants.STATUS_IGNORE));
ignoreURL.setParameter(
"userNotificationEventId",
String.valueOf(userNotificationEvent.getUserNotificationEventId()));
ignoreURL.setWindowState(WindowState.NORMAL);
return StringUtil.replace(
getBodyTemplate(),
new String[] {
"[$CONFIRM$]", "[$CONFIRM_URL$]", "[$IGNORE$]",
"[$IGNORE_URL$]", "[$TITLE$]"
},
new String[] {
serviceContext.translate("confirm"), confirmURL.toString(),
serviceContext.translate("ignore"), ignoreURL.toString(), title
});
}
@Override
protected String getLink(
UserNotificationEvent userNotificationEvent,
ServiceContext serviceContext)
throws Exception {
return StringPool.BLANK;
}
protected String getUserNameLink(
long userId, ServiceContext serviceContext) {
try {
if (userId <= 0) {
return StringPool.BLANK;
}
User user = UserLocalServiceUtil.getUserById(userId);
String userName = user.getFullName();
String userDisplayURL = user.getDisplayURL(
serviceContext.getThemeDisplay());
return "<a href=\"" + userDisplayURL + "\">" +
HtmlUtil.escape(userName) + "</a>";
}
catch (Exception e) {
return StringPool.BLANK;
}
}
}