From efcd12e072d871f2b112d8ab78f6c257c610fbc6 Mon Sep 17 00:00:00 2001 From: Stephan Schwär Date: Mon, 19 Apr 2021 21:41:26 +0200 Subject: [server] Remove redundant null values in sql query --- .../openslx/bwlp/sat/database/mappers/DbImage.java | 53 +++++++--------------- 1 file changed, 17 insertions(+), 36 deletions(-) diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java index 2c58773a..8482087a 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java @@ -88,17 +88,11 @@ public class DbImage { public static ImageDetailsRead getImageDetails(UserInfo user, String imageBaseId) throws TNotFoundException, SQLException { try (MysqlConnection connection = Database.getConnection()) { - - // if Student is trying to download only needed information is filled MysqlStatement stmt = null; - if (user.role == Role.STUDENT) + // Students should only be able to request a download of an image. Therefore not all information is needed for this task. + if (user.role == Role.STUDENT) { - // Todo remove evaluate and minimize the null placeholders - stmt = connection.prepareStatement("SELECT i.imagebaseid, i.latestversionid," - + " null, null, null, null, null, null, null, null," - + " null, null," - + " null, null, null, null," - + " null, null, null, null" + stmt = connection.prepareStatement("SELECT i.imagebaseid, i.latestversionid" + " FROM imagebase i" + " LEFT JOIN imagepermission perm ON (i.imagebaseid = perm.imagebaseid AND perm.userid = :userid)" + " WHERE i.imagebaseid = :imagebaseid"); @@ -111,10 +105,7 @@ public class DbImage { + " FROM imagebase i" + " LEFT JOIN imagepermission perm ON (i.imagebaseid = perm.imagebaseid AND perm.userid = :userid)" + " WHERE i.imagebaseid = :imagebaseid"); - } - - // if Student is trying to download only needed information is filled stmt.setString("userid", user == null ? "-" : user.userId); stmt.setString("imagebaseid", imageBaseId); ResultSet rs = stmt.executeQuery(); @@ -125,16 +116,15 @@ public class DbImage { List versions = getImageVersions(connection, imageBaseId, user); ImageDetailsRead image; - if (user.role == Role.STUDENT) { + // Students should only have download permissions. + // todo evaluate if this is needed and if there is a nicer way to create ImageDetailsRead object ImagePermissions defaultPermissions = new ImagePermissions(false, true, false, false); - image = new ImageDetailsRead(rs.getString("imagebaseid"), rs.getString("latestversionid"), versions, imageBaseId, imageBaseId, tags, 0, imageBaseId, 0, 0, imageBaseId, imageBaseId, null, false, defaultPermissions); image.setUserPermissions(defaultPermissions); } else { ImagePermissions defaultPermissions = DbImagePermissions.fromResultSetDefault(rs); - image = new ImageDetailsRead(rs.getString("imagebaseid"), rs.getString("latestversionid"), versions, rs.getString("displayname"), rs.getString("description"), tags, rs.getInt("osid"), rs.getString("virtid"), @@ -142,7 +132,6 @@ public class DbImage { rs.getString("updaterid"), toShareMode(rs.getString("sharemode")), rs.getByte("istemplate") != 0, defaultPermissions); image.setUserPermissions(DbImagePermissions.fromResultSetUser(rs)); - } User.setCombinedUserPermissions(image, user); return image; @@ -297,28 +286,20 @@ public class DbImage { } stmt.setString("imagebaseid", imageBaseId); ResultSet rs = stmt.executeQuery(); - if (user.role == Role.STUDENT) { - while (rs.next()) { - String imageVersionId = rs.getString("imageversionid"); - versionList.add(new ImageVersionDetails(imageVersionId, rs.getLong("createtime"), - // todo evaluate this empty string for uploaderid - rs.getLong("expiretime"), rs.getLong("filesize"), "", - rs.getByte("isrestricted") != 0, rs.getByte("isvalid") != 0, - rs.getByte("isprocessed") != 0, DbSoftwareTag.getImageVersionSoftwareList(connection, - imageVersionId))); - } - - } else { - while (rs.next()) { - String imageVersionId = rs.getString("imageversionid"); - versionList.add(new ImageVersionDetails(imageVersionId, rs.getLong("createtime"), - rs.getLong("expiretime"), rs.getLong("filesize"), rs.getString("uploaderid"), - rs.getByte("isrestricted") != 0, rs.getByte("isvalid") != 0, - rs.getByte("isprocessed") != 0, DbSoftwareTag.getImageVersionSoftwareList(connection, - imageVersionId))); + + while (rs.next()) { + String imageVersionId = rs.getString("imageversionid"); + String uploaderID = ""; + // Only student doesn't know the uploaderid + if (user.role != Role.STUDENT) { + uploaderID = rs.getString("uploaderid"); } + versionList.add(new ImageVersionDetails(imageVersionId, rs.getLong("createtime"), + rs.getLong("expiretime"), rs.getLong("filesize"), uploaderID, + rs.getByte("isrestricted") != 0, rs.getByte("isvalid") != 0, + rs.getByte("isprocessed") != 0, DbSoftwareTag.getImageVersionSoftwareList(connection, + imageVersionId))); } - stmt.close(); return versionList; } -- cgit v1.2.3-55-g7522