From ca7e4969253e408af539dc5da789d27f8b62ceb0 Mon Sep 17 00:00:00 2001 From: bdc34 Date: Thu, 6 Jan 2011 23:22:53 +0000 Subject: [PATCH] Moving menu n3 data into model. NIHVIVO-1560. --- .../vitro/webapp/dao/DisplayVocabulary.java | 8 ++ .../webapp/dao/jena/DisplayModelDaoJena.java | 88 +++++++++++++------ .../URLRewritingHttpServletResponseTest.java | 4 +- 3 files changed, 73 insertions(+), 27 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/DisplayVocabulary.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/DisplayVocabulary.java index 56bf9e008..92728275f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/DisplayVocabulary.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/DisplayVocabulary.java @@ -2,6 +2,10 @@ package edu.cornell.mannlib.vitro.webapp.dao; +import com.hp.hpl.jena.rdf.model.Property; +import com.hp.hpl.jena.rdf.model.Resource; +import com.hp.hpl.jena.rdf.model.ResourceFactory; + public class DisplayVocabulary { @@ -21,4 +25,8 @@ public class DisplayVocabulary { public static final String URL_MAPPING = NS + "urlMapping"; public static final String TITLE = NS + "title"; public static final String REQUIRES_BODY_TEMPLATE = NS + "requiresBodyTemplate"; + + /* URIs for storing menu.n3 */ + public static final String MENU_TEXT_RES = NS + "MenuText"; + public static final String HAS_TEXT_REPRESENTATION = NS + "hasMenuText"; } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/DisplayModelDaoJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/DisplayModelDaoJena.java index 39615a65c..d1568e413 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/DisplayModelDaoJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/DisplayModelDaoJena.java @@ -18,9 +18,14 @@ import javax.servlet.ServletContext; import com.hp.hpl.jena.ontology.OntModel; 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.rdf.model.Statement; import com.hp.hpl.jena.util.FileManager; import edu.cornell.mannlib.vitro.webapp.dao.DisplayModelDao; +import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; public class DisplayModelDaoJena implements DisplayModelDao { WebappDaoFactoryJena wdf; @@ -28,6 +33,11 @@ public class DisplayModelDaoJena implements DisplayModelDao { protected static final String MENU_N3_FILE = "/WEB-INF/ontologies/app/menu.n3"; protected static final String MENU_N3_FILE_BACKUP = "/WEB-INF/ontologies/app/menu.backup"; + protected static Resource MENU_TEXT_RES = + ResourceFactory.createResource(DisplayVocabulary.MENU_TEXT_RES); + protected static Property HAS_TEXT_REPRESENTATION = + ResourceFactory.createProperty(DisplayVocabulary.HAS_TEXT_REPRESENTATION); + public DisplayModelDaoJena(WebappDaoFactoryJena wdfj){ this.wdf = wdfj; } @@ -37,17 +47,17 @@ public class DisplayModelDaoJena implements DisplayModelDao { //get old menu file and turn into model Model oldMenuStmts = ModelFactory.createDefaultModel(); - InputStream oldIn = FileManager.get().open( context.getRealPath(MENU_N3_FILE ) ); - if( oldIn == null ){ - throw new Exception("Cannot open existing menu file."); - } +// InputStream oldIn = FileManager.get().open( context.getRealPath(MENU_N3_FILE ) ); +// if( oldIn == null ){ +// throw new Exception("Cannot open existing menu file."); +// } try{ - oldMenuStmts.read(oldIn, null, "N3"); + oldMenuStmts.read(new StringReader(getDisplayModel(context)), null, "N3"); }catch(Throwable th){ - throw new Exception("Cannot read in existing menu file."); + throw new Exception("Cannot read in existing menu. " + th.getMessage()); } - //get menu file and turn into model + //turn the N3 text for the new menu into a model Model newMenuStmts = ModelFactory.createDefaultModel(); StringReader newIn = new StringReader( n3 ); try{ @@ -59,15 +69,18 @@ public class DisplayModelDaoJena implements DisplayModelDao { displayModel.enterCriticalSection(true); try{ //copy old menu file to backup - File oldMenuFile = new File(context.getRealPath(MENU_N3_FILE)); - File oldMenuFileBackup = new File(context.getRealPath(MENU_N3_FILE_BACKUP)); - copyFile(oldMenuFile , oldMenuFileBackup); +// File oldMenuFile = new File(context.getRealPath(MENU_N3_FILE)); +// File oldMenuFileBackup = new File(context.getRealPath(MENU_N3_FILE_BACKUP)); +// copyFile(oldMenuFile , oldMenuFileBackup); //save new menu file to old menu file - File newMenuFile = new File(context.getRealPath(MENU_N3_FILE)); - FileWriter mfWriter = new FileWriter(newMenuFile); - mfWriter.write(n3); - mfWriter.close(); + displayModel.removeAll(MENU_TEXT_RES, HAS_TEXT_REPRESENTATION, null); + displayModel.add(MENU_TEXT_RES, HAS_TEXT_REPRESENTATION, n3); + +// File newMenuFile = new File(context.getRealPath(MENU_N3_FILE)); +// FileWriter mfWriter = new FileWriter(newMenuFile); +// mfWriter.write(n3); +// mfWriter.close(); //remove old menu statements from display model displayModel.remove(oldMenuStmts); @@ -80,18 +93,43 @@ public class DisplayModelDaoJena implements DisplayModelDao { } - public String getDisplayModel(ServletContext context) throws IOException{ - File oldMenuFile = new File(context.getRealPath(MENU_N3_FILE)); - StringBuffer str = new StringBuffer(2000); - BufferedReader reader = new BufferedReader(new FileReader(oldMenuFile)); - char[] buf = new char[1024]; - int numRead=0; - while((numRead=reader.read(buf)) != -1){ - String readData = String.valueOf(buf, 0, numRead); - str.append(readData); + public String getDisplayModel(ServletContext context) throws IOException{ + OntModel displayModel = wdf.getOntModelSelector().getDisplayModel(); + String text = null; + displayModel.enterCriticalSection(false); + try{ + Statement stmt = displayModel.getProperty(MENU_TEXT_RES,HAS_TEXT_REPRESENTATION); + if( stmt != null && stmt.getLiteral() != null) + text = stmt.getLiteral().getLexicalForm(); + }finally{ + displayModel.leaveCriticalSection(); } - reader.close(); - return str.toString(); + if( text == null ){ + //text of file is not yet in model + File oldMenuFile = new File(context.getRealPath(MENU_N3_FILE)); + StringBuffer str = new StringBuffer(2000); + BufferedReader reader = new BufferedReader(new FileReader(oldMenuFile)); + char[] buf = new char[1024]; + int numRead=0; + while((numRead=reader.read(buf)) != -1){ + String readData = String.valueOf(buf, 0, numRead); + str.append(readData); + } + reader.close(); + + //Now write the file contents into the display model so that on + //future edits, the user can be presented with their last input. + String menuN3Content = str.toString(); + displayModel.enterCriticalSection(true); + try{ + displayModel.add(MENU_TEXT_RES, HAS_TEXT_REPRESENTATION, menuN3Content); + }finally{ + displayModel.leaveCriticalSection(); + } + return menuN3Content; + }else{ + return text; + } } public static void copyFile(File sourceFile, File destFile) throws IOException { diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/filters/URLRewritingHttpServletResponseTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/filters/URLRewritingHttpServletResponseTest.java index 13618c80d..477549e25 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/filters/URLRewritingHttpServletResponseTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/filters/URLRewritingHttpServletResponseTest.java @@ -170,8 +170,8 @@ public class URLRewritingHttpServletResponseTest { } @Test public void test42090(){ urlEncodingStyleA( - "vclassEdit?home=1&uri=http%3a%2f%2fvivoweb.org%2fontology%2fcore%23AwardOrHonor", - "vclassEdit?home=1&uri=http%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23AwardOrHonor"); + "vclassEdit?home=1&uri=http%3a%2f%2fvivoweb.org%2fontology%2fcore%23Award", + "vclassEdit?home=1&uri=http%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23Award"); } @Test public void test42091(){ urlEncodingStyleA(