user data dao and tests
diff --git a/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java b/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
index 38e0834..cab2d4c 100644
--- a/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
+++ b/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
@@ -67,6 +67,7 @@
}
}
+ //todo: uniqueness constraints on usersettings and details (every user only one entry for data and settings)
@Override
public int updateSettings(UserSettings settings) throws KustvaktException {
final String sql =
diff --git a/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java b/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
index 4321def..50d22b9 100644
--- a/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
+++ b/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
@@ -73,7 +73,7 @@
}
@Deprecated
- public static final void setProvgiders(PolicyHandlerIface policyHandler,
+ public static final void setProviders(PolicyHandlerIface policyHandler,
EncryptionIface crypto, Collection<ResourceOperationIface> ifaces) {
SecurityManager.policydao = policyHandler;
SecurityManager.crypto = crypto;
diff --git a/src/main/java/de/ids_mannheim/korap/user/UserDataDbIface.java b/src/main/java/de/ids_mannheim/korap/user/UserDataDbIface.java
new file mode 100644
index 0000000..2d2f231
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/user/UserDataDbIface.java
@@ -0,0 +1,21 @@
+package de.ids_mannheim.korap.user;
+
+/**
+ * @author hanl
+ * @date 27/01/2016
+ */
+public interface UserDataDbIface<T extends Userdata> {
+
+ public int store(T data);
+
+ public int update(T data);
+
+ public T get(Integer id);
+
+ public T get(User user);
+
+ public int delete(T data);
+
+ public int deleteAll();
+
+}
diff --git a/src/main/java/de/ids_mannheim/korap/user/UserDetailsDao.java b/src/main/java/de/ids_mannheim/korap/user/UserDetailsDao.java
new file mode 100644
index 0000000..4e16bed
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/user/UserDetailsDao.java
@@ -0,0 +1,134 @@
+package de.ids_mannheim.korap.user;
+
+import de.ids_mannheim.korap.interfaces.db.PersistenceClient;
+import org.springframework.dao.DataAccessException;
+import org.springframework.jdbc.core.RowMapper;
+import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
+import org.springframework.jdbc.support.GeneratedKeyHolder;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.HashMap;
+
+/**
+ * @author hanl
+ * @date 27/01/2016
+ */
+public class UserDetailsDao implements UserDataDbIface<Userdetails2> {
+
+ private NamedParameterJdbcTemplate jdbcTemplate;
+
+ public UserDetailsDao(PersistenceClient client) {
+ this.jdbcTemplate = (NamedParameterJdbcTemplate) client.getSource();
+ }
+
+ @Override
+ public int store(Userdetails2 data) {
+ String sql = "INSERT INTO user_details2 (user_id, data) VALUES (:userid, :data);";
+ MapSqlParameterSource source = new MapSqlParameterSource();
+ source.addValue("userid", data.getUserID());
+ source.addValue("data", data.data());
+
+ GeneratedKeyHolder gen = new GeneratedKeyHolder();
+ try {
+ this.jdbcTemplate.update(sql, source, gen);
+ int id = gen.getKey().intValue();
+ data.setId(id);
+ return id;
+ }catch (DataAccessException e) {
+ e.printStackTrace();
+ return -1;
+ }
+ }
+
+ @Override
+ public int update(Userdetails2 data) {
+ String sql = "UPDATE user_details2 SET data = :data WHERE user_id=:userid;";
+ MapSqlParameterSource source = new MapSqlParameterSource();
+ source.addValue("userid", data.getUserID());
+ source.addValue("data", data.data());
+
+ try {
+ return this.jdbcTemplate.update(sql, source);
+ }catch (DataAccessException e) {
+ return -1;
+ }
+ }
+
+ @Override
+ public Userdetails2 get(Integer id) {
+ String sql = "SELECT * FROM user_details2 WHERE id=:id;";
+ MapSqlParameterSource source = new MapSqlParameterSource();
+ source.addValue("id", id);
+
+ try {
+ return this.jdbcTemplate
+ .queryForObject(sql, source, new RowMapper<Userdetails2>() {
+
+ @Override
+ public Userdetails2 mapRow(ResultSet rs, int rowNum)
+ throws SQLException {
+ Userdetails2 details = new Userdetails2(
+ rs.getInt("user_id"));
+ details.setId(rs.getInt("id"));
+ details.setData(rs.getString("data"));
+ return details;
+ }
+ });
+
+ }catch (DataAccessException e) {
+ return null;
+ }
+ }
+
+ @Override
+ public Userdetails2 get(User user) {
+ String sql = "SELECT * FROM user_details2 WHERE user_id=:userid;";
+ MapSqlParameterSource source = new MapSqlParameterSource();
+ source.addValue("userid", user.getId());
+
+ try {
+ return this.jdbcTemplate
+ .queryForObject(sql, source, new RowMapper<Userdetails2>() {
+
+ @Override
+ public Userdetails2 mapRow(ResultSet rs, int rowNum)
+ throws SQLException {
+ Userdetails2 details = new Userdetails2(
+ rs.getInt("user_id"));
+ details.setId(rs.getInt("id"));
+ details.setData(rs.getString("data"));
+ return details;
+ }
+ });
+
+ }catch (DataAccessException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ @Override
+ public int delete(Userdetails2 data) {
+ String sql = "DELETE FROM user_details2 WHERE id=:id";
+ MapSqlParameterSource source = new MapSqlParameterSource();
+ source.addValue("id", data.getId());
+ try {
+ return this.jdbcTemplate.update(sql, source);
+ }catch (DataAccessException e) {
+ return -1;
+ }
+ }
+
+ @Override
+ public int deleteAll() {
+ String sql = "DELETE FROM user_details2;";
+ try {
+ return this.jdbcTemplate.update(sql, new HashMap<String, Object>());
+ }catch (DataAccessException e) {
+ e.printStackTrace();
+ return -1;
+ }
+ }
+}
diff --git a/src/main/java/de/ids_mannheim/korap/user/Userdata.java b/src/main/java/de/ids_mannheim/korap/user/Userdata.java
new file mode 100644
index 0000000..63fac27
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/user/Userdata.java
@@ -0,0 +1,87 @@
+package de.ids_mannheim.korap.user;
+
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.utils.JsonUtils;
+import lombok.AccessLevel;
+import lombok.Data;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.*;
+
+/**
+ * @author hanl
+ * @date 22/01/2016
+ */
+@Data
+public abstract class Userdata {
+
+ private Integer id;
+ @Getter(AccessLevel.PRIVATE)
+ @Setter(AccessLevel.PRIVATE)
+ private Map<String, Object> fields;
+ @Setter(AccessLevel.PRIVATE)
+ private Integer userID;
+
+ public Userdata(Integer userid) {
+ this.fields = new HashMap<>();
+ this.userID = userid;
+ this.id = -1;
+ }
+
+ public void setData(Map<String, Object> map) throws KustvaktException {
+ Set missing = missing(map);
+ if (!missing.isEmpty())
+ throw new KustvaktException(StatusCodes.MISSING_ARGUMENTS,
+ missing.toString());
+ this.fields.clear();
+ this.fields.putAll(map);
+ }
+
+ private Set<String> missing(Map<String, Object> map) {
+ Set<String> missing = new HashSet<>();
+ for (String key : requiredFields()) {
+ if (!map.containsKey(key))
+ missing.add(key);
+ }
+ return missing;
+ }
+
+ public int size() {
+ return this.fields.size();
+ }
+
+ public Object get(String key) {
+ return this.fields.get(key);
+ }
+
+ public boolean isValid() {
+ return this.missing(this.fields).isEmpty() && this.userID != -1;
+ }
+
+ public Set<String> keys() {
+ return this.fields.keySet();
+ }
+
+ public Collection<Object> values() {
+ return this.fields.values();
+ }
+
+ public void setData(String data) {
+ Map m = JsonUtils.readSimple(data, Map.class);
+ if (m != null)
+ this.fields.putAll(m);
+ }
+
+ public String data() {
+ return JsonUtils.toJSON(this.fields);
+ }
+
+ public void addField(String key, Object value) {
+ this.fields.put(key, value);
+ }
+
+ public abstract String[] requiredFields();
+
+}
diff --git a/src/main/java/de/ids_mannheim/korap/user/Userdetails2.java b/src/main/java/de/ids_mannheim/korap/user/Userdetails2.java
new file mode 100644
index 0000000..141b9b1
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/user/Userdetails2.java
@@ -0,0 +1,20 @@
+package de.ids_mannheim.korap.user;
+
+/**
+ * @author hanl
+ * @date 22/01/2016
+ * persistence issue with query request
+ */
+public class Userdetails2 extends Userdata {
+
+ public Userdetails2(Integer userid) {
+ super(userid);
+ }
+
+ @Override
+ public String[] requiredFields() {
+ return new String[] { Attributes.EMAIL, Attributes.ADDRESS,
+ Attributes.LASTNAME, Attributes.FIRSTNAME };
+ }
+
+}
diff --git a/src/main/resources/db/sqlite/V1__Initial_version.sql b/src/main/resources/db/sqlite/V1__Initial_version.sql
index edbf4ee..c26fce3 100644
--- a/src/main/resources/db/sqlite/V1__Initial_version.sql
+++ b/src/main/resources/db/sqlite/V1__Initial_version.sql
@@ -65,6 +65,18 @@
on delete cascade
);
+CREATE TABLE IF NOT EXISTS user_details2 (
+id INTEGER PRIMARY KEY AUTOINCREMENT,
+user_id INTEGER UNIQUE NOT NULL,
+data BLOB NOT NULL
+);
+
+CREATE TABLE IF NOT EXISTS user_settings2 (
+id INTEGER PRIMARY KEY AUTOINCREMENT,
+user_id INTEGER UNIQUE NOT NULL,
+data BLOB NOT NULL
+);
+
CREATE TABLE IF NOT EXISTS user_queries (
id INTEGER PRIMARY KEY,
diff --git a/src/test/java/UserdataTest.java b/src/test/java/UserdataTest.java
new file mode 100644
index 0000000..fc9e27e
--- /dev/null
+++ b/src/test/java/UserdataTest.java
@@ -0,0 +1,61 @@
+import de.ids_mannheim.korap.config.BeanConfiguration;
+import de.ids_mannheim.korap.user.KorAPUser;
+import de.ids_mannheim.korap.user.User;
+import de.ids_mannheim.korap.user.UserDetailsDao;
+import de.ids_mannheim.korap.user.Userdetails2;
+import org.junit.*;
+
+/**
+ * @author hanl
+ * @date 27/01/2016
+ */
+public class UserdataTest {
+
+ @BeforeClass
+ public static void init() {
+ BeanConfiguration.loadClasspathContext("default-config.xml");
+ }
+
+ @AfterClass
+ public static void drop() {
+ BeanConfiguration.closeApplication();
+ }
+
+ @Before
+ public void clear() {
+ UserDetailsDao dao = new UserDetailsDao(
+ BeanConfiguration.getBeans().getPersistenceClient());
+ assert dao.deleteAll() != -1;
+ }
+
+ @Test
+ public void testDataStore() {
+ User user = new KorAPUser();
+ user.setId(1);
+ UserDetailsDao dao = new UserDetailsDao(
+ BeanConfiguration.getBeans().getPersistenceClient());
+ Userdetails2 d = new Userdetails2(1);
+ d.addField("key_1", "value is a value");
+ assert dao.store(d) != -1;
+ }
+
+ @Test
+ public void testDataGet() {
+ User user = new KorAPUser();
+ user.setId(1);
+ UserDetailsDao dao = new UserDetailsDao(
+ BeanConfiguration.getBeans().getPersistenceClient());
+ Userdetails2 d = new Userdetails2(1);
+ d.addField("key_1", "value is a value");
+ assert dao.store(d) != -1;
+
+ d = dao.get(d.getId());
+ assert d != null;
+ assert "value is a value".equals(d.get("key_1"));
+
+ d = dao.get(user);
+ assert d != null;
+ assert "value is a value".equals(d.get("key_1"));
+ }
+
+}