2016-05-26 18:12:53 +02:00
package org.gcube.portlets.widgets.ckandatapublisherwidget.server ;
import java.util.ArrayList ;
2016-06-06 17:24:52 +02:00
import java.util.Calendar ;
2016-05-26 18:12:53 +02:00
import java.util.List ;
2016-05-27 19:08:03 +02:00
import java.util.Map ;
2016-06-08 18:31:54 +02:00
2016-07-04 12:44:20 +02:00
import javax.servlet.http.HttpSession ;
2016-06-16 18:09:54 +02:00
import org.gcube.application.framework.core.session.ASLSession ;
import org.gcube.application.framework.core.session.SessionManager ;
2016-05-27 19:08:03 +02:00
import org.gcube.common.homelibrary.home.HomeLibrary ;
import org.gcube.common.homelibrary.home.workspace.Workspace ;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem ;
2016-09-12 17:58:50 +02:00
import org.gcube.common.homelibrary.home.workspace.catalogue.WorkspaceCatalogue ;
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem ;
2016-07-02 23:58:36 +02:00
import org.gcube.datacatalogue.ckanutillibrary.CKanUtils ;
2016-08-02 09:46:58 +02:00
import org.gcube.datacatalogue.ckanutillibrary.CkanUtilsFactory ;
2016-06-14 12:35:10 +02:00
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean ;
2016-06-16 18:09:54 +02:00
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper ;
2016-05-26 18:12:53 +02:00
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherService ;
2016-07-20 19:14:20 +02:00
import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.Utils ;
2016-05-27 19:08:03 +02:00
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.DatasetMetadataBean ;
2016-05-26 18:12:53 +02:00
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean ;
2016-06-09 18:58:19 +02:00
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataProfileBean ;
2016-07-20 22:41:15 +02:00
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.OrganizationBean ;
2016-06-14 12:35:10 +02:00
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper ;
2016-05-27 19:08:03 +02:00
import org.gcube.vomanagement.usermanagement.UserManager ;
2016-06-21 14:36:49 +02:00
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager ;
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2016-05-26 18:12:53 +02:00
import org.slf4j.LoggerFactory ;
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
import com.liferay.portal.service.UserLocalServiceUtil ;
2016-07-13 16:11:15 +02:00
import eu.trentorise.opendata.jackan.model.CkanLicense ;
2016-05-26 18:12:53 +02:00
/ * *
* Server side of the data publisher .
* @author Costantino Perciante at ISTI - CNR ( costantino . perciante @isti.cnr.it )
* /
@SuppressWarnings ( " serial " )
public class CKANPublisherServicesImpl extends RemoteServiceServlet implements CKanPublisherService {
// Logger
2016-06-06 17:24:52 +02:00
private static final org . slf4j . Logger logger = LoggerFactory . getLogger ( CKANPublisherServicesImpl . class ) ;
2016-06-18 10:13:37 +02:00
2016-06-16 18:09:54 +02:00
public static final String TEST_SCOPE = " /gcube/devsec/devVRE " ;
2016-06-29 14:00:19 +02:00
public static final String TEST_USER = " test.user " ;
2016-07-06 18:41:43 +02:00
private final static String TEST_SEC_TOKEN = " a1e19695-467f-42b8-966d-bf83dd2382ef " ;
2016-06-18 10:13:37 +02:00
2016-07-02 23:40:45 +02:00
// ckan keys for ASL
private static final String CKAN_TOKEN_KEY = " ckanToken " ;
private static final String CKAN_LICENSES_KEY = " ckanLicenses " ; // licenses
private static final String CKAN_ORGANIZATIONS_PUBLISH_KEY = " ckanOrganizationsPublish " ; // here he can publish
private static final String CKAN_PROFILES_KEY = " ckanProfiles " ; // product profiles
2016-09-12 17:58:50 +02:00
private final static String SCOPE_CLIENT_PORTLET_URL = " currentClientUrlPortletScope " ; // set by the ckan portlet in session according the user's current url location
2016-07-02 23:40:45 +02:00
2016-06-27 11:12:15 +02:00
/ * *
2016-07-20 19:14:20 +02:00
* Retrieve an instance of the library for the scope
* @param scope if it is null it is evaluated from the session
2016-06-27 11:12:15 +02:00
* @return
* /
2016-07-20 19:14:20 +02:00
public CKanUtils getCkanUtilsObj ( String scope ) {
2016-06-18 10:13:37 +02:00
2016-07-04 16:13:00 +02:00
CKanUtils instance = null ;
2016-07-04 16:24:00 +02:00
try {
2016-07-20 19:14:20 +02:00
String scopeInWhichDiscover = ( scope ! = null & & ! scope . isEmpty ( ) ) ? scope : getASLSession ( ) . getScope ( ) ;
logger . debug ( " Discovering ckan instance into scope " + scopeInWhichDiscover ) ;
2016-08-02 09:46:58 +02:00
instance = CkanUtilsFactory . getFactory ( ) . getUtilsPerScope ( scopeInWhichDiscover ) ;
2016-07-04 16:24:00 +02:00
} catch ( Exception e ) {
logger . error ( " Unable to retrieve ckan utils " , e ) ;
2016-07-04 16:13:51 +02:00
}
2016-06-27 11:12:15 +02:00
return instance ;
2016-06-16 18:09:54 +02:00
}
2016-05-26 18:12:53 +02:00
/ * *
2016-06-16 18:09:54 +02:00
* the current ASLSession
* @return the session
2016-05-26 18:12:53 +02:00
* /
2016-06-16 18:09:54 +02:00
private ASLSession getASLSession ( ) {
String sessionID = this . getThreadLocalRequest ( ) . getSession ( ) . getId ( ) ;
String user = ( String ) this . getThreadLocalRequest ( ) . getSession ( ) . getAttribute ( ScopeHelper . USERNAME_ATTRIBUTE ) ;
if ( user = = null ) {
logger . warn ( " USER IS NULL setting testing user and Running OUTSIDE PORTAL " ) ;
user = getDevelopmentUser ( ) ;
SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) . setScope ( TEST_SCOPE ) ;
}
return SessionManager . getInstance ( ) . getASLSession ( sessionID , user ) ;
}
2016-06-06 17:24:52 +02:00
2016-06-16 18:09:54 +02:00
/ * *
* when packaging test will fail if the user is not set to test . user
* @return .
* /
public String getDevelopmentUser ( ) {
2016-06-29 14:00:19 +02:00
String user = TEST_USER ;
2016-09-12 18:00:00 +02:00
// user = "costantino.perciante";
2016-06-16 18:09:54 +02:00
return user ;
}
2016-06-06 17:24:52 +02:00
2016-06-16 18:09:54 +02:00
/ * *
2016-07-20 19:14:20 +02:00
* Get current user ' s token ( for a given scope )
* @param scope if it is not specified it will be retrieved from the asl
2016-06-16 18:09:54 +02:00
* @return String the ckan user ' s token
* /
2016-07-20 19:14:20 +02:00
private String getUserCKanTokenFromSession ( String scope ) {
2016-06-16 18:09:54 +02:00
String token = null ;
2016-06-18 10:13:37 +02:00
2016-06-30 12:56:34 +02:00
if ( ! isWithinPortal ( ) ) {
logger . warn ( " You are running outside the portal " ) ;
token = TEST_SEC_TOKEN ;
} else {
2016-06-18 10:13:37 +02:00
2016-07-04 12:44:20 +02:00
ASLSession aslSession = getASLSession ( ) ;
String username = aslSession . getUsername ( ) ;
// store info in the http session
HttpSession httpSession = getThreadLocalRequest ( ) . getSession ( ) ;
2016-07-20 19:14:20 +02:00
// check the scope we need to discover
String scopeInWhichDiscover = ( scope ! = null & & ! scope . isEmpty ( ) ) ? scope : getASLSession ( ) . getScope ( ) ;
2016-07-04 12:44:20 +02:00
// get the key per scope
2016-07-20 19:14:20 +02:00
String keyPerScope = Utils . concatenateSessionKeyScope ( CKAN_TOKEN_KEY , scopeInWhichDiscover ) ;
2016-06-30 12:56:34 +02:00
// check if session expired
if ( username . equals ( TEST_USER ) ) {
logger . warn ( " Session expired, returning null token " ) ;
token = null ;
2016-05-26 18:12:53 +02:00
2016-06-30 12:56:34 +02:00
} else {
try {
logger . debug ( " User in session is " + username ) ;
2016-07-04 12:44:20 +02:00
if ( httpSession . getAttribute ( keyPerScope ) ! = null ) {
token = ( String ) httpSession . getAttribute ( keyPerScope ) ;
logger . debug ( " Found ckan token into session " ) ;
}
2016-06-30 12:56:34 +02:00
else {
2016-07-20 19:14:20 +02:00
token = getCkanUtilsObj ( scopeInWhichDiscover ) . getApiKeyFromUsername ( username ) ;
2016-07-04 12:44:20 +02:00
httpSession . setAttribute ( keyPerScope , token ) ;
2016-06-30 12:56:34 +02:00
logger . debug ( " Ckan token has been set for user " + username ) ;
}
2016-07-20 19:14:20 +02:00
logger . debug ( " Found ckan token " + token . substring ( 0 , 3 ) + " ************************ " +
" for user " + username + " into scope " + scopeInWhichDiscover ) ;
2016-06-30 12:56:34 +02:00
} catch ( Exception e ) {
logger . error ( " Error while retrieving the key " , e ) ;
}
}
}
return token ;
2016-06-16 18:09:54 +02:00
}
/ * *
2016-07-20 19:14:20 +02:00
* Retrieve the list of organizations in which the user can publish ( roles ADMIN )
2016-06-16 18:09:54 +02:00
* @param username
* @return the list of organizations
* /
2016-07-20 22:41:15 +02:00
private List < OrganizationBean > getUserOrganizationsListAdmin ( String username , String scope ) {
2016-06-17 22:43:57 +02:00
2016-06-16 18:09:54 +02:00
logger . debug ( " Request for user " + username + " organizations list " ) ;
2016-07-20 22:41:15 +02:00
List < OrganizationBean > orgsName = new ArrayList < OrganizationBean > ( ) ;
2016-07-04 16:13:00 +02:00
2016-07-04 12:44:20 +02:00
// get http session
HttpSession httpSession = getThreadLocalRequest ( ) . getSession ( ) ;
2016-07-04 16:13:00 +02:00
2016-07-04 12:44:20 +02:00
// get key
2016-07-20 19:14:20 +02:00
String keyPerScope = Utils . concatenateSessionKeyScope ( CKAN_ORGANIZATIONS_PUBLISH_KEY , scope ) ;
2016-07-04 16:13:00 +02:00
2016-07-04 12:44:20 +02:00
if ( httpSession . getAttribute ( keyPerScope ) ! = null ) {
2016-07-20 22:41:15 +02:00
orgsName = ( List < OrganizationBean > ) httpSession . getAttribute ( keyPerScope ) ;
2016-07-21 11:26:18 +02:00
logger . info ( " List of organizations was into session " + orgsName ) ;
2016-06-28 11:52:04 +02:00
}
else {
2016-07-20 19:14:20 +02:00
orgsName = Utils . getUserOrganizationsListAdmin ( scope , username , getASLSession ( ) . getGroupName ( ) , this ) ;
2016-07-04 12:44:20 +02:00
httpSession . setAttribute ( keyPerScope , orgsName ) ;
2016-07-20 22:41:15 +02:00
logger . info ( " Organizations name for user " + username + " has been saved into session " + orgsName ) ;
2016-06-17 22:43:57 +02:00
}
2016-06-28 11:52:04 +02:00
2016-06-17 22:43:57 +02:00
return orgsName ;
2016-05-26 18:12:53 +02:00
}
/ * *
* Online or in development mode ?
* @return true if you ' re running into the portal , false if in development
* /
private boolean isWithinPortal ( ) {
try {
UserLocalServiceUtil . getService ( ) ;
return true ;
}
catch ( com . liferay . portal . kernel . bean . BeanLocatorException ex ) {
logger . trace ( " Development Mode ON " ) ;
return false ;
}
}
2016-06-06 17:24:52 +02:00
/ * *
* Find a license id given the license text .
* @param chosenLicense
* @return
* /
2016-06-28 11:52:04 +02:00
private String findLicenseIdByLicense ( String chosenLicense ) {
2016-09-12 17:58:50 +02:00
String scope = ( String ) getThreadLocalRequest ( ) . getSession ( ) . getAttribute ( SCOPE_CLIENT_PORTLET_URL ) ;
return getCkanUtilsObj ( scope ) . findLicenseIdByLicenseTitle ( chosenLicense ) ;
2016-06-08 18:31:54 +02:00
}
2016-06-06 17:24:52 +02:00
2016-05-26 18:12:53 +02:00
@Override
public LicensesBean getLicenses ( ) {
2016-06-28 11:52:04 +02:00
ASLSession session = getASLSession ( ) ;
String username = session . getUsername ( ) ;
2016-07-04 16:13:00 +02:00
2016-07-04 12:44:20 +02:00
// get http session
HttpSession httpSession = getThreadLocalRequest ( ) . getSession ( ) ;
2016-09-12 17:58:50 +02:00
String scope = ( String ) httpSession . getAttribute ( SCOPE_CLIENT_PORTLET_URL ) ;
logger . info ( " Request for CKAN licenses for scope " + scope ) ;
2016-07-04 16:13:00 +02:00
2016-07-04 12:44:20 +02:00
// get key per scope
2016-09-05 18:19:18 +02:00
String keyPerScope = Utils . concatenateSessionKeyScope ( CKAN_LICENSES_KEY , scope ) ;
2016-06-28 11:52:04 +02:00
LicensesBean licensesBean = null ;
2016-07-04 12:44:20 +02:00
if ( httpSession . getAttribute ( keyPerScope ) ! = null ) {
licensesBean = ( LicensesBean ) httpSession . getAttribute ( keyPerScope ) ;
2016-06-28 11:52:04 +02:00
logger . debug ( " List of licenses was into session " ) ;
}
else {
2016-09-05 18:19:18 +02:00
List < CkanLicense > titlesLicenses = getCkanUtilsObj ( scope ) . getLicenses ( ) ;
2016-07-13 16:11:15 +02:00
List < String > titles = new ArrayList < String > ( ) ;
List < String > urls = new ArrayList < String > ( ) ;
for ( CkanLicense license : titlesLicenses ) {
titles . add ( license . getTitle ( ) ) ;
String url = ( license . getUrl ( ) ! = null & & ! license . getUrl ( ) . isEmpty ( ) ) ? license . getUrl ( ) : " " ;
urls . add ( url ) ;
}
licensesBean = new LicensesBean ( titles , urls ) ;
2016-07-04 12:44:20 +02:00
httpSession . setAttribute ( keyPerScope , licensesBean ) ;
2016-06-28 11:52:04 +02:00
logger . debug ( " List of licenses has been saved into session " ) ;
2016-05-26 18:12:53 +02:00
}
2016-06-28 11:52:04 +02:00
return licensesBean ;
2016-05-26 18:12:53 +02:00
}
2016-05-27 19:08:03 +02:00
@Override
2016-09-12 17:58:50 +02:00
public DatasetMetadataBean getDatasetBean ( String folderId ) {
2016-05-27 19:08:03 +02:00
DatasetMetadataBean bean = null ;
2016-06-06 17:24:52 +02:00
2016-07-06 18:41:43 +02:00
ASLSession aslSession = getASLSession ( ) ;
2016-09-12 17:58:50 +02:00
String userName = aslSession . getUsername ( ) ;
logger . info ( " DatasetBean request for " + folderId + " and " + userName ) ;
2016-09-05 18:19:18 +02:00
HttpSession httpSession = this . getThreadLocalRequest ( ) . getSession ( ) ;
2016-09-12 17:58:50 +02:00
String scope = ( String ) httpSession . getAttribute ( SCOPE_CLIENT_PORTLET_URL ) ;
logger . debug ( " Scope recovered from session is " + scope ) ;
2016-05-27 19:08:03 +02:00
if ( isWithinPortal ( ) ) {
try {
2016-06-28 20:27:04 +02:00
// check if session expired
2016-09-12 17:58:50 +02:00
if ( userName . equals ( TEST_USER ) ) {
2016-06-28 20:27:04 +02:00
logger . debug ( " SESSION EXPIRED " ) ;
return null ;
}
2016-06-30 12:56:34 +02:00
2016-05-27 19:08:03 +02:00
logger . debug ( " Request dataset metadata bean for folder with id " + folderId
2016-09-12 17:58:50 +02:00
+ " whose owner is " + userName ) ;
2016-05-27 19:08:03 +02:00
// get usermanager (liferay)
UserManager liferUserManager = new LiferayUserManager ( ) ;
2016-09-12 17:58:50 +02:00
GCubeUser userOwner = liferUserManager . getUserByUsername ( userName ) ;
2016-06-27 11:55:06 +02:00
2016-05-27 19:08:03 +02:00
// build bean
logger . debug ( " Building bean " ) ;
bean = new DatasetMetadataBean ( ) ;
bean . setId ( folderId ) ;
2016-09-12 17:58:50 +02:00
bean . setOwnerIdentifier ( userName ) ;
2016-05-27 19:08:03 +02:00
bean . setVersion ( 1 ) ;
2016-07-13 16:11:15 +02:00
bean . setAuthorName ( userOwner . getFirstName ( ) ) ;
bean . setAuthorSurname ( userOwner . getLastName ( ) ) ;
2016-05-27 19:08:03 +02:00
bean . setAuthorEmail ( userOwner . getEmail ( ) ) ;
bean . setMaintainer ( userOwner . getFullname ( ) ) ;
bean . setMaintainerEmail ( userOwner . getEmail ( ) ) ;
2016-09-12 17:58:50 +02:00
bean . setOrganizationList ( getUserOrganizationsListAdmin ( userName , scope ) ) ;
2016-05-27 19:08:03 +02:00
2016-06-08 18:31:54 +02:00
// if the request comes from the workspace
2016-06-06 17:24:52 +02:00
if ( folderId ! = null & & ! folderId . isEmpty ( ) ) {
2016-09-12 17:58:50 +02:00
handleWorkspaceResources ( folderId , userName , bean ) ;
2016-06-06 17:24:52 +02:00
}
2016-06-14 12:35:10 +02:00
2016-05-27 19:08:03 +02:00
} catch ( Exception e ) {
logger . error ( " Error while retrieving folder information " , e ) ;
}
} else {
2016-06-06 17:24:52 +02:00
try {
bean = new DatasetMetadataBean ( ) ;
bean . setId ( folderId ) ;
bean . setDescription ( " This is a fantastic description " ) ;
bean . setVersion ( 1 ) ;
2016-07-06 18:41:43 +02:00
String onlyAlphanumeric = " test-creation-blablabla " . replaceAll ( " [^A-Za-z0-9] " , " " ) ;
bean . setTitle ( onlyAlphanumeric + Calendar . getInstance ( ) . getTimeInMillis ( ) ) ;
2016-07-13 16:11:15 +02:00
bean . setAuthorName ( " Costantino " ) ;
bean . setAuthorSurname ( " Perciante " ) ;
2016-06-06 17:24:52 +02:00
bean . setAuthorEmail ( " costantino.perciante@isti.cnr.it " ) ;
bean . setMaintainer ( " Costantino Perciante " ) ;
bean . setMaintainerEmail ( " costantino.perciante@isti.cnr.it " ) ;
2016-09-12 17:58:50 +02:00
bean . setOrganizationList ( getUserOrganizationsListAdmin ( userName , TEST_SCOPE ) ) ;
bean . setOwnerIdentifier ( userName ) ;
2016-06-06 17:24:52 +02:00
if ( folderId ! = null & & ! folderId . isEmpty ( ) ) {
2016-09-12 17:58:50 +02:00
handleWorkspaceResources ( folderId , userName , bean ) ;
2016-06-06 17:24:52 +02:00
}
2016-06-09 18:58:19 +02:00
2016-06-06 17:24:52 +02:00
} catch ( Exception e ) {
logger . error ( " Error while building bean into dev mode " , e ) ;
}
2016-05-27 19:08:03 +02:00
}
return bean ;
}
2016-09-12 17:58:50 +02:00
/ * *
* This method receives a folder id within the user ' s workspace and set the list of resources in the dataset bean to be returned
* @param folderId
* @param owner
* @param bean
* @param userName
* @throws Exception
* /
private void handleWorkspaceResources ( String folderId , String userName ,
DatasetMetadataBean bean ) throws Exception {
// get workspace
Workspace ws = HomeLibrary
. getHomeManagerFactory ( )
. getHomeManager ( )
. getHome ( userName ) . getWorkspace ( ) ;
WorkspaceItem originalFolder = ws . getItem ( folderId ) ;
// set some info
String onlyAlphanumeric = originalFolder . getName ( ) . replaceAll ( " [^A-Za-z0-9] " , " " ) ; // that is, remove characters different than A-Z,a-z,0-9
bean . setTitle ( onlyAlphanumeric ) ;
bean . setDescription ( originalFolder . getDescription ( ) ) ;
// Create the folder in the catalogue
Map < String , String > folderItems = Utils . getGcubeItemProperties ( originalFolder ) ;
bean . setCustomFields ( folderItems ) ;
// check the resources within the folder (skip subdirectories for now TODO)
List < String > childrenIds = new ArrayList < String > ( ) ;
for ( WorkspaceItem file : originalFolder . getChildren ( ) ) {
if ( ! file . isFolder ( ) ) {
childrenIds . add ( file . getId ( ) ) ;
}
}
List < ResourceBeanWrapper > listOfResources = Utils . getWorkspaceResourcesInformation ( childrenIds , ws , userName ) ;
bean . setResources ( listOfResources ) ;
}
/ * *
* Copy into the . catalogue area folder the checked resources
* @param resourcesToAdd
* @param folderId
* @param userName
* @param bean
* @return
* /
private List < ResourceBean > copyResourcesToUserCatalogueArea (
List < ResourceBeanWrapper > resourcesToAdd , String folderId , String userName , DatasetMetadataBean bean ) throws Exception {
// in to the .catalogue area of the user's workspace
Workspace ws = HomeLibrary
. getHomeManagerFactory ( )
. getHomeManager ( )
. getHome ( userName ) . getWorkspace ( ) ;
WorkspaceItem originalFolder = ws . getItem ( folderId ) ;
// Retrieve the catalogue of the user
WorkspaceCatalogue userCatalogue = ws . getCatalogue ( ) ;
// Create the folder in the catalogue
WorkspaceItem copiedFolder = userCatalogue . addWorkspaceItem ( originalFolder . getPath ( ) , null ) ; // add to .catalogue root
List < ResourceBean > resources = new ArrayList < ResourceBean > ( ) ;
// copy only the selected ones
for ( ResourceBeanWrapper resourceBeanWrapper : resourcesToAdd ) {
if ( resourceBeanWrapper . isToBeAdded ( ) ) {
// ok it is a file, so copy it into the copiedFolder
WorkspaceItem copiedFile = userCatalogue . addWorkspaceItem ( resourceBeanWrapper . getId ( ) , copiedFolder . getId ( ) ) ;
resources . add ( new ResourceBean (
copiedFile . getPublicLink ( true ) ,
copiedFile . getName ( ) ,
copiedFile . getDescription ( ) ,
copiedFile . getId ( ) ,
userName ,
null , // to be set
( ( FolderItem ) copiedFile ) . getMimeType ( ) ) ) ;
}
}
return resources ;
}
2016-06-06 17:24:52 +02:00
@Override
2016-09-15 14:56:35 +02:00
public DatasetMetadataBean createCKanDataset ( DatasetMetadataBean toCreate ) {
2016-06-14 12:36:50 +02:00
2016-06-14 12:35:10 +02:00
logger . debug ( " Request for creating a dataset with these information " + toCreate ) ;
2016-06-06 17:24:52 +02:00
2016-06-28 20:27:04 +02:00
ASLSession aslSession = getASLSession ( ) ;
2016-09-12 17:58:50 +02:00
String userName = aslSession . getUsername ( ) ;
2016-06-28 20:27:04 +02:00
2016-06-14 12:35:10 +02:00
try {
2016-06-28 20:27:04 +02:00
// check if session expired
2016-09-12 17:58:50 +02:00
if ( userName . equals ( TEST_USER ) ) {
2016-06-28 20:27:04 +02:00
logger . debug ( " SESSION EXPIRED " ) ;
return null ;
}
2016-06-14 12:35:10 +02:00
String title = toCreate . getTitle ( ) ;
String organizationNameOrId = toCreate . getSelectedOrganization ( ) ;
2016-07-13 16:11:15 +02:00
String author = toCreate . getAuthorFullName ( ) ;
2016-06-14 12:35:10 +02:00
String authorMail = toCreate . getAuthorEmail ( ) ;
String maintainer = toCreate . getMaintainer ( ) ;
String maintainerMail = toCreate . getMaintainerEmail ( ) ;
long version = toCreate . getVersion ( ) ;
String description = toCreate . getDescription ( ) ;
String chosenLicense = toCreate . getLicense ( ) ;
String licenseId = findLicenseIdByLicense ( chosenLicense ) ;
List < String > listOfTags = toCreate . getTags ( ) ;
Map < String , String > customFields = toCreate . getCustomFields ( ) ;
boolean setPublic = toCreate . getVisibility ( ) ;
2016-07-06 18:41:43 +02:00
// get the list of resources and convert to ResourceBean
2016-09-12 17:58:50 +02:00
List < ResourceBean > resources = null ;
2016-07-06 18:41:43 +02:00
List < ResourceBeanWrapper > resourcesToAdd = toCreate . getResources ( ) ;
2016-06-06 17:24:52 +02:00
2016-09-12 17:58:50 +02:00
// we need to copy such resource in the .catalogue area of the user's ws
if ( resourcesToAdd ! = null & & ! resourcesToAdd . isEmpty ( ) ) {
2016-06-06 17:24:52 +02:00
2016-09-12 17:58:50 +02:00
resources = copyResourcesToUserCatalogueArea ( resourcesToAdd , toCreate . getId ( ) , userName , toCreate ) ;
2016-06-06 17:24:52 +02:00
2016-09-12 17:58:50 +02:00
}
2016-07-20 19:14:20 +02:00
logger . debug ( " The user wants to publish in organization with name " + organizationNameOrId + " and current scope name is " + aslSession . getScopeName ( ) ) ;
String scope = Utils . retrieveScopeFromOrganizationName ( organizationNameOrId ) ;
CKanUtils utils = getCkanUtilsObj ( scope ) ;
String datasetId = utils . createCKanDataset ( getUserCKanTokenFromSession ( scope ) , title , organizationNameOrId , author ,
2016-06-14 12:36:50 +02:00
authorMail , maintainer , maintainerMail , version , description , licenseId ,
listOfTags , customFields , resources , setPublic ) ;
2016-06-10 16:59:55 +02:00
2016-06-14 12:35:10 +02:00
if ( datasetId ! = null ) {
2016-06-14 12:36:50 +02:00
2016-06-14 12:35:10 +02:00
logger . debug ( " Dataset created! " ) ;
toCreate . setId ( datasetId ) ;
2016-06-14 12:36:50 +02:00
2016-06-14 12:35:10 +02:00
// retrieve the url
2016-07-20 19:14:20 +02:00
String datasetUrl = utils . getPortletUrl ( ) + " ?path= " + utils . getUrlFromDatasetIdOrName ( getUserCKanTokenFromSession ( scope ) , datasetId , true ) ;
2016-06-14 12:35:10 +02:00
toCreate . setSource ( datasetUrl ) ;
return toCreate ;
2016-06-14 12:36:50 +02:00
2016-06-14 12:35:10 +02:00
} else {
2016-06-14 12:36:50 +02:00
2016-06-14 12:35:10 +02:00
logger . error ( " Failed to create the dataset " ) ;
2016-06-14 12:36:50 +02:00
2016-06-14 12:35:10 +02:00
}
2016-06-06 17:24:52 +02:00
} catch ( Exception e ) {
2016-06-14 12:35:10 +02:00
logger . error ( " Unable to create the dataset " , e ) ;
2016-06-06 17:24:52 +02:00
}
2016-06-10 18:06:31 +02:00
2016-06-14 12:36:50 +02:00
return null ;
2016-06-10 18:06:31 +02:00
}
2016-06-07 17:50:33 +02:00
@Override
2016-09-12 17:58:50 +02:00
public ResourceBeanWrapper addResourceToDataset ( ResourceBeanWrapper resource , String datasetId ) {
ASLSession session = getASLSession ( ) ;
String username = session . getUsername ( ) ;
2016-06-08 18:31:54 +02:00
2016-07-20 19:14:20 +02:00
logger . debug ( " Incoming request for creating new resource for dataset with id " + datasetId + " and organization name of the dataset is " + resource . getOrganizationNameDatasetParent ( ) ) ;
2016-09-12 17:58:50 +02:00
logger . debug ( " Owner is " + username + " and resource is " + resource ) ;
2016-06-08 18:31:54 +02:00
2016-06-30 12:56:34 +02:00
if ( ! isWithinPortal ( ) ) {
2016-06-08 18:31:54 +02:00
2016-06-30 12:56:34 +02:00
logger . warn ( " Running outside the portal " ) ;
return resource ;
2016-06-08 18:31:54 +02:00
2016-06-30 12:56:34 +02:00
} else {
2016-06-09 18:58:19 +02:00
2016-06-30 12:56:34 +02:00
if ( username . equals ( TEST_USER ) ) {
2016-06-08 18:31:54 +02:00
2016-06-30 12:56:34 +02:00
logger . warn ( " SESSION EXPIRED! " ) ;
return null ;
2016-06-14 12:35:10 +02:00
2016-06-30 12:56:34 +02:00
} else {
try {
2016-06-08 18:31:54 +02:00
2016-06-30 12:56:34 +02:00
ResourceBean resourceBean = new ResourceBean (
resource . getUrl ( ) ,
resource . getName ( ) ,
resource . getDescription ( ) ,
null ,
2016-09-12 17:58:50 +02:00
username ,
2016-06-30 12:56:34 +02:00
datasetId ,
null ) ;
2016-07-20 19:14:20 +02:00
// get the scope in which we should discover the ckan instance given the organization name in which the dataset was created
String scope = Utils . retrieveScopeFromOrganizationName ( resource . getOrganizationNameDatasetParent ( ) ) ;
String resourceId = getCkanUtilsObj ( scope ) . addResourceToDataset ( resourceBean , getUserCKanTokenFromSession ( scope ) ) ;
2016-06-30 12:56:34 +02:00
if ( resourceId ! = null ) {
logger . debug ( " Resource " + resource . getName ( ) + " is now available " ) ;
// set its id and turn it to the client
resource . setId ( resourceId ) ;
return resource ;
}
} catch ( Exception e ) {
logger . error ( " Unable to create new resource " , e ) ;
}
}
logger . debug ( " No resource created " ) ;
return null ;
}
2016-06-08 18:31:54 +02:00
}
@Override
2016-09-12 17:58:50 +02:00
public boolean deleteResourceFromDataset ( ResourceBeanWrapper resource ) {
2016-06-08 18:31:54 +02:00
2016-06-30 12:56:34 +02:00
logger . debug ( " Request for deleting resource " + resource ) ;
2016-06-14 12:35:10 +02:00
boolean deleted = false ;
2016-06-08 18:31:54 +02:00
2016-06-30 12:56:34 +02:00
if ( ! isWithinPortal ( ) ) {
logger . warn ( " Running outside the portal " ) ;
return deleted ;
} else {
2016-06-08 18:31:54 +02:00
2016-06-30 12:56:34 +02:00
ASLSession session = getASLSession ( ) ;
String username = session . getUsername ( ) ;
2016-06-09 18:58:19 +02:00
2016-06-30 12:56:34 +02:00
if ( username . equals ( TEST_USER ) ) {
2016-07-20 22:41:15 +02:00
logger . warn ( " SESSION EXPIRED! " ) ;
2016-06-30 12:56:34 +02:00
return deleted ;
} else {
try {
2016-07-20 19:14:20 +02:00
// get the scope in which we should discover the ckan instance given the organization name in which the dataset was created
String scope = Utils . retrieveScopeFromOrganizationName ( resource . getOrganizationNameDatasetParent ( ) ) ;
deleted = getCkanUtilsObj ( scope ) .
deleteResourceFromDataset ( resource . getId ( ) , getUserCKanTokenFromSession ( scope ) ) ;
2016-06-30 12:56:34 +02:00
if ( deleted ) {
logger . debug ( " Resource described by " + resource + " deleted " ) ;
} else
logger . error ( " Resource described by " + resource + " NOT deleted " ) ;
} catch ( Exception e ) {
logger . error ( " Error while trying to delete resource described by " + resource , e ) ;
}
return deleted ;
}
2016-06-07 17:50:33 +02:00
}
}
2016-07-04 12:44:20 +02:00
2016-07-20 19:14:20 +02:00
@Override
public List < MetaDataProfileBean > getProfiles ( String orgName ) {
2016-09-12 17:58:50 +02:00
2016-07-22 12:51:19 +02:00
logger . debug ( " Requested profiles for products into orgName " + orgName ) ;
2016-07-06 18:41:43 +02:00
2016-07-21 19:31:05 +02:00
List < MetaDataProfileBean > toReturn = new ArrayList < MetaDataProfileBean > ( ) ;
2016-07-06 18:41:43 +02:00
try {
2016-07-22 12:51:19 +02:00
String evaluatedScope = Utils . retrieveScopeFromOrganizationName ( orgName ) ;
logger . debug ( " Evaluated scope is " + evaluatedScope ) ;
toReturn = Utils . getMetadataProfilesList ( evaluatedScope , getThreadLocalRequest ( ) . getSession ( ) , getASLSession ( ) , CKAN_PROFILES_KEY ) ;
2016-07-06 18:41:43 +02:00
} catch ( Exception e ) {
2016-07-20 19:14:20 +02:00
logger . error ( " Failed to retrieve profiles for scope coming from organization name " + orgName , e ) ;
2016-07-06 18:41:43 +02:00
}
return toReturn ;
}
2016-06-30 12:56:34 +02:00
}