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

package edu.cornell.mannlib.vitro.webapp.utils.jena;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletContext;

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

import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
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.Syntax;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.util.ResourceUtils;
import com.hp.hpl.jena.util.iterator.ClosableIterator;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;

import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;

public class InitialJenaModelUtils {

	private static final String INIT_DATA_DIRECTORY = "/WEB-INF/init-data";
	
	private static final Log log = LogFactory.getLog(InitialJenaModelUtils.class.getName());
	
	public static Model loadInitialModel(ServletContext ctx, String defaultNamespace) {
	
		OntModel initialModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
		
		try {
			JenaDataSourceSetupBase.readOntologyFilesInPathSet(INIT_DATA_DIRECTORY, ctx, initialModel);
		} catch (Throwable t) {
			log.warn("Unable to read initial model data from " + INIT_DATA_DIRECTORY, t);
		}
	
		if (initialModel.size() == 0) {
			return initialModel;
		}
		
		//find and rename portal object
		//currently, only a single portal is permitted in the initialization data
		Resource portalResource = null;
		ClosableIterator<Resource> portalResIt = initialModel.listSubjectsWithProperty(RDF.type, initialModel.getResource(VitroVocabulary.PORTAL));
		try {
			if (portalResIt.hasNext()) {
				Resource portalRes = portalResIt.next();
				if (portalRes.isAnon()) {
					portalResource = portalRes;
				}
			}
		} finally {
			portalResIt.close();
		}
		if (portalResource != null) {
			ResourceUtils.renameResource(portalResource, defaultNamespace + "portal1");
		}
		
		return initialModel;
		
	}
	
	public static Model basicInterfaceData(String defaultNamespace) {
		OntModel essentialInterfaceData = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
        Resource portalClass = essentialInterfaceData.getResource(VitroVocabulary.PORTAL);
        Property themeDirProperty = essentialInterfaceData.getProperty(VitroVocabulary.PORTAL_THEMEDIR);
        Individual portal1 = essentialInterfaceData.createIndividual(defaultNamespace+"portal1",portalClass);
        String defaultThemeStr = ApplicationBean.DEFAULT_THEME_DIR_FROM_CONTEXT;
        if (defaultThemeStr == null) {
        	throw new RuntimeException("No default theme has been set; unable to create default portal.");      	
        }
        portal1.setPropertyValue(themeDirProperty,ResourceFactory.createPlainLiteral(defaultThemeStr));
		portal1.setLabel("Vitro", null);	
		return essentialInterfaceData;		
	}
	
	public static Model basicClassgroup(String defaultNamespace) {
		OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
		Individual thingsClassGroup = m.createIndividual(
				defaultNamespace+"vitroClassGroupThings", 
				m.createResource(VitroVocabulary.CLASSGROUP));
		thingsClassGroup.addLabel("Things", null);
		thingsClassGroup.addProperty(
				m.getProperty(VitroVocabulary.DISPLAY_RANK_ANNOT),
				"50", XSDDatatype.XSDint);
		m.add(OWL.Thing, 
				m.getProperty(VitroVocabulary.IN_CLASSGROUP), thingsClassGroup);
		return m;
	}
				
}

@@ -136,138 +136,8 @@
 a;%0A%0A
-import java.io.BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileReader;%0Aimport java.io.IOException;%0Aimport java.util.Set;%0A%0A
 impo
@@ -283,601 +283,201 @@
 ena.
-datatypes.xsd.XSDDatatype;%0Aimport com.hp.hpl.jena.ontology.Individual;%0Aimport com.hp.hpl.jena.ontology.OntModel;%0Aimport com.hp.hpl.jena.ontology.OntModelSpec;%0Aimport com.hp.hpl.jena.query.Query;%0Aimport com.hp.hpl.jena.query.QueryExecution;%0Aimport com.hp.hpl.jena.query.QueryExecutionFactory;%0Aimport com.hp.hpl.jena.query.QueryFactory;%0Aimport com.hp.hpl.jena.query.Syntax;%0Aimport com.hp.hpl.jena.rdf.model.Model;%0Aimport com.hp.hpl.jena.rdf.model.ModelFactory;%0Aimport com.hp.hpl.jena.rdf.model.Property;%0Aimport com.hp.hpl.jena.rdf.model.Resource;%0Aimport com.hp.hpl.jena.rdf.model.ResourceFactory
+ontology.OntModel;%0Aimport com.hp.hpl.jena.ontology.OntModelSpec;%0Aimport com.hp.hpl.jena.rdf.model.Model;%0Aimport com.hp.hpl.jena.rdf.model.ModelFactory;%0Aimport com.hp.hpl.jena.rdf.model.Resource
 ;%0Aim
@@ -576,47 +576,8 @@
 or;%0A
-import com.hp.hpl.jena.vocabulary.OWL;%0A
 impo
@@ -616,71 +616,8 @@
 F;%0A%0A
-import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;%0A
 impo
@@ -947,18 +947,8 @@
 lass
-.getName()
 );%0A%09
@@ -2041,1449 +2041,6 @@
 %7D%0A%09%0A
-%09public static Model basicInterfaceData(String defaultNamespace) %7B%0A%09%09OntModel essentialInterfaceData = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);%0A        Resource portalClass = essentialInterfaceData.getResource(VitroVocabulary.PORTAL);%0A        Property themeDirProperty = essentialInterfaceData.getProperty(VitroVocabulary.PORTAL_THEMEDIR);%0A        Individual portal1 = essentialInterfaceData.createIndividual(defaultNamespace+%22portal1%22,portalClass);%0A        String defaultThemeStr = ApplicationBean.DEFAULT_THEME_DIR_FROM_CONTEXT;%0A        if (defaultThemeStr == null) %7B%0A        %09throw new RuntimeException(%22No default theme has been set; unable to create default portal.%22);      %09%0A        %7D%0A        portal1.setPropertyValue(themeDirProperty,ResourceFactory.createPlainLiteral(defaultThemeStr));%0A%09%09portal1.setLabel(%22Vitro%22, null);%09%0A%09%09return essentialInterfaceData;%09%09%0A%09%7D%0A%09%0A%09public static Model basicClassgroup(String defaultNamespace) %7B%0A%09%09OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);%0A%09%09Individual thingsClassGroup = m.createIndividual(%0A%09%09%09%09defaultNamespace+%22vitroClassGroupThings%22, %0A%09%09%09%09m.createResource(VitroVocabulary.CLASSGROUP));%0A%09%09thingsClassGroup.addLabel(%22Things%22, null);%0A%09%09thingsClassGroup.addProperty(%0A%09%09%09%09m.getProperty(VitroVocabulary.DISPLAY_RANK_ANNOT),%0A%09%09%09%09%2250%22, XSDDatatype.XSDint);%0A%09%09m.add(OWL.Thing, %0A%09%09%09%09m.getProperty(VitroVocabulary.IN_CLASSGROUP), thingsClassGroup);%0A%09%09return m;%0A%09%7D%0A%09%09%09%09%0A
 %7D%0A

Remove unused methods from InitialJenaModelUtil: basicInterfaceData() and basicClassgroup()