Skip to content

Commit a53baf2

Browse files
committed
Support form versioning for download #1025
1 parent fa892cc commit a53baf2

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

grails-app/controllers/au/org/ala/ecodata/MetadataController.groovy

+48-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MetadataController {
1212
static responseFormats = ['json']
1313

1414
def metadataService, activityService, commonService, projectService, webService
15-
15+
ActivityFormService activityFormService
1616
def activitiesModel() {
1717
render metadataService.activitiesModel()
1818
}
@@ -72,7 +72,21 @@ class MetadataController {
7272
render result as JSON
7373
return null
7474
}
75-
def annotatedModel = metadataService.annotatedOutputDataModel(outputName)
75+
String activityForm = params.activityForm
76+
Integer formVersion = params.getInt('formVersion', null)
77+
def annotatedModel
78+
if (activityForm) {
79+
ActivityForm form = activityFormService.findActivityForm(activityForm, formVersion)
80+
if (!form) {
81+
def result = [status:400, error:'No form with name '+activityForm+' and version '+formVersion+' was found']
82+
render result as JSON
83+
return null
84+
}
85+
annotatedModel = form.getFormSection(outputName).annotatedTemplate()
86+
}
87+
else {
88+
annotatedModel = metadataService.annotatedOutputDataModel(outputName)
89+
}
7690

7791
if (!annotatedModel) {
7892
def result = [status:404, error:"No output of type ${outputName} exists"]
@@ -83,23 +97,48 @@ class MetadataController {
8397
render annotatedModel as JSON
8498
}
8599

100+
private Map getModelAndAnnotatedModel(String outputName, String activityFormName, Integer activityFormVersion, def expandList) {
101+
List annotatedModel
102+
def model
103+
if (activityFormName) {
104+
ActivityForm form = activityFormService.findActivityForm(activityFormName, activityFormVersion)
105+
model = form?.sections?.find{it.name == outputName}
106+
OutputMetadata metadata = new OutputMetadata(model?.template)
107+
annotatedModel = metadata.annotateDataModel()
108+
}
109+
else {
110+
// Legacy support
111+
model = metadataService.getOutputDataModel(outputName)
112+
if (expandList && expandList == 'true') {
113+
annotatedModel = metadataService.annotatedOutputDataModel(outputName, true)
114+
} else {
115+
annotatedModel = metadataService.annotatedOutputDataModel(outputName)
116+
}
117+
}
118+
return [model:model, annotatedModel:annotatedModel]
119+
}
120+
86121
/**
87122
* Returns an Excel template that can be populated with output data and uploaded.
88123
*/
89124
def excelOutputTemplate() {
90125

91126
def outputName, listName, data, expandList
92127
boolean editMode, allowExtraRows, autosizeColumns, includeDataPathHeader
128+
String activityForm
129+
Integer formVersion
93130
def json = request.getJSON()
94131
if (json) {
132+
activityForm = json.activityForm
133+
formVersion = json.formVersion
95134
outputName = json.type
96135
listName = json.listName
97136
editMode = Boolean.valueOf(json.editMode)
98137
allowExtraRows = Boolean.valueOf(json.allowExtraRows)
99138
autosizeColumns = json.autosizeColumns != null ? Boolean.valueOf(json.autosizeColumns) : true
100139
includeDataPathHeader = json.includeDataPathHeader != null ? Boolean.valueOf(json.includeDataPathHeader) : false
101-
data = JSON.parse(json.data)
102-
140+
data = json.data ? JSON.parse(json.data) : null
141+
expandList = json.expandList
103142

104143
}
105144
else {
@@ -110,6 +149,8 @@ class MetadataController {
110149
allowExtraRows = params.getBoolean('allowExtraRows', false)
111150
autosizeColumns = params.getBoolean('autosizeColumns', true)
112151
includeDataPathHeader = params.getBoolean('includeDataPathHeader', false)
152+
activityForm = params.activityForm
153+
formVersion = params.getInt('formVersion', null)
113154
}
114155

115156

@@ -119,13 +160,9 @@ class MetadataController {
119160
return null
120161
}
121162

122-
Map model = metadataService.getOutputDataModelByName(outputName)
123-
def annotatedModel = null
124-
if (expandList && expandList == 'true') {
125-
annotatedModel = metadataService.annotatedOutputDataModel(outputName, true)
126-
} else {
127-
annotatedModel = metadataService.annotatedOutputDataModel(outputName)
128-
}
163+
Map modelAndAnnotatedModel = getModelAndAnnotatedModel(outputName, activityForm, formVersion, expandList)
164+
def model = modelAndAnnotatedModel.model
165+
List annotatedModel = modelAndAnnotatedModel.annotatedModel
129166
if (!annotatedModel) {
130167
def result = [status:404, error:"No output of type ${outputName} exists"]
131168
render result as JSON
@@ -173,9 +210,6 @@ class MetadataController {
173210

174211
builder.save(response.outputStream)
175212

176-
// response.getOutputStream().flush();
177-
// response.getOutputStream().close();
178-
179213
}
180214

181215
/**

grails-app/domain/au/org/ala/ecodata/ActivityForm.groovy

-2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,4 @@ class ActivityForm {
145145
status != Status.DELETED
146146
}.find()
147147
}
148-
149-
150148
}

grails-app/domain/au/org/ala/ecodata/FormSection.groovy

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package au.org.ala.ecodata
22

33
import au.org.ala.ecodata.graphql.mappers.FormSectionGraphQLMapper
44
import au.org.ala.ecodata.graphql.models.SectionTemplate
5+
import au.org.ala.ecodata.metadata.OutputMetadata
56

67
class FormSection {
78

@@ -43,4 +44,9 @@ class FormSection {
4344
return outputData
4445
}
4546

47+
List annotatedTemplate() {
48+
OutputMetadata metadata = new OutputMetadata(template)
49+
return metadata.annotateDataModel()
50+
}
51+
4652
}

0 commit comments

Comments
 (0)