use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class RequiredIfPropertyValidator method validate.
@Override
public PropertyValidationResult validate(Entity entity, Serializable instance, Map<String, FieldMetadata> entityFieldMetadata, Map<String, String> validationConfiguration, BasicFieldMetadata propertyMetadata, String propertyName, String value) {
String errorMessage = "";
String compareFieldName = lookupCompareFieldName(propertyName, validationConfiguration);
String compareFieldValue = validationConfiguration.get("compareFieldValue");
String compareFieldRegEx = validationConfiguration.get("compareFieldRegEx");
Property compareFieldProperty = null;
boolean valid = true;
if (StringUtils.isEmpty(value)) {
compareFieldProperty = entity.getPMap().get(compareFieldName);
if (compareFieldProperty != null) {
if (compareFieldValue != null) {
valid = !compareFieldValue.equals(compareFieldProperty.getValue());
} else if (compareFieldRegEx != null) {
String expression = validationConfiguration.get("compareFieldRegEx");
valid = !compareFieldProperty.getValue().matches(expression);
}
}
}
if (!valid) {
BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
MessageSource messages = context.getMessageSource();
FieldMetadata fmd = entityFieldMetadata.get(compareFieldName);
String fieldName = messages.getMessage(fmd.getFriendlyName(), null, context.getJavaLocale());
errorMessage = messages.getMessage("requiredIfValidationFailure", new Object[] { fieldName, compareFieldProperty.getValue() }, context.getJavaLocale());
}
return new PropertyValidationResult(valid, errorMessage);
}
use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class BasicFieldTypeValidator method validate.
@Override
public PropertyValidationResult validate(PopulateValueRequest populateValueRequest, Serializable instance) {
BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
Locale locale = brc.getJavaLocale();
DecimalFormat format = populateValueRequest.getDataFormatProvider().getDecimalFormatter();
ParsePosition pp;
switch(populateValueRequest.getMetadata().getFieldType()) {
case INTEGER:
try {
if (int.class.isAssignableFrom(populateValueRequest.getReturnType()) || Integer.class.isAssignableFrom(populateValueRequest.getReturnType())) {
Integer.parseInt(populateValueRequest.getRequestedValue());
} else if (byte.class.isAssignableFrom(populateValueRequest.getReturnType()) || Byte.class.isAssignableFrom(populateValueRequest.getReturnType())) {
Byte.parseByte(populateValueRequest.getRequestedValue());
} else if (short.class.isAssignableFrom(populateValueRequest.getReturnType()) || Short.class.isAssignableFrom(populateValueRequest.getReturnType())) {
Short.parseShort(populateValueRequest.getRequestedValue());
} else if (long.class.isAssignableFrom(populateValueRequest.getReturnType()) || Long.class.isAssignableFrom(populateValueRequest.getReturnType())) {
Long.parseLong(populateValueRequest.getRequestedValue());
}
} catch (NumberFormatException e) {
return new PropertyValidationResult(false, "Field must be an valid number");
}
break;
case DECIMAL:
pp = new ParsePosition(0);
if (BigDecimal.class.isAssignableFrom(populateValueRequest.getReturnType())) {
format.setParseBigDecimal(true);
format.parse(populateValueRequest.getRequestedValue(), pp);
format.setParseBigDecimal(false);
} else {
format.parse(populateValueRequest.getRequestedValue(), pp);
}
if (pp.getIndex() != populateValueRequest.getRequestedValue().length()) {
return new PropertyValidationResult(false, "Field must be a valid decimal");
}
break;
case MONEY:
pp = new ParsePosition(0);
try {
if (Double.class.isAssignableFrom(populateValueRequest.getReturnType())) {
format.parse(populateValueRequest.getRequestedValue(), pp);
} else {
format.setParseBigDecimal(true);
format.parse(populateValueRequest.getRequestedValue(), pp);
format.setParseBigDecimal(false);
}
if (pp.getIndex() != populateValueRequest.getRequestedValue().length()) {
return new PropertyValidationResult(false, "Field must be a valid number");
}
} catch (NumberFormatException e) {
return new PropertyValidationResult(false, "Field must be a valid number");
}
break;
case DATE:
try {
populateValueRequest.getDataFormatProvider().getSimpleDateFormatter().parse(populateValueRequest.getRequestedValue());
} catch (ParseException e) {
return new PropertyValidationResult(false, "Field must be a date of the format: " + populateValueRequest.getDataFormatProvider().getSimpleDateFormatter().toPattern());
}
break;
case FOREIGN_KEY:
case ADDITIONAL_FOREIGN_KEY:
if (Collection.class.isAssignableFrom(populateValueRequest.getReturnType())) {
Collection collection;
try {
collection = (Collection) populateValueRequest.getFieldManager().getFieldValue(instance, populateValueRequest.getProperty().getName());
} catch (FieldNotAvailableException e) {
return new PropertyValidationResult(false, "External entity cannot be added to the specified collection at " + populateValueRequest.getProperty().getName());
} catch (IllegalAccessException e) {
return new PropertyValidationResult(false, "External entity cannot be added to the specified collection at " + populateValueRequest.getProperty().getName());
}
} else if (Map.class.isAssignableFrom(populateValueRequest.getReturnType())) {
return new PropertyValidationResult(false, "External entity cannot be added to a map at " + populateValueRequest.getProperty().getName());
}
case ID:
if (populateValueRequest.getSetId()) {
switch(populateValueRequest.getMetadata().getSecondaryType()) {
case INTEGER:
Long.valueOf(populateValueRequest.getRequestedValue());
break;
default:
}
}
default:
return new PropertyValidationResult(true);
}
return new PropertyValidationResult(true);
}
use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class AddSortLinkProcessor method getModifiedAttributes.
@Override
public BroadleafAttributeModifier getModifiedAttributes(String tagName, Map<String, String> tagAttributes, String attributeName, String attributeValue, BroadleafTemplateContext context) {
BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
HttpServletRequest request = blcContext.getRequest();
String baseUrl = request.getRequestURL().toString();
Map<String, String[]> params = new HashMap<>(request.getParameterMap());
String key = SearchCriteria.SORT_STRING;
List<String[]> sortedFields = new ArrayList<>();
String[] paramValues = params.get(key);
if (ArrayUtils.isNotEmpty(paramValues)) {
String sortQueries = paramValues[0];
for (String sortQuery : sortQueries.split(",")) {
String[] sort = sortQuery.split(" ");
if (sort.length == 2) {
sortedFields.add(new String[] { sort[0], sort[1] });
}
}
}
boolean currentlySortingOnThisField = false;
boolean currentlyAscendingOnThisField = false;
for (String[] sortedField : sortedFields) {
if (attributeValue.equals(sortedField[0])) {
currentlySortingOnThisField = true;
currentlyAscendingOnThisField = sortedField[1].equals("asc");
sortedField[1] = currentlyAscendingOnThisField ? "desc" : "asc";
}
}
String sortString = attributeValue;
String classString = "";
if (currentlySortingOnThisField) {
classString += "active ";
if (currentlyAscendingOnThisField) {
sortString += " desc";
classString += "asc ";
} else {
sortString += " asc";
classString += "desc ";
}
} else {
sortString += " asc";
classString += "asc ";
params.remove(SearchCriteria.PAGE_NUMBER);
}
if (allowMultipleSorts) {
StringBuilder sortSb = new StringBuilder();
for (String[] sortedField : sortedFields) {
sortSb.append(sortedField[0]).append(" ").append(sortedField[1]).append(",");
}
sortString = sortSb.toString();
if (sortString.charAt(sortString.length() - 1) == ',') {
sortString = sortString.substring(0, sortString.length() - 1);
}
}
params.put(key, new String[] { sortString });
String url = ProcessorUtils.getUrl(baseUrl, params);
Map<String, String> newAttributes = new HashMap<>();
newAttributes.put("class", classString);
newAttributes.put("href", url);
return new BroadleafAttributeModifier(newAttributes);
}
use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class OnePageCheckoutProcessor method populateProcessingError.
/**
* This method is responsible for populating any Payment Procecessing Errors that may have been put
* as a Redirect Attribute when attempting to checkout.
*
* @param localVars
*/
protected void populateProcessingError(Map<String, Object> localVars) {
BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
HttpServletRequest request = blcContext.getRequest();
String processorError = request.getParameter(PaymentGatewayAbstractController.PAYMENT_PROCESSING_ERROR);
localVars.put(PaymentGatewayAbstractController.PAYMENT_PROCESSING_ERROR, processorError);
}
use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.
the class SolrConfiguration method getSiteReindexServer.
public SolrClient getSiteReindexServer() {
BroadleafRequestContext ctx = BroadleafRequestContext.getBroadleafRequestContext();
Site site = ctx.getNonPersistentSite();
CloudSolrClient client = (CloudSolrClient) primaryServer;
client.connect();
String aliasName = getSiteReindexAliasName(site);
if (aliasName != null) {
String collectionName = getSiteReindexCollectionName(site);
createCollectionIfNotExist(client, collectionName);
createAliasIfNotExist(client, collectionName, collectionName);
}
return client;
}
Aggregations