diff options
author | Gerwin Gsenger <g.gsenger@datentechnik-innovation.at> | 2015-01-26 11:26:12 +0100 |
---|---|---|
committer | Thomas Lenz <tlenz@iaik.tugraz.at> | 2015-06-19 11:09:24 +0200 |
commit | c9518b7d9772240b0d840f9175f8e576a5f6d3f6 (patch) | |
tree | 7db9e2d5a9a712459236469e40b2b7a6215d3114 /id/server/moa-id-commons/src | |
parent | caa4a3f833b2846ffc97b27b4fcc98dd74cdd51c (diff) | |
download | moa-id-spss-c9518b7d9772240b0d840f9175f8e576a5f6d3f6.tar.gz moa-id-spss-c9518b7d9772240b0d840f9175f8e576a5f6d3f6.tar.bz2 moa-id-spss-c9518b7d9772240b0d840f9175f8e576a5f6d3f6.zip |
rework behaviour of force switch, add getAllKeys to configuration
Diffstat (limited to 'id/server/moa-id-commons/src')
6 files changed, 324 insertions, 250 deletions
diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/ConfigurationUtil.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/ConfigurationUtil.java new file mode 100644 index 000000000..e771b96a2 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/ConfigurationUtil.java @@ -0,0 +1,227 @@ +package com.datentechnik.moa.id.conf; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Enumeration; +import java.util.List; +import java.util.Properties; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import at.gv.egovernment.moa.id.commons.db.MOAIDConfigurationConstants; +import at.gv.egovernment.moa.id.commons.db.dao.config.MOAIDConfiguration; + +import com.datentechnik.moa.id.conf.persistence.Configuration; +import com.datentechnik.moa.id.conf.persistence.JsonMapper; +import com.fasterxml.jackson.core.JsonProcessingException; + +public class ConfigurationUtil { + + final boolean isOverwriteData; + + public ConfigurationUtil(boolean isOverwriteData){ + this.isOverwriteData = isOverwriteData; + } + + /** + * Read an input MOAID 2 XML file, transfer it to properties and write the + * properties to a MOAID 3 property file. + * + * @param inStream + * the input stream to read from. + * @param outFile + * the output file to write to. + * @throws JAXBException + */ + public void readFromXMLFileConvertToPropertyFile(FileInputStream inStream, File outFile) throws JAXBException { + + try (FileOutputStream outStream = new FileOutputStream(outFile);) { + + // get config from xml file + JAXBContext jc = JAXBContext.newInstance("at.gv.egovernment.moa.id.commons.db.dao.config"); + Unmarshaller m = jc.createUnmarshaller(); + MOAIDConfiguration config = (MOAIDConfiguration) m.unmarshal(inStream); + + // serialize config to JSON properties + Properties result = moaIdConfigToJsonProperties(config); + + // write to output stream + result.store(outStream, null); + + } catch (FileNotFoundException e) { + System.out.println("Could not find the output file."); + System.exit(1); + } catch (IOException e) { + System.out.println("Could not write to the output file."); + System.exit(1); + } + } + + /** + * Helper method to serialize a {@link MOAIDConfiguration} to Properties + * with JSON encoded values. + * + * @param config + * the MOAIDConfiguration to serialize + * @return {@link Properties} containing the database key and the serialized + * values + * @throws JsonProcessingException + * is thrown if problem occurred while serializing one of the + * database values + */ + private Properties moaIdConfigToJsonProperties(MOAIDConfiguration config) throws JsonProcessingException { + + Properties result = new Properties(); + boolean prettyPrint = true; + JsonMapper mapper = new JsonMapper(prettyPrint); + + // serialize config to JSON + String oaJson = mapper.serialize(config.getOnlineApplication()); + String authCompGeneralJson = mapper.serialize(config.getAuthComponentGeneral()); + String chainingModeJson = mapper.serialize(config.getChainingModes()); + String defaultBKUJson = mapper.serialize(config.getDefaultBKUs()); + String genericConfigJson = mapper.serialize(config.getGenericConfiguration()); + String pvp2RefreshJson = mapper.serialize(config.getPvp2RefreshItem()); + String slRequestTemplatesJson = mapper.serialize(config.getSLRequestTemplates()); + String timestampJson = mapper.serialize(config.getTimestampItem()); + String trustedCaCertJson = mapper.serialize(config.getTrustedCACertificates()); + + // add to properties + result.put(MOAIDConfigurationConstants.ONLINE_APPLICATIONS_KEY, oaJson); + result.put(MOAIDConfigurationConstants.AUTH_COMPONENT_GENERAL_KEY, authCompGeneralJson); + result.put(MOAIDConfigurationConstants.CHAINING_MODES_KEY, chainingModeJson); + result.put(MOAIDConfigurationConstants.DEFAULT_BKUS_KEY, defaultBKUJson); + result.put(MOAIDConfigurationConstants.GENERIC_CONFIGURATION_KEY, genericConfigJson); + result.put(MOAIDConfigurationConstants.PVP2REFRESH_ITEM_KEY, pvp2RefreshJson); + result.put(MOAIDConfigurationConstants.SLREQUEST_TEMPLATES_KEY, slRequestTemplatesJson); + result.put(MOAIDConfigurationConstants.TIMESTAMP_ITEM_KEY, timestampJson); + result.put(MOAIDConfigurationConstants.TRUSTED_CERTIFICATES_KEY, trustedCaCertJson); + + return result; + } + + /** + * Exports a key-value database to a property file, where keys are the same + * as in the database, and the values are serialized JSON objects. + * + * @param inputDBConfigFilePath + * the path to the database properties, for the db the data is + * read from. + * @param outFile + * the destination file for the exported data. + */ + public void readFromDBWriteToFile(String inputDBConfigFilePath, File outFile) { + + try (FileOutputStream outStream = new FileOutputStream(outFile);) { + + Properties result = new Properties(); + + System.getProperties().setProperty("location", "file:" + inputDBConfigFilePath); + ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml"); + Configuration dbConfiguration = (Configuration) context.getBean("config"); + boolean prettyPrint = true; + com.datentechnik.moa.id.conf.persistence.JsonMapper mapper = new JsonMapper(prettyPrint); + + for (String key : MOAIDConfigurationConstants.getAllMOAIDConfigurationKeys()) { + + // extract database value + Object value = dbConfiguration.get(key); + + // serialize value to JSON + String json = mapper.serialize(value); + + // add to properties + result.setProperty(key, json); + } + + // write to output stream + result.store(outStream, null); + + System.out.println("Property configuration written to:"); + System.out.println(outFile.getAbsolutePath()); + + } catch (FileNotFoundException e) { + System.out.println("Could not find the output file."); + System.exit(1); + } catch (IOException e) { + System.out.println("Could not write to the output file."); + System.exit(1); + } + } + + /** + * Read an input property file, deserialize it's values and write them to + * the given database. + * + * @param inStream + * the FileInputStream to read from. + * @param outputDBConfigFilePath + * the path to the database properties, for the db which is + * written. + * @throws IOException + * is thrown in case the properties could not be loaded from the + * stream + */ + public void readFromFileWriteToDB(FileInputStream inStream, String outputDBConfigFilePath) throws IOException { + + Properties inProperties = new Properties(); + inProperties.load(inStream); + + System.getProperties().setProperty("location", "file:" + outputDBConfigFilePath); + ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml"); + Configuration dbConfiguration = (Configuration) context.getBean("config"); + boolean prettyPrint = true; + JsonMapper mapper = new JsonMapper(prettyPrint); + + List<String> keys = dbConfiguration.getAllKeys(); + + if (keys == null) { + System.out.println("Database can not be read."); + System.exit(1); + } + + if (!keys.isEmpty() && !isOverwriteData) { + System.out.println("The database already contains configuration data."); + System.out.println("Use force switch if you want to override data)"); + System.exit(1); + } + + if (isOverwriteData) { + // remove existing entries + for (String key : keys) { + dbConfiguration.set(key, null); + } + } + + Enumeration<?> propertyNames = inProperties.propertyNames(); + + while (propertyNames.hasMoreElements()) { + String key = (String) propertyNames.nextElement(); + // extract database value + String json = inProperties.getProperty(key); + + // deserialize value to object + Object value = mapper.deserialize(json, null); + + // add to database + boolean result = dbConfiguration.set(key, value); + if (!result) { + System.out.println("Could NOT persist the configuration file's information in the database."); + } + } + System.out.println("Data has been successfully written to the database."); + } + + private static void readFromDBWriteToDB(String inputDBConfigFilePath, String outputDBConfigFilePath) { + //TODO: implement + } + +} diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java index 6110f41ea..fefcf5028 100644 --- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/MigrateConfiguration.java @@ -3,260 +3,101 @@ package com.datentechnik.moa.id.conf; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -import java.util.Properties; -import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import at.gv.egovernment.moa.id.commons.db.MOAIDConfigurationConstants; -import at.gv.egovernment.moa.id.commons.db.dao.config.MOAIDConfiguration; import com.datentechnik.moa.id.conf.cli.MOAIDConfCLI; import com.datentechnik.moa.id.conf.cli.MigrateConfigurationParams; -import com.datentechnik.moa.id.conf.persistence.Configuration; -import com.datentechnik.moa.id.conf.persistence.JsonMapper; -import com.fasterxml.jackson.core.JsonProcessingException; /** * CLI tool which is able to perform the following tasks: * <ul> - * <li>transform a MoaID 2 XML configuration XML file to a MoaID 3 property file</li> + * <li>transform a MoaID 2 XML configuration XML file to a MoaID 3 property file + * </li> * <li>read a property file and transfer it's content to a database</li> * <li>write the content of a database to a property file</li> * </ul> */ public class MigrateConfiguration { - public static boolean isOverwriteData = false; - public static void main(String[] args) { MOAIDConfCLI cli = new MOAIDConfCLI(); MigrateConfigurationParams parsedParameters = cli.parse(args); // consider settings of force switch - isOverwriteData = parsedParameters.isOverwriteData(); - - try { - if (!parsedParameters.isInputDB() && (parsedParameters.getInputTarget() != null)) { - // read input from file - File inFile = new File(parsedParameters.getInputTarget()); - try (FileInputStream inStream = new FileInputStream(inFile);) { - - if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { - // input from file and output to a file is desired - File outFile = new File(parsedParameters.getOutputFile()); - readFromFileWriteToFile(inStream, outFile); - - } else if (parsedParameters.getOutputDBConfig() != null) { - // input from file and output to a database is desired - readFromFileWriteToDB(inStream, parsedParameters.getOutputDBConfig()); - } - } - - } else if (parsedParameters.getInputDBConfig() != null) { - // read input from database - if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { - // input from database and output to a file is desired - File outFile = new File(parsedParameters.getOutputFile()); - String inputDBConfigFilePath = parsedParameters.getInputDBConfig(); - readFromDBWriteToFile(inputDBConfigFilePath, outFile); - - } else if (parsedParameters.getOutputDBConfig() != null) { - // input from database and output to a database is desired - // readFromDBWriteToDB(inDBConfigFilePath, outDBConfigFilePath); - } - - } else { - System.exit(1); - } - } catch (JAXBException e) { - System.out.println("MOA-ID XML configuration can not be loaded from given file."); - System.exit(1); - } catch (FileNotFoundException e) { - System.out.println("Could not find the input file."); - System.exit(1); - } catch (IOException e) { - System.out.println("Could not read from the input file."); - System.exit(1); - } - } - - /** - * Read an input XML file, transfer it to properties and write the properties to a file. - * - * @param inStream - * the input stream to read from. - * @param outFile - * the output file to write to. - * @throws JAXBException - */ - private static void readFromFileWriteToFile(FileInputStream inStream, File outFile) throws JAXBException { - - try (FileOutputStream outStream = new FileOutputStream(outFile);) { + boolean isOverwriteData = parsedParameters.isOverwriteData(); + ConfigurationUtil configUtil = new ConfigurationUtil(isOverwriteData); - // get config from xml file - JAXBContext jc = JAXBContext.newInstance("at.gv.egovernment.moa.id.commons.db.dao.config"); - Unmarshaller m = jc.createUnmarshaller(); - MOAIDConfiguration config = (MOAIDConfiguration) m.unmarshal(inStream); + if (!parsedParameters.isInputDB() && (parsedParameters.getInputTarget() != null)) { + // read input from file + workWithInputFromFile(parsedParameters.getInputTarget(), parsedParameters, configUtil); - // serialize config to JSON properties - Properties result = moaIdConfigToJsonProperties(config); + } else if (parsedParameters.getInputDBConfig() != null) { + // read input from database + workWithImputFromDB(parsedParameters, configUtil); - // write to output stream - result.store(outStream, null); - - } catch (FileNotFoundException e) { - System.out.println("Could not find the output file."); - System.exit(1); - } catch (IOException e) { - System.out.println("Could not write to the output file."); + } else { System.exit(1); } } /** - * Helper method to serialize a {@link MOAIDConfiguration} to Properties - * with JSON encoded values. + * Handle the case where input from a file is read. * - * @param config - * the MOAIDConfiguration to serialize - * @return {@link Properties} containing the database key and the serialized - * values - * @throws JsonProcessingException - * is thrown if problem occurred while serializing one of the - * database values + * @param inputFileUrl + * the url of the input file. + * @param parsedParameters + * the command line parameters. + * @param configUtil + * the class for working with the configuration. */ - private static Properties moaIdConfigToJsonProperties(MOAIDConfiguration config) throws JsonProcessingException { - - Properties result = new Properties(); - boolean prettyPrint = true; - JsonMapper mapper = new JsonMapper(prettyPrint); - - // serialize config to JSON - String oaJson = mapper.serialize(config.getOnlineApplication()); - String authCompGeneralJson = mapper.serialize(config.getAuthComponentGeneral()); - String chainingModeJson = mapper.serialize(config.getChainingModes()); - String defaultBKUJson = mapper.serialize(config.getDefaultBKUs()); - String genericConfigJson = mapper.serialize(config.getGenericConfiguration()); - String pvp2RefreshJson = mapper.serialize(config.getPvp2RefreshItem()); - String slRequestTemplatesJson = mapper.serialize(config.getSLRequestTemplates()); - String timestampJson = mapper.serialize(config.getTimestampItem()); - String trustedCaCertJson = mapper.serialize(config.getTrustedCACertificates()); - - // add to properties - result.put(MOAIDConfigurationConstants.ONLINE_APPLICATIONS_KEY, oaJson); - result.put(MOAIDConfigurationConstants.AUTH_COMPONENT_GENERAL_KEY, authCompGeneralJson); - result.put(MOAIDConfigurationConstants.CHAINING_MODES_KEY, chainingModeJson); - result.put(MOAIDConfigurationConstants.DEFAULT_BKUS_KEY, defaultBKUJson); - result.put(MOAIDConfigurationConstants.GENERIC_CONFIGURATION_KEY, genericConfigJson); - result.put(MOAIDConfigurationConstants.PVP2REFRESH_ITEM_KEY, pvp2RefreshJson); - result.put(MOAIDConfigurationConstants.SLREQUEST_TEMPLATES_KEY, slRequestTemplatesJson); - result.put(MOAIDConfigurationConstants.TIMESTAMP_ITEM_KEY, timestampJson); - result.put(MOAIDConfigurationConstants.TRUSTED_CERTIFICATES_KEY, trustedCaCertJson); - - return result; - } - - /** - * Exports a key-value database to a property file, where keys are the same - * as in the database, and the values are serialized JSON objects. - * - * @param inputDBConfigFilePath - * the path to the database properties, for the db the data is - * read from. - * @param outFile - * the destination file for the exported data. - */ - private static void readFromDBWriteToFile(String inputDBConfigFilePath, File outFile) { - - try (FileOutputStream outStream = new FileOutputStream(outFile);) { - - Properties result = new Properties(); - - System.getProperties().setProperty("location", "file:" + inputDBConfigFilePath); - ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml"); - Configuration dbConfiguration = (Configuration) context.getBean("config"); - boolean prettyPrint = true; - com.datentechnik.moa.id.conf.persistence.JsonMapper mapper = new JsonMapper(prettyPrint); - - for (String key : MOAIDConfigurationConstants.getAllMOAIDConfigurationKeys()) { - - // extract database value - Object value = dbConfiguration.get(key); - - // serialize value to JSON - String json = mapper.serialize(value); - - // add to properties - result.setProperty(key, json); + private static void workWithInputFromFile(String inputFileUrl, MigrateConfigurationParams parsedParameters, + ConfigurationUtil configUtil) { + File inFile = new File(inputFileUrl); + try (FileInputStream inStream = new FileInputStream(inFile);) { + + if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { + // input from file and output to a file is desired + File outFile = new File(parsedParameters.getOutputFile()); + configUtil.readFromXMLFileConvertToPropertyFile(inStream, outFile); + + } else if (parsedParameters.getOutputDBConfig() != null) { + // input from file and output to a database is desired + configUtil.readFromFileWriteToDB(inStream, parsedParameters.getOutputDBConfig()); } - - // write to output stream - result.store(outStream, null); - - System.out.println("Old XML configuration written to:"); - System.out.println(outFile.getAbsolutePath()); - + } catch (JAXBException e) { + System.out.println("MOA-ID XML configuration can not be loaded from given file."); + System.exit(1); } catch (FileNotFoundException e) { - System.out.println("Could not find the output file."); + System.out.println("Could not find the input file."); System.exit(1); } catch (IOException e) { - System.out.println("Could not write to the output file."); + System.out.println("Could not read from the input file."); System.exit(1); } } /** - * Read an input property file, deserialize it's values and write them to - * the given database. + * Handle the case where input is read from a database. * - * @param inStream - * the FileInputStream to read from. - * @param outputDBConfigFilePath - * the path to the database properties, for the db which is - * written. - * @throws IOException - * is thrown in case the properties could not be loaded from the - * stream + * @param parsedParameters + * the command line parameters. + * @param configUtil + * the class for working with the configuration. */ - private static void readFromFileWriteToDB(FileInputStream inStream, String outputDBConfigFilePath) throws IOException { - - Properties inProperties = new Properties(); - inProperties.load(inStream); - - System.getProperties().setProperty("location", "file:" + outputDBConfigFilePath); - ApplicationContext context = new ClassPathXmlApplicationContext("configuration.beans.xml"); - Configuration dbConfiguration = (Configuration) context.getBean("config"); - boolean prettyPrint = true; - com.datentechnik.moa.id.conf.persistence.JsonMapper mapper = new JsonMapper(prettyPrint); - - for (String key : MOAIDConfigurationConstants.getAllMOAIDConfigurationKeys()) { - - // extract database value - String json = inProperties.getProperty(key); - - // deserialize value to object - Object value = mapper.deserialize(json, null); - - // add to database - boolean result = dbConfiguration.set(key, value, isOverwriteData); - if (!result) { - System.out.println("Could NOT persist the configuration file's information in the database."); - System.out.println("The database already contains a configuration (see force switch if you want to override data.)"); - System.exit(1); - } - System.out.println("Data has been successfully written to the database."); + private static void workWithImputFromDB(MigrateConfigurationParams parsedParameters, ConfigurationUtil configUtil) { + if (!parsedParameters.isOutputDB() && (parsedParameters.getOutputFile() != null)) { + // input from database and output to a file is desired + File outFile = new File(parsedParameters.getOutputFile()); + String inputDBConfigFilePath = parsedParameters.getInputDBConfig(); + configUtil.readFromDBWriteToFile(inputDBConfigFilePath, outFile); + + } else if (parsedParameters.getOutputDBConfig() != null) { + // input from database and output to a database is desired + // configUtil.readFromDBWriteToDB(inDBConfigFilePath, + // outDBConfigFilePath); } } - - private static void readFromDBWriteToDB(String inputDBConfigFilePath, String outputDBConfigFilePath) { - //TODO: implement - } }
\ No newline at end of file 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 bc90208b6..406c21026 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 @@ -8,6 +8,12 @@ import java.util.List; public interface Configuration { /** + * Gets all keys in the database. NOTE: may return an empty list or {@code null}. + * @return a List containing all keys in the database or {@code null}. + */ + List<String> getAllKeys(); + + /** * Get the value associated with the given key as {@link Object}. * @param key the key * @return the object associated with the given key or {@code null} if the key does not exist or does not have a value. @@ -33,16 +39,6 @@ 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 297a1db4c..43974de7a 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 @@ -4,6 +4,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import javax.persistence.EntityExistsException; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Required; @@ -39,6 +41,16 @@ public class ConfigurationImpl implements Configuration { } @Override + public List<String> getAllKeys(){ + try { + return this.configPropertyDao.getAllKeys(); + } catch (Exception e) { + log.debug("Error while retrieving a list of all keys in the database."); + return null; + } + } + + @Override public Object get(String key) { // return null if key does not exist try { @@ -78,11 +90,6 @@ public class ConfigurationImpl implements Configuration { @Override public boolean set(String key, Object value) { - return this.set(key, value, false); - } - - @Override - public boolean set(String key, Object value, boolean overwrite) { try { if (value == null) { @@ -94,13 +101,16 @@ public class ConfigurationImpl implements Configuration { keyValue.setKey(key); keyValue.setValue(mapper.serialize(value)); - configPropertyDao.saveProperty(keyValue, overwrite); + configPropertyDao.saveProperty(keyValue); return true; } } catch (JsonProcessingException e) { log.debug("Error while serializing object for key '{}'.", key); return false; - } catch (Exception e){ + } 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; } 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 bfc3bc6cd..8f00bd226 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 @@ -10,31 +10,30 @@ import java.util.Set; public interface ConfigPropertyDao { /** + * Gets all keys in the database. + * @return a List containing all keys in the database. + */ + List<String> getAllKeys(); + + /** * 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); + ConfigProperty getProperty(String key); /** * 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); + void saveProperty(ConfigProperty property); /** * Returns a {@link List} containing all stored {@linkplain ConfigProperty ConfigProperties}. * @return The list with the properties. */ - public List<ConfigProperty> getProperties(); + 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}. @@ -42,18 +41,18 @@ public interface ConfigPropertyDao { * @param key The configuration key. * @return The configuration property value or {@code null}. */ - public String getPropertyValue(String key); + 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); + void saveProperties(Set<ConfigProperty> properties); /** * Deletes the object associated with the given key. * @param key the key */ - public void delete(String key); + void delete(String key); } 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 dfb1f542f..6de10e9b9 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 @@ -25,24 +25,25 @@ public class ConfigPropertyDaoImpl implements ConfigPropertyDao { private EntityManager em; @Override - public void saveProperty(ConfigProperty property) { - this.saveProperty(property, false); + public List<String> getAllKeys() { + if (null == em) { + log.error("No EntityManager set!"); + return null; + } + TypedQuery<String> query = em.createQuery("select key from ConfigProperty", String.class); + List<String> result = query.getResultList(); + return result; } @Override - public void saveProperty(ConfigProperty property, boolean overwrite) { + public void saveProperty(ConfigProperty property) { if (null == em) { log.error("No EntityManager set!"); return; } log.debug("Storing '{}'.", property.toString()); - if (overwrite) { - em.merge(property); - } else { - log.debug("Storing '{}'.", property.toString()); - em.persist(property); - } + em.persist(property); } @Override |