Fixing service

This commit is contained in:
luca.frosini 2023-06-27 15:10:19 +02:00
parent 3465ac76f5
commit 475242d44d
11 changed files with 323 additions and 32 deletions

View File

@ -0,0 +1,60 @@
package org.gcube.grsf.publisher.configuration;
import java.util.Map;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.annotation.JsonGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonSetter;
import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
import org.gcube.grsf.publisher.utils.Organization;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GRSFCatalogueConfiguration extends ServiceCatalogueConfiguration {
protected Set<String> grsfSupportedOrganizations;
public GRSFCatalogueConfiguration() {
Map<String,String> organizationMap = Organization.getInstance().getOrganizations();
this.grsfSupportedOrganizations = organizationMap.keySet();
}
@JsonGetter(value = SOCIAL_POST_ENABLED_KEY)
public boolean isSocialPostEnabledOriginal() {
return socialPostEnabled;
}
@JsonGetter(value = NOTIFICATION_TO_USER_ENABLED_KEY)
public boolean isNotificationToUsersEnabledOriginal() {
return notificationToUsersEnabled;
}
@JsonGetter(value = MODERATION_ENABLED_KEY)
public boolean isModerationEnabledOriginal() {
return moderationEnabled;
}
public boolean isSocialPostEnabled() {
return false;
}
public boolean isNotificationToUsersEnabled() {
return false;
}
public boolean isModerationEnabled() {
return false;
}
@JsonGetter(value = SUPPORTED_ORGANIZATIONS_KEY)
public Set<String> getOriginalSupportedOrganizations() {
return supportedOrganizations;
}
@Override
public Set<String> getSupportedOrganizations() {
return grsfSupportedOrganizations;
}
}

View File

