Added foundry rewrite with user default setting.

Change-Id: Ibc09468b6851af359eaf18df140fc973ab9dce69
diff --git a/core/Changes b/core/Changes
index 5b04a5e..4aaa3b2 100644
--- a/core/Changes
+++ b/core/Changes
@@ -18,7 +18,8 @@
    - Added create, edit, retrieve user default setting controllers (margaretha)
 24/01/2019
    - Added default setting key validation & fixed UserdataTest (margaretha)   
-
+28/01/2019
+   - Updated Rewrite handler (margaretha)
 
 version 0.61.4
 14/11/2018
diff --git a/core/src/main/java/de/ids_mannheim/korap/config/Attributes.java b/core/src/main/java/de/ids_mannheim/korap/config/Attributes.java
index dd54f28..2ad58cc 100644
--- a/core/src/main/java/de/ids_mannheim/korap/config/Attributes.java
+++ b/core/src/main/java/de/ids_mannheim/korap/config/Attributes.java
@@ -144,10 +144,11 @@
     /**
      * default layers
      */
-    public static final String DEFAULT_POS_FOUNDRY = "POSFoundry";
-    public static final String DEFAULT_LEMMA_FOUNDRY = "lemmaFoundry";
-    public static final String DEFAULT_CONST_FOUNDRY = "constFoundry";
-    public static final String DEFAULT_REL_FOUNDRY = "relFoundry";
+    public static final String DEFAULT_FOUNDRY_POS = "pos-foundry";
+    public static final String DEFAULT_FOUNDRY_LEMMA = "lemma-foundry";
+    public static final String DEFAULT_FOUNDRY_CONSTITUENT = "constituent-foundry";
+    public static final String DEFAULT_FOUNDRY_RELATION = "relation-foundry";
+    public static final String DEFAULT_FOUNDRY_MORPHOLOGY = "morphology-foundry";
 
     /**
      * db column keys
diff --git a/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java b/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
index 2d0d720..f4f107f 100644
--- a/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
+++ b/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
@@ -71,6 +71,7 @@
     private String passcodeSaltField;
 
     private String default_pos;
+    private String default_morphology;
     private String default_lemma;
     private String default_token;
     private String default_dep;
@@ -95,6 +96,7 @@
     // random code generator
     private String secureRandomAlgorithm;
     private String messageDigestAlgorithm;
+
     
     public KustvaktConfiguration (Properties properties) throws Exception {
         load(properties);
@@ -131,14 +133,15 @@
             queryLanguages.add(querylang.trim().toUpperCase());
 
         default_const =
-                properties.getProperty("default.layer.constituent", "mate");
+                properties.getProperty("default.foundry.constituent", "corenlp");
         default_dep =
-                properties.getProperty("default.layer.dependency", "mate");
-        default_lemma = properties.getProperty("default.layer.lemma", "tt");
+                properties.getProperty("default.foundry.dependency", "malt");
+        default_lemma = properties.getProperty("default.foundry.lemma", "tt");
+        default_morphology = properties.getProperty("default.foundry.morphology", "marmot");
         default_pos =
-                properties.getProperty("default.layer.partOfSpeech", "tt");
+                properties.getProperty("default.foundry.partOfSpeech", "tt");
         default_token =
-                properties.getProperty("default.layer.orthography", "opennlp");
+                properties.getProperty("default.foundry.orthography", "opennlp");
 
         // security configuration
         inactiveTime = TimeUtils.convertTimeToSeconds(
diff --git a/core/src/main/java/de/ids_mannheim/korap/rewrite/CollectionCleanRewrite.java b/core/src/main/java/de/ids_mannheim/korap/rewrite/CollectionCleanRewrite.java
index 6cacd4b..530eb86 100644
--- a/core/src/main/java/de/ids_mannheim/korap/rewrite/CollectionCleanRewrite.java
+++ b/core/src/main/java/de/ids_mannheim/korap/rewrite/CollectionCleanRewrite.java
@@ -1,17 +1,16 @@
 package de.ids_mannheim.korap.rewrite;
 
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import de.ids_mannheim.korap.config.BeanInjectable;
-import de.ids_mannheim.korap.config.ContextHolder;
-import de.ids_mannheim.korap.config.KustvaktConfiguration;
-import de.ids_mannheim.korap.user.User;
-import de.ids_mannheim.korap.utils.JsonUtils;
-import edu.emory.mathcs.backport.java.util.Arrays;
-
 import java.util.Iterator;
 
-/**
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.user.User;
+import edu.emory.mathcs.backport.java.util.Arrays;
+
+/** EM: not used anymore. This rewrite was to remove an empty koral:doc group in operands.
+ * 
  * @author hanl
  * @date 28/07/2015
  */
