Skip to content

Commit

Permalink
Implementing token-based update (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
philboeselager committed Sep 22, 2020
1 parent bf7dd06 commit bd11c56
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 29 deletions.
43 changes: 24 additions & 19 deletions grails-app/controllers/ygor/EnrichmentController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,18 @@ class EnrichmentController implements ControllersHelper{
/**
* Current Test configuration via Postman:
*
* POST /ygor/enrichment/processCompleteNoInteraction?
* POST /ygor/enrichment/processCompleteWithToken?
* addOnly=false&
* processOption=kbart,zdb,ezb&
* pkgId=<yourPackageId>&
* packageToken=<packageToken>
*
* HTTP/1.1
* Host: localhost:8092
* cache-control: no-cache
* Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
* pkgNominalPlatformId=<theIdOfThePlatformBelongingToThisPackage>&
* updateToken=<packageUpdateToken>&
* titleIdNamespace=<theNamespaceForTheTitleId>
*
* Content-Disposition: form-data; name="uploadFile"; filename="yourKBartTestFile.tsv"
*/
def processCompleteNoInteraction = {
def processCompleteWithToken(){
// create a sessionFolder
CommonsMultipartFile file = request.getFile('uploadFile')
def locale = RequestContextUtils.getLocale(request).getLanguage()
if (file == null){
Expand All @@ -328,13 +326,16 @@ class EnrichmentController implements ControllersHelper{
}
def addOnly = params.get('addOnly') // "on" or "off"
def pmOptions = params.get('processOption') // "kbart", "zdb", "ezb"
def gokbUsername = params.gokbUsername
def gokbPassword = params.gokbPassword
Enrichment enrichment = enrichmentService.enrichmentFromFile(file)
enrichment.addOnly = (addOnly.equals("on")) ? true : false
File sessionFolder = enrichmentService.getSessionFolder()
String fileName = file.originalFilename
Enrichment enrichment = new Enrichment(sessionFolder, fileName)
enrichment.addOnly = (addOnly.equals("on") || addOnly.equals("true")) ? true : false
enrichment.processingOptions = EnrichmentService.decodeApiCalls(pmOptions)

// TODO get parameters from IDs and add to pm

enrichmentService.prepareFile(enrichment, request.parameterMap)
UploadJob uploadJob = enrichmentService.processCompleteNoInteraction(enrichment, gokbUsername, gokbPassword)
UploadJob uploadJob = enrichmentService.processComplete(enrichment, null, null, false)
render(
model: [
message : watchUpload(uploadJob, Enrichment.FileType.PACKAGE, file.originalFilename)
Expand All @@ -343,12 +344,6 @@ class EnrichmentController implements ControllersHelper{
}


def processCompleteNoInteraction(Enrichment enrichment){
UploadJob uploadJob = enrichmentService.processCompleteNoInteraction(enrichment, null, null)
watchUpload(uploadJob, Enrichment.FileType.PACKAGE, enrichment.originName)
}


static String watchUpload(UploadJob uploadJob, Enrichment.FileType fileType, String fileName){
while (true){
uploadJob.updateCount()
Expand Down Expand Up @@ -534,4 +529,14 @@ class EnrichmentController implements ControllersHelper{
result.items = gokbService.getNamespaceList()
render result as JSON
}


private void addParameterToParameterMap(String parameterName, String parameterValue, Map<String, String[]> parameterMap){
if (parameterMap == null){
parameterMap = new HashMap<>()
}
String[] value = new String[1]
value[0] = parameterValue
parameterMap.put(parameterName, value)
}
}
17 changes: 17 additions & 0 deletions grails-app/domain/ygor/Enrichment.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import grails.util.Holders
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.apache.commons.lang.StringUtils
import org.springframework.web.multipart.commons.CommonsMultipartFile
import ygor.field.FieldKeyMapping
import ygor.field.MappingsContainer
import ygor.field.MultiField
Expand Down Expand Up @@ -101,6 +102,22 @@ class Enrichment{
}


Enrichment(CommonsMultipartFile commonsMultipartFile){
this.sessionFolder = sessionFolder
originName = originalFilename.replaceAll(/\s+/, '_')
originHash = FileToolkit.getMD5Hash(originName + Math.random())
originPathName = this.sessionFolder.getPath() + File.separator + originHash
resultHash = FileToolkit.getMD5Hash(originName + Math.random())
enrichmentFolder = sessionFolder.getPath() + File.separator + resultHash + File.separator
new File(enrichmentFolder).mkdirs()
mappingsContainer = new MappingsContainer()
dataContainer = new DataContainer(sessionFolder, enrichmentFolder, resultHash, mappingsContainer)
isZdbIntegrated = false
isEzbIntegrated = false
autoUpdate = false
}


def process(HashMap options, KbartReader kbartReader) throws Exception{
resultName = FileToolkit.getDateTimePrefixedFileName(originName)
ygorVersion = options.get('ygorVersion')
Expand Down
35 changes: 25 additions & 10 deletions grails-app/services/ygor/EnrichmentService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,26 @@ class EnrichmentService{
}


getNominalPlatform(String platformId){
// TODO
}


getPackage(String packageId){
// TODO
}


private def getPlatform(Map pm){
if (pm['pkgNominalPlatform'] == null){
log.error("ParameterMap missing nominalPlatform.")
return null
}
log.debug("Getting platforms for: ${pm['pkgNominalPlatform'][0]}")
def platformSplit = splitPlatformString(pm['pkgNominalPlatform'][0])
if (platformSplit == null || platformSplit.size() != 2){
log.error("Could not split platform string.")
return
return null
}
def platform = pickPlatform(platformSplit[0], platformSplit[1])
if (platform == null){
Expand Down Expand Up @@ -213,7 +227,7 @@ class EnrichmentService{

Enrichment enrichmentFromFile(CommonsMultipartFile commonsMultipartFile){
String fileName = commonsMultipartFile.originalFilename
String encoding = getEncoding(commonsMultipartFile)
String encoding = getEncoding(commonsMultipartFile.getInputStream())
if (encoding != "UTF-8"){
log.error(String.format("Transferred file has encoding %s. Aborting.", encoding))
return
Expand All @@ -231,17 +245,15 @@ class EnrichmentService{
}


UploadJob processCompleteNoInteraction(Enrichment enrichment, String gokbUsername, String gokbPassword){
processComplete(enrichment, gokbUsername, gokbPassword, false, null)
}


/**
* used by AutoUpdateService
*/
UploadJob processCompleteUpdate(Enrichment enrichment){
try{
URL originUrl = new URL(enrichment.originUrl)
kbartReader = new KbartFromUrlReader(originUrl, enrichment.sessionFolder)
enrichment.dataContainer.records = []
processComplete(enrichment, null, null, true, enrichment.dataContainer?.pkgHeader?.token)
processComplete(enrichment, null, null, true)
}
catch (Exception e){
log.error(e.getMessage())
Expand All @@ -250,8 +262,11 @@ class EnrichmentService{
}


private UploadJob processComplete(Enrichment enrichment, String gokbUsername, String gokbPassword,
boolean isUpdate, String token){
/**
* used by AutoUpdateService --> processCompleteUpdate
* used by processCompleteWithToken
*/
private UploadJob processComplete(Enrichment enrichment, String gokbUsername, String gokbPassword, boolean isUpdate){
FieldKeyMapping tippNameMapping =
enrichment.setTippPlatformNameMapping(enrichment.dataContainer?.pkgHeader?.nominalPlatform.name)
enrichment.enrollMappingToRecords(tippNameMapping)
Expand Down

0 comments on commit bd11c56

Please sign in to comment.