removed cleaner thread for .catalogue folder after failed publication. The folder and its content are now renamed and the timestamp is append to these names to avoid conflicts

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@132887 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-10-07 08:44:31 +00:00
parent c3267a79ec
commit a9d2a076a5
3 changed files with 135 additions and 197 deletions

View File

@ -10,12 +10,6 @@ import javax.servlet.http.HttpSession;
import org.gcube.application.framework.core.session.ASLSession; import org.gcube.application.framework.core.session.ASLSession;
import org.gcube.application.framework.core.session.SessionManager; import org.gcube.application.framework.core.session.SessionManager;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.catalogue.WorkspaceCatalogue;
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue; import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory; import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory;
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean; import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
@ -24,6 +18,7 @@ import org.gcube.datacatalogue.ckanutillibrary.utils.UtilMethods;
import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper; import org.gcube.portal.custom.scopemanager.scopehelper.ScopeHelper;
import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherService; import org.gcube.portlets.widgets.ckandatapublisherwidget.client.CKanPublisherService;
import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.Utils; import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.Utils;
import org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils.WorkspaceUtils;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.DatasetMetadataBean; import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.DatasetMetadataBean;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean; import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataProfileBean; import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataProfileBean;
@ -294,7 +289,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
// if the request comes from the workspace // if the request comes from the workspace
if(folderId != null && !folderId.isEmpty()){ if(folderId != null && !folderId.isEmpty()){
handleWorkspaceResources(folderId, userName, bean); WorkspaceUtils.handleWorkspaceResources(folderId, userName, bean);
} }
@ -321,7 +316,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
if(folderId != null && !folderId.isEmpty()){ if(folderId != null && !folderId.isEmpty()){
handleWorkspaceResources(folderId, userName, bean); WorkspaceUtils.handleWorkspaceResources(folderId, userName, bean);
} }
@ -333,124 +328,6 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
return bean; return bean;
} }
/**
* 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 the ones inside
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, String[] idCatalogueFolder) throws Exception{
logger.debug("Request to copy onto catalogue area....");
List<ResourceBean> resources = new ArrayList<ResourceBean>();
WorkspaceItem copiedFolder = null;
WorkspaceCatalogue userCatalogue = null;
try{
// in to the .catalogue area of the user's workspace
Workspace ws = HomeLibrary
.getHomeManagerFactory()
.getHomeManager()
.getHome(userName)
.getWorkspace();
// Retrieve the catalogue of the user
userCatalogue = ws.getCatalogue();
// Create the folder in the catalogue
copiedFolder = userCatalogue.addWorkspaceItem(folderId, userCatalogue.getId()); // add to .catalogue root area
// change description for the folder
copiedFolder.setDescription(bean.getDescription());
// change name of the copied folder to match the title
((WorkspaceFolder)copiedFolder).rename(org.gcube.datacatalogue.ckanutillibrary.utils.UtilMethods.fromProductTitleToName(bean.getTitle()));
// 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());
// name and description could have been edited
copiedFile.setDescription(resourceBeanWrapper.getDescription());
resources.add(new ResourceBean(
copiedFile.getPublicLink(true),
resourceBeanWrapper.getName(),
copiedFile.getDescription(),
copiedFile.getId(),
userName,
null, // to be set
((FolderItem)copiedFile).getMimeType()));
// postpone rename operation
copiedFile.rename(resourceBeanWrapper.getName());
}
}
}catch(Exception e){
logger.error("Failed to copy information into .catalogue area");
if(copiedFolder != null){
logger.info("Starting cleaner thread to remove folder that was created in .catalogue area");
new DeleteFolderCatalogueThread(copiedFolder.getId(), userName).start();
}
return null;
}
// set folder id
idCatalogueFolder[0] = copiedFolder.getId();
// return
return resources;
}
@Override @Override
public DatasetMetadataBean createCKanDataset(DatasetMetadataBean toCreate) { public DatasetMetadataBean createCKanDataset(DatasetMetadataBean toCreate) {
@ -458,7 +335,6 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
ASLSession aslSession = getASLSession(); ASLSession aslSession = getASLSession();
String userName = aslSession.getUsername(); String userName = aslSession.getUsername();
String[] folderCatalogueId = new String[1];
try{ try{
@ -488,14 +364,8 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
// we need to copy such resource in the .catalogue area of the user's ws // we need to copy such resource in the .catalogue area of the user's ws
if(resourcesToAdd != null && !resourcesToAdd.isEmpty()){ if(resourcesToAdd != null && !resourcesToAdd.isEmpty()){
resources = copyResourcesToUserCatalogueArea(resourcesToAdd, toCreate.getId(), userName, toCreate, folderCatalogueId);
if(resources == null){ resources = WorkspaceUtils.copyResourcesToUserCatalogueArea(resourcesToAdd, toCreate.getId(), userName, toCreate);
logger.error("An error arises, returning null");
return null;
}
} }
@ -536,9 +406,6 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
logger.error("Unable to create the dataset", e); logger.error("Unable to create the dataset", e);
} }
// if we are here, we need to remove the folder in the catalogue area created (if present)
new DeleteFolderCatalogueThread(folderCatalogueId[0], userName).start();
return null; return null;
} }

View File

@ -1,60 +0,0 @@
package org.gcube.portlets.widgets.ckandatapublisherwidget.server;
import java.util.List;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.catalogue.WorkspaceCatalogue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* On product creation error, the folder catalogue is deleted
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class DeleteFolderCatalogueThread extends Thread{
private static final Logger logger = LoggerFactory.getLogger(DeleteFolderCatalogueThread.class);
private String folderRootCatalogueId;
private String username;
public DeleteFolderCatalogueThread(String folderRootCatalogueId, String username){
logger.debug("Folder in the catalogue area to delete is " + folderRootCatalogueId);
this.folderRootCatalogueId = folderRootCatalogueId;
this.username = username;
}
@Override
public void run() {
try {
// retrieve user's workspace
Workspace ws = HomeLibrary
.getHomeManagerFactory()
.getHomeManager()
.getHome(username)
.getWorkspace();
// Retrieve the catalogue of the user
WorkspaceCatalogue userCatalogue = ws.getCatalogue();
// remove children, if any
WorkspaceFolder folder = (WorkspaceFolder)userCatalogue.getCatalogueItemByPath(folderRootCatalogueId);
List<WorkspaceItem> children = folder.getChildren();
for (WorkspaceItem workspaceItem : children) {
try{
workspaceItem.remove();
}catch(Exception e){
logger.error("Unable to delete folder's children", e);
}
}
// remove folder
folder.remove();
} catch (Exception e) {
logger.error("Unable to delete folder", e);
}
}
}

View File

@ -0,0 +1,131 @@
package org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.catalogue.WorkspaceCatalogue;
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.DatasetMetadataBean;
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.ResourceBeanWrapper;
import org.slf4j.LoggerFactory;
public class WorkspaceUtils {
/**
* logger
*/
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(WorkspaceUtils.class);
/**
* Copy into the .catalogue area folder the checked resources
* @param resourcesToAdd
* @param folderId
* @param userName
* @param bean
* @return
*/
public static List<ResourceBean> copyResourcesToUserCatalogueArea(
List<ResourceBeanWrapper> resourcesToAdd, String folderId, String userName, DatasetMetadataBean bean) throws Exception{
logger.debug("Request to copy onto catalogue area....");
List<ResourceBean> resources = new ArrayList<ResourceBean>();
WorkspaceItem copiedFolder = null;
WorkspaceCatalogue userCatalogue = null;
// in to the .catalogue area of the user's workspace
Workspace ws = HomeLibrary
.getHomeManagerFactory()
.getHomeManager()
.getHome(userName)
.getWorkspace();
// Retrieve the catalogue of the user
userCatalogue = ws.getCatalogue();
// Create the folder in the catalogue
copiedFolder = userCatalogue.addWorkspaceItem(folderId, userCatalogue.getId()); // add to .catalogue root area
// change description for the folder
copiedFolder.setDescription(bean.getDescription());
// change name of the copied folder to match the title (append the timestamp to avoid ties)
((WorkspaceFolder)copiedFolder).rename(org.gcube.datacatalogue.ckanutillibrary.utils.UtilMethods.fromProductTitleToName(bean.getTitle()) + "_" + System.currentTimeMillis());
// 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());
// name and description could have been edited
copiedFile.setDescription(resourceBeanWrapper.getDescription());
resources.add(new ResourceBean(
copiedFile.getPublicLink(true),
resourceBeanWrapper.getName(),
copiedFile.getDescription(),
copiedFile.getId(),
userName,
null, // to be set
((FolderItem)copiedFile).getMimeType()));
// postpone rename operation
copiedFile.rename(resourceBeanWrapper.getName() + "_" + System.currentTimeMillis());
}
}
// return
return resources;
}
/**
* 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
*/
public static 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 the ones inside
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);
}
}