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
|
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;
import org.openslx.util.Util;
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 addCustomStmt = connection
.prepareStatement("INSERT IGNORE INTO networkshare (lectureid, sharedata)"
+ " VALUES (:lectureid, :sharedata)");
MysqlStatement addPresetStmt = connection
.prepareStatement("INSERT IGNORE INTO networkshare (lectureid, sharepresetid)"
+ " VALUES (:lectureid, :shareid)");
addCustomStmt.setString("lectureid", lectureId);
addPresetStmt.setString("lectureid", lectureId);
for (NetShare share : shares) {
if (share.shareId > 0) {
// Preset
addPresetStmt.setInt("shareid", share.shareId);
addPresetStmt.executeUpdate();
} else {
// Custom
if (Util.isEmptyString(share.path) || Util.isEmptyString(share.mountpoint))
continue;
String netshareJson = Json.serialize(share);
addCustomStmt.setString("sharedata", netshareJson);
addCustomStmt.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 pns.shareid, 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.deserialize(rs.getString("sharedata"), NetShare.class)
.setShareId(rs.getInt("shareid")));
}
return list;
}
public static List<NetShare> getPredefinedNetshares() throws SQLException {
try (MysqlConnection connection = Database.getConnection()) {
List<NetShare> list = new ArrayList<>();
MysqlStatement stmt = connection
.prepareStatement("SELECT shareid, sharedata" + " FROM presetnetworkshare");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
list.add(Json.deserialize(rs.getString("sharedata"), NetShare.class)
.setShareId(rs.getInt("shareid")));
}
return list;
} catch (SQLException e) {
LOGGER.error("Query failed in getPredefinedNetshares()", e);
throw e;
}
}
}
|