summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/LectureWizard.java
blob: 577cd16ec7ce8e6e1b521aa7106b7cb22e8a6637 (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.openslx.bwlp.thrift.iface.ImageSummaryRead;
import org.openslx.bwlp.thrift.iface.LectureWrite;
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.UiFeedback;
import org.openslx.dozmod.gui.window.LectureListWindow;
import org.openslx.dozmod.gui.wizard.page.LectureCreationPage;
import org.openslx.dozmod.gui.wizard.page.LectureCustomPermissionPage;
import org.openslx.dozmod.gui.wizard.page.LectureImageListPage;
import org.openslx.dozmod.gui.wizard.page.LectureLocationSelectionPage;
import org.openslx.dozmod.gui.wizard.page.LectureOptionsPage;
import org.openslx.dozmod.state.LectureWizardState;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.ThriftActions;
import org.openslx.dozmod.thrift.cache.LectureCache;
import org.openslx.dozmod.thrift.cache.MetaDataCache;

@SuppressWarnings("serial")
public class LectureWizard extends Wizard implements UiFeedback {

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

	public final LectureWizardState state = new LectureWizardState();

	/**
	 * Wizard for creating or editing a lecture.
	 * 
	 * @param parent window of this wizard
	 * @param image ImageSummaryRead of the image to link this lecture to,
	 *            if this is null it will add the LectureImageListPage to
	 *            the wizard.
	 * @param editExistingLecture whether to create new or edit existing lecture
	 */
	public LectureWizard(Window parent, ImageSummaryRead image, String imageVersionId) {
		super(parent);

		if (image != null && imageVersionId != null) {
			state.image = image;
			state.imageVersionId = imageVersionId;
		}
		state.defaultPermissions = Session.getSatelliteConfig().defaultLecturePermissions;

		// create the shared object for all pages of the wizard
		addPage(new LectureCreationPage(this, state));
		if (image == null && imageVersionId == null)
			addPage(new LectureImageListPage(this, state));
		addPage(new LectureOptionsPage(this, state));
		addPage(new LectureCustomPermissionPage(this, state));
		if (MetaDataCache.getLocations() != null && !MetaDataCache.getLocations().isEmpty()) {
			addPage(new LectureLocationSelectionPage(this, state));
		}
	}

	@Override
	public String getWindowTitle() {
		return "Neue Veranstaltung erzeugen";
	}

	@Override
	protected boolean onCancelRequest() {
		return Gui.showMessageBox(this, "Möchten Sie den Vorgang wirklich abbrechen?",
				MessageType.QUESTION_YESNO, null, null);
	}

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

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

	@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.
		if (!isStateValid())
			return false;
		// Now try to submit
		if (state.uuid != null) {
			// already got a uuid, this is bad as we were about to do it!
			LOGGER.error("UUID for new lecture already present, something's bad...");
			return false;
		}
		// ok, lets create it
		String uuid = null;
		uuid = ThriftActions.createLecture(JOptionPane.getFrameForComponent(this), lectureWriteFromState());
		if (uuid == null)
			return false;

		state.uuid = uuid;
		// now push the permissions if we have any
		if (state.permissionMap != null && !state.permissionMap.isEmpty()) {
			if (!ThriftActions.writeLecturePermissions(JOptionPane.getFrameForComponent(this), state.uuid,
					state.permissionMap)) {
				return false;
			}
		}

		// all good, refresh cache and change to LectureList
		Gui.showMessageBox(this, "Veranstaltung erstellt!", MessageType.INFO, null, null);
		LectureCache.get(true);
		MainWindow.showPage(LectureListWindow.class);
		return true;
	}

	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.start == null) {
			LOGGER.error("No start date set in state!");
			return false;
		}
		if (state.end == null) {
			LOGGER.error("No start date set in state!");
			return false;
		}
		return true;
	}

	private LectureWrite lectureWriteFromState() {
		LectureWrite lw = new LectureWrite(state.name, state.description, state.imageVersionId, state.autoUpdate,
				state.isEnabled, state.start.getTime() / 1000L, state.end.getTime() / 1000L, state.runScriptText, null,
				state.isExam, state.internetAccess, state.defaultPermissions, state.locations, state.onlyInSelectedLocations, false, state.usbAllowed);
		lw.networkExceptions = state.netRules;
		return lw;
	}
}