From 68c06d090a52a3b44b809fed5d71ba2a22ff96cd Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Wed, 12 Aug 2015 14:34:02 +0200 Subject: [PATCH] =?utf8?q?Added=20more=20data-gateway=20pattern=20classes?= =?utf8?q?=20+=20more=20rewrites=20for=20better=20usability=20in=20web-bas?= =?utf8?q?ed=20applications:=20-=20Added=20methods=20values()=20and=20entr?= =?utf8?q?ySet()=20to=20Criteria=20instances=20-=20Added=20method=20add()?= =?utf8?q?=20to=20Result=20instances=20-=20Added=20method=20matchesAnd()?= =?utf8?q?=20which=20does=20a=20boolean=20check=20on=20all=20criteria=20on?= =?utf8?q?=20each=20Storeable=20instance=20-=20Added=20DatabaseResult=20cl?= =?utf8?q?ass=20with=20no=20implemented=20methods=20(will=20follow)=20-=20?= =?utf8?q?Method=20doSelectByCriteria()=20in=20Base64=20database=20backend?= =?utf8?q?=20is=20now=20"basicly=20finished"=20-=20Added=20more=20thrown?= =?utf8?q?=20exceptions=20to=20not=20exit=20application=20servers=20on=20a?= =?utf8?q?ny=20thrown=20exception=20Signed-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../mxchange/jcore/criteria/BaseCriteria.java | 23 +++++++ src/org/mxchange/jcore/criteria/Criteria.java | 16 +++++ .../criteria/searchable/SearchCriteria.java | 30 +++++++++ .../searchable/SearchableCritera.java | 9 +++ .../database/backend/DatabaseBackend.java | 5 +- .../base64/Base64CsvDatabaseBackend.java | 50 +++++++++++++-- .../jcore/database/result/DatabaseResult.java | 63 +++++++++++++++++++ .../jcore/database/result/Result.java | 7 +++ 8 files changed, 197 insertions(+), 6 deletions(-) create mode 100644 src/org/mxchange/jcore/database/result/DatabaseResult.java 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); } -- 2.39.5