]> git.mxchange.org Git - jcore.git/commitdiff
Added more data-gateway pattern classes + more rewrites for better usability in web...
authorRoland Haeder <roland@mxchange.org>
Wed, 12 Aug 2015 12:34:02 +0000 (14:34 +0200)
committerRoland Haeder <roland@mxchange.org>
Wed, 12 Aug 2015 12:34:02 +0000 (14:34 +0200)
- Added methods values() and entrySet() to Criteria instances
- Added method add() to Result instances
- Added method matchesAnd() which does a boolean check on all criteria on each Storeable instance
- Added DatabaseResult class with no implemented methods (will follow)
- Method doSelectByCriteria() in Base64 database backend is now "basicly finished"
- Added more thrown exceptions to not exit application servers on any thrown exception
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/jcore/criteria/BaseCriteria.java
src/org/mxchange/jcore/criteria/Criteria.java
src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java
src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java
src/org/mxchange/jcore/database/backend/DatabaseBackend.java
src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java
src/org/mxchange/jcore/database/result/DatabaseResult.java [new file with mode: 0644]
src/org/mxchange/jcore/database/result/Result.java

index 36028fc5ef3f8b12e99c7f0580f0d5e109b02875..395fabae61030e6529559f6dc70ddb585d85f48d 100644 (file)
@@ -18,6 +18,7 @@ package org.mxchange.jcore.criteria;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 import org.mxchange.jcore.BaseFrameworkSystem;
 
 /**
@@ -44,4 +45,26 @@ public class BaseCriteria extends BaseFrameworkSystem implements Criteria {
                // Add to map
                this.criteria.put(key, value);
        }
+
+       /**
+        * Gets all values from underlaying map in an iterator.
+        *
+        * @return Values iteratable
+        */
+       @Override
+       public Iterable<Object> values () {
+               // Call map's method
+               return this.criteria.values();
+               
+       }
+
+       /**
+        * Gets all entries as a key-value pair
+        *
+        * @return Key-value paira of all entries
+        */
+       @Override
+       public Set<Map.Entry<String, Object>> entrySet () {
+               return this.criteria.entrySet();
+       }
 }
index a2003ec80403fea5fc8da99d8a574d0ba7ec2a0b..0016a5057858e8eb00c3199607175d1c2564619b 100644 (file)
@@ -18,6 +18,8 @@ package org.mxchange.jcore.criteria;
  */
 
 
+import java.util.Map;
+import java.util.Set;
 import org.mxchange.jcore.FrameworkInterface;
 
 /**
@@ -33,4 +35,18 @@ public interface Criteria extends FrameworkInterface {
         * @param value Value of criteria
         */
        public void addCriteria (final String key, final boolean value);
+
+       /**
+        * Gets all values from underlaying map in an iterator.
+        *
+        * @return Values iteratable
+        */
+       public Iterable<Object> values ();
+
+       /**
+        * Gets all entries as a key-value pair
+        *
+        * @return Key-value paira of all entries
+        */
+       public Set<Map.Entry<String, Object>> entrySet ();
 }
index 6f85f442ff91974adfe292aff91c5fff25132383..96a637fa3d323cad6f97bb30a4c0da7a885c320a 100644 (file)
  */
 package org.mxchange.jcore.criteria.searchable;
 
+import java.text.MessageFormat;
+import java.util.Map;
 import org.mxchange.jcore.criteria.BaseCriteria;
+import org.mxchange.jcore.database.storage.Storeable;
 
 /**
  * A search criteria class
@@ -29,4 +32,31 @@ public class SearchCriteria extends BaseCriteria implements SearchableCritera {
         */
        public SearchCriteria () {
        }
+
+       @Override
+       public boolean matchesAnd (final Storeable storeable) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("storeable={0} - CALLED!", storeable));
+
+               // Must not be null
+               if (storeable == null) {
+                       // Abort here
+                       throw new NullPointerException("storeable is null");
+               }
+
+               // Default is matching
+               boolean matches = true;
+
+               // Check all conditions
+               for (final Map.Entry<String, Object> criteria : this.entrySet()) {
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("criteria: key={0},value={1}", criteria.getKey(), criteria.getValue()));
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("matches={0} - EXIT!", matches));
+
+               // Return result
+               return matches;
+       }
 }
index 5f85a3606c7578681fdb7f0a239ab8075373b536..2447e2b27e5ae88dbaa8c210610c5bad554e1b49 100644 (file)
@@ -1,6 +1,7 @@
 package org.mxchange.jcore.criteria.searchable;
 
 import org.mxchange.jcore.criteria.Criteria;
+import org.mxchange.jcore.database.storage.Storeable;
 
 /*
  * Copyright (C) 2015 Roland Haeder
@@ -26,4 +27,12 @@ import org.mxchange.jcore.criteria.Criteria;
  * @author Roland Haeder
  */
 public interface SearchableCritera extends Criteria {
+
+       /**
+        * Checks if the given instance of a Storeable class matchesAnd all criteria
+        *
+        * @param storeable A Storeable instance to check
+        * @return Whether the Storeable instance matchesAnd all criteria
+        */
+       public boolean matchesAnd (final Storeable storeable);
 }
