summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLectureFilter.java
blob: dddd47b0df5bc402e8956d6dde910c091c8fdd73 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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.MysqlConnection;
import org.openslx.bwlp.sat.database.MysqlStatement;
import org.openslx.bwlp.sat.web.XmlFilterEntry;
import org.openslx.bwlp.thrift.iface.LdapFilter;
import org.openslx.util.Util;

public class DbLectureFilter {

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

	public static List<LdapFilter> getLectureLdapFilters(MysqlConnection connection, String lectureId)
			throws SQLException {
		List<LdapFilter> list = new ArrayList<>();
		MysqlStatement stmt = connection.prepareStatement("SELECT filterkey, filtervalue FROM lecturefilter"
				+ " WHERE lectureid = :lectureid AND filtertype = 'LDAP'");
		stmt.setString("lectureid", lectureId);
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
			String key = rs.getString("filterkey");
			String value = rs.getString("filtervalue");
			list.add(new LdapFilter(key, value));
		}
		return list;
	}

	public static final List<XmlFilterEntry> getFiltersXml(MysqlConnection connection, String lectureId)
			throws SQLException {
		List<XmlFilterEntry> list = null;
		MysqlStatement stmt = connection.prepareStatement("SELECT filterkey, filtervalue FROM lecturefilter"
				+ " WHERE lectureid = :lectureid AND filtertype = 'LDAP'");
		stmt.setString("lectureid", lectureId);
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
			String key = rs.getString("filterkey");
			String value = rs.getString("filtervalue");
			if (list == null) {
				list = new ArrayList<>();
			}
			list.add(new XmlFilterEntry("LDAP", key, value));
		}
		return list;
	}

	public static void writeLdapFilters(MysqlConnection connection, String lectureId, List<LdapFilter> list)
			throws SQLException {
		if (lectureId == null || lectureId.isEmpty()) {
			return;
		}
		MysqlStatement delStmt = connection.prepareStatement("DELETE FROM lecturefilter WHERE lectureid = :lectureid");
		delStmt.setString("lectureid", lectureId);
		delStmt.executeUpdate();
		if (list == null || list.isEmpty()) {
			return;
		}
		MysqlStatement addStmt = connection.prepareStatement("INSERT INTO lecturefilter (lectureid, filtertype, filterkey, filtervalue)"
				+ " VALUES (:lectureid, 'LDAP', :key, :value)");
		addStmt.setString("lectureid", lectureId);
		for (LdapFilter filter : list) {
			if (Util.isEmptyString(filter.attribute) || filter.value == null)
				continue;
			addStmt.setString("key", filter.attribute);
			addStmt.setString("value", filter.value);
			addStmt.executeUpdate();
		}
	}

}