summaryrefslogtreecommitdiffstats
path: root/Dozentenmodul/src/gui/intro/Login_GUI.java
blob: 06a87307b83cf67ae5931c8b021b88b5b6cd0c61 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package gui.intro;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import models.SessionData;
import models.person;

import org.apache.thrift.TException;
import org.ini4j.Wini;

import thrift.ImageServer.Client;
import thrift.MasterThriftConnection;
import thrift.ThriftConnection;
import thrift.UserInfo;
import util.GuiOrganizer;
import config.config_file;

@SuppressWarnings("serial")
public class Login_GUI extends JFrame {

	private JPanel contentPane;
	private JTextField lblusername;
	private JPasswordField lblpass;
	JLabel lblConnectionInfo = new JLabel("");
	boolean isClientReturnedCorrectly = false;
	boolean login;
	String masterServerIP = MasterThriftConnection.MASTERSERVERIP;
	MasterThriftConnection thrift;
	Client client;
	String token;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					// Pruefe und Erzeuge gegebenfalls Config
					config_file cf = new config_file();
					boolean conf = cf.createConfig();
					if (conf == true) {
						// Aufruf und Anzeige des Login Fensters
						Login_GUI frame = new Login_GUI();
						frame.setVisible(true);
					} else {
						JOptionPane.showMessageDialog(null,
								"Fehler beim erzeugen der Konfigurationsfile",
								"Message", JOptionPane.ERROR_MESSAGE);
					}

				} catch (Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(null, e.getStackTrace(),
							"Message", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login_GUI() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		// Fenster darf nicht vergroessert werden
		setResizable(false);

		try {
			// Setzt das Look and Feel auf System
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					e.getCause() + "\n" + e.getMessage(), "Debug-Message",
					JOptionPane.ERROR_MESSAGE);
		}
		// Titel des Fensters setzen
		setTitle("Dozentenmodul *Prototyp*");
		// Aktion die beim Schliessen durchgefuehrt werden soll
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Größe setzen und mittig ausrichten
		setBounds(0, 0, 603, 400);
		GuiOrganizer.centerGUI(this);
		
		// Erzeugen eines Panels
		contentPane = new JPanel();
		// Hintergrund Farbe des Panels setzen
		contentPane.setBackground(SystemColor.menu);
		// Rahmen des Fensters setzen
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		// Layout setzen
		contentPane.setLayout(null);
		// Label fuer das Logo erzeugen
		JLabel imgLabel = new JLabel();
		// Groesse und Position des Logos festelegen
		imgLabel.setBounds(150, 11, 350, 142);
		// Pfadangabe des Logos

		ImageIcon icon = new ImageIcon(getClass().getResource(
				"/Logo_bwLehrpool.png"), "Logo");
		// Skalierung des Logos
		Image scaled = icon.getImage().getScaledInstance(270, 64, 0);
		imgLabel.setIcon(new ImageIcon(scaled));
		// Hinzufuegen des Logos in das Fenster
		contentPane.add(imgLabel);

		// Erzeugen und Hinzufuegen des Labels
		JLabel LabelUser = new JLabel("bwIDM-Benutzername:");
		LabelUser.setBounds(111, 164, 134, 20);
		contentPane.add(LabelUser);

		// Erzeugen und Hinzufuegen des Textfeldes
		lblusername = new JTextField();
		lblusername
				.setToolTipText("Bitte geben Sie Ihren bwIDM-Benutzernamen ein.");
		lblusername.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				performLogin();
			}
		});
		lblusername.setBounds(292, 164, 237, 20);
		contentPane.add(lblusername);
		lblusername.setColumns(10);

		// Erzeugen und Hinzufuegen des Labels
		JLabel LabelPass = new JLabel("bwIDM-Passwort:");
		LabelPass.setBounds(111, 195, 134, 20);
		contentPane.add(LabelPass);

		// Erzeugen, Hinzufuegen und definierung der Aktion des Buttons
		JButton BtnLogin = new JButton("Login");
		BtnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				performLogin();
			}// end actionPerformed
		});

		BtnLogin.setBounds(292, 244, 237, 23);
		contentPane.add(BtnLogin);

		// Erzeugen und Hinzufuegen des Passwortfeldes
		lblpass = new JPasswordField();
		lblpass.setToolTipText("Bitte geben Sie Ihr bwIDM-Passwort ein.");
		lblpass.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				performLogin();
			}
		});
		lblpass.setBounds(292, 195, 237, 20);
		contentPane.add(lblpass);

		lblConnectionInfo.setHorizontalAlignment(SwingConstants.LEFT);
		lblConnectionInfo.setBounds(292, 290, 237, 20);
		contentPane.add(lblConnectionInfo);
		//GuiOrganizer.centerGUI(this);
	}// end LoginGUI()

	// Versuche den Benutzer einzuloggen
	public void performLogin() {

		login = false;
		thrift = new MasterThriftConnection();
		client = thrift.getMasterThriftConnection();
		token = null;
		thrift.SessionData result = null;

		try {
			login = true;
			String passText = new String(lblpass.getPassword());
			// check if client has received a connection with information or a
			// null package
			if (checkClientPackage() == true) {
				// package has information
				result = client.authenticate(lblusername.getText(), passText);
				SessionData.session.setAuthToken(result.authToken);
				SessionData.session.setSessionID(result.sessionId);
				SessionData.session.setServerAdress(result.serverAddress);

				// set connection to bwLehrpoolSuiteServer
				ThriftConnection bwthrift = new ThriftConnection();
				server.Server.Client bwClient = bwthrift.getThriftConnection();
				models.Client.clientcon.setClient(bwClient);

				System.out.println("Session-ID: " + result.sessionId
						+ "\nToken: " + result.authToken);

				if (login == true) {
					//Daten speichern
					if (SessionData.session.getAuthToken() == null) {
						System.out.println("Token ist null");
						JOptionPane.showMessageDialog(null,
								"Error: Token ist null", "Token defekt",
								JOptionPane.ERROR_MESSAGE);
					}
					System.out.println("Hole jetzt Infos aus SessionData...");
					UserInfo user = client.getUserFromToken(SessionData.session
							.getAuthToken());

					String hochschule = user.eMail.substring(user.eMail
							.indexOf("@") + 1);

					person.verantwortlicher.setUsername(lblusername.getText()
							.substring(0, lblusername.getText().indexOf("@")));
					person.verantwortlicher.setName(user.lastName);
					person.verantwortlicher.setVorname(user.firstName);
					person.verantwortlicher.setEMail(user.eMail);
					person.verantwortlicher.setHochschule(hochschule);

					try {
						// Lege config File an und entscheide welches Fenster
						// als naechstes geoeffnet wird
						Wini ini = new Wini(
								new File(
										"C:\\Users\\"
												+ System.getProperty("user.name")
												+ "\\AppData\\Roaming\\bwLehrpoolSuite\\config.ini"));
						// Pruefe ob Bills Of Rights schon akzeptiert wurden,
						// wenn
						// nicht zeige diese an
						if (ini.get("main", "BillOfRights", boolean.class) == false) {
							// Erstellen einer Instanz der Aktionsauswahl
							BillOfRights_GUI re = new BillOfRights_GUI();
							re.setVisible(true);
							// Schliessen des Fensters nach erfolgreichen Login
							setVisible(false);
						} else {
							// Pruefe ob die Links zu vmware angezeigt werden
							// sollen
							if (ini.get("main", "vmware", boolean.class) == false) {
								VmWareLink_GUI vm = new VmWareLink_GUI();
								vm.setVisible(true);
								setVisible(false);
							} else {

								// oeffne das Hauptmenue
								MainMenue_GUI main = new MainMenue_GUI();
								main.setVisible(true);
								setVisible(false);
							}
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						JOptionPane.showMessageDialog(null, e.getCause() + "\n"
								+ e.getMessage(), "Debug-Message",
								JOptionPane.ERROR_MESSAGE);
					}

				} else {
					JOptionPane.showMessageDialog(null, "Login denied!",
							"Message", JOptionPane.INFORMATION_MESSAGE);
				}

			} else {
				// package from server is null
				login = false;
				lblConnectionInfo.setForeground(Color.red);
				lblConnectionInfo
						.setText("Fehler: Client nicht mit Server verbunden.");

			}

		} catch (thrift.AuthenticationException e) {
			System.out
					.println("Authentifizierung fehlgeschlagen: " + e.message);
			JOptionPane
					.showMessageDialog(
							null,
							"Benutzeranmeldung fehlgeschlagen: Bitte Benutzername und Passwort \u00fcberpr\u00fcfen.",
							"Anmeldung fehlgeschlagen",
							JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					e.getCause() + "\n" + e.getMessage(), "Message",
					JOptionPane.ERROR_MESSAGE);
			return;
		} catch (TException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					e.getCause() + "\n" + e.getMessage(), "Debug-Message",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
	}// end performLogin

	public boolean checkClientPackage() {
		// check if client is null or has been returned with information
		if (client != null) {
			// client contains information
			isClientReturnedCorrectly = true;
			System.out.println("Verbindungsinformationen erhalten");
		} else {
			// client has returned with error
			isClientReturnedCorrectly = false;
			System.out
					.println("Fehler: Keine korrekten Verbindungsdaten erhalten.");
		}

		return isClientReturnedCorrectly;
	}// end checkServerConnection()

}// end class