summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2018-12-10 11:00:17 +0100
committerSimon Rettberg2018-12-10 11:00:17 +0100
commit845399319d0fed09cb6cb3ffde7468247a78d73d (patch)
tree8b7485907b68b05bb41a2d6227c1d21736fbeb11
parent[server] Support downloading and querying preset runscripts (diff)
downloadtutor-module-845399319d0fed09cb6cb3ffde7468247a78d73d.tar.gz
tutor-module-845399319d0fed09cb6cb3ffde7468247a78d73d.tar.xz
tutor-module-845399319d0fed09cb6cb3ffde7468247a78d73d.zip
[server] ADD CLASS! FINISH WRITING!t push
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java3
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbRunScript.java103
2 files changed, 106 insertions, 0 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java
index a8889d78..fd2b8c19 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java
@@ -152,6 +152,9 @@ public class DbLecture {
if (lecture.isSetLdapFilters()) {
DbLectureFilter.writeLdapFilters(connection, lectureId, lecture.ldapFilters);
}
+ if (lecture.isSetPresetScriptIds()) {
+ DbRunScript.writeLectureRunScripts(connection, lectureId, lecture.presetScriptIds);
+ }
stmt.executeUpdate();
}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbRunScript.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbRunScript.java
new file mode 100644
index 00000000..172ef691
--- /dev/null
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbRunScript.java
@@ -0,0 +1,103 @@
+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.PresetRunScript;
+import org.openslx.util.Util;
+
+public class DbRunScript {
+
+ private static final Logger LOGGER = Logger.getLogger(DbRunScript.class);
+
+ public static void writeLectureRunScripts(MysqlConnection connection, String lectureId, List<Integer> scripts)
+ throws SQLException {
+ if (lectureId == null || lectureId.isEmpty()) {
+ return;
+ }
+ MysqlStatement delStmt = connection
+ .prepareStatement("DELETE FROM lecture_x_runscript WHERE lectureid = :lectureid");
+ delStmt.setString("lectureid", lectureId);
+ delStmt.executeUpdate();
+ if (scripts == null || scripts.isEmpty())
+ return;
+ MysqlStatement addStmt = connection
+ .prepareStatement("INSERT IGNORE INTO scripts (lectureid, runscriptid)"
+ + " VALUES (:lectureid, :scriptid)");
+ addStmt.setString("lectureid", lectureId);
+ for (Integer id : scripts) {
+ if (id == null)
+ continue;
+ addStmt.setInt("scriptid", id);
+ addStmt.executeUpdate();
+ }
+ }
+
+ static List<DbLecture.RunScript> getRunScriptsForLaunch(MysqlConnection connection, String lectureId,
+ int osId) {
+ List<DbLecture.RunScript> retval = null;
+ try {
+ MysqlStatement stmt = connection.prepareStatement("SELECT s.content, s.extension, s.visibility, s.passcreds"
+ + " FROM presetrunscript s"
+ + " INNER JOIN lecture_x_runscript lxr ON "
+ + " ((lxr.lectureid = :lectureid AND lxr.runscriptid = s.runscriptid) OR (s.isglobal))"
+ + " INNER JOIN presetrunscript_x_operatingsystem pxo ON "
+ + " (pxo.runscriptid = s.runscriptid AND pxo.osid = :osid)"
+ + " ORDER BY s.scriptname ASC");
+ stmt.setString("lectureid", lectureId);
+ stmt.setInt("osid", osId);
+ ResultSet rs = stmt.executeQuery();
+ while (rs.next()) {
+ if (retval == null) {
+ retval = new ArrayList<>();
+ }
+ retval.add(new DbLecture.RunScript(rs.getString("content"), rs.getString("extension"),
+ rs.getInt("visibility"), rs.getBoolean("passcreds")));
+ }
+ } catch (SQLException e) {
+ // DERP
+ }
+ return retval;
+ }
+
+ public static List<PresetRunScript> getPredefinedRunScripts() throws SQLException {
+ List<PresetRunScript> list = new ArrayList<>();
+ try (MysqlConnection connection = Database.getConnection()) {
+ ResultSet rs = connection.prepareStatement(
+ "SELECT s.runscriptid, s.scriptname,"
+ + " Group_Concat(sxo.osid) AS osids FROM presetrunscript s"
+ + " INNER JOIN presetrunscript_x_operatingsystem sxo USING (runscriptid)"
+ + " WHERE isglobal = 0 "
+ + " GROUP BY runscriptid").executeQuery();
+ while (rs.next()) {
+ list.add(new PresetRunScript(rs.getInt("runscriptid"), rs.getString("scriptname"),
+ splitStringToInt(rs.getString("osids"))));
+ }
+ } catch (SQLException e) {
+ LOGGER.error("Query failed in getPredefinedRunScripts()", e);
+ throw e;
+ }
+ return list;
+ }
+
+ private static List<Integer> splitStringToInt(String input) {
+ if (input == null)
+ return new ArrayList<>(0);
+ String[] parts = input.split(",");
+ List<Integer> list = new ArrayList<>(parts.length);
+ for (String s : parts) {
+ int i = Util.parseInt(s, -1);
+ if (i == -1)
+ continue;
+ list.add(i);
+ }
+ return list;
+ }
+
+}