use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class FormBuilderServiceImpl method buildAdornedListForm.
@Override
public EntityForm buildAdornedListForm(AdornedTargetCollectionMetadata adornedMd, AdornedTargetList adornedList, String parentId, boolean isViewCollectionItem, EntityForm ef, List<SectionCrumb> sectionCrumbs, boolean isAdd) throws ServiceException {
ef.setEntityType(adornedList.getAdornedTargetEntityClassname());
// Get the metadata for this adorned field
PersistencePackageRequest request = PersistencePackageRequest.adorned().withCeilingEntityClassname(adornedMd.getCollectionCeilingEntity()).withAdornedList(adornedList).withSectionCrumbs(sectionCrumbs);
request.setAddOperationInspect(isAdd);
ClassMetadata collectionMetadata = adminEntityService.getClassMetadata(request).getDynamicResultSet().getClassMetaData();
List<Property> entityFormProperties = new ArrayList<>();
if (isViewCollectionItem) {
Collections.addAll(entityFormProperties, collectionMetadata.getProperties());
} else {
// We want our entity form to only render the maintained adorned target fields
for (String targetFieldName : adornedMd.getMaintainedAdornedTargetFields()) {
Property p = collectionMetadata.getPMap().get(targetFieldName);
if (p.getMetadata() instanceof BasicFieldMetadata && BooleanUtils.isNotTrue(p.getMetadata().getExcluded())) {
((BasicFieldMetadata) p.getMetadata()).setVisibility(VisibilityEnum.VISIBLE_ALL);
entityFormProperties.add(p);
}
}
}
// Set the maintained fields on the form
setEntityFormFields(collectionMetadata, ef, entityFormProperties);
// Add these two additional hidden fields that are required for persistence
Field f = new Field().withName(adornedList.getLinkedObjectPath() + "." + adornedList.getLinkedIdProperty()).withFieldType(SupportedFieldType.HIDDEN.toString()).withValue(parentId);
ef.addHiddenField(collectionMetadata, f);
f = new Field().withName(adornedList.getTargetObjectPath() + "." + adornedList.getTargetIdProperty()).withFieldType(SupportedFieldType.HIDDEN.toString()).withIdOverride("adornedTargetIdProperty");
ef.addHiddenField(collectionMetadata, f);
if (StringUtils.isNotBlank(adornedList.getSortField())) {
f = new Field().withName(adornedList.getSortField()).withFieldType(SupportedFieldType.HIDDEN.toString());
ef.addHiddenField(collectionMetadata, f);
}
ef.setParentId(parentId);
extensionManager.getProxy().addAdditionalAdornedFormActions(ef);
return ef;
}
use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method buildAddCollectionItemModel.
/**
* Builds out all of the model information needed for showing the add modal for collection items on both the initial GET
* as well as after a POST with validation errors
*
* @param request
* @param model
* @param id
* @param collectionField
* @param sectionKey
* @param collectionProperty
* @param md
* @param ppr
* @return the appropriate view to display for the modal
* @see {@link #addCollectionItem(HttpServletRequest, HttpServletResponse, Model, Map, String, String, EntityForm, BindingResult)}
* @see {@link #showAddCollectionItem(HttpServletRequest, HttpServletResponse, Model, Map, String, String, MultiValueMap)}
* @throws ServiceException
*/
protected String buildAddCollectionItemModel(HttpServletRequest request, HttpServletResponse response, Model model, String id, String collectionField, String sectionKey, Property collectionProperty, FieldMetadata md, PersistencePackageRequest ppr, EntityForm entityForm, Entity entity) throws ServiceException {
// For requests to add a new collection item include the main class that the subsequent request comes from.
// For instance, with basic collections we know the main associated class for a fetch through the ForeignKey
// persistence item but map and adorned target lookups make a standard persistence request. This solution
// fixes all cases.
String mainClassName = getClassNameForSection(sectionKey);
ppr.addCustomCriteria("owningClass=" + mainClassName);
ppr.setAddOperationInspect(true);
if (entityForm != null) {
entityForm.clearFieldsMap();
}
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
if (md instanceof BasicCollectionMetadata) {
BasicCollectionMetadata fmd = (BasicCollectionMetadata) md;
// and sometimes show a list grid to allow the user to associate an existing record.
if (fmd.getAddMethodType().equals(AddMethodType.PERSIST)) {
ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
if (entityForm == null) {
entityForm = formService.createEntityForm(collectionMetadata, sectionCrumbs);
entityForm.setCeilingEntityClassname(ppr.getCeilingEntityClassname());
entityForm.setEntityType(ppr.getCeilingEntityClassname());
} else {
formService.populateEntityForm(collectionMetadata, entityForm, sectionCrumbs);
formService.populateEntityFormFieldValues(collectionMetadata, entity, entityForm);
}
formService.removeNonApplicableFields(collectionMetadata, entityForm, ppr.getCeilingEntityClassname());
entityForm.getTabs().iterator().next().getIsVisible();
model.addAttribute("entityForm", entityForm);
model.addAttribute("viewType", "modal/simpleAddEntity");
} else {
DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
ListGrid listGrid = formService.buildCollectionListGrid(id, drs, collectionProperty, sectionKey, sectionCrumbs);
listGrid.setPathOverride(request.getRequestURL().toString());
if (AddMethodType.LOOKUP.equals(fmd.getAddMethodType()) || AddMethodType.SELECTIZE_LOOKUP.equals(fmd.getAddMethodType())) {
listGrid.removeAllRowActions();
}
model.addAttribute("listGrid", listGrid);
model.addAttribute("viewType", "modal/simpleSelectEntity");
}
} else if (md instanceof AdornedTargetCollectionMetadata) {
AdornedTargetCollectionMetadata fmd = (AdornedTargetCollectionMetadata) md;
// Even though this field represents an adorned target collection, the list we want to show in the modal
// is the standard list grid for the target entity of this field
ppr.setOperationTypesOverride(null);
ppr.setType(PersistencePackageRequest.Type.STANDARD);
ppr.setSectionEntityField(collectionField);
ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
ListGrid listGrid = formService.buildCollectionListGrid(id, drs, collectionProperty, sectionKey, sectionCrumbs);
listGrid.setSubCollectionFieldName(collectionField);
listGrid.setPathOverride(request.getRequestURL().toString());
listGrid.setFriendlyName(collectionMetadata.getPolymorphicEntities().getFriendlyName());
if (entityForm == null) {
entityForm = formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, false, sectionCrumbs, true);
entityForm.setCeilingEntityClassname(ppr.getAdornedList().getAdornedTargetEntityClassname());
} else {
formService.buildAdornedListForm(fmd, ppr.getAdornedList(), id, false, entityForm, sectionCrumbs, true);
formService.populateEntityFormFieldValues(collectionMetadata, entity, entityForm);
}
listGrid.setListGridType(ListGrid.Type.ADORNED);
for (Entry<String, Field> entry : entityForm.getFields().entrySet()) {
if (entry.getValue().getIsVisible()) {
listGrid.setListGridType(ListGrid.Type.ADORNED_WITH_FORM);
break;
}
}
// This is part of an add, so we want to be able to filter/sort the listgrid
listGrid.setIsSortable(false);
listGrid.setCanFilterAndSort(true);
listGrid.removeAllRowActions();
model.addAttribute("listGrid", listGrid);
model.addAttribute("entityForm", entityForm);
model.addAttribute("viewType", "modal/adornedSelectEntity");
} else if (md instanceof MapMetadata) {
MapMetadata fmd = (MapMetadata) md;
ClassMetadata collectionMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
if (entityForm == null) {
entityForm = formService.buildMapForm(fmd, ppr.getMapStructure(), collectionMetadata, id);
} else {
formService.buildMapForm(fmd, ppr.getMapStructure(), collectionMetadata, id, entityForm);
formService.populateEntityFormFieldValues(collectionMetadata, entity, entityForm);
}
model.addAttribute("entityForm", entityForm);
model.addAttribute("viewType", "modal/mapAddEntity");
}
// Set the parent id on the entity form
if (entityForm != null) {
entityForm.setParentId(id);
}
model.addAttribute("currentUrl", request.getRequestURL().toString());
model.addAttribute("modalHeaderType", ModalHeaderType.ADD_COLLECTION_ITEM.getType());
model.addAttribute("collectionProperty", collectionProperty);
setModelAttributes(model, sectionKey);
return "modules/modalContainer";
}
use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminEntityServiceImpl method getClassMetadata.
@Override
public PersistenceResponse getClassMetadata(PersistencePackageRequest request) throws ServiceException {
PersistenceResponse response = inspect(request);
ClassMetadata cmd = response.getDynamicResultSet().getClassMetaData();
cmd.setCeilingType(request.getCeilingEntityClassname());
cmd.setSecurityCeilingType(request.getSecurityCeilingEntityClassname());
return response;
}
use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class StructuredContentTypeCustomPersistenceHandler method inspect.
@Override
public DynamicResultSet inspect(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, InspectHelper helper) throws ServiceException {
String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
try {
String structuredContentTypeId = persistencePackage.getCustomCriteria()[3];
StructuredContentType structuredContentType = structuredContentService.findStructuredContentTypeById(Long.valueOf(structuredContentTypeId));
ClassMetadata metadata = new ClassMetadata();
metadata.setCeilingType(StructuredContentType.class.getName());
ClassTree entities = new ClassTree(StructuredContentTypeImpl.class.getName());
metadata.setPolymorphicEntities(entities);
Property[] properties = dynamicFieldUtil.buildDynamicPropertyList(structuredContentType.getStructuredContentFieldTemplate().getFieldGroups(), StructuredContentTypeImpl.class);
metadata.setProperties(properties);
DynamicResultSet results = new DynamicResultSet(metadata);
return results;
} catch (Exception e) {
throw new ServiceException("Unable to perform inspect for entity: " + ceilingEntityFullyQualifiedClassname, e);
}
}
use of org.broadleafcommerce.openadmin.dto.ClassMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method addSelectizeCollectionItem.
/**
* Adds the requested collection item via Selectize
*
* @param request
* @param response
* @param model
* @param pathVars
* @param id
* @param collectionField
* @param entityForm
* @return the return view path
* @throws Exception
*/
@RequestMapping(value = "/{id}/{collectionField:.*}/selectize-add", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> addSelectizeCollectionItem(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result) throws Exception {
Map<String, Object> returnVal = new HashMap<>();
String sectionKey = getSectionKey(pathVars);
String mainClassName = getClassNameForSection(sectionKey);
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
ClassMetadata mainMetadata = service.getClassMetadata(getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars)).getDynamicResultSet().getClassMetaData();
Property collectionProperty = mainMetadata.getPMap().get(collectionField);
if (StringUtils.isBlank(entityForm.getEntityType())) {
FieldMetadata fmd = collectionProperty.getMetadata();
if (fmd instanceof BasicCollectionMetadata) {
entityForm.setEntityType(((BasicCollectionMetadata) fmd).getCollectionCeilingEntity());
}
}
PersistencePackageRequest ppr = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
ppr.addCustomCriteria(buildSelectizeCustomCriteria());
declareShouldIgnoreAdditionStatusFilter();
Entity entity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
// First, we must save the collection entity
PersistenceResponse persistenceResponse = service.addSubCollectionEntity(entityForm, mainMetadata, collectionProperty, entity, sectionCrumbs);
Entity savedEntity = persistenceResponse.getEntity();
entityFormValidator.validate(entityForm, savedEntity, result);
if (result.hasErrors()) {
returnVal.put("error", result.getFieldError());
return returnVal;
}
if (savedEntity.findProperty(ALTERNATE_ID_PROPERTY) != null) {
returnVal.put("alternateId", savedEntity.findProperty(ALTERNATE_ID_PROPERTY).getValue());
}
return returnVal;
}
Aggregations