blob: 2aaa10f67dd89c50171656ddbe8dcde3729aeb3d [file] [log] [blame]
Michael Hanl72c7b832015-09-03 08:42:15 +02001package de.ids_mannheim.korap.handlers;
2
margarethae6f079d2017-10-09 15:57:27 +02003import java.sql.Timestamp;
4import java.util.List;
5
Michael Hanl72c7b832015-09-03 08:42:15 +02006import org.joda.time.DateTime;
7import org.joda.time.LocalDate;
8import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
9import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
10import org.springframework.jdbc.core.namedparam.SqlParameterSource;
11
margarethae6f079d2017-10-09 15:57:27 +020012import de.ids_mannheim.korap.auditing.AuditRecord;
13import de.ids_mannheim.korap.interfaces.db.AuditingIface;
14import de.ids_mannheim.korap.interfaces.db.PersistenceClient;
15import de.ids_mannheim.korap.user.User;
Michael Hanl72c7b832015-09-03 08:42:15 +020016
17/**
18 * @author hanl
19 * @date 13/01/2014
20 */
21public class JDBCAuditing extends AuditingIface {
22
23 private NamedParameterJdbcTemplate template;
24
Michael Hanl8abaf9e2016-05-23 16:46:35 +020025 public JDBCAuditing (PersistenceClient client) {
Michael Hanl72c7b832015-09-03 08:42:15 +020026 this.template = (NamedParameterJdbcTemplate) client.getSource();
27 }
28
Michael Hanl8abaf9e2016-05-23 16:46:35 +020029
Michael Hanl72c7b832015-09-03 08:42:15 +020030 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020031 public <T extends AuditRecord> List<T> retrieveRecords (
Michael Hanl72c7b832015-09-03 08:42:15 +020032 AuditRecord.CATEGORY category, DateTime day, DateTime until,
33 boolean dayOnly, int limit) {
34 MapSqlParameterSource p = new MapSqlParameterSource();
35 p.addValue("limit", limit);
36 p.addValue("cat", category.toString());
37
Michael Hanl8abaf9e2016-05-23 16:46:35 +020038 String sql = "select * from audit_records where aud_timestamp > :today AND"
39 + " aud_timestamp < :tomorr AND aud_category=:cat limit :limit;";
Michael Hanl72c7b832015-09-03 08:42:15 +020040
41 if (dayOnly) {
42 LocalDate today = day.toLocalDate();
43 DateTime start = today.toDateTimeAtStartOfDay(day.getZone());
Michael Hanl8abaf9e2016-05-23 16:46:35 +020044 DateTime end = today.plusDays(1).toDateTimeAtStartOfDay(
45 day.getZone());
Michael Hanl72c7b832015-09-03 08:42:15 +020046 p.addValue("today", start.getMillis());
47 p.addValue("tomorr", end.getMillis());
Michael Hanl8abaf9e2016-05-23 16:46:35 +020048 }
49 else {
Michael Hanl72c7b832015-09-03 08:42:15 +020050 p.addValue("today", day.getMillis());
51 p.addValue("tomorr", until.getMillis());
52 }
Michael Hanl8abaf9e2016-05-23 16:46:35 +020053 return (List<T>) this.template.query(sql, p,
54 new RowMapperFactory.AuditMapper());
Michael Hanl72c7b832015-09-03 08:42:15 +020055 }
56
Michael Hanl8abaf9e2016-05-23 16:46:35 +020057
Michael Hanl72c7b832015-09-03 08:42:15 +020058 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020059 public <T extends AuditRecord> List<T> retrieveRecords (
Michael Hanl72c7b832015-09-03 08:42:15 +020060 AuditRecord.CATEGORY category, User user, int limit) {
61 MapSqlParameterSource p = new MapSqlParameterSource();
62 p.addValue("limit", limit);
63 p.addValue("us", user.getUsername());
64 p.addValue("cat", category.toString());
65
Michael Hanl8abaf9e2016-05-23 16:46:35 +020066 String sql = "select * from audit_records where aud_category=:cat and aud_user=:us "
67 + "order by aud_timestamp desc limit :limit;";
Michael Hanl72c7b832015-09-03 08:42:15 +020068
Michael Hanl8abaf9e2016-05-23 16:46:35 +020069 return (List<T>) this.template.query(sql, p,
70 new RowMapperFactory.AuditMapper());
Michael Hanl72c7b832015-09-03 08:42:15 +020071 }
72
Michael Hanl8abaf9e2016-05-23 16:46:35 +020073
Michael Hanl72c7b832015-09-03 08:42:15 +020074 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020075 public <T extends AuditRecord> List<T> retrieveRecords (LocalDate day,
Michael Hanl72c7b832015-09-03 08:42:15 +020076 int hitMax) {
77 return null;
78 }
79
Michael Hanl8abaf9e2016-05-23 16:46:35 +020080
Michael Hanl72c7b832015-09-03 08:42:15 +020081 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020082 public <T extends AuditRecord> List<T> retrieveRecords (String userID,
Michael Hanl72c7b832015-09-03 08:42:15 +020083 LocalDate start, LocalDate end, int hitMax) {
84 return null;
85 }
86
Michael Hanl8abaf9e2016-05-23 16:46:35 +020087
Michael Hanl72c7b832015-09-03 08:42:15 +020088 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +020089 public void apply () {
Michael Hanl72c7b832015-09-03 08:42:15 +020090 String sql;
91 sql = "INSERT INTO audit_records (aud_target, aud_category, aud_user, aud_location, aud_timestamp, "
92 + "aud_status, aud_field_1, aud_args) "
93 + "VALUES (:target, :category, :account, :loc, :timestamp, :status, :field, :args);";
94 List<AuditRecord> records = getRecordsToSave();
95 SqlParameterSource[] s = new SqlParameterSource[records.size()];
96 for (int i = 0; i < records.size(); i++) {
97 AuditRecord rec = records.get(i);
98 MapSqlParameterSource source = new MapSqlParameterSource();
99 source.addValue("category", rec.getCategory().toString());
100 source.addValue("account", rec.getUserid());
101 source.addValue("target", rec.getTarget());
102 source.addValue("loc", rec.getLoc());
103 source.addValue("timestamp", new Timestamp(rec.getTimestamp()));
104 source.addValue("status", rec.getStatus());
105 source.addValue("field", rec.getField_1());
106 source.addValue("args", rec.getArgs());
107 s[i] = source;
108 }
109 this.template.batchUpdate(sql, s);
110 records.clear();
111 }
112
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200113
Michael Hanl72c7b832015-09-03 08:42:15 +0200114 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200115 public void finish () {
Michael Hanl72c7b832015-09-03 08:42:15 +0200116
117 }
118
119}