summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLog.java
blob: 272405820be0b971043a482779177780d1c4c835 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package org.openslx.bwlp.sat.database.mappers;

import java.sql.SQLException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.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.UserInfo;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
import org.openslx.util.Util;

public class DbLog {

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

	/**
	 * Add entry to logging table.
	 * 
	 * @param userId user causing the action (can be null)
	 * @param targetId object being acted upon (userid, lectureid, imageid, or
	 *            null)
	 * @param description Human readable description of the action being
	 *            performed
	 */
	public static void log(final String userId, final String targetId, final String description) {
		final long timeStamp = Util.unixTime();
		QuickTimer.scheduleOnce(new Task() {
			@Override
			public void fire() {
				try (MysqlConnection connection = Database.getConnection()) {
					MysqlStatement stmt = connection.prepareStatement("INSERT INTO actionlog"
							+ " (dateline, userid, targetid, description) VALUES"
							+ " (:dateline, :userid, :targetid, :description)");
					stmt.setLong("dateline", timeStamp);
					stmt.setString("userid", userId);
					stmt.setString("targetid", targetId);
					stmt.setString("description", description == null ? "" : description);
					stmt.executeUpdate();
					connection.commit();
				} catch (SQLException e) {
					LOGGER.error("Query failed in DbLog.log()", e);
				}
			}
		});
	}

	public static void log(UserInfo user, String targetId, String description) {
		log(user == null ? null : user.userId, targetId, description);
	}

}