@Override
public FieldProviderResponse addMetadataFromFieldType(AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) {
if (!canHandleFieldForTypeMetadata(addMetadataFromFieldTypeRequest, metadata)) {
return FieldProviderResponse.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()});
}
}
// Handle scenarios where the collection metadata is also a foreign key. The {@link BasicFieldMetadata} that has all
// of the information about the foreign key will travel along with the built {@link BasicCollectionMetadata} under
// the {@link FieldMetadata#getAdditionalMetadata()} field. This is then pulled out within
// {@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 FieldProviderResponse.HANDLED;
}