This commit is contained in:
Francesco Mangiacrapa 2024-06-19 15:21:56 +02:00
parent c0ab40afea
commit 4359b13ae1
7 changed files with 472 additions and 21 deletions

View File

@ -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] />
</#list>
<#else>
<#assign tags = tags + [in_tags] />
</#if>
</#if>
</#macro>
<#-- Macro to assign a Resource -->
<#macro assignResource name url description format>
<#if description??>
<#else>
<#assign description = ""/>
</#if>
<#if url??>
<#assign resource = {"name": name, "url": url, "description": description, "format": format}>
<#assign resources = resources + [resource]/>
</#if>
</#macro>
<#-- 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] />
</#list>
<#else>
<#assign groups = groups + [in_groups] />
</#if>
</#if>
</#macro>
<#-- Macro to assign Extras field -->
<#macro assignExtraField key value asObject>
<#if key??>
<#assign extra = {"key": key, "value": value, "asObject": asObject}>
<#assign extras = extras + [extra]/>
</#if>
</#macro>
<#-- 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>,</#if>
</#list>
],
</#if>
</#macro>
<#-- 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>,</#if>
</#list>
],
</#if>
</#macro>
<#-- 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>,</#if>
</#list>
],
</#if>
</#macro>
<#-- 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>
<#if extra?is_last==false>,</#if>
</#list>
]
</#if>
</#macro>
<#--
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></@assignResource>
</#if>
</#if>
<#-- 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></@assignResource>
</#if>
</#if>
</#list>
</#if>
<#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></@assignResource>
</#if>
</#if>
<#-- Building Resources -->
<@buildResources the_resources=resources></@buildResources>
<#--
#######################################
TAGS
#######################################
-->
<#-- Mapping "paroleChiaveLibere" as tag -->
<#if theDocument.paroleChiaveLibere??>
<@assignTag in_tags=theDocument.paroleChiaveLibere></@assignTag>
</#if>
<#-- Building tags -->
<@buildTags the_tags=tags></@buildTags>
<#--
#######################################
GROUPS
#######################################
-->
<#-- Mapping the year of the "dataInizioProgetto" as group -->
<#assign dateString = theDocument.dataInizioProgetto?trim>
<#assign dataInizioYear = dateString?split("-")[0]>
<#-- <@assignGroup in_groups=dataInizioYear></@assignGroup> -->
<#-- Building groups -->
<@buildGroups the_groups=groups></@buildGroups>
<#--
#######################################
EXTRAS
#######################################
-->
<#-- Mapping extras fields -->
<#if theDocument.responsabile??>
<@assignExtraField key="Responsabile dei contenuti" value=theDocument.responsabile asObject=false></@assignExtraField>
</#if>
<#if theDocument.editore??>
<@assignExtraField key="Ente responsabile del progetto" value=theDocument.editore asObject=false></@assignExtraField>
</#if>
<#if theDocument.ufficioMic??>
<@assignExtraField key="Ufficio MiC competente per territorio" value=theDocument.ufficioMic asObject=false></@assignExtraField>
</#if>
<#if theDocument.fontiFinanziamento??>
<@assignExtraField key="Fonte del finanziamento" value=theDocument.fontiFinanziamento asObject=false></@assignExtraField>
</#if>
<#if theDocument.dataInizioProgetto??>
<@assignExtraField key="Data inizio Campagna" value=theDocument.dataInizioProgetto?trim asObject=false></@assignExtraField>
</#if>
<#if theDocument.dataFineProgetto??>
<@assignExtraField key="Data fine Campagna" value=theDocument.dataFineProgetto?trim asObject=false></@assignExtraField>
</#if>
<#if theDocument.modalitaIndividuazione??>
<@assignExtraField key="Modalità di individuazione" value=theDocument.modalitaIndividuazione asObject=false></@assignExtraField>
</#if>
<#if theDocument.statoAttuale??>
<@assignExtraField key="Stato attuale" value=theDocument.statoAttuale asObject=false></@assignExtraField>
</#if>
<#if theDocument.cronologiaMacrofase??>
<#if theDocument.cronologiaMacrofase?is_sequence>
<#list theDocument.cronologiaMacrofase as my_extra>
<@assignExtraField key="Cronologia (Macrofase)" value=my_extra asObject=false></@assignExtraField>
</#list>
<#else>
<@assignExtraField key="Cronologia (Macrofase)" value=theDocument.cronologiaMacrofase asObject=false></@assignExtraField>
</#if>
</#if>
<#-- 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></@assignExtraField>
</#if>
</#if>
<#-- Adding extra field "Anno" to add it as group -->
<@assignExtraField key="Anno" value=dataInizioYear asObject=false></@assignExtraField>
<#-- system:type -->
<@assignExtraField key="system:type" value="D4GNA" asObject=true></@assignExtraField>
<@buildExtrasFields the_extras=extras></@buildExtrasFields>
}

