Skip to main content

Groovy Script - Set Private Comments by User Role

Description: Sample Groovy Script to set private comments by user role.

Context: Document

Document Type: File

Remark: Accessible in 'On Load' task mode only.

Script:

def execute(){
//************************************ Configurable Parameter Section *************************************
String roleName = "Lead Reviewer,Field Contractor"; // Here you can give multiple role by comma (,) seperated
//************************************ Configurable Parameter Section *************************************
DocumentVO document = documentService.getDocumentDetails();
String currentUserEmail = documentService.getCurrentUserDetails().getEmail();
boolean hasRole = isUserPartOfGivenRole(roleName, currentUserEmail);
JsonObject responseJSON = new JsonObject();
if(hasRole){
responseJSON.add(IGroovyConstant.MARK_AS_PRIVATE, addAttributes("true", "false")); // Param: Editable true/false, Mark As Private true/false
}else{
responseJSON.add(IGroovyConstant.MARK_AS_PRIVATE, addAttributes("false", "true")); // Param: Editable true/false, Mark As Private true/false
}
JsonObject distributionList = new JsonObject();
responseJSON.add(IGroovyConstant.DISTRIBUTION_LIST, distributionList);
document.setResponseStatus(false);
document.setDynamicObject(responseJSON.toString());
return document;
}
public JsonElement addAttributes(String editable, String defaultValue) {
JsonElement jsonObject = new JsonObject();
jsonObject.addProperty(IGroovyConstant.EDITABLE, editable);
jsonObject.addProperty(IGroovyConstant.VALUE, defaultValue);
return jsonObject;
}
private boolean isUserPartOfGivenRole(String roleName, String userEmail){
List<String> distributionRoleList = new ArrayList<String>();
List<String> finalDistributionRoleList = new ArrayList<String>();
if(roleName != null && !roleName.isEmpty() && roleName.contains(",")){
distributionRoleList = roleName.split(",").toList()
} else{
distributionRoleList.add(roleName);
}
distributionRoleList.each {String it->
it = it.trim();
finalDistributionRoleList.add(it)
}
boolean hasRole = false;
Map<String, List<String>> distributionMap = new HashMap();
distributionMap.put(IGroovyConstant.DISTRIBUTION_ROLES, finalDistributionRoleList);
Map<String, Map<String, List<String>>> RecipientUsersEmailIds = documentService.getUsersEmailIdByGroupOrRole(distributionMap);
Map<String,List<String>> RolesEmailMap = RecipientUsersEmailIds.get(IGroovyConstant.DISTRIBUTION_ROLES);
for(Map.Entry<String, List<String>> roleMap :RolesEmailMap.entrySet()){
if(roleMap.getValue().contains(userEmail)){
hasRole = true;
break;
}
}
return hasRole;
}
Did this answer your question?