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
|
package org.openslx.bwlp.sat.database.mappers;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.sat.util.Sanitizer;
import org.openslx.bwlp.thrift.iface.LecturePermissions;
public class DbLecturePermissions {
private static final Logger LOGGER = LogManager.getLogger(DbLecturePermissions.class);
/**
* Build an instance of {@link LecturePermissions} by reading the given
* columns from the given {@link ResultSet}. If there are no permissions
* given in the ResultSet, <code>null</code> is returned.
*
* @param rs the {@link ResultSet} to read from
* @param canLink Name of the column to read the "can link" permission from
* @param canDownload Name of the column to read the "can download"
* permission from
* @param canEdit Name of the column to read the "can edit" permission from
* @param canAdmin Name of the column to read the "can admin" permission
* from
* @return instance of {@link LecturePermissions}, or <code>null</code>
* @throws SQLException
*/
private static LecturePermissions fromResultSet(ResultSet rs, String canEdit, String canAdmin)
throws SQLException {
boolean edit = rs.getBoolean(canEdit);
if (rs.wasNull())
return null;
return new LecturePermissions(edit, rs.getBoolean(canAdmin));
}
/**
* Build an instance of {@link LecturePermissions} by reading the
* columns <code>canlink</code>, <code>candownload</code>,
* <code>canedit</code>, <code>canadmin</code> from the given
* {@link ResultSet}. If there are no permissions
* given in the ResultSet, <code>null</code> is returned.
*
* @param rs the {@link ResultSet} to read from
* @return instance of {@link LecturePermissions}, or <code>null</code>
* @throws SQLException
*/
public static LecturePermissions fromResultSetUser(ResultSet rs) throws SQLException {
return fromResultSet(rs, "canedit", "canadmin");
}
/**
* Build an instance of {@link LecturePermissions} by reading the
* columns <code>canlinkdefault</code>, <code>candownloaddefault</code>,
* <code>caneditdefault</code>, <code>canadmindefault</code> from the given
* {@link ResultSet}. If there are no permissions
* given in the ResultSet, <code>null</code> is returned.
*
* @param rs the {@link ResultSet} to read from
* @return instance of {@link LecturePermissions}, or <code>null</code>
* @throws SQLException
*/
public static LecturePermissions fromResultSetDefault(ResultSet rs) throws SQLException {
return fromResultSet(rs, "caneditdefault", "canadmindefault");
}
public static void writeForLecture(String lectureId, Map<String, LecturePermissions> permissions)
throws SQLException {
try (MysqlConnection connection = Database.getConnection()) {
MysqlStatement stmt = connection.prepareStatement("DELETE FROM lecturepermission"
+ " WHERE lectureid = :lectureid");
stmt.setString("lectureid", lectureId);
stmt.executeUpdate();
stmt = connection.prepareStatement("INSERT INTO lecturepermission"
+ " (lectureid, userid, canedit, canadmin)"
+ " VALUES (:lectureid, :userid, :canedit, :canadmin)");
stmt.setString("lectureid", lectureId);
for (Map.Entry<String, LecturePermissions> entry : permissions.entrySet()) {
LecturePermissions perm = entry.getValue();
perm = Sanitizer.handleLecturePermissions(perm);
stmt.setString("userid", entry.getKey());
stmt.setBoolean("canedit", perm.edit);
stmt.setBoolean("canadmin", perm.admin);
stmt.executeUpdate();
}
connection.commit();
} catch (SQLException e) {
LOGGER.error("Query failed in DbLecturePermissions.writeForLecture()", e);
throw e;
}
}
public static Map<String, LecturePermissions> getForLecture(String lectureId, boolean adminOnly)
throws SQLException {
try (MysqlConnection connection = Database.getConnection()) {
MysqlStatement stmt = connection.prepareStatement("SELECT userid, canedit, canadmin"
+ " FROM lecturepermission WHERE lectureid = :lectureid");
stmt.setString("lectureid", lectureId);
ResultSet rs = stmt.executeQuery();
Map<String, LecturePermissions> list = new HashMap<>();
while (rs.next()) {
boolean admin = rs.getBoolean("canadmin");
if (adminOnly && !admin)
continue;
LecturePermissions perm = new LecturePermissions(rs.getBoolean("canedit"), admin);
list.put(rs.getString("userid"), perm);
}
return list;
} catch (SQLException e) {
LOGGER.error("Query failed in DbImagePermissions.getForImageBase()", e);
throw e;
}
}
}
|