summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageUploadPage.java
blob: 24fc13f10707132f149a853fee234d8b666d96a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.openslx.dozmod.gui.wizard.page;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
import org.openslx.dozmod.Config;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.QFileChooser;
import org.openslx.dozmod.gui.wizard.Wizard;
import org.openslx.dozmod.gui.wizard.layout.ImageUploadPageLayout;
import org.openslx.dozmod.state.UploadWizardState;
import org.openslx.dozmod.thrift.ThriftActions;
import org.openslx.dozmod.thrift.ThriftError;
import org.openslx.dozmod.thrift.UploadInitiator;
import org.openslx.dozmod.thrift.WrappedException;
import org.openslx.dozmod.thrift.cache.MetaDataCache;
import org.openslx.util.vm.VmMetaData.HardDisk;
import org.openslx.util.vm.VmwareMetaData;

/**
 * Page for uploading a new image.
 */
@SuppressWarnings("serial")
public class ImageUploadPage extends ImageUploadPageLayout {

	private final static Logger LOGGER = Logger.getLogger(ImageUploadPage.class);

	private UploadWizardState state;
	private String lastDetectedName = null;
	private ImageDetailsRead existingImage = null;

	public ImageUploadPage(Wizard wizard, UploadWizardState uploadWizardState,
			final ImageDetailsRead existingImage) {
		super(wizard);
		setPageComplete(false);
		this.canComeBack = false;
		this.state = uploadWizardState;
		this.existingImage = existingImage;

		lblImageName.setVisible(existingImage == null);
		txtImageName.setVisible(existingImage == null);
		// show the licenced software checkbox since we are uploading new version
		chkLicenseRestricted.setVisible(existingImage != null);
		chkLicenseRestricted.setSelected(existingImage != null); // TODO selected by default?

		// Browse for *.vmx
		btnBrowseForImage.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				browseForVm();
			}
		});

		txtImageFile.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1)
					browseForVm();
			}
		});

		btnBrowseForImage.requestFocus();
	}

	private void browseForVm() {
		QFileChooser fc = new QFileChooser(Config.getUploadPath(), false);
		FileNameExtensionFilter filter = new FileNameExtensionFilter("VMware Virtual Machine", "vmx");
		fc.setFileFilter(filter);

		int action = fc.showOpenDialog(getDialog());
		File file = fc.getSelectedFile();
		if (action != JFileChooser.APPROVE_OPTION || file == null)
			return;
		vmxSelected(file.getAbsoluteFile());
	}

	private void vmxSelected(File file) {
		Config.setUploadPath(file.getParent());
		try {
			state.meta = new VmwareMetaData(MetaDataCache.getOperatingSystems(), file);
		} catch (IOException e) {
			Gui.showMessageBox(this, "Konnte " + file.getPath() + " nicht lesen", MessageType.ERROR, LOGGER,
					e);
			setPageComplete(false);
			return;
		}
		LOGGER.info(new String(state.meta.getFilteredDefinition().array(), StandardCharsets.UTF_8));
		if (state.meta == null || state.meta.getDisplayName() == null) {
			setErrorMessage("Ungültige vmx-Datei ausgewählt!");
			setPageComplete(false);
			return;
		}
		List<HardDisk> hdds = state.meta.getHdds();
		if (hdds.size() == 0 || hdds.get(0).diskImage == null) {
			setErrorMessage("Die gewählte vmx-Datei enthält keine virtuelle Festplatte!");
			setPageComplete(false);
			return;
		}
		if (hdds.size() > 1) {
			setErrorMessage("Die gewählte vmx-Datei enthält mehr als eine virtuelle Festplatte!");
			setPageComplete(false);
			return;
			// Allow to continue!? 
		}

		// now check the disk files
		File vmDiskFileInfo = new File(hdds.get(0).diskImage);
		if (!vmDiskFileInfo.isAbsolute()) {
			// it's relative, compose path using the vmx location
			File vmxBaseDirectory = file.getParentFile();
			vmDiskFileInfo = new File(vmxBaseDirectory, hdds.get(0).diskImage);
		}
		if (vmDiskFileInfo.canRead()) {
			state.diskFile = vmDiskFileInfo;
		} else {
			setErrorMessage("'" + vmDiskFileInfo.getName() + "' kann nicht gelesen werden!");
			setPageComplete(false);
			return;
		}

		// vmx ok, set it as our description file
		state.descriptionFile = file;

		if (existingImage == null) {
			// User didn't enter a name yet or didn't change it -> set
			String imageName = txtImageName.getText();
			if (imageName.isEmpty() || imageName.equals(lastDetectedName)) {
				txtImageName.setText(state.meta.getDisplayName());
			}
		}
		lastDetectedName = state.meta.getDisplayName();
		state.detectedOs = state.meta.getOs();
		txtImageFile.setText(file.getAbsolutePath());
		// let the user know the upload is ready
		setErrorMessage(null);
		setDescription("Sie können jetzt den Upload starten.");
		setPageComplete(true);
	}

	/**
	 * This function starts the image creation process. It is triggered by the
	 * "Next" button.
	 * 
	 * Depending on the state, it will first try to get a UUID for the new image
	 * by calling createImage() of the thrift API. If a UUID is received, it
	 * will request an upload with requestImageVersionUpload(). If granted, it
	 * will finally start a thread for the UploadTask.
	 * 
	 * Then a callback to the Gui is executed where we can process the upload
	 * state and give the user feedback about it.
	 * 
	 */
	@Override
	protected boolean wantNextOrFinish() {
		// are we creating a new image? then either:
		// get the image name either auto filled by VmwareMetaData or by user
		// get the image name from the image we are uploading a new version of
		state.name = existingImage != null ? existingImage.getImageName() : txtImageName.getText();

		// -- create image to get uuid --
		if (existingImage == null) {
			if (state.uuid == null) {
				state.uuid = ThriftActions.createImage(JOptionPane.getFrameForComponent(this), state.name);
				if (state.uuid == null)
					return false;

				txtImageName.setEnabled(false);
				btnBrowseForImage.setEnabled(false);
				txtImageFile.setEnabled(false);
			}
		} else {
			state.uuid = existingImage.getImageBaseId();
		}
		// Create upload initiator that will manage requesting a token, hashing the file, connecting for upload...
		if (state.upload == null) {
			try {
				state.upload = new UploadInitiator(state.uuid, state.diskFile,
						state.meta.getFilteredDefinition());
			} catch (WrappedException e) {
				ThriftError.showMessage(this, LOGGER, e.exception, e.displayMessage);
				return false;
			} catch (IOException e) {
				Gui.showMessageBox(this, "Upload-Initialisierung fehlgeschlagen", MessageType.ERROR, LOGGER,
						e);
				return false;
			}
		}
		// Start the hash check now
		state.upload.startHashing();
		return true;

	}
}