diff options
author | Christian Wagner <c.wagner@datentechnik-innovation.com> | 2015-01-13 10:23:25 +0100 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-06-19 11:06:48 +0200 |
commit | cb6daf2e54d72a4a0bdca0252d96191440cd5f52 (patch) | |
tree | a3e95fef0a36866ad3ec4190d5a4e00222545f0c /id/server/moa-id-commons/src | |
parent | 70721aa00fa8eea76e9b632e7f17dd4424d2ad4e (diff) | |
download | moa-id-spss-cb6daf2e54d72a4a0bdca0252d96191440cd5f52.tar.gz moa-id-spss-cb6daf2e54d72a4a0bdca0252d96191440cd5f52.tar.bz2 moa-id-spss-cb6daf2e54d72a4a0bdca0252d96191440cd5f52.zip |
move 'persistence' package from project 'moa-id-DTI' here
Diffstat (limited to 'id/server/moa-id-commons/src')
3 files changed, 228 insertions, 0 deletions
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<ConfigProperty> 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<ConfigProperty> 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<ConfigProperty> getProperties() { + log.debug("Retrieving all properties from database."); + TypedQuery<ConfigProperty> query = em.createQuery("select mc from ConfigProperty mc", ConfigProperty.class); + try { + List<ConfigProperty> questionerVoterList = query.getResultList(); + return questionerVoterList; + } catch (NoResultException e) { + log.debug("No property found in database."); + return null; + } + } + + @Override + public void saveProperties(Set<ConfigProperty> properties) { + log.debug("Storing {} properties to database.", properties.size()); + for (ConfigProperty cp : properties) { + saveProperty(cp); + } + em.flush(); + } + +} |