diff options
author | Jonathan Bauer | 2015-08-19 16:39:06 +0200 |
---|---|---|
committer | Jonathan Bauer | 2015-08-19 16:39:06 +0200 |
commit | 96a86129edb0845dc8fb6eda3ff6cfa7124b9ba2 (patch) | |
tree | b1f66dd399d7dac02e9f513d57a5f29163902343 /dozentenmodul | |
parent | [client] fix all user getting same permissions object in wizard custom perms ... (diff) | |
download | tutor-module-96a86129edb0845dc8fb6eda3ff6cfa7124b9ba2.tar.gz tutor-module-96a86129edb0845dc8fb6eda3ff6cfa7124b9ba2.tar.xz tutor-module-96a86129edb0845dc8fb6eda3ff6cfa7124b9ba2.zip |
[client] refactored TransferHelper to ThriftActions
Diffstat (limited to 'dozentenmodul')
6 files changed, 194 insertions, 116 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferHelper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferHelper.java deleted file mode 100644 index a7126c40..00000000 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/filetransfer/TransferHelper.java +++ /dev/null @@ -1,108 +0,0 @@ -package org.openslx.dozmod.filetransfer; - -import java.awt.Frame; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; - -import javax.swing.JFileChooser; - -import org.apache.log4j.Logger; -import org.apache.thrift.TException; -import org.openslx.bwlp.thrift.iface.TransferInformation; -import org.openslx.dozmod.Config; -import org.openslx.dozmod.gui.Gui; -import org.openslx.dozmod.gui.MainWindow; -import org.openslx.dozmod.gui.helper.MessageType; -import org.openslx.dozmod.gui.helper.QFileChooser; -import org.openslx.dozmod.thrift.Session; -import org.openslx.dozmod.thrift.ThriftError; -import org.openslx.thrifthelper.ThriftManager; -import org.openslx.util.QuickTimer; -import org.openslx.util.QuickTimer.Task; - -public class TransferHelper { - - private static final Logger LOGGER = Logger.getLogger(TransferHelper.class); - - public interface DownloadCallback { - void downloadInitialized(boolean success); - } - public static void initDownload(final Frame frame, final String imageVersionId, final String imageName, - final String virtualizerId, final long imageSize, final DownloadCallback callback) { - QFileChooser fc = new QFileChooser(Config.getDownloadPath(), true); - fc.setDialogTitle("Bitte wählen Sie einen Speicherort"); - int action = fc.showSaveDialog(frame); - final File file = fc.getSelectedFile(); - if (action != JFileChooser.APPROVE_OPTION || file == null) - return; - - QuickTimer.scheduleOnce(new Task() { - @Override - public void fire() { - final TransferInformation transInf; - try { - transInf = ThriftManager.getSatClient() - .requestDownload(Session.getSatelliteToken(), imageVersionId); - } catch (TException e) { - ThriftError.showMessage(frame, LOGGER, e, "Die Download-Anfrage ist gescheitert"); - if (callback != null) - callback.downloadInitialized(false); - return; - } - - File df = null; - try { - file.getAbsoluteFile().mkdirs(); - df = new File(file.getAbsolutePath(), generateFilename(imageName, virtualizerId)); - df.createNewFile(); - } catch (IOException e) { - LOGGER.warn("Cannot prepare download destination", e); - } - final File destFile = df; - // TODO: Check if file already exists - final DownloadTask dlTask; - try { - dlTask = new DownloadTask(Session.getSatelliteAddress(), transInf.getPlainPort(), transInf - .getToken(), destFile, imageSize, null); - } catch (final FileNotFoundException e) { - Gui.asyncExec(new Runnable() { - @Override - public void run() { - Gui.showMessageBox(frame, - "Konnte Download nicht vorbereiten: Der gewählte Zielort ist nicht beschreibbar", - MessageType.ERROR, LOGGER, e); - } - }); - if (callback != null) - callback.downloadInitialized(false); - return; - } - new Thread(dlTask).start(); - - Gui.asyncExec(new Runnable() { - @Override - public void run() { - Config.setDownloadPath(file.getAbsolutePath()); - MainWindow.addDownload(imageName, destFile.getName(), dlTask); - } - }); - } - }); - } - private static String generateFilename(String imageName, String virtualizerId) { - String fileName = imageName.replaceAll("[^a-zA-Z0-9_\\.\\-]+", "_"); - if (fileName.length() > 50) { - fileName = fileName.substring(0, 50); - } - if ("vmware".equals(virtualizerId)) { - fileName += ".vmdk"; - } else if ("virtualbox".equals(virtualizerId)) { - fileName += ".vdi"; - } else { - fileName += ".img"; - } - return fileName; - } - -} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java index 78714408..9d6e56e5 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java @@ -26,7 +26,6 @@ import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.bwlp.thrift.iface.ShareMode; import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.bwlp.thrift.iface.Virtualizer; -import org.openslx.dozmod.filetransfer.TransferHelper; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.gui.helper.MessageType; @@ -38,6 +37,7 @@ import org.openslx.dozmod.gui.wizard.LectureWizard; import org.openslx.dozmod.permissions.ImagePerms; import org.openslx.dozmod.thrift.Session; import org.openslx.dozmod.thrift.ThriftError; +import org.openslx.dozmod.thrift.ThriftActions; import org.openslx.dozmod.thrift.cache.MetaDataCache; import org.openslx.dozmod.thrift.cache.UserCache; import org.openslx.dozmod.util.FormatHelper; @@ -312,7 +312,7 @@ public class ImageDetailsWindow extends ImageDetailsWindowLayout implements UiFe null, null); return; } - TransferHelper.initDownload(JOptionPane.getFrameForComponent(this), selected.versionId, + ThriftActions.initDownload(JOptionPane.getFrameForComponent(this), selected.versionId, image.imageName, image.virtId, selected.fileSize, null); } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java index 1fc9d1ce..d06c6717 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageListWindow.java @@ -21,8 +21,6 @@ import javax.swing.event.ListSelectionListener; import org.apache.log4j.Logger; import org.openslx.bwlp.thrift.iface.ImageSummaryRead; -import org.openslx.dozmod.filetransfer.TransferHelper; -import org.openslx.dozmod.filetransfer.TransferHelper.DownloadCallback; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.gui.helper.MessageType; @@ -33,6 +31,8 @@ import org.openslx.dozmod.gui.wizard.ImageWizard; import org.openslx.dozmod.gui.wizard.LectureWizard; import org.openslx.dozmod.permissions.ImagePerms; import org.openslx.dozmod.thrift.Session; +import org.openslx.dozmod.thrift.ThriftActions; +import org.openslx.dozmod.thrift.ThriftActions.DownloadCallback; import org.openslx.dozmod.thrift.cache.ImageCache; import org.openslx.thrifthelper.ThriftManager; import org.openslx.util.QuickTimer; @@ -362,7 +362,7 @@ public class ImageListWindow extends ImageListWindowLayout implements DownloadCa if (!hasLatestVersion(image)) return; downloadButton.setEnabled(false); - TransferHelper.initDownload(JOptionPane.getFrameForComponent(this), image.latestVersionId, + ThriftActions.initDownload(JOptionPane.getFrameForComponent(this), image.latestVersionId, image.imageName, image.virtId, image.fileSize, this); } /** diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureDetailsWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureDetailsWindow.java index c0956cf3..90de4aed 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureDetailsWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LectureDetailsWindow.java @@ -18,7 +18,6 @@ import org.openslx.bwlp.thrift.iface.ImageDetailsRead; import org.openslx.bwlp.thrift.iface.ImageVersionDetails; import org.openslx.bwlp.thrift.iface.LectureRead; import org.openslx.bwlp.thrift.iface.UserInfo; -import org.openslx.dozmod.filetransfer.TransferHelper; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.gui.helper.MessageType; @@ -28,6 +27,7 @@ import org.openslx.dozmod.gui.window.layout.LectureDetailsWindowLayout; import org.openslx.dozmod.permissions.LecturePerms; import org.openslx.dozmod.thrift.Session; import org.openslx.dozmod.thrift.ThriftError; +import org.openslx.dozmod.thrift.ThriftActions; import org.openslx.dozmod.thrift.cache.UserCache; import org.openslx.dozmod.util.FormatHelper; import org.openslx.thrifthelper.ThriftManager; @@ -259,7 +259,7 @@ public class LectureDetailsWindow extends LectureDetailsWindowLayout implements null, null); return; } - TransferHelper.initDownload(JOptionPane.getFrameForComponent(this), lecture.imageVersionId, + ThriftActions.initDownload(JOptionPane.getFrameForComponent(this), lecture.imageVersionId, image.imageName, image.virtId, versionSize, null); } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureCustomPermissionPage.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureCustomPermissionPage.java index 45c1f05d..52a6ca35 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureCustomPermissionPage.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/LectureCustomPermissionPage.java @@ -62,7 +62,6 @@ public class LectureCustomPermissionPage extends LectureCustomPermissionPageLayo }); // delete user button adapter removeUser.addActionListener(new ActionListener() { - @Override public void actionPerformed(ActionEvent e) { final UserLecturePermissions selected = permissionTableViewer.getSelectedItem(); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftActions.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftActions.java new file mode 100644 index 00000000..4aa6d1ae --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/ThriftActions.java @@ -0,0 +1,187 @@ +package org.openslx.dozmod.thrift; + +import java.awt.Frame; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import javax.swing.JFileChooser; + +import org.apache.log4j.Logger; +import org.apache.thrift.TException; +import org.openslx.bwlp.thrift.iface.TransferInformation; +import org.openslx.dozmod.Config; +import org.openslx.dozmod.filetransfer.DownloadTask; +import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.gui.Gui.GuiCallable; +import org.openslx.dozmod.gui.MainWindow; +import org.openslx.dozmod.gui.helper.MessageType; +import org.openslx.dozmod.gui.helper.QFileChooser; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.util.QuickTimer; +import org.openslx.util.QuickTimer.Task; + +public class ThriftActions { + + private static final Logger LOGGER = Logger.getLogger(ThriftActions.class); + + /******************************************************************************** + * + * DOWNLOAD ACTION + * + * Download image version action composed of the interface 'DownloadCallback' + * and the actual static method 'initDownload' to start the download. + * + ********************************************************************************/ + /** + * The callback interface to inform the GUI about the status of the operation + */ + public interface DownloadCallback { + void downloadInitialized(boolean success); + } + /** + * Initializes the download of the given imageVersionId saving it to the given + * imageName + * + * @param frame caller of this method + * @param imageVersionId image version id to download + * @param imageName destination file name + * @param virtualizerId id of the virtualizer + * @param imageSize size in bytes of the image to be downloaded + * @param callback callback function to return status of this operation to the GUI + */ + public static void initDownload(final Frame frame, final String imageVersionId, final String imageName, + final String virtualizerId, final long imageSize, final DownloadCallback callback) { + QFileChooser fc = new QFileChooser(Config.getDownloadPath(), true); + fc.setDialogTitle("Bitte wählen Sie einen Speicherort"); + int action = fc.showSaveDialog(frame); + final File file = fc.getSelectedFile(); + if (action != JFileChooser.APPROVE_OPTION || file == null) + return; + + QuickTimer.scheduleOnce(new Task() { + @Override + public void fire() { + final TransferInformation transInf; + try { + transInf = ThriftManager.getSatClient() + .requestDownload(Session.getSatelliteToken(), imageVersionId); + } catch (TException e) { + ThriftError.showMessage(frame, LOGGER, e, "Die Download-Anfrage ist gescheitert"); + if (callback != null) + callback.downloadInitialized(false); + return; + } + + File df = null; + try { + file.getAbsoluteFile().mkdirs(); + df = new File(file.getAbsolutePath(), generateFilename(imageName, virtualizerId)); + df.createNewFile(); + } catch (IOException e) { + LOGGER.warn("Cannot prepare download destination", e); + } + final File destFile = df; + if (destFile.exists()) { + Boolean confirmed = Gui.syncExec(new GuiCallable<Boolean>() { + @Override + public Boolean run() { + return Gui.showMessageBox(frame, "SIcher?", MessageType.QUESTION_YESNO, LOGGER, null); + } + }); + if (!confirmed) { + // TODO potentially ask him a new path? + return; + } + } + final DownloadTask dlTask; + try { + dlTask = new DownloadTask(Session.getSatelliteAddress(), transInf.getPlainPort(), transInf + .getToken(), destFile, imageSize, null); + } catch (final FileNotFoundException e) { + Gui.asyncMessageBox("Konnte Download nicht vorbereiten: Der gewählte Zielort ist nicht beschreibbar", + MessageType.ERROR, LOGGER, e); + if (callback != null) + callback.downloadInitialized(false); + return; + } + new Thread(dlTask).start(); + + Gui.asyncExec(new Runnable() { + @Override + public void run() { + Config.setDownloadPath(file.getAbsolutePath()); + MainWindow.addDownload(imageName, destFile.getName(), dlTask); + if (callback != null) + callback.downloadInitialized(true); + } + }); + } + }); + } + /** + * Generates a filename based on the given imageName and with the proper extension + * depending on the virtualizer + * + * @param imageName + * @param virtualizerId + * @return the generated name as String + */ + private static String generateFilename(String imageName, String virtualizerId) { + String fileName = imageName.replaceAll("[^a-zA-Z0-9_\\.\\-]+", "_"); + if (fileName.length() > 50) { + fileName = fileName.substring(0, 50); + } + if ("vmware".equals(virtualizerId)) { + fileName += ".vmdk"; + } else if ("virtualbox".equals(virtualizerId)) { + fileName += ".vdi"; + } else { + fileName += ".img"; + } + return fileName; + } + + /******************************************************************************** + * + * DELETE ACTION + * + * Deletes a specific image version or the latest if none is specified + * + ********************************************************************************/ + public interface DeleteCallback { + void isDeleted(boolean success); + } + public static void deleteImageVersion(final Frame frame, final String imageBaseId, final String imageVersionId, final DeleteCallback callback) { + // requires confirmation of the user + if (!Gui.showMessageBox(frame, "Wollen Sie die letzte Version dieses Images wirklich löschen?", + MessageType.QUESTION_YESNO, LOGGER, null)) + return; + // try to actually delete this version of the image + QuickTimer.scheduleOnce(new Task() { + boolean success = false; + @Override + public void fire() { + try { + ThriftManager.getSatClient().deleteImageVersion(Session.getSatelliteToken(), + imageVersionId); + LOGGER.info("Deleted version '" + imageVersionId + "' of image '" + + imageBaseId + "'."); + success = true; + } catch (TException e) { + ThriftError.showMessage(frame, LOGGER, e, "Das Löschen der Version ist gescheitert"); + if (callback != null) + callback.isDeleted(success); + return; + } + Gui.asyncExec(new Runnable() { + @Override + public void run() { + if (callback != null) + callback.isDeleted(success); + } + }); + } + }); + } +} |