revisited the set/get licenses code

This commit is contained in:
Francesco Mangiacrapa 2023-07-27 14:39:38 +02:00
parent 04795542f8
commit 2d4488703a
6 changed files with 159 additions and 121 deletions

View File

@ -329,9 +329,9 @@ public class BasicInformationView extends Composite implements FormValidator {
// License
if (zMeta.getLicenses() != null) {
List<String> listLicensesIds = null;
List<String> selectedLicensesId = FieldUtil.toZenodoIds(zMeta.getLicenses().getSelectedValues());
if(zMeta.getLicenses().getSelectableValues()!=null) {
listLicensesIds = FieldUtil.toZenodoIds(zMeta.getLicenses().getSelectableValues());
List<String> selectedLicensesId = FieldUtil.toZenodoIds(zMeta.getLicenses());
if(zMeta.getLicenses()!=null) {
listLicensesIds = FieldUtil.toZenodoIds(zMeta.getLicenses());
FieldUtil.addValuesToListBox(field_license, listLicensesIds);
field_license.setEnabled(true);
}else {

View File

@ -163,7 +163,8 @@ public class Ckan2ZenodoViewManager {
String licenseId = basicForm.getField_license().getSelectedValue();
if(licenseId!=null) {
LicenseBean licenseBean = new LicenseBean(licenseId, null, null);
meta.setLicenses(new SerializableEnum<LicenseBean>(Arrays.asList(licenseBean),null));
//meta.setLicenses(new SerializableEnum<LicenseBean>(Arrays.asList(licenseBean),null));
meta.setLicenses(Arrays.asList(licenseBean));
}
//embargo date

View File

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

View File

@ -239,8 +239,10 @@ public class ItemToZenodoConverter {
if(depositionMetadata.getLicense()!=null) {
selectedLicenses = Arrays.asList(new LicenseBean(depositionMetadata.getLicense(),depositionMetadata.getLicense(), null));
}
if(selectedLicenses!=null)
zm.setLicenses(CkanToZenodoUtil.licenseToSerializableEnum(selectedLicenses, null)); //NEED TO ADD ALL LICENSES
if(selectedLicenses!=null) {
zm.setLicenses(selectedLicenses); //NEED TO ADD ALL LICENSES
//zm.setLicenses(CkanToZenodoUtil.licenseToSerializableEnum(selectedLicenses, null)); //NEED TO ADD ALL LICENSES
}
if(depositionMetadata.getEmbargo_date()!=null) {
@ -526,11 +528,11 @@ public class ItemToZenodoConverter {
}
//license
SerializableEnum<LicenseBean> licenses = metadata.getLicenses();
List<LicenseBean> licenses = metadata.getLicenses();
LOG.debug("Read licenses: "+licenses);
if(licenses!=null) {
try {
LicenseBean lB = licenses.getSelectedValues().get(0);
LicenseBean lB = licenses.get(0);
LOG.debug("Set license: "+lB);
depositionMetadata.setLicense(lB.getId());
}catch (Exception e) {

View File

@ -3,8 +3,6 @@ package org.gcube.portlets.widgets.ckan2zenodopublisher.shared;
import java.io.Serializable;
import java.util.List;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.LicenseBean;
/**
* The Class SerializableEnum.
*
@ -13,7 +11,7 @@ import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.LicenseBea
* Dec 10, 2019
* @param <T> the generic type
*/
public class SerializableEnum<E extends Serializable> implements Serializable{
public class SerializableEnum<E> implements Serializable{
/**
*
*/
@ -22,8 +20,6 @@ public class SerializableEnum<E extends Serializable> implements Serializable{
private List<E> selectedValues;
private List<E> selectableValues;
private LicenseBean licenseBean; //never used. Do not remove it, required for GWT serialization.
public SerializableEnum() {

View File

@ -77,7 +77,9 @@ public class ZenodoMetadata implements Serializable {
private List<ZenodoDateInterval> dates;
private String method; // TODO html
private SerializableEnum<LicenseBean> licenses;
private List<LicenseBean> licenses;
private List<LicenseBean> allLicenses;
/**
* Instantiates a new zenodo metadata.
@ -103,10 +105,20 @@ public class ZenodoMetadata implements Serializable {
this.upload_type = upload_type;
}
/**
* Gets the contributors types.
*
* @return the contributors types
*/
public SerializableEnum<String> getContributorsTypes() {
return contributorsTypes;
}
/**
* Sets the contributors types.
*
* @param contributorsTypes the new contributors types
*/
public void setContributorsTypes(SerializableEnum<String> contributorsTypes) {
this.contributorsTypes = contributorsTypes;
}
@ -872,21 +884,42 @@ public class ZenodoMetadata implements Serializable {
*
* @param licenses the new licenses
*/
public void setLicenses(SerializableEnum<LicenseBean> licenses) {
public void setLicenses(List<LicenseBean> licenses) {
this.licenses = licenses;
}
/**
* Gets the licenses.
* Gets the licenses. The selectable values
*
* @return the licenses
*/
public SerializableEnum<LicenseBean> getLicenses() {
public List<LicenseBean> getLicenses() {
return licenses;
}
/**
* Gets the all licenses.
*
* @return the all licenses
*/
public void getAllLicenses() {
this.allLicenses = licenses;
}
/**
* Sets the licenses.
*
* @param licenses the new licenses
*/
public void setAllLicenses(List<LicenseBean> licenses) {
this.allLicenses = licenses;
}
/**
* To string.
*
* @return the string
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();