diff options
Diffstat (limited to 'id/server')
8 files changed, 122 insertions, 50 deletions
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 a313107ad..6110f41ea 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 @@ -33,10 +33,16 @@ import com.fasterxml.jackson.core.JsonProcessingException; */ 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 @@ -52,12 +58,6 @@ public class MigrateConfiguration { // input from file and output to a database is desired readFromFileWriteToDB(inStream, parsedParameters.getOutputDBConfig()); } - } 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); } } else if (parsedParameters.getInputDBConfig() != null) { @@ -79,6 +79,12 @@ public class MigrateConfiguration { } 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); } } @@ -131,7 +137,7 @@ public class MigrateConfiguration { Properties result = new Properties(); boolean prettyPrint = true; - com.datentechnik.moa.id.conf.persistence.JsonMapper mapper = new JsonMapper(prettyPrint); + JsonMapper mapper = new JsonMapper(prettyPrint); // serialize config to JSON String oaJson = mapper.serialize(config.getOnlineApplication()); @@ -195,6 +201,9 @@ public class MigrateConfiguration { // write to output stream result.store(outStream, null); + System.out.println("Old XML configuration written to:"); + System.out.println(outFile.getAbsolutePath()); + } catch (FileNotFoundException e) { System.out.println("Could not find the output file."); System.exit(1); @@ -237,7 +246,13 @@ public class MigrateConfiguration { Object value = mapper.deserialize(json, null); // add to database - dbConfiguration.set(key, value); + 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."); } } diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/CLIConstants.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/CLIConstants.java index b5e0f242f..481b6d6f6 100644 --- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/CLIConstants.java +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/CLIConstants.java @@ -27,9 +27,11 @@ public class CLIConstants { public static final String CLI_PARAM_INDB_LONG = "input-dbconf"; public static final String CLI_PARAM_OUTDB = "outdb"; public static final String CLI_PARAM_OUTDB_LONG = "output-dbconf"; - + public static final String CLI_PARAM_HELP = "h"; public static final String CLI_PARAM_HELP_LONG = "help"; - - + + public static final String CLI_PARAM_FORCE = "f"; + public static final String CLI_PARAM_FORCE_LONG = "force"; + } diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MOAIDConfCLI.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MOAIDConfCLI.java index 78b9450ce..ac5ead171 100644 --- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MOAIDConfCLI.java +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MOAIDConfCLI.java @@ -71,8 +71,10 @@ public class MOAIDConfCLI { HelpFormatter formatter = new HelpFormatter(); pOut.println(); - pOut.println("usage: " + CLIConstants.CMD_LINE_SYNTAX + " -" + CLIConstants.CLI_PARAM_IN + " <inputfile> | -" + CLIConstants.CLI_PARAM_INDB + " <dbconfig> -" + CLIConstants.CLI_PARAM_OUT - + " <outputfile> | -" + CLIConstants.CLI_PARAM_OUTDB + " <dbconfig> [-" + CLIConstants.CLI_PARAM_HELP + "]"); + pOut.println("usage: " + CLIConstants.CMD_LINE_SYNTAX + " -" + CLIConstants.CLI_PARAM_FORCE + " -" + + CLIConstants.CLI_PARAM_IN + " <inputfile> | -" + CLIConstants.CLI_PARAM_INDB + " <dbconfig> -" + + CLIConstants.CLI_PARAM_OUT + " <outputfile> | -" + CLIConstants.CLI_PARAM_OUTDB + " <dbconfig> [-" + + CLIConstants.CLI_PARAM_HELP + "]"); pOut.println(); pOut.println(CLIConstants.HELP_HEADER); if(printOptions){ @@ -100,7 +102,6 @@ public class MOAIDConfCLI { inGroup.addOption(optionDBInput); inGroup.addOption(optionInput); optionInput.setRequired(false); - OptionGroup outGroup = new OptionGroup(); Option optionOutput = new Option(CLIConstants.CLI_PARAM_OUT, CLIConstants.CLI_PARAM_OUT_LONG, true, "target file to write to"); @@ -115,6 +116,9 @@ public class MOAIDConfCLI { options.addOptionGroup(inGroup); options.addOptionGroup(outGroup); + Option optForce = new Option(CLIConstants.CLI_PARAM_FORCE, CLIConstants.CLI_PARAM_FORCE_LONG, false, "overwrite existing data with imported data"); + options.addOption(optForce); + Option optHelp = new Option(CLIConstants.CLI_PARAM_HELP, CLIConstants.CLI_PARAM_HELP_LONG, false, "prints this message"); options.addOption(optHelp); return options; diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MigrateConfigurationParams.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MigrateConfigurationParams.java index 0a13d952b..da2cac31b 100644 --- a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MigrateConfigurationParams.java +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/cli/MigrateConfigurationParams.java @@ -15,6 +15,8 @@ public class MigrateConfigurationParams { private String inputDbConfigFile = null; private String outputDbConfigFile = null; + private boolean overwriteData = false; + /** * Get the path to the input source which is MOAID 2.x config file in XML-format. * @return the path to the input source or {@code null} if not set. @@ -64,6 +66,14 @@ public class MigrateConfigurationParams { } /** + * Returns whether existing data should be overwritten by the imported data or not. + * @return <code>true</code> if the existing data should be overwritten; <code>false</code> otherwise. + */ + public boolean isOverwriteData() { + return overwriteData; + } + + /** * * @param cmdLine * @throws MissingOptionException @@ -73,6 +83,7 @@ public class MigrateConfigurationParams { inputDbConfigFile = cmdLine.getOptionValue(CLIConstants.CLI_PARAM_INDB); outputFile = cmdLine.getOptionValue(CLIConstants.CLI_PARAM_OUT); outputDbConfigFile = cmdLine.getOptionValue(CLIConstants.CLI_PARAM_OUTDB); + overwriteData = cmdLine.hasOption(CLIConstants.CLI_PARAM_FORCE); if (null == inputFile && null == inputDbConfigFile) { throw new MissingOptionException("One of [-" + CLIConstants.CLI_PARAM_IN + ", -" + CLIConstants.CLI_PARAM_INDB + "] required."); 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); - } } } |