2019-12-06 17:23:52 +01:00
package org.gcube.portlets.widgets.ckan2zenodopublisher.server ;
2020-01-10 17:07:03 +01:00
import java.util.ArrayList ;
2019-12-20 16:36:49 +01:00
import org.gcube.data.publishing.ckan2zenodo.Ckan2Zenodo ;
import org.gcube.data.publishing.ckan2zenodo.model.CkanItemDescriptor ;
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 ;
import org.gcube.data.publishing.ckan2zenodo.model.zenodo.FileDeposition ;
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 ;
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 .
* /
@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
@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-10 17:07:03 +01:00
ZenodoDeposition zdPreview = loadItemPreviewFromService ( zenodoItem . getName ( ) ) ;
//(EVENTUALLY) UPDATING INFO CHANGED BY FORM ON CLIENT-SIDE
//metadata
zdPreview . setTitle ( zenodoItem . getTitle ( ) ) ;
//files
if ( zenodoItem . getFiles ( ) ! = null ) {
ArrayList < FileDeposition > listOfFiles = new ArrayList < FileDeposition > ( zenodoItem . getFiles ( ) . size ( ) ) ;
for ( ZenodoFile zf : zenodoItem . getFiles ( ) ) {
FileDeposition fileDepos = ItemToZenodoConverter . toServiceFileDeposition ( zf ) ;
listOfFiles . add ( fileDepos ) ;
}
zdPreview . setFiles ( listOfFiles ) ;
}
2019-12-18 11:58:49 +01:00
//Ckan2ZenodoImpl.publish(item.getItemName());
2020-01-10 17:07:03 +01:00
//Eventually update values
//zdPreview.getMetadata().setAccess_conditions("Ask me");
//Actually publish to zenodo :
// Step 1 : metadata
Ckan2Zenodo client = Ckan2Zenodo . get ( ) ;
CkanItemDescriptor item = client . read ( zenodoItem . getName ( ) ) ;
zdPreview = client . updatedMetadata ( zdPreview ) ;
//Step 2 : publish Resources
// Future<ZenodoDeposition> future_Dep=client.uploadFiles(Collections.singleton(toFilter.get(0)), zdPreview);
// ZenodoDeposition preview = future_Dep.get();
//Finalize
// Get the item representation
client . publish ( zdPreview , item ) ;
return " OK " ;
2019-12-09 17:17:05 +01:00
} catch ( Exception e ) {
2020-01-10 17:07:03 +01:00
String error = " Error on 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 ) ;
}
}
@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
//Filter resources according to VRE policies
//List<CkanResource> toFilterFiles=client.filterResources(itemDescr);
//zdPreview.setFiles(toFilterFiles);
2020-01-10 17:07:03 +01:00
ZenodoItem zenodoItem = ItemToZenodoConverter . toZenodoItem ( zdPreview ) ;
zenodoItem . setName ( item . getItemName ( ) ) ;
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
* @return
* @throws GcatException
* @throws TransformationException
* @throws ConfigurationException
* @throws ZenodoException
* @throws InvalidItemException
* /
private ZenodoDeposition loadItemPreviewFromService ( String itemName ) throws GcatException , InvalidItemException , ZenodoException , ConfigurationException , TransformationException {
Ckan2Zenodo client = Ckan2Zenodo . get ( ) ;
// Get the item representation
CkanItemDescriptor itemDescr = client . read ( itemName ) ;
//Get a preview of the deposition to be published
return client . translate ( itemDescr ) ;
}
2019-12-20 16:36:49 +01:00
/ * *
* Online or in development mode ?
* @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
}