<commit_before>package edu.cornell.mannlib.vitro.webapp.search.beans;

import javax.servlet.ServletContext;

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

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.shared.Lock;

import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;

public class ContextNodesInclusionFactory {

	private OntModel fullModel;
	private String contextNodeURI;
	private String query = "";
	
	private static final String queryForEducationalTraining = "SELECT ?query WHERE {" +
		"?searchConfig <"+ DisplayVocabulary.QUERY_FOR_EDUCATIONAL_TRAINING + "> ?query . }";				
				
	private static Log log = LogFactory.getLog(ContextNodesInclusionFactory.class);
	
	public ContextNodesInclusionFactory(String contextNodeURI,
			OntModel displayOntModel, ServletContext context) {
		this.fullModel = ModelContext.getJenaOntModel(context);
		this.contextNodeURI = contextNodeURI;
		query = getQueryFromModel(contextNodeURI, displayOntModel);
	}

	private String getQueryFromModel(String uri, OntModel displayOntModel) {
		
		String resultQuery = "";
		QuerySolutionMap initialBinding = new QuerySolutionMap();
		Resource searchConfig = ResourceFactory.createResource(uri);
		
		initialBinding.add("searchConfig", searchConfig);
		
		Query query = QueryFactory.create(queryForEducationalTraining);
		displayOntModel.enterCriticalSection(Lock.READ);
		try{
			QueryExecution qExec = QueryExecutionFactory.create(query, displayOntModel, initialBinding);
			try{
				ResultSet results = qExec.execSelect();
				while(results.hasNext()){
					QuerySolution soln = results.nextSolution();
					Literal node = soln.getLiteral("query");
					if(node.isLiteral()){
						resultQuery = node.toString();
					}else{
						log.warn("unexpected literal in the object position for context node queries " + node.toString());
					}
				} 
			}catch(Throwable t){
				log.error(t,t);
			} finally{
				qExec.close();
			} 
		}finally{
				displayOntModel.leaveCriticalSection();
		}
		
		return resultQuery.substring(0, resultQuery.length() - 3);
	}
	
	
//	public List<Field> getFieldValues(String uri, Model modelToQuery, List<String> queries){
		
		//what do the queries need to be like?<commit_msg>Fixing the code in ContextNodesInclusionFactory. getQueryFromModel() is commented out because there is nothing specified in search.n3 to be read into the model during the startup. Eventually, all the contextnode queries willl be extracted out into search.n3<commit_after>package edu.cornell.mannlib.vitro.webapp.search.beans;

import javax.servlet.ServletContext;

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

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.QuerySolutionMap;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.shared.Lock;

import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;

public class ContextNodesInclusionFactory {

	private OntModel fullModel;
//	private String contextNodeURI;
	//private String query = "";
	
//	private static final String queryForEducationalTraining = "SELECT ?query WHERE {" +
//		"?searchConfig <"+ DisplayVocabulary.QUERY_FOR_EDUCATIONAL_TRAINING + "> ?query . }";				
				
	private static Log log = LogFactory.getLog(ContextNodesInclusionFactory.class);
	
	public ContextNodesInclusionFactory(String contextNodeURI,
			OntModel displayOntModel, ServletContext context) {
		this.fullModel = ModelContext.getJenaOntModel(context);
//		this.contextNodeURI = contextNodeURI;
		//query = getQueryFromModel(contextNodeURI, displayOntModel);
	}
	
	/*
	 * bk392 : The original idea behind writing up this method was to check
	 * if I can read the queries from search.n3, write them to the displayOntModel(during startup) and 
	 * read them in this class.
	 * 
	 * Eventually, its going to be like that. All these hardcoded queries
	 * will go into search.n3 and will be written into the display model.
	 * ContextNodesInclusionFactors gets the queries out from the display Model
	 * and fires them, gets the values, concatenates them and passes them back to
	 * IndividualToSolrDoc.
	 */
//	private String getQueryFromModel(String uri, OntModel displayOntModel) {
//		
//		String resultQuery = "";
//		QuerySolutionMap initialBinding = new QuerySolutionMap();
//		Resource searchConfig = ResourceFactory.createResource(uri);
//		
//		initialBinding.add("searchConfig", searchConfig);
//		
//		Query query = QueryFactory.create(queryForEducationalTraining);
//		displayOntModel.enterCriticalSection(Lock.READ);
//		try{
//			QueryExecution qExec = QueryExecutionFactory.create(query, displayOntModel, initialBinding);
//			try{
//				ResultSet results = qExec.execSelect();
//				while(results.hasNext()){
//					QuerySolution soln = results.nextSolution();
//					Literal node = soln.getLiteral("query");
//					if(node.isLiteral()){
//						resultQuery = node.toString();
//					}else{
//						log.warn("unexpected literal in the object position for context node queries " + node.toString());
//					}
//				} 
//			}catch(Throwable t){
//				log.error(t,t);
//			} finally{
//				qExec.close();
//			} 
//		}finally{
//				displayOntModel.leaveCriticalSection();
//		}
//		
//		return resultQuery.substring(0, resultQuery.length() - 3);
//	}
	
	
//	public List<Field> getFieldValues(String uri, Model modelToQuery, List<String> queries){
		
		//what do the queries need to be like?
{'fraction_non_alphanumeric': 0.11409806018023522, 'fraction_numerical': 0.0016801588513823125, 'mean_word_length': 4.3409461663947795, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4895879', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2070, 'n_words': 436}