Getting metadata to the Form?
Posted: Sun Feb 02, 2014 10:28 am
So I needed something to help me manipulate the Admin forms a little easier,
I made an annotation with AngularJS attribute declarations, so I extended FieldMetaDataProviderAdapter upon getting invoked my values get added to additionalMetadata (Confirmed in my debug console)
I've overridden blAdminWebCustomTemplateResolver to point to my directory and set it at the highest precedence.
But the value doesn't exist when I added it to the overridden field template. It just throws a Thymeleaf Exception.
My next thought is extend AbstractFormBuilderExtensionHandler am I on the right track?
I made an annotation with AngularJS attribute declarations, so I extended FieldMetaDataProviderAdapter upon getting invoked my values get added to additionalMetadata (Confirmed in my debug console)
Code: Select all
<bean id="blMetadataProviders" class="org.broadleafcommerce.common.util.SortedListFactoryBean" scope="prototype">
<property name="sourceList">
<list>
<ref bean="blBasicFieldMetadataProvider"/>
<ref bean="blAngularJSMetadataProvider" />
<ref bean="blCollectionFieldMetadataProvider"/>
<ref bean="blAdornedTargetCollectionFieldMetadataProvider"/>
<ref bean="blMapFieldMetadataProvider"/>
<ref bean="blMapFieldsFieldMetadataProvider"/>
<ref bean="blPasswordFieldMetadataProvider"/>
</list>
</property>
</bean>
Code: Select all
@Component("blAngularJSMetadataProvider")
@Scope("prototype")
public class AngularJSMetadataProvider extends FieldMetadataProviderAdapter{
protected boolean canHandleFieldForConfiguredMetadata(AddMetadataRequest addMetadataRequest) {
AngularJS annotation = addMetadataRequest.getRequestedField().getAnnotation(AngularJS.class);
return annotation != null;
}
@Override
public FieldProviderResponse addMetadata(AddMetadataRequest addMetadataRequest, Map<String, FieldMetadata> metadata) {
if (!canHandleFieldForConfiguredMetadata(addMetadataRequest)) {
return FieldProviderResponse.NOT_HANDLED;
}
AngularJS annotation = addMetadataRequest.getRequestedField().getAnnotation(AngularJS.class);
FieldInfo fieldInfo = buildFieldInfo(addMetadataRequest.getRequestedField());
Map<String, Object> annotationAttributes = AnnotationUtils.getAnnotationAttributes(annotation);
Map<String, Object> annotationAttributesFixed = new HashMap<String, Object>();
for(Map.Entry<String, Object> annotationAttribute: annotationAttributes.entrySet())
{
if(!annotationAttribute.getValue().toString().isEmpty())
{
String lc = annotationAttribute.getKey().toLowerCase();
String dash = lc.replace("ng", "ng-");
annotationAttributesFixed.put(dash, annotationAttribute.getValue());
}
}
String ngCsv = Joiner.on(",").withKeyValueSeparator("=").join(annotationAttributesFixed);
buildBasicMetadata(metadata, fieldInfo, ngCsv);
return FieldProviderResponse.HANDLED;
}
protected void buildBasicMetadata(Map<String, FieldMetadata> attributes, FieldInfo field, String ngCsv) {
BasicFieldMetadata serverMetadata = (BasicFieldMetadata) attributes.get(field.getName());
BasicFieldMetadata metadata;
if (serverMetadata != null) {
metadata = serverMetadata;
} else {
metadata = new BasicFieldMetadata();
}
Map<String, Object> addMeta = new HashMap<String, Object>();
addMeta.put("angular", ngCsv);
metadata.setAdditionalMetadata(addMeta);
attributes.put(field.getName(), metadata);
}
}
I've overridden blAdminWebCustomTemplateResolver to point to my directory and set it at the highest precedence.
Code: Select all
<bean id="blAdminWebCustomTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/admin/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="cacheable" value="${cache.page.templates}"/>
<property name="cacheTTLMs" value="${cache.page.templates.ttl}" />
<property name="order" value="100"/>
</bean>
But the value doesn't exist when I added it to the overridden field template. It just throws a Thymeleaf Exception.
My next thought is extend AbstractFormBuilderExtensionHandler am I on the right track?
Code: Select all
<bean id="blFormBuilderExtensionHandlers" class="org.springframework.beans.factory.config.ListFactoryBean" >
<property name="sourceList">
<list>
<ref bean="blAngularJSFormBuilderExtensionHandler" />
</list>
</property>
</bean>