diff --git a/D4S_UCDs/DEV/devVRE/catalogue/binding/freemarker_templates/d4gna_to_catalogue.ftl b/D4S_UCDs/DEV/devVRE/catalogue/binding/freemarker_templates/d4gna_to_catalogue.ftl index c413b33..2acd404 100644 --- a/D4S_UCDs/DEV/devVRE/catalogue/binding/freemarker_templates/d4gna_to_catalogue.ftl +++ b/D4S_UCDs/DEV/devVRE/catalogue/binding/freemarker_templates/d4gna_to_catalogue.ftl @@ -245,13 +245,33 @@ Starting document mapping to Catalogue <#if theDocument.modalitaIndividuazione??> -<@assignExtraField key="Modalità di individuazione" value=theDocument.modalitaIndividuazione asObject=false> +<#if theDocument.modalitaIndividuazione?is_sequence> + <#list theDocument.modalitaIndividuazione as my_extra> + <@assignExtraField key="Modalità di individuazione" value=my_extra asObject=false> + + <#else> + <@assignExtraField key="Modalità di individuazione" value=theDocument.modalitaIndividuazione asObject=false> + + + +<#if theDocument.contestoIndagine??> +<#if theDocument.contestoIndagine?is_sequence> + <#list theDocument.contestoIndagine as my_extra> + <@assignExtraField key="Definizione del contesto d'indagine" value=my_extra asObject=false> + + <#else> + <@assignExtraField key="Definizione del contesto d'indagine" value=theDocument.contestoIndagine asObject=false> + <#if theDocument.statoAttuale??> <@assignExtraField key="Stato attuale" value=theDocument.statoAttuale asObject=false> +<#if theDocument.accessibilita??> +<@assignExtraField key="Accessibilità" value=theDocument.accessibilita asObject=false> + + <#if theDocument.cronologiaMacrofase??> <#if theDocument.cronologiaMacrofase?is_sequence> <#list theDocument.cronologiaMacrofase as my_extra> diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/BindingAction.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/BindingAction.java index dc838f4..6a30a6e 100644 --- a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/BindingAction.java +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/BindingAction.java @@ -4,6 +4,8 @@ import java.io.IOException; import java.net.MalformedURLException; import java.util.List; +import javax.ws.rs.WebApplicationException; + import org.gcube.application.cms.cataloguebinding.EventSubscribed; import org.gcube.application.cms.cataloguebinding.config.BindingWhen; import org.gcube.application.cms.cataloguebinding.freemarker.FreemarkerConfig; @@ -12,6 +14,7 @@ import org.gcube.application.cms.plugins.events.ItemObserved; import org.gcube.application.geoportal.common.model.document.Project; import org.gcube.application.geoportal.common.model.document.lifecycle.LifecycleInformation; import org.gcube.gcat.client.GCatClientDiscovery; +import org.gcube.gcat.client.Item; import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; @@ -45,17 +48,30 @@ public class BindingAction { public void doAction() { log.info("Do action called on: {}", itemObserved.getEvent()); + // The project id is the dataset name + String datasetName = itemObserved.getProjectId(); + switch (eventSubscribed.getEvent()) { - case PROJECT_CREATED: - log.info("Going to create item on the catalogue"); + case PROJECT_CREATED: { + log.info("Going to create item with id {} on the catalogue...", datasetName); + // Create or Update the item on the catalogue + String catalogueItemJSON = toCatalogueItem(); + new GCatCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON); break; - case PROJECT_DELETED: - log.info("Going to delete item on the catalogue"); + } + case PROJECT_DELETED: { + log.info("Going to delete item with name {} on the catalogue...", datasetName); + new GCatCaller().deleteDatasetOnCatalogue(datasetName); break; - case PROJECT_UPDATED: - log.info("Going to update the item on the catalogue"); + } + case PROJECT_UPDATED: { + log.info("Going to update item with id {} on the catalogue...", datasetName); + // Create or Update the item on the catalogue + String catalogueItemJSON = toCatalogueItem(); + new GCatCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON); break; - case LIFECYCLE_STEP_PERFORMED: + } + case LIFECYCLE_STEP_PERFORMED: { List listBindingWhen = eventSubscribed.getWhen(); if (listBindingWhen == null || listBindingWhen.size() == 0) { @@ -91,14 +107,14 @@ public class BindingAction { itemPhase, lastInvokedStep); if (itemPhase.equalsIgnoreCase(TARGET_PHASE_PUBLISHED)) { - log.info("Going to create item on the catalogue"); - // Create or Update the item on the catalogue - String catelogueItem = toCatalogueItem(); + String catalogueItemJSON = toCatalogueItem(); + log.info("Going to create item with name {} on the catalogue...", datasetName); + new GCatCaller().createOrUpdateTheDatasetOnCatalogue(datasetName, catalogueItemJSON); + } else if (itemPhase.equalsIgnoreCase(TARGET_PHASE_DRAFT)) { // Delete the item on the catalogue - String projectID = itemObserved.getProjectId(); - log.info("Going to delete item with id {} on the catalogue", projectID); - + log.info("Going to delete item with name {} on the catalogue...", datasetName); + new GCatCaller().deleteDatasetOnCatalogue(datasetName); } } else { log.info( @@ -109,6 +125,7 @@ public class BindingAction { } break; + } default: break; diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/GCatCaller.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/GCatCaller.java new file mode 100644 index 0000000..7058859 --- /dev/null +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/doaction/GCatCaller.java @@ -0,0 +1,94 @@ +/* + * + */ +package org.gcube.application.cms.cataloguebinding.doaction; + +import java.net.MalformedURLException; + +import javax.ws.rs.WebApplicationException; + +import org.gcube.gcat.client.Item; + +import lombok.extern.slf4j.Slf4j; + +/** + * The Class GCatCaller. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Jun 20, 2024 + */ +@Slf4j +public class GCatCaller { + + /** + * Creates the or update the dataset on catalogue. + * + * @param datasetName the dataset name + * @param datasetJSON the dataset JSON + */ + public void createOrUpdateTheDatasetOnCatalogue(String datasetName, String datasetJSON) { + log.info("createOrUpdateTheDatasetOnCatalogue with name {} called", datasetName); + try { + + boolean datasetExists = false; + try { + String dataset = new Item().read(datasetName); + datasetExists = dataset != null ? true : false; + } catch (Exception e) { + } + + log.info("datasetExists is {}", datasetExists); + + if (!datasetExists) { + log.info("calling create dataset..."); + String itemCreated = new Item().create(datasetJSON, false); + if (itemCreated != null) + log.info("Dataset created with success"); + else + log.warn("Dataset not created!"); + } else { + String itemCreated = new Item().update(datasetName, datasetJSON); + if (itemCreated != null) + log.info("Dataset updated with success!"); + else + log.warn("Dataset not updated!"); + } + + } catch (WebApplicationException | MalformedURLException e) { + log.error("Error occurred on creating the dataset " + datasetName + " on the catalogue", e); + } + } + + /** + * Delete dataset on catalogue. + * + * @param datasetName the dataset name + */ + public void deleteDatasetOnCatalogue(String datasetName) { + log.info("deleteDatasetOnCatalogue with name {} called", datasetName); + try { + + boolean datasetExists = false; + try { + String dataset = new Item().read(datasetName); + datasetExists = dataset != null ? true : false; + } catch (Exception e) { + } + + log.info("datasetExists is {}", datasetExists); + + if (datasetExists) { + log.info("calling delete dataset and purge"); + new Item().delete(datasetName, true); + log.info("Dataset deleted and purged with success!"); + } else { + log.warn("Dataset does not exists, returning.."); + return; + } + + } catch (WebApplicationException | MalformedURLException e) { + log.error("Error occurred on deleting the dataset " + datasetName + " on the catalogue", e); + } + } +} diff --git a/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/d4gna_to_catalogue_template.ftl b/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/d4gna_to_catalogue_template.ftl index c413b33..2acd404 100644 --- a/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/d4gna_to_catalogue_template.ftl +++ b/catalogue-binding-plugin/src/test/java/geoportal_to_catalogue/d4gna_to_catalogue_template.ftl @@ -245,13 +245,33 @@ Starting document mapping to Catalogue <#if theDocument.modalitaIndividuazione??> -<@assignExtraField key="Modalità di individuazione" value=theDocument.modalitaIndividuazione asObject=false> +<#if theDocument.modalitaIndividuazione?is_sequence> + <#list theDocument.modalitaIndividuazione as my_extra> + <@assignExtraField key="Modalità di individuazione" value=my_extra asObject=false> + + <#else> + <@assignExtraField key="Modalità di individuazione" value=theDocument.modalitaIndividuazione asObject=false> + + + +<#if theDocument.contestoIndagine??> +<#if theDocument.contestoIndagine?is_sequence> + <#list theDocument.contestoIndagine as my_extra> + <@assignExtraField key="Definizione del contesto d'indagine" value=my_extra asObject=false> + + <#else> + <@assignExtraField key="Definizione del contesto d'indagine" value=theDocument.contestoIndagine asObject=false> + <#if theDocument.statoAttuale??> <@assignExtraField key="Stato attuale" value=theDocument.statoAttuale asObject=false> +<#if theDocument.accessibilita??> +<@assignExtraField key="Accessibilità" value=theDocument.accessibilita asObject=false> + + <#if theDocument.cronologiaMacrofase??> <#if theDocument.cronologiaMacrofase?is_sequence> <#list theDocument.cronologiaMacrofase as my_extra> diff --git a/catalogue-binding-plugin/src/test/java/junit/CatalogueBindingPluginTest.java b/catalogue-binding-plugin/src/test/java/junit/CatalogueBindingPluginTest.java index 0bb9d37..9026e37 100644 --- a/catalogue-binding-plugin/src/test/java/junit/CatalogueBindingPluginTest.java +++ b/catalogue-binding-plugin/src/test/java/junit/CatalogueBindingPluginTest.java @@ -23,11 +23,21 @@ import org.junit.Test; import test.TestContextConfig; +/** + * The Class CatalogueBindingPluginTest. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Jun 20, 2024 + */ public class CatalogueBindingPluginTest extends BasicPluginTest { public final static String profileID = "profiledConcessioni"; - public final static String projectID = "642d4288c2133270c058ec39"; + public final static String projectID = "661d2c6f8804530afb90b132"; + /** + * Check plugin. + */ // @Test public void checkPlugin() { org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); @@ -48,6 +58,9 @@ public class CatalogueBindingPluginTest extends BasicPluginTest { } + /** + * Check plugin config. + */ // @Test public void checkPluginConfig() { org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); @@ -76,10 +89,10 @@ public class CatalogueBindingPluginTest extends BasicPluginTest { } /** - * Check notifications SUBMIT_FOR_REVIEW to USERS + * Check notify APPROV E SUBMITTE D on catalogue. */ - //@Test - public void checkNotify() { + @Test + public void checkNotify_APPROVE_SUBMITTED_onCatalogue() { org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); CatalogueBindingPlugin plugin = (CatalogueBindingPlugin) plugins.get(CatalogueBindingPlugin.DESCRIPTOR.getId()); UseCaseDescriptor descriptor = TestProfiles.profiles.get(profileID); @@ -104,7 +117,7 @@ public class CatalogueBindingPluginTest extends BasicPluginTest { LifecycleInformation lifecycleInfo = item.getProject().getLifecycleInformation(); - // Test Stage (Pending Approval, "SUBMIT-FOR-REVIEW") + // test PUBLISH ON CATALOGUE lifecycleInfo.setPhase("Published"); lifecycleInfo.setLastInvokedStep("APPROVE-SUBMITTED"); @@ -117,11 +130,139 @@ public class CatalogueBindingPluginTest extends BasicPluginTest { } } + + /** + * Check notify UNPUBLIS H on catalogue. + */ + @Test + public void checkNotify_UNPUBLISH_onCatalogue() { + org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); + CatalogueBindingPlugin plugin = (CatalogueBindingPlugin) plugins.get(CatalogueBindingPlugin.DESCRIPTOR.getId()); + UseCaseDescriptor descriptor = TestProfiles.profiles.get(profileID); + + CatalogueBindingPluginConfigModel pluginBindingModel; + try { + pluginBindingModel = plugin.readEventsSubscribedFromConfigurationInTheUCD(descriptor); + System.out.println("Events: " + pluginBindingModel); + } catch (Exception e) { + e.printStackTrace(); + } + + ItemObserved item; + try { + EventManager.Event event = Event.LIFECYCLE_STEP_PERFORMED; + item = mockItemObserverd(event); + + // Setting creator + User creator = new User(); + creator.setUsername("francesco.mangiacrapa"); + item.getProject().getInfo().getCreationInfo().setUser(creator); + + LifecycleInformation lifecycleInfo = item.getProject().getLifecycleInformation(); + + // Test delete the dataset on CATALOGUE + lifecycleInfo.setPhase("DRAFT"); + lifecycleInfo.setLastInvokedStep("UNPUBLISH"); + + System.out.println("By notifying event " + event + " project " + item.getProjectId()); + EventManager.getInstance().notify(event, item); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + /** + * Check notify PROJEC T CREATE D on catalogue. + */ + // @Test + public void checkNotify_PROJECT_CREATED_onCatalogue() { + org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); + CatalogueBindingPlugin plugin = (CatalogueBindingPlugin) plugins.get(CatalogueBindingPlugin.DESCRIPTOR.getId()); + UseCaseDescriptor descriptor = TestProfiles.profiles.get(profileID); + + CatalogueBindingPluginConfigModel pluginBindingModel; + try { + pluginBindingModel = plugin.readEventsSubscribedFromConfigurationInTheUCD(descriptor); + System.out.println("Events: " + pluginBindingModel); + } catch (Exception e) { + e.printStackTrace(); + } + + ItemObserved item; + try { + EventManager.Event event = Event.PROJECT_CREATED; + item = mockItemObserverd(event); + + // Setting creator + User creator = new User(); + creator.setUsername("francesco.mangiacrapa"); + item.getProject().getInfo().getCreationInfo().setUser(creator); + + LifecycleInformation lifecycleInfo = item.getProject().getLifecycleInformation(); + + // Test delete the dataset on CATALOGUE + lifecycleInfo.setPhase("DRAFT"); + + System.out.println("By notifying event " + event + " project " + item.getProjectId()); + EventManager.getInstance().notify(event, item); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + + /** + * Check notify PROJEC T DELETE D on catalogue. + */ + @Test + public void checkNotify_PROJECT_DELETED_onCatalogue() { + org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled()); + CatalogueBindingPlugin plugin = (CatalogueBindingPlugin) plugins.get(CatalogueBindingPlugin.DESCRIPTOR.getId()); + UseCaseDescriptor descriptor = TestProfiles.profiles.get(profileID); + + CatalogueBindingPluginConfigModel pluginBindingModel; + try { + pluginBindingModel = plugin.readEventsSubscribedFromConfigurationInTheUCD(descriptor); + System.out.println("Events: " + pluginBindingModel); + } catch (Exception e) { + e.printStackTrace(); + } + + ItemObserved item; + try { + EventManager.Event event = Event.PROJECT_DELETED; + item = mockItemObserverd(event); + + // Setting creator + User creator = new User(); + creator.setUsername("francesco.mangiacrapa"); + item.getProject().getInfo().getCreationInfo().setUser(creator); + + LifecycleInformation lifecycleInfo = item.getProject().getLifecycleInformation(); + + // Test delete the dataset on CATALOGUE + lifecycleInfo.setPhase("DRAFT"); + + System.out.println("By notifying event " + event + " project " + item.getProjectId()); + EventManager.getInstance().notify(event, item); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + /** * Mock item observerd. * + * @param event the event * @return the item observed - * @throws Exception + * @throws Exception the exception */ private static ItemObserved mockItemObserverd(EventManager.Event event) throws Exception {