private MethodMetadataBuilder getInstallLabelConvertersMethod() {
final List<JavaType> sortedRelevantDomainTypes = new ArrayList<JavaType>(
relevantDomainTypes);
Collections.sort(sortedRelevantDomainTypes);
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final Set<String> methodNames = new HashSet<String>();
for (final JavaType formBackingObject : sortedRelevantDomainTypes) {
String simpleName = formBackingObject.getSimpleTypeName();
while (methodNames.contains(simpleName)) {
simpleName += "_";
}
methodNames.add(simpleName);
final JavaSymbolName toIdMethodName = new JavaSymbolName("get"
+ simpleName + "ToStringConverter");
builder.addMethod(getToStringConverterMethod(formBackingObject,
toIdMethodName, toStringMethods.get(formBackingObject)));
bodyBuilder.appendFormalLine("registry.addConverter("
+ toIdMethodName.getSymbolName() + "());");
final JavaSymbolName toTypeMethodName = new JavaSymbolName(
"getIdTo" + simpleName + CONVERTER);
final MethodMetadataBuilder toTypeConverterMethod = getToTypeConverterMethod(
formBackingObject, toTypeMethodName,
findMethods.get(formBackingObject),
idTypes.get(formBackingObject));
if (toTypeConverterMethod != null) {
builder.addMethod(toTypeConverterMethod);
bodyBuilder.appendFormalLine("registry.addConverter("
+ toTypeMethodName.getSymbolName() + "());");
}
// Only allow conversion if ID type is not String already.
if (!idTypes.get(formBackingObject).equals(JavaType.STRING)) {
final JavaSymbolName stringToTypeMethodName = new JavaSymbolName(
"getStringTo" + simpleName + CONVERTER);
builder.addMethod(getStringToTypeConverterMethod(
formBackingObject, stringToTypeMethodName,
idTypes.get(formBackingObject)));
bodyBuilder.appendFormalLine("registry.addConverter("
+ stringToTypeMethodName.getSymbolName() + "());");
}
}
for (final Entry<JavaType, Map<Object, JavaSymbolName>> entry : compositePrimaryKeyTypes
.entrySet()) {
final JavaType targetType = entry.getKey();
final Map<Object, JavaSymbolName> jsonMethodNames = entry
.getValue();
final MethodMetadataBuilder jsonToConverterMethod = getJsonToConverterMethod(
targetType,
jsonMethodNames.get(CustomDataJsonTags.FROM_JSON_METHOD));
if (jsonToConverterMethod != null) {
builder.addMethod(jsonToConverterMethod);
bodyBuilder.appendFormalLine("registry.addConverter("
+ jsonToConverterMethod.getMethodName().getSymbolName()
+ "());");
}
final MethodMetadataBuilder toJsonConverterMethod = getToJsonConverterMethod(
targetType,
jsonMethodNames.get(CustomDataJsonTags.TO_JSON_METHOD));
if (toJsonConverterMethod != null) {
builder.addMethod(toJsonConverterMethod);
bodyBuilder.appendFormalLine("registry.addConverter("
+ toJsonConverterMethod.getMethodName().getSymbolName()
+ "());");
}
}