From a53baf249ab90571cb6425cc814fccb7a256c993 Mon Sep 17 00:00:00 2001 From: chrisala Date: Thu, 19 Dec 2024 13:28:01 +1100 Subject: [PATCH 1/2] Support form versioning for download #1025 --- .../org/ala/ecodata/MetadataController.groovy | 62 ++++++++++++++----- .../au/org/ala/ecodata/ActivityForm.groovy | 2 - .../au/org/ala/ecodata/FormSection.groovy | 6 ++ 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy b/grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy index 1910fd78f..f886a7cdb 100644 --- a/grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy +++ b/grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy @@ -12,7 +12,7 @@ class MetadataController { static responseFormats = ['json'] def metadataService, activityService, commonService, projectService, webService - + ActivityFormService activityFormService def activitiesModel() { render metadataService.activitiesModel() } @@ -72,7 +72,21 @@ class MetadataController { render result as JSON return null } - def annotatedModel = metadataService.annotatedOutputDataModel(outputName) + String activityForm = params.activityForm + Integer formVersion = params.getInt('formVersion', null) + def annotatedModel + if (activityForm) { + ActivityForm form = activityFormService.findActivityForm(activityForm, formVersion) + if (!form) { + def result = [status:400, error:'No form with name '+activityForm+' and version '+formVersion+' was found'] + render result as JSON + return null + } + annotatedModel = form.getFormSection(outputName).annotatedTemplate() + } + else { + annotatedModel = metadataService.annotatedOutputDataModel(outputName) + } if (!annotatedModel) { def result = [status:404, error:"No output of type ${outputName} exists"] @@ -83,6 +97,27 @@ class MetadataController { render annotatedModel as JSON } + private Map getModelAndAnnotatedModel(String outputName, String activityFormName, Integer activityFormVersion, def expandList) { + List annotatedModel + def model + if (activityFormName) { + ActivityForm form = activityFormService.findActivityForm(activityFormName, activityFormVersion) + model = form?.sections?.find{it.name == outputName} + OutputMetadata metadata = new OutputMetadata(model?.template) + annotatedModel = metadata.annotateDataModel() + } + else { + // Legacy support + model = metadataService.getOutputDataModel(outputName) + if (expandList && expandList == 'true') { + annotatedModel = metadataService.annotatedOutputDataModel(outputName, true) + } else { + annotatedModel = metadataService.annotatedOutputDataModel(outputName) + } + } + return [model:model, annotatedModel:annotatedModel] + } + /** * Returns an Excel template that can be populated with output data and uploaded. */ @@ -90,16 +125,20 @@ class MetadataController { def outputName, listName, data, expandList boolean editMode, allowExtraRows, autosizeColumns, includeDataPathHeader + String activityForm + Integer formVersion def json = request.getJSON() if (json) { + activityForm = json.activityForm + formVersion = json.formVersion outputName = json.type listName = json.listName editMode = Boolean.valueOf(json.editMode) allowExtraRows = Boolean.valueOf(json.allowExtraRows) autosizeColumns = json.autosizeColumns != null ? Boolean.valueOf(json.autosizeColumns) : true includeDataPathHeader = json.includeDataPathHeader != null ? Boolean.valueOf(json.includeDataPathHeader) : false - data = JSON.parse(json.data) - + data = json.data ? JSON.parse(json.data) : null + expandList = json.expandList } else { @@ -110,6 +149,8 @@ class MetadataController { allowExtraRows = params.getBoolean('allowExtraRows', false) autosizeColumns = params.getBoolean('autosizeColumns', true) includeDataPathHeader = params.getBoolean('includeDataPathHeader', false) + activityForm = params.activityForm + formVersion = params.getInt('formVersion', null) } @@ -119,13 +160,9 @@ class MetadataController { return null } - Map model = metadataService.getOutputDataModelByName(outputName) - def annotatedModel = null - if (expandList && expandList == 'true') { - annotatedModel = metadataService.annotatedOutputDataModel(outputName, true) - } else { - annotatedModel = metadataService.annotatedOutputDataModel(outputName) - } + Map modelAndAnnotatedModel = getModelAndAnnotatedModel(outputName, activityForm, formVersion, expandList) + def model = modelAndAnnotatedModel.model + List annotatedModel = modelAndAnnotatedModel.annotatedModel if (!annotatedModel) { def result = [status:404, error:"No output of type ${outputName} exists"] render result as JSON @@ -173,9 +210,6 @@ class MetadataController { builder.save(response.outputStream) - // response.getOutputStream().flush(); - // response.getOutputStream().close(); - } /** diff --git a/grails-app/domain/au/org/ala/ecodata/ActivityForm.groovy b/grails-app/domain/au/org/ala/ecodata/ActivityForm.groovy index e49637f11..a4f5a93bc 100644 --- a/grails-app/domain/au/org/ala/ecodata/ActivityForm.groovy +++ b/grails-app/domain/au/org/ala/ecodata/ActivityForm.groovy @@ -145,6 +145,4 @@ class ActivityForm { status != Status.DELETED }.find() } - - } diff --git a/grails-app/domain/au/org/ala/ecodata/FormSection.groovy b/grails-app/domain/au/org/ala/ecodata/FormSection.groovy index aa675c442..46e589105 100644 --- a/grails-app/domain/au/org/ala/ecodata/FormSection.groovy +++ b/grails-app/domain/au/org/ala/ecodata/FormSection.groovy @@ -2,6 +2,7 @@ package au.org.ala.ecodata import au.org.ala.ecodata.graphql.mappers.FormSectionGraphQLMapper import au.org.ala.ecodata.graphql.models.SectionTemplate +import au.org.ala.ecodata.metadata.OutputMetadata class FormSection { @@ -43,4 +44,9 @@ class FormSection { return outputData } + List annotatedTemplate() { + OutputMetadata metadata = new OutputMetadata(template) + return metadata.annotateDataModel() + } + } From e704291729eed09ac67d46410ac996710ac045e4 Mon Sep 17 00:00:00 2001 From: chrisala Date: Fri, 20 Dec 2024 08:13:48 +1100 Subject: [PATCH 2/2] Fixed tests #1025 --- .../controllers/au/org/ala/ecodata/MetadataController.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy b/grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy index f886a7cdb..165ac5f81 100644 --- a/grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy +++ b/grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy @@ -108,7 +108,7 @@ class MetadataController { } else { // Legacy support - model = metadataService.getOutputDataModel(outputName) + model = metadataService.getOutputDataModelByName(outputName) if (expandList && expandList == 'true') { annotatedModel = metadataService.annotatedOutputDataModel(outputName, true) } else {