Search in sources :

Example 1 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class ErrorsProcessor method populateModelVariables.

@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, String attributeName, String attributeValue, BroadleafTemplateContext context) {
    BindStatus bindStatus = context.getBindStatus(attributeValue);
    Map<String, Object> newLocalVars = new HashMap<>();
    if (bindStatus.isError()) {
        EntityForm form = (EntityForm) ((BindingResult) bindStatus.getErrors()).getTarget();
        // Map of tab name -> (Map field Name -> list of error messages)
        Map<String, Map<String, List<String>>> result = new HashMap<>();
        if (!hideTopLevelFieldErrors) {
            for (FieldError err : bindStatus.getErrors().getFieldErrors()) {
                // attempt to look up which tab the field error is on. If it can't be found, just use
                // the default tab for the group
                String tabName = EntityForm.DEFAULT_TAB_NAME;
                Tab tab = form.findTabForField(err.getField());
                if (tab != null) {
                    tabName = tab.getTitle();
                }
                Map<String, List<String>> tabErrors = result.get(tabName);
                if (tabErrors == null) {
                    tabErrors = new HashMap<>();
                    result.put(tabName, tabErrors);
                }
                if (err.getField().contains(DynamicEntityFormInfo.FIELD_SEPARATOR)) {
                    // at this point the field name actually occurs within some array syntax
                    String fieldName = extractFieldName(err);
                    String[] fieldInfo = fieldName.split("\\" + DynamicEntityFormInfo.FIELD_SEPARATOR);
                    Field formField = form.getDynamicForm(fieldInfo[0]).findField(fieldName);
                    if (formField != null) {
                        addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
                    } else {
                        LOG.warn("Could not find field " + fieldName + " within the dynamic form " + fieldInfo[0]);
                        addFieldError(fieldName, err.getCode(), tabErrors);
                    }
                } else {
                    if (form.getTabs().size() > 0) {
                        Field formField = form.findField(err.getField());
                        if (formField != null) {
                            addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
                        } else {
                            LOG.warn("Could not find field " + err.getField() + " within the main form");
                            addFieldError(err.getField(), err.getCode(), tabErrors);
                        }
                    } else {
                        // this is the code that is executed when a Translations add action contains errors
                        // this branch of the code just puts a placeholder "tabErrors", to avoid errprProcessor parsing errors, and
                        // avoids checking on tabs, fieldGroups or fields (which for translations are empty), thus skipping any warning
                        newLocalVars.put("tabErrors", tabErrors);
                        return newLocalVars;
                    }
                }
            }
        }
        String translatedGeneralTab = GENERAL_ERRORS_TAB_KEY;
        BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
        if (blcContext != null && blcContext.getMessageSource() != null) {
            translatedGeneralTab = blcContext.getMessageSource().getMessage(translatedGeneralTab, null, translatedGeneralTab, blcContext.getJavaLocale());
        }
        for (ObjectError err : bindStatus.getErrors().getGlobalErrors()) {
            Map<String, List<String>> tabErrors = result.get(GENERAL_ERRORS_TAB_KEY);
            if (tabErrors == null) {
                tabErrors = new HashMap<>();
                result.put(translatedGeneralTab, tabErrors);
            }
            addFieldError(GENERAL_ERROR_FIELD_KEY, err.getCode(), tabErrors);
        }
        newLocalVars.put("tabErrors", result);
    }
    return newLocalVars;
}
Also used : EntityForm(org.broadleafcommerce.openadmin.web.form.entity.EntityForm) HashMap(java.util.HashMap) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) FieldError(org.springframework.validation.FieldError) BindStatus(org.springframework.web.servlet.support.BindStatus) Field(org.broadleafcommerce.openadmin.web.form.entity.Field) ObjectError(org.springframework.validation.ObjectError) Tab(org.broadleafcommerce.openadmin.web.form.entity.Tab) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class CategoryBreadcrumbServiceExtensionHandler method determineFirstCategory.

