parameterTypes);
if (userMethod != null) {
Validate.isTrue(userMethod.getReturnType().equals(returnType),
"Method '%s' on '%s' must return '%s'", methodName,
destination, returnType.getNameIncludingTypeParameters());
return new MethodMetadataBuilder(userMethod);
}
// Create the method body
builder.getImportRegistrationResolver().addImports(ARRAY_LIST,
ITERATOR, CONSTRAINT_VIOLATION_EXCEPTION, CONSTRAINT_VIOLATION);
findEntriesMethod.copyAdditionsTo(builder, governorTypeDetails);
persistMethod.copyAdditionsTo(builder, governorTypeDetails);
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final String dataField = getDataFieldName().getSymbolName();
bodyBuilder.appendFormalLine("int from = 0;");
bodyBuilder.appendFormalLine("int to = " + quantity + ";");
bodyBuilder.appendFormalLine(dataField + " = "
+ findEntriesMethod.getMethodCall() + ";");
bodyBuilder.appendFormalLine("if (" + dataField + " == null) {");
bodyBuilder.indent();
bodyBuilder
.appendFormalLine("throw new IllegalStateException(\"Find entries implementation for '"
+ entity.getSimpleTypeName()
+ "' illegally returned null\");");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("if (!" + dataField + ".isEmpty()) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.appendFormalLine("");
bodyBuilder.appendFormalLine(dataField + " = new ArrayList<"
+ entity.getSimpleTypeName() + ">();");
bodyBuilder.appendFormalLine("for (int i = 0; i < " + quantity
+ "; i++) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(entity.getSimpleTypeName() + " " + OBJ_VAR
+ " = "
+ newTransientEntityMethod.getMethodName().getSymbolName()
+ "(i);");
bodyBuilder.appendFormalLine("try {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine(persistMethod.getMethodCall() + ";");
bodyBuilder.indentRemove();
bodyBuilder
.appendFormalLine("} catch (final ConstraintViolationException e) {");
bodyBuilder.indent();
bodyBuilder
.appendFormalLine("final StringBuilder msg = new StringBuilder();");
bodyBuilder
.appendFormalLine("for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {");
bodyBuilder.indent();
bodyBuilder
.appendFormalLine("final ConstraintViolation<?> cv = iter.next();");
bodyBuilder
.appendFormalLine("msg.append(\"[\").append(cv.getRootBean().getClass().getName()).append(\".\").append(cv.getPropertyPath()).append(\": \").append(cv.getMessage()).append(\" (invalid value = \").append(cv.getInvalidValue()).append(\")\").append(\"]\");");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder
.appendFormalLine("throw new IllegalStateException(msg.toString(), e);");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
if (flushMethod != null) {
bodyBuilder.appendFormalLine(flushMethod.getMethodCall() + ";");
flushMethod.copyAdditionsTo(builder, governorTypeDetails);
}
bodyBuilder.appendFormalLine(dataField + ".add(" + OBJ_VAR + ");");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// Create the method
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName,
returnType,
AnnotatedJavaType.convertFromJavaTypes(parameterTypes),
parameterNames, bodyBuilder);
}