Implementing FreeMarker solution

This commit is contained in:
Luca Frosini 2023-04-11 17:18:39 +02:00
parent cf89cb3fc1
commit 07ce42c4bf
4 changed files with 137 additions and 16 deletions

View File

@ -13,6 +13,11 @@ import freemarker.template.Template;
public class FreeMarkerTest { public class FreeMarkerTest {
public File getTraceabilityUnitExampleDirectory() throws Exception { public File getTraceabilityUnitExampleDirectory() throws Exception {
File resourcesDirectory = FreeMarker.getResourcesDirectory();
return new File(resourcesDirectory, "examples/TraceabilityUnit");
}
public File getAssessmentUnitExampleDirectory() throws Exception {
File resourcesDirectory = FreeMarker.getResourcesDirectory(); File resourcesDirectory = FreeMarker.getResourcesDirectory();
return new File(resourcesDirectory, "examples/AssessmentUnit"); return new File(resourcesDirectory, "examples/AssessmentUnit");
} }
@ -21,19 +26,22 @@ public class FreeMarkerTest {
public void testIt() throws Exception { public void testIt() throws Exception {
FreeMarker freeMarker = new FreeMarker(); FreeMarker freeMarker = new FreeMarker();
Template template = freeMarker.getTemplate("example.ftl"); Template template = freeMarker.getTemplate("AssessmentUnit.ftl");
File json = new File(getTraceabilityUnitExampleDirectory(), "00a1f849-1b43-3fd1-9255-1d1134c969bb.json"); File assessmentUnitExamplesDir = getAssessmentUnitExampleDirectory();
ObjectMapper mapper = new ObjectMapper(); for(File json : assessmentUnitExamplesDir.listFiles()) {
Map map = mapper.readValue(json, Map.class); ObjectMapper mapper = new ObjectMapper();
Map map = mapper.readValue(json, Map.class);
File out = new File(FreeMarker.getResourcesDirectory(), "output.json"); map.put("uri_resolver_base_url", "https://data.d4science.org/ctlg/GRSF_Admin");
out.delete();
File out = new File(FreeMarker.getResourcesDirectory(), json.getName());
Writer writer = new FileWriter(out); out.delete();
template.process(map, writer);
writer.flush(); Writer writer = new FileWriter(out);
writer.close(); template.process(map, writer);
writer.flush();
writer.close();
}
} }

View File

@ -0,0 +1,58 @@
{
"title" : "${stock_name}",
"license_id": "${license_id}",
"description": "Short Name: ${short_name}\nGRSF Semantic Identifier: ${grsf_semantic_identifier}\nRecord URL: ${uri_resolver_base_url}/${grsf_uuid}",
"tags" : [
<#list assessment_area as aa>
{
"name": "${aa}"
},
</#list>
{
"name": "${similarities_indicator}"
}
],
"extras": [
<#list assessment_area as aa>
{
"key": "stock_identity:Assessment Area",
"value": "${aa}"
},
</#list>
<#if connected??>
<#list connected as connected_record>
{
"key": "stock_identity:Connected Record",
"value": "${connected_record}"
},
</#list>
</#if>
<#list database_sources as database_source>
{
"key": "stock_identity:Database Source",
"value": "${database_source.name}"
},
</#list>
{
"key": "stock_identity:GRSF Stock Name",
"value": "${stock_name}"
},
{
"key": "stock_identity:Short Name",
"value": "${short_name}"
},
{
"key": "stock_identity:GRSF Semantic Identifier",
"value": "${grsf_semantic_identifier}"
},
{
"key": "Record URL",
"value": "${uri_resolver_base_url}/${grsf_uuid}"
}
]
}

View File

@ -1,17 +1,58 @@
{ {
"title" : "${stock_name}", "title" : "${stock_name}",
"license_id": "${license_id}", "license_id": "${license_id}",
"description": "description": "Short Name: ${short_name}\nGRSF Semantic Identifier: ${grsf_semantic_identifier}\nRecord URL: ${uri_resolver_base_url}/${grsf_uuid}",
"Short Name: ${short_name}\nGRSF Semantic Identifier: ${grsf_semantic_identifier}\nRecord URL: https://data.d4science.org/ctlg/GRSF_Admin/${grsf_uuid}",
"tags" : [ "tags" : [
<#list assessment_area as aa>
{
"name": "${aa}"
},
</#list>
{ {
"name": "${similarities_indicator}" "name": "${similarities_indicator}"
} }
], ],
"extras": [ "extras": [
<#list assessment_area as aa>
{
"key": "stock_identity:Assessment Area",
"value": "${aa}"
},
</#list>
<#if connected??>
<#list connected as connected_record>
{
"key": "stock_identity:Connected Record",
"value": "${connected_record}"
},
</#list>
</#if>
<#list database_sources as database_source>
{
"key": "stock_identity:Database Source",
"value": "${database_source.name}"
},
</#list>
{
"key": "stock_identity:GRSF Stock Name",
"value": "${stock_name}"
},
{
"key": "stock_identity:Short Name",
"value": "${short_name}"
},
{
"key": "stock_identity:GRSF Semantic Identifier",
"value": "${grsf_semantic_identifier}"
},
{ {
"key": "Record URL", "key": "Record URL",
"value": "https://data.d4science.org/ctlg/GRSF_Admin/${grsf_uuid}" "value": "${uri_resolver_base_url}/${grsf_uuid}"
} }
] ]
} }

View File

@ -0,0 +1,14 @@
{
"globals" : {
"vars": [
{
"name": "uri_resolver_base_url",
"value": "https://data.d4science.org/ctlg/GRSF_Admin"
}
]
},
"fields": [
]
}