protected Category determineFirstCategory(String testUrl, Map<String, String[]> params, ExtensionResultHolder<List<BreadcrumbDTO>> holder) {
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    Category returnCategory = null;
    if (brc != null && brc.getRequest() != null) {
        if (returnCategory == null) {
            returnCategory = getMatchingCategoryFromProduct(brc, testUrl, params);
        }
        if (returnCategory == null) {
            returnCategory = getCategoryFromCategoryAttribute(brc, testUrl, params);
        }
        if (returnCategory == null) {
            returnCategory = getCategoryFromUrl(brc, testUrl, params);
        }
    }
    return returnCategory;
}
Also used : Category(org.broadleafcommerce.core.catalog.domain.Category) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext)

Example 3 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class MvelHelperTest method testRequestMapProperty.

/**
 * Tests MVEL syntax for accessing request property map values.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testRequestMapProperty() {
    BroadleafRequestContext.setBroadleafRequestContext(new BroadleafRequestContext());
    RequestDTO dto = new RequestDTOImpl();
    dto.getProperties().put("blcSearchTerm", "hot");
    Map parameters = new HashMap();
    parameters.put("request", dto);
    // If the "key" property doesn't contain an underscore, the expression returns true
    boolean result = MvelHelper.evaluateRule("request.properties['blcSearchTerm'] == 'hot'", parameters);
    assertTrue(result);
}
Also used : HashMap(java.util.HashMap) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) RequestDTOImpl(org.broadleafcommerce.common.RequestDTOImpl) RequestDTO(org.broadleafcommerce.common.RequestDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class StreamTransactionCapableUtilFailureSimulator method endFailureMode.

public void endFailureMode(String persistenceUnit) {
    checkPU(persistenceUnit);
    BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
    context.getAdditionalProperties().remove(FAILURE_MODE_KEY);
    context.getAdditionalProperties().remove(FAILURE_MODE_PU);
    context.getAdditionalProperties().remove(FAILURE_MODE_EXCEPTION);
}
Also used : BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext)

Example 5 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class AdminTestHelper method startView.

/**
 * Useful to start an EntityManager-In-View. This allows test operations that want to read directly from the database
 * to work without lazy init exceptions, etc... This is not needed for MockMVC#perform operations, since those
 * requests will include the OpenEntityManagerInView filter as part of their flow. At the completion of the test
 * operation, the {@link #endView()} method should be called to end the scope of the view.
 * </p>
 * This view scope is also aware of nested views against the same persistence unit, so you don't need to worry
 * about coding carefully to avoid nesting calls to startView.
 *
 * @param siteId
 * @param sandBoxName
 */
public void startView(Long siteId, String sandBoxName) {
    EntityManagerFactory emf = ((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory();
    boolean isEntityManagerInView = TransactionSynchronizationManager.hasResource(emf);
    if (!isEntityManagerInView) {
        EntityManager em = emf.createEntityManager();
        em.clear();
        EntityManagerHolder emHolder = new EntityManagerHolder(em);
        TransactionSynchronizationManager.bindResource(emf, emHolder);
        Stack<String> stack = new Stack<>();
        TransactionSynchronizationManager.bindResource("emStack", stack);
        if (siteId != null) {
            Site site = siteService.retrievePersistentSiteById(siteId);
            ThreadLocalManager.remove();
            BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
            context.setSite(site);
            context.setDeployBehavior(DeployBehavior.CLONE_PARENT);
            context.setAdmin(true);
            if (!StringUtils.isEmpty(sandBoxName)) {
                SandBox sb = findSandBox(siteId, sandBoxName, false);
                context.setSandBox(sb);
            }
        }
    }
    Stack<String> stack = (Stack<String>) TransactionSynchronizationManager.getResource("emStack");
    RuntimeException trace = new RuntimeException();
    StringWriter writer = new StringWriter();
    PrintWriter pw = new PrintWriter(writer);
    trace.printStackTrace(pw);
    stack.push(writer.toString());
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) SandBox(org.broadleafcommerce.common.sandbox.domain.SandBox) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) Stack(java.util.Stack) EntityManager(javax.persistence.EntityManager) StringWriter(java.io.StringWriter) EntityManagerFactory(javax.persistence.EntityManagerFactory) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) PrintWriter(java.io.PrintWriter)

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