HOT PATCH: Reorder tasks for new VC.

New VC should be cached first to avoid calculating leafs to compute
statistics when storing VC in NamedVCLoader. This allows using existing
vc-cache containing new vc.

Change-Id: I2ae463a7396962565fca16b81dd75c593d0ec297
diff --git a/Changes b/Changes
index d45c6c2..9a68158 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,11 @@
 # version 1.0
 
+HOT PATCH: Reorder tasks for new VC.
+New VC should be cached first to avoid calculating leafs to compute statistics 
+when storing VC in NamedVCLoader. This allows using existing vc-cache 
+containing new vc.
+
+
 # version 0.80.1-SNAPSHOT
 
 - Calculate and store statistics for named VC.
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 3be7984..b2a8eff 100644
--- a/src/main/java/de/ids_mannheim/korap/init/NamedVCLoader.java
+++ b/src/main/java/de/ids_mannheim/korap/init/NamedVCLoader.java
@@ -165,8 +165,10 @@
             			QueryType.VIRTUAL_CORPUS, apiVersion);
             	vcService.updateVCStatistics(existingVC,statistics);
             }
+            cacheVC(vcId, json, updateCache);
         }
         catch (KustvaktException e) {
+        	cacheVC(vcId, json, updateCache);
             // VC doesn't exist in the DB
             if (e.getStatusCode() == StatusCodes.NO_RESOURCE_FOUND) {
                 storeVCinDB(vcId, json, null, apiVersion);
@@ -174,9 +176,7 @@
             else {
                 throw new RuntimeException(e);
             }    
-        }
-        
-        cacheVC(vcId, json, updateCache);
+        }      
     }
 
     private String[] readFile (File file, String filename) throws IOException {
diff --git a/src/main/java/de/ids_mannheim/korap/service/QueryService.java b/src/main/java/de/ids_mannheim/korap/service/QueryService.java
index cd93de2..9def218 100644
--- a/src/main/java/de/ids_mannheim/korap/service/QueryService.java
+++ b/src/main/java/de/ids_mannheim/korap/service/QueryService.java
@@ -414,11 +414,8 @@
             jlog.debug("Storing query: " + queryName + "in the database ");
         }
 
-        String statistics = null;
-        if (queryType.equals(QueryType.VIRTUAL_CORPUS)) {
-        	statistics = krill.getStatistics(koralQuery); 
-        }
-        
+		String statistics = computeStatisticsForVC(queryType, isCached,
+				apiVersion, queryName, koralQuery);        
         int queryId = 0;
         try {
             if (existingQuery==null) {
@@ -452,6 +449,29 @@
         }
     }
     
+	private String computeStatisticsForVC (QueryType queryType, boolean isCached,
+			double apiVersion, String queryName, String koralQuery)
+			throws KustvaktException {
+        if (queryType.equals(QueryType.VIRTUAL_CORPUS)) {
+        	if (isCached) {
+                String kq = updateKoralQueryForCachedVC (apiVersion, queryName);
+                return krill.getStatistics(kq);
+            }
+        	else {
+        		return krill.getStatistics(koralQuery); 
+        	}
+        }
+        return null;
+	}
+    
+	private String updateKoralQueryForCachedVC (double apiVersion,
+			String queryName) throws KustvaktException {
+    	KoralCollectionQueryBuilder koral = 
+        		new KoralCollectionQueryBuilder(apiVersion);
+        koral.with("referTo " + queryName);
+        return koral.toJSON();
+	}
+    
 	public void updateVCStatistics (QueryDO existingQuery, String statistics)
 			throws KustvaktException {
 		queryDao.editQuery(existingQuery, null, null, null, null, null, null,
@@ -493,14 +513,10 @@
             String koralQuery, double apiVersion) throws KustvaktException {
 
         if (isCached) {
-            KoralCollectionQueryBuilder koral = 
-            		new KoralCollectionQueryBuilder(apiVersion);
-            koral.with("referTo " + name);
-            koralQuery = koral.toJSON();
+            koralQuery = updateKoralQueryForCachedVC (apiVersion, name);
             if (DEBUG) {
                 jlog.debug("Determine vc access with vc ref: " + koralQuery);
             }
-
         }
 
 		if (findDocWithLicense(koralQuery, config.getAllOnlyRegex(),