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;
}
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;
}
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);
}
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);
}
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());
}
Aggregations