Skip to content

Instantly share code, notes, and snippets.

@ryanschuhler
Last active November 15, 2015 05:03
Show Gist options
  • Save ryanschuhler/f6ce5b4c1ba2ab6a4be8 to your computer and use it in GitHub Desktop.
Save ryanschuhler/f6ce5b4c1ba2ab6a4be8 to your computer and use it in GitHub Desktop.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License for more
* details.
*/
package com.liferay.content.targeting.rule.persona;
import com.liferay.content.targeting.anonymous.users.model.AnonymousUser;
import com.liferay.content.targeting.api.model.BaseRule;
import com.liferay.content.targeting.api.model.Rule;
import com.liferay.content.targeting.model.RuleInstance;
import com.liferay.content.targeting.rule.categories.UserAttributesRuleCategory;
import com.liferay.hubspot.model.HSContact;
import com.liferay.hubspot.service.HSContactLocalService;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import java.util.Locale;
import java.util.Map;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.servlet.http.HttpServletRequest;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
/**
* @author Brian Chan
*/
@Component(immediate = true, service = Rule.class)
public class PersonaRule extends BaseRule {
@Activate
@Override
public void activate() {
super.activate();
}
@Deactivate
@Override
public void deActivate() {
super.deActivate();
}
@Override
public boolean evaluate(
HttpServletRequest request, RuleInstance ruleInstance,
AnonymousUser anonymousUser)
throws Exception {
String userPersona = getUserPersona(request);
String persona = ruleInstance.getTypeSettings();
if (Validator.equals(userPersona, persona)) {
return true;
}
return false;
}
@Override
public String getIcon() {
return "icon-male";
}
@Override
public String getRuleCategoryKey() {
return UserAttributesRuleCategory.KEY;
}
@Override
public String getSummary(RuleInstance ruleInstance, Locale locale) {
return LanguageUtil.get(locale, ruleInstance.getTypeSettings());
}
public String getUserPersona(PortletRequest request) {
String hsutk = request.getAttribute("hsutk");
HSContact hsContact =
HSContactLocalServiceUtil.fetchHSContactByUserToken(hsutk);
return hsContact.getPersona();
}
@Override
public String processRule(
PortletRequest request, PortletResponse response, String id,
Map<String, String> values) {
return values.get("persona");
}
@Override
protected void populateContext(
RuleInstance ruleInstance, Map<String, Object> context,
Map<String, String> values) {
String persona = StringPool.BLANK;
if (!values.isEmpty()) {
persona = values.get("persona");
}
else if (ruleInstance != null) {
persona = ruleInstance.getTypeSettings();
}
context.put("persona", persona);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment