summaryrefslogblamecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java
blob: 7f2fa4c53fef5d9d4ce0aa5038b2be3fb3109585 (plain) (tree)








































































































































                                                                                                                                         
package org.openslx.bwlp.sat.database.mappers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

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.sat.util.Json;
import org.openslx.bwlp.thrift.iface.LecturePermissions;
import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.bwlp.thrift.iface.LectureWrite;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.bwlp.thrift.iface.UserInfo;

public class DbLecture {

	private static final Logger LOGGER = Logger.getLogger(DbLecture.class);

	public static String create(UserInfo user, LectureWrite lecture) throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("INSERT INTO lecture"
					+ " (lectureid, displayname, description, imageversionid, autoupdate,"
					+ "  isenabled, starttime, endtime, createtime, updatetime,"
					+ "  ownerid, updaterid, runscript, nics, netrules, isexam,"
					+ "  hasinternetaccess, caneditdefault, canadmindefault)"
					+ "         VALUES             "
					+ " (:lectureid, '<defunct>', '<defunct>', :imageversionid, 0,"
					+ "  0, 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(),"
					+ "  :userid, :userid, NULL, NULL, NULL, 0, 0, 0, 0)");
			String lectureId = UUID.randomUUID().toString();
			stmt.setString("lectureid", lectureId);
			stmt.setString("imageversionid", lecture.imageVersionId);
			stmt.setString("userid", user.userId);
			stmt.executeUpdate();
			update(connection, user, lectureId, lecture);
			connection.commit();
			return lectureId;
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbLecture.create()", e);
			throw e;
		}
	}

	public static void setOwner(UserInfo user, String lectureId, String newOwnerId) throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("UPDATE lecture"
					+ " SET ownerid = :ownerid, updaterid = :userid, updatetime = UNIX_TIMESTAMP()"
					+ " WHERE lectureid = :lectureid");
			stmt.setString("ownerid", newOwnerId);
			stmt.setString("userid", user.userId);
			stmt.setString("lectureid", lectureId);
			stmt.executeUpdate();
			connection.commit();
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbLecture.setOwner()", e);
			throw e;
		}
	}

	public static void update(UserInfo user, String lectureId, LectureWrite lecture) throws SQLException {
		try (MysqlConnection connection = Database.getConnection()) {
			update(connection, user, lectureId, lecture);
			connection.commit();
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbLecture.update()", e);
			throw e;
		}
	}

	private static void update(MysqlConnection connection, UserInfo user, String lectureId,
			LectureWrite lecture) throws SQLException {
		String nicsJson = Json.serialize(lecture.nics);
		String netruleJson = Json.serialize(lecture.networkExceptions);
		MysqlStatement stmt = connection.prepareStatement("UPDATE lecture SET "
				+ " displayname = :displayname, description = :description, imageversionid = :imageversionid,"
				+ " autoupdate = :autoupdate, isenabled = :isenabled, starttime = :starttime,"
				+ " endtime = :endtime, createtime = :createtime, updatetime = :updatetime,"
				+ " updaterid = :updaterid, runscript = :runscript, nics = :nics,"
				+ " netrules = :netrules, isexam = :isexam, hasinternetaccess = :hasinternetaccess,"
				+ " caneditdefault = :caneditdefault, canadmindefault = :canadmindefault"
				+ " WHERE lectureid = :lectureid");
		stmt.setString("lectureid", lectureId);
		stmt.setString("displayname", lecture.lectureName);
		stmt.setString("description", lecture.description);
		stmt.setString("imageversionid", lecture.imageVersionId);
		stmt.setBoolean("autoupdate", lecture.autoUpdate);
		stmt.setBoolean("isenabled", lecture.isEnabled);
		stmt.setLong("starttime", lecture.startTime);
		stmt.setLong("endtime", lecture.endTime);
		stmt.setString("updaterid", user.userId);
		stmt.setString("runscript", lecture.runscript);
		stmt.setString("nics", nicsJson);
		stmt.setString("netrules", netruleJson);
		stmt.setBoolean("isexam", lecture.isExam);
		stmt.setBoolean("hasinternetaccess", lecture.hasInternetAccess);
		stmt.setBoolean("caneditdefault", lecture.defaultPermissions.edit);
		stmt.setBoolean("canadmindefault", lecture.defaultPermissions.admin);
		stmt.executeUpdate();
	}

	public static LectureSummary getLectureSummary(UserInfo user, String lectureId) throws SQLException,
			TNotFoundException {
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("SELECT"
					+ " l.lectureid, l.displayname AS lecturename, l.imageversionid, l.isenabled,"
					+ " l.starttime, l.endtime, l.lastused, l.usecount, l.ownerid, l.updaterid,"
					+ " l.isexam, l.hasinternetaccess, l.caneditdefault, l.canadmindefault,"
					+ " perm.canedit, perm.canadmin"
					+ " LEFT JOIN lecturepermission perm ON (perm.lectureid = l.lectureid AND perm.userid = :userid)"
					+ " WHERE lectureid = :lectureid");
			stmt.setString("lectureid", lectureId);
			stmt.setString("userid", user.userId);
			ResultSet rs = stmt.executeQuery();
			if (!rs.next())
				throw new TNotFoundException();
			return resultSetToSummary(rs);
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbLecture.getLectureSummary()", e);
			throw e;
		}
	}

	private static LectureSummary resultSetToSummary(ResultSet rs) throws SQLException {
		LecturePermissions defaultPermissions = DbLecturePermissions.fromResultSetDefault(rs);
		LectureSummary entry = new LectureSummary(rs.getString("lectureid"), rs.getString("lecturename"),
				rs.getString("imageversionid"), null, rs.getBoolean("isenabled"), rs.getLong("starttime"),
				rs.getLong("endtime"), rs.getLong("lastused"), rs.getInt("usecount"),
				rs.getString("ownerid"), rs.getString("updaterid"), rs.getBoolean("isexam"),
				rs.getBoolean("hasinternetaccess"), defaultPermissions, false);
		entry.userPermissions = DbLecturePermissions.fromResultSetUser(rs);
		return entry;
	}

}