From c4629e78be03ea15d81109f1d48007cd46a75b94 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Thu, 8 Feb 2024 15:00:19 +0100 Subject: [PATCH] Added method update item [#26640] --- CHANGELOG.md | 6 ++ pom.xml | 4 +- .../utillibrary/server/DataCatalogue.java | 30 ++++++++++ .../utillibrary/server/DataCatalogueImpl.java | 55 ++++++++++++++++++- .../server/utils/CKANConveter.java | 4 +- .../utillibrary/test/ContextTest.java | 13 ++++- .../test/TestDataCatalogueCMS.java | 2 - .../test/TestDataCatalogueLib.java | 42 ++++++++------ 8 files changed, 128 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 736f133..19095da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ 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.4.0-SNAPSHOT] - 2024-02-08 + +**Enhancements** + +- Added method update item [#26640] + ## [v1.3.0] - 2023-02-06 **Enhancements** diff --git a/pom.xml b/pom.xml index 976a77a..4f26798 100644 --- a/pom.xml +++ b/pom.xml @@ -6,13 +6,13 @@ maven-parent org.gcube.tools - 1.1.0 + 1.2.0 org.gcube.datacatalogue catalogue-util-library - 1.3.0 + 1.4.0-SNAPSHOT Ckan utility library Utility library to retrieve users information, organizations information and so on from the ckan d4science datacatalogue diff --git a/src/main/java/org/gcube/datacatalogue/utillibrary/server/DataCatalogue.java b/src/main/java/org/gcube/datacatalogue/utillibrary/server/DataCatalogue.java index 5250398..d60d482 100644 --- a/src/main/java/org/gcube/datacatalogue/utillibrary/server/DataCatalogue.java +++ b/src/main/java/org/gcube/datacatalogue/utillibrary/server/DataCatalogue.java @@ -331,6 +331,36 @@ public interface DataCatalogue { String description, String licenseId, List tags, Map> customFieldsMultiple, List resources, boolean setPublic, boolean setSearchable, boolean socialPost) throws Exception; + + + /** + * Update ckan dataset multiple custom fields. + * + * @param username the username + * @param title the title + * @param name the name + * @param organizationName the organization name + * @param author the author + * @param authorMail the author mail + * @param maintainer the maintainer + * @param maintainerMail the maintainer mail + * @param version the version + * @param description the description + * @param licenseId the license id + * @param tags the tags + * @param customFieldsMultiple the custom fields multiple + * @param resources the resources + * @param setPublic the set public + * @param setSearchable the set searchable + * @param socialPost the social post + * @return the string + * @throws Exception the exception + */ + String updateCkanDatasetMultipleCustomFields(String username, String title, String name, String organizationName, + String author, String authorMail, String maintainer, String maintainerMail, long version, + String description, String licenseId, List tags, Map> customFieldsMultiple, + List resources, boolean setPublic, boolean setSearchable, boolean socialPost) + throws Exception; /** * Add a resource described by the bean to the dataset id into diff --git a/src/main/java/org/gcube/datacatalogue/utillibrary/server/DataCatalogueImpl.java b/src/main/java/org/gcube/datacatalogue/utillibrary/server/DataCatalogueImpl.java index a5e664e..2e044aa 100644 --- a/src/main/java/org/gcube/datacatalogue/utillibrary/server/DataCatalogueImpl.java +++ b/src/main/java/org/gcube/datacatalogue/utillibrary/server/DataCatalogueImpl.java @@ -866,7 +866,7 @@ public class DataCatalogueImpl implements DataCatalogue { // in order to avoid errors, the username is always converted String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username); - LOG.debug("username: "+username + " converted to ckanUsername: "+ckanUsername); + LOG.debug("username: " + username + " converted to ckanUsername: " + ckanUsername); // check in the hashmap first if (apiKeysMap.containsKey(ckanUsername)) { @@ -1305,7 +1305,7 @@ public class DataCatalogueImpl implements DataCatalogue { if (jsonValueDataset != null) { CkanDataset toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ); - LOG.info("Dataset with name " + toCkanDataset.getName() + " has been created correctly"); + LOG.info("Dataset with name " + toCkanDataset.getName() + " has been correctly created"); return toCkanDataset.getId(); } } catch (Exception e) { @@ -1320,6 +1320,57 @@ public class DataCatalogueImpl implements DataCatalogue { return null; } + @Override + public String updateCkanDatasetMultipleCustomFields(String username, String title, String name, + String organizationName, String author, String authorMail, String maintainer, String maintainerMail, + long version, String description, String licenseId, List tags, + Map> customFieldsMultiple, List resources, boolean setPublic, + boolean setSearchable, boolean socialPost) throws Exception { + LOG.info("Called updateCkanDatasetMultipleCustomFields"); + + // checks (minimum) + checkNotNull(username); + // checkNotNull(organizationNameOrId); + // checkArgument(!organizationNameOrId.isEmpty()); + checkArgument(!(title == null && name == null || title.isEmpty() && name.isEmpty()), + "Name and Title cannot be empty/null at the same time!"); + + GcubeContext gcubeContext = null; + try { + + gcubeContext = GCubeUtils.detectAndSetTheOrgNameContext(organizationName, username, true); + + 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 to update is: " + jsonValueDataset); + jsonValueDataset = gCatCaller.updateDataset(name, jsonValueDataset); + LOG.debug("Updated dataset is: " + jsonValueDataset); + + if (jsonValueDataset != null) { + CkanDataset toCkanDataset = MarshUnmarshCkanObject.toCkanDataset(jsonValueDataset, METHOD.TO_READ); + LOG.info("Dataset with name " + toCkanDataset.getName() + " has been correctly updated"); + return toCkanDataset.getId(); + } + } catch (Exception e) { + LOG.error("Error on updating the dataset: ", e); + throw e; + } finally { + + if (gcubeContext != null) + GCubeUtils.revertToSourceContext(gcubeContext); + } + + return null; + } + /** * Refresh dataset. * diff --git a/src/main/java/org/gcube/datacatalogue/utillibrary/server/utils/CKANConveter.java b/src/main/java/org/gcube/datacatalogue/utillibrary/server/utils/CKANConveter.java index 5e79e01..4b104df 100644 --- a/src/main/java/org/gcube/datacatalogue/utillibrary/server/utils/CKANConveter.java +++ b/src/main/java/org/gcube/datacatalogue/utillibrary/server/utils/CKANConveter.java @@ -68,8 +68,10 @@ public class CKANConveter { String ckanUsername = CatalogueUtilMethods.fromUsernameToCKanUsername(username); String nameToUse = name; - if(nameToUse == null) + //Applying normalize function if and only if the paramenter 'name' is null + if(nameToUse == null) { nameToUse = CatalogueUtilMethods.fromProductTitleToName(title); + } LOG.debug("Name of the dataset is going to be " + nameToUse + ". Title is going to be " + title); diff --git a/src/test/java/org/gcube/datacatalogue/utillibrary/test/ContextTest.java b/src/test/java/org/gcube/datacatalogue/utillibrary/test/ContextTest.java index 82abe9f..af885c7 100644 --- a/src/test/java/org/gcube/datacatalogue/utillibrary/test/ContextTest.java +++ b/src/test/java/org/gcube/datacatalogue/utillibrary/test/ContextTest.java @@ -27,9 +27,16 @@ public class ContextTest { protected static Properties properties; protected static final String PROPERTIES_FILENAME = "token.properties"; - public static final String ROOT = "/gcube"; - public static final String VO = ROOT + "/devsec"; - public static final String VRE = VO + "/devVRE"; +// public static final String ROOT = "/gcube"; +// public static final String VO = ROOT + "/devsec"; +// public static final String VRE = VO + "/devVRE"; + + + public static final String ROOT = "/d4science.research-infrastructures.eu"; + public static final String VO = ROOT + "/D4OS"; + public static final String VRE = VO + "/Blue-Cloud2026Project"; + + public static final String scope = VRE; static { properties = new Properties(); diff --git a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java index d34c9a5..860bfa9 100644 --- a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java +++ b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueCMS.java @@ -14,8 +14,6 @@ import org.gcube.datacatalogue.utillibrary.server.cms.CatalogueContentModeratorS import org.gcube.datacatalogue.utillibrary.shared.ItemStatus; import org.gcube.datacatalogue.utillibrary.shared.jackan.model.CkanDataset; import org.gcube.gcat.client.Item; -import org.junit.Before; -import org.junit.Test; import org.slf4j.LoggerFactory; /** diff --git a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueLib.java b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueLib.java index 7eb0b57..5d0672c 100644 --- a/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueLib.java +++ b/src/test/java/org/gcube/datacatalogue/utillibrary/test/TestDataCatalogueLib.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map; 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; @@ -28,12 +27,12 @@ import org.slf4j.LoggerFactory; * * @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy) Jun 1, 2020 */ -public class TestDataCatalogueLib { +public class TestDataCatalogueLib extends ContextTest{ private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TestDataCatalogueLib.class); private DataCatalogueFactory factory; - private String scope = "/gcube/devsec/devVRE"; + //private String scope = "/gcube/devsec/devVRE"; // private String scope = "/gcube"; // private String scope = "/pred4s/preprod/preVRE"; // private String scope = @@ -42,7 +41,7 @@ public class TestDataCatalogueLib { // "/d4science.research-infrastructures.eu/gCubeApps/BiodiversityLab"; // private String testUser = "costantino_perciante"; private String testUser = "francesco.mangiacrapa"; - private String authorizationToken = ""; + //private String authorizationToken = ""; String subjectId = "aa_father4"; String objectId = "bb_son4"; String productName = "adatasetcreatedbycatalogue-util-library-873805063"; @@ -52,7 +51,7 @@ public class TestDataCatalogueLib { * * @throws Exception the exception */ - // @Before + @Before public void before() throws Exception { factory = DataCatalogueFactory.getFactory(); } @@ -124,10 +123,17 @@ public class TestDataCatalogueLib { */ //@Test public void getDataset() throws Exception { + + String datasetname = "blue-cloud_2026_-_a_federated_european_fair_and_open_research_ecosystem_for_oceans_seas_coastal_and"; + DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String username = testUser; - CkanDataset dataset = instance.getDataset("my_first_restful_transaction_model_private", username); + CkanDataset dataset = instance.getDataset(datasetname, username); LOG.debug("Got dataset: " + dataset.getName() + ", with id: " + dataset.getId()); + LOG.debug("Got getAuthor: " + dataset.getAuthor()); + LOG.debug("Got getAuthorEmail: " + dataset.getAuthorEmail()); + LOG.debug("Got getMantainer: " + dataset.getMaintainer()); + LOG.debug("Got getMaintainerEmail: " + dataset.getMaintainerEmail()); } /** @@ -236,8 +242,8 @@ public class TestDataCatalogueLib { try { - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String licenseId = instance.getLicenses().get(0).getId(); @@ -282,8 +288,8 @@ public class TestDataCatalogueLib { // @Test public void createGroup() throws Exception { - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); DataCatalogueImpl instance = factory.getUtilsPerScope(scope); int random = new Random().nextInt(); @@ -296,8 +302,8 @@ public class TestDataCatalogueLib { // @Test public void addResource() throws Exception { - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); DataCatalogueImpl instance = factory.getUtilsPerScope(scope); CkanDataset dataset = instance.getDataset(productName, testUser); @@ -312,8 +318,8 @@ public class TestDataCatalogueLib { // @Test public void deleteResource() throws Exception { - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String resourceId = "ce7295cf-47db-4faf-901f-4fec2d3fae7a"; @@ -325,8 +331,8 @@ public class TestDataCatalogueLib { // @Test public void patchFieldsForDataset() throws Exception { - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); DataCatalogueImpl instance = factory.getUtilsPerScope(scope); Map customFieldsToChange = new HashMap(); @@ -337,8 +343,8 @@ public class TestDataCatalogueLib { // @Test public void getUrlProduct() throws Exception { - ScopeProvider.instance.set(scope); - SecurityTokenProvider.instance.set(authorizationToken); +// ScopeProvider.instance.set(scope); +// SecurityTokenProvider.instance.set(authorizationToken); DataCatalogueImpl instance = factory.getUtilsPerScope(scope); String datasetName = productName;