diff --git a/src/fr/devinsy/sikevadb/core/UnopenedDatabaseException.java b/src/fr/devinsy/sikevadb/core/ClosedDatabaseException.java similarity index 77% rename from src/fr/devinsy/sikevadb/core/UnopenedDatabaseException.java rename to src/fr/devinsy/sikevadb/core/ClosedDatabaseException.java index 4aabced..5f6d085 100644 --- a/src/fr/devinsy/sikevadb/core/UnopenedDatabaseException.java +++ b/src/fr/devinsy/sikevadb/core/ClosedDatabaseException.java @@ -22,23 +22,23 @@ package fr.devinsy.sikevadb.core; * * @author Christian Pierre MOMON (christian.momon@devinsy.fr) */ -public class UnopenedDatabaseException extends SikevaDBException +public class ClosedDatabaseException extends SikevaDBException { private static final long serialVersionUID = 8364599416669077052L; /** * */ - public UnopenedDatabaseException() + public ClosedDatabaseException() { - super(); + super("Invalid database status for this operation: closed."); } /** * * @param message */ - public UnopenedDatabaseException(final String message) + public ClosedDatabaseException(final String message) { super(message); } @@ -48,7 +48,7 @@ public class UnopenedDatabaseException extends SikevaDBException * @param message * @param cause */ - public UnopenedDatabaseException(final String message, final Throwable cause) + public ClosedDatabaseException(final String message, final Throwable cause) { super(message, cause); } @@ -57,7 +57,7 @@ public class UnopenedDatabaseException extends SikevaDBException * * @param cause */ - public UnopenedDatabaseException(final Throwable cause) + public ClosedDatabaseException(final Throwable cause) { super(cause); } diff --git a/src/fr/devinsy/sikevadb/core/OpenedDatabaseException.java b/src/fr/devinsy/sikevadb/core/OpenedDatabaseException.java new file mode 100644 index 0000000..685deaa --- /dev/null +++ b/src/fr/devinsy/sikevadb/core/OpenedDatabaseException.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2018 Christian Pierre MOMON + * + * This file is part of SikevaDB, simple key value database. + * + * SikevaDB is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * SikevaDB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with SikevaDB. If not, see . + */ +package fr.devinsy.sikevadb.core; + +/** + * + * @author Christian Pierre MOMON (christian.momon@devinsy.fr) + */ +public class OpenedDatabaseException extends SikevaDBException +{ + private static final long serialVersionUID = 8364599416669077052L; + + /** + * + */ + public OpenedDatabaseException() + { + super("Invalid database status for this operation: opened."); + } + + /** + * + * @param message + */ + public OpenedDatabaseException(final String message) + { + super(message); + } + + /** + * + * @param message + * @param cause + */ + public OpenedDatabaseException(final String message, final Throwable cause) + { + super(message, cause); + } + + /** + * + * @param cause + */ + public OpenedDatabaseException(final Throwable cause) + { + super(cause); + } +} diff --git a/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java b/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java index 1c4b832..212e06f 100644 --- a/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java @@ -81,11 +81,11 @@ public class XMLSikevaDB { if (out == null) { - throw new NullPointerException("out is null."); + throw new IllegalArgumentException("out is null."); } else if (source == null) { - throw new NullPointerException("source is null."); + throw new IllegalArgumentException("source is null."); } else if (fileName == null) { @@ -249,11 +249,11 @@ public class XMLSikevaDB if (out == null) { - throw new NullPointerException("out is null."); + throw new IllegalArgumentException("out is null."); } else if (source == null) { - throw new NullPointerException("element is null."); + throw new IllegalArgumentException("element is null."); } else { @@ -307,7 +307,7 @@ public class XMLSikevaDB { if (out == null) { - throw new NullPointerException("out is null."); + throw new IllegalArgumentException("out is null."); } else if (source == null) { diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java index eb01aa3..39ef9fa 100644 --- a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java @@ -30,8 +30,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.sikevadb.core.Archiver; +import fr.devinsy.sikevadb.core.ClosedDatabaseException; import fr.devinsy.sikevadb.core.Element; import fr.devinsy.sikevadb.core.Elements; +import fr.devinsy.sikevadb.core.OpenedDatabaseException; import fr.devinsy.sikevadb.core.SikevaDB; import fr.devinsy.sikevadb.core.SikevaDBException; import fr.devinsy.util.ToolBox; @@ -113,7 +115,19 @@ public class FileTreeSikevaDB implements SikevaDB @Override public Archiver archiver() throws SikevaDBException { - return this.archiver; + Archiver result; + + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = this.archiver; + } + + // + return result; } /* (non-Javadoc) @@ -122,7 +136,11 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void clear() throws SikevaDBException { - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { try { @@ -137,10 +155,6 @@ public class FileTreeSikevaDB implements SikevaDB throw new SikevaDBException("Error clearing database", exception); } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } } /* (non-Javadoc) @@ -149,16 +163,16 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void close() throws SikevaDBException { - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { this.archiver.close(); this.status = Status.CLOSED; } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } } /* (non-Javadoc) @@ -169,9 +183,12 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { - File[] topFiles = this.dataDirectory.listFiles(); result = 0; @@ -188,10 +205,6 @@ public class FileTreeSikevaDB implements SikevaDB } } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -205,30 +218,27 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) { - if (key == null) - { - throw new IllegalArgumentException("Null key detected."); - } - else - { - File targetDirectory = new File(this.dataDirectory, key); - File[] subFiles = targetDirectory.listFiles(); - - if (subFiles == null) - { - result = 1; - } - else - { - result = subFiles.length; - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + File targetDirectory = new File(this.dataDirectory, key); + File[] subFiles = targetDirectory.listFiles(); + + if (subFiles == null) + { + result = 1; + } + else + { + result = subFiles.length; + } } // @@ -243,7 +253,14 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - result = countOfElements(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = countOfElements(key, String.valueOf(subkey)); + } // return result; @@ -257,29 +274,26 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) { - 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); - if (targetFile.exists()) - { - result = 1; - } - else - { - result = 0; - } - } + throw new ClosedDatabaseException(); + } + else if ((key == null) || (subkey == null)) + { + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + File targetDirectory = new File(this.dataDirectory, key); + File targetFile = new File(targetDirectory, subkey); + if (targetFile.exists()) + { + result = 1; + } + else + { + result = 0; + } } // @@ -292,9 +306,9 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void create() throws SikevaDBException { - if (this.status == Status.OPENED) + if (isOpened()) { - throw new SikevaDBException("Invalid database status (opened)."); + throw new OpenedDatabaseException(); } else if (this.homeDirectory == null) { @@ -318,33 +332,30 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void delete(final String key) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - if (key == null) - { - throw new IllegalArgumentException("Null key detected."); - } - else - { - File targetFile = new File(this.dataDirectory, key); - - if (targetFile.isFile()) - { - boolean result = targetFile.delete(); - if (!result) - { - throw new SikevaDBException("Error deleting [" + targetFile.getAbsolutePath() + "]"); - } - } - else - { - throw new SikevaDBException("Invalid target to delete [" + targetFile.getAbsolutePath() + "]"); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + File targetFile = new File(this.dataDirectory, key); + + if (targetFile.isFile()) + { + boolean result = targetFile.delete(); + if (!result) + { + throw new SikevaDBException("Error deleting [" + targetFile.getAbsolutePath() + "]"); + } + } + else + { + throw new SikevaDBException("Invalid target to delete [" + targetFile.getAbsolutePath() + "]"); + } } } @@ -354,7 +365,14 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void delete(final String key, final long subkey) throws SikevaDBException { - delete(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + delete(key, String.valueOf(subkey)); + } } /* (non-Javadoc) @@ -363,34 +381,31 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void delete(final String key, final String subkey) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - 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); - - if (targetFile.isFile()) - { - boolean result = targetFile.delete(); - if (!result) - { - throw new SikevaDBException("Error deleting [" + targetFile.getAbsolutePath() + "]"); - } - } - else - { - throw new SikevaDBException("Invalid target to deleting [" + targetFile.getAbsolutePath() + "]"); - } - } + throw new ClosedDatabaseException(); + } + else if ((key == null) || (subkey == null)) + { + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + 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 deleting [" + targetFile.getAbsolutePath() + "]"); + } + } + else + { + throw new SikevaDBException("Invalid target to deleting [" + targetFile.getAbsolutePath() + "]"); + } } } @@ -400,26 +415,23 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void deleteMany(final String key, final String... subkeys) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - if (key == null) - { - throw new IllegalArgumentException("Null key detected."); - } - else - { - if ((subkeys != null) && (subkeys.length > 0)) - { - for (String subkey : subkeys) - { - delete(key, subkey); - } - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (opened)."); + if ((subkeys != null) && (subkeys.length > 0)) + { + for (String subkey : subkeys) + { + delete(key, subkey); + } + } } } @@ -431,7 +443,7 @@ public class FileTreeSikevaDB implements SikevaDB { if (isOpened()) { - throw new SikevaDBException("Invalid database status (opened)."); + throw new OpenedDatabaseException(); } else if (this.homeDirectory == null) { @@ -547,26 +559,23 @@ public class FileTreeSikevaDB implements SikevaDB { Element result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + File targetFile = new File(this.dataDirectory, key); + result = FileTreeSikevaDBTools.loadElement(targetFile); + if (result != null) + { + result.setKey(key); + result.setSubkey(null); + } } // @@ -581,7 +590,14 @@ public class FileTreeSikevaDB implements SikevaDB { Element result; - result = getElement(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = getElement(key, String.valueOf(subkey)); + } // return result; @@ -595,29 +611,26 @@ public class FileTreeSikevaDB implements SikevaDB { Element result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if ((key == null) || (subkey == null)) + { + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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); + } } // @@ -632,7 +645,11 @@ public class FileTreeSikevaDB implements SikevaDB { Elements result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { result = new Elements((int) countOfElements()); @@ -661,10 +678,6 @@ public class FileTreeSikevaDB implements SikevaDB } } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -678,32 +691,29 @@ public class FileTreeSikevaDB implements SikevaDB { Elements result; - if (isOpened()) + if (isClosed()) { - 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); - } - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + 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); + } + } } // @@ -733,7 +743,11 @@ public class FileTreeSikevaDB implements SikevaDB { StringList result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { result = new StringList((int) countOfElements()); @@ -758,10 +772,6 @@ public class FileTreeSikevaDB implements SikevaDB } } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -805,31 +815,28 @@ public class FileTreeSikevaDB implements SikevaDB { StringList result; - if (isOpened()) + if (isClosed()) { - 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()); - } - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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()); + } + } } // @@ -844,30 +851,27 @@ public class FileTreeSikevaDB implements SikevaDB { String result; - if (isOpened()) + if (isClosed()) { - if (key == null) - { - throw new IllegalArgumentException("Null key detected."); - } - else - { - File targetFile = new File(this.dataDirectory, key); - if (targetFile.exists()) - { - Element element = FileTreeSikevaDBTools.loadElement(targetFile); - - result = element.getValue(); - } - else - { - result = null; - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + File targetFile = new File(this.dataDirectory, key); + if (targetFile.exists()) + { + Element element = FileTreeSikevaDBTools.loadElement(targetFile); + + result = element.getValue(); + } + else + { + result = null; + } } // @@ -882,7 +886,14 @@ public class FileTreeSikevaDB implements SikevaDB { String result; - result = getValue(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = getValue(key, String.valueOf(subkey)); + } // return result; @@ -896,31 +907,28 @@ public class FileTreeSikevaDB implements SikevaDB { String result; - if (isOpened()) + if (isClosed()) { - 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); - if (targetFile.exists()) - { - Element element = FileTreeSikevaDBTools.loadElement(targetFile); - - result = element.getValue(); - } - else - { - result = null; - } - } + throw new ClosedDatabaseException(); + } + else if ((key == null) || (subkey == null)) + { + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + 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; + } } // @@ -935,39 +943,36 @@ public class FileTreeSikevaDB implements SikevaDB { StringList result; - if (isOpened()) + if (isClosed()) { - if (key == null) - { - throw new IllegalArgumentException("Null key detected."); - } - else - { - // - result = new StringList((int) countOfElements(key)); - - // - File targetDirectory = new File(this.dataDirectory, key); - - if (targetDirectory.isDirectory()) - { - File[] subFiles = targetDirectory.listFiles(); - - for (File subFile : subFiles) - { - if (subFile.isFile()) - { - Element element = FileTreeSikevaDBTools.loadElement(subFile); - - result.add(element.getValue()); - } - } - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + result = new StringList((int) countOfElements(key)); + + // + File targetDirectory = new File(this.dataDirectory, key); + + if (targetDirectory.isDirectory()) + { + File[] subFiles = targetDirectory.listFiles(); + + for (File subFile : subFiles) + { + if (subFile.isFile()) + { + Element element = FileTreeSikevaDBTools.loadElement(subFile); + + result.add(element.getValue()); + } + } + } } // @@ -982,7 +987,7 @@ public class FileTreeSikevaDB implements SikevaDB { boolean result; - if (this.status == Status.OPENED) + if (isOpened()) { result = this.archiver.isActivated(); } @@ -1052,7 +1057,11 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { // result = 0; @@ -1083,10 +1092,6 @@ public class FileTreeSikevaDB implements SikevaDB } } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -1100,7 +1105,11 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { if (key == null) { @@ -1131,10 +1140,6 @@ public class FileTreeSikevaDB implements SikevaDB } } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -1148,7 +1153,14 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - result = memorySize(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = memorySize(key, String.valueOf(subkey)); + } // return result; @@ -1162,7 +1174,11 @@ public class FileTreeSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { if ((key == null) || (subkey == null)) { @@ -1185,10 +1201,6 @@ public class FileTreeSikevaDB implements SikevaDB } } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -1224,47 +1236,44 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void put(final Element element) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - if (element == null) - { - throw new IllegalArgumentException("element is null."); - } - else if (element.getKey() == null) - { - throw new IllegalArgumentException("Null key detected."); - } - else - { - if (element.getSubkey() == null) - { - File targetFile = new File(this.dataDirectory, element.getKey()); - FileTreeSikevaDBTools.saveElement(targetFile, element); - } - else - { - File targetDirectory = new File(this.dataDirectory, element.getKey()); - if (!targetDirectory.exists()) - { - 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() + "]"); - } - - File targetFile = new File(targetDirectory, element.getSubkey()); - FileTreeSikevaDBTools.saveElement(targetFile, element); - } - } + throw new ClosedDatabaseException(); + } + else if (element == null) + { + throw new IllegalArgumentException("element is null."); + } + else if (element.getKey() == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + if (element.getSubkey() == null) + { + File targetFile = new File(this.dataDirectory, element.getKey()); + FileTreeSikevaDBTools.saveElement(targetFile, element); + } + else + { + File targetDirectory = new File(this.dataDirectory, element.getKey()); + if (!targetDirectory.exists()) + { + 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() + "]"); + } + + File targetFile = new File(targetDirectory, element.getSubkey()); + FileTreeSikevaDBTools.saveElement(targetFile, element); + } } } @@ -1274,7 +1283,14 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void put(final String key, final long subkey, final String value) throws SikevaDBException { - put(key, String.valueOf(subkey), value); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + put(key, String.valueOf(subkey), value); + } } /* (non-Javadoc) @@ -1283,37 +1299,34 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void put(final String key, final String value) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - if (key == null) - { - throw new IllegalArgumentException("Null key detected."); - } - else - { - // - Element element = getElement(key); - - // - if (element == null) - { - element = new Element(); - element.setKey(key); - element.update(value); - element.setArchiveDate(null); - } - else - { - element.update(value); - } - - // - put(element); - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Null key detected."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + Element element = getElement(key); + + // + if (element == null) + { + element = new Element(); + element.setKey(key); + element.update(value); + element.setArchiveDate(null); + } + else + { + element.update(value); + } + + // + put(element); } } @@ -1323,38 +1336,35 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void put(final String key, final String subkey, final String value) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - if ((key == null) || (subkey == null)) - { - throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); - } - 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); - } - - // - put(element); - } + throw new ClosedDatabaseException(); + } + else if ((key == null) || (subkey == null)) + { + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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); + } + + // + put(element); } } @@ -1366,41 +1376,38 @@ public class FileTreeSikevaDB implements SikevaDB { this.logger.debug("renameKey starting... [{}][{}]", oldKey, newKey); - if (isOpened()) + if (isClosed()) { - if (oldKey == null) - { - 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); + throw new ClosedDatabaseException(); + } + else if (oldKey == null) + { + 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 (oldFile.isFile()) + { + if (newFile.isFile()) { - if (newFile.isFile()) - { - throw new IllegalArgumentException("Invalid newKey [" + newKey + "]."); - } - else - { - oldFile.renameTo(newFile); - } + throw new IllegalArgumentException("Invalid newKey [" + newKey + "]."); } else { oldFile.renameTo(newFile); } } - } - else - { - throw new SikevaDBException("Invalid database status (closed)."); + else + { + oldFile.renameTo(newFile); + } } this.logger.debug("renameKey done."); @@ -1412,7 +1419,14 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void renameSubkey(final String key, final long oldSubKey, final long newSubkey) throws SikevaDBException { - renameSubkey(key, String.valueOf(oldSubKey), String.valueOf(newSubkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + renameSubkey(key, String.valueOf(oldSubKey), String.valueOf(newSubkey)); + } } /* (non-Javadoc) @@ -1423,46 +1437,43 @@ public class FileTreeSikevaDB implements SikevaDB { this.logger.debug("renameSybKey starting... [{}][{}][{}]", oldSubkey, newSubkey); - if (isOpened()) + if (isClosed()) { - if (key == null) - { - 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 - { - 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 + "]."); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + 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 SikevaDBException("Invalid database status (closed)."); + 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 + "]."); + } } this.logger.debug("renameSubKey done."); @@ -1476,24 +1487,21 @@ public class FileTreeSikevaDB implements SikevaDB { this.logger.info("replaceInValue starting... [{}]", key); - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { - // String value = getValue(key); - // 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."); } @@ -1506,9 +1514,12 @@ public class FileTreeSikevaDB implements SikevaDB { this.logger.info("replaceInValues starting... [{}]", key); - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { - // Elements elements = getElements(key); long count = 0; @@ -1535,10 +1546,6 @@ public class FileTreeSikevaDB implements SikevaDB } } } - 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 157a2d2..435a63a 100644 --- a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java @@ -37,8 +37,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.sikevadb.core.Archiver; +import fr.devinsy.sikevadb.core.ClosedDatabaseException; import fr.devinsy.sikevadb.core.Element; import fr.devinsy.sikevadb.core.Elements; +import fr.devinsy.sikevadb.core.OpenedDatabaseException; import fr.devinsy.sikevadb.core.SikevaDB; import fr.devinsy.sikevadb.core.SikevaDBException; import fr.devinsy.util.strings.StringList; @@ -64,8 +66,10 @@ public class SQLSikevaDB implements SikevaDB private String url; private String login; private String password; - private Connection singleConnection; + private String contextName; + + private Connection singleConnection; private DataSource dataSource; private Archiver archiver; @@ -98,19 +102,19 @@ public class SQLSikevaDB implements SikevaDB { if (StringUtils.isBlank(driverClassName)) { - throw new NullPointerException("driverClassName is null."); + throw new IllegalArgumentException("driverClassName is null."); } else if (StringUtils.isBlank(url)) { - throw new NullPointerException("url is null."); + throw new IllegalArgumentException("url is null."); } else if (StringUtils.isBlank(login)) { - throw new NullPointerException("login is null."); + throw new IllegalArgumentException("login is null."); } else if (password == null) { - throw new NullPointerException("password is null"); + throw new IllegalArgumentException("password is null."); } else { @@ -126,10 +130,19 @@ public class SQLSikevaDB implements SikevaDB /** * Activate archiver. + * + * @throws ClosedDatabaseException */ - public void activateArchiver() + public void activateArchiver() throws ClosedDatabaseException { - this.archiver.activate(); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + this.archiver.activate(); + } } /* (non-Javadoc) @@ -140,8 +153,16 @@ public class SQLSikevaDB implements SikevaDB { Archiver result; - result = this.archiver; + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = this.archiver; + } + // return result; } @@ -151,7 +172,11 @@ public class SQLSikevaDB implements SikevaDB @Override public void clear() throws SikevaDBException { - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { Connection connection = null; PreparedStatement statement = null; @@ -173,10 +198,6 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } } /* (non-Javadoc) @@ -268,7 +289,11 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { Connection connection = null; Statement statement = null; @@ -293,10 +318,6 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -310,42 +331,39 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + 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); + } } // @@ -360,7 +378,14 @@ public class SQLSikevaDB implements SikevaDB { long result; - result = countOfElements(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = countOfElements(key, String.valueOf(subkey)); + } // return result; @@ -374,47 +399,44 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + 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); + } } // @@ -427,12 +449,15 @@ public class SQLSikevaDB implements SikevaDB @Override public void create() throws SikevaDBException { - if (this.status == Status.CLOSED) + if (isOpened()) { - throw new SikevaDBException("Invalid state."); + throw new OpenedDatabaseException(); } else { + // + open(); + // Connection connection = null; Statement statement = null; @@ -475,6 +500,9 @@ public class SQLSikevaDB implements SikevaDB System.out.println("============================== APRÈS2"); } + + // + close(); } catch (SQLException exception) { @@ -499,56 +527,53 @@ public class SQLSikevaDB implements SikevaDB @Override public void delete(final String key) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - 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("Delete action without existing target [key=" + key + "]"); - } - } - catch (SQLException exception) - { - logger.error("Error deleting element.", exception); - try - { - connection.rollback(); - } - catch (SQLException exception1) - { - logger.error("Rollback failed.", exception); - } - throw new SikevaDBException("Error deleting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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("Delete action without existing target [key=" + key + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error deleting element.", exception); + try + { + connection.rollback(); + } + catch (SQLException exception1) + { + logger.error("Rollback failed.", exception); + } + throw new SikevaDBException("Error deleting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } } @@ -558,7 +583,14 @@ public class SQLSikevaDB implements SikevaDB @Override public void delete(final String key, final long subkey) throws SikevaDBException { - delete(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + delete(key, String.valueOf(subkey)); + } } /* (non-Javadoc) @@ -567,52 +599,49 @@ public class SQLSikevaDB implements SikevaDB @Override public void delete(final String key, final String subkey) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - 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("Delete action without existing target [key=" + key + "][subkey=" + subkey + "]"); - } - } - catch (SQLException exception) - { - logger.error("Error deleting subkey.", exception); - throw new SikevaDBException("Error deleting subkey", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); } else { - throw new SikevaDBException("Invalid database status (opened)."); + // + 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("Delete action without existing target [key=" + key + "][subkey=" + subkey + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error deleting subkey.", exception); + throw new SikevaDBException("Error deleting subkey", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } } @@ -622,56 +651,53 @@ public class SQLSikevaDB implements SikevaDB @Override public void deleteMany(final String key, final String... subkeys) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - if (key == null) - { - throw new IllegalArgumentException("Key is null."); - } - else if ((subkeys != null) && (subkeys.length > 0)) + throw new ClosedDatabaseException(); + } + else 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 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++) { - // - 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("Delete action without existing target [key=" + key + "][subkeys=" + new StringList(subkeys).toStringWithCommas() + "]"); - } + statement.setString(2 + index, subkeys[index]); } - catch (SQLException exception) + + // + int rowCount = statement.executeUpdate(); + if (rowCount == 0) { - logger.error("Error deleting subkeys.", exception); - throw new SikevaDBException("Error deleting subkeys", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + logger.warn("Delete action without existing target [key=" + key + "][subkeys=" + new StringList(subkeys).toStringWithCommas() + "]"); } } - } - else - { - throw new SikevaDBException("Invalid database status (closed)."); + catch (SQLException exception) + { + logger.error("Error deleting subkeys.", exception); + throw new SikevaDBException("Error deleting subkeys", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } } @@ -681,7 +707,14 @@ public class SQLSikevaDB implements SikevaDB @Override public void destroy() throws SikevaDBException { - // TODO + if (isOpened()) + { + throw new OpenedDatabaseException(); + } + else + { + // TODO + } } /* (non-Javadoc) @@ -712,17 +745,24 @@ public class SQLSikevaDB implements SikevaDB { boolean result; - if (element == null) + if (isClosed()) { - result = false; - } - else if (getElement(element.getId()) == null) - { - result = false; + throw new ClosedDatabaseException(); } else { - result = true; + if (element == null) + { + result = false; + } + else if (getElement(element.getId()) == null) + { + result = false; + } + else + { + result = true; + } } // @@ -758,7 +798,7 @@ public class SQLSikevaDB implements SikevaDB } else { - throw new NullPointerException("Connection is not initialized."); + throw new IllegalArgumentException("Connection not initialized."); } // @@ -808,67 +848,64 @@ public class SQLSikevaDB implements SikevaDB { Element result; - if (isOpened()) + if (isClosed()) { - if (id == Element.NO_ID) + throw new ClosedDatabaseException(); + } + else if (id == Element.NO_ID) + { + result = null; + } + else + { + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { - result = null; - } - else - { - 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 ID=?"); + statement.setLong(1, id); + resultSet = statement.executeQuery(); + + // + if (resultSet.next()) { // - 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(); + 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()) { - // - 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; + throw new SikevaDBException("More than only once result [id=" + id + "]."); } } - catch (SQLException exception) + else { - logger.error("Error getting element.", exception); - throw new SikevaDBException("Error getting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + result = null; } } - } - else - { - throw new SikevaDBException("Invalid database status (closed)."); + catch (SQLException exception) + { + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } // @@ -883,68 +920,65 @@ public class SQLSikevaDB implements SikevaDB { Element result; - if (isOpened()) + if (isClosed()) { - if (key == null) + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else + { + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try { - 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 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 = 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(); + 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()) { - // - 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; + throw new SikevaDBException("More than only once result."); } } - catch (SQLException exception) + else { - logger.error("Error getting element.", exception); - throw new SikevaDBException("Error getting element", exception); - } - finally - { - closeQuietly(connection, statement, resultSet); + result = null; } } - } - else - { - throw new SikevaDBException("Invalid database status (closed)."); + catch (SQLException exception) + { + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } // @@ -959,7 +993,14 @@ public class SQLSikevaDB implements SikevaDB { Element result; - result = getElement(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = getElement(key, String.valueOf(subkey)); + } // return result; @@ -973,74 +1014,71 @@ public class SQLSikevaDB implements SikevaDB { Element result; - if (isOpened()) + if (isClosed()) { - if (key == null) + throw new ClosedDatabaseException(); + } + else 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 { - 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 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 = 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(); + 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()) { - // - 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; + 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); - } } - } - else - { - throw new SikevaDBException("Invalid database status (closed)."); + catch (SQLException exception) + { + logger.error("Error getting element.", exception); + throw new SikevaDBException("Error getting element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } // @@ -1055,7 +1093,11 @@ public class SQLSikevaDB implements SikevaDB { Elements result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { // result = new Elements((int) countOfElements()); @@ -1101,10 +1143,6 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -1118,63 +1156,60 @@ public class SQLSikevaDB implements SikevaDB { Elements result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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); + } } // @@ -1189,7 +1224,11 @@ public class SQLSikevaDB implements SikevaDB { StringList result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { // result = new StringList(); @@ -1220,10 +1259,6 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -1267,49 +1302,46 @@ public class SQLSikevaDB implements SikevaDB { StringList result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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); + } } // @@ -1339,56 +1371,53 @@ public class SQLSikevaDB implements SikevaDB { String result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + 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); + } } // @@ -1403,7 +1432,14 @@ public class SQLSikevaDB implements SikevaDB { String result; - result = getValue(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = getValue(key, String.valueOf(subkey)); + } // return result; @@ -1417,61 +1453,58 @@ public class SQLSikevaDB implements SikevaDB { String result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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); + } } // @@ -1486,7 +1519,11 @@ public class SQLSikevaDB implements SikevaDB { StringList result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { // result = new StringList((int) countOfElements(key)); @@ -1518,10 +1555,6 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -1535,7 +1568,7 @@ public class SQLSikevaDB implements SikevaDB { boolean result; - if (this.status == Status.OPENED) + if (isOpened()) { result = this.archiver.isActivated(); } @@ -1605,7 +1638,11 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { // Connection connection = null; @@ -1631,10 +1668,6 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } // return result; @@ -1648,42 +1681,39 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + if (key == null) + { + throw new IllegalArgumentException("Key is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + 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); + } } // @@ -1698,7 +1728,14 @@ public class SQLSikevaDB implements SikevaDB { long result; - result = memorySize(key, String.valueOf(subkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + result = memorySize(key, String.valueOf(subkey)); + } // return result; @@ -1712,48 +1749,45 @@ public class SQLSikevaDB implements SikevaDB { long result; - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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); + } } // @@ -1827,125 +1861,121 @@ public class SQLSikevaDB implements SikevaDB @Override public void put(final Element element) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - if (element == null) - { - throw new IllegalArgumentException("element is null."); - } - else - { - if (exists(element)) - { - // - 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); - } - } - else - { - // - 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); - } - } - } + throw new ClosedDatabaseException(); + } + else if (element == null) + { + throw new IllegalArgumentException("element is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + if (exists(element)) + { + // + 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); + } + } + else + { + // + 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); + } + } } } @@ -1955,7 +1985,14 @@ public class SQLSikevaDB implements SikevaDB @Override public void put(final String key, final long subkey, final String value) throws SikevaDBException { - put(key, String.valueOf(subkey), String.valueOf(value)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + put(key, String.valueOf(subkey), String.valueOf(value)); + } } /* (non-Javadoc) @@ -1964,7 +2001,11 @@ public class SQLSikevaDB implements SikevaDB @Override public void put(final String key, final String value) throws SikevaDBException { - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { // Element element = getElement(key); @@ -2039,10 +2080,6 @@ public class SQLSikevaDB implements SikevaDB closeQuietly(connection, statement, resultSet); } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } } /* (non-Javadoc) @@ -2051,97 +2088,94 @@ public class SQLSikevaDB implements SikevaDB @Override public void put(final String key, final String subkey, final String value) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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); + } } } @@ -2153,48 +2187,45 @@ public class SQLSikevaDB implements SikevaDB { logger.info("renameKey starting... [{}][{}]", oldKey, newKey); - if (isOpened()) + if (isClosed()) { - 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); - } - } + throw new ClosedDatabaseException(); + } + else if (oldKey == null) + { + throw new IllegalArgumentException("OldKey is null."); + } + else if (newKey == null) + { + throw new IllegalArgumentException("NewKey is null."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // + 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); + } } logger.info("renameKey done."); @@ -2206,7 +2237,14 @@ public class SQLSikevaDB implements SikevaDB @Override public void renameSubkey(final String key, final long oldSubkey, final long newSubkey) throws SikevaDBException { - renameSubkey(key, String.valueOf(oldSubkey), String.valueOf(newSubkey)); + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else + { + renameSubkey(key, String.valueOf(oldSubkey), String.valueOf(newSubkey)); + } } /* (non-Javadoc) @@ -2215,20 +2253,17 @@ public class SQLSikevaDB implements SikevaDB @Override public void renameSubkey(final String key, final String oldKey, final String newKey) throws SikevaDBException { - if (isOpened()) + if (isClosed()) { - 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 - } + throw new ClosedDatabaseException(); + } + else if ((key == null) || (oldKey == null) || (newKey == null)) + { + throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "]."); } else { - throw new SikevaDBException("Invalid database status (closed)."); + // TODO Auto-generated method stub } } @@ -2240,7 +2275,11 @@ public class SQLSikevaDB implements SikevaDB { logger.info("replaceInValue starting... [{}]", key); - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { // String value = getValue(key); @@ -2254,10 +2293,6 @@ public class SQLSikevaDB implements SikevaDB // put(key, value); } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } logger.info("replaceInValue done."); } @@ -2270,7 +2305,11 @@ public class SQLSikevaDB implements SikevaDB { logger.info("replaceInValues starting... [{}]", key); - if (isOpened()) + if (isClosed()) + { + throw new ClosedDatabaseException(); + } + else { Elements elements = getElements(key); @@ -2298,10 +2337,6 @@ public class SQLSikevaDB implements SikevaDB } } } - else - { - throw new SikevaDBException("Invalid database status (closed)."); - } logger.info("replaceInValues done."); } diff --git a/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java b/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java index c4653fa..bb94a21 100644 --- a/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java @@ -701,7 +701,7 @@ public class SQLSikevaDBTest // "12345678"); database = new SQLSikevaDB("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:sikevadb-unittest;sql.syntax_mys=true", "sa", ""); - database.open(); database.create(); + database.open(); } }