Search in sources :

Example 71 with BroadleafRequestContext

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);
}
Also used : FieldMetadata(org.broadleafcommerce.openadmin.dto.FieldMetadata) BasicFieldMetadata(org.broadleafcommerce.openadmin.dto.BasicFieldMetadata) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) MessageSource(org.springframework.context.MessageSource) Property(org.broadleafcommerce.openadmin.dto.Property)

Example 72 with BroadleafRequestContext

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);
}
Also used : Locale(java.util.Locale) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) DecimalFormat(java.text.DecimalFormat) FieldNotAvailableException(org.broadleafcommerce.openadmin.server.service.persistence.module.FieldNotAvailableException) Collection(java.util.Collection) ParseException(java.text.ParseException) Map(java.util.Map) ParsePosition(java.text.ParsePosition)

Example 73 with BroadleafRequestContext

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) ArrayList(java.util.ArrayList) BroadleafAttributeModifier(org.broadleafcommerce.presentation.model.BroadleafAttributeModifier)

Example 74 with BroadleafRequestContext

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext)

Example 75 with BroadleafRequestContext

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;
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Aggregations

BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)78 Site (org.broadleafcommerce.common.site.domain.Site)16 HashMap (java.util.HashMap)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 Locale (org.broadleafcommerce.common.locale.domain.Locale)8 SandBox (org.broadleafcommerce.common.sandbox.domain.SandBox)5 StructuredContentDTO (org.broadleafcommerce.common.structure.dto.StructuredContentDTO)5 MessageSource (org.springframework.context.MessageSource)5 List (java.util.List)4 StructuredContent (org.broadleafcommerce.cms.structure.domain.StructuredContent)4 Field (java.lang.reflect.Field)3 HashSet (java.util.HashSet)3 Locale (java.util.Locale)3 Catalog (org.broadleafcommerce.common.site.domain.Catalog)3 BroadleafAttributeModifier (org.broadleafcommerce.presentation.model.BroadleafAttributeModifier)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2