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,59 +384,70 @@ 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);
// in to the .catalogue area of the user's workspace
Workspace ws = HomeLibrary
.getHomeManagerFactory()
.getHomeManager()
.getHome(userName)
.getWorkspace();
// Retrieve the catalogue of the user
WorkspaceCatalogue userCatalogue = ws.getCatalogue();
// Create the folder in the catalogue
WorkspaceItem 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()));
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();
// copy only the selected ones
for(ResourceBeanWrapper resourceBeanWrapper : resourcesToAdd){
// Retrieve the catalogue of the user
userCatalogue = ws.getCatalogue();
if (resourceBeanWrapper.isToBeAdded()) {
// Create the folder in the catalogue
copiedFolder = userCatalogue.addWorkspaceItem(folderId, userCatalogue.getId()); // add to .catalogue root area
// ok it is a file, so copy it into the copiedFolder
WorkspaceItem copiedFile = userCatalogue.addWorkspaceItem(resourceBeanWrapper.getId(), copiedFolder.getId());
// change description for the folder
copiedFolder.setDescription(bean.getDescription());
// name and description could have been edited
copiedFile.setDescription(resourceBeanWrapper.getDescription());
// change name of the copied folder to match the title
((WorkspaceFolder)copiedFolder).rename(org.gcube.datacatalogue.ckanutillibrary.utils.UtilMethods.fromProductTitleToName(bean.getTitle()));
resources.add(new ResourceBean(
copiedFile.getPublicLink(true),
resourceBeanWrapper.getName(),
copiedFile.getDescription(),
copiedFile.getId(),
userName,
null, // to be set
((FolderItem)copiedFile).getMimeType()));
// copy only the selected ones
for(ResourceBeanWrapper resourceBeanWrapper : resourcesToAdd){
// postpone rename operation
copiedFile.rename(resourceBeanWrapper.getName());
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;
}
@ -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;
}
@ -650,7 +671,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
* if no match is found, it retrieves such information by using liferay
*/
private String getScopeFromOrgName(String orgName){
logger.debug("Request for scope related to orgName " + orgName + "[ map that will be used is " + mapOrganizationScope.toString() + " ]");
if(orgName == null || orgName.isEmpty())
@ -671,8 +692,8 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
logger.error("Failed to retrieve scope from OrgName for organization " + orgName, e);
}
}
logger.debug("Returning scope " + toReturn);
return toReturn;

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);
}
}
}