Skip to content

Instantly share code, notes, and snippets.

@14paxton
Created April 28, 2022 21:30
Show Gist options
  • Save 14paxton/1fa8f703b708b9488408c9217a83b3a9 to your computer and use it in GitHub Desktop.
Save 14paxton/1fa8f703b708b9488408c9217a83b3a9 to your computer and use it in GitHub Desktop.
def constructAttachmentMap(params, assessmentOrder) {
try {
def selectedAttachmentList = DocumentType.
getEnumConstants()*.
getKey().
findAll { params[it] == 'on' || params[it] == 'true' }
if (selectedAttachmentList) {
def authToken = userService.myToken().accessToken
def availableAttachmentList = getAttachmentDownloadList(assessmentOrder, authToken)
def documentObjects = availableAttachmentList.findAll { selectedAttachmentList.contains(it?.context) }
return documentObjects.collect {
getAttachments(it, authToken)
}
}
}
catch (Exception e) {
log.error("Error getting documents to attach for ${params?.assessmentResultId}", e)
}
return null
}
def getAttachmentDownloadList(assessmentOrder, authToken) {
def availableDocuments = []
if (assessmentOrder && authToken) {
def uriResource = config.externalResource.documents.documentListURI
def uri = uriResource.replace("@@clientSetupId@@", "${assessmentOrder?.clientSetupId}")
def httpResponse = awsDocumentHTTPBuilder(authToken, uri, [type: 'order', objectId:
assessmentOrder?.
id, max: 200])
if (httpResponse?.pass) {
log.debug("retrieved ${httpResponse?.jsonData?.total} documents for assessment ${assessmentOrder?.id}")
availableDocuments = httpResponse?.jsonData?.data
}
else {
log.debug "Failed to get documents for assessment order id ${assessmentOrder.id}"
log.error "${httpResponse?.response?.statusLine}"
log.debug "Response json: ${httpResponse?.jsonData}"
def headersString
httpResponse?.response?.headers?.each {
headersString += "${it.name} : ${it.value}"
}
log.error "HTTP Response Headers ${headersString}"
}
}
return availableDocuments
}
def getAttachments(documentObject, authToken) {
def uri = StringUtils.replaceEach(config.externalResource.documents.documentDownloadUrlURI,
['@@clientSetupId@@', "@@documentId@@"] as String[],
["${documentObject?.clientSetupId}","${documentObject?.id}"] as String[])
def httpResponse = awsDocumentHTTPBuilder(authToken, uri)
if (httpResponse?.pass) {
log.debug("retrieved download url for ${documentObject?.originalFileName}")
return [name: "${documentObject?.originalFileName}", downloadUrl: httpResponse?.jsonData?.downloadUrl]
}
else {
log.debug "Failed to get download url for ${documentObject?.originalFileName}"
log.error "${httpResponse?.response?.statusLine}"
log.debug "Response json: ${httpResponse?.jsonData}"
def headersString
httpResponse?.response?.headers?.each {
headersString += "${it.name} : ${it.value}"
}
log.error "HTTP Response Headers ${headersString}"
return null
}
}
def awsDocumentHTTPBuilder(authToken, uriString, def uriParameterMap = null) {
def result = [pass: false, response: '', jsonData: '']
def target = config.externalResource.documents.url
def builder = new HTTPBuilder(target)
CoreUtils.acceptUnsecureCert(builder)
builder.request(Method.GET) {
headers['Authorization'] = "Bearer ${authToken}"
requestContentType = ContentType.ANY
headers.Accept = 'application/octet-stream, application/json, text/plain, */*'
uri.path = uriString
uri.query = uriParameterMap
log.info "Sending documents request to uri:${builder?.uri}, path: ${uriString}, queryparams: ${uriParameterMap}"
response.success = { resp, json ->
log.info "Documents Response status from API call : ${resp.responseBase} "
result.pass = true
result.response = resp
result.jsonData = json
}
response.failure = { resp, json ->
result.pass = false
result.response = resp
result.jsonData = json
log.error "Failed in awsDocumentHTTPBuilder to fetch documents ${resp?.responseBase}"
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment