Skip to content

Instantly share code, notes, and snippets.

@14paxton
Created September 16, 2022 16:34
Show Gist options
  • Save 14paxton/ef4f6e91fa7fa44015c41f26a1caf3ae to your computer and use it in GitHub Desktop.
Save 14paxton/ef4f6e91fa7fa44015c41f26a1caf3ae to your computer and use it in GitHub Desktop.
static Map<String, RoleGroup> formatRoleGroupsToMap(List<RoleGroup> roleGroupList){
roleGroupList.collectEntries {
[it.displayName.trim().toUpperCase().replaceAll(~/\s/, "_"), it]
}
}
//get all available RoleGroups minus the admin groups if condition is not true
static List<RoleGroup> availableRoleGroups(boolean allowAddingAdmin = false){
def removeAdminList = allowAddingAdmin ? " " : RESTRICTED_ADMIN_ROLE_GROUPS.groupName
RoleGroup.withTransaction {
RoleGroup.where { not { 'in'('name', removeAdminList) } }.list()
}
}
//create map with parsed value and original value
static Map<String, String> parseStringParmToMap(String parameter) {
Map<String, String> rgRequestMap = [:]
List paramList = parameter.split(/[-.,\/]+/)
paramList?.removeAll {it == null || it?.isEmpty()}
Closure transform = { value -> value.trim().toUpperCase().replaceAll(~/\s/, "") }
Closure assignToMap = { value, index ->
def ugKey = transform(value)
if (rgRequestMap.containsKey(ugKey)) {
rgRequestMap[("DUPLICATE_${ugKey}${index}")] = value
}
else {
rgRequestMap[(ugKey)] = value
}
}
paramList.eachWithIndex(assignToMap)
return rgRequestMap
}
//use retrieved RoleGroups the create a list from the requested role groups
//put groups without a match and duplicates in their own list
static ArrayList<List> getRequestedRoleGroups(String requestedRG, boolean allowAddingAdmin = false) {
def restrictedGroups = allowAddingAdmin ? [] : RESTRICTED_ADMIN_ROLE_GROUPS.requestFormat
List returnRGList = []
List duplicates = []
List unmatchedGroups = []
List requestedAdminGroups = []
List<RoleGroup> roleGroupList = availableRoleGroups(allowAddingAdmin)
Map<String, RoleGroup> roleGroupMap = formatRoleGroupsToMap(roleGroupList)
Map<String, String> originalValueMap = parseStringParmToMap(requestedRG)
withPool {
Map<String, Closure> closureMap = [:]
closureMap['RETURNLIST'] = { value -> returnRGList << value }
closureMap['DUPLICATE'] = { value -> duplicates << value }
closureMap['UNMATCHED'] = { value ->
def rg = originalValueMap.get(value)
if (rg in restrictedGroups) {
requestedAdminGroups << rg
return
}
unmatchedGroups << rg
}
Closure addGroupToList = { string ->
String closure = (string ==~ /DUPLICATE_.+/) ? 'DUPLICATE' : null
if (!closure) {
def rg = roleGroupMap.get(string)
closure = rg ? 'DUPLICATE' : 'UNMATCHED'
if (rg && !returnRGList.contains(rg)) {
closureMap['RETURNLIST'].callAsync(rg)
return
}
}
closureMap[closure].callAsync(string)
}
originalValueMap.keySet().each(addGroupToList)
}
[[returnRGList: returnRGList], [unmatchedGroups: unmatchedGroups], [duplicates : duplicates], [requestedAdminGroups: requestedAdminGroups] , [originalValueMap : originalValueMap]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment