|
|
|
@ -3,18 +3,14 @@ package org.gcube.portlets.widgets.githubconnector.server.storage;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
import org.gcube.common.homelibrary.home.HomeLibrary;
|
|
|
|
|
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
|
|
|
|
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.exceptions.InsufficientPrivilegesException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemAlreadyExistException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WrongDestinationException;
|
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile;
|
|
|
|
|
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
|
|
|
|
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
|
|
|
|
import org.gcube.common.storagehub.client.dsl.ItemContainer;
|
|
|
|
|
import org.gcube.common.storagehub.client.dsl.OpenResolver;
|
|
|
|
|
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
|
|
|
|
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.portlets.widgets.githubconnector.shared.exception.ServiceException;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -36,22 +32,21 @@ public class StorageUtil {
|
|
|
|
|
* @throws ServiceException
|
|
|
|
|
* service exception
|
|
|
|
|
*/
|
|
|
|
|
public static void deleteItem(String user, String itemId) throws ServiceException {
|
|
|
|
|
Workspace ws;
|
|
|
|
|
public void deleteItem(String user, String itemId) throws ServiceException {
|
|
|
|
|
try {
|
|
|
|
|
logger.debug("User: " + user + ", ItemId:" + itemId);
|
|
|
|
|
ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
|
logger.info("Delete Item: [User=" + user + ", ItemId=" + itemId + "]");
|
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
|
OpenResolver openResolver = shc.open(itemId);
|
|
|
|
|
|
|
|
|
|
ws.removeItems(itemId);
|
|
|
|
|
ItemContainer<Item> itemContainer = openResolver.asItem();
|
|
|
|
|
itemContainer.delete();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
} catch (InsufficientPrivilegesException | WorkspaceFolderNotFoundException | InternalErrorException
|
|
|
|
|
| HomeNotFoundException | ItemNotFoundException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
logger.error("Delete Item on workspace: " + e.getLocalizedMessage(), e);
|
|
|
|
|
throw new ServiceException(e.getLocalizedMessage(), e);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -68,65 +63,65 @@ public class StorageUtil {
|
|
|
|
|
* @throws ServiceException
|
|
|
|
|
* service exception
|
|
|
|
|
*/
|
|
|
|
|
public static String createFolderOnWorkspace(String user, String destinationFolderId, String folderName,
|
|
|
|
|
public String createFolderOnWorkspace(String user, String destinationFolderId, String folderName,
|
|
|
|
|
String folderDescription) throws ServiceException {
|
|
|
|
|
try {
|
|
|
|
|
logger.debug("CreateFolderOnWorkspace: [User=" + user + ", FolderId:" + destinationFolderId
|
|
|
|
|
+ ", folderName=" + folderName + ", folderDescription=" + folderDescription + "]");
|
|
|
|
|
Workspace ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
|
WorkspaceFolder workspaceFolder = ws.createFolder(folderName, folderDescription, destinationFolderId);
|
|
|
|
|
return workspaceFolder.getId();
|
|
|
|
|
|
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException | HomeNotFoundException
|
|
|
|
|
| InsufficientPrivilegesException | ItemAlreadyExistException | WrongDestinationException
|
|
|
|
|
| ItemNotFoundException e) {
|
|
|
|
|
logger.error("CreateFolderOnWorkspace: " + e.getLocalizedMessage());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.info("CreateFolderOnWorkspace: [User=" + user + ", FolderId:" + destinationFolderId + ", folderName="
|
|
|
|
|
+ folderName + ", folderDescription=" + folderDescription + "]");
|
|
|
|
|
|
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
|
OpenResolver openResolver = shc.open(destinationFolderId);
|
|
|
|
|
|
|
|
|
|
FolderContainer parentFolderContainer = openResolver.asFolder();
|
|
|
|
|
FolderContainer folderContainer = parentFolderContainer.newFolder(folderName, folderDescription);
|
|
|
|
|
FolderItem folderItem = folderContainer.get();
|
|
|
|
|
|
|
|
|
|
logger.debug("Folder created: " + folderItem.getId());
|
|
|
|
|
return folderItem.getId();
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
logger.error("Error in create folder on workspace: " + e.getLocalizedMessage(), e);
|
|
|
|
|
throw new ServiceException(e.getLocalizedMessage(), e);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param user
|
|
|
|
|
* user
|
|
|
|
|
* @param destinationFolderId
|
|
|
|
|
* @param folderId
|
|
|
|
|
* destination folder id
|
|
|
|
|
* @param fileName
|
|
|
|
|
* @param name
|
|
|
|
|
* file name
|
|
|
|
|
* @param fileDescription
|
|
|
|
|
* @param description
|
|
|
|
|
* file description
|
|
|
|
|
* @param is
|
|
|
|
|
* @param inputStream
|
|
|
|
|
* input stream
|
|
|
|
|
* @return file id
|
|
|
|
|
* @throws ServiceException
|
|
|
|
|
* service exception
|
|
|
|
|
*/
|
|
|
|
|
public static String saveOnWorkspace(String user, String destinationFolderId, String fileName,
|
|
|
|
|
String fileDescription, InputStream is) throws ServiceException {
|
|
|
|
|
public String saveOnWorkspace(String user, String folderId, String name, String description,
|
|
|
|
|
InputStream inputStream) throws ServiceException {
|
|
|
|
|
try {
|
|
|
|
|
logger.debug("saveOnWorkspace: [User=" + user + ", FolderId:" + destinationFolderId + ", fileName="
|
|
|
|
|
+ fileName + ", fileDescription=" + fileDescription + "]");
|
|
|
|
|
Workspace ws = HomeLibrary.getUserWorkspace(user);
|
|
|
|
|
|
|
|
|
|
WorkspaceItem workspaceItem = ws.getItem(destinationFolderId);
|
|
|
|
|
if (workspaceItem.isFolder()) {
|
|
|
|
|
ExternalFile externalfile = ws.createExternalFile(fileName, fileDescription, null, is,
|
|
|
|
|
destinationFolderId);
|
|
|
|
|
return externalfile.getId();
|
|
|
|
|
} else {
|
|
|
|
|
throw new ServiceException("Invalid destination folder!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (WorkspaceFolderNotFoundException | InternalErrorException | HomeNotFoundException
|
|
|
|
|
| InsufficientPrivilegesException | ItemAlreadyExistException | WrongDestinationException
|
|
|
|
|
| ItemNotFoundException e) {
|
|
|
|
|
logger.error("SaveOnWorkspace: " + e.getLocalizedMessage());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
logger.info("Save item on workspace: [user=" + user + ", name=" + name + ", description=" + description
|
|
|
|
|
+ ", folderId=" + folderId + "]");
|
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
|
OpenResolver openResolver = shc.open(folderId);
|
|
|
|
|
|
|
|
|
|
FolderContainer folderContainer = openResolver.asFolder();
|
|
|
|
|
FileContainer fileContainer = folderContainer.uploadFile(inputStream, name, description);
|
|
|
|
|
AbstractFileItem abstractFileItem = fileContainer.get();
|
|
|
|
|
|
|
|
|
|
logger.debug("File saved id: " + abstractFileItem.getId());
|
|
|
|
|
return abstractFileItem.getId();
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
logger.error("Save item on workspace: " + e.getLocalizedMessage(), e);
|
|
|
|
|
throw new ServiceException(e.getLocalizedMessage(), e);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|