Skip to content

Commit

Permalink
Merge pull request #62 from samply/bugfix/query-in-template
Browse files Browse the repository at this point in the history
Bugfix/query in template
  • Loading branch information
djuarezgf authored Dec 3, 2023
2 parents 17a0aea + a774f4f commit bb673d3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Apt-get clean in docker
- Tomcat max-http-header-size
- Request Template
- Query in body for CREATE_QUERY

### Changed
- Move FHIR Packages to dktk-exporter
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/de/samply/core/ExporterCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private Query checkParametersAndFetchQuery(ExporterParameters exporterParameters
query = queryOptional.get();
}
} else {
if (exporterParameters.query() != null || template.getQuery() != null){
if (exporterParameters.query() != null || template.getQuery() != null) {
query = createNewQuery(exporterParameters, template);
} else {
errors.addError("Query not defined");
Expand All @@ -88,9 +88,9 @@ private Query checkParametersAndFetchQuery(ExporterParameters exporterParameters
private Query createNewQuery(ExporterParameters exporterParameters, RequestTemplate template) {
Query query = new Query();
String sQuery = null;
if (exporterParameters.query() != null){
if (exporterParameters.query() != null) {
sQuery = exporterParameters.query();
} else if (template != null && template.getQuery() != null){
} else if (template != null && template.getQuery() != null) {
sQuery = template.getQuery();
}
query.setQuery(sQuery);
Expand All @@ -111,19 +111,19 @@ private Query createNewQuery(ExporterParameters exporterParameters, RequestTempl
private RequestTemplate checkParametersAndFetchRequestTemplate(ExporterParameters exporterParameters,
Errors errors) {
RequestTemplate result = null;
ConverterTemplate converterTemplate = null;
if (exporterParameters.templateId() != null) {
ConverterTemplate converterTemplate = converterTemplateManager.getConverterTemplate(exporterParameters.templateId());
if (result == null) {
converterTemplate = converterTemplateManager.getConverterTemplate(exporterParameters.templateId());
if (converterTemplate == null) {
errors.addError("Converter Template " + exporterParameters.templateId() + " not found");
} else {
result = new RequestTemplate();
result.setConverterTemplate(converterTemplate);
result.setQuery(exporterParameters.query());
}
}
if (exporterParameters.templateId() == null || (exporterParameters.query() == null && exporterParameters.queryId() == null)){
if (exporterParameters.templateId() == null || (exporterParameters.query() == null && exporterParameters.queryId() == null)) {
boolean fetchTemplate = true;
if (exporterParameters.requestTemplate() == null) {
if ((result == null || result.getConverterTemplate() == null) && exporterParameters.requestTemplate() == null) {
errors.addError("Request Template not defined");
fetchTemplate = false;
}
Expand All @@ -137,9 +137,12 @@ private RequestTemplate checkParametersAndFetchRequestTemplate(ExporterParameter
}
if (fetchTemplate) {
result = fetchRequestTemplate(exporterParameters, errors);
if (converterTemplate != null) {
result.setConverterTemplate(converterTemplate); // Override template if it is already defined
}
}
}
if (result.getConverterTemplate() == null){
if (result.getConverterTemplate() == null) {
errors.addError("Converter Template not defined");
}
return result;
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/de/samply/exporter/ExporterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import de.samply.merger.FilesMergerManager;
import de.samply.template.ConverterTemplate;
import de.samply.template.ConverterTemplateManager;
import de.samply.template.RequestTemplate;
import de.samply.template.graph.ConverterGraph;
import de.samply.template.graph.factory.ConverterTemplateGraphFactoryManager;
import de.samply.template.token.TokenContext;
Expand Down Expand Up @@ -97,17 +98,36 @@ public ResponseEntity<String> info() {
}

@PostMapping(value = ExporterConst.CREATE_QUERY, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> createQuery(@RequestParam(name = ExporterConst.QUERY) String query,
public ResponseEntity<String> createQuery(@RequestParam(name = ExporterConst.QUERY, required = false) String query, // It can also be in requestTemplate
@RequestParam(name = ExporterConst.QUERY_FORMAT) Format queryFormat,
@RequestParam(name = ExporterConst.QUERY_LABEL) String queryLabel,
@RequestParam(name = ExporterConst.QUERY_DESCRIPTION) String queryDescription,
@RequestParam(name = ExporterConst.QUERY_CONTACT_ID) String queryContactId,
@RequestParam(name = ExporterConst.QUERY_DEFAULT_TEMPLATE_ID, required = false) String defaultTemplateId,
@RequestParam(name = ExporterConst.QUERY_CONTEXT, required = false) String queryContext,
@RequestParam(name = ExporterConst.QUERY_DEFAULT_OUTPUT_FORMAT, required = false) Format defaultOutputFormat,
@RequestParam(name = ExporterConst.QUERY_EXPIRATION_DATE, required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate queryExpirationDate) {
@RequestParam(name = ExporterConst.QUERY_EXPIRATION_DATE, required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate queryExpirationDate,
@RequestHeader(name = "Content-Type", required = false) String contentType,
@RequestBody(required = false) String requestTemplate) {
Query tempQuery = new Query();
tempQuery.setQuery(query);
String sQuery = null;
if (query != null){
sQuery = query;
} else if (requestTemplate != null){
if (contentType == null){
return ResponseEntity.badRequest().body("Content-Type not set");
}
try {
RequestTemplate requestTemplate1 = exporterCore.fetchRequestTemplate(requestTemplate, contentType);
sQuery = requestTemplate1.getQuery();
} catch (IOException e) {
return createInternalServerError(e);
}
}
if (sQuery == null){
return ResponseEntity.badRequest().body("Query not set");
}
tempQuery.setQuery(sQuery);
tempQuery.setFormat(queryFormat);
tempQuery.setLabel(queryLabel);
tempQuery.setDescription(queryDescription);
Expand Down

0 comments on commit bb673d3

Please sign in to comment.