summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLectureNetshare.java
blob: 1d505de6d30ba3ea40e4cd36f1bec1bcabf57860 (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
package org.openslx.bwlp.sat.database.mappers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.database.Database;
import org.openslx.bwlp.sat.database.MysqlConnection;
import org.openslx.bwlp.sat.database.MysqlStatement;
import org.openslx.bwlp.thrift.iface.NetShare;
import org.openslx.util.Json;

public class DbLectureNetshare {

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

	public static void writeNetworkShares(MysqlConnection connection, String lectureId, List<NetShare> shares)
			throws SQLException {
		if (lectureId == null || lectureId.isEmpty()) {
			return;
		}
		MysqlStatement delStmt = connection
				.prepareStatement("DELETE FROM networkshare WHERE lectureid = :lectureid");
		delStmt.setString("lectureid", lectureId);
		delStmt.executeUpdate();
		if (shares == null || shares.isEmpty()) {
			return;
		}
		MysqlStatement addStmt = connection
				.prepareStatement("INSERT IGNORE INTO networkshare (lectureid, sharepresetid, sharedata)"
						+ " VALUES (:lectureid, NULL, :sharedata)");
		addStmt.setString("lectureid", lectureId);
		for (NetShare share : shares) {
			String netshareJson = Json.serialize(share);
			addStmt.setString("sharedata", netshareJson);
			addStmt.executeUpdate();
		}
	}

	public static List<NetShare> getLectureNetshares(String lectureId) throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			return getLectureNetshares(connection, lectureId);
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbNetshare.getLectureNetshares()", e);
			throw e;
		}
	}

	public static List<NetShare> getLectureNetshares(MysqlConnection connection, String lectureId)
			throws SQLException {
		List<NetShare> list = new ArrayList<>();
		MysqlStatement netsharestmt = connection.prepareStatement(
				"SELECT IFNULL(pns.sharedata, ns.sharedata) AS sharedata FROM networkshare ns"
						+ " LEFT JOIN presetnetworkshare pns ON (ns.sharepresetid = pns.shareid)"
						+ " WHERE ns.lectureid = :lectureid");
		netsharestmt.setString("lectureid", lectureId);
		ResultSet rs = netsharestmt.executeQuery();
		while (rs.next()) {
			list.add(Json.deserializeThrift(rs.getString("sharedata"), NetShare.class));
		}
		return list;
	}

	public static List<NetShare> getPredefinedNetshares() throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			List<NetShare> list = new ArrayList<>();
			MysqlStatement stmt = connection
					.prepareStatement("SELECT sharedata" + " FROM presetnetworkshare");
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				list.add(Json.deserializeThrift(rs.getString("sharedata"), NetShare.class));
			}
			return list;
		} catch (SQLException e) {
			LOGGER.error("Query failed in getPredefinedNetshares()", e);
			throw e;
		}
	}

}