2019-12-06 17:23:52 +01:00
package org.gcube.portlets.widgets.ckan2zenodopublisher.server ;
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-15 17:51:22 +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.InvalidItemException ;
import org.gcube.data.publishing.ckan2zenodo.model.faults.TransformationException ;
import org.gcube.data.publishing.ckan2zenodo.model.faults.ZenodoException ;
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 ;
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.CkanToZendoPublisherWidgetConstant ;
2019-12-06 17:23:52 +01:00
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.CkanToZenodoPublisherService ;
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-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 )
*
* 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
2019-12-09 17:17:05 +01:00
2020-01-15 17:51:22 +01:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . ckan2zenodopublisher . client . CkanToZenodoPublisherService # publishOnZenodo ( org . gcube . portlets . widgets . ckan2zenodopublisher . shared . wrapped . ZenodoItem )
* /
2019-12-09 17:17:05 +01:00
@Override
2020-01-10 17:07:03 +01:00
public String publishOnZenodo ( ZenodoItem zenodoItem ) throws Exception {
2019-12-06 17:23:52 +01:00
2019-12-09 17:17:05 +01:00
try {
2019-12-09 15:42:22 +01:00
2020-01-14 12:28:20 +01:00
if ( isOutFromPortal ( ) ) {
PortalUtils . getCurrentContext ( this . getThreadLocalRequest ( ) , true ) ;
PortalUtils . getCurrentToken ( this . getThreadLocalRequest ( ) , true ) ;
}
2020-01-15 17:51:22 +01:00
ZenodoDeposition zenodoDepositionPreview = loadItemPreviewFromService ( zenodoItem . getName ( ) ) ;
//(EVENTUALLY) UPDATING BASIC INFO CHANGED BY FORM ON CLIENT-SIDE
2020-01-13 17:48:41 +01:00
//Basic info
2020-01-15 17:51:22 +01:00
zenodoDepositionPreview . setTitle ( zenodoItem . getTitle ( ) ) ;
2020-01-14 12:28:20 +01: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 ( ) ;
2020-01-13 17:48:41 +01:00
depositionMetadata = ItemToZenodoConverter . updateMetadataInfoOfDepositionMetadata ( metadata , depositionMetadata ) ;
2020-01-15 17:51:22 +01:00
zenodoDepositionPreview . setMetadata ( depositionMetadata ) ;
//Loading Filtered resources according to VRE policies
List < CkanResource > filteredResources = loadFilterResources ( zenodoItem . getName ( ) ) ;
2020-01-10 17:07:03 +01:00
2020-01-15 17:51:22 +01:00
HashSet < CkanResource > toUpload = new HashSet < CkanResource > ( ) ;
2020-01-10 17:07:03 +01:00
2020-01-15 17:51:22 +01:00
//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 ) {
toUpload . add ( ckanResource ) ;
}
}
}
}
2020-01-10 17:07:03 +01:00
//Actually publish to zenodo :
// Step 1 : metadata
2020-01-15 17:51:22 +01:00
Ckan2Zenodo client = new Ckan2ZenodoImpl ( ) ;
zenodoDepositionPreview = client . updatedMetadata ( zenodoDepositionPreview ) ;
2020-01-10 17:07:03 +01:00
//Step 2 : publish Resources
2020-01-15 17:51:22 +01:00
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 ( 180 , TimeUnit . SECONDS ) ;
}
2020-01-10 17:07:03 +01:00
// Get the item representation
2020-01-15 17:51:22 +01:00
CkanItemDescriptor item = client . read ( zenodoItem . getName ( ) ) ;
2020-01-10 17:07:03 +01:00
2020-01-15 17:51:22 +01:00
//Finalize
client . publish ( zenodoDepositionPreview , item ) ;
return zenodoDepositionPreview . toString ( ) ;
2019-12-09 17:17:05 +01:00
2020-01-15 17:51:22 +01:00
} catch ( TimeoutException e ) {
String error = " I'm waiting too time for publishing on Zenodo the catalogue item: " + zenodoItem . getName ( ) + " . Check by yourself the result later " ;
LOG . error ( error , e ) ;
String clientError = String . format ( " %s " , error ) ;
throw new Exception ( clientError ) ;
2019-12-09 17:17:05 +01:00
} catch ( Exception e ) {
2020-01-14 12:28:20 +01:00
String error = " Error when publishing on Zenodo the catalogue item: " + zenodoItem . getName ( ) ;
2019-12-09 17:17:05 +01:00
LOG . error ( error , e ) ;
String clientError = String . format ( " %s. %s " , error , CkanToZendoPublisherWidgetConstant . MSG_OF_ERROR_REFRESH_AND_TRY_AGAIN_OR_CONTACT_THE_SUPPORT ) ;
throw new Exception ( clientError ) ;
}
}
2020-01-15 17:51:22 +01:00
/ * ( non - Javadoc )
* @see org . gcube . portlets . widgets . ckan2zenodopublisher . client . CkanToZenodoPublisherService # convertToZenodoItem ( org . gcube . portlets . widgets . ckan2zenodopublisher . shared . CatalogueItem )
* /
2019-12-09 17:17:05 +01:00
@Override
public ZenodoItem convertToZenodoItem ( CatalogueItem item ) throws Exception {
try {
2019-12-20 16:36:49 +01:00
if ( isOutFromPortal ( ) ) {
PortalUtils . getCurrentContext ( this . getThreadLocalRequest ( ) , true ) ;
PortalUtils . getCurrentToken ( this . getThreadLocalRequest ( ) , true ) ;
}
2020-01-09 16:00:46 +01:00
//Get a preview of the deposition to be published
2020-01-10 17:07:03 +01:00
ZenodoDeposition zdPreview = loadItemPreviewFromService ( item . getItemName ( ) ) ;
2020-01-09 16:00:46 +01:00
2020-01-15 17:51:22 +01:00
//Loading Filtered resources according to VRE policies
List < CkanResource > filteredResources = loadFilterResources ( item . getItemName ( ) ) ;
LOG . debug ( " Loaded filtered resources: " + filteredResources ) ;
//Converting ZenodoDeposition to ZenodoItem
2020-01-10 17:07:03 +01:00
ZenodoItem zenodoItem = ItemToZenodoConverter . toZenodoItem ( zdPreview ) ;
2020-01-15 17:51:22 +01:00
//Adding the filtered resources
List < ZenodoFile > resources = ItemToZenodoConverter . toZenodoFiles ( filteredResources , false ) ;
zenodoItem . addFiles ( resources ) ;
2020-01-10 17:07:03 +01:00
zenodoItem . setName ( item . getItemName ( ) ) ;
2020-01-13 17:48:41 +01:00
LOG . debug ( " Returning item: " + zenodoItem ) ;
2020-01-10 17:07:03 +01:00
return zenodoItem ;
2019-12-20 16:36:49 +01:00
// //TO TEST
2020-01-09 16:00:46 +01:00
// ZenodoDeposition zd = new ZenodoDeposition();
// zd.setTitle("My beautiful title");
// ArrayList<FileDeposition> files = new ArrayList<FileDeposition>();
// for (int i = 0; i < 5; i++) {
// FileDeposition fd = new FileDeposition();
// int index = i+1;
// fd.setFilename("My file "+index);
// files.add(fd);
// }
// zd.setFiles(files);
// DepositionMetadata metadata = new DepositionMetadata(UploadType.dataset, new Date(), "My title", new ArrayList<Creator>(), "My desr", AccessRights.open);
// metadata.setDescription("My beautiful description");
// List<RelatedIdentifier> related_identifiers = new ArrayList<RelatedIdentifier>(1);
// related_identifiers.add(new RelatedIdentifier("12345", Relation.cites));
// metadata.setRelated_identifiers(related_identifiers);
// zd.setMetadata(metadata);
// return ItemToZenodoConverter.toZenodoItem(zd);
2019-12-20 16:36:49 +01:00
2019-12-09 17:17:05 +01:00
} catch ( Exception e ) {
String error = " Error on converting the catalogue item: " + item . getItemName ( ) ;
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 ) ;
}
}
2019-12-20 16:36:49 +01:00
2020-01-10 17:07:03 +01:00
/ * *
* Load item preview from service .
*
* @param itemName the item name
2020-01-15 17:51:22 +01:00
* @return the zenodo deposition
* @throws GcatException the gcat exception
* @throws InvalidItemException the invalid item exception
* @throws ZenodoException the zenodo exception
* @throws ConfigurationException the configuration exception
* @throws TransformationException the transformation exception
2020-01-10 17:07:03 +01:00
* /
private ZenodoDeposition loadItemPreviewFromService ( String itemName ) throws GcatException , InvalidItemException , ZenodoException , ConfigurationException , TransformationException {
2020-01-15 17:51:22 +01:00
Ckan2Zenodo client = new Ckan2ZenodoImpl ( ) ;
2020-01-10 17:07:03 +01:00
// Get the item representation
CkanItemDescriptor itemDescr = client . read ( itemName ) ;
//Get a preview of the deposition to be published
2020-01-15 17:51:22 +01:00
return client . translate ( itemDescr ) ;
}
/ * *
* Load filter resources .
*
* @param itemName the item name
* @return the list
* @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 {
2020-01-14 12:28:20 +01:00
2020-01-15 17:51:22 +01:00
Ckan2Zenodo client = new Ckan2ZenodoImpl ( ) ;
// Get the item representation
CkanItemDescriptor itemDescr = client . read ( itemName ) ;
2020-01-14 12:28:20 +01:00
//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
}
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 ;
}
catch ( com . liferay . portal . kernel . bean . BeanLocatorException ex ) {
LOG . debug ( " Development Mode ON " ) ;
return true ;
}
}
2019-12-06 17:23:52 +01:00
}