final String body = method.getBody(lowerLayerCallAdditions);
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine(body);
List<JavaSymbolName> parameterNames = method
.getParameterNames(domainType, idType);
MethodMetadataBuilder methodMetadataBuilder = new MethodMetadataBuilder(
getId(), Modifier.PUBLIC, methodName,
method.getReturnType(domainType),
AnnotatedJavaType.convertFromJavaTypes(method
.getParameterTypes(domainType, idType)),
parameterNames, bodyBuilder);
boolean isCreateOrUpdateMethod = false;
boolean isReadMethod = false;
boolean isDeleteMethod = false;
// checks to see if the method is a "save" method
if (method.getKey().equals(
CustomDataKeys.PERSIST_METHOD.name())
|| method.getKey().equals(
CustomDataKeys.MERGE_METHOD.name())) {
isCreateOrUpdateMethod = true;
}
// Checks to see if the method is a "delete method
if (method.getKey().equals(
CustomDataKeys.REMOVE_METHOD.name())) {
isDeleteMethod = true;
}
// Checks to see if the method is a "read" method
if (method.getKey().equals(
CustomDataKeys.FIND_ALL_METHOD.name())
|| method.getKey().equals(
CustomDataKeys.FIND_ENTRIES_METHOD.name())
|| method.getKey().equals(
CustomDataKeys.FIND_METHOD.name())
|| method.getKey().equals(
CustomDataKeys.COUNT_ALL_METHOD)) {
isReadMethod = true;
}
String authorizeValue = "";
String authorizedRolesComponent = "";
String permissionEvalutorComponent = "";
// Adds required roles to @PreAuthorize or @PostAuthorize annotation if the
// required roles for persist methods
if (serviceAnnotationValues.getAuthorizedCreateOrUpdateRoles() != null &&
serviceAnnotationValues.getAuthorizedCreateOrUpdateRoles().length > 0
&& isCreateOrUpdateMethod) {
authorizedRolesComponent = getRoles(serviceAnnotationValues.getAuthorizedCreateOrUpdateRoles());
}
// Adds required roles to @PreAuthorize or @PostAuthorize annotation if the
// required roles exist for read methods
if (serviceAnnotationValues.getAuthorizedReadRoles() != null &&
serviceAnnotationValues.getAuthorizedReadRoles().length > 0
&& isReadMethod) {
authorizedRolesComponent = getRoles(serviceAnnotationValues.getAuthorizedReadRoles());
}
// Adds required roles to @PreAuthorize or @PostAuthorize annotation if the
// required roles exist for delete methods
if (serviceAnnotationValues.getAuthorizedDeleteRoles() != null &&
serviceAnnotationValues.getAuthorizedDeleteRoles().length > 0
&& isDeleteMethod) {
authorizedRolesComponent = getRoles(serviceAnnotationValues.getAuthorizedDeleteRoles());
}
final String permissionName = method.getPermissionName(domainType,
domainTypePlurals.get(domainType));
if (permissionName != null && serviceAnnotationValues.usePermissionEvaluator()) {
// Add hasPermission to @PreAuthorize or @PostAuthorize annotation if
// required
permissionEvalutorComponent = String.format("hasPermission(%s, '%s')", method.usesPostAuthorize() ? "returnObject" : "#" + parameterNames.get(0).getSymbolName(), permissionName);
}
// Builds value for @PreAuthorize
if (!authorizedRolesComponent.equals("") && !permissionEvalutorComponent.equals("")) {
authorizeValue= String.format("isAuthenticated() AND ((%s) OR %s)", authorizedRolesComponent, permissionEvalutorComponent);
}
else if (!authorizedRolesComponent.equals("")) {
authorizeValue= String.format("isAuthenticated() AND (%s)", authorizedRolesComponent);
}
else if (!permissionEvalutorComponent.equals("")) {
authorizeValue= String.format("isAuthenticated() AND %s", permissionEvalutorComponent);
}
else if (serviceAnnotationValues.requireAuthentication()) {
authorizeValue ="isAuthenticated()";
}
if (!authorizeValue.equals("")) {
final AnnotationMetadataBuilder annotationMetadataBuilder = new AnnotationMetadataBuilder(method.usesPostAuthorize() ? SpringJavaType.POST_AUTHORIZE : PRE_AUTHORIZE);
annotationMetadataBuilder.addStringAttribute("value",
authorizeValue.toString());
methodMetadataBuilder
.addAnnotation(annotationMetadataBuilder
.build());
}
builder.addMethod(methodMetadataBuilder);