docdao und post rewrite tests
diff --git a/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java b/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
index 7cd0ecf..01bf704 100644
--- a/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
+++ b/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
@@ -82,7 +82,7 @@
         maxhits = new Integer(properties.getProperty("maxhits", "50000"));
         returnhits = new Integer(properties.getProperty("returnhits", "50000"));
         indexDir = properties.getProperty("lucene.indexDir", "");
-        port = new Integer(properties.getProperty("server.port", "8080"));
+        port = new Integer(properties.getProperty("server.port", "8095"));
         // server options
         serverHost = String
                 .valueOf(properties.getProperty("server.host", "localhost"));
diff --git a/src/main/java/de/ids_mannheim/korap/handlers/DocumentDao.java b/src/main/java/de/ids_mannheim/korap/handlers/DocumentDao.java
index fe192db..99edf0e 100644
--- a/src/main/java/de/ids_mannheim/korap/handlers/DocumentDao.java
+++ b/src/main/java/de/ids_mannheim/korap/handlers/DocumentDao.java
@@ -41,20 +41,22 @@
     public Document findbyId(Integer id, User user) throws KustvaktException {
         MapSqlParameterSource s = new MapSqlParameterSource();
         s.addValue("id", id);
-        String sql = "select * from doc_store where id=:id";
+        String sql = "select id, persistent_id, disabled, strftime('%s', created) as created from doc_store where id=:id";
         try {
             return this.jdbcTemplate
                     .query(sql, s, new ResultSetExtractor<Document>() {
                         @Override
                         public Document extractData(ResultSet rs)
                                 throws SQLException, DataAccessException {
-                            Document doc = new Document(
-                                    rs.getString("persistent_id"));
-                            doc.setId(rs.getInt("id"));
-                            doc.setCreated(
-                                    rs.getTimestamp("created").getTime());
-                            doc.setDisabled(rs.getBoolean("disabled"));
-                            return doc;
+                            if (rs.isFirst()) {
+                                Document doc = new Document(
+                                        rs.getString("persistent_id"));
+                                doc.setId(rs.getInt("id"));
+                                doc.setCreated(rs.getLong("created"));
+                                doc.setDisabled(rs.getBoolean("disabled"));
+                                return doc;
+                            }
+                            return null;
                         }
                     });
         }catch (DataAccessException e) {
@@ -67,7 +69,7 @@
     public Document findbyId(String id, User user) throws KustvaktException {
         MapSqlParameterSource s = new MapSqlParameterSource();
         s.addValue("id", id);
-        String sql = "select id, persistent_id, strftime('%s', created) as created from doc_store where persistent_id=:id";
+        String sql = "select id, persistent_id, disabled, strftime('%s', created) as created from doc_store where persistent_id=:id";
 
         try {
             return this.jdbcTemplate
@@ -75,16 +77,18 @@
                         @Override
                         public Document extractData(ResultSet rs)
                                 throws SQLException, DataAccessException {
-                            Document doc = new Document(
-                                    rs.getString("persistent_id"));
-                            doc.setId(rs.getInt("id"));
-                            doc.setCreated(rs.getLong("created"));
-                            doc.setDisabled(rs.getBoolean("disabled"));
-                            return doc;
+                            if (!rs.isClosed()) {
+                                Document doc = new Document(
+                                        rs.getString("persistent_id"));
+                                doc.setId(rs.getInt("id"));
+                                doc.setCreated(rs.getLong("created"));
+                                doc.setDisabled(rs.getBoolean("disabled"));
+                                return doc;
+                            }
+                            return null;
                         }
                     });
         }catch (DataAccessException e) {
-            e.printStackTrace();
             throw new KustvaktException(StatusCodes.CONNECTION_ERROR);
         }
     }
@@ -124,12 +128,16 @@
                         @Override
                         public Document mapRow(ResultSet rs, int rowNum)
                                 throws SQLException {
-                            Document doc = new Document(
-                                    rs.getString("persistent_id"));
-                            doc.setId(rs.getInt("id"));
-                            doc.setCreated(rs.getLong("created"));
-                            doc.setDisabled(rs.getBoolean("disabled"));
-                            return doc;
+                            // todo: test on empty/closed resultset!
+                            if (!rs.isClosed()) {
+                                Document doc = new Document(
+                                        rs.getString("persistent_id"));
+                                doc.setId(rs.getInt("id"));
+                                doc.setCreated(rs.getLong("created"));
+                                doc.setDisabled(rs.getBoolean("disabled"));
+                                return doc;
+                            }
+                            return null;
                         }
                     });
         }catch (DataAccessException e) {
@@ -163,7 +171,6 @@
         try {
             return this.jdbcTemplate.update(sql, s);
         }catch (DataAccessException e) {
-            e.printStackTrace();
             throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT,
                     "illegal argument given", resource.getPersistentID());
         }
