use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class DynamicEntityDaoImpl method getPropertiesForEntityClass.
protected Map<String, FieldMetadata> getPropertiesForEntityClass(Class<?> targetClass, ForeignKey foreignField, String[] additionalNonPersistentProperties, ForeignKey[] additionalForeignFields, MergedPropertyType mergedPropertyType, Boolean populateManyToOneFields, String[] includeFields, String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname, List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded, String parentPrefix) {
Map<String, FieldMetadata> presentationAttributes = metadata.getFieldMetadataForTargetClass(null, targetClass, this, "");
if (isParentExcluded) {
for (String key : presentationAttributes.keySet()) {
LOG.debug("getPropertiesForEntityClass:Excluding " + key + " because parent is excluded.");
presentationAttributes.get(key).setExcluded(true);
}
}
Map idMetadata = getIdMetadata(targetClass);
Map<String, FieldMetadata> fields = new HashMap<>();
String idProperty = (String) idMetadata.get("name");
List<String> propertyNames = getPropertyNames(targetClass);
propertyNames.add(idProperty);
Type idType = (Type) idMetadata.get("type");
List<Type> propertyTypes = getPropertyTypes(targetClass);
propertyTypes.add(idType);
PersistentClass persistentClass = getPersistentClass(targetClass.getName());
Iterator testIter = persistentClass.getPropertyIterator();
List<Property> propertyList = new ArrayList<>();
// check the properties for problems
while (testIter.hasNext()) {
Property property = (Property) testIter.next();
if (property.getName().contains(".")) {
throw new IllegalArgumentException("Properties from entities that utilize a period character ('.') in their name are incompatible with this system. The property name in question is: (" + property.getName() + ") from the class: (" + targetClass.getName() + ")");
}
propertyList.add(property);
}
buildProperties(targetClass, foreignField, additionalForeignFields, additionalNonPersistentProperties, mergedPropertyType, presentationAttributes, propertyList, fields, propertyNames, propertyTypes, idProperty, populateManyToOneFields, includeFields, excludeFields, configurationKey, ceilingEntityFullyQualifiedClassname, parentClasses, prefix, isParentExcluded, false, parentPrefix);
BasicFieldMetadata presentationAttribute = new BasicFieldMetadata();
presentationAttribute.setExplicitFieldType(SupportedFieldType.STRING);
presentationAttribute.setVisibility(VisibilityEnum.HIDDEN_ALL);
if (!ArrayUtils.isEmpty(additionalNonPersistentProperties)) {
Class<?>[] entities = getAllPolymorphicEntitiesFromCeiling(targetClass);
for (String additionalNonPersistentProperty : additionalNonPersistentProperties) {
if (StringUtils.isEmpty(prefix) || (!StringUtils.isEmpty(prefix) && additionalNonPersistentProperty.startsWith(prefix))) {
String myAdditionalNonPersistentProperty = additionalNonPersistentProperty;
// get final property if this is a dot delimited property
int finalDotPos = additionalNonPersistentProperty.lastIndexOf('.');
if (finalDotPos >= 0) {
myAdditionalNonPersistentProperty = myAdditionalNonPersistentProperty.substring(finalDotPos + 1, myAdditionalNonPersistentProperty.length());
}
// check all the polymorphic types on this target class to see if the end property exists
Field testField = null;
Method testMethod = null;
for (Class<?> clazz : entities) {
try {
testMethod = clazz.getMethod(myAdditionalNonPersistentProperty);
if (testMethod != null) {
break;
}
} catch (NoSuchMethodException e) {
// do nothing - method does not exist
}
testField = getFieldManager().getField(clazz, myAdditionalNonPersistentProperty);
if (testField != null) {
break;
}
}
// if the property exists, add it to the metadata for this class
if (testField != null || testMethod != null) {
fields.put(additionalNonPersistentProperty, metadata.getFieldMetadata(prefix, additionalNonPersistentProperty, propertyList, SupportedFieldType.STRING, null, targetClass, presentationAttribute, mergedPropertyType, this));
}
}
}
}
return fields;
}
use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class DynamicFieldPersistenceHandlerHelper method buildDynamicPropertyList.
/**
* Builds all of the metadata for all of the dynamic properties within a {@link StructuredContentType}, gleaned from
* the {@link FieldGroup}s and {@link FieldDefinition}s.
*
* @param fieldGroups groups that the {@link Property}s are built from
* @param inheritedType the value that each built {@link FieldMetadata} for each property will use to notate where the
* dynamic field actually came from (meaning {@link FieldMetadata#setAvailableToTypes(String[])} and {@link FieldMetadata#setInheritedFromType(String)}
* @return
*/
public Property[] buildDynamicPropertyList(List<FieldGroup> fieldGroups, Class<?> inheritedType) {
List<Property> propertiesList = new ArrayList<Property>();
int groupCount = 1;
int fieldCount = 0;
for (FieldGroup group : fieldGroups) {
List<FieldDefinition> definitions = group.getFieldDefinitions();
for (FieldDefinition def : definitions) {
Property property = buildDynamicProperty(def, inheritedType);
BasicFieldMetadata fieldMetadata = (BasicFieldMetadata) property.getMetadata();
fieldMetadata.setOrder(fieldCount++);
fieldMetadata.setGroup(group.getName());
fieldMetadata.setGroupOrder(groupCount);
fieldMetadata.setGroupCollapsed(group.getInitCollapsedFlag());
propertiesList.add(property);
}
groupCount++;
fieldCount = 0;
}
Property property = new Property();
property.setName("id");
BasicFieldMetadata fieldMetadata = new BasicFieldMetadata();
property.setMetadata(fieldMetadata);
fieldMetadata.setFieldType(SupportedFieldType.ID);
fieldMetadata.setSecondaryType(SupportedFieldType.INTEGER);
fieldMetadata.setMutable(true);
fieldMetadata.setInheritedFromType(inheritedType.getName());
fieldMetadata.setAvailableToTypes(new String[] { inheritedType.getName() });
fieldMetadata.setForeignKeyCollection(false);
fieldMetadata.setMergedPropertyType(MergedPropertyType.PRIMARY);
fieldMetadata.setName("id");
fieldMetadata.setFriendlyName("ID");
fieldMetadata.setSecurityLevel("");
fieldMetadata.setVisibility(VisibilityEnum.HIDDEN_ALL);
fieldMetadata.setExplicitFieldType(SupportedFieldType.UNKNOWN);
fieldMetadata.setLargeEntry(false);
fieldMetadata.setProminent(false);
fieldMetadata.setColumnWidth("*");
fieldMetadata.setBroadleafEnumeration("");
fieldMetadata.setReadOnly(true);
propertiesList.add(property);
Property[] properties = new Property[propertiesList.size()];
properties = propertiesList.toArray(properties);
Arrays.sort(properties, new Comparator<Property>() {
@Override
public int compare(Property o1, Property o2) {
/*
* First, compare properties based on order fields
*/
if (o1.getMetadata().getOrder() != null && o2.getMetadata().getOrder() != null) {
return o1.getMetadata().getOrder().compareTo(o2.getMetadata().getOrder());
} else if (o1.getMetadata().getOrder() != null && o2.getMetadata().getOrder() == null) {
/*
* Always favor fields that have an order identified
*/
return -1;
} else if (o1.getMetadata().getOrder() == null && o2.getMetadata().getOrder() != null) {
/*
* Always favor fields that have an order identified
*/
return 1;
} else if (o1.getMetadata().getFriendlyName() != null && o2.getMetadata().getFriendlyName() != null) {
return o1.getMetadata().getFriendlyName().compareTo(o2.getMetadata().getFriendlyName());
} else {
return o1.getName().compareTo(o2.getName());
}
}
});
return properties;
}
use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminBasicEntityController method viewCollectionItemDetails.
/**
* Shows the modal popup for the current selected "to-one" field. For instance, if you are viewing a list of products
* then this method is invoked when a user clicks on the name of the default category field.
*
* @param request
* @param response
* @param model
* @param pathVars
* @param collectionField
* @param id
* @return
* @throws Exception
*/
@RequestMapping(value = "/{collectionField:.*}/{id}/view", method = RequestMethod.GET)
public String viewCollectionItemDetails(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "collectionField") String collectionField, @PathVariable(value = "id") String id) 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);
BasicFieldMetadata md = (BasicFieldMetadata) collectionProperty.getMetadata();
AdminSection section = adminNavigationService.findAdminSectionByClassAndSectionId(md.getForeignKeyClass(), sectionKey);
String sectionUrlKey = (section.getUrl().startsWith("/")) ? section.getUrl().substring(1) : section.getUrl();
Map<String, String> varsForField = new HashMap<>();
varsForField.put("sectionKey", sectionUrlKey);
return viewEntityForm(request, response, model, varsForField, id);
}
use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class RuleFieldPersistenceProvider method filterProperties.
@Override
public MetadataProviderResponse filterProperties(AddFilterPropertiesRequest addFilterPropertiesRequest, Map<String, FieldMetadata> properties) {
// This may contain rule Json fields - convert and filter out
List<Property> propertyList = new ArrayList<Property>();
propertyList.addAll(Arrays.asList(addFilterPropertiesRequest.getEntity().getProperties()));
Iterator<Property> itr = propertyList.iterator();
List<Property> additionalProperties = new ArrayList<Property>();
while (itr.hasNext()) {
Property prop = itr.next();
if (prop.getName().endsWith("Json")) {
for (Map.Entry<String, FieldMetadata> entry : properties.entrySet()) {
String propName = prop.getName().substring(0, prop.getName().length() - 4);
if (propName.equals(entry.getKey())) {
BasicFieldMetadata originalFM = (BasicFieldMetadata) entry.getValue();
if (originalFM.getFieldType() == SupportedFieldType.RULE_SIMPLE || originalFM.getFieldType() == SupportedFieldType.RULE_SIMPLE_TIME || originalFM.getFieldType() == SupportedFieldType.RULE_WITH_QUANTITY) {
Property originalProp = addFilterPropertiesRequest.getEntity().findProperty(entry.getKey());
if (originalProp == null) {
originalProp = new Property();
originalProp.setName(entry.getKey());
additionalProperties.add(originalProp);
}
originalProp.setValue(prop.getValue());
originalProp.setRawValue(prop.getRawValue());
originalProp.setUnHtmlEncodedValue(prop.getUnHtmlEncodedValue());
itr.remove();
break;
}
}
}
}
}
propertyList.addAll(additionalProperties);
addFilterPropertiesRequest.getEntity().setProperties(propertyList.toArray(new Property[propertyList.size()]));
return MetadataProviderResponse.HANDLED;
}
use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class BasicFieldPersistenceProvider method extractValue.
@Override
public MetadataProviderResponse extractValue(ExtractValueRequest extractValueRequest, Property property) throws PersistenceException {
if (!canHandleExtraction(extractValueRequest, property)) {
return MetadataProviderResponse.NOT_HANDLED;
}
try {
if (extractValueRequest.getRequestedValue() != null) {
String val = null;
if (extractValueRequest.getMetadata().getForeignKeyCollection()) {
((BasicFieldMetadata) property.getMetadata()).setFieldType(extractValueRequest.getMetadata().getFieldType());
} else if (extractValueRequest.getMetadata().getFieldType().equals(SupportedFieldType.BOOLEAN) && extractValueRequest.getRequestedValue() instanceof Character) {
val = (extractValueRequest.getRequestedValue().equals('Y')) ? "true" : "false";
} else if (Date.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format((Date) extractValueRequest.getRequestedValue());
} else if (Timestamp.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format(new Date(((Timestamp) extractValueRequest.getRequestedValue()).getTime()));
} else if (Calendar.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
val = extractValueRequest.getDataFormatProvider().getSimpleDateFormatter().format(((Calendar) extractValueRequest.getRequestedValue()).getTime());
} else if (Double.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
val = extractValueRequest.getDataFormatProvider().getDecimalFormatter().format(extractValueRequest.getRequestedValue());
} else if (BigDecimal.class.isAssignableFrom(extractValueRequest.getRequestedValue().getClass())) {
BigDecimal decimal = (BigDecimal) extractValueRequest.getRequestedValue();
DecimalFormat format = extractValueRequest.getDataFormatProvider().getDecimalFormatter();
// track all the decimal places in the scale of the BigDecimal - even if they're all zeros
StringBuilder sb = new StringBuilder();
sb.append("0");
if (decimal.scale() > 0) {
sb.append(".");
for (int j = 0; j < decimal.scale(); j++) {
sb.append("0");
}
}
format.applyPattern(sb.toString());
val = format.format(extractValueRequest.getRequestedValue());
} else if (extractValueRequest.getMetadata().getForeignKeyClass() != null) {
try {
val = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), extractValueRequest.getMetadata().getForeignKeyProperty()).toString();
if (extensionManager != null) {
ExtensionResultHolder<Serializable> resultHolder = new ExtensionResultHolder<Serializable>();
ExtensionResultStatusType result = extensionManager.getProxy().transformForeignKey(extractValueRequest, property, resultHolder);
if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
val = String.valueOf(resultHolder.getResult());
}
}
// see if there's a name property and use it for the display value
String entityName = null;
if (extractValueRequest.getRequestedValue() instanceof AdminMainEntity) {
entityName = ((AdminMainEntity) extractValueRequest.getRequestedValue()).getMainEntityName();
}
Object temp = null;
if (!StringUtils.isEmpty(extractValueRequest.getMetadata().getForeignKeyDisplayValueProperty())) {
String nameProperty = extractValueRequest.getMetadata().getForeignKeyDisplayValueProperty();
try {
temp = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), nameProperty);
} catch (FieldNotAvailableException e) {
// do nothing
}
}
if (temp == null && StringUtils.isEmpty(entityName)) {
try {
temp = extractValueRequest.getFieldManager().getFieldValue(extractValueRequest.getRequestedValue(), "name");
} catch (FieldNotAvailableException e) {
// do nothing
}
}
if (temp != null) {
extractValueRequest.setDisplayVal(temp.toString());
} else if (!StringUtils.isEmpty(entityName)) {
extractValueRequest.setDisplayVal(entityName);
}
} catch (FieldNotAvailableException e) {
throw new IllegalArgumentException(e);
}
} else if (SupportedFieldType.ID == extractValueRequest.getMetadata().getFieldType()) {
val = extractValueRequest.getRequestedValue().toString();
if (extensionManager != null) {
ExtensionResultHolder<Serializable> resultHolder = new ExtensionResultHolder<Serializable>();
ExtensionResultStatusType result = extensionManager.getProxy().transformId(extractValueRequest, property, resultHolder);
if (ExtensionResultStatusType.NOT_HANDLED != result && resultHolder.getResult() != null) {
val = String.valueOf(resultHolder.getResult());
}
}
} else {
val = extractValueRequest.getRequestedValue().toString();
}
property.setValue(val);
property.setDisplayValue(extractValueRequest.getDisplayVal());
}
} catch (IllegalAccessException e) {
throw new PersistenceException(e);
}
return MetadataProviderResponse.HANDLED;
}
Aggregations