use of org.broadleafcommerce.openadmin.web.form.entity.Tab 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.openadmin.web.form.entity.Tab in project BroadleafCommerce by BroadleafCommerce.
the class AdminEntityServiceImpl method getRecordsForSelectedTab.
@Override
public Map<String, DynamicResultSet> getRecordsForSelectedTab(ClassMetadata cmd, Entity containingEntity, List<SectionCrumb> sectionCrumb, String currentTabName) throws ServiceException {
Map<String, DynamicResultSet> map = new HashMap<String, DynamicResultSet>();
for (Property p : cmd.getProperties()) {
if (ArrayUtils.contains(p.getMetadata().getAvailableToTypes(), containingEntity.getType()[0]) && p.getMetadata() instanceof CollectionMetadata) {
CollectionMetadata collectionMetadata = (CollectionMetadata) p.getMetadata();
// Give preference to the Group since EntityForm.addListGrid() gives preference to the Group
TabMetadata tabMetadata = cmd.getTabMetadataUsingGroupKey(collectionMetadata.getGroup());
if (tabMetadata == null) {
tabMetadata = cmd.getTabMetadataUsingTabKey(collectionMetadata.getTab());
}
String tabName = tabMetadata == null ? collectionMetadata.getTab() : tabMetadata.getTabName();
int tabOrder = tabMetadata == null ? collectionMetadata.getTabOrder() : tabMetadata.getTabOrder();
updateTabInfo(collectionMetadata, cmd, tabName, tabOrder);
if (collectionMetadata.getLazyFetch() != null && collectionMetadata.getLazyFetch() && tabName.toUpperCase().startsWith(currentTabName.toUpperCase()) && !collectionMetadata.getManualFetch()) {
PersistenceResponse response2 = getRecordsForCollection(cmd, containingEntity, p, null, null, null, sectionCrumb);
map.put(p.getName(), response2.getDynamicResultSet());
} else if (collectionMetadata.getLazyFetch() != null && !collectionMetadata.getLazyFetch() && !collectionMetadata.getManualFetch()) {
PersistenceResponse response2 = getRecordsForCollection(cmd, containingEntity, p, null, null, null, sectionCrumb);
map.put(p.getName(), response2.getDynamicResultSet());
} else {
DynamicResultSet drs = new DynamicResultSet();
Map<String, Tab> tabMap = new HashMap<String, Tab>();
Tab tab = new Tab();
tab.setKey(tabName);
tab.setTitle(BLCMessageUtils.getMessage(tabName));
tab.setOrder(tabOrder);
tabMap.put(tab.getTitle(), tab);
drs.setUnselectedTabMetadata(tabMap);
drs.setTotalRecords(0);
drs.setStartIndex(0);
drs.setBatchId(1);
drs.setClassMetaData(null);
drs.setPageSize(1);
drs.setRecords(new Entity[0]);
map.put(p.getName(), drs);
}
}
}
return map;
}
use of org.broadleafcommerce.openadmin.web.form.entity.Tab in project BroadleafCommerce by BroadleafCommerce.
the class AdminAbstractController method populateJsonValidationErrors.
/**
* Populates the given <b>json</b> response object based on the given <b>form</b> and <b>result</b>
* @return the same <b>result</b> that was passed in
*/
protected JsonResponse populateJsonValidationErrors(EntityForm form, BindingResult result, JsonResponse json) {
List<Map<String, Object>> errors = new ArrayList<>();
for (FieldError e : result.getFieldErrors()) {
Map<String, Object> errorMap = new HashMap<>();
errorMap.put("errorType", "field");
String fieldName = e.getField().substring(e.getField().indexOf("[") + 1, e.getField().indexOf("]")).replace("_", "-");
errorMap.put("field", fieldName);
errorMap.put("message", translateErrorMessage(e));
errorMap.put("code", e.getCode());
String tabFieldName = fieldName.replaceAll("-+", ".");
Tab errorTab = form.findTabForField(tabFieldName);
if (errorTab != null) {
errorMap.put("tab", errorTab.getTitle());
}
errors.add(errorMap);
}
for (ObjectError e : result.getGlobalErrors()) {
Map<String, Object> errorMap = new HashMap<>();
errorMap.put("errorType", "global");
errorMap.put("code", e.getCode());
errorMap.put("message", translateErrorMessage(e));
errors.add(errorMap);
}
json.with("errors", errors);
return json;
}
Aggregations