Add API version to NamedVCLoader (#806)

The API version is static, set to 1.1 at Initializator. This requires
renaming collection to corpus in the named-vc files.

Change-Id: I04089afb385830acf2249e5981b5f1e455cc9c5f
diff --git a/Changes b/Changes
index d2eeb46..edd54e1 100644
--- a/Changes
+++ b/Changes
@@ -12,6 +12,7 @@
 - Update Query QuerySerializer with API version in the test suite (#806)
 - Add API version to KoralCollectionQueryBuilder (#806)
 - Clean up collection node from Krill response (#806)
+- Add API version to NamedVCLoader (#806)
 
 # version 0.79.1
 
diff --git a/src/main/java/de/ids_mannheim/korap/init/Initializator.java b/src/main/java/de/ids_mannheim/korap/init/Initializator.java
index 586f275..e2fddc3 100644
--- a/src/main/java/de/ids_mannheim/korap/init/Initializator.java
+++ b/src/main/java/de/ids_mannheim/korap/init/Initializator.java
@@ -44,6 +44,8 @@
     private OAuth2InitClientService clientService;
     @Autowired
     private QueryService queryService;
+    
+    private double apiVersion = 1.1;
 
     public Initializator () {}
 
@@ -58,6 +60,7 @@
                     OAuth2InitClientService.OUTPUT_FILENAME);
         }
 
+        vcLoader.apiVersion = apiVersion;
         Thread t = new Thread(vcLoader);
         t.start();
     }
@@ -70,6 +73,7 @@
 		if (config.createInitialSuperClient()) {
 			clientService.createInitialTestSuperClient();
 		}
+		vcLoader.apiVersion = apiVersion;
 		vcLoader.loadVCToCache("system-vc", "/vc/system-vc.jsonld");
 		adminDao.addAccount(new KorAPUser("admin"));
 		
@@ -79,7 +83,8 @@
 		q.setQuery("[]");
 		q.setDescription("\"system\" query");
 		q.setQueryType(QueryType.QUERY);
-		queryService.handlePutRequest("system", "system", "system-q", q, 1.1);
+		queryService.handlePutRequest("system", "system", "system-q", q, 
+				apiVersion);
 	}
 
     public void initResourceTest () throws IOException, KustvaktException {
diff --git a/src/main/java/de/ids_mannheim/korap/init/NamedVCLoader.java b/src/main/java/de/ids_mannheim/korap/init/NamedVCLoader.java
index 294c7f8..55c10f6 100644
--- a/src/main/java/de/ids_mannheim/korap/init/NamedVCLoader.java
+++ b/src/main/java/de/ids_mannheim/korap/init/NamedVCLoader.java
@@ -54,6 +54,8 @@
 
     public static Logger jlog = LogManager.getLogger(NamedVCLoader.class);
     public static boolean DEBUG = false;
+    
+    public double apiVersion;
 
     @Override
     public void run () {
@@ -69,11 +71,11 @@
         }
     }
 
-    public void loadVCToCache (String filename, String filePath)
-            throws IOException, QueryException, KustvaktException {
-        loadVCToCache(filename,filePath,null);
-    }
-    
+	public void loadVCToCache (String filename, String filePath)
+			throws IOException, QueryException, KustvaktException {
+		loadVCToCache(filename, filePath, null);
+	}
+
     /**
      * Used for testing
      * 
@@ -83,14 +85,14 @@
      * @throws QueryException
      * @throws KustvaktException
      */
-    public void loadVCToCache (String filename, String filePath, String json)
-            throws IOException, QueryException {
+    public void loadVCToCache (String filename, String filePath, String json) 
+    		throws IOException, QueryException {
 
         if (json==null || json.isEmpty()) {
             InputStream is = NamedVCLoader.class.getResourceAsStream(filePath);
             json = IOUtils.toString(is, "utf-8");
         }
-        processVC(filename, json);
+        processVC(filename, json, apiVersion);
     }
 
     public void loadVCToCache () throws IOException, QueryException {
@@ -116,7 +118,7 @@
             filename = strArr[0];
             String json = strArr[1];
             if (json != null) {
-                processVC(filename, json);
+                processVC(filename, json, this.apiVersion);
             }
         }
     }
@@ -142,7 +144,7 @@
      * @throws IOException
      * @throws QueryException
      */
-    private void processVC (String vcId, String json)
+    private void processVC (String vcId, String json, double apiVersion)
             throws IOException, QueryException {
         boolean updateCache = false;
         try {
@@ -155,13 +157,13 @@
             if (json.hashCode() != koralQuery.hashCode()) {
                 updateCache = true;
                 // updateVCinDB
-                storeVCinDB(vcId, json, existingVC);
+                storeVCinDB(vcId, json, existingVC, apiVersion);
             }
         }
         catch (KustvaktException e) {
             // VC doesn't exist in the DB
             if (e.getStatusCode() == StatusCodes.NO_RESOURCE_FOUND) {
-                storeVCinDB(vcId, json, null);
+                storeVCinDB(vcId, json, null, apiVersion);
             }
             else {
                 throw new RuntimeException(e);
@@ -244,14 +246,15 @@
      * @param vcId
      * @param koralQuery
      */
-    private void storeVCinDB (String vcId, String koralQuery, QueryDO existingVC) {
+    private void storeVCinDB (String vcId, String koralQuery, 
+    		QueryDO existingVC, double apiVersion) {
         try {
             String info = (existingVC == null) ? "Storing" : "Updating";
             jlog.info("{} {} in the database ", info, vcId);
             
             vcService.storeQuery(existingVC, "system", vcId, ResourceType.SYSTEM,
                     QueryType.VIRTUAL_CORPUS, koralQuery, null, null, null,
-                    true, "system", null, null);
+                    true, "system", null, null, apiVersion);
         }
         catch (Exception e) {
             jlog.error("Failed storing VC: "+vcId, e);