use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicOperationsController method getTypeaheadResults.
@RequestMapping(value = "/{owningClass:.*}/{collectionField:.*}/typeahead", method = RequestMethod.GET)
@ResponseBody
public List<Map<String, String>> getTypeaheadResults(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "owningClass") String owningClass, @PathVariable(value = "collectionField") String collectionField, @RequestParam(required = false) String query, @RequestParam(required = false) String requestingEntityId, @RequestParam MultiValueMap<String, String> requestParams) throws Exception {
List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, null, null);
String validatedClass = getClassNameForSection(owningClass);
PersistencePackageRequest ppr = getSectionPersistencePackageRequest(validatedClass, requestParams, sectionCrumbs, pathVars);
ClassMetadata mainMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
Property collectionProperty = mainMetadata.getPMap().get(collectionField);
FieldMetadata md = collectionProperty.getMetadata();
ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs);
ppr.addFilterAndSortCriteria(getCriteria(requestParams));
ppr.setStartIndex(getStartIndex(requestParams));
ppr.setMaxIndex(getMaxIndex(requestParams));
ppr.removeFilterAndSortCriteria("query");
ppr.removeFilterAndSortCriteria("requestingEntityId");
ppr.addCustomCriteria("requestingEntityId=" + requestingEntityId);
// This list of datums will populate the typeahead suggestions.
List<Map<String, String>> responses = new ArrayList<Map<String, String>>();
if (md instanceof BasicFieldMetadata) {
String searchField = searchFieldResolver.resolveField(((BasicFieldMetadata) md).getForeignKeyClass());
ppr.addFilterAndSortCriteria(new FilterAndSortCriteria(searchField, query));
DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
ClassMetadata lookupMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
for (Entity e : drs.getRecords()) {
Map<String, String> responseMap = new HashMap<String, String>();
String idProperty = service.getIdProperty(lookupMetadata);
responseMap.put("id", e.findProperty(idProperty).getValue());
String displayKey = e.findProperty(searchField).getDisplayValue();
if (StringUtils.isBlank(displayKey)) {
displayKey = e.findProperty(searchField).getValue();
}
responseMap.put("displayKey", displayKey);
responses.add(responseMap);
}
}
return responses;
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminAbstractController method getDynamicFieldTemplateForm.
/**
* Convenience method for obtaining a dynamic field template form for a particular entity. This method differs from
* {@link #getBlankDynamicFieldTemplateForm(DynamicEntityFormInfo)} in that it will fill out the current values for
* the fields in this dynamic form from the database. This method is invoked when the initial view of a page containing
* a dynamic form is triggered.
*
* Optionally, you can pass in a pre-existing dynamic form to this method that already has updated values. Example usage
* would be for after validation has failed and you do not want to lookup old values from the database again.
*
* @param info
* @param entityId
* @param dynamicFormOverride optional dynamic form that already has values to fill out
* @return the entity form
* @throws ServiceException
*/
protected EntityForm getDynamicFieldTemplateForm(DynamicEntityFormInfo info, String entityId, EntityForm dynamicFormOverride) throws ServiceException {
// We need to inspect with the second custom criteria set to the id of
// the desired structured content type
PersistencePackageRequest ppr = PersistencePackageRequest.standard().withCeilingEntityClassname(info.getCeilingClassName()).withSecurityCeilingEntityClassname(info.getSecurityCeilingClassName()).withCustomCriteria(new String[] { info.getCriteriaName(), entityId, info.getPropertyName(), info.getPropertyValue() });
ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
// However, when we fetch, the second custom criteria needs to be the id
// of this particular structured content entity
ppr.setCustomCriteria(new String[] { info.getCriteriaName(), entityId });
Entity entity = service.getRecord(ppr, info.getPropertyValue(), cmd, true).getDynamicResultSet().getRecords()[0];
List<Field> fieldsToMove = new ArrayList<>();
// override the results of the entity with the dynamic form passed in
if (dynamicFormOverride != null) {
dynamicFormOverride.clearFieldsMap();
Map<String, Field> fieldOverrides = dynamicFormOverride.getFields();
for (Entry<String, Field> override : fieldOverrides.entrySet()) {
if (entity.getPMap().containsKey(override.getKey())) {
entity.getPMap().get(override.getKey()).setValue(override.getValue().getValue());
} else {
fieldsToMove.add(override.getValue());
}
}
}
// Assemble the dynamic form for structured content type
EntityForm dynamicForm = formService.createEntityForm(cmd, entity, null, null);
for (Field field : fieldsToMove) {
FieldMetadata fmd = cmd.getPMap().get(field.getName()).getMetadata();
if (fmd instanceof BasicFieldMetadata) {
BasicFieldMetadata bfmd = (BasicFieldMetadata) fmd;
field.setFieldType(bfmd.getFieldType().toString());
field.setFriendlyName(bfmd.getFriendlyName());
field.setRequired(bfmd.getRequired());
}
dynamicForm.addField(cmd, field);
}
setSpecializedNameForFields(info, dynamicForm);
extensionManager.getProxy().modifyDynamicForm(dynamicForm, entityId);
return dynamicForm;
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class CategoryCustomPersistenceHandler method inspect.
@Override
public DynamicResultSet inspect(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, InspectHelper helper) throws ServiceException {
Map<String, FieldMetadata> md = getMetadata(persistencePackage, helper);
if (!isDefaultCategoryLegacyMode()) {
md.remove("allParentCategoryXrefs");
BasicFieldMetadata defaultCategory = ((BasicFieldMetadata) md.get(DEFAULT_PARENT_CATEGORY));
defaultCategory.setFriendlyName("CategoryImpl_ParentCategory");
}
return getResultSet(persistencePackage, helper, md);
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class CategoryCustomPersistenceHandler method add.
@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
entity = validateParentCategory(entity, true);
if (entity.isValidationFailure()) {
return entity;
} else {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Category adminInstance = (Category) Class.forName(entity.getType()[0]).newInstance();
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Category.class.getName(), persistencePerspective);
adminInstance = (Category) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
adminInstance = dynamicEntityDao.merge(adminInstance);
boolean handled = false;
if (extensionManager != null) {
ExtensionResultStatusType result = extensionManager.getProxy().manageParentCategoryForAdd(persistencePackage, adminInstance);
handled = ExtensionResultStatusType.NOT_HANDLED != result;
}
if (!handled) {
setupXref(adminInstance);
}
adminInstance = dynamicEntityDao.merge(adminInstance);
return helper.getRecord(adminProperties, adminInstance, null, null);
}
} catch (Exception e) {
throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
}
}
use of org.broadleafcommerce.openadmin.dto.FieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class CustomerCustomPersistenceHandler method add.
@Override
public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
Entity entity = persistencePackage.getEntity();
try {
PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
Customer adminInstance = (Customer) Class.forName(entity.getType()[0]).newInstance();
adminInstance.setId(customerService.findNextCustomerId());
Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Customer.class.getName(), persistencePerspective);
adminInstance = (Customer) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);
if (useEmailForLogin) {
adminInstance.setUsername(adminInstance.getEmailAddress());
}
if (!entity.isPreAdd()) {
Entity errorEntity = validateUniqueUsername(entity, adminInstance);
if (errorEntity != null) {
return errorEntity;
}
}
adminInstance = dynamicEntityDao.merge(adminInstance);
customerService.createRegisteredCustomerRoles(adminInstance);
Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null);
return adminEntity;
} catch (Exception e) {
LOG.error("Unable to execute persistence activity", e);
throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
}
}
Aggregations