summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/ImageCreationWizard.java
blob: 845c4e3fdd576dfd3bce29873f96ba5b9dfa5ff4 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                      
 
                       
 

                               
                               
                                    
                                                    
                                                       
                                  
                                                 
                                                      
                                                
                                                                    

                                                            
                                                                 
                                                  
                                         
                                               

                                                                        
                                              

                                        
 
                           
                                                                                         
 
                                                                                         
 
                                                                        

                                                      
                                                                      

                                                   


                                                  
           
                                                                                
           
                                                   
                              
                                                                                                
 


                                                                                       


                                                   

         

                                        
                                          

         
                 

                                                                                     

                                                                                          
                              


                                                                                                                    
                                                                       
                                     
                 





                                                                                                     
                                                                                                                   
                                             
                         
                                           
                 







                                                                                                                                 
                                                                                                                          



                                                     
                 
 





















                                                                                                                                         
         
 


                                           

                                                                                 
           

                                        
                                                                 


                                                              
                                                                               


                                                                     
                                                    

                                                                          
                        

                                                                                                           


                                             
                                             

                                                                   
                        

                                                                                                    


                                             
                                               

                                                            
                 

                                                                                     
                                     
                 
                                         
                                                                      
                                     
                 
                                                       

                                                                     
                 
                                              


                                                                    
                                         


                                                              
                            
         
 

                                                          


                                                                                                                    
         


                                             
                                       
                                    
                                                                                                            
                                                                        
                                


                                                            



                                                                                               
                                             
                                                                                                                                      


                                                                                                   
                                 
                           

                                 



                                          
                                          





                                     






                                                            
 
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.QuitNotification;
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.gui.wizard.page.ImageUploadSummaryPage;
import org.openslx.dozmod.state.UploadWizardState;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.ThriftActions;
import org.openslx.dozmod.thrift.ThriftError;
import org.openslx.dozmod.thrift.UploadInitiator.GotUploadTokenCallback;
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, QuitNotification {

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

	private final UploadWizardState state = new UploadWizardState();
	protected ImageUploadPage imageUploadPage;
	protected ImageMetaDataPage imageMetaDataPage;
	protected ImageCustomPermissionPage imageCustomPermissionPage;
	private boolean baseWritten = false;
	private boolean permissionsWritten = false;

	/**
	 * Wizard for creating or editing an image
	 * 
	 * @param editExistingImage whether to create new or edit existing image
	 */
	public ImageCreationWizard(Window parent) {
		super(parent);
		state.defaultPermissions = Session.getSatelliteConfig().defaultImagePermissions;

		imageUploadPage = new ImageUploadPage(this, state, null);
		imageMetaDataPage = new ImageMetaDataPage(this, state);
		imageCustomPermissionPage = new ImageCustomPermissionPage(this, state);
		addPage(imageUploadPage);
		addPage(imageMetaDataPage);
		addPage(imageCustomPermissionPage);
	}

	@Override
	public String getWindowTitle() {
		return "Neue VM 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
		if (!isStateValid()) {
			// TODO: Show what went wrong
			Gui.showMessageBox(this, "Ein interner Fehler ist aufgetreten.\n\nDetails in der Logdatei.",
					MessageType.ERROR, null, null);
			return false;
		}
		// push image base to satellite
		if (!baseWritten) {
			try {
				ThriftActions.updateImageBase(state.uuid, imageBaseWriteFromState());
			} catch (TException e) {
				ThriftError.showMessage(null, LOGGER, e,
						"Konnte die Metadaten der VM nicht auf dem Satelliten speichern!");
				return false;
			}
			baseWritten = true;
		}
		// push permissions to satellite if we have custom permissions
		if (!permissionsWritten) {
			if (state.permissionMap != null && !state.permissionMap.isEmpty()) {
				try {
					ThriftActions.writeImagePermissions(state.uuid, state.permissionMap);
				} catch (TException e) {
					Gui.showMessageBox(this, "Konnte die Berechtigungen nicht auf dem Satelliten speichern!",
							MessageType.ERROR, null, null);
					ThriftActions.deleteImageBase(JOptionPane.getFrameForComponent(this), state.uuid);
					return false;
				}
			}
			permissionsWritten = true;
		}

		state.upload.startUpload(new GotUploadTokenCallback() {
			@Override
			public void fire() {
				// push version data
				try {
					ThriftActions.updateImageVersion(state.upload.getToken(), new ImageVersionWrite(
							state.isRestricted));
				} catch (TException e) {
					if (state.isRestricted) {
						Gui.showMessageBox(null, "Unerwarteter Fehler beim Setzen der Option"
								+ " 'Enthält lizenzpflichtige Software' für diese Virtuelle Maschine.",
								MessageType.WARNING, LOGGER, e);
					}
				}
			}
		});
		return true;
	}

	@Override
	public WizardPage performFinish() {
		return new ImageUploadSummaryPage(this, state, true);
	}

	/**
	 * Checks the validity of the state
	 * 
	 * @return true if we have all the needed information in the state, false
	 *         otherwise
	 */
	private boolean isStateValid() {
		// debug purposes
		if (state.name == null || state.name.isEmpty()) {
			LOGGER.error("No name set in state!");
			return false;
		}
		if (state.description == null || state.description.isEmpty()) {
			LOGGER.error("No description set in state!");
			return false;
		}
		if (state.descriptionFile == null) {
			LOGGER.error("No description file set in state!");
			return false;
		} else {
			if (!state.descriptionFile.canRead()) {
				LOGGER.error(state.descriptionFile.getAbsolutePath() + " cannot be read!");
				return false;
			}
		}
		if (state.diskFile == null) {
			LOGGER.error("No disk file set in state!");
			return false;
		} else {
			if (!state.diskFile.canRead()) {
				LOGGER.error(state.diskFile.getAbsolutePath() + " cannot be read!");
				return false;
			}
		}
		if (state.selectedOs == null) {
			LOGGER.error("No OS set in state!");
			return false;
		}
		if (!state.selectedOs.isSetOsId()) {
			LOGGER.error("OS has no id: " + state.selectedOs.toString());
			return false;
		}
		if (state.meta == null) {
			LOGGER.error("No vm meta data set in state!");
			return false;
		}
		if (state.defaultPermissions == null) {
			LOGGER.error("No permissions set in state!");
			return false;
		}
		if (state.shareMode == null) {
			LOGGER.error("No share mode set in state!");
			return false;
		}
		if (state.uuid == null) {
			LOGGER.error("No uuid set in state!");
			return false;
		}
		return true;
	}

	private ImageBaseWrite imageBaseWriteFromState() {
		// build imageBaseWrite
		return new ImageBaseWrite(state.name, state.description, state.selectedOs.getOsId(),
				state.meta.getVirtualizer().getVirtId(), state.isTemplate, state.defaultPermissions,
				state.shareMode);
	}

	@Override
	protected boolean onCancelRequest() {
		if (state.uuid == null)
			return true;
		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() {
					if (state.upload != null) {
						state.upload.cancelError();
					}
					// As we're creating a new VM, delete base image aswell
					try {
						ThriftManager.getSatClient().deleteImageBase(Session.getSatelliteToken(), state.uuid);
					} catch (TException e) {
						LOGGER.debug("Error canceling upload on sat: ", e);
					}
				}
			});
		}
		return confirmed;
	}

	@Override
	public boolean wantConfirmQuit() {
		return state.uuid != null;
	}

	@Override
	public void escapePressed() {
		doCancel();
	}

	@Override
	public void onApplicationQuit() {
		if (state != null && state.upload != null) {
			state.upload.cancelError();
		}
	}
}