userdata dao via generics type reference
diff --git a/src/main/java/de/ids_mannheim/korap/config/KustvaktClassLoader.java b/src/main/java/de/ids_mannheim/korap/config/KustvaktClassLoader.java
index c175824..f63a023 100644
--- a/src/main/java/de/ids_mannheim/korap/config/KustvaktClassLoader.java
+++ b/src/main/java/de/ids_mannheim/korap/config/KustvaktClassLoader.java
@@ -38,6 +38,8 @@
return reflections.getTypesAnnotatedWith(annotation);
}
+
+
@Deprecated
public static void registerResourceClasses() {
PersistenceClient cl = BeanConfiguration.getBeans()
diff --git a/src/main/java/de/ids_mannheim/korap/interfaces/db/EntityHandlerIface.java b/src/main/java/de/ids_mannheim/korap/interfaces/db/EntityHandlerIface.java
index f349f23..ec70921 100644
--- a/src/main/java/de/ids_mannheim/korap/interfaces/db/EntityHandlerIface.java
+++ b/src/main/java/de/ids_mannheim/korap/interfaces/db/EntityHandlerIface.java
@@ -12,12 +12,16 @@
* Time: 11:04 AM
*/
public interface EntityHandlerIface {
+ @Deprecated
UserSettings getUserSettings(Integer userid) throws KustvaktException;
+ @Deprecated
int updateSettings(UserSettings settings) throws KustvaktException;
+ @Deprecated
UserDetails getUserDetails(Integer userid) throws KustvaktException;
+ @Deprecated
int updateUserDetails(UserDetails details) throws KustvaktException;
// List<UserQuery> getUserQueries(User user) throws KorAPException;
@@ -26,8 +30,8 @@
// void updateUserQueries(User user, List<UserQuery> newOnes) throws KorAPException;
- User getAccount(String username) throws
- EmptyResultException, KustvaktException;
+ User getAccount(String username)
+ throws EmptyResultException, KustvaktException;
int updateAccount(User user) throws KustvaktException;
@@ -35,8 +39,8 @@
int deleteAccount(Integer userid) throws KustvaktException;
- int resetPassphrase(String username, String uriToken,
- String passphrase) throws KustvaktException;
+ int resetPassphrase(String username, String uriToken, String passphrase)
+ throws KustvaktException;
int activateAccount(String username, String uriToken)
throws KustvaktException;
diff --git a/src/main/java/de/ids_mannheim/korap/interfaces/db/ResourceOperationIface.java b/src/main/java/de/ids_mannheim/korap/interfaces/db/ResourceOperationIface.java
index cfa4c99..d35f924 100644
--- a/src/main/java/de/ids_mannheim/korap/interfaces/db/ResourceOperationIface.java
+++ b/src/main/java/de/ids_mannheim/korap/interfaces/db/ResourceOperationIface.java
@@ -11,6 +11,8 @@
// todo: user instance only required for auditing pointcut operations
public interface ResourceOperationIface<T extends KustvaktResource> {
+ // todo: remove and use type reference!
+ @Deprecated
Class<T> getType();
<T extends KustvaktResource> T findbyId(String id, User user)
diff --git a/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java b/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
index d34f968..1309458 100644
--- a/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
+++ b/src/main/java/de/ids_mannheim/korap/security/auth/KustvaktAuthenticationManager.java
@@ -657,6 +657,7 @@
new DateTime(param.getUriExpiration()) };
}
+ @Deprecated
public void updateUserSettings(User user, UserSettings settings)
throws KustvaktException {
if (user instanceof DemoUser)
@@ -674,6 +675,7 @@
}
}
+ @Deprecated
public void updateUserDetails(User user, UserDetails details)
throws KustvaktException {
if (user instanceof DemoUser)
@@ -692,8 +694,26 @@
}
}
+ public <T extends Userdata> T getUserData(User user, Class<T> clazz) {
+ UserDataDbIface<T> dao = UserdataFactory.getInstance(clazz);
+ return dao.get(user);
+ }
+
+ //todo: cache userdata outside of the user object!
+ public void updateUserData(User user, Userdata data) {
+ UserDataDbIface dao = UserdataFactory.getInstance(data.getClass());
+ dao.update(data);
+ }
+
+ // todo: use getuserdata for that!
+ @Deprecated
public UserDetails getUserDetails(User user) throws KustvaktException {
try {
+ for (Userdata data : user.getUserdata()) {
+ if (data instanceof Userdetails2) {
+
+ }
+ }
if (user.getDetails() == null)
user.setDetails(entHandler.getUserDetails(user.getId()));
}catch (KustvaktException e) {
@@ -702,6 +722,7 @@
return user.getDetails();
}
+ @Deprecated
public UserSettings getUserSettings(User user) throws KustvaktException {
try {
if (user.getSettings() == null)
diff --git a/src/main/java/de/ids_mannheim/korap/user/User.java b/src/main/java/de/ids_mannheim/korap/user/User.java
index 5c4510e..346af53 100644
--- a/src/main/java/de/ids_mannheim/korap/user/User.java
+++ b/src/main/java/de/ids_mannheim/korap/user/User.java
@@ -30,6 +30,8 @@
private UserDetails details;
private List<UserQuery> queries;
+ private List<? extends Userdata> userdata;
+
protected User() {
this.fields = new ParamFields();
this.accountCreation = TimeUtils.getNow().getMillis();
@@ -58,35 +60,38 @@
//todo: repair transfer
public void transfer(User user) {
- this.setSettings(user.getSettings());
- this.setDetails(user.getDetails());
+ // this.setSettings(user.getSettings());
+ // this.setDetails(user.getDetails());
// this.setQueries(user.getQueries());
if (this instanceof KorAPUser) {
- this.getSettings().setUserID(this.id);
- this.getDetails().setUserID(this.id);
+
+ // this.getSettings().setUserID(this.id);
+ // this.getDetails().setUserID(this.id);
// for (UserQuery q : this.getQueries())
// q.setOwner(this.accountID);
}
}
+ @Deprecated
public void setDetails(UserDetails details) {
if (details != null)
details.setUserID(this.id);
- this.details = details;
+ // this.details = details;
}
+ @Deprecated
public void setSettings(UserSettings settings) {
if (settings != null)
settings.setUserID(this.id);
- this.settings = settings;
+ // this.settings = settings;
}
public void setId(Integer id) {
this.id = id;
- if (this.settings != null)
- this.settings.setUserID(this.id);
- if (this.details != null)
- this.details.setUserID(this.id);
+ // if (this.settings != null)
+ // this.settings.setUserID(this.id);
+ // if (this.details != null)
+ // this.details.setUserID(this.id);
}
public Map<String, Object> toMap() {
@@ -95,15 +100,11 @@
//TimeUtils.format(new DateTime(this.accountCreation))
map.put(Attributes.ACCOUNT_CREATION, this.accountCreation);
- if (this.getDetails() != null)
- map.putAll(this.getDetails().toMap());
+ // if (this.getDetails() != null)
+ // map.putAll(this.getDetails().toMap());
return map;
}
- public String toJson() {
- return JsonUtils.toJSON(this.toMap());
- }
-
public Map toCache() {
Map map = new HashMap();
map.put(Attributes.ID, this.id);
diff --git a/src/main/java/de/ids_mannheim/korap/user/UserDataDbIface.java b/src/main/java/de/ids_mannheim/korap/user/UserDataDbIface.java
index 2d2f231..6d4e2a1 100644
--- a/src/main/java/de/ids_mannheim/korap/user/UserDataDbIface.java
+++ b/src/main/java/de/ids_mannheim/korap/user/UserDataDbIface.java
@@ -6,6 +6,8 @@
*/
public interface UserDataDbIface<T extends Userdata> {
+ public Class<? extends Userdata> getType();
+
public int store(T data);
public int update(T data);
diff --git a/src/main/java/de/ids_mannheim/korap/user/UserDetailsDao.java b/src/main/java/de/ids_mannheim/korap/user/UserDetailsDao.java
index 4e16bed..0bd204d 100644
--- a/src/main/java/de/ids_mannheim/korap/user/UserDetailsDao.java
+++ b/src/main/java/de/ids_mannheim/korap/user/UserDetailsDao.java
@@ -24,6 +24,11 @@
}
@Override
+ public Class<Userdetails2> getType() {
+ return Userdetails2.class;
+ }
+
+ @Override
public int store(Userdetails2 data) {
String sql = "INSERT INTO user_details2 (user_id, data) VALUES (:userid, :data);";
MapSqlParameterSource source = new MapSqlParameterSource();
diff --git a/src/main/java/de/ids_mannheim/korap/user/Userdata.java b/src/main/java/de/ids_mannheim/korap/user/Userdata.java
index 63fac27..1ffdfe1 100644
--- a/src/main/java/de/ids_mannheim/korap/user/Userdata.java
+++ b/src/main/java/de/ids_mannheim/korap/user/Userdata.java
@@ -84,4 +84,5 @@
public abstract String[] requiredFields();
+
}
diff --git a/src/main/java/de/ids_mannheim/korap/user/UserdataFactory.java b/src/main/java/de/ids_mannheim/korap/user/UserdataFactory.java
new file mode 100644
index 0000000..43000e1
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/user/UserdataFactory.java
@@ -0,0 +1,62 @@
+package de.ids_mannheim.korap.user;
+
+import de.ids_mannheim.korap.config.BeanConfiguration;
+import de.ids_mannheim.korap.config.KustvaktClassLoader;
+import de.ids_mannheim.korap.interfaces.db.PersistenceClient;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @author hanl
+ * @date 27/01/2016
+ */
+public class UserdataFactory {
+
+ private static final Map<Class<? extends Userdata>, UserDataDbIface> instances = new HashMap<>();
+
+ private UserdataFactory() {
+ }
+
+ public static Class<? extends UserDataDbIface> getClass(
+ Class<? extends Userdata> data) {
+ Set<Class<? extends UserDataDbIface>> c = KustvaktClassLoader
+ .loadSubTypes(UserDataDbIface.class);
+ for (Class<? extends UserDataDbIface> o : c) {
+ Type type = o.getGenericInterfaces()[0];
+ if (type instanceof ParameterizedType) {
+ ParameterizedType ptype = (ParameterizedType) type;
+ Class ctype = (Class) ptype.getActualTypeArguments()[0];
+ if (ctype.equals(data))
+ return o;
+ }
+ }
+ return null;
+ }
+
+ public static UserDataDbIface getInstance(Class<? extends Userdata> data) {
+ if (instances.get(data) == null) {
+ Class<? extends UserDataDbIface> cl = getClass(data);
+ if (BeanConfiguration.hasContext()) {
+ try {
+ Constructor c = cl.getConstructor(PersistenceClient.class);
+ UserDataDbIface iface = (UserDataDbIface) c.newInstance(
+ BeanConfiguration.getBeans()
+ .getPersistenceClient());
+ instances.put(data, iface);
+ return iface;
+ }catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
+ return null;
+ }
+
+ }
+ }else
+ return instances.get(data);
+ return null;
+ }
+}
diff --git a/src/test/java/de/ids_mannheim/korap/config/ConfigTest.java b/src/test/java/de/ids_mannheim/korap/config/ConfigTest.java
index d4f2ad5..ded5cff 100644
--- a/src/test/java/de/ids_mannheim/korap/config/ConfigTest.java
+++ b/src/test/java/de/ids_mannheim/korap/config/ConfigTest.java
@@ -6,6 +6,8 @@
import de.ids_mannheim.korap.security.ac.ResourceFinder;
import de.ids_mannheim.korap.user.Attributes;
import de.ids_mannheim.korap.user.User;
+import de.ids_mannheim.korap.user.UserdataFactory;
+import de.ids_mannheim.korap.user.Userdetails2;
import de.ids_mannheim.korap.utils.ServiceVersion;
import de.ids_mannheim.korap.utils.TimeUtils;
import org.junit.After;
@@ -76,6 +78,11 @@
BeanConfiguration.getBeans().getEncryption()
.validateEntry(v, Attributes.EMAIL);
}
+
+ @Test
+ public void testTypeReference() {
+ UserdataFactory.getInstance(Userdetails2.class);
+ }
}