diff --git a/distro/changelog.xml b/distro/changelog.xml index 04411dc..27e8012 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -5,6 +5,8 @@ [Task #12533] added trash operations [Task #12556] added download facility [Task #12601] added download folder facility + [Task #12604] Migrate Move operation to StorageHub + [Task #12603] Migrate Copy operation to StorageHub diff --git a/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java b/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java index 16b0c88..3217019 100644 --- a/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java +++ b/src/main/java/org/gcube/common/storagehubwrapper/server/StorageHubClientService.java @@ -17,6 +17,7 @@ import org.gcube.common.storagehub.client.plugins.AbstractPlugin; import org.gcube.common.storagehub.client.proxies.ItemManagerClient; import org.gcube.common.storagehub.client.proxies.WorkspaceManagerClient; import org.gcube.common.storagehub.model.acls.ACL; +import org.gcube.common.storagehub.model.items.AbstractFileItem; import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.Item; import org.gcube.common.storagehub.model.items.SharedFolder; @@ -138,6 +139,19 @@ public class StorageHubClientService { } + /** + * Gets the folder container. + * + * @param itemId the item id + * @return the folder container + * @throws Exception the exception + */ + public FolderContainer getFolderContainer(String itemId) throws Exception{ + setContextProviders(scope, authorizationToken); + return shcClient.open(itemId).asFolder(); + } + + /** * Gets the parents. * @@ -430,7 +444,7 @@ public class StorageHubClientService { */ public void deleteItemById(String itemId) throws Exception{ - Validate.notNull(itemId, "Bad invoking operation the "+itemId+" is null"); + Validate.notNull(itemId, "Bad invoking operation the itemId is null"); shcClient.open(itemId).asItem().delete(); } @@ -472,7 +486,7 @@ public class StorageHubClientService { */ public Item restoreThrashItem(String itemId) throws Exception{ - Validate.notNull(itemId, "Bad invoking restore trash item the "+itemId+" is null"); + Validate.notNull(itemId, "Bad invoking restore trash item the item Id is null"); GenericItemContainer container = shcClient.restoreThrashItem(itemId); @@ -488,6 +502,44 @@ public class StorageHubClientService { } + /** + * Move item. + * + * @param itemId the move item id + * @param destFolderContainer the dest folder container + * @throws Exception the exception + */ + public void moveItem(String itemId, FolderContainer destFolderContainer) throws Exception{ + + Validate.notNull(itemId, "Bad invoking move file, the itemId is null"); + Validate.notNull(destFolderContainer, "Bad invoking move file, the itemId is null"); + + shcClient.open(itemId).asItem().move(destFolderContainer); + + } + + + /** + * Copy item. + * + * @param fileItemId the copy item id + * @param destFolderContainer the dest folder container + * @param newFileName the new file name + * @return the abstract file item + * @throws Exception the exception + */ + public AbstractFileItem copyFileItem(String fileItemId, FolderContainer destFolderContainer, String newFileName) throws Exception{ + + Validate.notNull(fileItemId, "Bad invoking copy file, the fileItemId is null"); + Validate.notNull(destFolderContainer, "Bad invoking copy file, the destFolderContainer is null"); + + FileContainer copyingItem = shcClient.open(fileItemId).asFile(); + String newName = newFileName!=null && !newFileName.isEmpty()?newFileName:"Copy of "+copyingItem.get().getName(); + FileContainer newItem = copyingItem.copy(destFolderContainer, newName); + return newItem.get(); + } + + /* (non-Javadoc) * @see java.lang.Object#toString() */ diff --git a/src/main/java/org/gcube/common/storagehubwrapper/server/WorkspaceStorageHubClientService.java b/src/main/java/org/gcube/common/storagehubwrapper/server/WorkspaceStorageHubClientService.java index 07a113a..7519733 100644 --- a/src/main/java/org/gcube/common/storagehubwrapper/server/WorkspaceStorageHubClientService.java +++ b/src/main/java/org/gcube/common/storagehubwrapper/server/WorkspaceStorageHubClientService.java @@ -12,6 +12,8 @@ import java.util.Map; import org.apache.commons.lang.Validate; import org.gcube.common.storagehub.client.StreamDescriptor; +import org.gcube.common.storagehub.client.dsl.FolderContainer; +import org.gcube.common.storagehub.model.items.AbstractFileItem; import org.gcube.common.storagehub.model.items.FolderItem; import org.gcube.common.storagehub.model.items.Item; import org.gcube.common.storagehub.model.types.GenericItemType; @@ -678,6 +680,89 @@ public final class WorkspaceStorageHubClientService implements Workspace{ } + /** + * Move items. + * + * @param itemIds the item ids + * @param folderDestinationId the folder destination id + * @throws Exception the exception + */ + public void moveItems(List itemIds, String folderDestinationId) throws Exception{ + + FolderContainer destFolderContainer = null; + + try { + + if(itemIds==null || itemIds.size()==0) + throw new Exception("Input list of id for moving is null or empty"); + + Validate.notNull(folderDestinationId,"The folderDestinationId is null"); + + destFolderContainer = storageHubClientService.getFolderContainer(folderDestinationId); + + for (String itemId : itemIds) { + + try{ + storageHubClientService.moveItem(itemId, destFolderContainer); + }catch(Exception e){ + logger.error("Error on moving the item with id: "+itemId+ " in the folder id: "+destFolderContainer.get().getId(), e); + } + } + + } catch (Exception e) { + logger.error("Error on moving item/items in the folder with id: "+folderDestinationId, e); + String error = e.getMessage()!=null?e.getMessage():""; + throw new Exception("Error on moving item/s. "+error); + } + + } + + + /** + * Copy file items. + * + * @param itemIds the item ids + * @param folderDestinationId the folder destination id + * @return the list + * @throws Exception the exception + */ + public List copyFileItems(List itemIds, String folderDestinationId) throws Exception{ + + + FolderContainer destFolderContainer = null; + + try { + + if(itemIds==null || itemIds.size()==0) + throw new Exception("Input list of id for copying is null or empty"); + + Validate.notNull(folderDestinationId,"The folderDestinationId is null"); + + destFolderContainer = storageHubClientService.getFolderContainer(folderDestinationId); + + List toReturnItems = new ArrayList(itemIds.size()); + + for (String itemId : itemIds) { + + try{ + AbstractFileItem toReturnItem = storageHubClientService.copyFileItem(itemId, destFolderContainer, null); + toReturnItems.add(HLMapper.toWorkspaceItem(toReturnItem)); + }catch(Exception e){ + logger.error("Error on copying the item with id: "+itemId+ " in the folder id: "+destFolderContainer.get().getId(), e); + } + } + + return toReturnItems; + + } catch (Exception e) { + logger.error("Error on copying item/items in the folder with id: "+folderDestinationId, e); + String error = e.getMessage()!=null?e.getMessage():""; + throw new Exception("Error on copying item/s. "+error); + } + + } + +