From cb6daf2e54d72a4a0bdca0252d96191440cd5f52 Mon Sep 17 00:00:00 2001 From: Christian Wagner Date: Tue, 13 Jan 2015 10:23:25 +0100 Subject: move 'persistence' package from project 'moa-id-DTI' here --- .../id/conf/persistence/dal/ConfigProperty.java | 95 ++++++++++++++++++++++ .../id/conf/persistence/dal/ConfigPropertyDao.java | 45 ++++++++++ .../persistence/dal/ConfigPropertyDaoImpl.java | 88 ++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigProperty.java create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDao.java create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDaoImpl.java (limited to 'id/server/moa-id-commons/src/main') diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigProperty.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigProperty.java new file mode 100644 index 000000000..7e4e217b0 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigProperty.java @@ -0,0 +1,95 @@ +package com.datentechnik.moa.id.conf.persistence.dal; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Lob; +import javax.persistence.Table; + +/** + * Reflects a MOAID configuration entry. + * + */ +@Table(name = "moaid_configuration") +@Entity +public class ConfigProperty implements Serializable { + private static final long serialVersionUID = 1L; + + @Id + @Column(name = "propertyKey", unique = true) + private String key; + + @Lob + @Column(name = "propertyValue") + private String value; + + /** + * Returns the property's key. + * @return The key. + */ + public String getKey() { + return key; + } + + /** + * Sets the property's key. + * @param key The key + */ + public void setKey(String key) { + this.key = key; + } + + /** + * Returns the property's value (which might be {@code null}). + * @return The property's value (might be {@code null}). + */ + public String getValue() { + return value; + } + + /** + * Sets the property's value. + * @param value The value + */ + public void setValue(String value) { + this.value = value; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((key == null) ? 0 : key.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ConfigProperty other = (ConfigProperty) obj; + if (key == null) { + if (other.key != null) + return false; + } else if (!key.equals(other.key)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ConfigProperty [key="); + builder.append(key); + builder.append(", value="); + builder.append(value); + builder.append("]"); + return builder.toString(); + } +} 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 new file mode 100644 index 000000000..a11d23ce8 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDao.java @@ -0,0 +1,45 @@ +package com.datentechnik.moa.id.conf.persistence.dal; + +import java.util.List; +import java.util.Set; + +/** + * DAO interface providing means for accessing MOAID configuration properties. + * + */ +public interface ConfigPropertyDao { + + /** + * Returns the {@link ConfigProperty} associated with {@code key} or {@code null} if the entry does not exist. + * + * @param key The configuration key. + * @return The configuration property value or {@code null}. + */ + public ConfigProperty getProperty(String key); + + /** + * Persists a given {@link ConfigProperty}. + * @param property The property to be persisted. + */ + public void saveProperty(ConfigProperty property); + + /** + * Returns a {@link List} containing all stored {@linkplain ConfigProperty ConfigProperties}. + * @return The list with the properties. + */ + public List getProperties(); + + /** + * Returns the value for the configuration property associated with {@code key} or {@code null} if the entry does not exist or its value is {@code null}. + * + * @param key The configuration key. + * @return The configuration property value or {@code null}. + */ + public String getPropertyValue(String key); + + /** + * Persists a {@link List} of {@linkplain ConfigProperty ConfigProperties}. + * @param properties The list containing all the properties to be persisted. + */ + public void saveProperties(Set 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 new file mode 100644 index 000000000..e1e0a836c --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/dal/ConfigPropertyDaoImpl.java @@ -0,0 +1,88 @@ +package com.datentechnik.moa.id.conf.persistence.dal; + +import java.util.List; +import java.util.Set; + +import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.transaction.annotation.Transactional; + +/** + * Database backed implementation of the DAO interface + * + */ + +@Transactional("transactionManager") +public class ConfigPropertyDaoImpl implements ConfigPropertyDao { + + private Logger log = LoggerFactory.getLogger(getClass()); + + @PersistenceContext(unitName = "moaidconf") + private EntityManager em; + + @Override + public void saveProperty(ConfigProperty property) { + if (null == em) { + log.error("No EntityManager set!"); + return; + } + + if (em.find(ConfigProperty.class, property.getKey()) != null) { + log.trace("Property '{}' already exists!", property.toString()); + // TODO: think about merging + // em.merge(property); + } else { + log.debug("Storing '{}'.", property.toString()); + em.persist(property); + } + } + + @Override + public ConfigProperty getProperty(String key) { + log.debug("Looking for configuration property for key '{}'.", key); + ConfigProperty result = em.find(ConfigProperty.class, key); + if (result != null) { + log.debug("Found configuration property {}.", result); + } else { + log.debug("Unable to find configuration property for key '{}'.", key); + } + return result; + } + + @Override + public String getPropertyValue(String key) { + ConfigProperty property = getProperty(key); + if (property == null) { + return null; + } + return property.getValue(); + } + + @Override + public List getProperties() { + log.debug("Retrieving all properties from database."); + TypedQuery query = em.createQuery("select mc from ConfigProperty mc", ConfigProperty.class); + try { + List questionerVoterList = query.getResultList(); + return questionerVoterList; + } catch (NoResultException e) { + log.debug("No property found in database."); + return null; + } + } + + @Override + public void saveProperties(Set properties) { + log.debug("Storing {} properties to database.", properties.size()); + for (ConfigProperty cp : properties) { + saveProperty(cp); + } + em.flush(); + } + +} -- cgit v1.2.3