aboutsummaryrefslogtreecommitdiff
path: root/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence
diff options
context:
space:
mode:
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence')
-rw-r--r--id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java10
-rw-r--r--id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java79
-rw-r--r--id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDao.java7
-rw-r--r--id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDaoImpl.java16
4 files changed, 76 insertions, 36 deletions
diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java
index 873208aaf..bc90208b6 100644
--- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java
+++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/Configuration.java
@@ -33,6 +33,16 @@ public interface Configuration {
boolean set(String key, Object value);
/**
+ * Store an object associated with a key. If the given object is set to {@code null} then the entry associated with the key is deleted.
+ *
+ * @param key the key under which the value is stored, respectively key determining the entry to be deleted.
+ * @param value the object to store. if value is set to {@code null} then the entry associated with key {@code key} is deleted.
+ * @param overwrite determines the data should be inserted, even if data is already present (insert or update).
+ * @return {@code true} if the operation was carried out successfully, {@code false} otherwise.
+ */
+ boolean set(String key, Object value, boolean overwrite);
+
+ /**
* Get the object of type {@code T} associated with the given key from the database. If the key does not exist or does not have a value, the given default
* value is returned.
*
diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java
index 5cab9a5ff..297a1db4c 100644
--- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java
+++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java
@@ -42,9 +42,17 @@ public class ConfigurationImpl implements Configuration {
public Object get(String key) {
// return null if key does not exist
try {
- return mapper.deserialize(configPropertyDao.getProperty(key).getValue(), null);
+ ConfigProperty property = configPropertyDao.getProperty(key);
+ if (property != null && property.getValue() != null) {
+ return mapper.deserialize(property.getValue(), null);
+ } else {
+ return null;
+ }
+ } catch (IllegalArgumentException e) {
+ log.debug("Error while searching for key '{}' in the database.", key);
+ return null;
} catch (Exception e) {
- log.trace("Error while deserializing value of key '{}' to object.", key);
+ log.debug("Error while deserializing value of key '{}' to object.", key);
return null;
}
}
@@ -52,37 +60,49 @@ public class ConfigurationImpl implements Configuration {
@Override
public <T> T get(String key, Class<T> clazz) {
// return null if key does not exist
- ConfigProperty property = configPropertyDao.getProperty(key);
- if (property != null && property.getValue() != null) {
- try {
+ try {
+ ConfigProperty property = configPropertyDao.getProperty(key);
+ if (property != null && property.getValue() != null) {
return clazz.cast(mapper.deserialize(property.getValue(), clazz));
- } catch (IOException e) {
- log.trace("Error while deserializing value of key '{}' to object of type {}.",key,clazz.getClass());
+ } else {
return null;
}
- } else {
+ } catch (IllegalArgumentException e) {
+ log.debug("Error while searching for key '{}' in the database.", key);
+ return null;
+ } catch (Exception e) {
+ log.debug("Error while deserializing value of key '{}' to object of type {}.", key, clazz.getClass());
return null;
}
}
@Override
public boolean set(String key, Object value) {
+ return this.set(key, value, false);
+ }
- if (value == null) {
- configPropertyDao.delete(key);
- return true;
- } else {
+ @Override
+ public boolean set(String key, Object value, boolean overwrite) {
+
+ try {
+ if (value == null) {
+ configPropertyDao.delete(key);
+ return true;
+ } else {
+
+ ConfigProperty keyValue = new ConfigProperty();
+ keyValue.setKey(key);
- ConfigProperty keyValue = new ConfigProperty();
- keyValue.setKey(key);
- try {
keyValue.setValue(mapper.serialize(value));
- configPropertyDao.saveProperty(keyValue);
+ configPropertyDao.saveProperty(keyValue, overwrite);
return true;
- } catch (JsonProcessingException e) {
- log.trace("Error while serializing object for key '{}'.", key);
- return false;
}
+ } catch (JsonProcessingException e) {
+ log.debug("Error while serializing object for key '{}'.", key);
+ return false;
+ } catch (Exception e){
+ log.debug("Error while setting value for key '{}' in the database.", key);
+ return false;
}
}
@@ -102,24 +122,27 @@ public class ConfigurationImpl implements Configuration {
public <T> List<T> getList(String key, Class<T> clazz) {
CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, clazz);
-
- if(configPropertyDao.getProperty(key)==null){
- return new ArrayList<T>();
- }
- String json = configPropertyDao.getProperty(key).getValue();
- ObjectMapper mapper = new ObjectMapper();
-
try {
+ if ((configPropertyDao.getProperty(key) == null)
+ || (configPropertyDao.getProperty(key).getValue() == null)) {
+ return new ArrayList<T>();
+ }
+ String json = configPropertyDao.getProperty(key).getValue();
+ ObjectMapper mapper = new ObjectMapper();
+
return (List<T>) mapper.readValue(json, listType);
} catch (JsonMappingException e) {
ArrayList<T> tmp = new ArrayList<T>();
T value = get(key, clazz);
- if(value != null){
+ if (value != null) {
tmp.add(value);
}
return tmp;
} catch (IOException e) {
- log.trace("Error while deserializing value for key '{}' to List<{}>.", key,clazz.getClass());
+ log.debug("Error while deserializing value for key '{}' to List<{}>.", key, clazz.getClass());
+ return new ArrayList<T>();
+ } catch (Exception e){
+ log.debug("Error while searching key '{}' in the database.", key);
return new ArrayList<T>();
}
}
diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDao.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDao.java
index 50dddd745..bfc3bc6cd 100644
--- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDao.java
+++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDao.java
@@ -24,6 +24,13 @@ public interface ConfigPropertyDao {
public void saveProperty(ConfigProperty property);
/**
+ * Persists a given {@link ConfigProperty}.
+ * @param property The property to be persisted.
+ * @param overwrite determines the data should be inserted, even if data is already present (insert or update).
+ */
+ public void saveProperty(ConfigProperty property, boolean overwrite);
+
+ /**
* Returns a {@link List} containing all stored {@linkplain ConfigProperty ConfigProperties}.
* @return The list with the properties.
*/
diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDaoImpl.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDaoImpl.java
index 752c7dc09..dfb1f542f 100644
--- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDaoImpl.java
+++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDaoImpl.java
@@ -16,7 +16,6 @@ import org.springframework.transaction.annotation.Transactional;
* Database backed implementation of the DAO interface
*
*/
-
@Transactional("transactionManager")
public class ConfigPropertyDaoImpl implements ConfigPropertyDao {
@@ -27,14 +26,19 @@ public class ConfigPropertyDaoImpl implements ConfigPropertyDao {
@Override
public void saveProperty(ConfigProperty property) {
+ this.saveProperty(property, false);
+ }
+
+ @Override
+ public void saveProperty(ConfigProperty property, boolean overwrite) {
if (null == em) {
log.error("No EntityManager set!");
return;
}
- if (em.find(ConfigProperty.class, property.getKey()) != null) {
- log.trace("Property '{}' already exists!", property.toString());
- // em.merge(property);
+ log.debug("Storing '{}'.", property.toString());
+ if (overwrite) {
+ em.merge(property);
} else {
log.debug("Storing '{}'.", property.toString());
em.persist(property);
@@ -93,11 +97,7 @@ public class ConfigPropertyDaoImpl implements ConfigPropertyDao {
@Override
public void delete(String key) {
log.debug("Deleting entry with key '{}'.", key);
- try{
em.remove(em.find(ConfigProperty.class, key));
- }catch (IllegalArgumentException e){
- log.trace("Error while deleting entry with key '{}':" + e.getMessage(), key);
- }
}
}