diff --git a/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java b/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
index cbc102d..bf3ab79 100644
--- a/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
+++ b/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
@@ -45,6 +45,9 @@
     @Override
     public UserSettings getUserSettings(Integer userid)
             throws KustvaktException {
+//        TransactionDefinition def = new DefaultTransactionDefinition();
+//        TransactionStatus status = transactionManager.getTransaction(def);
+
         MapSqlParameterSource np = new MapSqlParameterSource();
         np.addValue("us", userid);
         final String sql =
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/DocMatchRewrite.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/DocMatchRewrite.java
index 079af47..d5896e7 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/DocMatchRewrite.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/DocMatchRewrite.java
@@ -1,6 +1,13 @@
 package de.ids_mannheim.korap.resource.rewrite;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import de.ids_mannheim.korap.config.BeanConfiguration;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.handlers.DocumentDao;
+import de.ids_mannheim.korap.resources.Document;
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
 
 /**
  * @author hanl
@@ -8,15 +15,41 @@
  */
 public class DocMatchRewrite implements RewriteTask.RewriteAfter {
 
+    private DocumentDao docDao;
+    private Cache cache;
 
+    // todo: cache already matched documents with ehcache!
+
+    public DocMatchRewrite() {
+        this.docDao = new DocumentDao(
+                BeanConfiguration.getBeans().getPersistenceClient());
+        this.cache = CacheManager.getInstance().getCache("documents");
+    }
 
     @Override
     public JsonNode postProcess(KoralNode node) {
+        Document doc = null;
 
+        // todo: check matches for parent
+        if (node.has("docID")) {
+            String docID = node.get("docID");
+            Element e = this.cache.get(docID);
+            if (e == null) {
+                try {
+                    doc = docDao.findbyId(docID, null);
+                }catch (KustvaktException ex) {
+                    ex.printStackTrace();
+                    // todo: what to do here?!
+                }
 
+                if (doc != null)
+                    this.cache.put(new Element(docID, doc));
+            }else
+                doc = (Document) e.getObjectValue();
 
-
-
-        return null;
+            if (doc != null && doc.isDisabled())
+                node.removeNode();
+        }
+        return node.rawNode();
     }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
index 017de70..585d0bf 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
@@ -30,6 +30,7 @@
         };
     }
 
+    @Deprecated
     public boolean setNode(Object path) {
         JsonNode n = null;
         if (this.node.isObject() && this.node.has((String) path))
@@ -91,6 +92,10 @@
         return null;
     }
 
+    public JsonNode at(String name) {
+        return this.node.at(name);
+    }
+
     public boolean has(Object ident) {
         if (ident instanceof String)
             return this.node.has((String) ident);
@@ -99,6 +104,7 @@
         return false;
     }
 
+
     public JsonNode rawNode() {
         return this.node;
     }
@@ -107,7 +113,7 @@
         this.remove = true;
     }
 
