List<JavaType> hasPermissionParameterTypes = new ArrayList<JavaType>();
hasPermissionParameterTypes.add(AUTHENTICATION);
hasPermissionParameterTypes.add(JavaType.OBJECT);
hasPermissionParameterTypes.add(JavaType.OBJECT);
JavaSymbolName hasPermissionMethodName = new JavaSymbolName("hasPermission");
if (!governorDetails.isMethodDeclaredByAnother(hasPermissionMethodName,
hasPermissionParameterTypes, getId())) {
List<JavaSymbolName> hasPermissionParameterNames = new ArrayList<JavaSymbolName>();
hasPermissionParameterNames.add(new JavaSymbolName("authentication"));
hasPermissionParameterNames.add(new JavaSymbolName("targetObject"));
hasPermissionParameterNames.add(new JavaSymbolName("permission"));
final InvocableMemberBodyBuilder hasPermissionBodyBuilder = new InvocableMemberBodyBuilder();
hasPermissionBodyBuilder.append("\n\t\treturn checkManagedPermissions(authentication, targetObject, permission);");
MethodMetadataBuilder hasPermissionMethodMetadataBuilder = new MethodMetadataBuilder(
getId(), PUBLIC, hasPermissionMethodName, JavaType.BOOLEAN_PRIMITIVE,
AnnotatedJavaType
.convertFromJavaTypes(hasPermissionParameterTypes),
hasPermissionParameterNames, hasPermissionBodyBuilder);
builder.addMethod(hasPermissionMethodMetadataBuilder.build());
}
//Creates method hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission)
hasPermissionParameterTypes = new ArrayList<JavaType>();
hasPermissionParameterTypes.add(AUTHENTICATION);
hasPermissionParameterTypes.add(JavaType.SERIALIZABLE);
hasPermissionParameterTypes.add(JavaType.STRING);
hasPermissionParameterTypes.add(JavaType.OBJECT);
if (!governorDetails.isMethodDeclaredByAnother(hasPermissionMethodName,
hasPermissionParameterTypes, getId())) {
final InvocableMemberBodyBuilder hasPermissionBodyBuilder2 = new InvocableMemberBodyBuilder();
hasPermissionBodyBuilder2.append(String.format("\n\t\treturn %s;\n",annotationValues.getDefaultReturnValue()));
List<JavaSymbolName> hasPermissionParameterNames2 = new ArrayList<JavaSymbolName>();
hasPermissionParameterNames2.add(new JavaSymbolName(
"authentication"));
hasPermissionParameterNames2.add(new JavaSymbolName("targetId"));
hasPermissionParameterNames2.add(new JavaSymbolName("targetType"));
hasPermissionParameterNames2.add(new JavaSymbolName("permission"));
MethodMetadataBuilder hasPermissionMethodMetadataBuilder2 = new MethodMetadataBuilder(
getId(),
PUBLIC,
new JavaSymbolName("hasPermission"),
JavaType.BOOLEAN_PRIMITIVE,
AnnotatedJavaType
.convertFromJavaTypes(hasPermissionParameterTypes),
hasPermissionParameterNames2, hasPermissionBodyBuilder2);
builder.addMethod(hasPermissionMethodMetadataBuilder2.build());
}
List<JavaType> checkManagedPermissionsParameterTypes = new ArrayList<JavaType>();
checkManagedPermissionsParameterTypes.add(AUTHENTICATION);
checkManagedPermissionsParameterTypes.add(JavaType.OBJECT);
checkManagedPermissionsParameterTypes.add(JavaType.OBJECT);
JavaSymbolName checkManagedPermissionsMethodName = new JavaSymbolName("checkManagedPermissions");
if (!governorDetails.isMethodDeclaredByAnother(checkManagedPermissionsMethodName,
checkManagedPermissionsParameterTypes, getId())) {
List<JavaSymbolName> checkManagedPermissionsParameterNames = new ArrayList<JavaSymbolName>();
checkManagedPermissionsParameterNames
.add(new JavaSymbolName("authentication"));
checkManagedPermissionsParameterNames.add(new JavaSymbolName("targetObject"));
checkManagedPermissionsParameterNames.add(new JavaSymbolName("permission"));
final InvocableMemberBodyBuilder checkManagedPermissionsBodyBuilder = new InvocableMemberBodyBuilder();
boolean firstPass = true;
for (Entry<JavaType, String> entrySet : domainTypeToPlurals.entrySet()) {
for (Permission permission : Permission.values()){
String permissionName = permission.getName(entrySet.getKey(), entrySet.getValue());
if (permissionName == null) {
continue;
}
checkManagedPermissionsBodyBuilder.append(String.format("\n\t\t%sif(permission.equals(\"%s\")){",firstPass ? "" : "else ", permissionName));
checkManagedPermissionsBodyBuilder.append(String.format("\n\t\t\treturn %s(authentication, (%s)targetObject);", permissionName, entrySet.getKey().getFullyQualifiedTypeName()));
checkManagedPermissionsBodyBuilder.append("\n\t\t}");
}
firstPass = false;
}
checkManagedPermissionsBodyBuilder.append(String.format("\n\t\treturn %s;\n",annotationValues.getDefaultReturnValue()));
MethodMetadataBuilder hasPermissionMethodMetadataBuilder = new MethodMetadataBuilder(
getId(), PUBLIC, checkManagedPermissionsMethodName, JavaType.BOOLEAN_PRIMITIVE,
AnnotatedJavaType
.convertFromJavaTypes(checkManagedPermissionsParameterTypes),
checkManagedPermissionsParameterNames, checkManagedPermissionsBodyBuilder);
builder.addMethod(hasPermissionMethodMetadataBuilder.build());
}
for (Entry<JavaType, String> entrySet : domainTypeToPlurals.entrySet()) {
for (Permission permission : Permission.values()){
String permissionName = permission.getName(entrySet.getKey(), entrySet.getValue());
if (permissionName == null) {
continue;
}
JavaSymbolName isAllowedMethodName = new JavaSymbolName(permissionName);
List<JavaType> isAllowedParameterTypes = new ArrayList<JavaType>();
isAllowedParameterTypes.add(AUTHENTICATION);
isAllowedParameterTypes.add(entrySet.getKey());
if (!governorDetails.isMethodDeclaredByAnother(isAllowedMethodName, isAllowedParameterTypes, getId())) {
List<JavaSymbolName> isAllowedParameterNames = new ArrayList<JavaSymbolName>();
isAllowedParameterNames.add(new JavaSymbolName("authentication"));
isAllowedParameterNames.add(JavaSymbolName.getReservedWordSafeName(entrySet.getKey()));
final InvocableMemberBodyBuilder isAllowedBodyBuilder = new InvocableMemberBodyBuilder();
isAllowedBodyBuilder.append(String.format("\n\t\treturn %s;\n",annotationValues.getDefaultReturnValue()));