use of org.broadleafcommerce.common.presentation.AdminPresentationMap in project BroadleafCommerce by BroadleafCommerce.
the class MapFieldMetadataProvider method addMetadata.
@Override
public MetadataProviderResponse addMetadata(AddFieldMetadataRequest addMetadataRequest, Map<String, FieldMetadata> metadata) {
if (!canHandleFieldForConfiguredMetadata(addMetadataRequest, metadata)) {
return MetadataProviderResponse.NOT_HANDLED;
}
AdminPresentationMap annot = addMetadataRequest.getRequestedField().getAnnotation(AdminPresentationMap.class);
FieldInfo info = buildFieldInfo(addMetadataRequest.getRequestedField());
FieldMetadataOverride override = constructMapMetadataOverride(annot);
buildMapMetadata(addMetadataRequest.getParentClass(), addMetadataRequest.getTargetClass(), metadata, info, override, addMetadataRequest.getDynamicEntityDao(), addMetadataRequest.getPrefix());
setClassOwnership(addMetadataRequest.getParentClass(), addMetadataRequest.getTargetClass(), metadata, info);
return MetadataProviderResponse.HANDLED;
}
use of org.broadleafcommerce.common.presentation.AdminPresentationMap in project BroadleafCommerce by BroadleafCommerce.
the class MapFieldMetadataProvider method buildAdminPresentationMapOverride.
protected void buildAdminPresentationMapOverride(String prefix, Boolean isParentExcluded, Map<String, FieldMetadata> mergedProperties, Map<String, AdminPresentationMapOverride> presentationMapOverrides, String propertyName, String key, DynamicEntityDao dynamicEntityDao) {
AdminPresentationMapOverride override = presentationMapOverrides.get(propertyName);
if (override != null) {
AdminPresentationMap annot = override.value();
if (annot != null) {
String testKey = prefix + key;
if ((testKey.startsWith(propertyName + ".") || testKey.equals(propertyName)) && annot.excluded()) {
FieldMetadata metadata = mergedProperties.get(key);
if (LOG.isDebugEnabled()) {
LOG.debug("buildAdminPresentationMapOverride:Excluding " + key + "because an override annotation declared " + testKey + "to be excluded");
}
metadata.setExcluded(true);
return;
}
if ((testKey.startsWith(propertyName + ".") || testKey.equals(propertyName)) && !annot.excluded()) {
FieldMetadata metadata = mergedProperties.get(key);
if (!isParentExcluded) {
if (LOG.isDebugEnabled()) {
LOG.debug("buildAdminPresentationMapOverride:Showing " + key + "because an override annotation declared " + testKey + " to not be excluded");
}
metadata.setExcluded(false);
}
}
if (!(mergedProperties.get(key) instanceof MapMetadata)) {
return;
}
MapMetadata serverMetadata = (MapMetadata) mergedProperties.get(key);
if (serverMetadata.getTargetClass() != null) {
try {
Class<?> targetClass = Class.forName(serverMetadata.getTargetClass());
Class<?> parentClass = null;
if (serverMetadata.getOwningClass() != null) {
parentClass = Class.forName(serverMetadata.getOwningClass());
}
String fieldName = serverMetadata.getFieldName();
Field field = dynamicEntityDao.getFieldManager().getField(targetClass, fieldName);
FieldMetadataOverride localMetadata = constructMapMetadataOverride(annot);
// do not include the previous metadata - we want to construct a fresh metadata from the override annotation
Map<String, FieldMetadata> temp = new HashMap<String, FieldMetadata>(1);
FieldInfo info = buildFieldInfo(field);
buildMapMetadata(parentClass, targetClass, temp, info, localMetadata, dynamicEntityDao, serverMetadata.getPrefix());
MapMetadata result = (MapMetadata) temp.get(field.getName());
result.setInheritedFromType(serverMetadata.getInheritedFromType());
result.setAvailableToTypes(serverMetadata.getAvailableToTypes());
mergedProperties.put(key, result);
if (isParentExcluded) {
if (LOG.isDebugEnabled()) {
LOG.debug("buildAdminPresentationMapOverride:Excluding " + key + "because the parent was excluded");
}
serverMetadata.setExcluded(true);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
}
use of org.broadleafcommerce.common.presentation.AdminPresentationMap in project BroadleafCommerce by BroadleafCommerce.
the class MapFieldMetadataProvider method buildMapMetadata.
protected void buildMapMetadata(Class<?> parentClass, Class<?> targetClass, Map<String, FieldMetadata> attributes, FieldInfo field, FieldMetadataOverride map, DynamicEntityDao dynamicEntityDao, String prefix) {
MapMetadata serverMetadata = (MapMetadata) attributes.get(field.getName());
Class<?> resolvedClass = parentClass == null ? targetClass : parentClass;
MapMetadata metadata;
if (serverMetadata != null) {
metadata = serverMetadata;
} else {
metadata = new MapMetadata();
}
if (map.getReadOnly() != null) {
metadata.setMutable(!map.getReadOnly());
}
if (map.getShowIfProperty() != null) {
metadata.setShowIfProperty(map.getShowIfProperty());
}
if (map.getShowIfFieldEquals() != null) {
metadata.setShowIfFieldEquals(map.getShowIfFieldEquals());
}
metadata.setPrefix(prefix);
metadata.setTargetClass(targetClass.getName());
metadata.setFieldName(field.getName());
org.broadleafcommerce.openadmin.dto.OperationTypes dtoOperationTypes = new org.broadleafcommerce.openadmin.dto.OperationTypes(OperationType.MAP, OperationType.MAP, OperationType.MAP, OperationType.MAP, OperationType.MAP);
if (map.getAddType() != null) {
dtoOperationTypes.setAddType(map.getAddType());
}
if (map.getRemoveType() != null) {
dtoOperationTypes.setRemoveType(map.getRemoveType());
}
if (map.getFetchType() != null) {
dtoOperationTypes.setFetchType(map.getFetchType());
}
if (map.getInspectType() != null) {
dtoOperationTypes.setInspectType(map.getInspectType());
}
if (map.getUpdateType() != null) {
dtoOperationTypes.setUpdateType(map.getUpdateType());
}
// don't allow additional non-persistent properties or additional foreign keys for an advanced collection datasource - they don't make sense in this context
PersistencePerspective persistencePerspective;
if (serverMetadata != null) {
persistencePerspective = metadata.getPersistencePerspective();
persistencePerspective.setOperationTypes(dtoOperationTypes);
} else {
persistencePerspective = new PersistencePerspective(dtoOperationTypes, new String[] {}, new ForeignKey[] {});
metadata.setPersistencePerspective(persistencePerspective);
}
String parentObjectClass = resolvedClass.getName();
Map idMetadata;
if (parentClass != null) {
idMetadata = dynamicEntityDao.getIdMetadata(parentClass);
} else {
idMetadata = dynamicEntityDao.getIdMetadata(targetClass);
}
String parentObjectIdField = (String) idMetadata.get("name");
String keyClassName = null;
if (serverMetadata != null) {
keyClassName = ((MapStructure) metadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getKeyClassName();
}
if (map.getKeyClass() != null && !void.class.getName().equals(map.getKeyClass())) {
keyClassName = map.getKeyClass();
}
if (keyClassName == null) {
java.lang.reflect.Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[0];
if (!ArrayUtils.isEmpty(dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz))) {
throw new IllegalArgumentException("Key class for AdminPresentationMap was determined to be a JPA managed type. Only primitive types for the key type are currently supported.");
}
keyClassName = clazz.getName();
}
}
if (keyClassName == null) {
keyClassName = String.class.getName();
}
String keyPropertyName = "key";
String mapKeyValueProperty = "";
if (StringUtils.isNotBlank(field.getMapKey())) {
mapKeyValueProperty = field.getMapKey();
}
if (StringUtils.isNotBlank(map.getMapKeyValueProperty())) {
mapKeyValueProperty = map.getMapKeyValueProperty();
}
String keyPropertyFriendlyName = null;
if (serverMetadata != null) {
keyPropertyFriendlyName = ((MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getKeyPropertyFriendlyName();
}
if (map.getKeyPropertyFriendlyName() != null) {
keyPropertyFriendlyName = map.getKeyPropertyFriendlyName();
}
Boolean deleteEntityUponRemove = null;
if (serverMetadata != null) {
deleteEntityUponRemove = ((MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE)).getDeleteValueEntity();
}
if (map.isDeleteEntityUponRemove() != null) {
deleteEntityUponRemove = map.isDeleteEntityUponRemove();
}
String valuePropertyName = "value";
String valuePropertyFriendlyName = null;
if (serverMetadata != null) {
MapStructure structure = (MapStructure) serverMetadata.getPersistencePerspective().getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
if (structure instanceof SimpleValueMapStructure) {
valuePropertyFriendlyName = ((SimpleValueMapStructure) structure).getValuePropertyFriendlyName();
} else {
valuePropertyFriendlyName = "";
}
}
if (map.getValuePropertyFriendlyName() != null) {
valuePropertyFriendlyName = map.getValuePropertyFriendlyName();
}
if (map.getMediaField() != null) {
metadata.setMediaField(map.getMediaField());
}
if (map.getToOneTargetProperty() != null) {
metadata.setToOneTargetProperty(map.getToOneTargetProperty());
}
if (map.getToOneParentProperty() != null) {
metadata.setToOneParentProperty((map.getToOneParentProperty()));
}
if (map.getValueClass() != null && !void.class.getName().equals(map.getValueClass())) {
metadata.setValueClassName(map.getValueClass());
}
if (metadata.getValueClassName() == null) {
java.lang.reflect.Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[1];
Class<?>[] entities = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz);
if (!ArrayUtils.isEmpty(entities)) {
metadata.setValueClassName(entities[entities.length - 1].getName());
}
}
}
if (metadata.getValueClassName() == null) {
if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())) {
metadata.setValueClassName(field.getManyToManyTargetEntity());
}
}
if (metadata.getValueClassName() == null) {
metadata.setValueClassName(String.class.getName());
}
Boolean simpleValue = null;
if (map.getSimpleValue() != null && map.getSimpleValue() != UnspecifiedBooleanType.UNSPECIFIED) {
simpleValue = map.getSimpleValue() == UnspecifiedBooleanType.TRUE;
}
if (simpleValue == null) {
java.lang.reflect.Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[1];
Class<?>[] entities = dynamicEntityDao.getAllPolymorphicEntitiesFromCeiling(clazz);
simpleValue = ArrayUtils.isEmpty(entities);
}
}
if (simpleValue == null) {
// ManyToMany manyToMany = field.getAnnotation(ManyToMany.class);
if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())) {
simpleValue = false;
}
}
if (simpleValue == null) {
throw new IllegalArgumentException("Unable to infer if the value for the map is of a complex or simple type based on any parameterized type or ManyToMany annotation. Please explicitly set the isSimpleValue property.");
}
metadata.setSimpleValue(simpleValue);
if (map.getKeys() != null) {
metadata.setKeys(map.getKeys());
}
metadata.setMapKeyValueProperty(mapKeyValueProperty);
if (map.getMapKeyOptionEntityClass() != null) {
if (!void.class.getName().equals(map.getMapKeyOptionEntityClass())) {
metadata.setMapKeyOptionEntityClass(map.getMapKeyOptionEntityClass());
} else {
metadata.setMapKeyOptionEntityClass("");
}
}
if (map.getMapKeyOptionEntityDisplayField() != null) {
metadata.setMapKeyOptionEntityDisplayField(map.getMapKeyOptionEntityDisplayField());
}
if (map.getMapKeyOptionEntityValueField() != null) {
metadata.setMapKeyOptionEntityValueField(map.getMapKeyOptionEntityValueField());
}
if (map.getForceFreeFormKeys() != null) {
if (!map.getForceFreeFormKeys() && ArrayUtils.isEmpty(metadata.getKeys()) && (StringUtils.isEmpty(metadata.getMapKeyOptionEntityClass()) || StringUtils.isEmpty(metadata.getMapKeyOptionEntityValueField()) || StringUtils.isEmpty(metadata.getMapKeyOptionEntityDisplayField()))) {
throw new IllegalArgumentException("Could not ascertain method for generating key options for the annotated map (" + field.getName() + "). Must specify either an array of AdminPresentationMapKey values for the keys property, or utilize the mapOptionKeyClass, mapOptionKeyDisplayField and mapOptionKeyValueField properties. If you wish to allow free form entry for key values, then set forceFreeFormKeys on AdminPresentationMap.");
}
}
MapStructure mapStructure;
if (serverMetadata != null) {
ForeignKey foreignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
foreignKey.setManyToField(parentObjectIdField);
foreignKey.setForeignKeyClass(parentObjectClass);
if (metadata.isSimpleValue()) {
mapStructure = (SimpleValueMapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
mapStructure.setKeyClassName(keyClassName);
mapStructure.setKeyPropertyName(keyPropertyName);
mapStructure.setKeyPropertyFriendlyName(keyPropertyFriendlyName);
mapStructure.setValueClassName(metadata.getValueClassName());
((SimpleValueMapStructure) mapStructure).setValuePropertyName(valuePropertyName);
((SimpleValueMapStructure) mapStructure).setValuePropertyFriendlyName(valuePropertyFriendlyName);
mapStructure.setMapProperty(prefix + field.getName());
mapStructure.setMutable(metadata.isMutable());
} else {
mapStructure = (MapStructure) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.MAPSTRUCTURE);
mapStructure.setKeyClassName(keyClassName);
mapStructure.setKeyPropertyName(keyPropertyName);
mapStructure.setKeyPropertyFriendlyName(keyPropertyFriendlyName);
mapStructure.setValueClassName(metadata.getValueClassName());
mapStructure.setMapProperty(prefix + field.getName());
mapStructure.setDeleteValueEntity(deleteEntityUponRemove);
mapStructure.setMutable(metadata.isMutable());
}
} else {
ForeignKey foreignKey = new ForeignKey(parentObjectIdField, parentObjectClass);
persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.FOREIGNKEY, foreignKey);
if (metadata.isSimpleValue()) {
mapStructure = new SimpleValueMapStructure(keyClassName, keyPropertyName, keyPropertyFriendlyName, metadata.getValueClassName(), valuePropertyName, valuePropertyFriendlyName, prefix + field.getName(), mapKeyValueProperty);
mapStructure.setMutable(metadata.isMutable());
} else {
mapStructure = new MapStructure(keyClassName, keyPropertyName, keyPropertyFriendlyName, metadata.getValueClassName(), prefix + field.getName(), deleteEntityUponRemove, mapKeyValueProperty);
mapStructure.setMutable(metadata.isMutable());
}
persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.MAPSTRUCTURE, mapStructure);
}
if (!StringUtils.isEmpty(map.getManyToField())) {
mapStructure.setManyToField(map.getManyToField());
}
if (mapStructure.getManyToField() == null) {
// try to infer the value
if (field.getManyToManyMappedBy() != null) {
mapStructure.setManyToField(field.getManyToManyMappedBy());
}
}
if (mapStructure.getManyToField() == null) {
// try to infer the value
if (field.getOneToManyMappedBy() != null) {
mapStructure.setManyToField(field.getOneToManyMappedBy());
}
}
if (map.getExcluded() != null) {
if (LOG.isDebugEnabled()) {
if (map.getExcluded()) {
LOG.debug("buildMapMetadata:Excluding " + field.getName() + " because it was explicitly declared in config");
} else {
LOG.debug("buildMapMetadata:Showing " + field.getName() + " because it was explicitly declared in config");
}
}
metadata.setExcluded(map.getExcluded());
}
if (map.getLazyFetch() != null) {
metadata.setLazyFetch(map.getLazyFetch());
}
if (map.getManualFetch() != null) {
metadata.setManualFetch(map.getManualFetch());
}
if (map.getFriendlyName() != null) {
metadata.setFriendlyName(map.getFriendlyName());
}
if (map.getSecurityLevel() != null) {
metadata.setSecurityLevel(map.getSecurityLevel());
}
if (map.getOrder() != null) {
metadata.setOrder(map.getOrder());
}
if (map.getTab() != null) {
metadata.setTab(map.getTab());
}
if (map.getTabOrder() != null) {
metadata.setTabOrder(map.getTabOrder());
}
if (map.getGroup() != null) {
metadata.setGroup(map.getGroup());
}
if (map.getCustomCriteria() != null) {
metadata.setCustomCriteria(map.getCustomCriteria());
}
if (map.getUseServerSideInspectionCache() != null) {
persistencePerspective.setUseServerSideInspectionCache(map.getUseServerSideInspectionCache());
}
if (map.getCurrencyCodeField() != null) {
metadata.setCurrencyCodeField(map.getCurrencyCodeField());
}
if (map.getForceFreeFormKeys() != null) {
metadata.setForceFreeFormKeys(map.getForceFreeFormKeys());
}
attributes.put(field.getName(), metadata);
}
use of org.broadleafcommerce.common.presentation.AdminPresentationMap in project BroadleafCommerce by BroadleafCommerce.
the class MapFieldsFieldMetadataProvider method addMetadata.
@Override
public MetadataProviderResponse addMetadata(AddFieldMetadataRequest addMetadataRequest, Map<String, FieldMetadata> metadata) {
if (!canHandleFieldForConfiguredMetadata(addMetadataRequest, metadata)) {
return MetadataProviderResponse.NOT_HANDLED;
}
AdminPresentationMapFields annot = addMetadataRequest.getRequestedField().getAnnotation(AdminPresentationMapFields.class);
for (AdminPresentationMapField mapField : annot.mapDisplayFields()) {
if (mapField.fieldPresentation().fieldType() == SupportedFieldType.UNKNOWN) {
throw new IllegalArgumentException("fieldType property on AdminPresentation must be set for AdminPresentationMapField");
}
FieldMetadataOverride override = constructBasicMetadataOverride(mapField.fieldPresentation(), null, null);
override.setFriendlyName(mapField.fieldPresentation().friendlyName());
FieldInfo myInfo = new FieldInfo();
myInfo.setName(addMetadataRequest.getRequestedField().getName() + FieldManager.MAPFIELDSEPARATOR + mapField.fieldName());
buildBasicMetadata(addMetadataRequest.getParentClass(), addMetadataRequest.getTargetClass(), metadata, myInfo, override, addMetadataRequest.getDynamicEntityDao());
setClassOwnership(addMetadataRequest.getParentClass(), addMetadataRequest.getTargetClass(), metadata, myInfo);
BasicFieldMetadata basicFieldMetadata = (BasicFieldMetadata) metadata.get(myInfo.getName());
if (!mapField.targetClass().equals(Void.class)) {
if (mapField.targetClass().isInterface()) {
throw new IllegalArgumentException("targetClass on @AdminPresentationMapField must be a concrete class");
}
basicFieldMetadata.setMapFieldValueClass(mapField.targetClass().getName());
}
if (!StringUtils.isEmpty(mapField.manyToField())) {
basicFieldMetadata.setManyToField(mapField.manyToField());
}
AdminPresentationMap annotMap = addMetadataRequest.getRequestedField().getAnnotation(AdminPresentationMap.class);
if (annotMap != null && !StringUtils.isEmpty(annotMap.toOneTargetProperty())) {
basicFieldMetadata.setToOneTargetProperty(annotMap.toOneTargetProperty());
} else if (!StringUtils.isEmpty(annot.toOneTargetProperty())) {
basicFieldMetadata.setToOneTargetProperty(annot.toOneTargetProperty());
}
if (annotMap != null && !StringUtils.isEmpty(annotMap.toOneParentProperty())) {
basicFieldMetadata.setToOneParentProperty(annotMap.toOneParentProperty());
} else if (!StringUtils.isEmpty(annot.toOneParentProperty())) {
basicFieldMetadata.setToOneParentProperty(annot.toOneParentProperty());
}
String mapKeyValueProperty = "key";
if (StringUtils.isNotBlank(myInfo.getMapKey())) {
mapKeyValueProperty = myInfo.getMapKey();
}
if (annotMap != null) {
if (StringUtils.isNotBlank(annotMap.mapKeyValueProperty())) {
mapKeyValueProperty = annotMap.mapKeyValueProperty();
}
}
basicFieldMetadata.setMapKeyValueProperty(mapKeyValueProperty);
}
return MetadataProviderResponse.HANDLED;
}
use of org.broadleafcommerce.common.presentation.AdminPresentationMap in project BroadleafCommerce by BroadleafCommerce.
the class AdvancedCollectionFieldMetadataProvider method canHandleFieldForTypeMetadata.
protected boolean canHandleFieldForTypeMetadata(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
AdminPresentationMap map = addMetadataFromFieldTypeRequest.getRequestedField().getAnnotation(AdminPresentationMap.class);
AdminPresentationCollection collection = addMetadataFromFieldTypeRequest.getRequestedField().getAnnotation(AdminPresentationCollection.class);
return map != null || collection != null;
}
Aggregations