@ -2,9 +2,14 @@ package org.gcube.grsf.publisher.configuration.isproxies.impl;
import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.gcat.configuration.isproxies.impl.FacetBasedISConfigurationProxy;
import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
import org.gcube.grsf.publisher.configuration.GRSFCatalogueConfiguration;
import org.gcube.grsf.publisher.utils.Organization;
/**
* @author Luca Frosini (ISTI - CNR)
@ -55,13 +60,17 @@ public class GRSFFacetBasedISConfigurationProxy extends FacetBasedISConfiguratio
// --------------------
//
@Override
protected ServiceCatalogueConfiguration readFromIS() {
ServiceCatalogueConfiguration scc = super.readFromIS();
scc.setModerationEnabled(false);
scc.setSocialPostEnabled(false);
scc.setNotificationToUsersEnabled(false);
return scc;
try {
ServiceCatalogueConfiguration scc = super.readFromIS();
ObjectMapper objectMapper = new ObjectMapper();
GRSFCatalogueConfiguration grsfCC = objectMapper.treeToValue(objectMapper.valueToTree(scc), GRSFCatalogueConfiguration.class);
return grsfCC;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -1,7 +1,11 @@
package org.gcube.grsf.publisher.configuration.isproxies.impl;
import java.util.Map;
import java.util.Set;
import org.gcube.gcat.configuration.isproxies.impl.GCoreISConfigurationProxy;
import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
import org.gcube.grsf.publisher.utils.Organization;
/**
* @author Luca Frosini (ISTI - CNR)
@ -18,6 +22,9 @@ public class GRSFGCoreISConfigurationProxy extends GCoreISConfigurationProxy {
scc.setModerationEnabled(false);
scc.setSocialPostEnabled(false);
scc.setNotificationToUsersEnabled(false);
Map<String,String> organizationMap = Organization.getInstance().getOrganizations();
Set<String> organizations = organizationMap.keySet();
scc.setSupportedOrganizations(organizations);
return scc;
}

View File

@ -3,6 +3,7 @@ package org.gcube.grsf.publisher.record;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
@ -19,6 +20,7 @@ import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.UriInfo;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.core.type.TypeReference;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
@ -30,12 +32,14 @@ import org.gcube.common.authorization.utils.secret.Secret;
import org.gcube.common.storagehub.client.dsl.FileContainer;
import org.gcube.common.storagehub.model.Metadata;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.gcat.persistence.ckan.CKAN;
import org.gcube.gcat.persistence.ckan.CKANPackage;
import org.gcube.gcat.persistence.ckan.CKANUserCache;
import org.gcube.gcat.utils.Constants;
import org.gcube.grsf.publisher.GRSFInitializator;
import org.gcube.grsf.publisher.freemarker.FreeMarker;
import org.gcube.grsf.publisher.utils.Organization;
import org.gcube.storagehub.MetadataMatcher;
import org.gcube.storagehub.StorageHubManagement;
import org.slf4j.Logger;
@ -133,10 +137,9 @@ public abstract class Record extends CKANPackage {
}
return jsonNode;
}
public String list(int limit, int offset) {
// TODO
return null;
return super.list(limit, offset);
}
protected Metadata getTimeSeriesMetadata() {
@ -165,16 +168,15 @@ public abstract class Record extends CKANPackage {
logger.debug("File {} has been persisted in StorageHub with ID:{}", file.getName(), fileContainer.getId());
return fileContainer;
}
protected Map<String, Object> getMapFromSourceJson(JsonNode jsonNode) throws JsonProcessingException {
@SuppressWarnings("unchecked")
Map<String, Object> map = objectMapper.treeToValue(jsonNode, Map.class);
Map<String, Object> map = objectMapper.convertValue(jsonNode, new TypeReference<Map<String, Object>>(){});
// TODO Get from IS
map.put("uri_resolver_base_url", "https://data.d4science.org/ctlg/GRSF_Admin");
// TODO get from Configuration
map.put("include_sensitive", true);
// map.put("source", sourceString); Source has been included in the record
return map;
}

View File

@ -0,0 +1,64 @@
package org.gcube.grsf.publisher.utils;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.core.type.TypeReference;
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.grsf.publisher.freemarker.FreeMarker;
import freemarker.template.Template;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Organization {
private JsonNode organizationJsonNode;
private Map<String, String> organizations;
public static final Organization getInstance() {
return new Organization();
}
protected Organization() {
this.organizationJsonNode = null;
this.organizations = null;
}
protected void init() {
FreeMarker freeMarker = new FreeMarker();
StringWriter writer = new StringWriter();
try {
Template orgTemplate = freeMarker.getTemplate("organizations.ftl");
orgTemplate.process(new HashMap<>(), writer);
writer.flush();
writer.close();
ObjectMapper mapper = new ObjectMapper();
organizationJsonNode = mapper.readTree(writer.toString());
organizations = mapper.convertValue(organizationJsonNode, new TypeReference<Map<String, String>>(){});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Map<String, String> getOrganizations() {
if(organizations==null || organizationJsonNode==null) {
init();
}
return organizations;
}
public JsonNode getOrganizationJsonNode() {
if(organizations==null || organizationJsonNode==null) {
init();
}
return organizationJsonNode;
}
}

View File

@ -5,7 +5,7 @@
"@class": "IsIdentifiedBy",
"target": {
"@class": "IdentifierFacet",
"value": "catalogue-configuration"
"value": "grsf-configuration"
}
}
],

View File

@ -9,7 +9,7 @@
"@class": "IsIdentifiedBy",
"target": {
"@class": "IdentifierFacet",
"value": "gcat-configuration"
"value": "grsf-configuration"
}
}
],

View File

@ -2,9 +2,17 @@ package org.gcube.grsf.publisher.record;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URI;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.grsf.publisher.ContextTest;
@ -22,16 +30,118 @@ public class RecordTest extends ContextTest {
return new File(resourcesDirectory, "examples/" + type);
}
public static UriInfo getUriInfo(MultivaluedMap<String, String> queryParameters) {
UriInfo uriInfo = new UriInfo() {
@Override
public URI resolve(URI uri) {
return null;
}
@Override
public URI relativize(URI uri) {
return null;
}
@Override
public UriBuilder getRequestUriBuilder() {
return null;
}
@Override
public URI getRequestUri() {
return null;
}
@Override
public MultivaluedMap<String, String> getQueryParameters(boolean decode) {
return null;
}
@Override
public MultivaluedMap<String, String> getQueryParameters() {
return queryParameters;
}
@Override
public List<PathSegment> getPathSegments(boolean decode) {
return null;
}
@Override
public List<PathSegment> getPathSegments() {
return null;
}
@Override
public MultivaluedMap<String, String> getPathParameters(boolean decode) {
return null;
}
@Override
public MultivaluedMap<String, String> getPathParameters() {
return null;
}
@Override
public String getPath(boolean decode) {
return null;
}
@Override
public String getPath() {
return null;
}
@Override
public List<String> getMatchedURIs(boolean decode) {
return null;
}
@Override
public List<String> getMatchedURIs() {
return null;
}
@Override
public List<Object> getMatchedResources() {
return null;
}
@Override
public UriBuilder getBaseUriBuilder() {
return null;
}
@Override
public URI getBaseUri() {
return null;
}
@Override
public UriBuilder getAbsolutePathBuilder() {
return null;
}
@Override
public URI getAbsolutePath() {
return null;
}
};
return uriInfo;
}
@Test
public void testRecords() throws Exception {
// -1 mean unlimited
int maxTestRecords = -1;
int maxTestRecordsPerSource = 3;
int maxTestRecordsPerSource = 6;
// String[] types = new String[] {"Stock", "Fishery", "TraceabilityUnit"};
String[] types = new String[] {"Stock"};
String[] types = new String[] { "Fishery", "Stock", "TraceabilityUnit" };
// String[] types = new String[] {"Fishery"};
// String[] types = new String[] {"Stock"};
// String[] types = new String[] {"TraceabilityUnit"};
Calendar start = Calendar.getInstance();
@ -63,9 +173,9 @@ public class RecordTest extends ContextTest {
File outputTypeDir = new File(outputDir, type);
outputTypeDir.mkdir();
// File[] sourceFiles = examplesDir.listFiles(dirnameFilter);
File[] sourceFiles = examplesDir.listFiles(dirnameFilter);
// File[] sourceFiles = new File[] { new File(examplesDir, "grsf") };
File[] sourceFiles = new File[] { new File(examplesDir, "firms") };
// File[] sourceFiles = new File[] { new File(examplesDir, "firms") };
for (File source : sourceFiles) {
@ -82,12 +192,12 @@ public class RecordTest extends ContextTest {
int countRecordPerSource = 0;
for (File jsonFile : jsonFiles) {
if(maxTestRecords>0 && countRecord >= maxTestRecords) {
if(maxTestRecords>0 && countRecord >= maxTestRecords+1) {
logger.info("The test has already elaborated {} records which is the max allowed", countRecord);
return;
}
if(maxTestRecordsPerSource>0 && countRecordPerSource >= maxTestRecordsPerSource) {
if(maxTestRecordsPerSource>0 && countRecordPerSource >= maxTestRecordsPerSource+1) {
logger.info("The test has already elaborated {} records for the source {} which is the max allowed for the source", countRecordPerSource, sourceString);
break;
}
@ -117,8 +227,10 @@ public class RecordTest extends ContextTest {
return;
}
record.setName(input.get("grsf_uuid").asText());
String grsfUUID = input.get("grsf_uuid").asText();
record.setName(grsfUUID);
String ret = record.create(mapper.writeValueAsString(input));
logger.debug("{} {} with GRSF UUID {} created successfully\n{}", sourceString, type, grsfUUID, ret);
// record.purge();
@ -129,7 +241,7 @@ public class RecordTest extends ContextTest {
long sleepTime = TimeUnit.MILLISECONDS.toMillis(200);
logger.trace("Going to sleep for {} millis", sleepTime);
// Thread.sleep(sleepTime);
Thread.sleep(sleepTime);
}
}
}
@ -140,5 +252,35 @@ public class RecordTest extends ContextTest {
countRecord, countTimeSeries, diff, TimeUnit.MILLISECONDS.toSeconds(diff));
}
@Test
public void testList() {
MultivaluedMap<String, String> mvm = new MultivaluedHashMap<String,String>();
UriInfo uriInfo = getUriInfo(mvm);
String[] types = new String[] { "Fishery", "Stock", "TraceabilityUnit" };
for (String type : types) {
Record record;
switch (type) {
case "Fishery":
record = new Fishery();
break;
case "Stock":
record = new Stock();
break;
case "TraceabilityUnit":
record = new TraceabilityUnit();
break;
default:
return;
}
record.setUriInfo(uriInfo);
String ret = record.list(0, 30);
logger.info("Listed {} records are {}", record.getType(), ret);
}
}
}

View File

@ -0,0 +1,6 @@
<#assign database_source_id_to_name = { "grsf":"Global Record of Stocks and Fisheries (GRSF)", "ram":"RAM Legacy Stock Assessment Database", "firms":"Fisheries and Resources Monitoring System (FIRMS)", "fishsource":"FishSource", "sdg":"FAO SDG 14.4.1 Questionnaire"} >
<#assign database_source_name_to_id = {} >
<#list database_source_id_to_name as key, value>
<#assign database_source_name_to_id += { "${value?json_string}" : "${key?json_string}" } >
</#list>

View File

@ -1,14 +1,9 @@
<#include "common.ftl">
<#assign timeseries_list = [] >
<#assign group_list = [] >
<#assign tag_list = [] >
<#assign database_source_id_to_name = { "grsf":"Global Record of Stocks and Fisheries (GRSF)", "ram":"RAM Legacy Stock Assessment Database", "firms":"Fisheries and Resources Monitoring System (FIRMS)", "fishsource":"FishSource", "sdg":"FAO SDG 14.4.1 Questionnaire"} >
<#assign database_source_name_to_id = {} >
<#list database_source_id_to_name as key, value>
<#assign database_source_name_to_id += { "${value?json_string}" : "${key?json_string}" } >
</#list>
<#assign record_url="${uri_resolver_base_url}/${grsf_uuid}" />
<#assign description = "Short Name: ${short_name}\n" >

View File

@ -0,0 +1,6 @@
<#include "common.ftl">
{
<#list database_source_id_to_name as key, value>
"${key?json_string}" : "${value?json_string}"<#sep>,</#sep>
</#list>
}