updates for page management, and replacing empty options with Constant empty options for pageData in EditConfigurationTemplateModel in populateDropdowns and also logging error if name in pageData already in use for that field name
This commit is contained in:
parent
a3fec6ed18
commit
7e2a5d22d4
14 changed files with 403 additions and 50 deletions
|
@ -0,0 +1,171 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.controller.edit;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
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.RDFNode;
|
||||
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.rdf.model.StmtIterator;
|
||||
import com.hp.hpl.jena.shared.Lock;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
|
||||
|
||||
/**
|
||||
*Process deletions for page, deleting page, data getters and any associated menu items.
|
||||
*/
|
||||
public class DeletePageController extends VitroHttpServlet {
|
||||
|
||||
private final static String REDIRECT_URL = "/pageList";
|
||||
private static Model removeStatements = null;
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest rawRequest, HttpServletResponse resp)
|
||||
throws ServletException, IOException {
|
||||
removeStatements = ModelFactory.createDefaultModel();
|
||||
VitroRequest vreq = new VitroRequest(rawRequest);
|
||||
String pageUri = vreq.getParameter("pageURI");
|
||||
if(pageUri != null) {
|
||||
doDeletePage(pageUri, vreq, resp);
|
||||
}
|
||||
resp.sendRedirect(rawRequest.getContextPath() + REDIRECT_URL);
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest rawRequest, HttpServletResponse resp) {
|
||||
doPost(rawRequest, resp);
|
||||
}
|
||||
|
||||
|
||||
//Parameter retrieval is identical, but in one case an entirey new menu item needs to be created
|
||||
//along with a new page
|
||||
public void doDeletePage(String pageUri, VitroRequest vreq, HttpServletResponse resp) {
|
||||
|
||||
OntModel displayModel = getDisplayModel(vreq);
|
||||
if(displayModel == null) {
|
||||
//Throw some kind of exception
|
||||
log.error("Display model not being retrieved correctly");
|
||||
}
|
||||
|
||||
String errorMessage = "";
|
||||
processDelete(pageUri, displayModel, vreq);
|
||||
|
||||
//Edits to model occur here
|
||||
displayModel.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
log.debug("Statement to be removed are ");
|
||||
StringWriter r = new StringWriter();
|
||||
removeStatements.write(r, "N3");
|
||||
log.debug(r.toString());
|
||||
r.close();
|
||||
displayModel.remove(removeStatements);
|
||||
|
||||
} catch(Exception ex) {
|
||||
log.error("An error occurred in processing command", ex);
|
||||
errorMessage += "An error occurred and the operation could not be completed successfully.";
|
||||
}finally {
|
||||
displayModel.leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private void processDelete(String pageUri, OntModel displayModel, VitroRequest vreq) {
|
||||
//get the page resource
|
||||
Resource pageResource = getExistingPage(pageUri, displayModel);
|
||||
//if the page is related to a menu item, get the menu item information
|
||||
Resource menuItemResource = getExistingMenuItem(pageResource, displayModel);
|
||||
|
||||
//What statements should be added and removed
|
||||
removeStatements.add(getStatementsToRemove(displayModel, menuItemResource, pageResource));
|
||||
//No statements to add
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//What statements need to be removed
|
||||
private Model getStatementsToRemove(OntModel displayModel,
|
||||
Resource menuItemResource, Resource pageResource) {
|
||||
Model removeModel = ModelFactory.createDefaultModel();
|
||||
removeModel.add(displayModel.listStatements(pageResource, null, (RDFNode) null));
|
||||
//if menu item exists for this page, get all statements related to menu item and page
|
||||
if(menuItemResource != null) {
|
||||
removeModel.add(displayModel.listStatements(menuItemResource, null, (RDFNode) null));
|
||||
//Also remove any statements where menu item resource is an object
|
||||
removeModel.add(displayModel.listStatements(null, null, menuItemResource));
|
||||
}
|
||||
//Get all data getter statements
|
||||
Model associatedDataGettersModel = getDataGettersStatements(pageResource, displayModel);
|
||||
removeModel.add(associatedDataGettersModel);
|
||||
return removeModel;
|
||||
}
|
||||
|
||||
//Get all data getters associated with page
|
||||
private Model getDataGettersStatements(Resource pageResource, OntModel displayModel) {
|
||||
Model dataGettersModel = ModelFactory.createDefaultModel();
|
||||
//To iterate through to get all data getters and then all their statements
|
||||
//PAge to data getter statements have already been added when all page statements were added
|
||||
StmtIterator dataGetterIt = displayModel.listStatements(
|
||||
pageResource,
|
||||
ResourceFactory.createProperty(DisplayVocabulary.HAS_DATA_GETTER),
|
||||
(RDFNode) null);
|
||||
while(dataGetterIt.hasNext()) {
|
||||
Statement dataGetterStmt = dataGetterIt.nextStatement();
|
||||
Resource dataGetterResource = dataGetterStmt.getSubject();
|
||||
dataGettersModel.add(displayModel.listStatements(dataGetterResource, null, (RDFNode) null));
|
||||
}
|
||||
return dataGettersModel;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private Resource getExistingPage(String pageUri, OntModel displayModel) {
|
||||
return ResourceFactory.createResource(pageUri);
|
||||
}
|
||||
|
||||
private Resource getExistingMenuItem(Resource pageResource, OntModel displayModel) {
|
||||
Resource menuItemResource = null;
|
||||
StmtIterator menuItemIt = displayModel.listStatements(null,
|
||||
DisplayVocabulary.TO_PAGE,
|
||||
pageResource);
|
||||
while(menuItemIt.hasNext()) {
|
||||
Statement menuStmt = menuItemIt.nextStatement();
|
||||
menuItemResource = menuStmt.getSubject();
|
||||
}
|
||||
return menuItemResource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//This should be in write mode
|
||||
//TODO: find better way of doing this
|
||||
private OntModel getDisplayModel(VitroRequest vreq) {
|
||||
if(vreq.getAttribute(vreq.SPECIAL_WRITE_MODEL) != null) {
|
||||
return vreq.getWriteModel();
|
||||
} else {
|
||||
return (OntModel) getServletContext().getAttribute("http://vitro.mannlib.cornell.edu/default/vitro-kb-displayMetadata");
|
||||
}
|
||||
}
|
||||
|
||||
Log log = LogFactory.getLog(MenuManagementEdit.class);
|
||||
}
|
|
@ -28,6 +28,7 @@ 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.ResourceFactory;
|
||||
import com.hp.hpl.jena.vocabulary.XSD;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
|
||||
|
@ -88,7 +89,7 @@ public class ManagePageGenerator extends BaseEditConfigurationGenerator implemen
|
|||
prepare(vreq, conf);
|
||||
//This method specifically retrieves information for edit
|
||||
populateExistingDataGetter(vreq, conf, session.getServletContext());
|
||||
|
||||
|
||||
return conf ;
|
||||
}
|
||||
|
||||
|
@ -145,7 +146,7 @@ public class ManagePageGenerator extends BaseEditConfigurationGenerator implemen
|
|||
FieldVTwo menuItemLinkTextField = new FieldVTwo().setName("menuLinkText");
|
||||
conf.addField(menuItemLinkTextField);
|
||||
|
||||
FieldVTwo menuItemPositionField = new FieldVTwo().setName("menuPosition");
|
||||
FieldVTwo menuItemPositionField = new FieldVTwo().setName("menuPosition").setRangeDatatypeUri(XSD.integer.getURI());
|
||||
conf.addField(menuItemPositionField);
|
||||
|
||||
//The actual page content information is stored in this field, and then
|
||||
|
@ -175,18 +176,16 @@ public class ManagePageGenerator extends BaseEditConfigurationGenerator implemen
|
|||
|
||||
String subjectUri = EditConfigurationUtils.getSubjectUri(vreq);
|
||||
String predicateUri = EditConfigurationUtils.getPredicateUri(vreq);
|
||||
|
||||
editConfiguration.setUrlToReturnTo(UrlBuilder.getUrl("/pageList"));
|
||||
//For the case of a new page
|
||||
if(subjectUri == null) {
|
||||
//Once added, return to pageList
|
||||
editConfiguration.setUrlToReturnTo(UrlBuilder.getUrl("/pageList"));
|
||||
editConfiguration.setEntityToReturnTo("?page");
|
||||
editConfiguration.setPredicateUri(predicateUri);
|
||||
|
||||
} else {
|
||||
//For the case of an existing page
|
||||
//Page title pageName or page hasDataGetter dataGetter
|
||||
editConfiguration.setUrlPatternToReturnTo("/individual");
|
||||
editConfiguration.setEntityToReturnTo(subjectUri);
|
||||
//Set update version here
|
||||
//if subject uri = page uri != null or empty, editing existing page
|
||||
|
@ -486,22 +485,15 @@ private String getExistingCustomTemplateQuery() {
|
|||
addNewPageData(vreq, formSpecificData);
|
||||
}
|
||||
|
||||
|
||||
//for menu position, return either existing menu position or get the highest available menu position
|
||||
retrieveMenuPosition(editConfiguration, vreq, formSpecificData);
|
||||
editConfiguration.setFormSpecificData(formSpecificData);
|
||||
}
|
||||
|
||||
private String getTemplate(EditConfigurationVTwo editConfiguration) {
|
||||
String returnTemplate = "default";
|
||||
if(editConfiguration.getSubjectUri() != null) {
|
||||
//Then template is EXISTING template
|
||||
//TODO: Get existing template value for page
|
||||
}
|
||||
return returnTemplate;
|
||||
|
||||
}
|
||||
|
||||
private void addRequiredPageData(VitroRequest vreq, Map<String, Object> data) {
|
||||
MenuManagementDataUtils.includeRequiredSystemData(vreq.getSession().getServletContext(), data);
|
||||
data.put("classGroup", new ArrayList<String>());
|
||||
data.put("classGroups", DataGetterUtils.getClassGroups(vreq.getSession().getServletContext()));
|
||||
}
|
||||
|
||||
private void addExistingPageData(VitroRequest vreq, Map<String, Object> data) {
|
||||
|
@ -514,18 +506,7 @@ private String getExistingCustomTemplateQuery() {
|
|||
|
||||
private void addNewPageData(VitroRequest vreq, Map<String, Object> data) {
|
||||
data.put("title", "Add Menu Item");
|
||||
data.put("menuAction", "Add");
|
||||
//Generate empty values for fields
|
||||
data.put("menuItem", "");
|
||||
data.put("pageName", "");
|
||||
data.put("prettyUrl", "");
|
||||
data.put("associatedPage", "");
|
||||
data.put("associatedPageURI", "");
|
||||
data.put("classGroup", new ArrayList<String>());
|
||||
//not a page already assigned a class group
|
||||
data.put("isClassGroupPage", false);
|
||||
data.put("includeAllClasses", false);
|
||||
data.put("classGroups", DataGetterUtils.getClassGroups(vreq.getSession().getServletContext()));
|
||||
data.put("menuAction", "Add");
|
||||
data.put("selectedTemplateType", "default");
|
||||
//defaults to regular class group page
|
||||
}
|
||||
|
@ -563,5 +544,57 @@ private String getExistingCustomTemplateQuery() {
|
|||
return prefixes + "?page display:hasDataGetter " + dataGetterVar + ".";
|
||||
}
|
||||
|
||||
public void retrieveMenuPosition(EditConfigurationVTwo editConfig, VitroRequest vreq, Map<String, Object> formSpecificData) {
|
||||
boolean returnHighestMenuPosition = false;
|
||||
//if adding a new page or if editing an existing page which does not have an associated
|
||||
//menu position
|
||||
int availableMenuPosition = this.getAvailableMenuPosition(editConfig, vreq);
|
||||
formSpecificData.put("highestMenuPosition", "" + availableMenuPosition);
|
||||
}
|
||||
|
||||
//Get the highest menu position and return that + 1 for the menu position that can be associated
|
||||
//for a new menu item
|
||||
public int getAvailableMenuPosition(EditConfigurationVTwo editConfig, VitroRequest vreq) {
|
||||
//Execute sparql query to get highest menu position
|
||||
int maxMenuPosition = getMaxMenuPosition(editConfig, vreq);
|
||||
return maxMenuPosition + 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private int getMaxMenuPosition(EditConfigurationVTwo editConfig, VitroRequest vreq) {
|
||||
int maxMenuPosition = 0;
|
||||
Literal menuPosition = null;
|
||||
setupModelSelectorsFromVitroRequest(vreq, editConfig);
|
||||
OntModel queryModel = (OntModel)vreq.getAttribute("jenaOntModel");
|
||||
String maxMenuPositionQuery = getMaxMenuPositionQueryString();
|
||||
QueryExecution qe = null;
|
||||
try{
|
||||
Query query = QueryFactory.create(maxMenuPositionQuery);
|
||||
qe = QueryExecutionFactory.create(query, queryModel);
|
||||
ResultSet results = qe.execSelect();
|
||||
while( results.hasNext()){
|
||||
QuerySolution qs = results.nextSolution();
|
||||
menuPosition = qs.getLiteral("menuPosition");
|
||||
}
|
||||
|
||||
} catch(Exception ex) {
|
||||
log.error("Error occurred in executing query " + maxMenuPositionQuery, ex);
|
||||
}
|
||||
|
||||
if(menuPosition != null) {
|
||||
maxMenuPosition = menuPosition.getInt();
|
||||
}
|
||||
return maxMenuPosition;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private String getMaxMenuPositionQueryString() {
|
||||
String query = getSparqlPrefix() +
|
||||
"SELECT ?menuPosition WHERE {?pageUri display:menuPosition ?menuPosition . } " +
|
||||
"ORDER BY DESC(?menuPosition) LIMIT 1";
|
||||
return query;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
|||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationUtils;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditElementVTwo;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.ConstantFieldOptions;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.SelectListGeneratorVTwo;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.beanswrappers.ReadOnlyBeansWrapper;
|
||||
|
@ -95,12 +96,16 @@ public class EditConfigurationTemplateModel extends BaseTemplateModel {
|
|||
WebappDaoFactory wdf = vreq.getWebappDaoFactory();
|
||||
for(String fieldName: editConfig.getFields().keySet()){
|
||||
FieldVTwo field = editConfig.getField(fieldName);
|
||||
//TODO: Check if we even need empty options if field options do not exist
|
||||
if( field.getFieldOptions() == null ){
|
||||
//putting empty map in here because FM can't deal
|
||||
pageData.put(fieldName, Collections.EMPTY_MAP);
|
||||
//empty options
|
||||
field.setOptions(new ConstantFieldOptions());
|
||||
}
|
||||
Map<String, String> optionsMap = SelectListGeneratorVTwo.getOptions(editConfig, fieldName, wdf);
|
||||
optionsMap = SelectListGeneratorVTwo.getSortedMap(optionsMap);
|
||||
if(pageData.containsKey(fieldName)) {
|
||||
log.error("Check the edit configuration setup as pageData already contains " + fieldName + " and this will be overwritten now with empty collection");
|
||||
}
|
||||
pageData.put(fieldName, optionsMap);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue