diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index d3d2b68..6caf699 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -3,9 +3,6 @@ - - uses - diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/Common.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/Common.java index 2f408e5..6366c56 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/Common.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/Common.java @@ -23,7 +23,7 @@ public class Common { @JsonProperty("description") private String description; - @JsonProperty("license") + @JsonProperty("license_id") private String license; // filled automatically by the server @@ -70,9 +70,9 @@ public class Common { @JsonProperty("extras") private Map extras = new HashMap<>(); - @JsonProperty("spatial") - @CustomField(key="Spatial") - private String spatial; + // @JsonProperty("spatial") + // @CustomField(key="Spatial") + // private String spatial; public Common() { super(); @@ -100,16 +100,16 @@ public class Common { this.type = type; this.resources = resources; this.extras = extras; - this.spatial = spatial; + // this.spatial = spatial; } - public String getSpatial() { - return spatial; - } - - public void setSpatial(String spatial) { - this.spatial = spatial; - } + // public String getSpatial() { + // return spatial; + // } + // + // public void setSpatial(String spatial) { + // this.spatial = spatial; + // } public String getDescription() { return description; @@ -233,8 +233,7 @@ public class Common { + ", databaseSources=" + databaseSources + ", sourceOfInformation=" + sourceOfInformation + ", dataOwner=" + dataOwner + ", type=" + type - + ", resources=" + resources + ", extras=" + extras - + ", spatial=" + spatial + "]"; + + ", resources=" + resources + ", extras=" + extras + "]"; } } diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java index 6779069..71497b2 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java @@ -37,14 +37,16 @@ import org.slf4j.LoggerFactory; */ @Path("fishery/") public class GrsfPublisherFisheryService { - + + private static final String DEFAULT_FISHERY_LICENSE = "CC-BY-SA-4.0"; + // the context @Context ServletContext contextServlet; // Logger private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherFisheryService.class); - + @GET @Path("hello") @Produces(MediaType.TEXT_PLAIN) @@ -52,6 +54,24 @@ public class GrsfPublisherFisheryService { return Response.ok("Hello.. Fishery service is here").build(); } + @GET + @Path("get-licenses") + @Produces(MediaType.APPLICATION_JSON) + public Response getLicenses(){ + + // since are equals for stock and fishery, we just retrieve them all + Map licenses = new HashMap(); + Status status; + try{ + licenses = HelperMethods.getLicenses(); + status = Status.OK; + }catch(Exception e){ + logger.error("Failed to retrieve the list of licenses"); + status = Status.INTERNAL_SERVER_ERROR; + } + return Response.status(status).entity(licenses).build(); + + } @POST @Path("publish-product") @@ -172,11 +192,18 @@ public class GrsfPublisherFisheryService { } } - - // if confirmed, set to visible TODO anyway if it is confirmed we should another method boolean setPublic = record.getStatus() == org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status.Confirmed; + // check the license id + String license = null; + if(record.getLicense() == null || record.getLicense().isEmpty()) + license = DEFAULT_FISHERY_LICENSE; + else + if(HelperMethods.existsLicenseId(record.getLicense())) + license = record.getLicense(); + else throw new Exception("Please check the license id!"); + // create the product id = catalogue.createCKanDataset( catalogue.getApiKeyFromUsername(username), @@ -188,7 +215,7 @@ public class GrsfPublisherFisheryService { record.getMaintainerContact(), record.getVersion(), record.getDescription(), - record.getLicense(), + license, tags, customFields, resources, diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java index c4c87bc..5f20a2e 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java @@ -38,13 +38,15 @@ import org.slf4j.LoggerFactory; @Path("stock/") public class GrsfPublisherStockService { + private static final String DEFAULT_STOCK_LICENSE = "CC-BY-SA-4.0"; + // the context @Context ServletContext contextServlet; // Logger private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherStockService.class); - + @GET @Path("hello") @Produces(MediaType.TEXT_PLAIN) @@ -52,6 +54,25 @@ public class GrsfPublisherStockService { return Response.ok("Hello.. Stock service is here").build(); } + @GET + @Path("get-licenses") + @Produces(MediaType.APPLICATION_JSON) + public Response getLicenses(){ + + // since are equals for stock and fishery, we just retrieve them all + Map licenses = new HashMap(); + Status status; + try{ + licenses = HelperMethods.getLicenses(); + status = Status.OK; + }catch(Exception e){ + logger.error("Failed to retrieve the list of licenses"); + status = Status.INTERNAL_SERVER_ERROR; + } + return Response.status(status).entity(licenses).build(); + + } + @POST @Path("publish-product") @Consumes(MediaType.APPLICATION_JSON) @@ -172,10 +193,18 @@ public class GrsfPublisherStockService { } - // if confirmed, set to visible TODO anyway if it is confirmed we should another method boolean setPublic = record.getStatus() == org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status.Confirmed; + // check the license id + String license = null; + if(record.getLicense() == null || record.getLicense().isEmpty()) + license = DEFAULT_STOCK_LICENSE; + else + if(HelperMethods.existsLicenseId(record.getLicense())) + license = record.getLicense(); + else throw new Exception("Please check the license id!"); + // create the product id = catalogue.createCKanDataset( catalogue.getApiKeyFromUsername(username), @@ -187,7 +216,7 @@ public class GrsfPublisherStockService { record.getMaintainerContact(), record.getVersion(), record.getDescription(), - record.getLicense(), + license, tags, customFields, resources, diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java index e47f2d6..940d740 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java @@ -4,11 +4,13 @@ import java.beans.PropertyDescriptor; import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Field; +import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletContext; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag; @@ -23,6 +25,7 @@ import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse; import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpGet; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder; +import eu.trentorise.opendata.jackan.model.CkanLicense; /** @@ -52,7 +55,7 @@ public abstract class HelperMethods { modified = modified.substring(1); if(modified.endsWith("-")) modified = modified.substring(0, modified.length() -1); - + return modified; } @@ -283,4 +286,36 @@ public abstract class HelperMethods { return null; } + /** + * Retrieve the list of ckan licenses and build up a map + * @return + * @throws Exception + */ + public static Map getLicenses() throws Exception { + + Map toReturn = new HashMap(); + String scope = ScopeProvider.instance.get(); + DataCatalogue catalogue = getDataCatalogueRunningInstance(scope); + List licenses = catalogue.getLicenses(); + + for (CkanLicense ckanLicense : licenses) { + toReturn.put(ckanLicense.getId(), ckanLicense.getTitle()); + } + + return toReturn; + } + + /** + * Check that the given license id is in CKAN + * @param license id to check + * @return + * @throws Exception + */ + public static boolean existsLicenseId(String license) throws Exception { + + Map licenses = getLicenses(); + return licenses.containsKey(license); + + } + } diff --git a/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JJerseyTest.java b/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JJerseyTest.java index 1acbde2..1d02c5b 100644 --- a/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JJerseyTest.java +++ b/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JJerseyTest.java @@ -14,11 +14,13 @@ import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; public class JJerseyTest extends JerseyTest{ //@Override protected Application configure() { + forceSet(TestProperties.CONTAINER_PORT, "0"); return new ResourceConfig(GrsfPublisherFisheryService.class, GrsfPublisherStockService.class); } @@ -30,9 +32,6 @@ public class JJerseyTest extends JerseyTest{ recordFishery.setType(Type.Fishing_Description); recordFishery.setDatabaseSources(Source.FIRMS); recordFishery.setStatus(Status.Pending); - recordFishery.setSpatial( - "{\"type\":\"Polygon\",\"coordinates\":[[[2.05827, 49.8625],[2.05827, 55.7447], [-6.41736, 55.7447], [-6.41736, 49.8625], [2.05827, 49.8625]]]}"); - Response res = target("fishery").path("/publish-product").request().post(Entity.entity(recordFishery, MediaType.APPLICATION_JSON)); System.out.println("Result is " + res.readEntity(String.class)); } @@ -50,4 +49,12 @@ public class JJerseyTest extends JerseyTest{ System.out.println("Result is " + res); } + + //@Test + public void getLicenses(){ + + Response res = target("fishery").path("/get-licenses").request().get(); + System.out.println("Result is " + res); + + } }