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:
parent
a7c24dc919
commit
c3267a79ec
|
@ -384,59 +384,70 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private List<ResourceBean> copyResourcesToUserCatalogueArea(
|
private List<ResourceBean> copyResourcesToUserCatalogueArea(
|
||||||
List<ResourceBeanWrapper> resourcesToAdd, String folderId, String userName, DatasetMetadataBean bean) throws Exception{
|
List<ResourceBeanWrapper> resourcesToAdd, String folderId, String userName, DatasetMetadataBean bean, String[] idCatalogueFolder) 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()));
|
|
||||||
|
|
||||||
|
logger.debug("Request to copy onto catalogue area....");
|
||||||
List<ResourceBean> resources = new ArrayList<ResourceBean>();
|
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
|
// Retrieve the catalogue of the user
|
||||||
for(ResourceBeanWrapper resourceBeanWrapper : resourcesToAdd){
|
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
|
// change description for the folder
|
||||||
WorkspaceItem copiedFile = userCatalogue.addWorkspaceItem(resourceBeanWrapper.getId(), copiedFolder.getId());
|
copiedFolder.setDescription(bean.getDescription());
|
||||||
|
|
||||||
// name and description could have been edited
|
// change name of the copied folder to match the title
|
||||||
copiedFile.setDescription(resourceBeanWrapper.getDescription());
|
((WorkspaceFolder)copiedFolder).rename(org.gcube.datacatalogue.ckanutillibrary.utils.UtilMethods.fromProductTitleToName(bean.getTitle()));
|
||||||
|
|
||||||
resources.add(new ResourceBean(
|
// copy only the selected ones
|
||||||
copiedFile.getPublicLink(true),
|
for(ResourceBeanWrapper resourceBeanWrapper : resourcesToAdd){
|
||||||
resourceBeanWrapper.getName(),
|
|
||||||
copiedFile.getDescription(),
|
|
||||||
copiedFile.getId(),
|
|
||||||
userName,
|
|
||||||
null, // to be set
|
|
||||||
((FolderItem)copiedFile).getMimeType()));
|
|
||||||
|
|
||||||
// postpone rename operation
|
if (resourceBeanWrapper.isToBeAdded()) {
|
||||||
copiedFile.rename(resourceBeanWrapper.getName());
|
|
||||||
|
// 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;
|
return resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -447,6 +458,7 @@ 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{
|
||||||
|
|
||||||
|
@ -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
|
// 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);
|
||||||
|
|
||||||
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);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -650,7 +671,7 @@ public class CKANPublisherServicesImpl extends RemoteServiceServlet implements C
|
||||||
* if no match is found, it retrieves such information by using liferay
|
* if no match is found, it retrieves such information by using liferay
|
||||||
*/
|
*/
|
||||||
private String getScopeFromOrgName(String orgName){
|
private String getScopeFromOrgName(String orgName){
|
||||||
|
|
||||||
logger.debug("Request for scope related to orgName " + orgName + "[ map that will be used is " + mapOrganizationScope.toString() + " ]");
|
logger.debug("Request for scope related to orgName " + orgName + "[ map that will be used is " + mapOrganizationScope.toString() + " ]");
|
||||||
|
|
||||||
if(orgName == null || orgName.isEmpty())
|
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.error("Failed to retrieve scope from OrgName for organization " + orgName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
logger.debug("Returning scope " + toReturn);
|
logger.debug("Returning scope " + toReturn);
|
||||||
return toReturn;
|
return toReturn;
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue