Skip to content

Commit

Permalink
Merge pull request #1050 from AtlasOfLivingAustralia/feature/issue1025
Browse files Browse the repository at this point in the history
Support form versioning for download #1025
  • Loading branch information
salomon-j authored Dec 19, 2024
2 parents fa892cc + e704291 commit 8fab86a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
62 changes: 48 additions & 14 deletions grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MetadataController {
static responseFormats = ['json']

def metadataService, activityService, commonService, projectService, webService

ActivityFormService activityFormService
def activitiesModel() {
render metadataService.activitiesModel()
}
Expand Down Expand Up @@ -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"]
Expand All @@ -83,23 +97,48 @@ 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.getOutputDataModelByName(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.
*/
def excelOutputTemplate() {

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 {
Expand All @@ -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)
}


Expand All @@ -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
Expand Down Expand Up @@ -173,9 +210,6 @@ class MetadataController {

builder.save(response.outputStream)

// response.getOutputStream().flush();
// response.getOutputStream().close();

}

/**
Expand Down
2 changes: 0 additions & 2 deletions grails-app/domain/au/org/ala/ecodata/ActivityForm.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,4 @@ class ActivityForm {
status != Status.DELETED
}.find()
}


}
6 changes: 6 additions & 0 deletions grails-app/domain/au/org/ala/ecodata/FormSection.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -43,4 +44,9 @@ class FormSection {
return outputData
}

List annotatedTemplate() {
OutputMetadata metadata = new OutputMetadata(template)
return metadata.annotateDataModel()
}

}

0 comments on commit 8fab86a

Please sign in to comment.