summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/LectureWizard.java
blob: bd7c0aebd65a01d71b38e7e06087321643a4ddea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
	}
}