| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.handlers; |
| 2 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 3 | import java.sql.Timestamp; |
| 4 | import java.util.List; |
| 5 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 6 | import org.joda.time.DateTime; |
| 7 | import org.joda.time.LocalDate; |
| 8 | import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; |
| 9 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 10 | import org.springframework.jdbc.core.namedparam.SqlParameterSource; |
| 11 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 12 | import de.ids_mannheim.korap.auditing.AuditRecord; |
| 13 | import de.ids_mannheim.korap.interfaces.db.AuditingIface; |
| 14 | import de.ids_mannheim.korap.interfaces.db.PersistenceClient; |
| 15 | import de.ids_mannheim.korap.user.User; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * @author hanl |
| 19 | * @date 13/01/2014 |
| 20 | */ |
| 21 | public class JDBCAuditing extends AuditingIface { |
| 22 | |
| 23 | private NamedParameterJdbcTemplate template; |
| 24 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 25 | public JDBCAuditing (PersistenceClient client) { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 26 | this.template = (NamedParameterJdbcTemplate) client.getSource(); |
| 27 | } |
| 28 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 29 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 30 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 31 | public <T extends AuditRecord> List<T> retrieveRecords ( |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 32 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 38 | String sql = "select * from audit_records where aud_timestamp > :today AND" |
| 39 | + " aud_timestamp < :tomorr AND aud_category=:cat limit :limit;"; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 40 | |
| 41 | if (dayOnly) { |
| 42 | LocalDate today = day.toLocalDate(); |
| 43 | DateTime start = today.toDateTimeAtStartOfDay(day.getZone()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 44 | DateTime end = today.plusDays(1).toDateTimeAtStartOfDay( |
| 45 | day.getZone()); |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 46 | p.addValue("today", start.getMillis()); |
| 47 | p.addValue("tomorr", end.getMillis()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 48 | } |
| 49 | else { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 50 | p.addValue("today", day.getMillis()); |
| 51 | p.addValue("tomorr", until.getMillis()); |
| 52 | } |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 53 | return (List<T>) this.template.query(sql, p, |
| 54 | new RowMapperFactory.AuditMapper()); |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 57 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 58 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 59 | public <T extends AuditRecord> List<T> retrieveRecords ( |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 60 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 66 | String sql = "select * from audit_records where aud_category=:cat and aud_user=:us " |
| 67 | + "order by aud_timestamp desc limit :limit;"; |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 68 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 69 | return (List<T>) this.template.query(sql, p, |
| 70 | new RowMapperFactory.AuditMapper()); |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 73 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 74 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 75 | public <T extends AuditRecord> List<T> retrieveRecords (LocalDate day, |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 76 | int hitMax) { |
| 77 | return null; |
| 78 | } |
| 79 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 80 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 81 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 82 | public <T extends AuditRecord> List<T> retrieveRecords (String userID, |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 83 | LocalDate start, LocalDate end, int hitMax) { |
| 84 | return null; |
| 85 | } |
| 86 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 87 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 88 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 89 | public void apply () { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 90 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 113 | |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 114 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 115 | public void finish () { |
| Michael Hanl | 72c7b83 | 2015-09-03 08:42:15 +0200 | [diff] [blame] | 116 | |
| 117 | } |
| 118 | |
| 119 | } |