From: Roland Haeder Date: Wed, 12 Aug 2015 12:34:02 +0000 (+0200) Subject: Added more data-gateway pattern classes + more rewrites for better usability in web... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=68c06d090a52a3b44b809fed5d71ba2a22ff96cd;p=jcore.git Added more data-gateway pattern classes + more rewrites for better usability in web-based applications: - 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 --- diff --git a/src/org/mxchange/jcore/criteria/BaseCriteria.java b/src/org/mxchange/jcore/criteria/BaseCriteria.java index 36028fc..395faba 100644 --- a/src/org/mxchange/jcore/criteria/BaseCriteria.java +++ b/src/org/mxchange/jcore/criteria/BaseCriteria.java @@ -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 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> entrySet () { + return this.criteria.entrySet(); + } } diff --git a/src/org/mxchange/jcore/criteria/Criteria.java b/src/org/mxchange/jcore/criteria/Criteria.java index a2003ec..0016a50 100644 --- a/src/org/mxchange/jcore/criteria/Criteria.java +++ b/src/org/mxchange/jcore/criteria/Criteria.java @@ -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 values (); + + /** + * Gets all entries as a key-value pair + * + * @return Key-value paira of all entries + */ + public Set> entrySet (); } diff --git a/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java b/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java index 6f85f44..96a637f 100644 --- a/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java +++ b/src/org/mxchange/jcore/criteria/searchable/SearchCriteria.java @@ -16,7 +16,10 @@ */ 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 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; + } } diff --git a/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java b/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java index 5f85a36..2447e2b 100644 --- a/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java +++ b/src/org/mxchange/jcore/criteria/searchable/SearchableCritera.java @@ -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); } diff --git a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java index 9f0dbef..0dd7dac 100644 --- a/src/org/mxchange/jcore/database/backend/DatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/DatabaseBackend.java @@ -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 doSelectByCriteria (final SearchableCritera critera); + public Result doSelectByCriteria (final SearchableCritera critera) throws IOException, BadTokenException; /** * Shuts down this backend diff --git a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java index 5a6076c..a2bfb92 100644 --- a/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java +++ b/src/org/mxchange/jcore/database/backend/base64/Base64CsvDatabaseBackend.java @@ -16,10 +16,10 @@ */ 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 doSelectByCriteria (final SearchableCritera critera) { - throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: criteria={0}", critera)); + public Result doSelectByCriteria (final SearchableCritera critera) throws IOException, BadTokenException { + // Trace message + this.getLogger().trace(MessageFormat.format("criteria={0} - CALLED!", critera)); + + // Init result instance + Result 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 index 0000000..df8386d --- /dev/null +++ b/src/org/mxchange/jcore/database/result/DatabaseResult.java @@ -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 . + */ +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 { + /** + * 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 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. + } +} diff --git a/src/org/mxchange/jcore/database/result/Result.java b/src/org/mxchange/jcore/database/result/Result.java index 32b00dd..0e5d9da 100644 --- a/src/org/mxchange/jcore/database/result/Result.java +++ b/src/org/mxchange/jcore/database/result/Result.java @@ -27,4 +27,11 @@ import org.mxchange.jcore.database.storage.Storeable; * @param Anything that is storeable */ public interface Result extends FrameworkInterface, Iterator, Iterable { + + /** + * Given Storeable instance as a query result. + * + * @param storeable An instance of a Storeable class + */ + public void add (final Storeable storeable); }