use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method removeCollectionItem.
/**
* Removes the requested collection item
*
* Note that the request must contain a parameter called "key" when attempting to remove a collection item from a
* map collection.
*
* @param request
* @param response
* @param model
* @param pathVars
* @param id
* @param collectionField
* @param collectionItemId
* @return the return view path
* @throws Exception
*/
@RequestMapping(value = "/{id}/{collectionField:.*}/{collectionItemId}/{alternateId}/delete", method = RequestMethod.POST)
public String removeCollectionItem(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @PathVariable(value = "collectionField") String collectionField, @PathVariable(value = "collectionItemId") String collectionItemId, @PathVariable(value = "alternateId") String alternateId) throws Exception {
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);
String priorKey = request.getParameter("key");
PersistencePackageRequest ppr = getSectionPersistencePackageRequest(mainClassName, sectionCrumbs, pathVars);
declareShouldIgnoreAdditionStatusFilter();
Entity entity = service.getRecord(ppr, id, mainMetadata, false).getDynamicResultSet().getRecords()[0];
// First, we must remove the collection entity
PersistenceResponse persistenceResponse = service.removeSubCollectionEntity(mainMetadata, collectionProperty, entity, collectionItemId, alternateId, priorKey, sectionCrumbs);
if (persistenceResponse.getEntity() != null && persistenceResponse.getEntity().isValidationFailure()) {
String error = "There was an error removing the whatever";
if (MapUtils.isNotEmpty(persistenceResponse.getEntity().getPropertyValidationErrors())) {
// If we failed, we'll return some JSON with the first error
error = persistenceResponse.getEntity().getPropertyValidationErrors().values().iterator().next().get(0);
} else if (CollectionUtils.isNotEmpty(persistenceResponse.getEntity().getGlobalValidationErrors())) {
error = persistenceResponse.getEntity().getGlobalValidationErrors().get(0);
}
return new JsonResponse(response).with("status", "error").with("message", BLCMessageUtils.getMessage(error)).done();
}
// Next, we must get the new list grid that represents this collection
// We return the new list grid so that it can replace the currently visible one
ListGrid listGrid = getCollectionListGrid(mainMetadata, entity, collectionProperty, null, sectionKey, persistenceResponse, sectionCrumbs);
model.addAttribute("listGrid", listGrid);
model.addAttribute("currentUrl", request.getRequestURL().toString());
setModelAttributes(model, sectionKey);
return "views/standaloneListGrid";
}
use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicOperationsController method showSelectCollectionItem.
/**
* Shows the modal dialog that is used to select a "to-one" collection item. For example, this could be used to show
* a list of categories for the ManyToOne field "defaultCategory" in Product.
*
* @param request
* @param response
* @param model
* @param pathVars
* @param owningClass
* @param collectionField
* @return the return view path
* @throws Exception
*/
@RequestMapping(value = "/{owningClass:.*}/{collectionField:.*}/select", method = RequestMethod.GET)
public String showSelectCollectionItem(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 requestingEntityId, @RequestParam(defaultValue = "false") boolean dynamicField, @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);
// We might need these fields in the initial inspect.
ppr.addCustomCriteria("requestingEntityId=" + requestingEntityId);
ppr.addCustomCriteria("owningClass=" + owningClass);
ppr.addCustomCriteria("requestingField=" + collectionField);
ClassMetadata mainMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
// Only get collection property metadata when there is a non-structured content field that I am looking for
Property collectionProperty = null;
FieldMetadata md = null;
if (!collectionField.contains("|") && !dynamicField) {
collectionProperty = mainMetadata.getPMap().get(collectionField);
md = collectionProperty.getMetadata();
ppr = PersistencePackageRequest.fromMetadata(md, sectionCrumbs);
} else {
md = new BasicFieldMetadata();
md.setFriendlyName(mainMetadata.getPolymorphicEntities().getFriendlyName());
collectionProperty = new Property();
collectionProperty.setMetadata(md);
}
ppr.addFilterAndSortCriteria(getCriteria(requestParams));
ppr.setStartIndex(getStartIndex(requestParams));
ppr.setMaxIndex(getMaxIndex(requestParams));
ppr.removeFilterAndSortCriteria("requestingEntityId");
ppr.addCustomCriteria("requestingEntityId=" + requestingEntityId);
ppr.addCustomCriteria("owningClass=" + owningClass);
ppr.addCustomCriteria("requestingField=" + collectionField);
modifyFetchPersistencePackageRequest(ppr, pathVars);
ClassMetadata targetClassMetadata = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
ExtensionResultStatusType extensionResultStatusType = extensionManager.getProxy().buildLookupListGrid(ppr, targetClassMetadata, ppr.getCeilingEntityClassname(), sectionCrumbs, model, requestParams);
if (extensionResultStatusType.equals(ExtensionResultStatusType.NOT_HANDLED)) {
DynamicResultSet drs = service.getRecords(ppr).getDynamicResultSet();
ListGrid listGrid = null;
if (collectionField.contains("|") || dynamicField) {
// If we're dealing with a lookup from a dynamic field, we need to build the list grid differently
listGrid = formService.buildMainListGrid(drs, mainMetadata, "/" + owningClass, sectionCrumbs);
listGrid.setListGridType(ListGrid.Type.TO_ONE);
listGrid.setSubCollectionFieldName(collectionField);
listGrid.setPathOverride("/" + owningClass + "/" + collectionField + "/select");
} else if (md instanceof BasicFieldMetadata) {
listGrid = formService.buildCollectionListGrid(null, drs, collectionProperty, owningClass, sectionCrumbs);
listGrid.removeAllRowActions();
}
model.addAttribute("listGrid", listGrid);
}
model.addAttribute("viewType", "modal/simpleSelectEntity");
model.addAttribute("currentUrl", request.getRequestURL().toString());
model.addAttribute("modalHeaderType", ModalHeaderType.SELECT_COLLECTION_ITEM.getType());
model.addAttribute("collectionProperty", collectionProperty);
model.addAttribute("sectionCrumbs", request.getParameter("sectionCrumbs"));
setModelAttributes(model, owningClass);
return "modules/modalContainer";
}
use of org.broadleafcommerce.openadmin.dto.Property 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.Property in project BroadleafCommerce by BroadleafCommerce.
the class FormBuilderServiceImpl method populateDropdownToOneFields.
protected void populateDropdownToOneFields(EntityForm ef, ClassMetadata cmd) throws ServiceException {
for (Property p : cmd.getProperties()) {
if (p.getMetadata() instanceof BasicFieldMetadata) {
BasicFieldMetadata fmd = (BasicFieldMetadata) p.getMetadata();
if (LookupType.DROPDOWN.equals(fmd.getLookupType()) && !ArrayUtils.contains(getFormHiddenVisibilities(), fmd.getVisibility())) {
// Get the records
PersistencePackageRequest toOnePpr = PersistencePackageRequest.standard().withCeilingEntityClassname(fmd.getForeignKeyClass());
Entity[] rows = adminEntityService.getRecords(toOnePpr).getDynamicResultSet().getRecords();
// Determine the id field
String idProp = null;
ClassMetadata foreignClassMd = adminEntityService.getClassMetadata(toOnePpr).getDynamicResultSet().getClassMetaData();
for (Property foreignP : foreignClassMd.getProperties()) {
if (foreignP.getMetadata() instanceof BasicFieldMetadata) {
BasicFieldMetadata foreignFmd = (BasicFieldMetadata) foreignP.getMetadata();
if (SupportedFieldType.ID.equals(foreignFmd.getFieldType())) {
idProp = foreignP.getName();
}
}
}
if (idProp == null) {
throw new RuntimeException("Could not determine ID property for " + fmd.getForeignKeyClass());
}
// Determine the display field
String displayProp = fmd.getLookupDisplayProperty();
// Build the options map
Map<String, String> options = new HashMap<>();
for (Entity row : rows) {
Property prop = row.findProperty(displayProp);
if (prop == null) {
LOG.warn("Could not find displayProp [" + StringUtil.sanitize(displayProp) + "] on entity [" + ef.getCeilingEntityClassname() + "]");
} else {
String displayValue = prop.getDisplayValue();
if (StringUtils.isBlank(displayValue)) {
displayValue = prop.getValue();
}
options.put(row.findProperty(idProp).getValue(), displayValue);
}
}
// Set the options on the entity field
ComboField cf = (ComboField) ef.findField(p.getName());
cf.setOptions(options);
}
}
}
}
use of org.broadleafcommerce.openadmin.dto.Property in project BroadleafCommerce by BroadleafCommerce.
the class FormBuilderServiceImpl method buildMapForm.
@Override
public EntityForm buildMapForm(MapMetadata mapMd, final MapStructure mapStructure, ClassMetadata cmd, String parentId, EntityForm ef) throws ServiceException {
ForeignKey foreignKey = (ForeignKey) mapMd.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
ef.setEntityType(foreignKey.getForeignKeyClass());
Field keyField;
if (!mapMd.getForceFreeFormKeys()) {
// We will use a combo field to render the key choices
ComboField temp = new ComboField();
temp.withName("key").withFieldType("combo_field").withFriendlyName(mapStructure.getKeyPropertyFriendlyName());
if (mapMd.getKeys() != null) {
// The keys can be explicitly set in the annotation...
temp.setOptions(mapMd.getKeys());
} else {
// Or they could be based on a different entity
PersistencePackageRequest ppr = PersistencePackageRequest.standard().withCeilingEntityClassname(mapMd.getMapKeyOptionEntityClass());
DynamicResultSet drs = adminEntityService.getRecords(ppr).getDynamicResultSet();
for (Entity entity : drs.getRecords()) {
String keyValue = entity.getPMap().get(mapMd.getMapKeyOptionEntityValueField()).getValue();
String keyDisplayValue = entity.getPMap().get(mapMd.getMapKeyOptionEntityDisplayField()).getValue();
temp.putOption(keyValue, keyDisplayValue);
}
}
keyField = temp;
} else {
keyField = new Field().withName("key").withFieldType(SupportedFieldType.STRING.toString()).withFriendlyName(mapStructure.getKeyPropertyFriendlyName());
}
keyField.setRequired(true);
ef.addMapKeyField(cmd, keyField);
// Set the fields for this form
List<Property> mapFormProperties;
if (mapMd.isSimpleValue()) {
ef.setIdProperty("key");
mapFormProperties = new ArrayList<>();
Property valueProp = cmd.getPMap().get("value");
mapFormProperties.add(valueProp);
} else {
String valueClassName = mapStructure.getValueClassName();
List<String> classNames = getValueClassNames(valueClassName);
mapFormProperties = new ArrayList<>(Arrays.asList(cmd.getProperties()));
filterMapFormProperties(mapFormProperties, classNames);
}
setEntityFormFields(cmd, ef, mapFormProperties);
Field f = new Field().withName("priorKey").withFieldType(SupportedFieldType.HIDDEN.toString());
ef.addHiddenField(cmd, f);
ef.setParentId(parentId);
return ef;
}
Aggregations