summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLocation.java
blob: 2f6ba4bb22127523f0270365c708cf6c44f23a61 (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.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;
import org.openslx.bwlp.sat.util.Configuration;
import org.openslx.bwlp.thrift.iface.Location;
import org.openslx.util.Util;

public class DbLocation {

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

	public static final List<Location> getLocations() throws SQLException {
		List<Location> list = new ArrayList<>();
		String locationsTable = Configuration.getDbLocationTable();
		if (Util.isEmptyString(locationsTable))
			return list;
		try (MysqlConnection connection = Database.getConnection()) {
			MysqlStatement stmt = connection.prepareStatement("SELECT locationid, parentlocationid, locationname FROM "
					+ locationsTable);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				list.add(new Location(rs.getInt("locationid"), rs.getString("locationname"), rs.getInt("parentlocationid")));
			}
		} catch (SQLException e) {
			LOGGER.error("Query failed in DbLocation.getLocations()", e);
			throw e;
		}
		return list;
	}

	public static List<Integer> getLectureLocations(MysqlConnection connection, String lectureId) throws SQLException {
		List<Integer> list = new ArrayList<>();
		String locationsTable = Configuration.getDbLocationTable();
		if (Util.isEmptyString(locationsTable))
			return list;
		MysqlStatement stmt = connection.prepareStatement("SELECT locationid FROM lecture_x_location"
				+ " WHERE lectureid = :lectureid");
		stmt.setString("lectureid", lectureId);
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
			list.add(rs.getInt("locationid"));
		}
		return list;
	}

}