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

                                                      

                                                 
                                                                    

                                                            
                                                  

                                              
 
                                         
 

                                                                                 
                                                    

                                                      
                                                                      
                                            


                                                  
                                                                                




                                                           
                                                                 







                                              

                                


                                                                                            


                                                                                             
                                                   

         

                                        


                                                                                          




                                                       



                                                                                   
                                                                              
                                                                               
                                                                   


                                                                               



                                                                                     
                                                                                                                                           









                                                                                                                    
                                                                           



                                                                                                 














                                                                                                 



                                                                                                                 

                                    
                                                                                                                  

                                     
         


                                        
                                                                                         


                                                              
                                                                                                       


                                                                     
                                                                


                                                                          

                                                                                                



                                                                      
                                                         


                                                                   

                                                                                         



                                                                      
                                                           


                                                            




                                                                                                                  


                                             
                                                            


                                                                     


                                                                                                                            


                                             
                                                            


                                                                     



                                                                                        
                                                                                                                           


                                             
                                                          


                                                                    
                                                     








                                                              

                                                              
                                                                       
                                                                          
                                                             

                                                              
         
 
package org.openslx.dozmod.gui.wizard;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.eclipse.jface.wizard.Wizard;
import org.openslx.bwlp.thrift.iface.ImageBaseWrite;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
import org.openslx.dozmod.gui.MainWindow;
import org.openslx.dozmod.gui.helper.MessageType;
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.thrifthelper.ThriftManager;

public class ImageWizard extends Wizard {

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

	private UploadWizardState uploadWizardState;
	protected ImageUploadPage imageUploadPage;
	protected ImageMetaDataPage imageMetaDataPage;
	protected ImageCustomPermissionPage imageCustomPermissionPage;
	protected boolean editExistingImage;

	/**
	 * Wizard for creating or editing an image
	 * @param editExistingImage whether to create new or edit existing image
	 */
	public ImageWizard(boolean editExistingImage) {
		super();
		setNeedsProgressMonitor(true);
		this.editExistingImage = editExistingImage;
		this.uploadWizardState = new UploadWizardState();
	}


	@Override
	public String getWindowTitle() {
		return "Neues Image erzeugen";
	}

	@Override
	public void addPages() {
		// create the shared object for all pages of the wizard
		imageUploadPage = new ImageUploadPage(uploadWizardState, editExistingImage);
		imageMetaDataPage = new ImageMetaDataPage(uploadWizardState);
		imageCustomPermissionPage = new ImageCustomPermissionPage(uploadWizardState);
		addPage(imageUploadPage);
		addPage(imageMetaDataPage);
		addPage(imageCustomPermissionPage);
	}

	@Override
	public boolean performFinish() {
		// since we only started the download 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()) {
			LOGGER.error("Invalid state!");
			return false;
		} else {
			LOGGER.debug(uploadWizardState.name);
			LOGGER.debug(uploadWizardState.description);
			LOGGER.debug(uploadWizardState.descriptionFile.toString());
			LOGGER.debug(uploadWizardState.diskFile.toString());
			LOGGER.debug(uploadWizardState.selectedOs.toString());
			LOGGER.debug(uploadWizardState.virtualizer.toString());
			LOGGER.debug(uploadWizardState.isTemplate);
			LOGGER.debug(uploadWizardState.permissions.toString());
			LOGGER.debug(uploadWizardState.shareMode.toString());
			LOGGER.debug(uploadWizardState.uuid.toString());
			// TODO run the actually request over external threaded class
			try {
				// push to sat
				ThriftManager.getSatClient().updateImageBase(
						Session.getSatelliteToken(), uploadWizardState.uuid.toString(), imageBaseWriteFromState());
			} catch (TException e) {
				LOGGER.error("Fail to push image metadata to satellite: ", e);
				return false;
			}
		}

		// seems like we did it! fetch it again
		ImageDetailsRead imageDetailsRead = null;
		try {
			imageDetailsRead = ThriftManager.getSatClient().getImageDetails(Session.getSatelliteToken(),
					uploadWizardState.uuid.toString());
		} catch (TException e) {
			LOGGER.error("Could not fetch image details of newly created image!", e);
			return false;
		}

		if (uploadWizardState.permissionList != null) {
			// user skipped permission list stuff
			// TODO push permission list to servor
			try {
				ThriftManager.getSatClient().writeImagePermissions(
						Session.getSatelliteToken(),
						uploadWizardState.uuid,
						uploadWizardState.permissionList);
			} catch (TException e) {
				// TODO Auto-generated catch block
				LOGGER.error("Could not push permission list to satellite: ", e);
				return false;
			}
		}
		if (imageDetailsRead != null) {
			// it did work :)
			LOGGER.debug(imageDetailsRead.toString());
			MainWindow.showMessageBox("Creation of image worked :)", MessageType.INFO, LOGGER, null);
			return true;
		} else {
			MainWindow.showMessageBox("Creation of image failed :(", MessageType.ERROR, LOGGER, null);
			return false;
		}
	}

	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.virtualizer == null) {
			LOGGER.error("No virtualizer set in state!");
			return false;
		}	else {
			if (!(uploadWizardState.virtualizer.isSetVirtId()
					&& uploadWizardState.virtualizer.isSetVirtName())) {
				LOGGER.error("Virtualizer has missing fields: " + uploadWizardState.virtualizer.toString());
				return false;
			}
		}
		if (uploadWizardState.permissions == null) {
			LOGGER.error("No permissions set in state!");
			return false;
		}	else {
			if (!(uploadWizardState.permissions.isSetAdmin()
					&& uploadWizardState.permissions.isSetDownload()
					&& uploadWizardState.permissions.isSetEdit()
					&& uploadWizardState.permissions.isSetLink())) {
				LOGGER.error("Virtualizer has missing fields: " + uploadWizardState.selectedOs.toString());
				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.virtualizer.getVirtId(),
				uploadWizardState.isTemplate,
				uploadWizardState.permissions,
				uploadWizardState.shareMode);
	}
}