package org.openslx.dozmod.gui.wizard;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.I18n;
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.*;
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;
import javax.swing.*;
import java.awt.*;
public class LectureWizard extends Wizard implements UiFeedback {
/**
* Version for serialization.
*/
private static final long serialVersionUID = -7447355922540362345L;
private final static Logger LOGGER = LogManager.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 I18n.WIZARD.getString("Lecture.Wizard.title");
}
@Override
protected boolean onCancelRequest() {
return Gui.showMessageBox(this, I18n.WIZARD.getString("Lecture.Message.yesNo.cancelRequest"),
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, I18n.WIZARD.getString("Lecture.Message.info.finish"), 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;
}
}