summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2015-06-16 18:22:54 +0200
committerSimon Rettberg2015-06-16 18:22:54 +0200
commit9085dcdcb35ae1f9e3a592c8cd5dfecdd4e9bde1 (patch)
treeb7fb7612f4319943426d8ca30d1a8a7fb68b4208
parent[server] On mah way (lots of restructuring, some early db classes, sql dump o... (diff)
downloadtutor-module-9085dcdcb35ae1f9e3a592c8cd5dfecdd4e9bde1.tar.gz
tutor-module-9085dcdcb35ae1f9e3a592c8cd5dfecdd4e9bde1.tar.xz
tutor-module-9085dcdcb35ae1f9e3a592c8cd5dfecdd4e9bde1.zip
[server] Add script field to lecture table; implement getImageDetails method to get detailed information about an image from the database
-rw-r--r--dozentenmodulserver/setup/sat-01-schema.sql107
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java11
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Database.java7
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java73
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbSoftwareTag.java70
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/permissions/UserPermissions.java52
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java16
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java43
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CachedList.java33
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OperatingSystemList.java2
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java14
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/QuickTimer.java4
12 files changed, 337 insertions, 95 deletions
diff --git a/dozentenmodulserver/setup/sat-01-schema.sql b/dozentenmodulserver/setup/sat-01-schema.sql
index 57e86e99..0c52f68b 100644
--- a/dozentenmodulserver/setup/sat-01-schema.sql
+++ b/dozentenmodulserver/setup/sat-01-schema.sql
@@ -11,37 +11,37 @@ USE `sat`;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `setversionbase`(IN baseid CHAR(36) CHARACTER SET ascii)
-BEGIN
- -- Variables
- DECLARE l_current, l_latest VARCHAR(36) CHARACTER SET ascii;
- DECLARE done INT DEFAULT FALSE;
- -- Our two cursors
- DECLARE cur_current CURSOR FOR
- SELECT imageversionid FROM imageversion
- WHERE imagebaseid = baseid AND isenabled = 1 AND isvalid = 1
- ORDER BY createtime DESC LIMIT 1;
- DECLARE cur_latest CURSOR FOR
- SELECT imageversionid FROM imageversion
- WHERE imagebaseid = baseid
- ORDER BY createtime DESC LIMIT 1;
- -- Handler
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
- -- Get the current version id
- OPEN cur_current;
- cur_loop: LOOP
- FETCH FROM cur_current INTO l_current;
- LEAVE cur_loop;
- END LOOP;
- CLOSE cur_current;
- -- Get the latest version id
- OPEN cur_latest;
- lat_loop: LOOP
- FETCH FROM cur_latest INTO l_latest;
- LEAVE lat_loop;
- END LOOP;
- CLOSE cur_latest;
- -- Update image table
- UPDATE imagebase SET currentversionid = l_current, latestversionid = l_latest WHERE imagebaseid = baseid LIMIT 1;
+BEGIN
+ -- Variables
+ DECLARE l_current, l_latest VARCHAR(36) CHARACTER SET ascii;
+ DECLARE done INT DEFAULT FALSE;
+ -- Our two cursors
+ DECLARE cur_current CURSOR FOR
+ SELECT imageversionid FROM imageversion
+ WHERE imagebaseid = baseid AND isenabled = 1 AND isvalid = 1
+ ORDER BY createtime DESC LIMIT 1;
+ DECLARE cur_latest CURSOR FOR
+ SELECT imageversionid FROM imageversion
+ WHERE imagebaseid = baseid
+ ORDER BY createtime DESC LIMIT 1;
+ -- Handler
+ DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
+ -- Get the current version id
+ OPEN cur_current;
+ cur_loop: LOOP
+ FETCH FROM cur_current INTO l_current;
+ LEAVE cur_loop;
+ END LOOP;
+ CLOSE cur_current;
+ -- Get the latest version id
+ OPEN cur_latest;
+ lat_loop: LOOP
+ FETCH FROM cur_latest INTO l_latest;
+ LEAVE lat_loop;
+ END LOOP;
+ CLOSE cur_latest;
+ -- Update image table
+ UPDATE imagebase SET currentversionid = l_current, latestversionid = l_latest WHERE imagebaseid = baseid LIMIT 1;
END$$
DELIMITER ;
@@ -123,35 +123,35 @@ CREATE TABLE IF NOT EXISTS `imageversion` (
DROP TRIGGER IF EXISTS `version_delete_post`;
DELIMITER //
CREATE TRIGGER `version_delete_post` AFTER DELETE ON `imageversion`
- FOR EACH ROW BEGIN
- CALL setversionbase(OLD.imagebaseid);
+ FOR EACH ROW BEGIN
+ CALL setversionbase(OLD.imagebaseid);
END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `version_delete_pre`;
DELIMITER //
CREATE TRIGGER `version_delete_pre` BEFORE DELETE ON `imageversion`
- FOR EACH ROW BEGIN
- UPDATE imagebase SET currentversionid = NULL WHERE currentversionid = OLD.imageversionid;
- UPDATE imagebase SET latestversionid = NULL WHERE latestversionid = OLD.imageversionid;
+ FOR EACH ROW BEGIN
+ UPDATE imagebase SET currentversionid = NULL WHERE currentversionid = OLD.imageversionid;
+ UPDATE imagebase SET latestversionid = NULL WHERE latestversionid = OLD.imageversionid;
END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `version_insert`;
DELIMITER //
CREATE TRIGGER `version_insert` AFTER INSERT ON `imageversion`
- FOR EACH ROW BEGIN
- CALL setversionbase(NEW.imagebaseid);
+ FOR EACH ROW BEGIN
+ CALL setversionbase(NEW.imagebaseid);
END
//
DELIMITER ;
DROP TRIGGER IF EXISTS `version_update`;
DELIMITER //
CREATE TRIGGER `version_update` AFTER UPDATE ON `imageversion`
- FOR EACH ROW BEGIN
- IF NEW.isenabled <> OLD.isenabled OR NEW.isvalid <> OLD.isvalid THEN
- CALL setversionbase(NEW.imagebaseid);
- END IF;
+ FOR EACH ROW BEGIN
+ IF NEW.isenabled <> OLD.isenabled OR NEW.isvalid <> OLD.isvalid THEN
+ CALL setversionbase(NEW.imagebaseid);
+ END IF;
END
//
DELIMITER ;
@@ -163,6 +163,13 @@ CREATE TABLE IF NOT EXISTS `imageversion_x_software` (
KEY `fk_imageversion_x_software_2_idx` (`softwareid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+CREATE TABLE IF NOT EXISTS `software_x_tag` (
+ `softwareid` int(11) NOT NULL,
+ `tagid` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
+ PRIMARY KEY (`softwareid`,`tagid`),
+ KEY `fk_software_x_tag_2_idx` (`tagid`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+
CREATE TABLE IF NOT EXISTS `lecture` (
`lectureid` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`displayname` varchar(100) NOT NULL,
@@ -177,6 +184,7 @@ CREATE TABLE IF NOT EXISTS `lecture` (
`updatetime` bigint(20) NOT NULL,
`ownerid` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`updaterid` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
+ `runscript` text DEFAULT NULL,
`isexam` tinyint(1) NOT NULL,
`hasinternetaccess` tinyint(1) NOT NULL,
`caneditdefault` tinyint(1) NOT NULL,
@@ -230,18 +238,16 @@ CREATE TABLE IF NOT EXISTS `os_x_virt` (
CREATE TABLE IF NOT EXISTS `software` (
`softwareid` int(11) NOT NULL AUTO_INCREMENT COMMENT 'This ID is used internally only, this never leaves the satellite.',
`softwarestring` varchar(120) NOT NULL,
- `tagid` char(36) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
PRIMARY KEY (`softwareid`),
- UNIQUE KEY `softwarestring_UNIQUE` (`softwarestring`),
- KEY `index2` (`tagid`,`softwareid`)
+ UNIQUE KEY `softwarestring_UNIQUE` (`softwarestring`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `tag` (
`tagid` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
`displayname` varchar(32) NOT NULL,
- `isrestricting` tinyint(1) NOT NULL,
+ `isrestricting` tinyint(1) NOT NULL COMMENT 'True if there is no Landeslizenz for this software, meaning it should not be downloadable by students.',
PRIMARY KEY (`tagid`),
- KEY `tagindex` (`displayname`)
+ UNIQUE KEY `tagindex` (`displayname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `user` (
@@ -307,12 +313,13 @@ ALTER TABLE `os_x_virt`
ADD CONSTRAINT `fk_os_x_virt_1` FOREIGN KEY (`osid`) REFERENCES `operatingsystem` (`osid`),
ADD CONSTRAINT `fk_os_x_virt_2` FOREIGN KEY (`virtid`) REFERENCES `virtualizer` (`virtid`);
-ALTER TABLE `software`
- ADD CONSTRAINT `fk_software_1` FOREIGN KEY (`tagid`) REFERENCES `tag` (`tagid`) ON UPDATE CASCADE;
-
ALTER TABLE `user`
ADD CONSTRAINT `fk_user_1` FOREIGN KEY (`organizationid`) REFERENCES `organization` (`organizationid`) ON UPDATE CASCADE;
+ALTER TABLE `software_x_tag`
+ ADD CONSTRAINT `fk_software_x_tag_1` FOREIGN KEY (`softwareid`) REFERENCES `software` (`softwareid`) ON UPDATE CASCADE,
+ ADD CONSTRAINT `fk_software_x_tag_2` FOREIGN KEY (`tagid`) REFERENCES `tag` (`tagid`) ON UPDATE CASCADE;
+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java
index 8aac1fcb..aaa344f8 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/App.java
@@ -1,6 +1,7 @@
package org.openslx.bwlp.sat;
import java.security.NoSuchAlgorithmException;
+import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -64,9 +65,13 @@ public class App {
// DEBUG
if (DEBUG) {
Database.printCharsetInformation();
- List<ImageSummaryRead> allVisible = DbImage.getAllVisible(new UserInfo("bla", "blu", null, null,
- null), null);
- log.info("Got " + allVisible.size());
+ List<ImageSummaryRead> allVisible;
+ try {
+ allVisible = DbImage.getAllVisible(new UserInfo("bla", "blu", null, null, null), null);
+ log.info("Got " + allVisible.size());
+ } catch (SQLException e) {
+ log.warn("could not test query getallvisible");
+ }
QuickTimer.scheduleAtFixedDelay(new TimerTask() {
@Override
public void run() {
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Database.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Database.java
index cfc6530b..422db229 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Database.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/Database.java
@@ -11,6 +11,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.log4j.Logger;
+import org.openslx.bwlp.sat.App;
import org.openslx.bwlp.sat.util.Configuration;
public class Database {
@@ -43,6 +44,9 @@ public class Database {
* @return connection to database, or <code>null</code>
*/
public static MysqlConnection getConnection() {
+ if (App.DEBUG) {
+ LOGGER.info("CONNECTION GET", new RuntimeException());
+ }
MysqlConnection con;
for (;;) {
con = pool.poll();
@@ -87,6 +91,9 @@ public class Database {
* @param connection
*/
static void returnConnection(MysqlConnection connection) {
+ if (App.DEBUG) {
+ LOGGER.info("CONNECTION RETURN", new RuntimeException());
+ }
if (!busyConnections.remove(connection))
throw new RuntimeException("Tried to return a mysql connection to the pool that was not taken!");
pool.add(connection);
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 b772edb4..bbb5dad9 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
@@ -9,16 +9,31 @@ 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.ImageDetailsRead;
import org.openslx.bwlp.thrift.iface.ImagePermissions;
import org.openslx.bwlp.thrift.iface.ImageSummaryRead;
+import org.openslx.bwlp.thrift.iface.ImageVersionDetails;
import org.openslx.bwlp.thrift.iface.ShareMode;
+import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.bwlp.thrift.iface.UserInfo;
public class DbImage {
private static final Logger LOGGER = Logger.getLogger(DbImage.class);
- public static List<ImageSummaryRead> getAllVisible(UserInfo user, List<String> tagSearch) {
+ /**
+ * Get list of all images visible to the given user, optionally filtered by
+ * the given list of tags.
+ *
+ * @param user Instance of {@link UserInfo} representing the user in
+ * question
+ * @param tagSearch list of tags an image must have to be included in the
+ * list.
+ * @return {@link List} of {@link ImageSummaryRead}
+ * @throws SQLException
+ */
+ public static List<ImageSummaryRead> getAllVisible(UserInfo user, List<String> tagSearch)
+ throws SQLException {
try (MysqlConnection connection = Database.getConnection()) {
MysqlStatement stmt = connection.prepareStatement("SELECT"
+ " i.imagebaseid, i.currentversionid, i.latestversionid, i.displayname,"
@@ -51,10 +66,64 @@ public class DbImage {
return list;
} catch (SQLException e) {
LOGGER.error("Query failed in DbImage.getAllVisible()", e);
- return null;
+ throw e;
}
}
+ public static ImageDetailsRead getImageDetails(UserInfo user, String imageBaseId)
+ throws TNotFoundException, SQLException {
+ try (MysqlConnection connection = Database.getConnection()) {
+ MysqlStatement stmt = connection.prepareStatement("SELECT imagebaseid, currentversionid, latestversionid,"
+ + " displayname, description, osid, virtid, createtime, updatetime, ownerid, updaterid,"
+ + " sharemode, istemplate,"
+ + " canlinkdefault, candownloaddefault, caneditdefault, canadmindefault,"
+ + " perm.canlink, perm.candownload, perm.canedit, perm.canadmin"
+ + " FROM imagebase i"
+ + " LEFT JOIN imagepermission perm ON (i.imagebaseid = perm.imagebaseid AND perm.userid = :userid)"
+ + " WHERE i.imagebaseid = :imagebaseid");
+ stmt.setString("userid", user.userId);
+ stmt.setString("imagebaseid", imageBaseId);
+ ResultSet rs = stmt.executeQuery();
+ if (!rs.next())
+ throw new TNotFoundException();
+ // Exists:
+ List<String> tags = DbSoftwareTag.getImageTags(connection, imageBaseId);
+ List<ImageVersionDetails> versions = getImageVersions(connection, imageBaseId);
+ ImagePermissions defaultPermissions = DbImagePermissions.fromResultSetDefault(rs);
+ ImageDetailsRead image = new ImageDetailsRead(rs.getString("imagebaseid"),
+ rs.getString("currentversionid"), rs.getString("latestversionid"), versions,
+ rs.getString("displayname"), rs.getString("description"), tags, rs.getInt("osid"),
+ rs.getString("virtid"), rs.getLong("createtime"), rs.getLong("updatetime"),
+ rs.getString("ownerid"), rs.getString("updaterid"),
+ toShareMode(rs.getString("sharemode")), rs.getByte("istemplate") != 0, defaultPermissions);
+ return image;
+ } catch (SQLException e) {
+ LOGGER.error("Query failed in DbImage.getImage()", e);
+ throw e;
+ }
+ }
+
+ public static List<ImageVersionDetails> getImageVersions(MysqlConnection connection, String imageBaseId)
+ throws SQLException {
+ List<ImageVersionDetails> versionList = new ArrayList<>();
+ MysqlStatement stmt = connection.prepareStatement("SELECT"
+ + " imageversionid, createtime, expiretime, filesize, uploaderid, isenabled,"
+ + " isrestricted, isvalid, isprocessed" + " FROM imageversion"
+ + " WHERE imagebaseid = :imagebaseid");
+ stmt.setString("imagebaseid", imageBaseId);
+ ResultSet rs = stmt.executeQuery();
+ 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("isenabled") != 0, rs.getByte("isrestricted") != 0,
+ rs.getByte("isvalid") != 0, rs.getByte("isprocessed") != 0,
+ DbSoftwareTag.getImageVersionSoftwareList(connection, imageVersionId)));
+ }
+ stmt.close();
+ return versionList;
+ }
+
private static ShareMode toShareMode(String string) {
return ShareMode.valueOf(string);
}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbSoftwareTag.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbSoftwareTag.java
new file mode 100644
index 00000000..6cabd021
--- /dev/null
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbSoftwareTag.java
@@ -0,0 +1,70 @@
+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;
+
+public class DbSoftwareTag {
+
+ private static final Logger LOGGER = Logger.getLogger(DbSoftwareTag.class);
+
+ /**
+ * Get list of software installed in a certain image version.
+ *
+ * @param connection database connection to use
+ * @param imageVersionId UUID of image version
+ * @return list of software products
+ * @throws SQLException
+ */
+ public static List<String> getImageVersionSoftwareList(MysqlConnection connection, String imageVersionId)
+ throws SQLException {
+ MysqlStatement stmt = connection.prepareStatement("SELECT softwarestring FROM software"
+ + " INNER JOIN imageversion_x_software USING (softwareid)"
+ + " WHERE imageversionid = :imageversionid");
+ stmt.setString("imageversionid", imageVersionId);
+ ResultSet rs = stmt.executeQuery();
+ List<String> softwareList = new ArrayList<>();
+ while (rs.next()) {
+ softwareList.add(rs.getString("softwarestring"));
+ }
+ stmt.close();
+ return softwareList;
+ }
+
+ /**
+ * Get list of software installed in a certain image version.
+ *
+ * @param imageVersionId UUID of image version
+ * @return list of software products
+ * @throws SQLException
+ */
+ public static List<String> getImageVersionSoftwareList(String imageVersionId) throws SQLException {
+ try (MysqlConnection connection = Database.getConnection()) {
+ return getImageVersionSoftwareList(connection, imageVersionId);
+ } catch (SQLException e) {
+ LOGGER.error("Query failed in DbSoftware.getImageVersionSoftwareList()", e);
+ throw e;
+ }
+ }
+
+ public static List<String> getImageTags(MysqlConnection connection, String imageBaseId) throws SQLException {
+ MysqlStatement stmt = connection.prepareStatement("SELECT displayname FROM tag"
+ + " INNER JOIN imagebase_x_tag USING (tagid)"
+ + " WHERE imagebaseid = :imagebaseid");
+ stmt.setString("imagebaseid", imageBaseId);
+ ResultSet rs = stmt.executeQuery();
+ List<String> tagList = new ArrayList<>();
+ while (rs.next()) {
+ tagList.add(rs.getString("displayname"));
+ }
+ stmt.close();
+ return tagList;
+ }
+
+}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/permissions/UserPermissions.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/permissions/UserPermissions.java
new file mode 100644
index 00000000..d741aa4b
--- /dev/null
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/permissions/UserPermissions.java
@@ -0,0 +1,52 @@
+package org.openslx.bwlp.sat.permissions;
+
+import org.openslx.bwlp.thrift.iface.ImagePermissions;
+import org.openslx.bwlp.thrift.iface.UserInfo;
+
+public class UserPermissions {
+
+ private enum Permission {
+ LINK,
+ DOWNLOAD,
+ EDIT,
+ ADMIN
+ }
+
+ public static boolean canLinkImage(UserInfo ui, ImagePermissions... imagePermissions) {
+ return canActionImage(Permission.LINK, imagePermissions) || isSuperUser(ui);
+ }
+
+ public static boolean canDownloadImage(UserInfo ui, ImagePermissions... imagePermissions) {
+ return canActionImage(Permission.DOWNLOAD, imagePermissions) || isSuperUser(ui);
+ }
+
+ public static boolean canEditImage(UserInfo ui, ImagePermissions... imagePermissions) {
+ return canActionImage(Permission.EDIT, imagePermissions) || isSuperUser(ui);
+ }
+
+ public static boolean canAdminImage(UserInfo ui, ImagePermissions... imagePermissions) {
+ return canActionImage(Permission.ADMIN, imagePermissions) || isSuperUser(ui);
+ }
+
+ private static boolean canActionImage(Permission checkPerm, ImagePermissions... imagePermissions) {
+ for (ImagePermissions perm : imagePermissions) {
+ if (perm == null)
+ continue;
+ if (checkPerm == Permission.LINK && perm.link)
+ return true;
+ if (checkPerm == Permission.DOWNLOAD && perm.download)
+ return true;
+ if (checkPerm == Permission.EDIT && perm.edit)
+ return true;
+ if (checkPerm == Permission.ADMIN && perm.admin)
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isSuperUser(UserInfo ui) {
+ // TODO: for superuser override
+ return false;
+ }
+
+}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java
index cf26b510..7307fbae 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/ServerHandler.java
@@ -1,6 +1,7 @@
package org.openslx.bwlp.sat.thrift;
import java.nio.ByteBuffer;
+import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@@ -23,6 +24,7 @@ import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.bwlp.thrift.iface.SatelliteServer;
import org.openslx.bwlp.thrift.iface.TAuthorizationException;
+import org.openslx.bwlp.thrift.iface.TInternalServerError;
import org.openslx.bwlp.thrift.iface.TInvalidTokenException;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.bwlp.thrift.iface.TTransferRejectedException;
@@ -109,14 +111,22 @@ public class ServerHandler implements SatelliteServer.Iface {
public List<ImageSummaryRead> getImageList(String userToken, List<String> tagSearch)
throws TAuthorizationException, TException {
UserInfo user = SessionManager.getOrFail(userToken);
- return DbImage.getAllVisible(user, tagSearch);
+ try {
+ return DbImage.getAllVisible(user, tagSearch);
+ } catch (SQLException e) {
+ throw new TInternalServerError();
+ }
}
@Override
public ImageDetailsRead getImageDetails(String userToken, String imageBaseId)
throws TAuthorizationException, TNotFoundException, TException {
- // TODO Auto-generated method stub
- return null;
+ UserInfo user = SessionManager.getOrFail(userToken);
+ try {
+ return DbImage.getImageDetails(user, imageBaseId);
+ } catch (SQLException e) {
+ throw new TInternalServerError();
+ }
}
@Override
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java
new file mode 100644
index 00000000..e42ee9fe
--- /dev/null
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CacheBase.java
@@ -0,0 +1,43 @@
+package org.openslx.bwlp.sat.thrift.cache;
+
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.apache.thrift.TException;
+
+/**
+ * Class that caches an instance of a given class for 10 minutes.
+ * If the cache expired and a fresh instance cannot be acquired,
+ * the old instance will be returned.
+ *
+ * @param <T> The class to cache
+ */
+public abstract class CacheBase<T> {
+
+ private static final Logger LOGGER = Logger.getLogger(CacheBase.class);
+
+ private static final int TIMEOUT = 10 * 60 * 1000;
+
+ private T cachedInstance = null;
+
+ private long cacheTimeout = 0;
+
+ protected abstract T getCallback() throws TException;
+
+ protected synchronized T getInternal() {
+ final long now = System.currentTimeMillis();
+ if (cachedInstance == null || now > cacheTimeout) {
+ try {
+ T freshInstance = getCallback();
+ if (freshInstance != null) {
+ cachedInstance = freshInstance;
+ cacheTimeout = now + TIMEOUT;
+ }
+ } catch (TException e) {
+ LOGGER.warn("Could not retrieve fresh instance of " + getClass().getSimpleName(), e);
+ }
+ }
+ return cachedInstance;
+ }
+
+}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CachedList.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CachedList.java
deleted file mode 100644
index 4c986fd2..00000000
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/CachedList.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package org.openslx.bwlp.sat.thrift.cache;
-
-import java.util.List;
-
-import org.apache.log4j.Logger;
-import org.apache.thrift.TException;
-import org.openslx.util.TimeoutReference;
-
-
-public abstract class CachedList<T> {
-
- private static final Logger LOGGER = Logger.getLogger(CachedList.class);
-
- private final TimeoutReference<List<T>> cachedList = new TimeoutReference<>(600000, null);
-
- protected abstract List<T> getCallback() throws TException;
-
- protected synchronized List<T> getInternal() {
- List<T> list = cachedList.get();
- if (list == null) {
- try {
- list = getCallback();
- } catch (TException e) {
- LOGGER.warn("Could not retrieve " + getClass().getSimpleName() + " list from master server",
- e);
- return null;
- }
- cachedList.set(list);
- }
- return list;
- }
-
-}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OperatingSystemList.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OperatingSystemList.java
index 020ae4ff..58b5d84e 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OperatingSystemList.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OperatingSystemList.java
@@ -10,7 +10,7 @@ import org.openslx.thrifthelper.ThriftManager;
* Holds the list of all known organizations. The list is synchronized with
* the master server.
*/
-public class OperatingSystemList extends CachedList<OperatingSystem> {
+public class OperatingSystemList extends CacheBase<List<OperatingSystem>> {
private static final OperatingSystemList instance = new OperatingSystemList();
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java
index 8db7e7e5..e012e5e2 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/cache/OrganizationList.java
@@ -1,9 +1,11 @@
package org.openslx.bwlp.sat.thrift.cache;
import java.util.List;
+import java.util.TimerTask;
import org.apache.thrift.TException;
import org.openslx.bwlp.sat.database.mappers.DbOrganization;
+import org.openslx.bwlp.sat.util.QuickTimer;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.thrifthelper.ThriftManager;
@@ -11,7 +13,7 @@ import org.openslx.thrifthelper.ThriftManager;
* Holds the list of all known organizations. The list is synchronized with
* the master server.
*/
-public class OrganizationList extends CachedList<Organization> {
+public class OrganizationList extends CacheBase<List<Organization>> {
private static final OrganizationList instance = new OrganizationList();
@@ -21,8 +23,14 @@ public class OrganizationList extends CachedList<Organization> {
@Override
protected List<Organization> getCallback() throws TException {
- List<Organization> organizations = ThriftManager.getMasterClient().getOrganizations();
- DbOrganization.storeOrganizations(organizations);
+ final List<Organization> organizations = ThriftManager.getMasterClient().getOrganizations();
+ // Also store the list in the local data base (asynchronous, in the timer thread)
+ QuickTimer.scheduleOnce(new TimerTask() {
+ @Override
+ public void run() {
+ DbOrganization.storeOrganizations(organizations);
+ }
+ });
return organizations;
}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/QuickTimer.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/QuickTimer.java
index 7a317ff7..ea5831ad 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/QuickTimer.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/util/QuickTimer.java
@@ -27,6 +27,10 @@ public class QuickTimer {
timer.schedule(task, delay);
}
+ public static void scheduleOnce(TimerTask timerTask) {
+ scheduleOnce(timerTask, 1);
+ }
+
/**
* Cancel this timer. Should only be called when the server is shutting
* down.