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 new file mode 100644 index 0000000..46e63cd --- /dev/null +++ b/D4S_UCDs/DEV/devVRE/catalogue/binding/freemarker_templates/d4gna_to_catalogue.ftl @@ -0,0 +1,285 @@ +<#assign jsonProj=doc> +<#assign theDocument=jsonProj._theDocument> + +<#assign resources=[]> +<#assign groups=[]> +<#assign tags=[]> +<#assign extras=[]> + +<#-- Macro to assign Tags --> +<#macro assignTag in_tags> +<#if in_tags??> + <#if in_tags?is_sequence> + <#list in_tags as my_tag> + <#assign tags = tags + [my_tag] /> + + <#else> + <#assign tags = tags + [in_tags] /> + + + + +<#-- Macro to assign a Resource --> +<#macro assignResource name url description format> +<#if description??> +<#else> +<#assign description = ""/> + +<#if url??> + <#assign resource = {"name": name, "url": url, "description": description, "format": format}> + <#assign resources = resources + [resource]/> + + + +<#-- Macro to assign Groups --> +<#macro assignGroup in_groups> +<#if in_groups??> + <#if in_groups?is_sequence> + <#list in_groups as my_group> + <#assign groups = groups + [my_group] /> + + <#else> + <#assign groups = groups + [in_groups] /> + + + + +<#-- Macro to assign Extras field --> +<#macro assignExtraField key value asObject> +<#if key??> + <#assign extra = {"key": key, "value": value, "asObject": asObject}> + <#assign extras = extras + [extra]/> + + + +<#-- Macro to build the tags as [{},{},..{}] --> +<#macro buildTags the_tags> +<#if the_tags??> + "tags": [ + <#list the_tags as tag> + { + "name": "${tag}" + } + <#if tag?is_last==false>, + + ], + + + +<#-- Macro to build the groups as [{},{},..{}] --> +<#macro buildGroups the_groups> +<#if the_groups??> + "groups": [ + <#list the_groups as the_group> + { + "name": "${the_group}" + } + <#if the_group?is_last==false>, + + ], + + + +<#-- Macro to build the resources as [{},{},..{}] --> +<#macro buildResources the_resources> +<#if the_resources??> + "resources": [ + <#list the_resources as resource> + { + "name": "${resource.name}", + "url": "${resource.url}", + "description": "${resource.description}", + "format": "${resource.format}" + } + <#if resource?is_last==false>, + + ], + + + +<#-- Macro to build the extras as [{},{},..{}] --> +<#macro buildExtrasFields the_extras> +<#if the_extras??> + "extras": [ + <#list the_extras as extra> + <#if extra.asObject?? && extra.asObject> + { + "key": "${extra.key}", + "value": ${extra.value} + } + <#else> + { + "key": "${extra.key}", + "value": "${extra.value}" + } + + <#if extra?is_last==false>, + + ] + + + + +<#-- +Starting document mapping to Catalogue +--> + +{ + "name": "${jsonProj._id}", + "title": "${theDocument.nome}", + "license_id": "CC-BY-SA-4.0", + "private": false, + <#assign description = theDocument.introduzione?trim /> + <#assign sanitizedNotes = description?replace("\\n", "")> + "notes": "${sanitizedNotes}", + +<#-- +####################################### + + RESOURCES + +####################################### +--> + +<#-- Mapping "Relazione Scavo" as resource --> +<#if theDocument.relazioneScavo?? && theDocument.relazioneScavo.fileset?? && theDocument.relazioneScavo.fileset._payloads??> +<#assign payloads = theDocument.relazioneScavo.fileset._payloads> +<#if payloads?size!=0> +<#-- Getting the first payload --> +<#assign payload = payloads[0]> +<@assignResource name=payload._name url=payload._link description=theDocument.relazioneScavo.titolo format=payload._mimetype> + + + +<#-- Mapping "Immagine Rappresentative" as resource --> +<#if theDocument.immaginiRappresentative??> +<#if theDocument.immaginiRappresentative?is_sequence && theDocument.immaginiRappresentative?size gt 0> + <#list theDocument.immaginiRappresentative as the_image> + <#if the_image.fileset?? && the_image.fileset._payloads??> + <#assign payloads = the_image.fileset._payloads> + <#if payloads?size!=0> + <#-- Getting the first payload --> + <#assign payload = payloads[0]> + <@assignResource name=payload._name url=payload._link description=the_image.didascalia format=payload._mimetype> + + + + + +<#assign payloads = theDocument.immaginiRappresentative[0].fileset._payloads> +<#if payloads?size!=0> +<#-- Getting the first payload --> +<#assign payload = payloads[0]> +<@assignResource name=payload._name url=payload._link description=theDocument.immaginiRappresentative[0].didascalia format=payload._mimetype> + + + +<#-- Building Resources --> +<@buildResources the_resources=resources> + + +<#-- +####################################### + + TAGS + +####################################### +--> + +<#-- Mapping "paroleChiaveLibere" as tag --> +<#if theDocument.paroleChiaveLibere??> +<@assignTag in_tags=theDocument.paroleChiaveLibere> + + +<#-- Building tags --> +<@buildTags the_tags=tags> + + +<#-- +####################################### + + GROUPS + +####################################### +--> + +<#-- Mapping the year of the "dataInizioProgetto" as group --> +<#assign dateString = theDocument.dataInizioProgetto?trim> +<#assign dataInizioYear = dateString?split("-")[0]> +<#-- <@assignGroup in_groups=dataInizioYear> --> +<#-- Building groups --> +<@buildGroups the_groups=groups> + + +<#-- +####################################### + + EXTRAS + +####################################### +--> + +<#-- Mapping extras fields --> +<#if theDocument.responsabile??> +<@assignExtraField key="Responsabile dei contenuti" value=theDocument.responsabile asObject=false> + + +<#if theDocument.editore??> +<@assignExtraField key="Ente responsabile del progetto" value=theDocument.editore asObject=false> + + +<#if theDocument.ufficioMic??> +<@assignExtraField key="Ufficio MiC competente per territorio" value=theDocument.ufficioMic asObject=false> + + +<#if theDocument.fontiFinanziamento??> +<@assignExtraField key="Fonte del finanziamento" value=theDocument.fontiFinanziamento asObject=false> + + +<#if theDocument.dataInizioProgetto??> +<@assignExtraField key="Data inizio Campagna" value=theDocument.dataInizioProgetto?trim asObject=false> + + +<#if theDocument.dataFineProgetto??> +<@assignExtraField key="Data fine Campagna" value=theDocument.dataFineProgetto?trim asObject=false> + + +<#if theDocument.modalitaIndividuazione??> +<@assignExtraField key="Modalità di individuazione" value=theDocument.modalitaIndividuazione asObject=false> + + +<#if theDocument.statoAttuale??> +<@assignExtraField key="Stato attuale" value=theDocument.statoAttuale asObject=false> + + +<#if theDocument.cronologiaMacrofase??> +<#if theDocument.cronologiaMacrofase?is_sequence> + <#list theDocument.cronologiaMacrofase as my_extra> + <@assignExtraField key="Cronologia (Macrofase)" value=my_extra asObject=false> + + <#else> + <@assignExtraField key="Cronologia (Macrofase)" value=theDocument.cronologiaMacrofase asObject=false> + + + +<#-- spatial field --> +<#if jsonProj._identificationReferences?? && jsonProj._identificationReferences?size!=0 && jsonProj._identificationReferences[0].geoJSON??> +<#assign geoJSON = jsonProj._identificationReferences[0].geoJSON> +<#-- if coordinates exists, managing it as a Point --> +<#if geoJSON.coordinates?? && geoJSON.coordinates?is_sequence && geoJSON.coordinates?size gt 2> + <#assign point_Coordinates = [geoJSON.coordinates[0], geoJSON.coordinates[1]]> + <#assign spatialField = '{\\"type\\": \\"${geoJSON.type}\\", \\"coordinates\\": [${point_Coordinates?join(", ")}]}'> + <@assignExtraField key="spatial" value=spatialField asObject=false> + + + + +<#-- Adding extra field "Anno" to add it as group --> +<@assignExtraField key="Anno" value=dataInizioYear asObject=false> + + +<#-- system:type --> +<@assignExtraField key="system:type" value="D4GNA" asObject=true> +<@buildExtrasFields the_extras=extras> +} + diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java index 12bb171..c7455df 100644 --- a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/CatalogueBindingPlugin.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; import org.bson.Document; +import org.gcube.application.cms.cataloguebinding.config.SubscribeEventsConfig; import org.gcube.application.cms.cataloguebinding.doaction.BindingAction; import org.gcube.application.cms.implementations.utils.UserUtils; import org.gcube.application.cms.plugins.EventListenerPluginInterface; @@ -28,7 +29,6 @@ import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDes import com.vdurmont.semver4j.Semver; -import lombok.Data; import lombok.Synchronized; import lombok.extern.slf4j.Slf4j; @@ -47,6 +47,8 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe public static final String PLUGIN_TYPE = "EventListener"; public static final PluginDescriptor DESCRIPTOR = new PluginDescriptor(PLUGIN_ID, PLUGIN_TYPE); + private static final String FREEMARKER_TEMPLATE_NAME = "freemarker_template"; + private static final String FREEMARKER_TEMPLATE_FOLDER = "link_to_freemarker_template_folder"; static { DESCRIPTOR.setVersion(new Semver("1.0.0")); @@ -63,11 +65,6 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe return DESCRIPTOR; } - - @Data - public class SubscribeEventsConfig { - private String event; - } /** * Inits the. @@ -168,7 +165,7 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe String context = UserUtils.getCurrent().getContext(); if (getCatalogueBindingMapPerContext() == null) { log.info("Initializing in " + context); - catalogueBindingMap.put(context, new EventsSubscribed()); + catalogueBindingMap.put(context, new LinkedHashMap()); } report.setStatus(Report.Status.OK); report.putMessage("Initialized " + DESCRIPTOR.getId() + " in the " + context); @@ -180,20 +177,24 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe return report; } - protected Map catalogueBindingMap = null; + // Map> + protected Map> catalogueBindingMap = null; /** * Gets the catalogue binding map per context. * * @return the catalogue binding map per context */ - protected EventsSubscribed getCatalogueBindingMapPerContext() { + protected Map getCatalogueBindingMapPerContext() { String context = UserUtils.getCurrent().getContext(); log.debug("Getting {} from cache map for context {}", CatalogueBindingPlugin.PLUGIN_ID, context); - if (catalogueBindingMap == null) - catalogueBindingMap = new LinkedHashMap(); + if (catalogueBindingMap == null) { + catalogueBindingMap = new LinkedHashMap>(); + } - return catalogueBindingMap.get(context); + // Map + Map map = catalogueBindingMap.get(context); + return map == null ? new LinkedHashMap() : map; } /** @@ -217,7 +218,9 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe String context = UserUtils.getCurrent().getContext(); - eventsSubscrInTheUCD = getCatalogueBindingMapPerContext(); + final Map mapCatalogBindingPerContext = getCatalogueBindingMapPerContext(); + + eventsSubscrInTheUCD = mapCatalogBindingPerContext.get(useCaseDescriptor.getId()); List listEventsSubscribedPerUCD = new ArrayList(); if (eventsSubscrInTheUCD == null || listEventsSubscribedPerUCD.isEmpty()) { @@ -241,10 +244,19 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe listEventsSubscribedPerUCD.add(event); } + String freemarker_template = profileConfiguration.get(FREEMARKER_TEMPLATE_NAME, String.class); + String link_to_freemarker_template_folder = profileConfiguration.get(FREEMARKER_TEMPLATE_FOLDER, + String.class); + eventsSubscrInTheUCD = new EventsSubscribed(); eventsSubscrInTheUCD.setContext(context); eventsSubscrInTheUCD.setListEventSubscribed(listEventsSubscribedPerUCD); - catalogueBindingMap.put(context, eventsSubscrInTheUCD); + eventsSubscrInTheUCD.setFreemarker_template(freemarker_template); + eventsSubscrInTheUCD.setLink_to_freemarker_template_folder(link_to_freemarker_template_folder); + + // updating the cache + mapCatalogBindingPerContext.put(useCaseDescriptor.getId(), eventsSubscrInTheUCD); + catalogueBindingMap.put(context, mapCatalogBindingPerContext); log.info("Events subscribed read from config {} ", eventsSubscrInTheUCD); @@ -275,12 +287,12 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe List listEvents = eventsSub.getListEventSubscribed(); log.info("List events is {}", listEvents); - - if(listEvents==null) { + + if (listEvents == null) { log.info("no event subscribed, returing false"); return false; } - + if (listEvents.contains(observerd.getEvent())) { log.info("the event {} is subscribed from config ", observerd.getEvent()); return true; @@ -309,11 +321,26 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe /** * Do action. * - * @param observerd the observerd + * @param itemObserved the item observed */ @Override - public void doAction(ItemObserved observerd) { - new BindingAction().doAction(observerd); + public void doAction(ItemObserved itemObserved) { + log.debug("doAction called..."); + + boolean isSubscribedEvent = checkIfSubscribedEvent(itemObserved); + + log.info("Is the event {} subscribed by " + CatalogueBindingPlugin.class.getSimpleName() + + " configured in the UCD? {} ", itemObserved.getEvent(), isSubscribedEvent); + + if (isSubscribedEvent) { + + // Map (UCD_ID, Notification) + final Map mapCatalogBindingPerContext = getCatalogueBindingMapPerContext(); + + EventsSubscribed eventsSubscribed = mapCatalogBindingPerContext.get(itemObserved.getUCD_Id()); + + new BindingAction().doAction(itemObserved, eventsSubscribed); + } } diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/EventsSubscribed.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/EventsSubscribed.java index 68acfb4..897e9e3 100644 --- a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/EventsSubscribed.java +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/EventsSubscribed.java @@ -10,4 +10,8 @@ import lombok.Data; public class EventsSubscribed { String context; List listEventSubscribed; + + String freemarker_template; + String link_to_freemarker_template_folder; + } diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/config/SubscribeEventsConfig.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/config/SubscribeEventsConfig.java new file mode 100644 index 0000000..bfc0041 --- /dev/null +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/config/SubscribeEventsConfig.java @@ -0,0 +1,10 @@ +package org.gcube.application.cms.cataloguebinding.config; + +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class SubscribeEventsConfig { + private String event; +} 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 1ca4720..653cee3 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 @@ -1,15 +1,49 @@ package org.gcube.application.cms.cataloguebinding.doaction; +import java.io.IOException; +import java.net.MalformedURLException; + +import org.gcube.application.cms.cataloguebinding.EventsSubscribed; +import org.gcube.application.cms.cataloguebinding.freemarker.FreemarkerConfig; import org.gcube.application.cms.plugins.events.ItemObserved; import org.gcube.application.geoportal.common.model.document.Project; +import freemarker.core.ParseException; +import freemarker.template.MalformedTemplateNameException; +import freemarker.template.Template; +import freemarker.template.TemplateException; +import freemarker.template.TemplateNotFoundException; import lombok.extern.slf4j.Slf4j; @Slf4j public class BindingAction { - public void doAction(ItemObserved itemObserved) { + public void doAction(ItemObserved itemObserved, EventsSubscribed eventsSubscribed) { log.info("Do action called on: {}", itemObserved.getEvent()); + + try { + FreemarkerConfig fmc = new FreemarkerConfig(eventsSubscribed.getLink_to_freemarker_template_folder()); + Template fmTemplate = fmc.getTemplate(eventsSubscribed.getFreemarker_template()); + } catch (MalformedURLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (TemplateException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (TemplateNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (MalformedTemplateNameException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + switch (itemObserved.getEvent()) { case PROJECT_CREATED: diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/freemarker/FreemarkerConfig.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/freemarker/FreemarkerConfig.java new file mode 100644 index 0000000..a4aeccb --- /dev/null +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/freemarker/FreemarkerConfig.java @@ -0,0 +1,47 @@ +package org.gcube.application.cms.cataloguebinding.freemarker; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Properties; + +import freemarker.core.ParseException; +import freemarker.template.Configuration; +import freemarker.template.MalformedTemplateNameException; +import freemarker.template.Template; +import freemarker.template.TemplateException; +import freemarker.template.TemplateNotFoundException; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class FreemarkerConfig { + + private String urlPathToTemplate; + private RemoteTemplateLoader rtl; + private Configuration cfg = new Configuration(Configuration.VERSION_2_3_32); + + public FreemarkerConfig(String urlPathToTemplate) throws MalformedURLException, TemplateException { + log.debug("Init "+FreemarkerConfig.class.getSimpleName()+" with root url: "+urlPathToTemplate); + this.urlPathToTemplate = urlPathToTemplate; + this.rtl = new RemoteTemplateLoader(new URL(urlPathToTemplate)); + freeMarkerConfigurer(); + } + + private Configuration freeMarkerConfigurer() throws TemplateException { + Properties properties = new Properties(); + properties.setProperty("localized_lookup", "false"); + this.cfg.setSettings(properties); + this.cfg.setTemplateLoader(rtl); + this.cfg.setDefaultEncoding("UTF-8"); + return cfg; + } + + public Template getTemplate(String templateName) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException { + return cfg.getTemplate(rtl.getURL(templateName).toString()); + } + + public String getUrlPathToTemplate() { + return urlPathToTemplate; + } + +} diff --git a/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/freemarker/RemoteTemplateLoader.java b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/freemarker/RemoteTemplateLoader.java new file mode 100644 index 0000000..036f63b --- /dev/null +++ b/catalogue-binding-plugin/src/main/java/org/gcube/application/cms/cataloguebinding/freemarker/RemoteTemplateLoader.java @@ -0,0 +1,44 @@ +package org.gcube.application.cms.cataloguebinding.freemarker; + +import java.net.MalformedURLException; +import java.net.URL; + +import freemarker.cache.URLTemplateLoader; + + +/** + * The Class RemoteTemplateLoader. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Jun 19, 2024 + */ +public class RemoteTemplateLoader extends URLTemplateLoader { + private URL root; + + /** + * Instantiates a new remote template loader. + * + * @param root the root + */ + public RemoteTemplateLoader(URL root) { + super(); + this.root = root; + } + + /** + * Gets the url. + * + * @param template the template + * @return the url + */ + @Override + protected URL getURL(String template) { + try { + return new URL(root, "/" + template); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + return null; + } +} \ No newline at end of file