index 9f0dbef5514c6b34088034aa310e92d53b86015f..0dd7dac728e13fe1a1d46b9fc482683ccaa94214 100644 (file)
@@ -22,6 +22,7 @@ import org.mxchange.jcore.FrameworkInterface;
 import org.mxchange.jcore.criteria.searchable.SearchableCritera;
 import org.mxchange.jcore.database.result.Result;
 import org.mxchange.jcore.database.storage.Storeable;
+import org.mxchange.jcore.exceptions.BadTokenException;
 
 /**
  * A generic interface for database frontends
@@ -44,8 +45,10 @@ public interface DatabaseBackend extends FrameworkInterface {
         *
         * @param critera Search critera
         * @return A result instance
+        * @throws java.io.IOException If any IO error occurs
+        * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
         */
-       public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera);
+       public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) throws IOException, BadTokenException;
 
        /**
         * Shuts down this backend
index 5a6076caf1f92c3f8c2d316487be438ad78e913d..a2bfb92113312d32940d8c35b39098278995f835 100644 (file)
  */
 package org.mxchange.jcore.database.backend.base64;
 
+import org.mxchange.jcore.database.result.DatabaseResult;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
-import java.sql.SQLException;
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.List;
@@ -90,20 +90,60 @@ public class Base64CsvDatabaseBackend extends BaseDatabaseBackend implements Dat
         * This database backend does not need to connect
         */
        @Override
-       public void connectToDatabase () throws SQLException {
+       public void connectToDatabase () {
                // Empty body
        }
 
+       /**
+        * Searches for given criteria over a file-based database. This method does
+        * always return a Result instance and never null.
+        *
+        * @param critera SearchableCriteria instance
+        * @return A Result instance for all matching Storeable instances
+        * @throws IOException
+        * @throws BadTokenException 
+        */
        @Override
-       public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) {
-               throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: criteria={0}", critera));
+       public Result<? extends Storeable> doSelectByCriteria (final SearchableCritera critera) throws IOException, BadTokenException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("criteria={0} - CALLED!", critera));
+
+               // Init result instance
+               Result<Storeable> result = new DatabaseResult();
+
+               // First rewind this backend
+               this.rewind();
+
+               // Then loop over all rows until the end has reached
+               while (this.isEndOfFile()) {
+                       // Read line
+                       String line = this.readLine();
+
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("line={0}", line));
+
+                       // Parse it to a Storeable instance
+                       Storeable storeable = this.getFrontend().parseLineToStoreable(line);
+
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("storeable={0}", storeable));
+
+                       // Now matchesAnd the found instance
+                       if (critera.matchesAnd(storeable)) {
+                               // Then add it to result
+                               result.add(storeable);
+                       }
+               }
+
+               // Return the result
+               return result;
        }
 
        /**
         * Shuts down this backend
         */
        @Override
-       public void doShutdown () throws SQLException, IOException {
+       public void doShutdown () throws IOException {
                // Trace message
                this.getLogger().trace("CALLED!"); //NOI18N
 
diff --git a/src/org/mxchange/jcore/database/result/DatabaseResult.java b/src/org/mxchange/jcore/database/result/DatabaseResult.java
new file mode 100644 (file)
index 0000000..df8386d
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcore.database.result;
+
+import java.util.Iterator;
+import org.mxchange.jcore.BaseFrameworkSystem;
+import org.mxchange.jcore.database.storage.Storeable;
+
+/**
+ * A database result
+ * @author Roland Haeder
+ */
+public class DatabaseResult extends BaseFrameworkSystem implements Result<Storeable> {
+       /**
+        * Default constructor
+        */
+       public DatabaseResult () {
+       }
+
+       /**
+        * Given Storeable instance as a query result.
+        *
+        * @param storeable An instance of a Storeable class
+        */
+       @Override
+       public void add (final Storeable storeable) {
+               throw new UnsupportedOperationException("Not supported yet: storeable=" + storeable); //To change body of generated methods, choose Tools | Templates.
+       }
+
+       @Override
+       public boolean hasNext () {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       }
+
+       @Override
+       public Iterator<Storeable> iterator () {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       }
+
+       @Override
+       public Storeable next () {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       }
+
+       @Override
+       public void remove () {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       }
+}
index 32b00dd4540db1aac431dc7db8d7fb93742a620b..0e5d9dae8221c0c4e7fac1844f22eb018ea64ff2 100644 (file)
@@ -27,4 +27,11 @@ import org.mxchange.jcore.database.storage.Storeable;
  * @param <T> Anything that is storeable
  */
 public interface Result<T extends Storeable> extends FrameworkInterface, Iterator<T>, Iterable<T> {
+
+       /**
+        * Given Storeable instance as a query result.
+        *
+        * @param storeable An instance of a Storeable class
+        */
+       public void add (final Storeable storeable);
 }