From 845399319d0fed09cb6cb3ffde7468247a78d73d Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 10 Dec 2018 11:00:17 +0100 Subject: [server] ADD CLASS! FINISH WRITING!t push --- .../bwlp/sat/database/mappers/DbLecture.java | 3 + .../bwlp/sat/database/mappers/DbRunScript.java | 103 +++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbRunScript.java (limited to 'dozentenmodulserver') 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 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 getRunScriptsForLaunch(MysqlConnection connection, String lectureId, + int osId) { + List 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 getPredefinedRunScripts() throws SQLException { + List 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 splitStringToInt(String input) { + if (input == null) + return new ArrayList<>(0); + String[] parts = input.split(","); + List list = new ArrayList<>(parts.length); + for (String s : parts) { + int i = Util.parseInt(s, -1); + if (i == -1) + continue; + list.add(i); + } + return list; + } + +} -- cgit v1.2.3-55-g7522