added code to remove the folder in the .catalogue area if an error arises and the product is not published

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/widgets/ckan-metadata-publisher-widget@132883 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-10-06 19:22:39 +00:00
parent a7c24dc919
commit c3267a79ec
2 changed files with 127 additions and 46 deletions

View File

@ -384,13 +384,13 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
* @return
*/
private List<ResourceBean> copyResourcesToUserCatalogueArea(
List<ResourceBeanWrapper> resourcesToAdd, String folderId, String userName, DatasetMetadataBean bean) throws Exception{
logger.debug("Request to copy onto catalogue area. Parameters are resources to add" + resourcesToAdd);
logger.debug("Request to copy onto catalogue area. Folder id is " + folderId);
logger.debug("Request to copy onto catalogue area. Username is " + userName);
logger.debug("Request to copy onto catalogue area. DatasetMetadataBean is " + bean);
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()
@ -399,10 +399,10 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
.getWorkspace();
// Retrieve the catalogue of the user
WorkspaceCatalogue userCatalogue = ws.getCatalogue();
userCatalogue = ws.getCatalogue();
// Create the folder in the catalogue
WorkspaceItem copiedFolder = userCatalogue.addWorkspaceItem(folderId, userCatalogue.getId()); // add to .catalogue root area
copiedFolder = userCatalogue.addWorkspaceItem(folderId, userCatalogue.getId()); // add to .catalogue root area
// change description for the folder
copiedFolder.setDescription(bean.getDescription());
@ -410,8 +410,6 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
// change name of the copied folder to match the title
((WorkspaceFolder)copiedFolder).rename(org.gcube.datacatalogue.ckanutillibrary.utils.UtilMethods.fromProductTitleToName(bean.getTitle()));
List<ResourceBean> resources = new ArrayList<ResourceBean>();
// copy only the selected ones
for(ResourceBeanWrapper resourceBeanWrapper : resourcesToAdd){
@ -437,6 +435,19 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
}
}
}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;
}
@ -447,6 +458,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
ASLSession aslSession = getASLSession();
String userName = aslSession.getUsername();
String[] folderCatalogueId = new String[1];
try{
@ -476,8 +488,14 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
// we need to copy such resource in the .catalogue area of the user's ws
if(resourcesToAdd != null && !resourcesToAdd.isEmpty()){
resources = copyResourcesToUserCatalogueArea(resourcesToAdd, toCreate.getId(), userName, toCreate, folderCatalogueId);
resources = copyResourcesToUserCatalogueArea(resourcesToAdd, toCreate.getId(), userName, toCreate);
if(resources == null){
logger.error("An error arises, returning null");
return null;
}
}
@ -518,6 +536,9 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
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;
}

View File

@ -0,0 +1,60 @@
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);
}
}
}