Merge pull request 'bug_21560' (#2) from bug_21560 into master

Reviewed-on: #2
This commit is contained in:
Francesco Mangiacrapa 2021-06-04 16:05:54 +02:00
commit 013a0eda3b
11 changed files with 390 additions and 230 deletions

View File

@ -6,13 +6,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
@ -31,5 +24,12 @@
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -16,12 +16,12 @@
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>

View File

@ -4,6 +4,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v1.0.2-SNAPSHOT] - 2021-06-03
**Fixes**
[#21560] Bug fixing property "visibility" Restricted/Public
## [v1.0.1] - 2021-05-11

View File

@ -12,7 +12,7 @@
<groupId>org.gcube.datacatalogue</groupId>
<artifactId>catalogue-util-library</artifactId>
<version>1.0.1</version>
<version>1.0.2-SNAPSHOT</version>
<name>Ckan utility library</name>
<description>
@ -40,7 +40,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>maven-portal-bom</artifactId>
<version>3.6.2</version>
<version>3.6.2-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>

View File

@ -241,10 +241,8 @@ public class DataCatalogueImpl implements DataCatalogue {
List<CkanLicense> licenses = ckanCaller.getLicenseList();
for (CkanLicense ckanLicense : licenses) {
result.add(ckanLicense.getTitle());
LOG.debug("License is " + ckanLicense.getTitle() + " and id " + ckanLicense.getId());
}
return result;
@ -281,13 +279,6 @@ public class DataCatalogueImpl implements DataCatalogue {
return client.getDataset(datasetIdOrName);
}
// String authzToken = SecurityTokenProvider.instance.get();
// if (authzToken != null && !authzToken.isEmpty()) {
// LOG.info("gcube-token found. Calling the gCat client");
// String jsonDataset = gCatCaller.getDatasetForName(datasetId);
// return MarshUnmarshCkanObject.toCkanDataset(jsonDataset, METHOD.TO_READ);
// }
LOG.info("No username found. Calling Ckan Client without API-KEY");
return ckanCaller.getDataset(datasetIdOrName);
@ -998,35 +989,15 @@ public class DataCatalogueImpl implements DataCatalogue {
String toPassOrganizationToGcat = null; //Not needed to pass this information to gCat, never.
// String ckanUsername = getUserFromApiKey(apiKey).getName();
LOG.debug("The visibility parameter passed is (isPublic): "+setPublic);
CkanDataset dataset = CKANConveter.toCkanDataset(ckanCaller, username, title, name, toPassOrganizationToGcat, author, authorMail,
maintainer, maintainerMail, version, description, licenseId, tags, null, customFieldsMultiple,
resources, setPublic, setSearchable);
LOG.debug("The isPriv property into dataset is: "+dataset.isPriv());
// trying to create by gCat
String jsonValueDataset = MarshUnmarshCkanObject.toJsonValueDataset(dataset,METHOD.TO_CREATE);
LOG.info("Serialized dataset is: " + jsonValueDataset);
/*JSONObject jsonDataset = null;
try {
JSONParser parser = new JSONParser();
jsonDataset = (JSONObject) parser.parse(jsonValueDataset);
// Object jsonSpatial = jsonDataset.get("spatial");
// if (jsonSpatial != null) {
// ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.configure(Feature.QUOTE_FIELD_NAMES, false);
// String spatialUnquoted = objectMapper.writeValueAsString(jsonSpatial);
// System.out.println(spatialUnquoted);
// jsonDataset.put("spatial", spatialUnquoted);
// }
}catch (Exception e) {
LOG.error("Serialized dataset not parsable as JSON: ", e);
throw new Exception("Error on reading data, refresh and try again");
}
if(jsonDataset==null)
throw new Exception("Error on converting data");*/
jsonValueDataset = gCatCaller.createDataset(jsonValueDataset,socialPost);
LOG.debug("Created dataset is: " + jsonValueDataset);

View File

@ -61,10 +61,7 @@ public class CKANConveter {
Map<String, List<String>> customFieldsMultipleValues, List<ResourceBean> resources, boolean setPublic, boolean setSearchable) {
LOG.info("Called convert data to CkanDataset");
//ExtendCkanClient client = new ExtendCkanClient(CKAN_CATALOGUE_URL, apiKey);
//String ckanUsername = getUserFromApiKey(apiKey).getName();
CkanDataset dataset = new CkanDataset();
// in order to avoid errors, the username is always converted

View File

@ -0,0 +1,52 @@
package org.gcube.datacatalogue.utillibrary.server.utils;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.core.JsonParser;
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
import org.gcube.com.fasterxml.jackson.databind.DeserializationContext;
import org.gcube.com.fasterxml.jackson.databind.JsonDeserializer;
// TODO: Auto-generated Javadoc
/**
* The Class SwitchBoolDeserializer.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jun 3, 2021
*/
public class SwitchBoolDeserializer extends JsonDeserializer<Object> {
private final static Set<String> trueStateSet = new HashSet<>(Arrays.asList("yes", "true"));
private final static Set<String> falseStateSet = new HashSet<>(Arrays.asList("no", "false"));
/**
* Deserialize.
*
* @param jsonParser the json parser
* @param deserializationContext the deserialization context
* @return the object
* @throws IOException Signals that an I/O exception has occurred.
* @throws JsonProcessingException the json processing exception
*/
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
String str = jsonParser.getText();
if (str == null) {
return null;
}
str = str.toLowerCase();
if (trueStateSet.contains(str)) {
return true;
}
if (falseStateSet.contains(str)) {
return false;
}
return null;
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.datacatalogue.utillibrary.server.utils;
import java.io.IOException;
import org.gcube.com.fasterxml.jackson.core.JsonGenerator;
import org.gcube.com.fasterxml.jackson.databind.JsonSerializer;
import org.gcube.com.fasterxml.jackson.databind.SerializerProvider;
/**
* The Class SwitchBoolSerializer.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Jun 3, 2021
*/
public class SwitchBoolSerializer extends JsonSerializer<Object> {
private final static String UNIFIED_TRUE_STRING = "true";
private final static String UNIFIED_FALSE_STRING = "false";
/**
* Serialize.
*
* @param o the o
* @param jsonGenerator the json generator
* @param serializerProvider the serializer provider
* @throws IOException Signals that an I/O exception has occurred.
*/
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
Boolean b = (Boolean) o;
jsonGenerator.writeString(b ? UNIFIED_TRUE_STRING : UNIFIED_FALSE_STRING);
}
}

View File

@ -21,221 +21,233 @@ import javax.annotation.Nullable;
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
/**
* Extends {@link CkanDatasetBase} with fields found in search operations.
*/
public class CkanDataset extends CkanDatasetBase {
private String creatorUserId;
private String licenseUrl;
private String licenseTitle;
private Timestamp metadataCreated;
private Timestamp metadataModified;
private int numResources;
private CkanTrackingSummary trackingSummary;
private int numTags;
private String notesRendered;
private Boolean open;
private String creatorUserId;
private String licenseUrl;
private String licenseTitle;
private Timestamp metadataCreated;
private Timestamp metadataModified;
private int numResources;
private CkanTrackingSummary trackingSummary;
private int numTags;
private String notesRendered;
private Boolean open;
private CkanOrganization organization;
private CkanOrganization organization;
/**
* Actually it is named 'private' in api. Appears in searches.
*/
private Boolean priv;
private String revisionId;
private Timestamp revisionTimestamp;
public CkanDataset() {
}
/**
* Actually it is named 'private' in api. Appears in searches.
*/
// private Boolean priv;
private String revisionId;
private Timestamp revisionTimestamp;
/**
* @see CkanDatasetBase#CkanDatasetBase(String)
*/
public CkanDataset(String name) {
super(name);
}
public CkanDataset() {
}
public String getCreatorUserId() {
return creatorUserId;
}
/**
* @see CkanDatasetBase#CkanDatasetBase(String)
*/
public CkanDataset(String name) {
super(name);
}
public void setCreatorUserId(@Nullable String creatorUserId) {
this.creatorUserId = creatorUserId;
}
public String getCreatorUserId() {
return creatorUserId;
}
/**
* <a href="http://docs.ckan.org/en/latest/api/legacy-api.html?highlight=isopen" target="_blank">
* Legacy api 1/2 docs</a> says: boolean indication of whether dataset is
* open according to Open Knowledge Definition, based on other fields
*/
@JsonProperty("isopen")
public Boolean isOpen() {
return open;
}
public void setCreatorUserId(@Nullable String creatorUserId) {
this.creatorUserId = creatorUserId;
}
/**
* @see #isOpen()
*/
@JsonProperty("isopen")
public void setOpen(Boolean isOpen) {
this.open = isOpen;
}
/**
* <a href="http://docs.ckan.org/en/latest/api/legacy-api.html?highlight=isopen"
* target="_blank"> Legacy api 1/2 docs</a> says: boolean indication of whether
* dataset is open according to Open Knowledge Definition, based on other fields
*/
@JsonProperty("isopen")
// @JsonSerialize(using = SwitchBoolSerializer.class)
// @JsonDeserialize(using = SwitchBoolDeserializer.class)
// @JsonInclude(JsonInclude.Include.NON_NULL)
public Boolean isOpen() {
return open;
}
public String getLicenseTitle() {
return licenseTitle;
}
/**
* @see #isOpen()
*/
@JsonProperty("isopen")
public void setOpen(Boolean isOpen) {
this.open = isOpen;
}
public void setLicenseTitle(String licenseTitle) {
this.licenseTitle = licenseTitle;
}
public String getLicenseTitle() {
return licenseTitle;
}
public String getLicenseUrl() {
return licenseUrl;
}
public void setLicenseTitle(String licenseTitle) {
this.licenseTitle = licenseTitle;
}
public void setLicenseUrl(String licenseUrl) {
this.licenseUrl = licenseUrl;
}
public String getLicenseUrl() {
return licenseUrl;
}
/**
* CKAN always refer to UTC timezone
*/
public Timestamp getMetadataCreated() {
return metadataCreated;
}
public void setLicenseUrl(String licenseUrl) {
this.licenseUrl = licenseUrl;
}
/**
* CKAN always refer to UTC timezone
*/
public void setMetadataCreated(Timestamp metadataCreated) {
this.metadataCreated = metadataCreated;
}
/**
* CKAN always refer to UTC timezone
*/
public Timestamp getMetadataCreated() {
return metadataCreated;
}
/**
* CKAN always refers to UTC timezone
*/
public Timestamp getMetadataModified() {
return metadataModified;
}
/**
* CKAN always refer to UTC timezone
*/
public void setMetadataCreated(Timestamp metadataCreated) {
this.metadataCreated = metadataCreated;
}
/**
* CKAN always refers to UTC timezone
*/
public void setMetadataModified(Timestamp metadataModified) {
this.metadataModified = metadataModified;
}
/**
* CKAN always refers to UTC timezone
*/
public Timestamp getMetadataModified() {
return metadataModified;
}
public String getNotesRendered() {
return notesRendered;
}
/**
* CKAN always refers to UTC timezone
*/
public void setMetadataModified(Timestamp metadataModified) {
this.metadataModified = metadataModified;
}
public void setNotesRendered(String notesRendered) {
this.notesRendered = notesRendered;
}
public String getNotesRendered() {
return notesRendered;
}
public int getNumTags() {
return numTags;
}
public void setNotesRendered(String notesRendered) {
this.notesRendered = notesRendered;
}
public void setNumTags(int numTags) {
this.numTags = numTags;
}
public int getNumTags() {
return numTags;
}
/**
* The organization that owns the dataset.
*
* Notice that if the dataset was obtained with a
* {@link eu.trentorise.opendata.jackan.CkanClient#getDataset(java.lang.String)} call, the returned
* organization won't have all the params you would get with a
* {@link eu.trentorise.opendata.jackan.CkanClient#getOrganization(java.lang.String)} call.
*/
public CkanOrganization getOrganization() {
return organization;
}
public void setNumTags(int numTags) {
this.numTags = numTags;
}
/**
* Sets the organization that owns the dataset.
*/
public void setOrganization(CkanOrganization organization) {
this.organization = organization;
}
/**
* The organization that owns the dataset.
*
* Notice that if the dataset was obtained with a
* {@link eu.trentorise.opendata.jackan.CkanClient#getDataset(java.lang.String)}
* call, the returned organization won't have all the params you would get with
* a
* {@link eu.trentorise.opendata.jackan.CkanClient#getOrganization(java.lang.String)}
* call.
*/
public CkanOrganization getOrganization() {
return organization;
}
/**
* Actually it is named "private" in the CKAN API. Appears in dataset
* searches.
*/
@JsonProperty("private")
public Boolean isPriv() {
return priv;
}
/**
* Sets the organization that owns the dataset.
*/
public void setOrganization(CkanOrganization organization) {
this.organization = organization;
}
/**
* Actually it is named "private" in the CKAN API. Appears in dataset
* searches.
*/
public void setPriv(Boolean priv) {
this.priv = priv;
}
public int getNumResources() {
return numResources;
}
public int getNumResources() {
return numResources;
}
public void setNumResources(int numResources) {
this.numResources = numResources;
}
public void setNumResources(int numResources) {
this.numResources = numResources;
}
/**
* Returns the alphanumerical id, like "39d94b20-ea72-4c5e-bd8f-967a77e03946"
*/
public String getRevisionId() {
return revisionId;
}
/**
* Returns the alphanumerical id, like
* "39d94b20-ea72-4c5e-bd8f-967a77e03946"
*/
public String getRevisionId() {
return revisionId;
}
/**
* Sets the alphanumerical id, like "39d94b20-ea72-4c5e-bd8f-967a77e03946"
*/
public void setRevisionId(String revisionId) {
this.revisionId = revisionId;
}
/**
* Sets the alphanumerical id, like "39d94b20-ea72-4c5e-bd8f-967a77e03946"
*/
public void setRevisionId(String revisionId) {
this.revisionId = revisionId;
}
/**
* Returns date in UTC timezone. Probably it is automatically calculated by
* CKAN.
*/
public Timestamp getRevisionTimestamp() {
return revisionTimestamp;
}
/**
* Returns date in UTC timezone. Probably it is automatically calculated by
* CKAN.
*/
public Timestamp getRevisionTimestamp() {
return revisionTimestamp;
}
/**
* CKAN always refer to UTC timezone. Probably it is automatically calculated by
* CKAN.
*
* @param revisionTimestamp
*/
public void setRevisionTimestamp(Timestamp revisionTimestamp) {
this.revisionTimestamp = revisionTimestamp;
}
/**
* CKAN always refer to UTC timezone. Probably it is automatically
* calculated by CKAN.
*
* @param revisionTimestamp
*/
public void setRevisionTimestamp(Timestamp revisionTimestamp) {
this.revisionTimestamp = revisionTimestamp;
}
public CkanTrackingSummary getTrackingSummary() {
return trackingSummary;
}
public CkanTrackingSummary getTrackingSummary() {
return trackingSummary;
}
public void setTrackingSummary(CkanTrackingSummary trackingSummary) {
this.trackingSummary = trackingSummary;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("CkanDataset [creatorUserId=");
builder.append(creatorUserId);
builder.append(", licenseUrl=");
builder.append(licenseUrl);
builder.append(", licenseTitle=");
builder.append(licenseTitle);
builder.append(", metadataCreated=");
builder.append(metadataCreated);
builder.append(", metadataModified=");
builder.append(metadataModified);
builder.append(", numResources=");
builder.append(numResources);
builder.append(", trackingSummary=");
builder.append(trackingSummary);
builder.append(", numTags=");
builder.append(numTags);
builder.append(", notesRendered=");
builder.append(notesRendered);
builder.append(", open=");
builder.append(open);
builder.append(", organization=");
builder.append(organization);
builder.append(", revisionId=");
builder.append(revisionId);
builder.append(", revisionTimestamp=");
builder.append(revisionTimestamp);
builder.append(", toString()=");
builder.append(super.toString());
builder.append("]");
return builder.toString();
}
public void setTrackingSummary(CkanTrackingSummary trackingSummary) {
this.trackingSummary = trackingSummary;
}
}

View File

@ -28,6 +28,7 @@ import javax.annotation.Nullable;
import org.gcube.com.fasterxml.jackson.annotation.JsonAnyGetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonAnySetter;
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
import org.gcube.com.fasterxml.jackson.annotation.JsonProperty;
@ -70,6 +71,8 @@ public class CkanDatasetBase {
private String type;
private String url;
private String version;
private Boolean priv;
/**
* Custom CKAN instances might sometimes gift us with properties that don't
@ -466,5 +469,75 @@ public class CkanDatasetBase {
return isNotEmpty(getName()) ? getName() : getId();
}
/**
* Actually it is named "private" in the CKAN API. Appears in dataset
* searches.
*/
@JsonProperty("private")
public Boolean isPriv() {
return priv;
}
/**
* Actually it is named "private" in the CKAN API. Appears in dataset
* searches.
*/
public void setPriv(Boolean priv) {
this.priv = priv;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("CkanDatasetBase [author=");
builder.append(author);
builder.append(", authorEmail=");
builder.append(authorEmail);
builder.append(", extras=");
builder.append(extras);
builder.append(", groups=");
builder.append(groups);
builder.append(", id=");
builder.append(id);
builder.append(", licenseId=");
builder.append(licenseId);
builder.append(", maintainer=");
builder.append(maintainer);
builder.append(", maintainerEmail=");
builder.append(maintainerEmail);
builder.append(", name=");
builder.append(name);
builder.append(", notes=");
builder.append(notes);
builder.append(", ownerOrg=");
builder.append(ownerOrg);
builder.append(", relationshipsAsObject=");
builder.append(relationshipsAsObject);
builder.append(", relationshipsAsSubject=");
builder.append(relationshipsAsSubject);
builder.append(", resources=");
builder.append(resources);
builder.append(", state=");
builder.append(state);
builder.append(", tags=");
builder.append(tags);
builder.append(", title=");
builder.append(title);
builder.append(", type=");
builder.append(type);
builder.append(", url=");
builder.append(url);
builder.append(", version=");
builder.append(version);
builder.append(", priv=");
builder.append(priv);
builder.append(", others=");
builder.append(others);
builder.append("]");
return builder.toString();
}
}

View File

@ -10,6 +10,8 @@ import java.util.Random;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject;
import org.gcube.datacatalogue.utillibrary.ckan.MarshUnmarshCkanObject.METHOD;
import org.gcube.datacatalogue.utillibrary.server.ApplicationProfileScopePerUrlReader;
import org.gcube.datacatalogue.utillibrary.server.DataCatalogueFactory;
import org.gcube.datacatalogue.utillibrary.server.DataCatalogueImpl;
@ -18,6 +20,8 @@ import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanGroup;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanOrganization;
import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanUser;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;
@ -33,8 +37,8 @@ public class TestDataCatalogueLib {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TestDataCatalogueLib.class);
private DataCatalogueFactory factory;
//private String scope = "/gcube/devsec/devVRE";
private String scope = "/gcube";
private String scope = "/gcube/devsec/devVRE";
//private String scope = "/gcube";
//private String scope = "/pred4s/preprod/preVRE";
//private String scope = "/d4science.research-infrastructures.eu/SoBigData/Catalogue-TerritoriAperti";
//private String scope = "/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab";
@ -257,7 +261,7 @@ public class TestDataCatalogueLib {
customFieldsMultiple.put("system:type", Arrays.asList("EmptyProfile"));
boolean setSearchable = true;
boolean setPublic = false;
boolean setPublic = true;
List<ResourceBean> resources = null;
String orgName = scope.split("/")[3].toLowerCase();
@ -359,5 +363,17 @@ public class TestDataCatalogueLib {
String url = instance.getUnencryptedUrlFromDatasetIdOrName(datasetName);
LOG.debug("url is " + url);
}
//@Test
public void marshallingDataset() throws Exception{
DataCatalogueImpl instance = factory.getUtilsPerScope(scope);
String username = testUser;
CkanDataset dataset = instance.getDataset("sarda-sarda", username);
LOG.debug("Got dataset: " +dataset.getName() + ", with id: "+dataset.getId() +" is private: "+dataset.isPriv());
String jsonValueDataset = MarshUnmarshCkanObject.toJsonValueDataset(dataset,METHOD.TO_CREATE);
System.out.println(jsonValueDataset);
dataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ);
System.out.println(dataset);
}
}