Moving menu n3 data into model. NIHVIVO-1560.

This commit is contained in:
bdc34 2011-01-06 23:22:53 +00:00
parent 28a2be38bc
commit ca7e496925
3 changed files with 73 additions and 27 deletions

View file

@ -2,6 +2,10 @@
package edu.cornell.mannlib.vitro.webapp.dao; 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 { public class DisplayVocabulary {
@ -21,4 +25,8 @@ public class DisplayVocabulary {
public static final String URL_MAPPING = NS + "urlMapping"; public static final String URL_MAPPING = NS + "urlMapping";
public static final String TITLE = NS + "title"; public static final String TITLE = NS + "title";
public static final String REQUIRES_BODY_TEMPLATE = NS + "requiresBodyTemplate"; 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";
} }

View file

@ -18,9 +18,14 @@ import javax.servlet.ServletContext;
import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory; 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 com.hp.hpl.jena.util.FileManager;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayModelDao; import edu.cornell.mannlib.vitro.webapp.dao.DisplayModelDao;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
public class DisplayModelDaoJena implements DisplayModelDao { public class DisplayModelDaoJena implements DisplayModelDao {
WebappDaoFactoryJena wdf; 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 = "/WEB-INF/ontologies/app/menu.n3";
protected static final String MENU_N3_FILE_BACKUP = "/WEB-INF/ontologies/app/menu.backup"; 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){ public DisplayModelDaoJena(WebappDaoFactoryJena wdfj){
this.wdf = wdfj; this.wdf = wdfj;
} }
@ -37,17 +47,17 @@ public class DisplayModelDaoJena implements DisplayModelDao {
//get old menu file and turn into model //get old menu file and turn into model
Model oldMenuStmts = ModelFactory.createDefaultModel(); Model oldMenuStmts = ModelFactory.createDefaultModel();
InputStream oldIn = FileManager.get().open( context.getRealPath(MENU_N3_FILE ) ); // InputStream oldIn = FileManager.get().open( context.getRealPath(MENU_N3_FILE ) );
if( oldIn == null ){ // if( oldIn == null ){
throw new Exception("Cannot open existing menu file."); // throw new Exception("Cannot open existing menu file.");
} // }
try{ try{
oldMenuStmts.read(oldIn, null, "N3"); oldMenuStmts.read(new StringReader(getDisplayModel(context)), null, "N3");
}catch(Throwable th){ }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(); Model newMenuStmts = ModelFactory.createDefaultModel();
StringReader newIn = new StringReader( n3 ); StringReader newIn = new StringReader( n3 );
try{ try{
@ -59,15 +69,18 @@ public class DisplayModelDaoJena implements DisplayModelDao {
displayModel.enterCriticalSection(true); displayModel.enterCriticalSection(true);
try{ try{
//copy old menu file to backup //copy old menu file to backup
File oldMenuFile = new File(context.getRealPath(MENU_N3_FILE)); // File oldMenuFile = new File(context.getRealPath(MENU_N3_FILE));
File oldMenuFileBackup = new File(context.getRealPath(MENU_N3_FILE_BACKUP)); // File oldMenuFileBackup = new File(context.getRealPath(MENU_N3_FILE_BACKUP));
copyFile(oldMenuFile , oldMenuFileBackup); // copyFile(oldMenuFile , oldMenuFileBackup);
//save new menu file to old menu file //save new menu file to old menu file
File newMenuFile = new File(context.getRealPath(MENU_N3_FILE)); displayModel.removeAll(MENU_TEXT_RES, HAS_TEXT_REPRESENTATION, null);
FileWriter mfWriter = new FileWriter(newMenuFile); displayModel.add(MENU_TEXT_RES, HAS_TEXT_REPRESENTATION, n3);
mfWriter.write(n3);
mfWriter.close(); // 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 //remove old menu statements from display model
displayModel.remove(oldMenuStmts); displayModel.remove(oldMenuStmts);
@ -80,18 +93,43 @@ public class DisplayModelDaoJena implements DisplayModelDao {
} }
public String getDisplayModel(ServletContext context) throws IOException{ public String getDisplayModel(ServletContext context) throws IOException{
File oldMenuFile = new File(context.getRealPath(MENU_N3_FILE)); OntModel displayModel = wdf.getOntModelSelector().getDisplayModel();
StringBuffer str = new StringBuffer(2000); String text = null;
BufferedReader reader = new BufferedReader(new FileReader(oldMenuFile)); displayModel.enterCriticalSection(false);
char[] buf = new char[1024]; try{
int numRead=0; Statement stmt = displayModel.getProperty(MENU_TEXT_RES,HAS_TEXT_REPRESENTATION);
while((numRead=reader.read(buf)) != -1){ if( stmt != null && stmt.getLiteral() != null)
String readData = String.valueOf(buf, 0, numRead); text = stmt.getLiteral().getLexicalForm();
str.append(readData); }finally{
displayModel.leaveCriticalSection();
} }
reader.close(); if( text == null ){
return str.toString(); //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 { public static void copyFile(File sourceFile, File destFile) throws IOException {

View file

@ -170,8 +170,8 @@ public class URLRewritingHttpServletResponseTest {
} }
@Test @Test
public void test42090(){ urlEncodingStyleA( 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%23Award",
"vclassEdit?home=1&uri=http%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23AwardOrHonor"); "vclassEdit?home=1&uri=http%3A%2F%2Fvivoweb.org%2Fontology%2Fcore%23Award");
} }
@Test @Test
public void test42091(){ urlEncodingStyleA( public void test42091(){ urlEncodingStyleA(