Added foundry rewrite with user default setting.

Change-Id: Ibc09468b6851af359eaf18df140fc973ab9dce69
diff --git a/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java b/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
index 0b5caba..1ce9893 100644
--- a/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
+++ b/full/src/main/java/de/ids_mannheim/korap/rewrite/CollectionConstraint.java
@@ -6,7 +6,9 @@
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.user.User;
 
-/**
+/** EM: not used anymore. This rewrite was meant to remove doc from 
+ * a collection by checking user access to the doc.  
+ * 
  * @author hanl
  * @date 03/07/2015
  */
diff --git a/full/src/main/java/de/ids_mannheim/korap/rewrite/FoundryRewrite.java b/full/src/main/java/de/ids_mannheim/korap/rewrite/FoundryRewrite.java
new file mode 100644
index 0000000..60beaaf
--- /dev/null
+++ b/full/src/main/java/de/ids_mannheim/korap/rewrite/FoundryRewrite.java
@@ -0,0 +1,33 @@
+package de.ids_mannheim.korap.rewrite;
+
+import org.springframework.beans.factory.annotation.Autowired;
+
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.service.DefaultSettingService;
+import de.ids_mannheim.korap.user.User;
+import de.ids_mannheim.korap.user.UserSettingProcessor;
+
+/**
+ * @author margaretha
+ *
+ */
+public class FoundryRewrite extends FoundryInject {
+
+    @Autowired
+    private DefaultSettingService settingService;
+
+    @Override
+    public KoralNode rewriteQuery (KoralNode node, KustvaktConfiguration config,
+            User user) throws KustvaktException {
+        String username = user.getUsername();
+        String jsonSettings =
+                settingService.retrieveDefaultSettings(username);
+        if (jsonSettings != null) {
+            UserSettingProcessor processor =
+                    new UserSettingProcessor(jsonSettings);
+            user.setUserSettingProcessor(processor);
+        }
+        return super.rewriteQuery(node, config, user);
+    }
+}
diff --git a/full/src/main/java/de/ids_mannheim/korap/rewrite/FullRewriteHandler.java b/full/src/main/java/de/ids_mannheim/korap/rewrite/FullRewriteHandler.java
deleted file mode 100644
index 092fe9e..0000000
--- a/full/src/main/java/de/ids_mannheim/korap/rewrite/FullRewriteHandler.java
+++ /dev/null
@@ -1,29 +0,0 @@
-package de.ids_mannheim.korap.rewrite;
-
-import org.springframework.beans.factory.annotation.Autowired;
-
-import de.ids_mannheim.korap.config.FullConfiguration;
-
-/**
- * Defines rewrite handling methods, relevant only in full version.
- * 
- * @author margaretha
- *
- */
-public class FullRewriteHandler extends RewriteHandler {
-
-    @Autowired
-    private VirtualCorpusRewrite vcRewrite;
-
-    public FullRewriteHandler (FullConfiguration config) {
-        super(config);
-    }
-
-    public void defaultRewriteConstraints () {
-        super.defaultRewriteConstraints();
-        this.addProcessor(vcRewrite);
-        this.add(CollectionRewrite.class);
-        // this.add(IdWriter.class);
-        this.add(CollectionCleanRewrite.class);
-    }
-}
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/DefaultSettingService.java b/full/src/main/java/de/ids_mannheim/korap/service/DefaultSettingService.java
index bd28723..d5f0b1e 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/DefaultSettingService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/DefaultSettingService.java
@@ -30,7 +30,7 @@
 
     private String verifiyUsername (String username, String contextUsername)
             throws KustvaktException {
-        username = username.substring(0);
+        username = username.substring(1);
         if (!username.equals(contextUsername)) {
             throw new KustvaktException(StatusCodes.INVALID_ARGUMENT,
                     "Username verification failed. Path parameter username "
@@ -93,10 +93,17 @@
             String contextUsername) throws KustvaktException {
 
         username = verifiyUsername(username, contextUsername);
+        return retrieveDefaultSettings(username);
+    }
+    
+    public String retrieveDefaultSettings (String username) throws KustvaktException {
         DefaultSetting defaultSetting =
                 settingDao.retrieveDefautlSetting(username);
+        if (defaultSetting == null) {
+            return null;
+        }
         return defaultSetting.getSettings();
-    }
+    }    
 
     public void deleteKey (String username, String contextUsername, String key)
             throws KustvaktException {
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/UserSettingController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/UserSettingController.java
index 5f24f00..4cb0cc9 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/UserSettingController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/UserSettingController.java
@@ -21,6 +21,7 @@
 
 import de.ids_mannheim.korap.constant.OAuth2Scope;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
 import de.ids_mannheim.korap.oauth2.service.OAuth2ScopeService;
 import de.ids_mannheim.korap.security.context.TokenContext;
 import de.ids_mannheim.korap.service.DefaultSettingService;
@@ -111,6 +112,11 @@
                     OAuth2Scope.READ_DEFAULT_SETTING);
             String settings = settingService.retrieveDefaultSettings(username,
                     tokenContext.getUsername());
+            if (settings == null){
+                username = tokenContext.getUsername();
+                throw new KustvaktException(StatusCodes.NO_RESULT_FOUND,
+                        "No setting is found for " + username, username);
+            }
             return Response.ok(settings).build();
         }
         catch (KustvaktException e) {
diff --git a/full/src/main/resources/db/predefined/V3.2__insert_predefined_roles.sql b/full/src/main/resources/db/predefined/V2.1__insert_predefined_roles.sql
similarity index 100%
rename from full/src/main/resources/db/predefined/V3.2__insert_predefined_roles.sql
rename to full/src/main/resources/db/predefined/V2.1__insert_predefined_roles.sql
diff --git a/full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql b/full/src/main/resources/db/test/V3.1__insert_virtual_corpus.sql
similarity index 100%
rename from full/src/main/resources/db/insert/V3.1__insert_virtual_corpus.sql
rename to full/src/main/resources/db/test/V3.1__insert_virtual_corpus.sql
diff --git a/full/src/main/resources/db/insert/V3.3__insert_member_roles.sql b/full/src/main/resources/db/test/V3.3__insert_member_roles.sql
similarity index 100%
rename from full/src/main/resources/db/insert/V3.3__insert_member_roles.sql
rename to full/src/main/resources/db/test/V3.3__insert_member_roles.sql
diff --git a/full/src/main/resources/db/insert/V3.4__insert_admins.sql b/full/src/main/resources/db/test/V3.4__insert_admins.sql
similarity index 100%
rename from full/src/main/resources/db/insert/V3.4__insert_admins.sql
rename to full/src/main/resources/db/test/V3.4__insert_admins.sql
diff --git a/full/src/main/resources/db/insert/V3.5__insert_oauth2_clients.sql b/full/src/main/resources/db/test/V3.5__insert_oauth2_clients.sql
similarity index 100%
rename from full/src/main/resources/db/insert/V3.5__insert_oauth2_clients.sql
rename to full/src/main/resources/db/test/V3.5__insert_oauth2_clients.sql
diff --git a/full/src/main/resources/db/test/V3.6__insert_default_settings.sql b/full/src/main/resources/db/test/V3.6__insert_default_settings.sql
new file mode 100644
index 0000000..5bd5264
--- /dev/null
+++ b/full/src/main/resources/db/test/V3.6__insert_default_settings.sql
@@ -0,0 +1,2 @@
+INSERT INTO default_setting(username,settings) 
+	VALUES ("bubbles",'{"pos-foundry":"corenlp", "lemma-foundry":"opennlp"}');
diff --git a/full/src/main/resources/default-config.xml b/full/src/main/resources/default-config.xml
index c8fd658..a3de4ac 100644
--- a/full/src/main/resources/default-config.xml
+++ b/full/src/main/resources/default-config.xml
@@ -215,8 +215,19 @@
 	</bean>
 	
 	<!-- Rewrite -->
-	<bean id="rewriteHandler" class="de.ids_mannheim.korap.rewrite.FullRewriteHandler">
-		<constructor-arg ref="kustvakt_config" />
+	<bean id="foundryRewrite" class="de.ids_mannheim.korap.rewrite.FoundryRewrite"/>
+	<bean id="collectionRewrite" class="de.ids_mannheim.korap.rewrite.CollectionRewrite"/>
+	<bean id="virtualCorpusRewrite" class="de.ids_mannheim.korap.rewrite.VirtualCorpusRewrite"/>
+	
+	<util:list id="rewriteTasks"
+		value-type="de.ids_mannheim.korap.rewrite.RewriteTask">
+		<ref bean="foundryRewrite" />
+		<ref bean="collectionRewrite" />
+		<ref bean="virtualCorpusRewrite" />
+	</util:list>
+	
+	<bean id="rewriteHandler" class="de.ids_mannheim.korap.rewrite.RewriteHandler">
+		<constructor-arg ref="rewriteTasks"/>
 	</bean>
 
 	<bean id="kustvakt_auditing" class="de.ids_mannheim.korap.handlers.JDBCAuditing">
diff --git a/full/src/main/resources/kustvakt.conf b/full/src/main/resources/kustvakt.conf
index a26cb2d..ec540f9 100644
--- a/full/src/main/resources/kustvakt.conf
+++ b/full/src/main/resources/kustvakt.conf
@@ -29,12 +29,13 @@
 ## mail.templates
 template.group.invitation = notification.vm
 
-## default layers
-default.layer.partOfSpeech = tt
-default.layer.lemma = tt
-default.layer.orthography = opennlp
-default.layer.dependency = mate
-default.layer.constituent = corenlp
+## default foundries for specific layers
+default.foundry.partOfSpeech = tt
+default.foundry.lemma = tt
+default.foundry.orthography = opennlp
+default.foundry.dependency = malt
+default.foundry.constituent = corenlp
+default.foundry.morphology = marmot
 
 ## delete configuration (default hard)
 # delete.auto.group = hard