Thanks for the response. So here is my code which has multiple messages to display some are dynamic messages, based on that document the message will display duplicate id’s
If you see I am using createViolation(Map)
I added at translation like below( I believe this is not right way, but I don’t want to add a message here as this is dynamic)
audience-validation#BlankAcct: ‘’"
The violation message is added to map with a key, my understanding is this message will be displayed based on key and looks up in translation, however it’s not how it’s behaving
two issues to resolve, how can we send the message instead of adding at translation, the reason is messages are dynamic as you see in code in message we send duplicate id’s, we can’t have them at translation- is there a way ?
import org.apache.commons.lang.StringUtils;
import org.apache.tika.Tika;
import org.onehippo.cms.services.validation.api.ValidationContext;
import org.onehippo.cms.services.validation.api.Validator;
import org.onehippo.cms.services.validation.api.Violation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class AudienceValidator implements Validator {
private static final Logger L = LoggerFactory.getLogger(AudienceValidator.class);
private static final String PROPERTY_ACC_NUM_LIST = “staplescms:accountNumberList”;
@Override
public Optional<Violation> validate(ValidationContext validationContext, String value){
String errorMsg = null;
List<String> messages = new ArrayList<>();
Map<String, String> errorMessages = new HashMap<>();
try {
Node mod = validationContext.getDocumentNode();
Node contentNode = mod.getNode(PROPERTY_ACC_NUM_LIST);
String mimeType = contentNode.getProperty("jcr:mimeType").getString();
Binary binary = contentNode.getProperty("jcr:data"). getBinary();
Tika tika = new Tika();
if(!mimeType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")){
//"Upload only Excel File (.xlsx)";
return Optional.of(validationContext.createViolation());//added at translation without subkey
}else {
final InputStream in = binary.getStream();
String text = tika.parseToString(in);
String customerIds = text.trim();
List<String> blankCustIds = new ArrayList<>();
List<String> inValidCustIds = new ArrayList<String>();
List<String> custIds = new ArrayList<String>();
if (StringUtils.isNotBlank(customerIds)) {
if ("...please wait while we process your request...".equals(customerIds)) {
L.debug("Processing redirect case : \"...please wait while we process your request...\".");
}
String lines[] = customerIds.split("\\r?\\n");
for (int i=0;i<lines.length;i++) {
String s = lines[i];
if (s.contains("Sheet") || s.contains("CUST")) {
//do nothing
} else {
if((StringUtils.isNotBlank(s) || StringUtils.isNotEmpty(s)) && s.contains("\t")){
String individualLines[] = s.split("\\t");
String customerId = individualLines[1].trim();
if(StringUtils.isBlank(customerId)) {
blankCustIds.add(Integer.toString(i+1));
}else {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher matcher = pattern.matcher(customerId);
if(!matcher.matches()) {
inValidCustIds.add(customerId);
}else {
custIds.add(customerId);
}
}
}
}
}
if(!blankCustIds.isEmpty()) {
errorMsg = "Customer Ids are blank for the following rows:"+blankCustIds;
errorMessages.put("BlankAcct", errorMsg);
}
if(!inValidCustIds.isEmpty()) {
errorMsg = "The following invalid Customer Ids are present :"+inValidCustIds;
errorMessages.put("InvalidAcct", errorMsg);
}
if(custIds.isEmpty()) {
errorMsg = "The Excel dose not have valid enteries/No Enteries is present";
errorMessages.put("InvalidExcel", errorMsg);
}else {
Set<String> duplicateCustIds = findDuplicateInStream(custIds);
if(!duplicateCustIds.isEmpty()) {
L.debug("The input has the following duplicate customer ids:"+duplicateCustIds);
errorMsg = "The input has the following duplicate customer ids:"+duplicateCustIds;
errorMessages.put("Duplicate", errorMsg);
}
}
}
if(!errorMessages.isEmpty()){
return Optional.of(validationContext.createViolation(errorMessages));
}
}
} catch (RepositoryException e) {
L.error("FAILURE!", e);
errorMsg = "The Excel is not valid .Please upload the Excel in the format Attached \'Audience_Sample.xlsx\'";
return Optional.of(validationContext.createViolation());
} catch (Exception e) {
L.error("Failed to Validate Excel Fields.", e);
errorMsg = "The Excel is not valid .Please upload the Excel in the format Attached \'Audience_Sample.xlsx\'";
return Optional.of(validationContext.createViolation());
}
return Optional.empty();
}
public Set<String> findDuplicateInStream(List<String> list) {
Map<String, Long> counts = list.stream()
.collect(Collectors.groupingBy(s -> s, Collectors.counting()));
return counts.entrySet().stream()
.filter(entry -> entry.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
}