Handled storing cached VC with VC reference

Change-Id: Ic0ad1384acb6ce593c9f06ee3c9ee22627680790
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