Created
November 29, 2017 15:56
-
-
Save epochcoder/ad2d4c87ddb1d22cc8c2bcff41b09973 to your computer and use it in GitHub Desktop.
An example product portfolio collector for the Backbase connect Targeting workshop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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