-    public boolean toRemove() {
+    public boolean isRemove() {
         return this.remove;
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
index 49cef0c..8be0799 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
@@ -12,6 +12,7 @@
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.Set;
 
 /**
  * @author hanl
@@ -26,6 +27,8 @@
     private Collection<RewriteTask.RewriteKoralToken> token_node_processors;
     private Collection<RewriteTask.RewriteQuery> query_processors;
 
+    private Set<Class> failed_task_registration;
+
     private KustvaktConfiguration config;
 
     // fixme: make default constructor with configuration!
@@ -34,6 +37,7 @@
         this.node_processors = new HashSet<>();
         this.token_node_processors = new HashSet<>();
         this.query_processors = new HashSet<>();
+        this.failed_task_registration = new HashSet<>();
     }
 
     public boolean addProcessor(RewriteTask rewriter) {
@@ -46,12 +50,15 @@
         else if (rewriter instanceof RewriteTask.RewriteBefore
                 | rewriter instanceof RewriteTask.RewriteAfter)
             return this.node_processors.add(rewriter);
-        //        else if (rewriter instanceof RewriteTask.RewriteAfter)
-        //            return this.node_post_processors
-        //                    .add((RewriteTask.RewriteAfter) rewriter);
+
+        this.failed_task_registration.add(rewriter.getClass());
         return false;
     }
 
+    public final Collection<Class> getFailedHandlers() {
+        return this.failed_task_registration;
+    }
+
     @Override
     public String toString() {
         StringBuilder b = new StringBuilder();
@@ -78,6 +85,7 @@
             task = (RewriteTask) c.newInstance();
         }catch (NoSuchMethodException | InvocationTargetException
                 | IllegalAccessException | InstantiationException e) {
+            this.failed_task_registration.add(rewriter);
             return false;
         }
         return addProcessor(task);
@@ -111,13 +119,11 @@
                         this.node_processors, post);
             }
         }else if (root.isArray()) {
-            //todo: test!
             Iterator<JsonNode> it = root.elements();
             while (it.hasNext()) {
                 JsonNode n = it.next();
-                if (process(n, user, post)) {
+                if (process(n, user, post))
                     it.remove();
-                }
             }
         }
         return false;
@@ -168,10 +174,10 @@
                         .preProcess(node, this.config, user);
             if (post && task instanceof RewriteTask.RewriteAfter)
                 ((RewriteTask.RewriteAfter) task).postProcess(node);
-            if (node.toRemove())
+            if (node.isRemove())
                 break;
         }
-        return node.toRemove();
+        return node.isRemove();
     }
 
 }
diff --git a/src/main/java/de/ids_mannheim/korap/security/ac/ResourceHandler.java b/src/main/java/de/ids_mannheim/korap/security/ac/ResourceHandler.java
index 78d0a44..c3e3cee 100644
--- a/src/main/java/de/ids_mannheim/korap/security/ac/ResourceHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/security/ac/ResourceHandler.java
@@ -58,7 +58,6 @@
         } catch (EmptyResultException e) {
             throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED);
         }
-
         return p.getResource();
     }
 
diff --git a/src/main/resources/default-config.xml b/src/main/resources/default-config.xml
index 4117b76..3f9f6a6 100644
--- a/src/main/resources/default-config.xml
+++ b/src/main/resources/default-config.xml
@@ -61,20 +61,21 @@
     </bean>
 
     <!--class="org.apache.commons.dbcp2.BasicDataSource"-->
+    <!-- org.springframework.jdbc.datasource.SingleConnectionDataSource -->
     <bean id="dataSource"
-          class="org.springframework.jdbc.datasource.SingleConnectionDataSource"
+          class="org.apache.commons.dbcp2.BasicDataSource"
           lazy-init="true">
         <property name="driverClassName" value="${jdbc.driverClassName}"/>
         <property name="url" value="${jdbc.url}"/>
         <property name="username" value="${jdbc.username}"/>
         <property name="password" value="${jdbc.password}"/>
         <!-- relevant for single connection datasource and sqlite -->
-        <property name="suppressClose">
-            <value>true</value>
-        </property>
-        <!--<property name="initialSize" value="1"/>-->
-        <!--<property name="maxIdle" value="1"/>-->
-        <!--<property name="poolPreparedStatements" value="true"/>-->
+        <!--<property name="suppressClose">-->
+        <!--<value>true</value>-->
+        <!--</property>-->
+        <property name="initialSize" value="2"/>
+        <property name="maxIdle" value="2"/>
+        <property name="poolPreparedStatements" value="true"/>
     </bean>
 
     <!-- to configure database for sqlite, mysql, etc. migrations -->
diff --git a/src/main/resources/ehcache.xml b/src/main/resources/ehcache.xml
index 3265324..34f478c 100644
--- a/src/main/resources/ehcache.xml
+++ b/src/main/resources/ehcache.xml
@@ -2,6 +2,12 @@
          xsi:noNamespaceSchemaLocation='http://ehcache.org/ehcache.xsd'>
     <defaultCache eternal='true' overflowToDisk='false'/>
     <!--maxBytesLocalHeap="200M"-->
+    <cache name="documents"
+           timeToIdleSeconds="172800"
+           eternal='false'
+           memoryStoreEvictionPolicy="LRU"
+           maxEntriesLocalHeap="2000"
+           overflowToDisk='false'/>
     <cache name='users'
            timeToIdleSeconds="172800"
            eternal='false'