summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLectureNetshare.java
blob: 1629178ba8fdd450db346cfabff694a7c2278eed (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
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 (shareid, lectureid, shareuid, sharedata)"
						+ " VALUES (DEFAULT, :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 sharedata FROM networkshare WHERE lectureid = :lectureid");
		netsharestmt.setString("lectureid", lectureId);
		ResultSet rs = netsharestmt.executeQuery();
		while (rs.next()) {
			// TODO get the shares saved on slx-admin, if any shareuid aren't null
			list.add(Json.deserializeThrift(rs.getString("sharedata"), NetShare.class));
		}
		return list;
	}

}