From 020df07995cc50943362479872ee186c7dd4fb63 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Wed, 26 Apr 2017 08:28:48 +0200 Subject: [PATCH] Added database status check in methods. --- .../sikevadb/filetree/FileTreeSikevaDB.java | 990 +++++---- src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java | 1963 +++++++++-------- .../filetree/TreeFileSikevaDBTest.java | 3 +- 3 files changed, 1667 insertions(+), 1289 deletions(-) diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java index 4e2af21..e04ff8b 100644 --- a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java @@ -84,7 +84,7 @@ public class FileTreeSikevaDB implements SikevaDB } else if (StringUtils.equals(homeDirectory.getAbsolutePath(), "/")) { - throw new SikevaDBException("Invalide home directory (file system root)."); + throw new SikevaDBException("Invalid home directory (file system root)."); } else { @@ -115,17 +115,24 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void clear() throws SikevaDBException { - try + if (isOpened()) { - // TODO journalize("clear database"); + try + { + // TODO journalize("clear database"); - FileUtils.deleteDirectory(this.dataDirectory); - this.dataDirectory.mkdir(); + FileUtils.deleteDirectory(this.dataDirectory); + this.dataDirectory.mkdir(); + } + catch (IOException exception) + { + this.logger.error("Error clearing database", exception); + throw new SikevaDBException("Error clearing database", exception); + } } - catch (IOException exception) + else { - this.logger.error("Error clearing database", exception); - throw new SikevaDBException("Error clearing database", exception); + throw new SikevaDBException("Invalid database status (closed)."); } } @@ -134,11 +141,18 @@ public class FileTreeSikevaDB implements SikevaDB * */ @Override - public void close() + public void close() throws SikevaDBException { - this.archiver.close(); + if (isOpened()) + { + this.archiver.close(); - this.status = Status.CLOSED; + this.status = Status.CLOSED; + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } } /** @@ -146,25 +160,33 @@ public class FileTreeSikevaDB implements SikevaDB * */ @Override - public long countOfElements() + public long countOfElements() throws SikevaDBException { long result; - File[] topFiles = this.dataDirectory.listFiles(); - - result = 0; - for (File file : topFiles) + if (isOpened()) { - if (file.isDirectory()) + + File[] topFiles = this.dataDirectory.listFiles(); + + result = 0; + for (File file : topFiles) { - File[] subFiles = file.listFiles(); - result += subFiles.length; - } - else - { - result += 1; + if (file.isDirectory()) + { + File[] subFiles = file.listFiles(); + result += subFiles.length; + } + else + { + result += 1; + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -173,30 +195,39 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} * + * @throws SikevaDBException + * */ @Override - public long countOfElements(final String key) + public long countOfElements(final String key) throws SikevaDBException { long result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); - } - else - { - File targetDirectory = new File(this.dataDirectory, key); - File[] subFiles = targetDirectory.listFiles(); - - if (subFiles == null) + if (key == null) { - result = 1; + throw new IllegalArgumentException("Null key detected."); } else { - result = subFiles.length; + File targetDirectory = new File(this.dataDirectory, key); + File[] subFiles = targetDirectory.listFiles(); + + if (subFiles == null) + { + result = 1; + } + else + { + result = subFiles.length; + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -222,27 +253,34 @@ public class FileTreeSikevaDB implements SikevaDB * */ @Override - public long countOfElements(final String key, final String subkey) + public long countOfElements(final String key, final String subkey) throws SikevaDBException { long result; - if ((key == null) || (subkey == null)) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); - } - else - { - File targetDirectory = new File(this.dataDirectory, key); - File targetFile = new File(targetDirectory, subkey); - if (targetFile.exists()) + if ((key == null) || (subkey == null)) { - result = 1; + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - result = 0; + File targetDirectory = new File(this.dataDirectory, key); + File targetFile = new File(targetDirectory, subkey); + if (targetFile.exists()) + { + result = 1; + } + else + { + result = 0; + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -257,7 +295,7 @@ public class FileTreeSikevaDB implements SikevaDB { if (this.status == Status.OPENED) { - throw new SikevaDBException("Invalid state."); + throw new SikevaDBException("Invalid database status (opened)."); } else if (this.homeDirectory == null) { @@ -282,7 +320,11 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void destroy() throws SikevaDBException { - if (this.homeDirectory == null) + if (isOpened()) + { + throw new SikevaDBException("Invalid database status (opened)."); + } + else if (this.homeDirectory == null) { throw new SikevaDBException("Invalid home directory (undefined), destroy operation is cancelled."); } @@ -364,19 +406,26 @@ public class FileTreeSikevaDB implements SikevaDB { Element result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + File targetFile = new File(this.dataDirectory, key); + result = FileTreeSikevaDBTools.loadElement(targetFile); + if (result != null) + { + result.setKey(key); + result.setSubkey(null); + } + } } else { - File targetFile = new File(this.dataDirectory, key); - result = FileTreeSikevaDBTools.loadElement(targetFile); - if (result != null) - { - result.setKey(key); - result.setSubkey(null); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -407,23 +456,29 @@ public class FileTreeSikevaDB implements SikevaDB { Element result; - // - if ((key == null) || (subkey == null)) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); + if ((key == null) || (subkey == null)) + { + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); + } + else + { + // + File targetDirectory = new File(this.dataDirectory, key); + File targetFile = new File(targetDirectory, subkey); + + result = FileTreeSikevaDBTools.loadElement(targetFile); + if (result != null) + { + result.setKey(key); + result.setSubkey(subkey); + } + } } else { - // - File targetDirectory = new File(this.dataDirectory, key); - File targetFile = new File(targetDirectory, subkey); - - result = FileTreeSikevaDBTools.loadElement(targetFile); - if (result != null) - { - result.setKey(key); - result.setSubkey(subkey); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -438,32 +493,39 @@ public class FileTreeSikevaDB implements SikevaDB { Elements result; - result = new Elements((int) countOfElements()); - - File[] topFiles = this.dataDirectory.listFiles(); - for (File file : topFiles) + if (isOpened()) { - if (file.isFile()) - { - Element element = getElement(file.getName()); + result = new Elements((int) countOfElements()); - result.add(element); - } - else if (file.isDirectory()) + File[] topFiles = this.dataDirectory.listFiles(); + for (File file : topFiles) { - File[] subFiles = file.listFiles(); - - for (File subFile : subFiles) + if (file.isFile()) { - if (subFile.isFile()) - { - Element element = getElement(file.getName(), subFile.getName()); + Element element = getElement(file.getName()); - result.add(element); + result.add(element); + } + else if (file.isDirectory()) + { + File[] subFiles = file.listFiles(); + + for (File subFile : subFiles) + { + if (subFile.isFile()) + { + Element element = getElement(file.getName(), subFile.getName()); + + result.add(element); + } } } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -478,25 +540,32 @@ public class FileTreeSikevaDB implements SikevaDB { Elements result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + result = new Elements((int) countOfElements(key)); + + File targetDirectory = new File(this.dataDirectory, key); + if (targetDirectory.exists()) + { + File[] files = targetDirectory.listFiles(); + for (File subfile : files) + { + Element element = getElement(key, subfile.getName()); + + result.add(element); + } + } + } } else { - result = new Elements((int) countOfElements(key)); - - File targetDirectory = new File(this.dataDirectory, key); - if (targetDirectory.exists()) - { - File[] files = targetDirectory.listFiles(); - for (File subfile : files) - { - Element element = getElement(key, subfile.getName()); - - result.add(element); - } - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -522,29 +591,35 @@ public class FileTreeSikevaDB implements SikevaDB { StringList result; - // - result = new StringList((int) countOfElements()); - - File[] topFiles = this.dataDirectory.listFiles(); - for (File file : topFiles) + if (isOpened()) { - if (file.isFile()) - { - result.add(file.getName()); - } - else if (file.isDirectory()) - { - File[] subFiles = file.listFiles(); + result = new StringList((int) countOfElements()); - for (File subFile : subFiles) + File[] topFiles = this.dataDirectory.listFiles(); + for (File file : topFiles) + { + if (file.isFile()) { - if (file.isFile()) + result.add(file.getName()); + } + else if (file.isDirectory()) + { + File[] subFiles = file.listFiles(); + + for (File subFile : subFiles) { - result.add(subFile.getName()); + if (file.isFile()) + { + result.add(subFile.getName()); + } } } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -578,24 +653,31 @@ public class FileTreeSikevaDB implements SikevaDB { StringList result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + // + result = new StringList(); + + File targetDirectory = new File(this.dataDirectory, key); + if (targetDirectory.exists()) + { + File[] subfiles = targetDirectory.listFiles(); + for (File subfile : subfiles) + { + result.add(subfile.getName()); + } + } + } } else { - // - result = new StringList(); - - File targetDirectory = new File(this.dataDirectory, key); - if (targetDirectory.exists()) - { - File[] subfiles = targetDirectory.listFiles(); - for (File subfile : subfiles) - { - result.add(subfile.getName()); - } - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -612,24 +694,31 @@ public class FileTreeSikevaDB implements SikevaDB { String result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); - } - else - { - File targetFile = new File(this.dataDirectory, key); - if (targetFile.exists()) + if (key == null) { - Element element = FileTreeSikevaDBTools.loadElement(targetFile); - - result = element.getValue(); + throw new IllegalArgumentException("Null key detected."); } else { - result = null; + File targetFile = new File(this.dataDirectory, key); + if (targetFile.exists()) + { + Element element = FileTreeSikevaDBTools.loadElement(targetFile); + + result = element.getValue(); + } + else + { + result = null; + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -660,25 +749,32 @@ public class FileTreeSikevaDB implements SikevaDB { String result; - if ((key == null) || (subkey == null)) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); - } - else - { - File targetDirectory = new File(this.dataDirectory, key); - File targetFile = new File(targetDirectory, subkey); - if (targetFile.exists()) + if ((key == null) || (subkey == null)) { - Element element = FileTreeSikevaDBTools.loadElement(targetFile); - - result = element.getValue(); + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - result = null; + File targetDirectory = new File(this.dataDirectory, key); + File targetFile = new File(targetDirectory, subkey); + if (targetFile.exists()) + { + Element element = FileTreeSikevaDBTools.loadElement(targetFile); + + result = element.getValue(); + } + else + { + result = null; + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -694,33 +790,40 @@ public class FileTreeSikevaDB implements SikevaDB { StringList result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); - } - else - { - // - result = new StringList((int) countOfElements(key)); - - // - File targetDirectory = new File(this.dataDirectory, key); - - if (targetDirectory.isDirectory()) + if (key == null) { - File[] subFiles = targetDirectory.listFiles(); + throw new IllegalArgumentException("Null key detected."); + } + else + { + // + result = new StringList((int) countOfElements(key)); - for (File subFile : subFiles) + // + File targetDirectory = new File(this.dataDirectory, key); + + if (targetDirectory.isDirectory()) { - if (subFile.isFile()) - { - Element element = FileTreeSikevaDBTools.loadElement(subFile); + File[] subFiles = targetDirectory.listFiles(); - result.add(element.getValue()); + for (File subFile : subFiles) + { + if (subFile.isFile()) + { + Element element = FileTreeSikevaDBTools.loadElement(subFile); + + result.add(element.getValue()); + } } } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -829,34 +932,41 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - // - result = 0; - - // - File[] topFiles = this.dataDirectory.listFiles(); - for (File file : topFiles) + if (isOpened()) { - if (file.isFile()) - { - Element element = FileTreeSikevaDBTools.loadElement(file); + // + result = 0; - result += element.getSize(); - } - else if (file.isDirectory()) + // + File[] topFiles = this.dataDirectory.listFiles(); + for (File file : topFiles) { - File[] subFiles = file.listFiles(); - - for (File subFile : subFiles) + if (file.isFile()) { - if (subFile.isFile()) - { - Element element = FileTreeSikevaDBTools.loadElement(subFile); + Element element = FileTreeSikevaDBTools.loadElement(file); - result += element.getSize(); + result += element.getSize(); + } + else if (file.isDirectory()) + { + File[] subFiles = file.listFiles(); + + for (File subFile : subFiles) + { + if (subFile.isFile()) + { + Element element = FileTreeSikevaDBTools.loadElement(subFile); + + result += element.getSize(); + } } } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -872,34 +982,41 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); - } - else - { - File targetFile = new File(this.dataDirectory, key); - if (targetFile.isFile()) + if (key == null) { - Element element = getElement(key); - - result = element.getSize(); - } - else if (targetFile.isDirectory()) - { - result = 0; - for (File subFile : targetFile.listFiles()) - { - Element element = getElement(key, subFile.getName()); - - result += element.getSize(); - } + throw new IllegalArgumentException("Null key detected."); } else { - result = 0; + File targetFile = new File(this.dataDirectory, key); + if (targetFile.isFile()) + { + Element element = getElement(key); + + result = element.getSize(); + } + else if (targetFile.isDirectory()) + { + result = 0; + for (File subFile : targetFile.listFiles()) + { + Element element = getElement(key, subFile.getName()); + + result += element.getSize(); + } + } + else + { + result = 0; + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -929,26 +1046,33 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - if ((key == null) || (subkey == null)) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); - } - else - { - File targetDirectory = new File(this.dataDirectory, key); - File targetFile = new File(targetDirectory, subkey); - - if (targetFile.isFile()) + if ((key == null) || (subkey == null)) { - Element element = FileTreeSikevaDBTools.loadElement(targetFile); - - result = element.getSize(); + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - result = 0; + File targetDirectory = new File(this.dataDirectory, key); + File targetFile = new File(targetDirectory, subkey); + + if (targetFile.isFile()) + { + Element element = FileTreeSikevaDBTools.loadElement(targetFile); + + result = element.getSize(); + } + else + { + result = 0; + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -986,41 +1110,48 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void put(final Element element) throws SikevaDBException { - if (element == null) + if (isOpened()) { - throw new IllegalArgumentException("element is null."); - } - else if (element.getKey() == null) - { - throw new IllegalArgumentException("Null key detected."); - } - else - { - if (element.getSubkey() == null) + if (element == null) { - File targetFile = new File(this.dataDirectory, element.getKey()); - FileTreeSikevaDBTools.saveElement(targetFile, element); + throw new IllegalArgumentException("element is null."); + } + else if (element.getKey() == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - File targetDirectory = new File(this.dataDirectory, element.getKey()); - if (!targetDirectory.exists()) + if (element.getSubkey() == null) { - boolean result = targetDirectory.mkdir(); - if (result == false) + File targetFile = new File(this.dataDirectory, element.getKey()); + FileTreeSikevaDBTools.saveElement(targetFile, element); + } + else + { + File targetDirectory = new File(this.dataDirectory, element.getKey()); + if (!targetDirectory.exists()) { - throw new SikevaDBException("Error creating key directory [" + targetDirectory + "]"); + boolean result = targetDirectory.mkdir(); + if (result == false) + { + throw new SikevaDBException("Error creating key directory [" + targetDirectory + "]"); + } + } + else if (!targetDirectory.isDirectory()) + { + throw new SikevaDBException("Invalid key directory [" + element.getKey() + "]"); } - } - else if (!targetDirectory.isDirectory()) - { - throw new SikevaDBException("Invalid key directory [" + element.getKey() + "]"); - } - File targetFile = new File(targetDirectory, element.getSubkey()); - FileTreeSikevaDBTools.saveElement(targetFile, element); + File targetFile = new File(targetDirectory, element.getSubkey()); + FileTreeSikevaDBTools.saveElement(targetFile, element); + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } } /** @@ -1040,30 +1171,37 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void put(final String key, final String value) throws SikevaDBException { - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); - } - else - { - // - Element element = getElement(key); - - // - if (element == null) + if (key == null) { - element = new Element(); - element.setKey(key); - element.update(value); - element.setArchiveDate(null); + throw new IllegalArgumentException("Null key detected."); } else { - element.update(value); - } + // + Element element = getElement(key); - // - put(element); + // + if (element == null) + { + element = new Element(); + element.setKey(key); + element.update(value); + element.setArchiveDate(null); + } + else + { + element.update(value); + } + + // + put(element); + } + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); } } @@ -1074,31 +1212,38 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void put(final String key, final String subkey, final String value) throws SikevaDBException { - if ((key == null) || (subkey == null)) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); - } - else - { - // - Element element = getElement(key, subkey); - - // - if (element == null) + if ((key == null) || (subkey == null)) { - element = new Element(); - element.setKey(key); - element.setSubkey(subkey); - element.update(value); - element.setArchiveDate(null); + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - element.update(value); - } + // + Element element = getElement(key, subkey); - // - put(element); + // + if (element == null) + { + element = new Element(); + element.setKey(key); + element.setSubkey(subkey); + element.update(value); + element.setArchiveDate(null); + } + else + { + element.update(value); + } + + // + put(element); + } + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); } } @@ -1109,27 +1254,34 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void remove(final String key) throws SikevaDBException { - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); - } - else - { - File targetFile = new File(this.dataDirectory, key); - - if (targetFile.isFile()) + if (key == null) { - boolean result = targetFile.delete(); - if (!result) - { - throw new SikevaDBException("Error removing [" + targetFile.getAbsolutePath() + "]"); - } + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid target to remove [" + targetFile.getAbsolutePath() + "]"); + File targetFile = new File(this.dataDirectory, key); + + if (targetFile.isFile()) + { + boolean result = targetFile.delete(); + if (!result) + { + throw new SikevaDBException("Error removing [" + targetFile.getAbsolutePath() + "]"); + } + } + else + { + throw new SikevaDBException("Invalid target to remove [" + targetFile.getAbsolutePath() + "]"); + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } } /** @@ -1149,28 +1301,35 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void remove(final String key, final String subkey) throws SikevaDBException { - if ((key == null) || (subkey == null)) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); - } - else - { - File targetDirectory = new File(this.dataDirectory, key); - File targetFile = new File(targetDirectory, subkey); - - if (targetFile.isFile()) + if ((key == null) || (subkey == null)) { - boolean result = targetFile.delete(); - if (!result) - { - throw new SikevaDBException("Error removing [" + targetFile.getAbsolutePath() + "]"); - } + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - throw new SikevaDBException("Invalid target to remove [" + targetFile.getAbsolutePath() + "]"); + File targetDirectory = new File(this.dataDirectory, key); + File targetFile = new File(targetDirectory, subkey); + + if (targetFile.isFile()) + { + boolean result = targetFile.delete(); + if (!result) + { + throw new SikevaDBException("Error removing [" + targetFile.getAbsolutePath() + "]"); + } + } + else + { + throw new SikevaDBException("Invalid target to remove [" + targetFile.getAbsolutePath() + "]"); + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } } /** @@ -1180,19 +1339,26 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void removeMany(final String key, final String... subkeys) throws SikevaDBException { - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Null key detected."); + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + if ((subkeys != null) && (subkeys.length > 0)) + { + for (String subkey : subkeys) + { + remove(key, subkey); + } + } + } } else { - if ((subkeys != null) && (subkeys.length > 0)) - { - for (String subkey : subkeys) - { - remove(key, subkey); - } - } + throw new SikevaDBException("Invalid database status (opened)."); } } @@ -1201,38 +1367,45 @@ public class FileTreeSikevaDB implements SikevaDB * */ @Override - public void renameKey(final String oldKey, final String newKey) + public void renameKey(final String oldKey, final String newKey) throws SikevaDBException { this.logger.debug("renameKey starting... [{}][{}]", oldKey, newKey); - if (oldKey == null) + if (isOpened()) { - throw new IllegalArgumentException("OldKey is null."); - } - else if (newKey == null) - { - throw new IllegalArgumentException("OldKey is null."); - } - else - { - File oldFile = new File(this.dataDirectory, oldKey); - File newFile = new File(this.dataDirectory, newKey); - - if (oldFile.isFile()) + if (oldKey == null) { - if (newFile.isFile()) + throw new IllegalArgumentException("OldKey is null."); + } + else if (newKey == null) + { + throw new IllegalArgumentException("OldKey is null."); + } + else + { + File oldFile = new File(this.dataDirectory, oldKey); + File newFile = new File(this.dataDirectory, newKey); + + if (oldFile.isFile()) { - throw new IllegalArgumentException("Invalid newKey [" + newKey + "]."); + if (newFile.isFile()) + { + throw new IllegalArgumentException("Invalid newKey [" + newKey + "]."); + } + else + { + oldFile.renameTo(newFile); + } } else { oldFile.renameTo(newFile); } } - else - { - oldFile.renameTo(newFile); - } + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); } this.logger.debug("renameKey done."); @@ -1256,40 +1429,47 @@ public class FileTreeSikevaDB implements SikevaDB { this.logger.debug("renameSybKey starting... [{}][{}][{}]", oldSubkey, newSubkey); - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Top key is null."); - } - if (oldSubkey == null) - { - throw new IllegalArgumentException("OldKey is null."); - } - else if (newSubkey == null) - { - throw new IllegalArgumentException("OldKey is null."); - } - else - { - File targetDirectory = new File(this.dataDirectory, key); - File oldFile = new File(targetDirectory, oldSubkey); - File newFile = new File(targetDirectory, newSubkey); - - if (oldFile.isFile()) + if (key == null) { - if (newFile.isFile()) - { - throw new IllegalArgumentException("Invalid newKey [" + newSubkey + "]."); - } - else - { - oldFile.renameTo(newFile); - } + throw new IllegalArgumentException("Top key is null."); + } + else if (oldSubkey == null) + { + throw new IllegalArgumentException("OldKey is null."); + } + else if (newSubkey == null) + { + throw new IllegalArgumentException("OldKey is null."); } else { - throw new IllegalArgumentException("Invalid oldKey [" + oldSubkey + "]."); + File targetDirectory = new File(this.dataDirectory, key); + File oldFile = new File(targetDirectory, oldSubkey); + File newFile = new File(targetDirectory, newSubkey); + + if (oldFile.isFile()) + { + if (newFile.isFile()) + { + throw new IllegalArgumentException("Invalid newKey [" + newSubkey + "]."); + } + else + { + oldFile.renameTo(newFile); + } + } + else + { + throw new IllegalArgumentException("Invalid oldKey [" + oldSubkey + "]."); + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } this.logger.debug("renameSubKey done."); } @@ -1304,17 +1484,24 @@ public class FileTreeSikevaDB implements SikevaDB { this.logger.info("replaceInValue starting... [{}]", key); - // - String value = getValue(key); - - // - for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + if (isOpened()) { - value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); - } + // + String value = getValue(key); - // - put(key, value); + // + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + { + value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(key, value); + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } this.logger.info("replaceInValue done."); } @@ -1329,32 +1516,39 @@ public class FileTreeSikevaDB implements SikevaDB { this.logger.info("replaceInValues starting... [{}]", key); - // - Elements elements = getElements(key); - - long count = 0; - for (Element element : elements) + if (isOpened()) { - this.logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size()); + // + Elements elements = getElements(key); - if (element.getSubkey() != null) + long count = 0; + for (Element element : elements) { - // - count += 1; + this.logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size()); - // - String value = element.getValue(); - - // - for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + if (element.getSubkey() != null) { - value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); - } + // + count += 1; - // - put(element.getKey(), element.getSubkey(), value); + // + String value = element.getValue(); + + // + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + { + value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(element.getKey(), element.getSubkey(), value); + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } this.logger.info("replaceInValues done."); } diff --git a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java index 786358e..a2676e2 100644 --- a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java @@ -145,24 +145,31 @@ public class SQLSikevaDB implements SikevaDB @Override public void clear() throws SikevaDBException { - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (isOpened()) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements"); + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements"); - statement.executeUpdate(); + statement.executeUpdate(); + } + catch (SQLException exception) + { + logger.error("SQL Error getting connection.", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } - catch (SQLException exception) + else { - logger.error("SQL Error getting connection.", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + throw new SikevaDBException("Invalid database status (closed)."); } } @@ -253,27 +260,34 @@ public class SQLSikevaDB implements SikevaDB { long result; - Connection connection = null; - Statement statement = null; - ResultSet resultSet = null; - try + if (isOpened()) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.createStatement(); - resultSet = statement.executeQuery("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); + Connection connection = null; + Statement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.createStatement(); + resultSet = statement.executeQuery("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); - resultSet.next(); - result = resultSet.getLong(1); + resultSet.next(); + result = resultSet.getLong(1); + } + catch (SQLException exception) + { + logger.error("Error counting elements.", exception); + throw new SikevaDBException("Error counting elements", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } - catch (SQLException exception) + else { - logger.error("Error counting elements.", exception); - throw new SikevaDBException("Error counting elements", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -289,35 +303,42 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else + { + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + } + catch (SQLException exception) + { + logger.error("Error counting elements.", exception); + throw new SikevaDBException("Error counting elements", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); - statement.setString(1, key); - resultSet = statement.executeQuery(); - - resultSet.next(); - result = resultSet.getLong(1); - } - catch (SQLException exception) - { - logger.error("Error counting elements.", exception); - throw new SikevaDBException("Error counting elements", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -348,40 +369,47 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); - } - else if (subkey == null) - { - throw new IllegalArgumentException("Subkey is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); + } + else + { + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + } + catch (SQLException exception) + { + logger.error("Error counting elements.", exception); + throw new SikevaDBException("Error counting elements", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); - statement.setString(1, key); - statement.setString(2, subkey); - resultSet = statement.executeQuery(); - - resultSet.next(); - result = resultSet.getLong(1); - } - catch (SQLException exception) - { - logger.error("Error counting elements.", exception); - throw new SikevaDBException("Error counting elements", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -560,61 +588,68 @@ public class SQLSikevaDB implements SikevaDB { Element result; - if (id == Element.NO_ID) + if (isOpened()) { - result = null; - } - else - { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (id == Element.NO_ID) + { + result = null; + } + else { // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ID=?"); - statement.setLong(1, id); - resultSet = statement.executeQuery(); - - // - if (resultSet.next()) + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { // - result = new Element(); - - result.setId(resultSet.getLong(1)); - result.setKey(resultSet.getString(2)); - result.setSubkey(resultSet.getString(3)); - result.setValue(resultSet.getString(4)); - result.setSize(resultSet.getLong(5)); - result.setDigest(resultSet.getString(6)); - result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); - result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); - result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ID=?"); + statement.setLong(1, id); + resultSet = statement.executeQuery(); // if (resultSet.next()) { - throw new SikevaDBException("More than only once result [id=" + id + "]."); + // + result = new Element(); + + result.setId(resultSet.getLong(1)); + result.setKey(resultSet.getString(2)); + result.setSubkey(resultSet.getString(3)); + result.setValue(resultSet.getString(4)); + result.setSize(resultSet.getLong(5)); + result.setDigest(resultSet.getString(6)); + result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); + result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); + result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + + // + if (resultSet.next()) + { + throw new SikevaDBException("More than only once result [id=" + id + "]."); + } + } + else + { + result = null; } } - else + catch (SQLException exception) { - result = null; + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); } } - catch (SQLException exception) - { - logger.error("Error getting element.", exception); - throw new SikevaDBException("Error getting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -630,63 +665,69 @@ public class SQLSikevaDB implements SikevaDB { Element result; - // - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); - } - else - { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else { // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection - .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); - statement.setString(1, key); - resultSet = statement.executeQuery(); - - // - if (resultSet.next()) + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { // - result = new Element(); - - result.setId(resultSet.getLong(1)); - result.setKey(resultSet.getString(2)); - result.setSubkey(resultSet.getString(3)); - result.setValue(resultSet.getString(4)); - result.setSize(resultSet.getLong(5)); - result.setDigest(resultSet.getString(6)); - result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); - result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); - result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); + statement.setString(1, key); + resultSet = statement.executeQuery(); // if (resultSet.next()) { - throw new SikevaDBException("More than only once result."); + // + result = new Element(); + + result.setId(resultSet.getLong(1)); + result.setKey(resultSet.getString(2)); + result.setSubkey(resultSet.getString(3)); + result.setValue(resultSet.getString(4)); + result.setSize(resultSet.getLong(5)); + result.setDigest(resultSet.getString(6)); + result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); + result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); + result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + + // + if (resultSet.next()) + { + throw new SikevaDBException("More than only once result."); + } + } + else + { + result = null; } } - else + catch (SQLException exception) { - result = null; + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); } } - catch (SQLException exception) - { - logger.error("Error getting element.", exception); - throw new SikevaDBException("Error getting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -717,69 +758,75 @@ public class SQLSikevaDB implements SikevaDB { Element result; - // - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); - } - else if (subkey == null) - { - throw new IllegalArgumentException("Subkey is null."); - } - else - { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); + } + else { // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection - .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); - statement.setString(1, key); - statement.setString(2, subkey); - resultSet = statement.executeQuery(); - - // - if (resultSet.next()) + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { // - result = new Element(); - - result.setId(resultSet.getLong(1)); - result.setKey(resultSet.getString(2)); - result.setSubkey(resultSet.getString(3)); - result.setValue(resultSet.getString(4)); - result.setSize(resultSet.getLong(5)); - result.setDigest(resultSet.getString(6)); - result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); - result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); - result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); // if (resultSet.next()) { - throw new SikevaDBException("More than only once result."); - } - } - else - { - result = null; - } + // + result = new Element(); + result.setId(resultSet.getLong(1)); + result.setKey(resultSet.getString(2)); + result.setSubkey(resultSet.getString(3)); + result.setValue(resultSet.getString(4)); + result.setSize(resultSet.getLong(5)); + result.setDigest(resultSet.getString(6)); + result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); + result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); + result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + + // + if (resultSet.next()) + { + throw new SikevaDBException("More than only once result."); + } + } + else + { + result = null; + } + + } + catch (SQLException exception) + { + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } - catch (SQLException exception) - { - logger.error("Error getting element.", exception); - throw new SikevaDBException("Error getting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -795,72 +842,10 @@ public class SQLSikevaDB implements SikevaDB { Elements result; - // - result = new Elements((int) countOfElements()); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection - .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL ORDER BY CREATION_DATE ASC"); - resultSet = statement.executeQuery(); - - while (resultSet.next()) - { - // - Element element = new Element(); - - element.setId(resultSet.getLong(1)); - element.setKey(resultSet.getString(2)); - element.setSubkey(resultSet.getString(3)); - element.setValue(resultSet.getString(4)); - element.setSize(resultSet.getLong(5)); - element.setDigest(resultSet.getString(6)); - element.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); - element.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); - element.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); - - // - result.add(element); - } - } - catch (SQLException exception) - { - logger.error("Error getting element.", exception); - throw new SikevaDBException("Error getting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } - - // - return result; - } - - /** - * {@inheritDoc} - * - */ - @Override - public Elements getElements(final String key) throws SikevaDBException - { - Elements result; - - // - if (key == null) - { - throw new IllegalArgumentException("Key is null."); - } - else + if (isOpened()) { // - result = new Elements((int) countOfElements(key)); + result = new Elements((int) countOfElements()); // Connection connection = null; @@ -871,8 +856,7 @@ public class SQLSikevaDB implements SikevaDB connection = getConnection(); connection.setAutoCommit(true); statement = connection - .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL ORDER BY CREATION_DATE ASC"); - statement.setString(1, key); + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL ORDER BY CREATION_DATE ASC"); resultSet = statement.executeQuery(); while (resultSet.next()) @@ -904,6 +888,82 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } + + // + return result; + } + + /** + * {@inheritDoc} + * + */ + @Override + public Elements getElements(final String key) throws SikevaDBException + { + Elements result; + + if (isOpened()) + { + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else + { + // + result = new Elements((int) countOfElements(key)); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection + .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL ORDER BY CREATION_DATE ASC"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) + { + // + Element element = new Element(); + + element.setId(resultSet.getLong(1)); + element.setKey(resultSet.getString(2)); + element.setSubkey(resultSet.getString(3)); + element.setValue(resultSet.getString(4)); + element.setSize(resultSet.getLong(5)); + element.setDigest(resultSet.getString(6)); + element.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7))); + element.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8))); + element.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9))); + + // + result.add(element); + } + } + catch (SQLException exception) + { + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } // return result; @@ -918,33 +978,40 @@ public class SQLSikevaDB implements SikevaDB { StringList result; - // - result = new StringList(); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (isOpened()) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT DISTINCT TOPKEY FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); - resultSet = statement.executeQuery(); + // + result = new StringList(); - while (resultSet.next()) + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { - result.add(resultSet.getString(1)); + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT DISTINCT TOPKEY FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); + resultSet = statement.executeQuery(); + + while (resultSet.next()) + { + result.add(resultSet.getString(1)); + } + } + catch (SQLException exception) + { + logger.error("Error getting keys.", exception); + throw new SikevaDBException("Error getting keys", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); } } - catch (SQLException exception) + else { - logger.error("Error getting keys.", exception); - throw new SikevaDBException("Error getting keys", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -978,43 +1045,49 @@ public class SQLSikevaDB implements SikevaDB { StringList result; - // - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else + { + // + result = new StringList(); + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection + .prepareStatement("SELECT DISTINCT SUBKEY,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) + { + result.add(resultSet.getString(1)); + } + } + catch (SQLException exception) + { + logger.error("Error getting subkeys.", exception); + throw new SikevaDBException("Error getting subkeys", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - // - result = new StringList(); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection - .prepareStatement("SELECT DISTINCT SUBKEY,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC"); - statement.setString(1, key); - resultSet = statement.executeQuery(); - - while (resultSet.next()) - { - result.add(resultSet.getString(1)); - } - } - catch (SQLException exception) - { - logger.error("Error getting subkeys.", exception); - throw new SikevaDBException("Error getting subkeys", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -1039,49 +1112,56 @@ public class SQLSikevaDB implements SikevaDB { String result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else + { + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + // + if (resultSet.next()) + { + result = resultSet.getString(1); + + if (resultSet.next()) + { + throw new SikevaDBException("More than only once result."); + } + } + else + { + result = null; + } + + } + catch (SQLException exception) + { + logger.error("Error getting value.", exception); + throw new SikevaDBException("Error getting value", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); - statement.setString(1, key); - resultSet = statement.executeQuery(); - - // - if (resultSet.next()) - { - result = resultSet.getString(1); - - if (resultSet.next()) - { - throw new SikevaDBException("More than only once result."); - } - } - else - { - result = null; - } - - } - catch (SQLException exception) - { - logger.error("Error getting value.", exception); - throw new SikevaDBException("Error getting value", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -1112,54 +1192,61 @@ public class SQLSikevaDB implements SikevaDB { String result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); - } - else if (subkey == null) - { - throw new IllegalArgumentException("Subkey is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + // + if (resultSet.next()) + { + result = resultSet.getString(1); + + if (resultSet.next()) + { + throw new SikevaDBException("More than only once result."); + } + } + else + { + result = null; + } + } + catch (SQLException exception) + { + logger.error("Error getting value.", exception); + throw new SikevaDBException("Error getting value", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); - statement.setString(1, key); - statement.setString(2, subkey); - resultSet = statement.executeQuery(); - - // - if (resultSet.next()) - { - result = resultSet.getString(1); - - if (resultSet.next()) - { - throw new SikevaDBException("More than only once result."); - } - } - else - { - result = null; - } - } - catch (SQLException exception) - { - logger.error("Error getting value.", exception); - throw new SikevaDBException("Error getting value", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -1175,34 +1262,41 @@ public class SQLSikevaDB implements SikevaDB { StringList result; - // - result = new StringList((int) countOfElements(key)); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (isOpened()) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC"); - statement.setString(1, key); - resultSet = statement.executeQuery(); + // + result = new StringList((int) countOfElements(key)); - while (resultSet.next()) + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { - result.add(resultSet.getString(1)); + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + while (resultSet.next()) + { + result.add(resultSet.getString(1)); + } + } + catch (SQLException exception) + { + logger.error("Error getting values.", exception); + throw new SikevaDBException("Error getting values", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); } } - catch (SQLException exception) + else { - logger.error("Error getting values.", exception); - throw new SikevaDBException("Error getting values", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -1308,28 +1402,35 @@ public class SQLSikevaDB implements SikevaDB { long result; - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (isOpened()) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); - resultSet = statement.executeQuery(); + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); + resultSet = statement.executeQuery(); - resultSet.next(); - result = resultSet.getLong(1); + resultSet.next(); + result = resultSet.getLong(1); + } + catch (SQLException exception) + { + logger.error("Error computing memory size.", exception); + throw new SikevaDBException("Error computing memory size", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } - catch (SQLException exception) + else { - logger.error("Error computing memory size.", exception); - throw new SikevaDBException("Error computing memory size", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -1345,35 +1446,42 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else + { + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?"); + statement.setString(1, key); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + } + catch (SQLException exception) + { + logger.error("Error computing key memory size.", exception); + throw new SikevaDBException("Error computing key memory size.", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?"); - statement.setString(1, key); - resultSet = statement.executeQuery(); - - resultSet.next(); - result = resultSet.getLong(1); - } - catch (SQLException exception) - { - logger.error("Error computing key memory size.", exception); - throw new SikevaDBException("Error computing key memory size.", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -1404,42 +1512,48 @@ public class SQLSikevaDB implements SikevaDB { long result; - // - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); - } - else if (subkey == null) - { - throw new IllegalArgumentException("Subkey is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); + statement.setString(1, key); + statement.setString(2, subkey); + resultSet = statement.executeQuery(); + + resultSet.next(); + result = resultSet.getLong(1); + } + catch (SQLException exception) + { + logger.error("Error computing subkey memory size.", exception); + throw new SikevaDBException("Error computing subkey memory size", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); - statement.setString(1, key); - statement.setString(2, subkey); - resultSet = statement.executeQuery(); - - resultSet.next(); - result = resultSet.getLong(1); - } - catch (SQLException exception) - { - logger.error("Error computing subkey memory size.", exception); - throw new SikevaDBException("Error computing subkey memory size", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } // @@ -1515,118 +1629,126 @@ public class SQLSikevaDB implements SikevaDB @Override public void put(final Element element) throws SikevaDBException { - if (element == null) + if (isOpened()) { - throw new IllegalArgumentException("element is null."); - } - else - { - if (exists(element)) + if (element == null) { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - - // Archive existing element. - statement = connection.prepareStatement("UPDATE sikevadb_elements SET TOPKEY=?,SUBKEY=?,VALUE=?,SIZE=?,DIGEST=?,CREATION_DATE=?,EDITION_DATE=?,ARCHIVE_DATE=? WHERE ID=?"); - - statement.setString(1, element.getKey()); - statement.setString(2, element.getSubkey()); - statement.setString(3, element.getValue()); - statement.setLong(4, element.getSize()); - statement.setString(5, element.getDigest()); - statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); - statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); - statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); - statement.setLong(9, element.getId()); - - statement.executeUpdate(); - } - catch (SQLException exception) - { - logger.error("Error putting element.", exception); - throw new SikevaDBException("Error putting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } - } - else if (element.getId() == Element.NO_ID) - { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); - - statement.setString(1, element.getKey()); - statement.setString(2, element.getSubkey()); - statement.setString(3, element.getValue()); - statement.setLong(4, element.getSize()); - statement.setString(5, element.getDigest()); - statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); - statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); - statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); - - statement.executeUpdate(); - } - catch (SQLException exception) - { - logger.error("Error putting element.", exception); - throw new SikevaDBException("Error putting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new IllegalArgumentException("element is null."); } else { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (exists(element)) { - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection - .prepareStatement("INSERT INTO sikevadb_elements (ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?,?, ?, ?, ?, ?, ?, ?, ?)"); + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(true); - statement.setLong(1, element.getId()); - statement.setString(2, element.getKey()); - statement.setString(3, element.getSubkey()); - statement.setString(4, element.getValue()); - statement.setLong(5, element.getSize()); - statement.setString(6, element.getDigest()); - statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); - statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); - statement.setTimestamp(9, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); + // Archive existing element. + statement = connection.prepareStatement("UPDATE sikevadb_elements SET TOPKEY=?,SUBKEY=?,VALUE=?,SIZE=?,DIGEST=?,CREATION_DATE=?,EDITION_DATE=?,ARCHIVE_DATE=? WHERE ID=?"); - statement.executeUpdate(); + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + statement.setString(3, element.getValue()); + statement.setLong(4, element.getSize()); + statement.setString(5, element.getDigest()); + statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); + statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); + statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); + statement.setLong(9, element.getId()); + + statement.executeUpdate(); + } + catch (SQLException exception) + { + logger.error("Error putting element.", exception); + throw new SikevaDBException("Error putting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } - catch (SQLException exception) + else if (element.getId() == Element.NO_ID) { - logger.error("Error putting element.", exception); - throw new SikevaDBException("Error putting element", exception); + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection + .prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); + + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + statement.setString(3, element.getValue()); + statement.setLong(4, element.getSize()); + statement.setString(5, element.getDigest()); + statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); + statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); + statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); + + statement.executeUpdate(); + } + catch (SQLException exception) + { + logger.error("Error putting element.", exception); + throw new SikevaDBException("Error putting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } - finally + else { - closeQuietly(connection, statement, resultSet); + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection + .prepareStatement("INSERT INTO sikevadb_elements (ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?,?, ?, ?, ?, ?, ?, ?, ?)"); + + statement.setLong(1, element.getId()); + statement.setString(2, element.getKey()); + statement.setString(3, element.getSubkey()); + statement.setString(4, element.getValue()); + statement.setLong(5, element.getSize()); + statement.setString(6, element.getDigest()); + statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); + statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); + statement.setTimestamp(9, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); + + statement.executeUpdate(); + } + catch (SQLException exception) + { + logger.error("Error putting element.", exception); + throw new SikevaDBException("Error putting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } } /** @@ -1646,106 +1768,16 @@ public class SQLSikevaDB implements SikevaDB @Override public void put(final String key, final String value) throws SikevaDBException { - // - Element element = getElement(key); - - // - if (element == null) - { - element = new Element(); - element.setKey(key); - element.update(value); - element.setArchiveDate(null); - } - else - { - element.update(value); - } - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (isOpened()) { // - connection = getConnection(); - connection.setAutoCommit(false); - - // Archive existing element. - if (element.getId() != Element.NO_ID) - { - statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); - - statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); - statement.setString(2, element.getKey()); - - statement.executeUpdate(); - statement.clearParameters(); - statement.close(); - } - - // Insert new version of the element. - statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); - - statement.setString(1, element.getKey()); - statement.setString(2, element.getSubkey()); - statement.setString(3, element.getValue()); - statement.setLong(4, element.getSize()); - statement.setString(5, element.getDigest()); - statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); - statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); - statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); - - statement.executeUpdate(); - - connection.commit(); - } - catch (SQLException exception) - { - logger.error("Error putting element.", exception); - try - { - connection.rollback(); - } - catch (SQLException exception1) - { - logger.error("Rollback failed.", exception); - } - throw new SikevaDBException("Error putting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } - } - - /** - * {@inheritDoc} - * - */ - @Override - public void put(final String key, final String subkey, final String value) throws SikevaDBException - { - if (key == null) - { - throw new IllegalArgumentException("Key is null."); - } - else if (subkey == null) - { - throw new IllegalArgumentException("Subkey is null."); - } - else - { - // - Element element = getElement(key, subkey); + Element element = getElement(key); // if (element == null) { element = new Element(); element.setKey(key); - element.setSubkey(subkey); element.update(value); element.setArchiveDate(null); } @@ -1767,11 +1799,10 @@ public class SQLSikevaDB implements SikevaDB // Archive existing element. if (element.getId() != Element.NO_ID) { - statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); + statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); statement.setString(2, element.getKey()); - statement.setString(3, element.getSubkey()); statement.executeUpdate(); statement.clearParameters(); @@ -1794,7 +1825,7 @@ public class SQLSikevaDB implements SikevaDB connection.commit(); } - catch (Exception exception) + catch (SQLException exception) { logger.error("Error putting element.", exception); try @@ -1812,6 +1843,111 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } + } + + /** + * {@inheritDoc} + * + */ + @Override + public void put(final String key, final String subkey, final String value) throws SikevaDBException + { + if (isOpened()) + { + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); + } + else + { + // + Element element = getElement(key, subkey); + + // + if (element == null) + { + element = new Element(); + element.setKey(key); + element.setSubkey(subkey); + element.update(value); + element.setArchiveDate(null); + } + else + { + element.update(value); + } + + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(false); + + // Archive existing element. + if (element.getId() != Element.NO_ID) + { + statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); + + statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); + statement.setString(2, element.getKey()); + statement.setString(3, element.getSubkey()); + + statement.executeUpdate(); + statement.clearParameters(); + statement.close(); + } + + // Insert new version of the element. + statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); + + statement.setString(1, element.getKey()); + statement.setString(2, element.getSubkey()); + statement.setString(3, element.getValue()); + statement.setLong(4, element.getSize()); + statement.setString(5, element.getDigest()); + statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate())); + statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); + statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate())); + + statement.executeUpdate(); + + connection.commit(); + } + catch (Exception exception) + { + logger.error("Error putting element.", exception); + try + { + connection.rollback(); + } + catch (SQLException exception1) + { + logger.error("Rollback failed.", exception); + } + throw new SikevaDBException("Error putting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } } /** @@ -1821,49 +1957,56 @@ public class SQLSikevaDB implements SikevaDB @Override public void remove(final String key) throws SikevaDBException { - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); + + statement.setString(1, key); + + int rowCount = statement.executeUpdate(); + + if (rowCount == 0) + { + logger.warn("Remove action without existing target [key=" + key + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error removing element.", exception); + try + { + connection.rollback(); + } + catch (SQLException exception1) + { + logger.error("Rollback failed.", exception); + } + throw new SikevaDBException("Error removing element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); - - statement.setString(1, key); - - int rowCount = statement.executeUpdate(); - - if (rowCount == 0) - { - logger.warn("Remove action without existing target [key=" + key + "]"); - } - } - catch (SQLException exception) - { - logger.error("Error removing element.", exception); - try - { - connection.rollback(); - } - catch (SQLException exception1) - { - logger.error("Rollback failed.", exception); - } - throw new SikevaDBException("Error removing element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } } @@ -1884,45 +2027,52 @@ public class SQLSikevaDB implements SikevaDB @Override public void remove(final String key, final String subkey) throws SikevaDBException { - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); - } - else if (subkey == null) - { - throw new IllegalArgumentException("Subkey is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); + + statement.setString(1, key); + statement.setString(2, subkey); + + int rowCount = statement.executeUpdate(); + + if (rowCount == 0) + { + logger.warn("Remove action without existing target [key=" + key + "][subkey=" + subkey + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error removing subkey.", exception); + throw new SikevaDBException("Error removing subkey", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); - - statement.setString(1, key); - statement.setString(2, subkey); - - int rowCount = statement.executeUpdate(); - - if (rowCount == 0) - { - logger.warn("Remove action without existing target [key=" + key + "][subkey=" + subkey + "]"); - } - } - catch (SQLException exception) - { - logger.error("Error removing subkey.", exception); - throw new SikevaDBException("Error removing subkey", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (opened)."); } } @@ -1933,49 +2083,56 @@ public class SQLSikevaDB implements SikevaDB @Override public void removeMany(final String key, final String... subkeys) throws SikevaDBException { - if (key == null) + if (isOpened()) { - throw new IllegalArgumentException("Key is null."); + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if ((subkeys != null) && (subkeys.length > 0)) + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + + // + String questionMarks = new StringList(subkeys.length).append('?').repeatLast(subkeys.length - 1).toStringWithCommas(); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IN (" + questionMarks + ") AND ARCHIVE_DATE IS NULL"); + // + statement.setString(1, key); + + // + for (int index = 0; index < subkeys.length; index++) + { + statement.setString(2 + index, subkeys[index]); + } + + // + int rowCount = statement.executeUpdate(); + if (rowCount == 0) + { + logger.warn("Remove action without existing target [key=" + key + "][subkeys=" + new StringList(subkeys).toStringWithCommas() + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error removing subkeys.", exception); + throw new SikevaDBException("Error removing subkeys", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } - else if ((subkeys != null) && (subkeys.length > 0)) + else { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - - // - String questionMarks = new StringList(subkeys.length).append('?').repeatLast(subkeys.length - 1).toStringWithCommas(); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IN (" + questionMarks + ") AND ARCHIVE_DATE IS NULL"); - // - statement.setString(1, key); - - // - for (int index = 0; index < subkeys.length; index++) - { - statement.setString(2 + index, subkeys[index]); - } - - // - int rowCount = statement.executeUpdate(); - if (rowCount == 0) - { - logger.warn("Remove action without existing target [key=" + key + "][subkeys=" + new StringList(subkeys).toStringWithCommas() + "]"); - } - } - catch (SQLException exception) - { - logger.error("Error removing subkeys.", exception); - throw new SikevaDBException("Error removing subkeys", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } } @@ -1988,41 +2145,48 @@ public class SQLSikevaDB implements SikevaDB { logger.info("renameKey starting... [{}][{}]", oldKey, newKey); - if (oldKey == null) + if (isOpened()) { - throw new IllegalArgumentException("OldKey is null."); - } - else if (newKey == null) - { - throw new IllegalArgumentException("NewKey is null."); + if (oldKey == null) + { + throw new IllegalArgumentException("OldKey is null."); + } + else if (newKey == null) + { + throw new IllegalArgumentException("NewKey is null."); + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("UPDATE sikevadb_elements SET TOPKEY=? WHERE TOPKEY=?"); + + statement.setString(1, newKey); + statement.setString(2, oldKey); + + statement.executeUpdate(); + } + catch (SQLException exception) + { + logger.error("Error renaming subkey.", exception); + throw new SikevaDBException("Error renaming subkey", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } } else { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("UPDATE sikevadb_elements SET TOPKEY=? WHERE TOPKEY=?"); - - statement.setString(1, newKey); - statement.setString(2, oldKey); - - statement.executeUpdate(); - } - catch (SQLException exception) - { - logger.error("Error renaming subkey.", exception); - throw new SikevaDBException("Error renaming subkey", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } + throw new SikevaDBException("Invalid database status (closed)."); } logger.info("renameKey done."); @@ -2045,13 +2209,20 @@ public class SQLSikevaDB implements SikevaDB @Override public void renameSubkey(final String key, final String oldKey, final String newKey) throws SikevaDBException { - if ((key == null) || (oldKey == null) || (newKey == null)) + if (isOpened()) { - throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "]."); + if ((key == null) || (oldKey == null) || (newKey == null)) + { + throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "]."); + } + else + { + // TODO Auto-generated method stub + } } else { - // TODO Auto-generated method stub + throw new SikevaDBException("Invalid database status (closed)."); } } @@ -2064,17 +2235,24 @@ public class SQLSikevaDB implements SikevaDB { logger.info("replaceInValue starting... [{}]", key); - // - String value = getValue(key); - - // - for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + if (isOpened()) { - value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); - } + // + String value = getValue(key); - // - put(key, value); + // + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + { + value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(key, value); + } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } logger.info("replaceInValue done."); } @@ -2088,33 +2266,38 @@ public class SQLSikevaDB implements SikevaDB { logger.info("replaceInValues starting... [{}]", key); - // - Elements elements = getElements(key); - - long count = 0; - for (Element element : elements) + if (isOpened()) { - // - logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size()); + Elements elements = getElements(key); - if (element.getSubkey() != null) + long count = 0; + for (Element element : elements) { - // - count += 1; + logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size()); - // - String value = element.getValue(); - - // - for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + if (element.getSubkey() != null) { - value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); - } + // + count += 1; - // - put(element.getKey(), element.getSubkey(), value); + // + String value = element.getValue(); + + // + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + { + value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(element.getKey(), element.getSubkey(), value); + } } } + else + { + throw new SikevaDBException("Invalid database status (closed)."); + } logger.info("replaceInValues done."); } diff --git a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java index 54bcb1a..0a62dd8 100644 --- a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java @@ -605,10 +605,11 @@ public class TreeFileSikevaDBTest } /** + * @throws SikevaDBException * */ @AfterClass - public static void afterClass() + public static void afterClass() throws SikevaDBException { if (database != null) {