use of org.broadleafcommerce.openadmin.server.dao.provider.metadata.request.AddMetadataFromFieldTypeRequest in project BroadleafCommerce by BroadleafCommerce.
the class DynamicEntityDaoImpl method buildBasicProperty.
protected void buildBasicProperty(Field field, Class<?> targetClass, ForeignKey foreignField, ForeignKey[] additionalForeignFields, String[] additionalNonPersistentProperties, MergedPropertyType mergedPropertyType, Map<String, FieldMetadata> presentationAttributes, List<Property> componentProperties, Map<String, FieldMetadata> fields, String idProperty, Boolean populateManyToOneFields, String[] includeFields, String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname, List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded, String propertyName, Type type, boolean propertyForeignKey, int additionalForeignKeyIndexPosition, Boolean isComponentPrefix, String parentPrefix) {
FieldMetadata presentationAttribute = presentationAttributes.get(propertyName);
Boolean amIExcluded = isParentExcluded || !testPropertyInclusion(presentationAttribute);
Boolean includeField = !testPropertyRecursion(prefix, parentClasses, propertyName, targetClass, ceilingEntityFullyQualifiedClassname, isComponentPrefix, parentPrefix);
SupportedFieldType explicitType = null;
if (presentationAttribute != null && presentationAttribute instanceof BasicFieldMetadata) {
explicitType = ((BasicFieldMetadata) presentationAttribute).getExplicitFieldType();
}
Class<?> returnedClass = type.getReturnedClass();
checkProp: {
if (type.isComponentType() && includeField) {
buildComponentProperties(targetClass, foreignField, additionalForeignFields, additionalNonPersistentProperties, mergedPropertyType, fields, idProperty, populateManyToOneFields, includeFields, excludeFields, configurationKey, ceilingEntityFullyQualifiedClassname, propertyName, type, returnedClass, parentClasses, amIExcluded, prefix, parentPrefix);
break checkProp;
}
/*
* Currently we do not support ManyToOne fields whose class type is the same
* as the target type, since this forms an infinite loop and will cause a stack overflow.
*/
if (type.isEntityType() && !returnedClass.isAssignableFrom(targetClass) && populateManyToOneFields && includeField) {
buildEntityProperties(fields, foreignField, additionalForeignFields, additionalNonPersistentProperties, populateManyToOneFields, includeFields, excludeFields, configurationKey, ceilingEntityFullyQualifiedClassname, propertyName, returnedClass, targetClass, parentClasses, prefix, amIExcluded, parentPrefix);
break checkProp;
}
}
// Don't include this property if it failed manyToOne inclusion and is not a specified foreign key
if (includeField || propertyForeignKey || additionalForeignKeyIndexPosition >= 0) {
defaultFieldMetadataProvider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(field, targetClass, foreignField, additionalForeignFields, mergedPropertyType, componentProperties, idProperty, prefix, propertyName, type, propertyForeignKey, additionalForeignKeyIndexPosition, presentationAttributes, presentationAttribute, explicitType, returnedClass, this), fields);
}
}
use of org.broadleafcommerce.openadmin.server.dao.provider.metadata.request.AddMetadataFromFieldTypeRequest in project BroadleafCommerce by BroadleafCommerce.
the class MapFieldsFieldMetadataProvider method addMetadataFromFieldType.
@Override
public MetadataProviderResponse addMetadataFromFieldType(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
if (!canHandleFieldForTypeMetadata(addMetadataFromFieldTypeRequest, metadata)) {
return MetadataProviderResponse.NOT_HANDLED;
}
// look for any map field metadata that was previously added for the requested field
for (Map.Entry<String, FieldMetadata> entry : addMetadataFromFieldTypeRequest.getPresentationAttributes().entrySet()) {
if (entry.getKey().startsWith(addMetadataFromFieldTypeRequest.getRequestedPropertyName() + FieldManager.MAPFIELDSEPARATOR)) {
TypeLocatorImpl typeLocator = new TypeLocatorImpl(new TypeResolver());
Type myType = null;
// first, check if an explicit type was declared
String valueClass = ((BasicFieldMetadata) entry.getValue()).getMapFieldValueClass();
if (valueClass != null) {
myType = typeLocator.entity(valueClass);
}
if (myType == null) {
SupportedFieldType fieldType = ((BasicFieldMetadata) entry.getValue()).getExplicitFieldType();
Class<?> basicJavaType = getBasicJavaType(fieldType);
if (basicJavaType != null) {
myType = typeLocator.basic(basicJavaType);
}
}
if (myType == null) {
java.lang.reflect.Type genericType = addMetadataFromFieldTypeRequest.getRequestedField().getGenericType();
if (genericType instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) genericType;
Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[1];
Class<?>[] entities = addMetadataFromFieldTypeRequest.getDynamicEntityDao().getAllPolymorphicEntitiesFromCeiling(clazz);
if (!ArrayUtils.isEmpty(entities)) {
myType = typeLocator.entity(entities[entities.length - 1]);
}
}
}
if (myType == null) {
throw new IllegalArgumentException("Unable to establish the type for the property (" + entry.getKey() + ")");
}
// add property for this map field as if it was a normal field
super.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(addMetadataFromFieldTypeRequest.getRequestedField(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getForeignField(), addMetadataFromFieldTypeRequest.getAdditionalForeignFields(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getComponentProperties(), addMetadataFromFieldTypeRequest.getIdProperty(), addMetadataFromFieldTypeRequest.getPrefix(), entry.getKey(), myType, addMetadataFromFieldTypeRequest.isPropertyForeignKey(), addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition(), addMetadataFromFieldTypeRequest.getPresentationAttributes(), entry.getValue(), ((BasicFieldMetadata) entry.getValue()).getExplicitFieldType(), myType.getReturnedClass(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()), metadata);
}
}
return MetadataProviderResponse.HANDLED;
}
use of org.broadleafcommerce.openadmin.server.dao.provider.metadata.request.AddMetadataFromFieldTypeRequest 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.server.dao.provider.metadata.request.AddMetadataFromFieldTypeRequest in project BroadleafCommerce by BroadleafCommerce.
the class DynamicEntityDaoImpl method buildProperties.
protected void buildProperties(Class<?> targetClass, ForeignKey foreignField, ForeignKey[] additionalForeignFields, String[] additionalNonPersistentProperties, MergedPropertyType mergedPropertyType, Map<String, FieldMetadata> presentationAttributes, List<Property> componentProperties, Map<String, FieldMetadata> fields, List<String> propertyNames, List<Type> propertyTypes, String idProperty, Boolean populateManyToOneFields, String[] includeFields, String[] excludeFields, String configurationKey, String ceilingEntityFullyQualifiedClassname, List<Class<?>> parentClasses, String prefix, Boolean isParentExcluded, Boolean isComponentPrefix, String parentPrefix) {
int j = 0;
Comparator<String> propertyComparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// check for property name equality and for map field properties
if (o1.equals(o2) || o1.startsWith(o2 + FieldManager.MAPFIELDSEPARATOR) || o2.startsWith(o1 + FieldManager.MAPFIELDSEPARATOR)) {
return 0;
}
return o1.compareTo(o2);
}
};
List<String> presentationKeyList = new ArrayList<>(presentationAttributes.keySet());
Collections.sort(presentationKeyList);
for (String propertyName : propertyNames) {
final Type type = propertyTypes.get(j);
boolean isPropertyForeignKey = testForeignProperty(foreignField, prefix, propertyName);
int additionalForeignKeyIndexPosition = findAdditionalForeignKeyIndex(additionalForeignFields, prefix, propertyName);
j++;
Field myField = getFieldManager().getField(targetClass, propertyName);
if (myField == null) {
// try to get the field with the prefix - needed for advanced collections that appear in @Embedded classes
myField = getFieldManager().getField(targetClass, prefix + propertyName);
}
if (!type.isAnyType() && !type.isCollectionType() || isPropertyForeignKey || additionalForeignKeyIndexPosition >= 0 || Collections.binarySearch(presentationKeyList, propertyName, propertyComparator) >= 0) {
if (myField != null) {
boolean handled = false;
for (FieldMetadataProvider provider : fieldMetadataProviders) {
FieldMetadata presentationAttribute = presentationAttributes.get(propertyName);
if (presentationAttribute != null) {
setExcludedBasedOnShowIfProperty(presentationAttribute);
}
MetadataProviderResponse response = provider.addMetadataFromFieldType(new AddMetadataFromFieldTypeRequest(myField, targetClass, foreignField, additionalForeignFields, mergedPropertyType, componentProperties, idProperty, prefix, propertyName, type, isPropertyForeignKey, additionalForeignKeyIndexPosition, presentationAttributes, presentationAttribute, null, type.getReturnedClass(), this), fields);
if (MetadataProviderResponse.NOT_HANDLED != response) {
handled = true;
}
if (MetadataProviderResponse.HANDLED_BREAK == response) {
break;
}
}
if (!handled) {
buildBasicProperty(myField, targetClass, foreignField, additionalForeignFields, additionalNonPersistentProperties, mergedPropertyType, presentationAttributes, componentProperties, fields, idProperty, populateManyToOneFields, includeFields, excludeFields, configurationKey, ceilingEntityFullyQualifiedClassname, parentClasses, prefix, isParentExcluded, propertyName, type, isPropertyForeignKey, additionalForeignKeyIndexPosition, isComponentPrefix, parentPrefix);
}
}
}
}
}
use of org.broadleafcommerce.openadmin.server.dao.provider.metadata.request.AddMetadataFromFieldTypeRequest 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;
}
}
Aggregations