package org.openslx.dozmod.gui.wizard; import java.awt.Window; import org.apache.log4j.Logger; import org.apache.thrift.TException; 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.LectureOptionsPage; import org.openslx.dozmod.state.LectureWizardState; import org.openslx.dozmod.thrift.Session; import org.openslx.dozmod.thrift.ThriftError; import org.openslx.dozmod.thrift.cache.LectureCache; import org.openslx.thrifthelper.ThriftManager; @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 editExistingLecture whether to create new or edit existing lecture */ public LectureWizard(Window parent, ImageSummaryRead image, String imageVersionId) { super(parent); // sanity check on the image if (image == null || imageVersionId == null) { throw new NullPointerException("LectureWizard needs ImageSummaryRead + imageVersionId!"); } this.state.image = image; this.state.imageVersionId = imageVersionId; // create the shared object for all pages of the wizard addPage(new LectureCreationPage(this, state)); addPage(new LectureOptionsPage(this, state)); addPage(new LectureCustomPermissionPage(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 true; } // ok, lets create it try { // push to sat state.uuid = ThriftManager.getSatClient().createLecture(Session.getSatelliteToken(), lectureWriteFromState()); } catch (TException e) { ThriftError.showMessage(this, LOGGER, e, "Fail to create lecture"); return false; } if (Gui.showMessageBox(this, "Veranstaltung erstellt! Wollen Sie zur Übersicht wechseln?", MessageType.QUESTION_YESNO, null, null)) { LectureCache.get(true); MainWindow.showPage(LectureListWindow.class); } // TODO evaluate table state and create the map of permissions // TODO run the actually request over external threaded 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() { return new LectureWrite(state.name, state.description, state.imageVersionId, state.autoUpdate, state.isEnabled, state.start.getTime() / 1000L, state.end.getTime() / 1000L, null, null, state.isExam, state.internetAccess, state.defaultPermissions); } }