Handled storing cached VC with VC reference

Change-Id: Ic0ad1384acb6ce593c9f06ee3c9ee22627680790
diff --git a/core/src/main/java/de/ids_mannheim/korap/annotation/AnnotationParser.java b/core/src/main/java/de/ids_mannheim/korap/annotation/AnnotationParser.java
index b6fd448..5b9cd87 100644
--- a/core/src/main/java/de/ids_mannheim/korap/annotation/AnnotationParser.java
+++ b/core/src/main/java/de/ids_mannheim/korap/annotation/AnnotationParser.java
@@ -1,12 +1,9 @@
 package de.ids_mannheim.korap.annotation;
 
 import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.net.URL;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Set;
@@ -53,7 +50,7 @@
         if (resources.length < 1) return;
 
         for (Resource r : resources) {
-            log.debug(r.getFilename());
+//            log.debug(r.getFilename());
             readFile(r.getInputStream());
         }
     }
diff --git a/full/Changes b/full/Changes
index b8d1645..b111732 100644
--- a/full/Changes
+++ b/full/Changes
@@ -3,7 +3,10 @@
    - Integrated lite and full services and controllers in core (margaretha)
 21/11/2018
    - Updated OAuth2 refresh token request to create a new refresh token and 
-     revoke the old one per request (margaretha)   
+     revoke the old one per request (margaretha)
+28/11/2018
+   - Updated NamedVCLoader to delete existing VC in DB (margaretha)
+   - Handled storing cached VC with VC reference (margaretha)
    
 # version 0.61.3
 17/10/2018
diff --git a/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java b/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
index 38f0fc9..2b72e84 100644
--- a/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
+++ b/full/src/main/java/de/ids_mannheim/korap/config/NamedVCLoader.java
@@ -16,6 +16,7 @@
 
 import de.ids_mannheim.korap.KrillCollection;
 import de.ids_mannheim.korap.constant.VirtualCorpusType;
+import de.ids_mannheim.korap.entity.VirtualCorpus;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.service.VirtualCorpusService;
 import de.ids_mannheim.korap.util.QueryException;
@@ -49,7 +50,7 @@
 
         String dir = config.getNamedVCPath();
         if (dir.isEmpty()) return;
-        
+
         File d = new File(dir);
         if (!d.isDirectory()) {
             throw new IOException("Directory " + dir + " is not valid");
@@ -66,13 +67,25 @@
             String json = strArr[1];
             if (json != null) {
                 cacheVC(json, filename);
-                vcService.storeVC(filename, VirtualCorpusType.SYSTEM, json, null,
-                        null, null, true, "system");
+                try {
+                    VirtualCorpus vc = vcService.searchVCByName("system",
+                            filename, "system");
+                    if (vc != null) {
+                        jlog.debug("Delete existing vc: " + filename);
+                        vcService.deleteVC("system", vc.getId());
+                    }
+                }
+                catch (KustvaktException e) {
+                    // ignore
+                }
+                vcService.storeVC(filename, VirtualCorpusType.SYSTEM, json,
+                        null, null, null, true, "system");
             }
         }
     }
 
-    private String[] readFile (File file, String filename) throws IOException, KustvaktException {
+    private String[] readFile (File file, String filename)
+            throws IOException, KustvaktException {
         String json = null;
         long start = System.currentTimeMillis();
         if (filename.endsWith(".jsonld")) {
@@ -94,13 +107,13 @@
         }
         long end = System.currentTimeMillis();
         jlog.debug("READ " + filename + " duration: " + (end - start));
-        
-        return new String[]{filename, json};
+
+        return new String[] { filename, json };
     }
 
     private void cacheVC (String json, String filename)
             throws IOException, QueryException {
-        jlog.info("Create KrillCollection: "+filename);
+        jlog.info("Create KrillCollection: " + filename);
         long start, end;
         start = System.currentTimeMillis();
 
@@ -108,8 +121,8 @@
         jlog.debug("Finished creating KrillCollection");
         jlog.debug("Set Index to collection");
         collection.setIndex(searchKrill.getIndex());
-                
-        jlog.debug("StoreInCache "+filename);
+
+        jlog.debug("StoreInCache " + filename);
         if (collection != null) {
             collection.storeInCache(filename);
         }
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java b/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
index 8ed6c09..790e520 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/VirtualCorpusService.java
@@ -257,8 +257,18 @@
                     "Unauthorized operation for user: " + username, username);
         }
 
-        CorpusAccess requiredAccess = determineRequiredAccess(koralQuery);
-        jlog.debug("Storing VC");
+        CorpusAccess requiredAccess;
+        if (isCached){
+            KoralCollectionQueryBuilder koral = new KoralCollectionQueryBuilder();
+            koral.with("referTo "+name);
+            String vcRef = koral.toJSON();
+            jlog.debug("Determine vc access with vc ref: " + vcRef);
+            requiredAccess = determineRequiredAccess(vcRef);
+        }
+        else{
+            requiredAccess = determineRequiredAccess(koralQuery);
+        }
+        jlog.debug("Storing VC "+name);
         int vcId = 0;
         try {
             vcId = vcDao.createVirtualCorpus(name, type, requiredAccess,
diff --git a/full/src/main/resources/default-config.xml b/full/src/main/resources/default-config.xml
index 966eb66..d0c3576 100644
--- a/full/src/main/resources/default-config.xml
+++ b/full/src/main/resources/default-config.xml
@@ -38,7 +38,7 @@
 				<value>file:./jdbc.properties</value>
 				<value>classpath:properties/mail.properties</value>
 				<value>file:./mail.properties</value>
-				<value>classpath:properties/hibernate.properties</value>
+				<value>classpath:hibernate.properties</value>
 
 				<value>classpath:kustvakt.conf</value>
 				<value>file:./kustvakt.conf</value>
diff --git a/full/src/main/resources/properties/hibernate.properties b/full/src/main/resources/hibernate.properties
similarity index 100%
rename from full/src/main/resources/properties/hibernate.properties
rename to full/src/main/resources/hibernate.properties