use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdvancedCollectionFieldMetadataProvider method addMetadataFromFieldType.
@Override
public MetadataProviderResponse addMetadataFromFieldType(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
if (!canHandleFieldForTypeMetadata(addMetadataFromFieldTypeRequest, metadata)) {
return MetadataProviderResponse.NOT_HANDLED;
}
CollectionMetadata fieldMetadata = (CollectionMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute();
if (StringUtils.isEmpty(fieldMetadata.getCollectionCeilingEntity())) {
ParameterizedType listType = (ParameterizedType) addMetadataFromFieldTypeRequest.getRequestedField().getGenericType();
Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];
fieldMetadata.setCollectionCeilingEntity(listClass.getName());
}
if (addMetadataFromFieldTypeRequest.getTargetClass() != null) {
if (StringUtils.isEmpty(fieldMetadata.getInheritedFromType())) {
fieldMetadata.setInheritedFromType(addMetadataFromFieldTypeRequest.getTargetClass().getName());
}
if (ArrayUtils.isEmpty(fieldMetadata.getAvailableToTypes())) {
fieldMetadata.setAvailableToTypes(new String[] { addMetadataFromFieldTypeRequest.getTargetClass().getName() });
}
}
// {@link BasicPersistenceModule#filterOutCollectionMetadata}
if (addMetadataFromFieldTypeRequest.getForeignField() != null && addMetadataFromFieldTypeRequest.isPropertyForeignKey()) {
FieldInfo info = buildFieldInfo(addMetadataFromFieldTypeRequest.getRequestedField());
BasicFieldMetadata basicMetadata = new BasicFieldMetadata();
basicMetadata.setName(info.getName());
basicMetadata.setExcluded(false);
// Don't show this anywhere on the form and ensure it's explicitly not required
basicMetadata.setVisibility(VisibilityEnum.HIDDEN_ALL);
basicMetadata.setRequired(false);
setClassOwnership(addMetadataFromFieldTypeRequest.getReturnedClass(), addMetadataFromFieldTypeRequest.getTargetClass(), metadata, info);
Map<String, FieldMetadata> fakedMd = new HashMap<String, FieldMetadata>();
fakedMd.put(addMetadataFromFieldTypeRequest.getRequestedField().getName(), basicMetadata);
// Fake out a request and some metadata to pass along as additional metadata within this metadata
AddMetadataFromFieldTypeRequest fakedRequest = new AddMetadataFromFieldTypeRequest(addMetadataFromFieldTypeRequest.getRequestedField(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getForeignField(), addMetadataFromFieldTypeRequest.getAdditionalForeignFields(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getComponentProperties(), addMetadataFromFieldTypeRequest.getIdProperty(), addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.isPropertyForeignKey(), addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition(), fakedMd, basicMetadata, addMetadataFromFieldTypeRequest.getExplicitType(), addMetadataFromFieldTypeRequest.getReturnedClass(), addMetadataFromFieldTypeRequest.getDynamicEntityDao());
defaultMetadataProvider.addMetadataFromFieldType(fakedRequest, fakedMd);
fieldMetadata.getAdditionalMetadata().put(FOREIGN_KEY_ADDITIONAL_METADATA_KEY, basicMetadata);
}
metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), fieldMetadata);
return MetadataProviderResponse.HANDLED;
}
use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class AdminEntityServiceImpl method getContextSpecificRelationshipId.
@Override
public String getContextSpecificRelationshipId(ClassMetadata cmd, Entity entity, String propertyName) {
String prefix;
if (propertyName.contains(".")) {
prefix = propertyName.substring(0, propertyName.lastIndexOf("."));
} else {
prefix = "";
}
if (prefix.equals("")) {
return entity.findProperty("id").getValue();
} else {
// we need to check all the parts of the prefix. For example, the prefix could include an @Embedded class like
// defaultSku.dimension. In this case, we want the id from the defaultSku property, since the @Embedded does
// not have an id property - nor should it.
String[] prefixParts = prefix.split("\\.");
for (int j = 0; j < prefixParts.length; j++) {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < prefixParts.length - j; x++) {
sb.append(prefixParts[x]);
if (x < prefixParts.length - j - 1) {
sb.append(".");
}
}
String tempPrefix = sb.toString();
for (Property property : entity.getProperties()) {
if (property.getName().startsWith(tempPrefix)) {
// in the case of the prefix defaultSku, we want defaultSku.id not defaultSku.skuAttributes.id
if (StringUtils.countMatches(property.getName().replace(tempPrefix, ""), ".") == 1) {
if (cmd.getPMap().containsKey(property.getName())) {
BasicFieldMetadata md = (BasicFieldMetadata) cmd.getPMap().get(property.getName()).getMetadata();
if (md.getFieldType().equals(SupportedFieldType.ID)) {
return property.getValue();
}
}
}
}
}
}
}
if (!prefix.contains(".")) {
// this may be an embedded class directly on the root entity (e.g. embeddablePriceList.restrictedPriceLists on OfferImpl)
return entity.findProperty("id").getValue();
}
throw new RuntimeException("Unable to establish a relationship id");
}
use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class PasswordFieldMetadataProvider method addMetadataFromFieldType.
@Override
public MetadataProviderResponse addMetadataFromFieldType(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
if (addMetadataFromFieldTypeRequest.getPresentationAttribute() instanceof BasicFieldMetadata && SupportedFieldType.PASSWORD.equals(((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()).getExplicitFieldType())) {
// build the metadata for the password field
addMetadataFromFieldTypeRequest.getDynamicEntityDao().getDefaultFieldMetadataProvider().addMetadataFromFieldType(addMetadataFromFieldTypeRequest, metadata);
((BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute()).setFieldType(SupportedFieldType.PASSWORD);
// clone the password field and add in a custom one
BasicFieldMetadata confirmMd = (BasicFieldMetadata) addMetadataFromFieldTypeRequest.getPresentationAttribute().cloneFieldMetadata();
confirmMd.setFieldName("passwordConfirm");
confirmMd.setFriendlyName("AdminUserImpl_Admin_Password_Confirm");
confirmMd.setExplicitFieldType(SupportedFieldType.PASSWORD_CONFIRM);
confirmMd.setValidationConfigurations(new HashMap<String, List<Map<String, String>>>());
metadata.put("passwordConfirm", confirmMd);
return MetadataProviderResponse.HANDLED;
} else {
return MetadataProviderResponse.NOT_HANDLED;
}
}
use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class DynamicEntityDaoImpl method getSimpleMergedProperties.
@Override
public Map<String, FieldMetadata> getSimpleMergedProperties(String entityName, PersistencePerspective persistencePerspective) {
Class<?>[] entityClasses;
try {
entityClasses = getAllPolymorphicEntitiesFromCeiling(Class.forName(entityName));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if (!ArrayUtils.isEmpty(entityClasses)) {
return getMergedProperties(entityName, entityClasses, (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY), persistencePerspective.getAdditionalNonPersistentProperties(), persistencePerspective.getAdditionalForeignKeys(), MergedPropertyType.PRIMARY, persistencePerspective.getPopulateToOneFields(), persistencePerspective.getIncludeFields(), persistencePerspective.getExcludeFields(), persistencePerspective.getConfigurationKey(), "");
} else {
Map<String, FieldMetadata> mergedProperties = new HashMap<>();
Class<?> targetClass;
try {
targetClass = Class.forName(entityName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Map<String, FieldMetadata> attributesMap = metadata.getFieldMetadataForTargetClass(null, targetClass, this, "");
for (String property : attributesMap.keySet()) {
FieldMetadata presentationAttribute = attributesMap.get(property);
if (!presentationAttribute.getExcluded()) {
Field field = FieldManager.getSingleField(targetClass, property);
if (!Modifier.isStatic(field.getModifiers())) {
boolean handled = false;
for (FieldMetadataProvider provider : fieldMetadataProviders) {
MetadataProviderResponse response = provider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(field, targetClass, null, new ForeignKey[] {}, MergedPropertyType.PRIMARY, null, null, "", property, null, false, 0, attributesMap, presentationAttribute, ((BasicFieldMetadata) presentationAttribute).getExplicitFieldType(), field.getType(), this), mergedProperties);
if (MetadataProviderResponse.NOT_HANDLED != response) {
handled = true;
}
if (MetadataProviderResponse.HANDLED_BREAK == response) {
break;
}
}
if (!handled) {
// this provider is not included in the provider list on purpose - it is designed to handle basic
// AdminPresentation fields, and those fields not admin presentation annotated at all
defaultFieldMetadataProvider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(field, targetClass, null, new ForeignKey[] {}, MergedPropertyType.PRIMARY, null, null, "", property, null, false, 0, attributesMap, presentationAttribute, ((BasicFieldMetadata) presentationAttribute).getExplicitFieldType(), field.getType(), this), mergedProperties);
}
}
}
}
return mergedProperties;
}
}
use of org.broadleafcommerce.openadmin.dto.BasicFieldMetadata in project BroadleafCommerce by BroadleafCommerce.
the class DynamicEntityDaoImpl method buildEntityProperties.
protected void buildEntityProperties(Map<String, FieldMetadata> fields, ForeignKey foreignField, ForeignKey[] additionalForeignFields, String[] additionalNonPersistentProperties, Boolean populateManyToOneFields, String[] includeFields, String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname, String propertyName, Class<?> returnedClass, Class<?> targetClass, List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded, String parentPrefix) {
Class<?>[] polymorphicEntities = getAllPolymorphicEntitiesFromCeiling(returnedClass);
List<Class<?>> clonedParentClasses = new ArrayList<>();
for (Class<?> parentClass : parentClasses) {
clonedParentClasses.add(parentClass);
}
clonedParentClasses.add(targetClass);
Map<String, FieldMetadata> newFields = getMergedPropertiesRecursively(ceilingEntityFullyQualifiedClassname, polymorphicEntities, foreignField, additionalNonPersistentProperties, additionalForeignFields, MergedPropertyType.PRIMARY, populateManyToOneFields, includeFields, excludeFields, configurationKey, clonedParentClasses, prefix + propertyName + '.', isParentExcluded, parentPrefix);
final String targetClassName = targetClass.getName();
for (FieldMetadata newMetadata : newFields.values()) {
newMetadata.setInheritedFromType(targetClassName);
newMetadata.setAvailableToTypes(new String[] { targetClassName });
}
Map<String, FieldMetadata> convertedFields = new HashMap<>(newFields.size());
for (Map.Entry<String, FieldMetadata> newField : newFields.entrySet()) {
final FieldMetadata fieldMetadata = newField.getValue();
final String key = newField.getKey();
convertedFields.put(propertyName + '.' + key, fieldMetadata);
if (fieldMetadata instanceof BasicFieldMetadata) {
for (Map.Entry<String, List<Map<String, String>>> validationConfigurations : ((BasicFieldMetadata) fieldMetadata).getValidationConfigurations().entrySet()) {
Class<?> validatorImpl = null;
try {
validatorImpl = Class.forName(validationConfigurations.getKey());
} catch (ClassNotFoundException e) {
Object bean = applicationContext.getBean(validationConfigurations.getKey());
if (bean != null) {
validatorImpl = bean.getClass();
}
}
if (validatorImpl != null && FieldNamePropertyValidator.class.isAssignableFrom(validatorImpl)) {
for (Map<String, String> configs : validationConfigurations.getValue()) {
for (Map.Entry<String, String> config : configs.entrySet()) {
final String value = config.getValue();
if (newFields.containsKey(value)) {
config.setValue(propertyName + "." + value);
}
}
}
}
}
}
if (isForeignKey(fieldMetadata)) {
setOriginatingFieldForForeignKey(propertyName, key, fieldMetadata);
}
}
fields.putAll(convertedFields);
}
Aggregations