ckan2zenodo-publisher-widget/src/main/java/org/gcube/portlets/widgets/ckan2zenodopublisher/server/CkanToZenodoPublisherServic...

342 lines
13 KiB
Java
Raw Normal View History

2019-12-06 17:23:52 +01:00
package org.gcube.portlets.widgets.ckan2zenodopublisher.server;
2023-12-06 11:39:13 +01:00
import java.util.Date;
2020-01-15 17:51:22 +01:00
import java.util.HashSet;
2020-01-14 12:28:20 +01:00
import java.util.List;
2020-01-17 12:46:32 +01:00
import java.util.Map;
2020-01-17 15:44:04 +01:00
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
2020-01-10 17:07:03 +01:00
2019-12-20 16:36:49 +01:00
import org.gcube.data.publishing.ckan2zenodo.Ckan2Zenodo;
2020-01-15 17:51:22 +01:00
import org.gcube.data.publishing.ckan2zenodo.Ckan2ZenodoImpl;
2019-12-20 16:36:49 +01:00
import org.gcube.data.publishing.ckan2zenodo.model.CkanItemDescriptor;
2020-01-14 12:28:20 +01:00
import org.gcube.data.publishing.ckan2zenodo.model.CkanResource;
2020-01-10 17:07:03 +01:00
import org.gcube.data.publishing.ckan2zenodo.model.faults.ConfigurationException;
import org.gcube.data.publishing.ckan2zenodo.model.faults.GcatException;
import org.gcube.data.publishing.ckan2zenodo.model.faults.TransformationException;
import org.gcube.data.publishing.ckan2zenodo.model.faults.ZenodoException;
import org.gcube.data.publishing.ckan2zenodo.model.report.EnvironmentReport;
2020-01-13 17:48:41 +01:00
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.DepositionMetadata;
2019-12-09 17:17:05 +01:00
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.ZenodoDeposition;
2019-12-06 17:23:52 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.CkanToZenodoPublisherService;
2020-01-16 18:15:17 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.server.configuration.ZenodoFieldsDescriptionsReader;
2019-12-10 16:31:06 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.server.converter.ItemToZenodoConverter;
2019-12-09 11:56:04 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.CatalogueItem;
2020-08-26 11:11:42 +02:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.ItemTranslateError;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.ItemTranslateError.ERROR_TYPE;
2020-02-07 11:57:43 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.ZenodoError;
2023-07-28 14:41:30 +02:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.DOI_dv;
2020-01-10 17:07:03 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoFile;
2019-12-09 17:17:05 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoItem;
2020-01-13 17:48:41 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoMetadata;
2019-12-09 17:17:05 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2019-12-06 17:23:52 +01:00
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
2019-12-20 16:36:49 +01:00
import com.liferay.portal.service.UserLocalServiceUtil;
2019-12-06 17:23:52 +01:00
/**
* The server side implementation of the RPC service.
2020-01-15 17:51:22 +01:00
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
2023-07-27 14:39:38 +02:00
* Jan 15, 2020
2019-12-06 17:23:52 +01:00
*/
@SuppressWarnings("serial")
2019-12-09 17:17:05 +01:00
public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet implements CkanToZenodoPublisherService {
2019-12-06 17:23:52 +01:00
2019-12-09 17:17:05 +01:00
private static Logger LOG = LoggerFactory.getLogger(CkanToZenodoPublisherServiceImpl.class);
2019-12-06 17:23:52 +01:00
2023-07-27 14:39:38 +02:00
/*
* (non-Javadoc)
*
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.
* CkanToZenodoPublisherService#publishOnZenodo(org.gcube.portlets.widgets.
* ckan2zenodopublisher.shared.wrapped.ZenodoItem)
2020-01-15 17:51:22 +01:00
*/
2019-12-09 17:17:05 +01:00
@Override
2023-07-28 14:41:30 +02:00
public DOI_dv publishOnZenodo(ZenodoItem zenodoItem) throws Exception {
LOG.info("publishOnZenodo called");
2019-12-09 17:17:05 +01:00
try {
2019-12-09 15:42:22 +01:00
2023-07-27 14:39:38 +02:00
if (isOutFromPortal()) {
2020-01-14 12:28:20 +01:00
PortalUtils.getCurrentContext(this.getThreadLocalRequest(), true);
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
}
LOG.debug("publishOnZenodo called with metadata: "+zenodoItem.getMetadata());
2023-07-27 14:39:38 +02:00
2020-08-26 11:11:42 +02:00
Ckan2Zenodo client = new Ckan2ZenodoImpl();
// Get the item representation
CkanItemDescriptor itemDescr = client.read(zenodoItem.getName());
// Get a preview of the deposition to be published
ZenodoDeposition zenodoDepositionPreview = null;
try {
zenodoDepositionPreview = client.translate(itemDescr);
} catch (ConfigurationException e) {
LOG.info("Using forceTranslation for: " + itemDescr.getItemUrl());
zenodoDepositionPreview = client.forceTranslation(itemDescr);
}
2023-07-27 14:39:38 +02:00
// (EVENTUALLY) UPDATING BASIC INFO CHANGED BY FORM ON CLIENT-SIDE
// Basic info
2020-01-15 17:51:22 +01:00
zenodoDepositionPreview.setTitle(zenodoItem.getTitle());
2023-07-27 14:39:38 +02:00
// Updating Deposition Metadata potentially changed on client-side
2020-01-13 17:48:41 +01:00
ZenodoMetadata metadata = zenodoItem.getMetadata();
2020-01-15 17:51:22 +01:00
DepositionMetadata depositionMetadata = zenodoDepositionPreview.getMetadata();
2023-07-27 14:39:38 +02:00
depositionMetadata = ItemToZenodoConverter.updateMetadataInfoOfDepositionMetadata(metadata,
depositionMetadata);
2020-01-15 17:51:22 +01:00
zenodoDepositionPreview.setMetadata(depositionMetadata);
2023-07-27 14:39:38 +02:00
// Loading Filtered resources according to VRE policies
2020-01-15 17:51:22 +01:00
List<CkanResource> filteredResources = loadFilterResources(zenodoItem.getName());
2023-07-27 14:39:38 +02:00
2020-01-15 17:51:22 +01:00
HashSet<CkanResource> toUpload = new HashSet<CkanResource>();
2020-01-10 17:07:03 +01:00
2023-07-27 14:39:38 +02:00
// Adding for publishing only the resources selected on clien-side
if (zenodoItem.getFiles() != null) {
2020-01-15 17:51:22 +01:00
for (ZenodoFile zf : zenodoItem.getFiles()) {
for (CkanResource ckanResource : filteredResources) {
2023-07-27 14:39:38 +02:00
if (zf.getId().compareTo(ckanResource.getId()) == 0 && zf.getIsAlreadyPublished() == false) {
if (CkanToZenodoUtil.isNotEmpty(zf.getFilename())) {
ckanResource.setName(zf.getFilename()); // only the filename can be changed on
// clien-side
2020-01-31 12:27:14 +01:00
toUpload.add(ckanResource);
}
2020-01-15 17:51:22 +01:00
}
}
}
}
2023-07-27 14:39:38 +02:00
// Actually publish to zenodo :
2020-01-10 17:07:03 +01:00
// Step 1 : metadata
2023-07-27 14:39:38 +02:00
client = new Ckan2ZenodoImpl();
LOG.debug("Calling updatedMetadata for: " + zenodoDepositionPreview);
2023-12-06 11:39:13 +01:00
//fixing issue #26166
Date thePublicationDate = zenodoDepositionPreview.getMetadata().getPublication_date();
LOG.debug("Publication_Date before update is: " + thePublicationDate);
2023-12-06 11:39:13 +01:00
2020-01-15 17:51:22 +01:00
zenodoDepositionPreview = client.updatedMetadata(zenodoDepositionPreview);
2020-01-10 17:07:03 +01:00
2023-07-27 14:39:38 +02:00
// Step 2 : publish Resources
if (toUpload.size() > 0) {
LOG.debug("Trying to publish the set of CKAN resources: " + toUpload.toString());
//fixing issue #26166
zenodoDepositionPreview.getMetadata().setPublication_date(thePublicationDate);
LOG.debug("Publication_Date before uploadFiles is: " + zenodoDepositionPreview.getMetadata().getPublication_date());
2023-07-27 14:39:38 +02:00
Future<ZenodoDeposition> future_Dep = client.uploadFiles(toUpload, zenodoDepositionPreview);
2023-07-27 14:39:38 +02:00
while (!future_Dep.isDone()) {
LOG.debug("Waiting for completed future computation (publishing of resources on Zenodo)...");
Thread.sleep(2000);
2020-01-15 17:51:22 +01:00
}
2023-07-27 14:39:38 +02:00
zenodoDepositionPreview = future_Dep.get(600, TimeUnit.SECONDS);
2020-01-15 17:51:22 +01:00
}
2023-07-27 14:39:38 +02:00
2020-01-10 17:07:03 +01:00
// Get the item representation
2023-07-27 14:39:38 +02:00
CkanItemDescriptor item = client.read(zenodoItem.getName());
2023-12-06 11:39:13 +01:00
//fixing issue #26166
LOG.debug("Publication_Date before publish is: " + thePublicationDate);
2023-12-06 11:39:13 +01:00
zenodoDepositionPreview.getMetadata().setPublication_date(thePublicationDate);
2023-07-27 14:39:38 +02:00
// Finalize
LOG.debug("Calling publish on Zenodo for: " + zenodoDepositionPreview);
2020-01-17 17:41:46 +01:00
zenodoDepositionPreview = client.publish(zenodoDepositionPreview, item);
2023-07-28 14:41:30 +02:00
return new DOI_dv(zenodoDepositionPreview.getDoi(), zenodoDepositionPreview.getDoi_url());
2023-07-27 14:39:38 +02:00
// throw new ZenodoException();
} catch (TimeoutException e) {
2020-01-31 16:17:50 +01:00
String error = "I'm waiting too time to upload the files to Zenodo. Check by yourself the result later";
2020-01-15 17:51:22 +01:00
LOG.error(error, e);
String clientError = String.format("%s", error);
throw new Exception(clientError);
2020-01-31 12:27:14 +01:00
} catch (ZenodoException e) {
2023-07-27 14:39:38 +02:00
String error = ZenodoException.class.getName() + " during upload to Zenodo the catalogue item: "
+ zenodoItem.getName();
2020-01-31 12:27:14 +01:00
LOG.error(error, e);
2023-07-27 14:39:38 +02:00
// String clientError = String.format("%s", e.getRemoteMessage());
2020-02-07 11:57:43 +01:00
ZenodoError zenodoError = new ZenodoError(e);
zenodoError.setRemoteMessage(e.getRemoteMessage());
zenodoError.setResponseHTTPCode(e.getResponseHTTPCode());
throw zenodoError;
2019-12-09 17:17:05 +01:00
} catch (Exception e) {
2023-07-27 14:39:38 +02:00
String error = "Error during upload to Zenodo the catalogue item: " + zenodoItem.getName();
2019-12-09 17:17:05 +01:00
LOG.error(error, e);
2020-01-27 17:56:16 +01:00
String clientError = String.format("%s. %s", error, e.getMessage());
2019-12-09 17:17:05 +01:00
throw new Exception(clientError);
}
}
2023-07-27 14:39:38 +02:00
/*
* (non-Javadoc)
*
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.
* CkanToZenodoPublisherService#convertToZenodoItem(org.gcube.portlets.widgets.
* ckan2zenodopublisher.shared.CatalogueItem)
2020-01-15 17:51:22 +01:00
*/
2019-12-09 17:17:05 +01:00
@Override
public ZenodoItem convertToZenodoItem(CatalogueItem item) throws Exception {
LOG.info("convertToZenodoItem called");
2019-12-09 17:17:05 +01:00
try {
2023-07-27 14:39:38 +02:00
if (isOutFromPortal()) {
2019-12-20 16:36:49 +01:00
PortalUtils.getCurrentContext(this.getThreadLocalRequest(), true);
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
}
2023-07-27 14:39:38 +02:00
2020-08-26 11:11:42 +02:00
Ckan2Zenodo client = new Ckan2ZenodoImpl();
// Get the item representation
CkanItemDescriptor itemDescr = client.read(item.getItemId());
// Get a preview of the deposition to be published
ZenodoDeposition zdDeposition = null;
ItemTranslateError itemTrError = null;
try {
zdDeposition = client.translate(itemDescr);
2023-07-27 15:09:39 +02:00
} catch (ConfigurationException | TransformationException e) {
2020-08-26 11:11:42 +02:00
LOG.info(ConfigurationException.class.getSimpleName() + " thrown, trying to use forceTranslation for: "
+ itemDescr.getName());
itemTrError = new ItemTranslateError(e.getMessage(), ERROR_TYPE.INFO);
zdDeposition = client.forceTranslation(itemDescr);
}
2023-07-27 15:09:39 +02:00
2023-07-27 15:27:26 +02:00
List<CkanResource> filteredResources = loadFilterResources(item.getItemId());
LOG.debug("Loaded filtered resources: " + filteredResources);
2023-07-27 14:39:38 +02:00
// Converting ZenodoDeposition to ZenodoItem
2020-08-26 11:11:42 +02:00
ZenodoItem zenodoItem = ItemToZenodoConverter.toZenodoItem(zdDeposition);
2023-07-27 14:39:38 +02:00
// Adding ItemTranslateError eventually occurred by calling client.translate
2020-08-26 11:11:42 +02:00
zenodoItem.setTranslateError(itemTrError);
2023-07-27 14:39:38 +02:00
// Adding the filtered resources
2020-01-15 17:51:22 +01:00
List<ZenodoFile> resources = ItemToZenodoConverter.toZenodoFiles(filteredResources, false);
zenodoItem.addFiles(resources);
2023-07-27 14:39:38 +02:00
// I'M SETTING THE ID BECAUSE THE NAME IS NULL INTO BEAN PASSED FROM PORTLET
// SIDE
// TODO I'M WAITING FOR itemName retrieved from ckan2zenodo library
zenodoItem.setName(item.getItemId());
2023-07-27 14:39:38 +02:00
2023-07-28 11:16:23 +02:00
LOG.debug("zenodoItem getLicenses as list: " + zenodoItem.getMetadata().getLicenseIDs());
2023-07-28 11:22:19 +02:00
//LOG.debug("setting licenses at null");
//zenodoItem.getMetadata().setLicenseIDs(null);
2023-07-28 10:55:03 +02:00
2023-07-27 14:39:38 +02:00
LOG.debug("Returning item: " + zenodoItem);
2020-01-10 17:07:03 +01:00
return zenodoItem;
2023-07-27 14:39:38 +02:00
} catch (ZenodoException e) {
2023-07-27 14:39:38 +02:00
String error = ZenodoException.class.getName() + " on converting the catalogue item with id: "
+ item.getItemId();
LOG.error(error, e);
2023-07-27 14:39:38 +02:00
// String clientError = String.format("%s. %s", error, e.getRemoteMessage());
2020-02-07 11:57:43 +01:00
ZenodoError zenodoError = new ZenodoError(e);
zenodoError.setRemoteMessage(e.getRemoteMessage());
zenodoError.setResponseHTTPCode(e.getResponseHTTPCode());
throw zenodoError;
2023-07-27 14:39:38 +02:00
2019-12-09 17:17:05 +01:00
} catch (Exception e) {
2023-07-27 14:39:38 +02:00
String error = "Error on converting the catalogue item with id: " + item.getItemId();
2019-12-09 17:17:05 +01:00
LOG.error(error, e);
2019-12-20 16:36:49 +01:00
String clientError = String.format("%s. %s", error, e.getMessage());
2019-12-09 17:17:05 +01:00
throw new Exception(clientError);
}
}
2023-07-27 14:39:38 +02:00
/*
* (non-Javadoc)
*
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.
* CkanToZenodoPublisherService#convertToZenodoItem(org.gcube.portlets.widgets.
* ckan2zenodopublisher.shared.CatalogueItem)
2020-01-17 12:46:32 +01:00
*/
@Override
public Map<String, String> readFieldsDescriptions() throws Exception {
LOG.info("readFieldsDescriptions called");
2020-01-17 12:46:32 +01:00
try {
2023-07-27 14:39:38 +02:00
if (isOutFromPortal()) {
2020-01-17 12:46:32 +01:00
PortalUtils.getCurrentContext(this.getThreadLocalRequest(), true);
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
}
2023-07-27 14:39:38 +02:00
2020-01-17 12:46:32 +01:00
return ZenodoFieldsDescriptionsReader.readProperties();
2023-07-27 14:39:38 +02:00
2020-01-17 12:46:32 +01:00
} catch (Exception e) {
String error = "Unable to read the file of properties with the fields desciptions";
LOG.error(error, e);
String clientError = String.format("%s. %s", error, e.getMessage());
throw new Exception(clientError);
}
}
2023-07-27 14:39:38 +02:00
@Override
public Boolean checkZenodoEnvironment() {
LOG.info("checkZenodoEnvironment called");
boolean isZenodoConfigured = false;
try {
2023-07-27 14:39:38 +02:00
if (isOutFromPortal()) {
PortalUtils.getCurrentContext(this.getThreadLocalRequest(), true);
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
}
2023-07-27 14:39:38 +02:00
LOG.info("readFieldsDescriptions called");
Ckan2Zenodo client = new Ckan2ZenodoImpl();
EnvironmentReport report = client.checkEnvironment();
2023-07-27 14:39:38 +02:00
LOG.info("EnvironmentReport returned: " + report);
isZenodoConfigured = report.isok();
} catch (Exception e) {
LOG.error("Error occurred during checkEnvironment: ", e);
}
LOG.info("Is Zenodo Environment configured? " + isZenodoConfigured);
return isZenodoConfigured;
}
2020-01-15 17:51:22 +01:00
/**
* Load filter resources.
*
* @param itemName the item name
* @return the list
2023-07-27 14:39:38 +02:00
* @throws GcatException the gcat exception
* @throws ConfigurationException the configuration exception
2020-01-15 17:51:22 +01:00
* @throws TransformationException the transformation exception
*/
2023-07-27 14:39:38 +02:00
private List<CkanResource> loadFilterResources(String itemName)
throws GcatException, ConfigurationException, TransformationException {
2020-08-26 11:11:42 +02:00
Ckan2Zenodo client = new Ckan2ZenodoImpl();
2020-01-15 17:51:22 +01:00
// Get the item representation
2023-07-27 14:39:38 +02:00
CkanItemDescriptor itemDescr = client.read(itemName);
// Filter resources according to VRE policies
2020-01-15 17:51:22 +01:00
return client.filterResources(itemDescr);
2020-01-10 17:07:03 +01:00
}
2023-07-27 14:39:38 +02:00
2019-12-20 16:36:49 +01:00
/**
2020-01-15 17:51:22 +01:00
* Online or in development mode?.
*
2019-12-20 16:36:49 +01:00
* @return true if you're running into the portal, false if in development
*/
private boolean isOutFromPortal() {
try {
UserLocalServiceUtil.getService();
return false;
2023-07-27 14:39:38 +02:00
} catch (com.liferay.portal.kernel.bean.BeanLocatorException ex) {
2019-12-20 16:36:49 +01:00
LOG.debug("Development Mode ON");
return true;
}
}
2023-07-27 14:39:38 +02:00
2019-12-06 17:23:52 +01:00
}