|
|
|
@ -39,27 +39,30 @@ import com.liferay.portal.service.UserLocalServiceUtil;
|
|
|
|
|
*
|
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
|
|
|
*
|
|
|
|
|
* Jan 15, 2020
|
|
|
|
|
* Jan 15, 2020
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("serial")
|
|
|
|
|
public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet implements CkanToZenodoPublisherService {
|
|
|
|
|
|
|
|
|
|
private static Logger LOG = LoggerFactory.getLogger(CkanToZenodoPublisherServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
|
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.CkanToZenodoPublisherService#publishOnZenodo(org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoItem)
|
|
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.
|
|
|
|
|
* CkanToZenodoPublisherService#publishOnZenodo(org.gcube.portlets.widgets.
|
|
|
|
|
* ckan2zenodopublisher.shared.wrapped.ZenodoItem)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public String publishOnZenodo(ZenodoItem zenodoItem) throws Exception {
|
|
|
|
|
LOG.info("publishOnZenodo called");
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
if(isOutFromPortal()) {
|
|
|
|
|
if (isOutFromPortal()) {
|
|
|
|
|
PortalUtils.getCurrentContext(this.getThreadLocalRequest(), true);
|
|
|
|
|
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ckan2Zenodo client = new Ckan2ZenodoImpl();
|
|
|
|
|
// Get the item representation
|
|
|
|
|
CkanItemDescriptor itemDescr = client.read(zenodoItem.getName());
|
|
|
|
@ -71,97 +74,104 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
|
|
|
|
|
LOG.info("Using forceTranslation for: " + itemDescr.getItemUrl());
|
|
|
|
|
zenodoDepositionPreview = client.forceTranslation(itemDescr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//(EVENTUALLY) UPDATING BASIC INFO CHANGED BY FORM ON CLIENT-SIDE
|
|
|
|
|
//Basic info
|
|
|
|
|
|
|
|
|
|
// (EVENTUALLY) UPDATING BASIC INFO CHANGED BY FORM ON CLIENT-SIDE
|
|
|
|
|
// Basic info
|
|
|
|
|
zenodoDepositionPreview.setTitle(zenodoItem.getTitle());
|
|
|
|
|
|
|
|
|
|
//Updating Deposition Metadata potentially changed on client-side
|
|
|
|
|
|
|
|
|
|
// Updating Deposition Metadata potentially changed on client-side
|
|
|
|
|
ZenodoMetadata metadata = zenodoItem.getMetadata();
|
|
|
|
|
DepositionMetadata depositionMetadata = zenodoDepositionPreview.getMetadata();
|
|
|
|
|
depositionMetadata = ItemToZenodoConverter.updateMetadataInfoOfDepositionMetadata(metadata, depositionMetadata);
|
|
|
|
|
depositionMetadata = ItemToZenodoConverter.updateMetadataInfoOfDepositionMetadata(metadata,
|
|
|
|
|
depositionMetadata);
|
|
|
|
|
zenodoDepositionPreview.setMetadata(depositionMetadata);
|
|
|
|
|
|
|
|
|
|
//Loading Filtered resources according to VRE policies
|
|
|
|
|
|
|
|
|
|
// Loading Filtered resources according to VRE policies
|
|
|
|
|
List<CkanResource> filteredResources = loadFilterResources(zenodoItem.getName());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HashSet<CkanResource> toUpload = new HashSet<CkanResource>();
|
|
|
|
|
|
|
|
|
|
//Adding for publishing only the resources selected on clien-side
|
|
|
|
|
if(zenodoItem.getFiles()!=null) {
|
|
|
|
|
// Adding for publishing only the resources selected on clien-side
|
|
|
|
|
if (zenodoItem.getFiles() != null) {
|
|
|
|
|
for (ZenodoFile zf : zenodoItem.getFiles()) {
|
|
|
|
|
for (CkanResource ckanResource : filteredResources) {
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
toUpload.add(ckanResource);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Actually publish to zenodo :
|
|
|
|
|
|
|
|
|
|
// Actually publish to zenodo :
|
|
|
|
|
// Step 1 : metadata
|
|
|
|
|
client= new Ckan2ZenodoImpl();
|
|
|
|
|
LOG.debug("Calling updatedMetadata for: "+zenodoDepositionPreview);
|
|
|
|
|
client = new Ckan2ZenodoImpl();
|
|
|
|
|
LOG.debug("Calling updatedMetadata for: " + zenodoDepositionPreview);
|
|
|
|
|
zenodoDepositionPreview = client.updatedMetadata(zenodoDepositionPreview);
|
|
|
|
|
|
|
|
|
|
//Step 2 : publish Resources
|
|
|
|
|
if(toUpload.size()>0) {
|
|
|
|
|
LOG.debug("Trying to publish the set of CKAN resources: "+toUpload.toString());
|
|
|
|
|
Future<ZenodoDeposition> future_Dep=client.uploadFiles(toUpload, zenodoDepositionPreview);
|
|
|
|
|
while(!future_Dep.isDone()) {
|
|
|
|
|
LOG.debug("Waiting for completed future computation (publishing of resources on Zenodo)...");
|
|
|
|
|
Thread.sleep(2000);
|
|
|
|
|
// Step 2 : publish Resources
|
|
|
|
|
if (toUpload.size() > 0) {
|
|
|
|
|
LOG.debug("Trying to publish the set of CKAN resources: " + toUpload.toString());
|
|
|
|
|
Future<ZenodoDeposition> future_Dep = client.uploadFiles(toUpload, zenodoDepositionPreview);
|
|
|
|
|
while (!future_Dep.isDone()) {
|
|
|
|
|
LOG.debug("Waiting for completed future computation (publishing of resources on Zenodo)...");
|
|
|
|
|
Thread.sleep(2000);
|
|
|
|
|
}
|
|
|
|
|
zenodoDepositionPreview = future_Dep.get(600,TimeUnit.SECONDS);
|
|
|
|
|
zenodoDepositionPreview = future_Dep.get(600, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get the item representation
|
|
|
|
|
CkanItemDescriptor item=client.read(zenodoItem.getName());
|
|
|
|
|
//Finalize
|
|
|
|
|
LOG.debug("Calling publish on Zenodo for: "+zenodoDepositionPreview);
|
|
|
|
|
CkanItemDescriptor item = client.read(zenodoItem.getName());
|
|
|
|
|
// Finalize
|
|
|
|
|
LOG.debug("Calling publish on Zenodo for: " + zenodoDepositionPreview);
|
|
|
|
|
zenodoDepositionPreview = client.publish(zenodoDepositionPreview, item);
|
|
|
|
|
return zenodoDepositionPreview.getDoi();
|
|
|
|
|
|
|
|
|
|
//throw new ZenodoException();
|
|
|
|
|
|
|
|
|
|
}catch (TimeoutException e) {
|
|
|
|
|
|
|
|
|
|
// throw new ZenodoException();
|
|
|
|
|
|
|
|
|
|
} catch (TimeoutException e) {
|
|
|
|
|
String error = "I'm waiting too time to upload the files to Zenodo. Check by yourself the result later";
|
|
|
|
|
LOG.error(error, e);
|
|
|
|
|
String clientError = String.format("%s", error);
|
|
|
|
|
throw new Exception(clientError);
|
|
|
|
|
} catch (ZenodoException e) {
|
|
|
|
|
String error = ZenodoException.class.getName()+" during upload to Zenodo the catalogue item: "+zenodoItem.getName();
|
|
|
|
|
String error = ZenodoException.class.getName() + " during upload to Zenodo the catalogue item: "
|
|
|
|
|
+ zenodoItem.getName();
|
|
|
|
|
LOG.error(error, e);
|
|
|
|
|
//String clientError = String.format("%s", e.getRemoteMessage());
|
|
|
|
|
// String clientError = String.format("%s", e.getRemoteMessage());
|
|
|
|
|
ZenodoError zenodoError = new ZenodoError(e);
|
|
|
|
|
zenodoError.setRemoteMessage(e.getRemoteMessage());
|
|
|
|
|
zenodoError.setResponseHTTPCode(e.getResponseHTTPCode());
|
|
|
|
|
throw zenodoError;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
String error = "Error during upload to Zenodo the catalogue item: "+zenodoItem.getName();
|
|
|
|
|
String error = "Error during upload to Zenodo the catalogue item: " + zenodoItem.getName();
|
|
|
|
|
LOG.error(error, e);
|
|
|
|
|
String clientError = String.format("%s. %s", error, e.getMessage());
|
|
|
|
|
throw new Exception(clientError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
|
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.CkanToZenodoPublisherService#convertToZenodoItem(org.gcube.portlets.widgets.ckan2zenodopublisher.shared.CatalogueItem)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.
|
|
|
|
|
* CkanToZenodoPublisherService#convertToZenodoItem(org.gcube.portlets.widgets.
|
|
|
|
|
* ckan2zenodopublisher.shared.CatalogueItem)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public ZenodoItem convertToZenodoItem(CatalogueItem item) throws Exception {
|
|
|
|
|
LOG.info("convertToZenodoItem called");
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
if(isOutFromPortal()) {
|
|
|
|
|
|
|
|
|
|
if (isOutFromPortal()) {
|
|
|
|
|
PortalUtils.getCurrentContext(this.getThreadLocalRequest(), true);
|
|
|
|
|
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ckan2Zenodo client = new Ckan2ZenodoImpl();
|
|
|
|
|
// Get the item representation
|
|
|
|
|
CkanItemDescriptor itemDescr = client.read(item.getItemId());
|
|
|
|
@ -177,74 +187,70 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
|
|
|
|
|
zdDeposition = client.forceTranslation(itemDescr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Loading Filtered resources according to VRE policies
|
|
|
|
|
// Loading Filtered resources according to VRE policies
|
|
|
|
|
List<CkanResource> filteredResources = loadFilterResources(item.getItemId());
|
|
|
|
|
|
|
|
|
|
LOG.debug("Loaded filtered resources: "+filteredResources);
|
|
|
|
|
|
|
|
|
|
//Converting ZenodoDeposition to ZenodoItem
|
|
|
|
|
LOG.debug("Loaded filtered resources: " + filteredResources);
|
|
|
|
|
|
|
|
|
|
// Converting ZenodoDeposition to ZenodoItem
|
|
|
|
|
ZenodoItem zenodoItem = ItemToZenodoConverter.toZenodoItem(zdDeposition);
|
|
|
|
|
|
|
|
|
|
//Adding ItemTranslateError eventually occurred by calling client.translate
|
|
|
|
|
|
|
|
|
|
// Adding ItemTranslateError eventually occurred by calling client.translate
|
|
|
|
|
zenodoItem.setTranslateError(itemTrError);
|
|
|
|
|
|
|
|
|
|
//Adding the filtered resources
|
|
|
|
|
|
|
|
|
|
// Adding the filtered resources
|
|
|
|
|
List<ZenodoFile> resources = ItemToZenodoConverter.toZenodoFiles(filteredResources, false);
|
|
|
|
|
zenodoItem.addFiles(resources);
|
|
|
|
|
|
|
|
|
|
//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());
|
|
|
|
|
|
|
|
|
|
LOG.info("zenodoItem setting contributors and image not null");
|
|
|
|
|
// zenodoItem.getMetadata().setContributors(null);
|
|
|
|
|
// zenodoItem.getMetadata().setContributorsTypes(null);
|
|
|
|
|
// zenodoItem.getMetadata().setImage_type(null);
|
|
|
|
|
|
|
|
|
|
// zenodoItem.getMetadata().setUpload_type(null);
|
|
|
|
|
// zenodoItem.getMetadata().setPublication_type(null);
|
|
|
|
|
|
|
|
|
|
LOG.info("zenodoItem setLicenses not null as LicenseBean Snew");
|
|
|
|
|
LOG.info("zenodoItem getLicenses: "+zenodoItem.getMetadata().getLicenses());
|
|
|
|
|
//zenodoItem.getMetadata().setLicenses(null);
|
|
|
|
|
|
|
|
|
|
LOG.debug("Returning item: "+zenodoItem);
|
|
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
|
|
|
|
|
LOG.debug("zenodoItem getLicenses as list: " + zenodoItem.getMetadata().getLicenses());
|
|
|
|
|
|
|
|
|
|
LOG.debug("Returning item: " + zenodoItem);
|
|
|
|
|
|
|
|
|
|
return zenodoItem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (ZenodoException e) {
|
|
|
|
|
String error = ZenodoException.class.getName()+" on converting the catalogue item with id: "+item.getItemId();
|
|
|
|
|
String error = ZenodoException.class.getName() + " on converting the catalogue item with id: "
|
|
|
|
|
+ item.getItemId();
|
|
|
|
|
LOG.error(error, e);
|
|
|
|
|
//String clientError = String.format("%s. %s", error, e.getRemoteMessage());
|
|
|
|
|
// String clientError = String.format("%s. %s", error, e.getRemoteMessage());
|
|
|
|
|
ZenodoError zenodoError = new ZenodoError(e);
|
|
|
|
|
zenodoError.setRemoteMessage(e.getRemoteMessage());
|
|
|
|
|
zenodoError.setResponseHTTPCode(e.getResponseHTTPCode());
|
|
|
|
|
throw zenodoError;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
String error = "Error on converting the catalogue item with id: "+item.getItemId();
|
|
|
|
|
String error = "Error on converting the catalogue item with id: " + item.getItemId();
|
|
|
|
|
LOG.error(error, e);
|
|
|
|
|
String clientError = String.format("%s. %s", error, e.getMessage());
|
|
|
|
|
throw new Exception(clientError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
|
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.CkanToZenodoPublisherService#convertToZenodoItem(org.gcube.portlets.widgets.ckan2zenodopublisher.shared.CatalogueItem)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* (non-Javadoc)
|
|
|
|
|
*
|
|
|
|
|
* @see org.gcube.portlets.widgets.ckan2zenodopublisher.client.
|
|
|
|
|
* CkanToZenodoPublisherService#convertToZenodoItem(org.gcube.portlets.widgets.
|
|
|
|
|
* ckan2zenodopublisher.shared.CatalogueItem)
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Map<String, String> readFieldsDescriptions() throws Exception {
|
|
|
|
|
LOG.info("readFieldsDescriptions called");
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
if(isOutFromPortal()) {
|
|
|
|
|
|
|
|
|
|
if (isOutFromPortal()) {
|
|
|
|
|
PortalUtils.getCurrentContext(this.getThreadLocalRequest(), true);
|
|
|
|
|
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ZenodoFieldsDescriptionsReader.readProperties();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
String error = "Unable to read the file of properties with the fields desciptions";
|
|
|
|
|
LOG.error(error, e);
|
|
|
|
@ -253,23 +259,23 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean checkZenodoEnvironment() {
|
|
|
|
|
LOG.info("checkZenodoEnvironment called");
|
|
|
|
|
boolean isZenodoConfigured = false;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
if(isOutFromPortal()) {
|
|
|
|
|
|
|
|
|
|
if (isOutFromPortal()) {
|
|
|
|
|
PortalUtils.getCurrentContext(this.getThreadLocalRequest(), true);
|
|
|
|
|
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LOG.info("readFieldsDescriptions called");
|
|
|
|
|
Ckan2Zenodo client = new Ckan2ZenodoImpl();
|
|
|
|
|
EnvironmentReport report = client.checkEnvironment();
|
|
|
|
|
LOG.info("EnvironmentReport returned: "+report);
|
|
|
|
|
LOG.info("EnvironmentReport returned: " + report);
|
|
|
|
|
isZenodoConfigured = report.isok();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOG.error("Error occurred during checkEnvironment: ", e);
|
|
|
|
@ -284,19 +290,20 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
|
|
|
|
|
*
|
|
|
|
|
* @param itemName the item name
|
|
|
|
|
* @return the list
|
|
|
|
|
* @throws GcatException the gcat exception
|
|
|
|
|
* @throws ConfigurationException the configuration exception
|
|
|
|
|
* @throws GcatException the gcat exception
|
|
|
|
|
* @throws ConfigurationException the configuration exception
|
|
|
|
|
* @throws TransformationException the transformation exception
|
|
|
|
|
*/
|
|
|
|
|
private List<CkanResource> loadFilterResources(String itemName) throws GcatException, ConfigurationException, TransformationException {
|
|
|
|
|
|
|
|
|
|
private List<CkanResource> loadFilterResources(String itemName)
|
|
|
|
|
throws GcatException, ConfigurationException, TransformationException {
|
|
|
|
|
|
|
|
|
|
Ckan2Zenodo client = new Ckan2ZenodoImpl();
|
|
|
|
|
// Get the item representation
|
|
|
|
|
CkanItemDescriptor itemDescr=client.read(itemName);
|
|
|
|
|
//Filter resources according to VRE policies
|
|
|
|
|
CkanItemDescriptor itemDescr = client.read(itemName);
|
|
|
|
|
// Filter resources according to VRE policies
|
|
|
|
|
return client.filterResources(itemDescr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Online or in development mode?.
|
|
|
|
|
*
|
|
|
|
@ -306,11 +313,10 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
|
|
|
|
|
try {
|
|
|
|
|
UserLocalServiceUtil.getService();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (com.liferay.portal.kernel.bean.BeanLocatorException ex) {
|
|
|
|
|
} catch (com.liferay.portal.kernel.bean.BeanLocatorException ex) {
|
|
|
|
|
LOG.debug("Development Mode ON");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|