diff geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorBase.java @ 132:5a583cff97ea

Implementation of the Datainfrastructure for fetching Data from different DataStores. geo-backend/trunk@12 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 04 Sep 2009 08:11:30 +0000
parents
children 56655046194f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/QueryExecutorBase.java	Fri Sep 04 08:11:30 2009 +0000
@@ -0,0 +1,97 @@
+/**
+ *
+ */
+package de.intevation.gnv.geobackend.base.query;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import de.intevation.gnv.geobackend.base.DefaultResult;
+import de.intevation.gnv.geobackend.base.DefaultResultDescriptor;
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
+
+/**
+ * This is an abstract Basicimplementation of the Interface
+ * QueryExecutor providing several Helpermethods.
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public abstract class QueryExecutorBase implements QueryExecutor {
+
+    /**
+     * Constructor
+     */
+    public QueryExecutorBase() {
+        super();
+    }
+    
+    /**
+     * This Method puts the Filtervalues into the Querystring
+     * @param queryString the Query which should be manipulated
+     * @param filter the values which should be put into the Query
+     * @return the manipulated Query
+     */
+    protected String setFilterValues(String queryString, String[] filter){
+        String returnValue = queryString;
+        for (int i = 0; i < filter.length; i++){
+            returnValue = returnValue.replaceFirst("?", filter[i]);
+        }
+        return returnValue;
+    }
+    
+    /**
+     * This Methods converts the ResultSet into an Collection of Result-objects
+     * @param resultSet the ResultSet which should be converted
+     * @return an Collection containing Result-Objects
+     * @throws SQLException
+     */
+    protected Collection<Result> createResultCollection(ResultSet resultSet) throws SQLException{
+        Collection<Result> returnValue =  new ArrayList<Result>();
+        
+        ResultDescriptor resultDescriptor = null;
+        int columns = -1;
+        List<String> columnNames = null;
+        while (resultSet.next()){
+            if (resultDescriptor == null){
+                resultDescriptor = new DefaultResultDescriptor();
+                ResultSetMetaData rsmd = resultSet.getMetaData();
+                columns = rsmd.getColumnCount();
+                columnNames = new ArrayList<String>(columns);
+                for (int i = 1; i <= columns; i++){
+                    resultDescriptor.addColumn(rsmd.getColumnName(i), rsmd.getColumnClassName(i));
+                    columnNames.add(rsmd.getColumnName(i));
+                }
+            }
+            
+            Result result = convertResult(resultSet, resultDescriptor,columnNames);
+            
+            returnValue.add(result);
+        }
+        return returnValue;
+    }
+
+    /**
+     * This Method converts one Singel ResultSetEntry into an Result-Object
+     * @param resultSet the ResultSet where the Entry should be took from.
+     * @param resultDescriptor the ResultsetDescriptor which describes the Entry
+     * @param columnNames the Name of the Columns which Values should be fetched.
+     * @return an new Result-Objects containing the Values of the ResultSet-Entry
+     * @throws SQLException
+     */
+    private Result convertResult(ResultSet resultSet,
+            ResultDescriptor resultDescriptor, List<String> columnNames)
+            throws SQLException {
+        Result result = new DefaultResult(resultDescriptor);
+        for (int i = 0; i < columnNames.size(); i++){
+            String columnName = columnNames.get(i);
+            result.addColumnValue(columnName, resultSet.getObject(columnName));
+        }
+        return result;
+    }
+
+}

http://dive4elements.wald.intevation.org