use of org.broadleafcommerce.openadmin.web.form.entity.ComboField 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.web.form.entity.ComboField 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;
}
use of org.broadleafcommerce.openadmin.web.form.entity.ComboField in project BroadleafCommerce by BroadleafCommerce.
the class FormBuilderServiceImpl method createListGrid.
/**
* Populate a ListGrid with ListGridRecords.
*
* @param className
* @param headerFields
* @param type
* @param drs
* @param sectionKey
* @param order
* @param idProperty
* @param sectionCrumbs
* @param sortPropery
* @return
*/
protected ListGrid createListGrid(String className, List<Field> headerFields, ListGrid.Type type, DynamicResultSet drs, String sectionKey, Integer order, String idProperty, List<SectionCrumb> sectionCrumbs, String sortPropery) {
// Create the list grid and set some basic attributes
ListGrid listGrid = new ListGrid();
listGrid.setClassName(className);
listGrid.getHeaderFields().addAll(headerFields);
listGrid.setListGridType(type);
listGrid.setSectionCrumbs(sectionCrumbs);
listGrid.setSectionKey(sectionKey);
listGrid.setOrder(order);
listGrid.setIdProperty(idProperty);
listGrid.setStartIndex(drs.getStartIndex());
listGrid.setTotalRecords(drs.getTotalRecords());
listGrid.setPageSize(drs.getPageSize());
String sectionIdentifier = extractSectionIdentifierFromCrumb(sectionCrumbs);
AdminSection section = navigationService.findAdminSectionByClassAndSectionId(className, sectionIdentifier);
if (section != null) {
listGrid.setExternalEntitySectionKey(section.getUrl());
}
// format date list grid cells
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, Y @ hh:mma");
DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
symbols.setAmPmStrings(new String[] { "am", "pm" });
formatter.setDateFormatSymbols(symbols);
// that are used for the header fields.
for (Entity e : drs.getRecords()) {
ListGridRecord record = new ListGridRecord();
record.setListGrid(listGrid);
record.setDirty(e.isDirty());
record.setEntity(e);
if (StringUtils.isNotBlank(sortPropery) && e.findProperty(sortPropery) != null) {
Property sort = e.findProperty(sortPropery);
record.setDisplayOrder(sort.getValue());
}
if (e.findProperty("hasError") != null) {
Boolean hasError = Boolean.parseBoolean(e.findProperty("hasError").getValue());
record.setIsError(hasError);
if (hasError) {
ExtensionResultStatusType messageResultStatus = listGridErrorExtensionManager.getProxy().determineErrorMessageForEntity(e, record);
if (ExtensionResultStatusType.NOT_HANDLED.equals(messageResultStatus)) {
record.setErrorKey("listgrid.record.error");
}
}
}
if (e.findProperty("progressStatus") != null) {
ExtensionResultStatusType messageResultStatus = listGridErrorExtensionManager.getProxy().determineStatusMessageForEntity(e, record);
if (ExtensionResultStatusType.NOT_HANDLED.equals(messageResultStatus)) {
record.setStatus(e.findProperty("progressStatus").getValue());
record.setStatusCssClass("listgrid.record.status");
}
}
if (e.findProperty(idProperty) != null) {
record.setId(e.findProperty(idProperty).getValue());
}
for (Field headerField : headerFields) {
Property p = e.findProperty(headerField.getName());
if (p != null) {
Field recordField = new Field().withName(headerField.getName()).withFriendlyName(headerField.getFriendlyName()).withOrder(p.getMetadata().getOrder());
if (headerField instanceof ComboField) {
recordField.setValue(((ComboField) headerField).getOption(p.getValue()));
recordField.setDisplayValue(p.getDisplayValue());
} else {
if (headerField.getFieldType().equals("DATE")) {
try {
Date date = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(p.getValue());
String newValue = formatter.format(date);
recordField.setValue(newValue);
} catch (Exception ex) {
recordField.setValue(p.getValue());
}
} else {
recordField.setValue(p.getValue());
}
recordField.setDisplayValue(p.getDisplayValue());
}
recordField.setDerived(isDerivedField(headerField, recordField, p));
record.getFields().add(recordField);
}
}
if (e.findProperty(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY) != null) {
Field hiddenField = new Field().withName(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY);
hiddenField.setValue(e.findProperty(AdminMainEntity.MAIN_ENTITY_NAME_PROPERTY).getValue());
record.getHiddenFields().add(hiddenField);
}
if (e.findProperty(BasicPersistenceModule.ALTERNATE_ID_PROPERTY) != null) {
record.setAltId(e.findProperty(BasicPersistenceModule.ALTERNATE_ID_PROPERTY).getValue());
}
extensionManager.getProxy().modifyListGridRecord(className, record, e);
listGrid.getRecords().add(record);
}
if (drs.getFirstId() != null) {
listGrid.setFirstId(drs.getFirstId());
}
if (drs.getLastId() != null) {
listGrid.setLastId(drs.getLastId());
}
if (drs.getUpperCount() != null) {
listGrid.setUpperCount(drs.getUpperCount());
}
if (drs.getLowerCount() != null) {
listGrid.setLowerCount(drs.getLowerCount());
}
if (drs.getFetchType() != null) {
listGrid.setFetchType(drs.getFetchType().toString());
}
if (drs.getTotalCountLessThanPageSize() != null) {
listGrid.setTotalCountLessThanPageSize(drs.getTotalCountLessThanPageSize());
}
if (drs.getPromptSearch() != null) {
listGrid.setPromptSearch(drs.getPromptSearch());
}
return listGrid;
}
use of org.broadleafcommerce.openadmin.web.form.entity.ComboField in project BroadleafCommerce by BroadleafCommerce.
the class TranslationFormBuilderServiceImpl method getLocaleField.
protected ComboField getLocaleField(String value) {
ComboField f = new ComboField();
f.setName("localeCode");
f.setFriendlyName("Translation_localeCode");
f.setFieldType(SupportedFieldType.EXPLICIT_ENUMERATION.toString());
f.setOrder(0);
if (StringUtils.isNotBlank(value)) {
f.setValue(value);
}
List<Locale> locales = localeService.findAllLocales();
Map<String, String> localeMap = new HashMap<String, String>();
for (Locale l : locales) {
localeMap.put(l.getLocaleCode(), l.getFriendlyName());
}
f.setOptions(localeMap);
return f;
}
use of org.broadleafcommerce.openadmin.web.form.entity.ComboField in project BroadleafCommerce by BroadleafCommerce.
the class FormBuilderServiceImpl method createHeaderField.
protected Field createHeaderField(Property p, BasicFieldMetadata fmd) {
Field hf;
if (fmd.getFieldType().equals(SupportedFieldType.EXPLICIT_ENUMERATION) || fmd.getFieldType().equals(SupportedFieldType.BROADLEAF_ENUMERATION) || fmd.getFieldType().equals(SupportedFieldType.DATA_DRIVEN_ENUMERATION) || fmd.getFieldType().equals(SupportedFieldType.EMPTY_ENUMERATION)) {
hf = new ComboField();
((ComboField) hf).setOptions(fmd.getEnumerationValues());
} else {
hf = new Field();
}
hf.withName(p.getName()).withFriendlyName(StringUtils.isNotEmpty(fmd.getFriendlyName()) ? fmd.getFriendlyName() : p.getName()).withOrder(fmd.getGridOrder()).withColumnWidth(fmd.getColumnWidth()).withForeignKeyDisplayValueProperty(fmd.getForeignKeyDisplayValueProperty()).withForeignKeyClass(fmd.getForeignKeyClass()).withForeignKeySectionPath(getAdminSectionPath(fmd.getForeignKeyClass())).withOwningEntityClass(fmd.getOwningClass() != null ? fmd.getOwningClass() : fmd.getTargetClass()).withCanLinkToExternalEntity(fmd.getCanLinkToExternalEntity());
String fieldType = fmd.getFieldType() == null ? null : fmd.getFieldType().toString();
hf.setFieldType(fieldType);
return hf;
}
Aggregations