diff options
author | Jonathan Bauer | 2015-08-26 14:09:51 +0200 |
---|---|---|
committer | Jonathan Bauer | 2015-08-26 14:09:51 +0200 |
commit | 95341561619639b8e44acf383c7c2125c55d256e (patch) | |
tree | de9c0acccde4290af8c604b36a9147117b01d2aa /dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageCreationWizard.java | |
parent | [client] InitUploadStatus: returns uploadTask and hashGen when initializing a... (diff) | |
download | tutor-module-95341561619639b8e44acf383c7c2125c55d256e.tar.gz tutor-module-95341561619639b8e44acf383c7c2125c55d256e.tar.xz tutor-module-95341561619639b8e44acf383c7c2125c55d256e.zip |
[client] refactor: ImageWizard -> ImageCreationWizard & UpdateWizard -> ImageUpdateWizard
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageCreationWizard.java')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageCreationWizard.java | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageCreationWizard.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageCreationWizard.java new file mode 100644 index 00000000..70bc06d2 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageCreationWizard.java @@ -0,0 +1,181 @@ +package org.openslx.dozmod.gui.wizard; + +import java.awt.Window; + +import javax.swing.JOptionPane; + +import org.apache.log4j.Logger; +import org.apache.thrift.TException; +import org.openslx.bwlp.thrift.iface.ImageBaseWrite; +import org.openslx.bwlp.thrift.iface.ImageVersionWrite; +import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.gui.helper.MessageType; +import org.openslx.dozmod.gui.helper.UiFeedback; +import org.openslx.dozmod.gui.wizard.page.ImageCustomPermissionPage; +import org.openslx.dozmod.gui.wizard.page.ImageMetaDataPage; +import org.openslx.dozmod.gui.wizard.page.ImageUploadPage; +import org.openslx.dozmod.state.UploadWizardState; +import org.openslx.dozmod.thrift.Session; +import org.openslx.dozmod.thrift.ThriftActions; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.util.QuickTimer; +import org.openslx.util.QuickTimer.Task; + +@SuppressWarnings("serial") +public class ImageCreationWizard extends Wizard implements UiFeedback { + + private final static Logger LOGGER = Logger.getLogger(ImageCreationWizard.class); + + private final UploadWizardState uploadWizardState = new UploadWizardState(); + protected ImageUploadPage imageUploadPage; + protected ImageMetaDataPage imageMetaDataPage; + protected ImageCustomPermissionPage imageCustomPermissionPage; + + /** + * Wizard for creating or editing an image + * + * @param editExistingImage whether to create new or edit existing image + */ + public ImageCreationWizard(Window parent) { + super(parent); + imageUploadPage = new ImageUploadPage(this, uploadWizardState, null); + imageMetaDataPage = new ImageMetaDataPage(this, uploadWizardState); + imageCustomPermissionPage = new ImageCustomPermissionPage(this, uploadWizardState); + addPage(imageUploadPage); + addPage(imageMetaDataPage); + addPage(imageCustomPermissionPage); + } + + @Override + public String getWindowTitle() { + return "Neues Image erzeugen"; + } + + @Override + public boolean wantFinish() { + // since we only started the upload and created a "blank" image entry + // we can here do all the sanity checks on the fields of UploadWizardState + // and react accordingly. + // check state + return isStateValid(); + } + + @Override + public void performFinish() { + // TODO check status of each step and stop if something goes wrong + // push image base to satellite + ThriftActions.updateImageBase(JOptionPane.getFrameForComponent(this), + uploadWizardState.uuid, imageBaseWriteFromState()); + + // push permissions to satellite if we have custom permissions + if (uploadWizardState.permissionMap != null) { + ThriftActions.writeImagePermissions(JOptionPane.getFrameForComponent(this), + uploadWizardState.uuid, uploadWizardState.permissionMap); + } + // push version metadata to satellite + ThriftActions.updateImageVersion(JOptionPane.getFrameForComponent(this), + uploadWizardState.transferInformation.getToken(), + new ImageVersionWrite(uploadWizardState.isRestricted)); + } + + private boolean isStateValid() { + // debug purposes + if (uploadWizardState.name == null || uploadWizardState.name.isEmpty()) { + LOGGER.error("No name set in state!"); + return false; + } + if (uploadWizardState.description == null || uploadWizardState.description.isEmpty()) { + LOGGER.error("No description set in state!"); + return false; + } + if (uploadWizardState.descriptionFile == null) { + LOGGER.error("No description file set in state!"); + return false; + } else { + if (!uploadWizardState.descriptionFile.canRead()) { + LOGGER.error(uploadWizardState.descriptionFile.getAbsolutePath() + " cannot be read!"); + return false; + } + } + if (uploadWizardState.diskFile == null) { + LOGGER.error("No disk file set in state!"); + return false; + } else { + if (!uploadWizardState.diskFile.canRead()) { + LOGGER.error(uploadWizardState.diskFile.getAbsolutePath() + " cannot be read!"); + return false; + } + } + if (uploadWizardState.selectedOs == null) { + LOGGER.error("No OS set in state!"); + return false; + } else { + if (!(uploadWizardState.selectedOs.isSetArchitecture() + && uploadWizardState.selectedOs.isSetOsId() && uploadWizardState.selectedOs.isSetOsName() && uploadWizardState.selectedOs.isSetVirtualizerOsId())) { + LOGGER.error("OS has missing fields: " + uploadWizardState.selectedOs.toString()); + return false; + } + } + if (uploadWizardState.meta == null) { + LOGGER.error("No vm meta data set in state!"); + return false; + } + if (uploadWizardState.defaultPermissions == null) { + LOGGER.error("No permissions set in state!"); + return false; + } + if (uploadWizardState.shareMode == null) { + LOGGER.error("No share mode set in state!"); + return false; + } + if (uploadWizardState.uuid == null) { + LOGGER.error("No uuid set in state!"); + return false; + } + return true; + } + + private ImageBaseWrite imageBaseWriteFromState() { + // build imageBaseWrite + return new ImageBaseWrite(uploadWizardState.name, uploadWizardState.description, + uploadWizardState.selectedOs.getOsId(), uploadWizardState.meta.getVirtualizer().getVirtId(), + uploadWizardState.isTemplate, uploadWizardState.defaultPermissions, uploadWizardState.shareMode); + } + + @Override + protected boolean onCancelRequest() { + boolean confirmed = Gui.showMessageBox(this, "Möchten Sie den Vorgang wirklich abbrechen?", + MessageType.QUESTION_YESNO, null, null); + if (confirmed) { + QuickTimer.scheduleOnce(new Task() { + @Override + public void fire() { + try { + if (uploadWizardState.uuid != null) + ThriftManager.getSatClient().deleteImageBase(Session.getSatelliteToken(), + uploadWizardState.uuid); + } catch (TException e) { + LOGGER.debug("Error canceling upload on sat: ", e); + } + if (uploadWizardState.uploadTask != null) { + uploadWizardState.uploadTask.cancel(); + } + if (uploadWizardState.hashGen != null) { + uploadWizardState.hashGen.cancel(); + } + } + }); + } + return confirmed; + } + + @Override + public boolean wantConfirmQuit() { + return uploadWizardState.uuid != null; + } + + @Override + public void escapePressed() { + doCancel(); + } +} |