Working on temporary menu editor NIHVIVO-710

This commit is contained in:
bdc34 2010-12-14 21:09:59 +00:00
parent baaa09e6a0
commit bfc524afdc
7 changed files with 245 additions and 2 deletions

View file

@ -0,0 +1,65 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.util.HashMap;
import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
public class MenuN3EditController extends FreemarkerHttpServlet {
protected final static String N3MENU_FORM = "menuN3Edit.ftl";
protected final static String N3MENU_SUCCESS_RESULT = "menuN3Edit.ftl";
protected final static String N3MENU_ERROR_RESULT = "menuN3Edit.ftl";
protected final static String N3_PARAM = "navigationN3";
@Override
protected ResponseValues processRequest(VitroRequest vreq) {
String n3 = vreq.getParameter(N3_PARAM);
if( n3 != null && ! n3.isEmpty()){
return setNewMenu(vreq);
}else{
return showForm(vreq);
}
}
private ResponseValues showForm(VitroRequest vreq) {
Map<String,Object> data = new HashMap<String,Object>();
String menuN3;
try {
menuN3 = vreq.getWebappDaoFactory().getDisplayModelDao()
.getDisplayModel(getServletContext());
data.put("menuN3", menuN3);
} catch (Exception e) {
data.put("errorMessage",e.getMessage());
}
return new TemplateResponseValues(N3MENU_FORM, data);
}
private ResponseValues setNewMenu(VitroRequest vreq) {
Map<String,Object> data = new HashMap<String,Object>();
String menuN3 = vreq.getParameter(N3_PARAM);
try {
vreq.getWebappDaoFactory().getDisplayModelDao()
.replaceDisplayModel(menuN3, getServletContext());
data.put("message", "success");
} catch (Exception e) {
data.put("errorMessage",e.getMessage());
}
if( data.containsKey("errorMessage"))
return new TemplateResponseValues(N3MENU_ERROR_RESULT,data);
else
return new TemplateResponseValues(N3MENU_SUCCESS_RESULT, data);
}
}

View file

@ -0,0 +1,18 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.dao;
import javax.servlet.ServletContext;
public interface DisplayModelDao {
/**
* ServletContext should only be used for getRealPath()
*/
public void replaceDisplayModel(String n3, ServletContext sc) throws Exception;
/**
* ServletContext should only be used for getRealPath()
*/
public String getDisplayModel(ServletContext sc) throws Exception;
}

View file

@ -106,6 +106,8 @@ public interface WebappDaoFactory {
public ObjectPropertyStatementDao getObjectPropertyStatementDao();
public DisplayModelDao getDisplayModelDao();
/* ======================== DAOs for other objects ======================== */
public ApplicationDao getApplicationDao();

View file

@ -11,6 +11,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.Classes2ClassesDao;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.DatatypeDao;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayModelDao;
import edu.cornell.mannlib.vitro.webapp.dao.FlagDao;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.KeywordDao;
@ -310,4 +311,9 @@ public class WebappDaoFactoryFiltering implements WebappDaoFactory {
public MenuDao getMenuDao(){
return innerWebappDaoFactory.getMenuDao();
}
@Override
public DisplayModelDao getDisplayModelDao(){
return innerWebappDaoFactory.getDisplayModelDao();
}
}

View file

@ -0,0 +1,120 @@
/* $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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.channels.FileChannel;
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.util.FileManager;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayModelDao;
public class DisplayModelDaoJena implements DisplayModelDao {
WebappDaoFactoryJena wdf;
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";
public DisplayModelDaoJena(WebappDaoFactoryJena wdfj){
this.wdf = wdfj;
}
public void replaceDisplayModel(String n3, ServletContext context) throws Exception{
OntModel displayModel = wdf.getOntModelSelector().getDisplayModel();
//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.");
}
try{
oldMenuStmts.read(oldIn, null, "N3");
}catch(Throwable th){
throw new Exception("Cannot read in existing menu file.");
}
//get menu file and turn into model
Model newMenuStmts = ModelFactory.createDefaultModel();
StringReader newIn = new StringReader( n3 );
try{
newMenuStmts.read(newIn, null,"N3");
}catch(Throwable th){
throw new Exception("There was an error in the menu N3: "+ th.getMessage());
}
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);
//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();
//remove old menu statements from display model
displayModel.remove(oldMenuStmts);
//add new menu statements to display model
displayModel.add(newMenuStmts);
}finally{
displayModel.leaveCriticalSection();
}
}
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);
}
reader.close();
return str.toString();
}
public static void copyFile(File sourceFile, File destFile) throws IOException {
if(!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
}
finally {
if(source != null) {
source.close();
}
if(destination != null) {
destination.close();
}
}
}
}

View file

@ -9,7 +9,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import com.hp.hpl.jena.iri.IRI;
import com.hp.hpl.jena.iri.IRIFactory;
@ -30,12 +29,12 @@ import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.dao.ApplicationDao;
import edu.cornell.mannlib.vitro.webapp.dao.Classes2ClassesDao;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.DatatypeDao;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayModelDao;
import edu.cornell.mannlib.vitro.webapp.dao.FlagDao;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.KeywordDao;
@ -592,4 +591,8 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
return menuDao;
}
@Override
public DisplayModelDao getDisplayModelDao(){
return new DisplayModelDaoJena( this );
}
}

View file

@ -0,0 +1,29 @@
<#-- $This file is distributed under the terms of the license in /doc/license.txt$ -->
<div id="navigationManagement">
<h2>Navigation Management</h2>
<#if errorMessage??>
<div id="errorAlert">
<img src="../images/iconAlert.png" alert="Error alert icon" height="31" width="32">
<p>${errorMessage}</p>
</div>
</#if>
<#if message??>
<p>${message}</p>
</#if>
<#if menuN3??>
<form class="" action="${urls.base}/MenuN3EditController" method="post">
<label for="navigatioN3"></label>
<textarea name="navigationN3" id="textarea" cols="45" rows="40">
${menuN3}
</textarea>
<input name="submit" class="submit" value="Save" type="submit"/>
or <a href="#">Cancel</a>
<p class="small red">&nbsp;</p>
</form>
</#if>
</div>