diff --git a/core/src/main/java/de/ids_mannheim/korap/rewrite/FoundryInject.java b/core/src/main/java/de/ids_mannheim/korap/rewrite/FoundryInject.java
index f682414..9146e5f 100644
--- a/core/src/main/java/de/ids_mannheim/korap/rewrite/FoundryInject.java
+++ b/core/src/main/java/de/ids_mannheim/korap/rewrite/FoundryInject.java
@@ -1,45 +1,28 @@
 package de.ids_mannheim.korap.rewrite;
 
-import java.util.Collection;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import com.fasterxml.jackson.databind.JsonNode;
 
-import de.ids_mannheim.korap.config.BeanInjectable;
-import de.ids_mannheim.korap.config.ContextHolder;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.rewrite.KoralNode.RewriteIdentifier;
 import de.ids_mannheim.korap.user.User;
-import edu.emory.mathcs.backport.java.util.Collections;
+import de.ids_mannheim.korap.user.UserSettingProcessor;
 
 /**
- * @author hanl
+ * @author hanl, margaretha
  * @date 30/06/2015
  */
-public class FoundryInject implements RewriteTask.IterableRewritePath,
-        BeanInjectable {
+public class FoundryInject implements RewriteTask.IterableRewritePath {
 
-    private Collection userdaos;
-
-
-    public FoundryInject () {
-        this.userdaos = Collections.emptyList();
-    }
-
-
+    @Autowired
+    protected LayerMapper mapper;
+    
     @Override
     public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
             User user) throws KustvaktException {
-        LayerMapper mapper;
-        // EM: do not use DB
-//        if (user != null && !userdaos.isEmpty()) {
-//            UserDataDbIface dao = BeansFactory.getTypeFactory()
-//                    .getTypeInterfaceBean(userdaos, UserSettings.class);
-//            mapper = new LayerMapper(config, dao.get(user));
-//        }
-//        else
-            mapper = new LayerMapper(config);
-            
+        
         if (node.get("@type").equals("koral:span")) {
             if (!node.isMissingNode("/wrap")){
                 node = node.at("/wrap");
@@ -49,18 +32,23 @@
         }
         else if (node.get("@type").equals("koral:term") && !node.has("foundry")) {
             String layer;
-            if (node.has("layer"))
+            if (node.has("layer")){
                 layer = node.get("layer");
-            else
+            }
+            else{
                 layer = node.get("key");
-            String foundry = mapper.findFoundry(layer);
+            }
+            UserSettingProcessor settingProcessor = null;
+            if (user!=null){
+                settingProcessor = user.getUserSettingProcessor();
+            }
+            String foundry = mapper.findFoundry(layer, settingProcessor);
             if (foundry != null)
                 node.put("foundry", foundry);
         }
         return node;
     }
 
-
     @Override
     public String path () {
         return "query";
@@ -71,10 +59,4 @@
     public JsonNode rewriteResult (KoralNode node) {
         return null;
     }
-
-
-    @Override
-    public <T extends ContextHolder> void insertBeans (T beans) {
-        this.userdaos = beans.getUserDataProviders();
-    }
 }
diff --git a/core/src/main/java/de/ids_mannheim/korap/rewrite/LayerMapper.java b/core/src/main/java/de/ids_mannheim/korap/rewrite/LayerMapper.java
index 672f7e7..a8a81bd 100644
--- a/core/src/main/java/de/ids_mannheim/korap/rewrite/LayerMapper.java
+++ b/core/src/main/java/de/ids_mannheim/korap/rewrite/LayerMapper.java
@@ -1,30 +1,30 @@
 package de.ids_mannheim.korap.rewrite;
 
-import de.ids_mannheim.korap.config.KustvaktConfiguration;
-import de.ids_mannheim.korap.config.Attributes;
-import de.ids_mannheim.korap.user.Userdata;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 
-/**
- * @author hanl
+import de.ids_mannheim.korap.config.Attributes;
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.user.UserSettingProcessor;
+
+/** EM:
+ *  <ul>
+ *  <li> Added default morphology foundry </li>
+ *  <li> Made this class as a spring component</li>
+ *  </ul>
+ * @author hanl, margaretha
  * @date 14/10/2014
  */
+@Component
 public class LayerMapper {
 
-    private Userdata settings;
+    @Autowired
     private KustvaktConfiguration config;
 
-
-    public LayerMapper (KustvaktConfiguration config, Userdata settings) {
-        this.settings = settings;
-        this.config = config;
+    public String findFoundry (String layer) {
+        return findFoundry(layer, null);
     }
 
-
-    public LayerMapper (KustvaktConfiguration config) {
-        this.config = config;
-    }
-
-
     /**
      * find foundry entry in settings specific settings. Includes a
      * call to #translateLayer to get the
@@ -35,23 +35,27 @@
      */
 
     //todo: make mapping configurable!
-    public String findFoundry (String layer) {
+    public String findFoundry (String layer, UserSettingProcessor settings) {
         if (settings != null) {
             switch (translateLayer(layer.toLowerCase().trim())) {
                 case "d":
                     return (String) settings
-                            .get(Attributes.DEFAULT_REL_FOUNDRY);
+                            .get(Attributes.DEFAULT_FOUNDRY_RELATION);
                 case "c":
                     return (String) settings
-                            .get(Attributes.DEFAULT_CONST_FOUNDRY);
+                            .get(Attributes.DEFAULT_FOUNDRY_CONSTITUENT);
                 case "pos":
                     return (String) settings
-                            .get(Attributes.DEFAULT_POS_FOUNDRY);
+                            .get(Attributes.DEFAULT_FOUNDRY_POS);
                 case "lemma":
                     return (String) settings
-                            .get(Attributes.DEFAULT_LEMMA_FOUNDRY);
+                            .get(Attributes.DEFAULT_FOUNDRY_LEMMA);
                 case "surface":
                     return "opennlp";
+                // EM: added
+                case "morphology":
+                    return (String) settings
+                            .get(Attributes.DEFAULT_FOUNDRY_MORPHOLOGY);    
                 default:
                     // if the layer is not in this specific listing, assume a default layer
                     // like orth or other tokenization layers
@@ -68,6 +72,8 @@
                     return config.getDefault_pos();
                 case "lemma":
                     return config.getDefault_lemma();
+                case "morphology":
+                    return config.getDefault_morphology();
                 case "surface":
                     return config.getDefault_token();
                     // refers to "structure" and is used for paragraphs or sentence boundaries
@@ -90,7 +96,8 @@
         //            case "lemma":
         //                return "l";
             case "m":
-                return "msd";
+                return "morphology"; // EM: changed msd to morphology
+//                return "msd";
                 //todo the orth layer does not need a foundry entry
             case "orth":
                 return "surface";
diff --git a/core/src/main/java/de/ids_mannheim/korap/rewrite/RewriteHandler.java b/core/src/main/java/de/ids_mannheim/korap/rewrite/RewriteHandler.java
index ddd7538..d674ba6 100644
--- a/core/src/main/java/de/ids_mannheim/korap/rewrite/RewriteHandler.java
+++ b/core/src/main/java/de/ids_mannheim/korap/rewrite/RewriteHandler.java
@@ -6,6 +6,7 @@
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -15,8 +16,6 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 
-import de.ids_mannheim.korap.config.BeanInjectable;
-import de.ids_mannheim.korap.config.ContextHolder;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.user.User;
@@ -39,21 +38,25 @@
     private Set<Class> failed_task_registration;
     @Autowired
     private KustvaktConfiguration config;
-    private ContextHolder beans;
 
-
+    public RewriteHandler (List<RewriteTask> rewriteTasks) {
+        this();
+        for (RewriteTask t : rewriteTasks){
+            addProcessor(t);
+        }
+    }
+    
+    // EM: for testing
     public RewriteHandler (KustvaktConfiguration config) {
         this();
-        this.config = config;
+        this.config=config;
     }
 
-
     public RewriteHandler () {
         this.node_processors = new HashSet<>();
         this.token_node_processors = new HashSet<>();
         this.query_processors = new LinkedHashSet<>();
         this.failed_task_registration = new HashSet<>();
-        this.beans = null;
     }
 
     public Set getFailedProcessors () {
@@ -99,6 +102,7 @@
      * @return boolean if rewriter class was successfully added to
      *         rewrite handler!
      */
+    @Deprecated
     public boolean add (Class<? extends RewriteTask> rewriter) {
         RewriteTask task;
         try {
@@ -244,10 +248,10 @@
                     jlog.debug("on processor: " + task.getClass().toString());
                 }
 
-                if (RewriteHandler.this.beans != null
-                        && task instanceof BeanInjectable)
-                    ((BeanInjectable) task)
-                            .insertBeans(RewriteHandler.this.beans);
+//                if (RewriteHandler.this.beans != null
+//                        && task instanceof BeanInjectable)
+//                    ((BeanInjectable) task)
+//                            .insertBeans(RewriteHandler.this.beans);
 
                 if (task instanceof RewriteTask.IterableRewritePath) {
                     RewriteTask.IterableRewritePath rw = (RewriteTask.IterableRewritePath) task;
@@ -302,7 +306,4 @@
 
     }
 
-    public void defaultRewriteConstraints () {
-        this.add(FoundryInject.class);
-    }
 }
diff --git a/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java b/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
index 10414bf..fa6a680 100644
--- a/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
+++ b/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
@@ -52,8 +52,6 @@
 
     @PostConstruct
     private void doPostConstruct () {
-        this.rewriteHandler.defaultRewriteConstraints();
-
         UriBuilder builder = UriBuilder.fromUri("http://10.0.10.13").port(9997);
         this.graphDBhandler = new ClientsHandler(builder.build());
     }
diff --git a/core/src/main/java/de/ids_mannheim/korap/user/KorAPUser.java b/core/src/main/java/de/ids_mannheim/korap/user/KorAPUser.java
index d25097f..8f51378 100644
--- a/core/src/main/java/de/ids_mannheim/korap/user/KorAPUser.java
+++ b/core/src/main/java/de/ids_mannheim/korap/user/KorAPUser.java
@@ -20,7 +20,7 @@
     private Long URIExpiration;
 
 
-    protected KorAPUser (String username) {
+    public KorAPUser (String username) {
         super(username, 0);
         this.URIFragment = "";
         this.URIExpiration = 0L;
diff --git a/core/src/main/java/de/ids_mannheim/korap/user/User.java b/core/src/main/java/de/ids_mannheim/korap/user/User.java
index 7aa4f57..eb6cf53 100644
--- a/core/src/main/java/de/ids_mannheim/korap/user/User.java
+++ b/core/src/main/java/de/ids_mannheim/korap/user/User.java
@@ -1,7 +1,6 @@
 package de.ids_mannheim.korap.user;
 
 import java.io.Serializable;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -46,7 +45,7 @@
     @Setter(AccessLevel.PRIVATE)
     private List<UserQuery> queries;
 
-    private List<Userdata> userdata;
+    private UserSettingProcessor userSettingProcessor;
 
 //    private boolean isSystemAdmin;
 
@@ -78,7 +77,6 @@
         this.isAccountLocked = false;
         this.username = "";
         this.id = -1;
-        this.userdata = new ArrayList<>();
         this.location 		= Location.EXTERN;
         this.corpusAccess 	= CorpusAccess.FREE;
     }
@@ -105,19 +103,6 @@
         return this.fields.get(cl);
     }
 
-
-    public void addUserData (Userdata data) {
-        if (data != null) {
-            for (Userdata d : this.userdata) {
-                // already has an object of that type!
-                if (d.getClass().equals(data.getClass()))
-                    return;
-            }
-            userdata.add(data);
-        }
-    }
-
-
     public void setId (Integer id) {
         this.id = id;
         //        if (this.settings != null)
diff --git a/core/src/main/java/de/ids_mannheim/korap/user/UserSettingProcessor.java b/core/src/main/java/de/ids_mannheim/korap/user/UserSettingProcessor.java
index df11e40..57b5e4b 100644
--- a/core/src/main/java/de/ids_mannheim/korap/user/UserSettingProcessor.java
+++ b/core/src/main/java/de/ids_mannheim/korap/user/UserSettingProcessor.java
@@ -30,10 +30,10 @@
 
     @Override
     public String[] defaultFields () {
-        return new String[] { Attributes.DEFAULT_REL_FOUNDRY,
-                Attributes.DEFAULT_POS_FOUNDRY,
-                Attributes.DEFAULT_CONST_FOUNDRY,
-                Attributes.DEFAULT_LEMMA_FOUNDRY, Attributes.QUERY_LANGUAGE,
+        return new String[] { Attributes.DEFAULT_FOUNDRY_RELATION,
+                Attributes.DEFAULT_FOUNDRY_POS,
+                Attributes.DEFAULT_FOUNDRY_CONSTITUENT,
+                Attributes.DEFAULT_FOUNDRY_LEMMA, Attributes.QUERY_LANGUAGE,
                 Attributes.PAGE_LENGTH };
     }
 }