Skip to content

Instantly share code, notes, and snippets.

@epochcoder
Created November 29, 2017 15:56
Show Gist options
  • Save epochcoder/ad2d4c87ddb1d22cc8c2bcff41b09973 to your computer and use it in GitHub Desktop.
Save epochcoder/ad2d4c87ddb1d22cc8c2bcff41b09973 to your computer and use it in GitHub Desktop.
An example product portfolio collector for the Backbase connect Targeting workshop
package com.backbase.connect.mockproducts;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.backbase.cxp.targeting.api.Collector;
import com.backbase.cxp.targeting.contexts.AbstractContextCollector;
import com.backbase.cxp.targeting.contexts.definition.CollectorType;
import com.backbase.cxp.targeting.contexts.definition.OperatorPrimitive;
import com.backbase.cxp.targeting.contexts.definition.SelectorDefinition;
import com.backbase.cxp.targeting.contexts.definition.UniqueUserProfile;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* A collector that mocks products a specific user may have
*/
@Collector(id = "product-portfolio-collector", label = "Product Portfolio", type = CollectorType.SESSION)
public class ProductPortfolioCollector extends AbstractContextCollector {
private static final String USER_ADMIN = "admin";
private static final String USER_MANAGER = "manager";
private final Multimap<String, String> userProducts = HashMultimap.create();
private final List<String> products = new ArrayList<>();
public ProductPortfolioCollector() {
// would be loaded from some other source
this.products.add("Car Loan");
this.products.add("Credit Card");
this.products.add("Home Insurance");
this.products.add("Share Builder");
this.userProducts.put(USER_ADMIN, this.products.get(0));
this.userProducts.put(USER_ADMIN, this.products.get(1));
this.userProducts.put(USER_MANAGER, this.products.get(0));
this.userProducts.put(USER_MANAGER, this.products.get(2));
this.userProducts.put(USER_MANAGER, this.products.get(3));
}
@Override
public List<SelectorDefinition> define(String portalName, Map<String, String> requestParameters) {
final List<SelectorDefinition> productsDef = new ArrayList<>();
for (String product : this.products) {
productsDef.add(new SelectorDefinition(OperatorPrimitive.BOOLEAN, getProductId(product), product)
.description(String.format("Does this customer have %s?", product)));
}
return productsDef;
}
@Override
public UniqueUserProfile execute(Map<String, String> requestMap, UniqueUserProfile uup) {
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
final String userName = authentication != null ? String.valueOf(authentication.getName()) : "";
// intially set all products to false
for (String product : this.products) {
final String pId = getProductId(product);
uup.removeSelectors(pId);
uup.addSelectorEntry(pId, "false");
}
// set user products
for (String userProduct : this.userProducts.get(userName)) {
final String pId = getProductId(userProduct);
uup.removeSelectors(pId);
uup.addSelectorEntry(pId, "true");
}
return uup;
}
private static String getProductId(String product) {
return "pid-" + product
.replaceAll("[^a-zA-Z0-9\\-]+", "-")
.replaceAll("-{2,}", "-")
.toLowerCase(Locale.ENGLISH)
.trim();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment