Adding MenuDao.java, MenuDaoJena.java, MenuDaoJenaTest.java and menuForTest.n3
This commit is contained in:
parent
6fe5ac59a9
commit
e20e2af6c6
4 changed files with 274 additions and 0 deletions
|
@ -0,0 +1,8 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.dao;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.Menu;
|
||||||
|
|
||||||
|
public interface MenuDao {
|
||||||
|
public Menu getMenu(String uri);
|
||||||
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
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.rdf.model.Literal;
|
||||||
|
import com.hp.hpl.jena.rdf.model.Model;
|
||||||
|
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.MenuDao;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.Menu;
|
||||||
|
|
||||||
|
public class MenuDaoJena extends JenaBaseDao implements MenuDao {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(MenuDaoJena.class);
|
||||||
|
|
||||||
|
static final String prefixes =
|
||||||
|
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" +
|
||||||
|
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n" +
|
||||||
|
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> \n" +
|
||||||
|
"PREFIX display: <" + VitroVocabulary.DISPLAY +"> \n";
|
||||||
|
|
||||||
|
|
||||||
|
static final protected String menuQueryString =
|
||||||
|
prefixes + "\n" +
|
||||||
|
"SELECT ?menuItem ?linkText ?urlMapping WHERE {\n" +
|
||||||
|
// " GRAPH ?g{\n"+
|
||||||
|
" ?menu rdf:type display:Menu .\n"+
|
||||||
|
" ?menu display:hasElement ?menuItem . \n"+
|
||||||
|
" OPTIONAL { ?menuItem display:linkText ?linkText }.\n"+
|
||||||
|
" OPTIONAL { ?menuItem display:menuPosition ?menuPosition }.\n"+
|
||||||
|
" OPTIONAL { ?menuItem display:toPage ?page . }\n"+
|
||||||
|
" OPTIONAL { ?page display:urlMapping ?urlMapping . }\n"+
|
||||||
|
// " }\n"+
|
||||||
|
"} \n" +
|
||||||
|
"ORDER BY ?menuPosition ?menuItemText \n";
|
||||||
|
|
||||||
|
static{
|
||||||
|
try{
|
||||||
|
menuQuery=QueryFactory.create(menuQueryString);
|
||||||
|
}catch(Throwable th){
|
||||||
|
log.error("could not create SPARQL query for menuQueryString " + th.getMessage());
|
||||||
|
log.error(menuQueryString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static protected Query menuQuery;
|
||||||
|
|
||||||
|
public MenuDaoJena(WebappDaoFactoryJena wadf) {
|
||||||
|
super(wadf);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Menu getMenu(String uri) {
|
||||||
|
return getMenu(uri, getOntModelSelector().getDisplayModel());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Menu getMenu(String uri, Model displayModel){
|
||||||
|
//setup query parameters
|
||||||
|
QuerySolutionMap initialBindings = new QuerySolutionMap();
|
||||||
|
initialBindings.add("menu", ResourceFactory.createResource(uri));
|
||||||
|
|
||||||
|
//run SPARQL query to get menu and menu items
|
||||||
|
QueryExecution qexec = QueryExecutionFactory.create(menuQuery,displayModel,initialBindings );
|
||||||
|
try{
|
||||||
|
Menu menu = new Menu();
|
||||||
|
ResultSet results =qexec.execSelect();
|
||||||
|
for( ; results.hasNext();){
|
||||||
|
QuerySolution soln = results.nextSolution();
|
||||||
|
Literal itemText = soln.getLiteral("linkText");
|
||||||
|
Literal itemLink = soln.getLiteral("urlMapping");
|
||||||
|
String text = itemText != null ? itemText.getLexicalForm():"(undefined text)";
|
||||||
|
String link = itemLink != null ? itemLink.getLexicalForm():"undefinedLink";
|
||||||
|
menu.addItem(text,link);
|
||||||
|
}
|
||||||
|
return menu;
|
||||||
|
}catch(Throwable th){
|
||||||
|
log.error(th,th);
|
||||||
|
return new Menu();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||||
|
|
||||||
|
package edu.cornell.mannlib.vitro.webapp.dao.jena;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
import org.apache.log4j.Level;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.hp.hpl.jena.ontology.OntModel;
|
||||||
|
import com.hp.hpl.jena.ontology.OntModelSpec;
|
||||||
|
import com.hp.hpl.jena.rdf.model.Model;
|
||||||
|
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||||
|
import com.hp.hpl.jena.rdf.model.impl.RDFDefaultErrorHandler;
|
||||||
|
|
||||||
|
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.Menu;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.menu.MenuItem;
|
||||||
|
|
||||||
|
|
||||||
|
public class MenuDaoJenaTest extends AbstractTestClass {
|
||||||
|
|
||||||
|
OntModel displayModel;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
// Suppress error logging.
|
||||||
|
setLoggerLevel(RDFDefaultErrorHandler.class, Level.OFF);
|
||||||
|
|
||||||
|
Model model = ModelFactory.createDefaultModel();
|
||||||
|
InputStream in = MenuDaoJenaTest.class.getResourceAsStream("resources/menuForTest.n3");
|
||||||
|
model.read(in,"","N3");
|
||||||
|
displayModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM,model);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getMenuItemTest(){
|
||||||
|
MenuDaoJena menuDaoJena = new MenuDaoJena(new WebappDaoFactoryJena(displayModel));
|
||||||
|
Menu menu = menuDaoJena.getMenu(VitroVocabulary.DISPLAY+"DefaultMenu", displayModel);
|
||||||
|
|
||||||
|
try{
|
||||||
|
Class clz = UrlBuilder.class;
|
||||||
|
Field f = clz.getDeclaredField( "contextPath" );
|
||||||
|
f.setAccessible(true);
|
||||||
|
f.set(null, "bogusUrlContextPath");
|
||||||
|
}catch(Exception e){
|
||||||
|
Assert.fail(e.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertNotNull(menu);
|
||||||
|
Assert.assertNotNull( menu.getItems() );
|
||||||
|
Assert.assertEquals(5, menu.getItems().size());
|
||||||
|
|
||||||
|
//The nulls in getUrl() are from the UrlBuilder not being setup correctly.
|
||||||
|
//it should be fine.
|
||||||
|
|
||||||
|
MenuItem item = menu.getItems().get(0);
|
||||||
|
Assert.assertNotNull(item);
|
||||||
|
Assert.assertEquals("Home",item.getLinkText());
|
||||||
|
Assert.assertEquals("bogusUrlContextPath/home",item.getUrl());
|
||||||
|
|
||||||
|
item = menu.getItems().get(1);
|
||||||
|
Assert.assertNotNull(item);
|
||||||
|
Assert.assertEquals("People",item.getLinkText());
|
||||||
|
Assert.assertEquals("bogusUrlContextPath/people",item.getUrl());
|
||||||
|
|
||||||
|
item = menu.getItems().get(2);
|
||||||
|
Assert.assertNotNull(item);
|
||||||
|
Assert.assertEquals("Publications",item.getLinkText());
|
||||||
|
Assert.assertEquals("bogusUrlContextPath/publications",item.getUrl());
|
||||||
|
|
||||||
|
item = menu.getItems().get(3);
|
||||||
|
Assert.assertNotNull(item);
|
||||||
|
Assert.assertEquals("Events",item.getLinkText());
|
||||||
|
Assert.assertEquals("bogusUrlContextPath/events",item.getUrl());
|
||||||
|
|
||||||
|
item = menu.getItems().get(4);
|
||||||
|
Assert.assertNotNull(item);
|
||||||
|
Assert.assertEquals("Organizations",item.getLinkText());
|
||||||
|
Assert.assertEquals("bogusUrlContextPath/organizations",item.getUrl());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,84 @@
|
||||||
|
# $This file is distributed under the terms of the license in /doc/license.txt$
|
||||||
|
|
||||||
|
@prefix owl: <http://www.w3.org/2002/07/owl#> .
|
||||||
|
@prefix display: <http://vitro.mannlib.cornell.edu/ontologies/display/1.1#> .
|
||||||
|
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
|
||||||
|
@prefix core: <http://vivoweb.org/ontology/core#> .
|
||||||
|
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
|
||||||
|
|
||||||
|
### This file is for the test MenuDaoJenaTest.java it is derived from vivoDefaultMenu.n3 in the vivo product.
|
||||||
|
|
||||||
|
#### Default Menu ####
|
||||||
|
|
||||||
|
display:DefaultMenu
|
||||||
|
a display:Menu ;
|
||||||
|
display:hasElement display:EventsMenuItem ;
|
||||||
|
display:hasElement display:HomeMenuItem ;
|
||||||
|
display:hasElement display:OrganizationsMenuItem ;
|
||||||
|
display:hasElement display:PeopleMenuItem ;
|
||||||
|
display:hasElement display:PublicationsMenuItem .
|
||||||
|
|
||||||
|
#### Menu Items for Default Menu ####
|
||||||
|
|
||||||
|
|
||||||
|
display:HomeMenuItem
|
||||||
|
a display:NavigationElement ;
|
||||||
|
display:menuPosition "1";
|
||||||
|
display:linkText "Home";
|
||||||
|
display:toPage display:Home .
|
||||||
|
|
||||||
|
display:PeopleMenuItem
|
||||||
|
a display:NavigationElement ;
|
||||||
|
display:menuPosition "2";
|
||||||
|
display:linkText "People";
|
||||||
|
display:toPage display:People .
|
||||||
|
|
||||||
|
display:PublicationsMenuItem
|
||||||
|
a display:NavigationElement ;
|
||||||
|
display:menuPosition "3";
|
||||||
|
display:linkText "Publications";
|
||||||
|
display:toPage display:Publications .
|
||||||
|
|
||||||
|
display:EventsMenuItem
|
||||||
|
a display:NavigationElement ;
|
||||||
|
display:menuPosition "4";
|
||||||
|
display:linkText "Events";
|
||||||
|
display:toPage display:Events .
|
||||||
|
|
||||||
|
display:OrganizationsMenuItem
|
||||||
|
a display:NavigationElement ;
|
||||||
|
display:menuPosition "5";
|
||||||
|
display:linkText "Organizations";
|
||||||
|
display:toPage display:Organizations .
|
||||||
|
|
||||||
|
########## Pages ############
|
||||||
|
|
||||||
|
display:Home
|
||||||
|
a display:Page ;
|
||||||
|
display:requiresBodyTemplate "home.ftl" ;
|
||||||
|
display:title "Home" ;
|
||||||
|
display:urlMapping "/home" .
|
||||||
|
|
||||||
|
display:Events
|
||||||
|
a display:Page ;
|
||||||
|
display:requiresBodyTemplate "Events.ftl" ;
|
||||||
|
display:title "Events" ;
|
||||||
|
display:urlMapping "/events" .
|
||||||
|
|
||||||
|
display:Organizations
|
||||||
|
a display:Page ;
|
||||||
|
display:requiresBodyTemplate "organizations.ftl" ;
|
||||||
|
display:title "Organizations" ;
|
||||||
|
display:urlMapping "/organizations" .
|
||||||
|
|
||||||
|
display:People
|
||||||
|
a display:Page ;
|
||||||
|
display:requiresBodyTemplate "people.ftl" ;
|
||||||
|
display:title "People" ;
|
||||||
|
display:urlMapping "/people" .
|
||||||
|
|
||||||
|
display:Publications
|
||||||
|
a display:Page ;
|
||||||
|
display:requiresBodyTemplate "publications.ftl" ;
|
||||||
|
display:title "Publications" ;
|
||||||
|
display:urlMapping "/publications" .
|
Loading…
Add table
Reference in a new issue