/* $This file is distributed under the terms of the license in /doc/license.txt$ */

package edu.cornell.mannlib.vitro.webapp.modelaccess.ontmodels;

import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hp.hpl.jena.ontology.OntModel;

import edu.cornell.mannlib.vitro.webapp.utils.logging.ToString;

/**
 * Use two OntModelCaches as one.
 * 
 * If both caches contain models with the same name, a warning will be written
 * to the log, and the model from the primary cache will be used.
 * 
 * Any new models will be created on the primary cache.
 */
public class JoinedOntModelCache implements OntModelCache {
	private static final Log log = LogFactory.getLog(JoinedOntModelCache.class);

	private final OntModelCache primary;
	private final OntModelCache secondary;

	public JoinedOntModelCache(OntModelCache primary, OntModelCache secondary) {
		this.primary = primary;
		this.secondary = secondary;

// extremely expensive with TDB : see below
//		Set<String> duplicateNames = new HashSet<String>(primary.getModelNames());
//		duplicateNames.retainAll(secondary.getModelNames());
//		if (!duplicateNames.isEmpty()) {
//			log.warn("These model names appear in both caches: "
//					+ duplicateNames);
//		}
	}


    /*
	 * Local mod: Calling getModelNames() under TDB is very expensive
	 */
	@Override
	public OntModel getOntModel(String name) {
	    OntModel sec = secondary.getOntModel(name);
	    if(sec.isEmpty()) {
	        return primary.getOntModel(name);
	    } else {
	        OntModel pri = primary.getOntModel(name);
	        if(!pri.isEmpty()) {
	            return pri;
	        } else {
	            return sec;
	        }
	    }
		//if (primary.getModelNames().contains(name)) {
		//	return primary.getOntModel(name);
		//}
		//if (secondary.getModelNames().contains(name)) {
		//	return secondary.getOntModel(name);
		//}
		//return primary.getOntModel(name);
	}

	@Override
	public SortedSet<String> getModelNames() {
		SortedSet<String> allNames = new TreeSet<String>(primary.getModelNames());
		allNames.addAll(secondary.getModelNames());
		return allNames;
	}

	@Override
	public String toString() {
		return "JoinedOntModelCache[" + ToString.hashHex(this) + ", primary="
				+ primary + ", secondary=" + secondary + "]";
	}

}

@@ -375,16 +375,80 @@
 Model;%0A%0A
+import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelNames;%0A
 import e
@@ -1419,16 +1419,322 @@
 %09%09%7D%0A%09%7D%0A%0A
+    protected boolean isKnownSecondaryModel(String name) %7B%0A        return (ModelNames.USER_ACCOUNTS.equals(name)%0A                %7C%7C ModelNames.DISPLAY.equals(name) %0A                %7C%7C ModelNames.DISPLAY_DISPLAY.equals(name)%0A                %7C%7C ModelNames.DISPLAY_TBOX.equals(name)%0A                );%0A    %7D%0A%09
 %0A    /*%0A
@@ -1856,24 +1856,126 @@
 ing name) %7B%0A
+%09    if(isKnownSecondaryModel(name)) %7B%0A%09        return secondary.getOntModel(name);%0A%09    %7D else %7B%0A    
 %09    OntMode
@@ -2007,24 +2007,28 @@
 odel(name);%0A
+    
 %09    if(sec.
@@ -2032,32 +2032,36 @@
 ec.isEmpty()) %7B%0A
+    
 %09        return 
@@ -2079,32 +2079,36 @@
 OntModel(name);%0A
+    
 %09    %7D else %7B%0A%09 
@@ -2097,32 +2097,36 @@
   %09    %7D else %7B%0A
+    
 %09        OntMode
@@ -2156,24 +2156,28 @@
 odel(name);%0A
+    
 %09        if(
@@ -2190,24 +2190,28 @@
 sEmpty()) %7B%0A
+    
 %09           
@@ -2223,16 +2223,20 @@
 rn pri;%0A
+    
 %09       
@@ -2245,16 +2245,20 @@
  else %7B%0A
+    
 %09       
@@ -2274,16 +2274,20 @@
 rn sec;%0A
+    
 %09       
@@ -2293,15 +2293,48 @@
   %7D%0A
+    
 %09    %7D%0A
+%09    %7D%0A%09    //original logic%0A
 %09%09//

Fix bug where modified JoinedOntModelSelector behaved improperly on empty stores