View File

@ -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"));
@ -64,11 +66,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<String, EventsSubscribed>());
}
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<String, EventsSubscribed> catalogueBindingMap = null;
// Map<Scope<Map<UDC_ID, EventsSubscribed>>
protected Map<String, Map<String, EventsSubscribed>> catalogueBindingMap = null;
/**
* Gets the catalogue binding map per context.
*
* @return the catalogue binding map per context
*/
protected EventsSubscribed getCatalogueBindingMapPerContext() {
protected Map<String, EventsSubscribed> getCatalogueBindingMapPerContext() {
String context = UserUtils.getCurrent().getContext();
log.debug("Getting {} from cache map for context {}", CatalogueBindingPlugin.PLUGIN_ID, context);
if (catalogueBindingMap == null)
catalogueBindingMap = new LinkedHashMap<String, EventsSubscribed>();
if (catalogueBindingMap == null) {
catalogueBindingMap = new LinkedHashMap<String, Map<String, EventsSubscribed>>();
}
return catalogueBindingMap.get(context);
// Map<UDC_ID, EventsSubscribed>
Map<String, EventsSubscribed> map = catalogueBindingMap.get(context);
return map == null ? new LinkedHashMap<String, EventsSubscribed>() : map;
}
/**
@ -217,7 +218,9 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe
String context = UserUtils.getCurrent().getContext();
eventsSubscrInTheUCD = getCatalogueBindingMapPerContext();
final Map<String, EventsSubscribed> mapCatalogBindingPerContext = getCatalogueBindingMapPerContext();
eventsSubscrInTheUCD = mapCatalogBindingPerContext.get(useCaseDescriptor.getId());
List<Event> listEventsSubscribedPerUCD = new ArrayList<EventManager.Event>();
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);
@ -276,7 +288,7 @@ public class CatalogueBindingPlugin extends AbstractPlugin implements EventListe
List<Event> listEvents = eventsSub.getListEventSubscribed();
log.info("List events is {}", listEvents);
if(listEvents==null) {
if (listEvents == null) {
log.info("no event subscribed, returing false");
return false;
}
@ -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<Project> observerd) {
new BindingAction().doAction(observerd);
public void doAction(ItemObserved<Project> 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<String, EventsSubscribed> mapCatalogBindingPerContext = getCatalogueBindingMapPerContext();
EventsSubscribed eventsSubscribed = mapCatalogBindingPerContext.get(itemObserved.getUCD_Id());
new BindingAction().doAction(itemObserved, eventsSubscribed);
}
}

View File

@ -10,4 +10,8 @@ import lombok.Data;
public class EventsSubscribed {
String context;
List<Event> listEventSubscribed;
String freemarker_template;
String link_to_freemarker_template_folder;
}

View File

@ -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;
}

View File

@ -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<Project> itemObserved) {
public void doAction(ItemObserved<Project> 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:

View File

@ -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;
}
}

View File

@ -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;
}
}