Moving menu n3 data into model. NIHVIVO-1560.
This commit is contained in:
parent
28a2be38bc
commit
ca7e496925
3 changed files with 73 additions and 27 deletions
|
@ -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";
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
@ -81,17 +94,42 @@ 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);
|
||||
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();
|
||||
}
|
||||
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;
|
||||
}
|
||||
reader.close();
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
public static void copyFile(File sourceFile, File destFile) throws IOException {
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Reference in a new issue