aboutsummaryrefslogtreecommitdiff
path: root/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java')
-rw-r--r--id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java136
1 files changed, 136 insertions, 0 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java
new file mode 100644
index 000000000..10ed19f83
--- /dev/null
+++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java
@@ -0,0 +1,136 @@
+package at.gv.egovernment.moa.id.commons.config.persistence;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.persistence.EntityExistsException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Required;
+import org.springframework.stereotype.Component;
+
+import at.gv.egiz.components.configuration.api.Configuration;
+
+/**
+ * The implementation of a key-value configuration implementing the {@link Configuration} interface.
+ * It employs the {@link ConfigPropertyDao} to persist configuration data.
+ */
+@Component
+public class MOAIDConfigurationImpl implements MOAIDConfiguration {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ Configuration configPropertyDao;
+// private JsonMapper mapper = new JsonMapper();
+
+ /**
+ * Sets the {@link ConfigPropertyDao}.
+ * @param configPropertyDao the ConfigPropertyDao
+ */
+ @Required
+ public void setConfigPropertyDao(Configuration configPropertyDao) {
+ this.configPropertyDao = configPropertyDao;
+ }
+
+ @Override
+ public List<String> getAllKeys(){
+ try {
+ return Arrays.asList(this.configPropertyDao.getConfigurationIds());
+ } catch (Exception e) {
+ log.debug("Error while retrieving a list of all keys in the database.");
+ return null;
+ }
+ }
+
+ @Override
+ public String get(String key) {
+ // return null if key does not exist
+ try {
+ return configPropertyDao.getStringValue(key);
+
+ } catch (Exception e) {
+ log.debug("Error while searching value of key '{}' to object.", key);
+ return null;
+ }
+ }
+
+ @Override
+ public <T> T get(String key, Class<T> clazz) {
+ // return null if key does not exist
+ try {
+ T property = configPropertyDao.getObjectValue(key, clazz);
+ return property;
+
+ } 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, String value) {
+
+ try {
+ //TODO: add delete
+ if (value == null) {
+ //configPropertyDao.delete(key);
+ return true;
+ } else {
+ configPropertyDao.setStringValue(key, value);
+ return true;
+ }
+ } catch (EntityExistsException e) {
+ log.debug("Property '{}' already exists!", key);
+ return false;
+ } catch (Exception e) {
+ log.debug("Error while setting value for key '{}' in the database.", key);
+ return false;
+ }
+ }
+
+// @Override
+// public <T> T get(String key, Class<T> clazz, Object defaultValue) {
+//
+// T value = get(key, clazz);
+// if (value != null) {
+// return value;
+// } else {
+// return clazz.cast(defaultValue);
+// }
+// }
+//
+// @SuppressWarnings("unchecked")
+// @Override
+// public <T> List<T> getList(String key, Class<T> clazz) {
+//
+// CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, clazz);
+// 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) {
+// tmp.add(value);
+// }
+// return tmp;
+// } catch (IOException e) {
+// 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>();
+// }
+// }
+
+}