in progress..

This commit is contained in:
Francesco Mangiacrapa 2024-06-20 12:14:14 +02:00
parent 5bd0e00e79
commit 60814532d5
4 changed files with 144 additions and 34 deletions

View File

@ -58,6 +58,12 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.portlets.user</groupId>
<artifactId>uri-resolver-manager</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>

View File

@ -26,7 +26,7 @@ public class BindingAction {
FreemarkerConfig fmc = new FreemarkerConfig(eventsSubscribed.getFreemarker_template_host());
Template fmTemplate = fmc.getTemplate(eventsSubscribed.getFreemarker_template_path());
String toCatalogueJSON = MappingToCatalogue.apply(fmTemplate, itemObserved.getProject(), itemObserved.getUseCaseDescriptor());
String toCatalogueJSON = MappingToCatalogue.apply(fmTemplate, itemObserved.getProject(), itemObserved.getUseCaseDescriptor(), itemObserved.getContext().getId());
} catch (MalformedURLException e) {
log.error("Error: ", e);

View File

@ -6,10 +6,13 @@ import java.util.HashMap;
import java.util.Map;
import org.bson.Document;
import org.gcube.application.cms.cataloguebinding.gis.GisClient;
import org.gcube.application.cms.cataloguebinding.util.JSONObjectOrdered;
import org.gcube.application.cms.cataloguebinding.util.SerializationUtil;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.TARGET_GEOPORTAL_APP;
import org.json.JSONException;
import org.json.JSONObject;
@ -18,6 +21,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.spi.json.JsonOrgJsonProvider;
@ -26,74 +30,115 @@ import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
/**
* The Class MappingToCatalogue.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jun 20, 2024
*/
@Slf4j
public class MappingToCatalogue {
private static final String SYSTEM_TYPE = "system:type";
private static final String GIS_LINK = "Gis Link";
public static String apply(Template fmTemplate, Project theProject, UseCaseDescriptor ucd)
/**
* Apply.
*
* @param fmTemplate the fm template
* @param theProject the the project
* @param ucd the ucd
* @param scope the scope
* @return the string
* @throws JsonProcessingException the json processing exception
*/
public static String apply(Template fmTemplate, Project theProject, UseCaseDescriptor ucd, String scope)
throws JsonProcessingException {
log.info("Apply template {} to project with id '{}' and ucd id '{}' called", fmTemplate.getName(),
theProject.getId(), ucd.getId());
try {
// Getting project as document
Document asDocument = SerializationUtil.asDocument(theProject);
// Transforming the document as json
String json = asDocument.toJson();
// Getting map from json object
Map toJsonMap = fmtJsonUtil(json);
StringWriter stringWriter = new StringWriter();
// Putting the map as doc to apply the freemarker template
StringWriter stringWriter = new StringWriter();
Map root = new HashMap();
root.put("doc", toJsonMap);
// applying the freemarker template
fmTemplate.process(root, stringWriter);
String toCatalogueJSON = stringWriter.toString();
System.out.println("\n\ntoCatalogueString:" + toCatalogueJSON);
log.info("template applied with success..");
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
.jsonProvider(new JsonOrgJsonProvider()).build();
DocumentContext theDoc = JsonPath.parse(toCatalogueJSON, configuration);
// GIS LINK
JSONObject theGisLinkObj = JSONObjectOrdered.instance();
try {
theGisLinkObj.put("key", GIS_LINK);
theGisLinkObj.put("value", "https://data.dev.d4science.org/test/my_gis_link");
theDoc.add("$.extras", theGisLinkObj);
} catch (JSONException e) {
log.warn("Error occurrend on adding " + GIS_LINK);
log.debug("Error occurrend on adding " + GIS_LINK, e);
String toCatalogueDatasetJSON = stringWriter.toString();
if (log.isDebugEnabled()) {
log.debug("Template applyed with result: " + toCatalogueDatasetJSON);
}
// system:type
// Parsing the Catalogue Dataset json as Document Context
com.jayway.jsonpath.Configuration configuration = com.jayway.jsonpath.Configuration.builder()
.jsonProvider(new JsonOrgJsonProvider()).build();
DocumentContext theDoc = JsonPath.parse(toCatalogueDatasetJSON, configuration);
log.info("Adding service fields..");
// Adding GIS LINK to doc
JSONObject theGisLinkObj = JSONObjectOrdered.instance();
try {
String link = GisClient.gisLink(RESOLVE_AS.PUBLIC, ucd.getId(), theProject.getId(), scope,
TARGET_GEOPORTAL_APP.GEO_DV, true);
theGisLinkObj.put("key", GIS_LINK);
theGisLinkObj.put("value", link);
theDoc.add("$.extras", theGisLinkObj);
} catch (JSONException e) {
String error = "Error occurrend on adding " + GIS_LINK;
log.warn(error);
log.debug(error, e);
}
// Adding system:type to doc
try {
JSONObject theSystemType = JSONObjectOrdered.instance();
theSystemType.put("key", SYSTEM_TYPE);
theSystemType.put("value", ucd.getName());
theDoc.add("$.extras", theSystemType);
} catch (JSONException e) {
log.warn("Error occurrend on adding " + SYSTEM_TYPE);
log.debug("Error occurrend on adding " + SYSTEM_TYPE, e);
String error = "Error occurrend on adding " + SYSTEM_TYPE;
log.warn(error);
log.debug(error, e);
}
System.out.println("\n\nTo pretty print JSON:");
String toCatalogueObject = theDoc.jsonString();
// System.out.println("gis link: "+theDoc.jsonString());
System.out.println(toCatalogueObject);
String catalogueDatasetJSON = prettyPrintJsonUtil(theDoc.jsonString());
return toCatalogueObject;
if (log.isDebugEnabled()) {
log.debug("To pretty print JSON: \n" + catalogueDatasetJSON);
}
log.info("returning catalogue dataset, json lenght is: " + catalogueDatasetJSON.length());
return catalogueDatasetJSON;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("Error on apply mapping to catalogue: ", e);
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("Error on apply mapping to catalogue: ", e);
}
return null;
}
/**
* Fmt json util.
*
* @param json the json
* @return the map
* @throws JsonParseException the json parse exception
* @throws JsonMappingException the json mapping exception
* @throws IOException Signals that an I/O exception has occurred.
*/
public static Map<String, Object> fmtJsonUtil(String json)
throws JsonParseException, JsonMappingException, IOException {
ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@ -102,4 +147,23 @@ public class MappingToCatalogue {
}
/**
* Pretty print json util.
*
* @param uglyJsonString the ugly json string
* @return the string
*/
public static String prettyPrintJsonUtil(String uglyJsonString) {
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
Object jsonObject;
String prettyJson = "";
try {
jsonObject = mapper.readValue(uglyJsonString, Object.class);
prettyJson = mapper.writeValueAsString(jsonObject);
} catch (IOException e) {
return uglyJsonString;
}
return prettyJson;
}
}

View File

@ -0,0 +1,40 @@
package org.gcube.application.cms.cataloguebinding.gis;
import java.util.Map;
import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.TARGET_GEOPORTAL_APP;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class GisClient {
public static String gisLink(RESOLVE_AS resolveAs, String profileID, String projectID, String scope, TARGET_GEOPORTAL_APP targetApp, boolean shortLink) {
log.debug("called gisLink as {} for scope {}, profileId {}, projectId {} ", resolveAs, scope, profileID,
projectID);
String theLink = "";
try {
log.debug("Trying to generate Geoportal Gis Link...");
// Contacting the Geoportal-Resolver via UriResolverManager
UriResolverManager uriResolverManager = new UriResolverManager("GEO");
GeoportalResolverQueryStringBuilder builder = new GeoportalResolverQueryStringBuilder(profileID, projectID);
builder.scope(scope);
builder.resolverAs(resolveAs);
builder.targetApp(targetApp);
Map<String, String> params = builder.buildQueryParameters();
theLink = uriResolverManager.getLink(params, shortLink);
log.info("Geoportal GisViewer link is {} ", theLink);
} catch (Exception e) {
log.error("Error on creating the Geoportal GisViewer link for project id {}", projectID, e);
}
return theLink;
}
}