updates for menu management and institutional internal class

This commit is contained in:
hjkhjk54 2011-07-14 03:18:06 +00:00
parent c44fae1552
commit 3e30283d6b
7 changed files with 138 additions and 50 deletions

View file

@ -6,6 +6,7 @@ import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -22,6 +23,7 @@ import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
@ -79,7 +81,7 @@ public class MenuManagementEdit extends VitroHttpServlet {
String command = getCommand(vreq);
if(command != null) {
processCommand(command, vreq);
processCommand(command, vreq, resp);
} else {
System.out.println("Command is null");
}
@ -119,7 +121,7 @@ public class MenuManagementEdit extends VitroHttpServlet {
//Parameter retrieval is identical, but in one case an entirey new menu item needs to be created
//along with a new page
public void processCommand(String command, VitroRequest vreq) {
public void processCommand(String command, VitroRequest vreq, HttpServletResponse resp) {
//Get parameters for menu item being edited
String menuItem = vreq.getParameter("menuItem");
OntModel displayModel = getDisplayModel(vreq);
@ -139,7 +141,7 @@ public class MenuManagementEdit extends VitroHttpServlet {
} else if(isDelete(command)) {
processDelete(menuItem, displayModel, command, vreq);
} else if(isReorder(command)) {
processReorder(displayModel, vreq);
processReorder(displayModel, vreq, resp);
}
//Edits to model occur here
@ -166,17 +168,34 @@ public class MenuManagementEdit extends VitroHttpServlet {
}
private void processReorder(OntModel displayModel, VitroRequest vreq) {
private void processReorder(OntModel displayModel, VitroRequest vreq, HttpServletResponse resp) {
//Get the new menu positions for all the elements
String predicate = vreq.getParameter("predicate");
//Assuming these two are in the same order
String[]individuals = vreq.getParameterValues("individuals");
String[] positions = vreq.getParameterValues("positions");
String errorMessage = null;
if(individuals.length > 0 && positions.length > 0 && individuals.length == positions.length) {
removeStatements = removePositionStatements(displayModel, individuals);
addStatements = addPositionStatements(displayModel, individuals, positions);
} else {
//Throw an error?
errorMessage = "Number of individuals and positions is out of synch";
}
try{
JSONObject rObj = new JSONObject();
resp.setCharacterEncoding("UTF-8");
resp.setContentType("application/json;charset=UTF-8");
if( errorMessage != null ){
rObj.put("errorMessage", errorMessage);
resp.setStatus(500 /*HttpURLConnection.HTTP_SERVER_ERROR*/);
}else{
rObj.put("errorMessage", "");
}
Writer writer = resp.getWriter();
writer.write(rObj.toString());
} catch(Exception ex) {
log.error("Error creating JSON object for response", ex);
}
}
@ -347,7 +366,9 @@ public class MenuManagementEdit extends VitroHttpServlet {
Model addModel, OntModel displayModel) {
String[] selectedClasses = vreq.getParameterValues("classInClassGroup");
Model dgModel = ModelFactory.createDefaultModel();
dgModel.add(dgModel.createStatement(dataGetterResource, RDF.type, DisplayVocabulary.CLASSINDIVIDUALS_PAGE_TYPE));
dgModel.add(dgModel.createStatement(dataGetterResource,
RDF.type,
ResourceFactory.createResource(DisplayVocabulary.CLASSINDIVIDUALS_PAGE_TYPE)));
for(String classUri: selectedClasses) {
dgModel.add(dgModel.createStatement(
dataGetterResource,
@ -370,7 +391,9 @@ public class MenuManagementEdit extends VitroHttpServlet {
private Model getClassGroupDataGetter(VitroRequest vreq, Resource dataGetterResource, Model addModel,
OntModel displayModel) {
Model dgModel = ModelFactory.createDefaultModel();
dgModel.add(dgModel.createStatement(dataGetterResource, RDF.type, DisplayVocabulary.CLASSGROUP_PAGE_TYPE));
dgModel.add(dgModel.createStatement(dataGetterResource,
RDF.type,
ResourceFactory.createResource(DisplayVocabulary.CLASSGROUP_PAGE_TYPE)));
return dgModel;
}
@ -471,7 +494,9 @@ public class MenuManagementEdit extends VitroHttpServlet {
DisplayVocabulary.MENU_POSITION,
addModel.createTypedLiteral(getLastPosition(displayModel))));
//page resource, type, title and url mapping, and what data getter associated
addModel.add(addModel.createStatement(pageResource, RDF.type, DisplayVocabulary.PAGE_TYPE));
addModel.add(addModel.createStatement(pageResource,
RDF.type,
ResourceFactory.createResource(DisplayVocabulary.PAGE_TYPE)));
//Need to create a data getter
Model dataGetterStatements = generateDataGetter(pageResource, displayModel);
addModel.add(dataGetterStatements);

View file

@ -7,10 +7,12 @@ import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import edu.cornell.mannlib.vedit.beans.Option;
import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
import edu.cornell.mannlib.vitro.webapp.beans.ResourceBean;
import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -18,16 +20,17 @@ import org.apache.commons.logging.LogFactory;
public class LocalNamespaceClassUtils {
private static final Log log = LogFactory.getLog(LocalNamespaceClassUtils.class.getName());
public static List<VClass> getLocalNamespacesClasses(VitroRequest vreq, List<String> namespace) {
HashMap<String, String> namespaceHash = convertToHash(namespace);
List<VClass> localClasses = new ArrayList<VClass>();
//Expects hash where key = namespace uri
//return hash where key = class uri, and value = display Name + (prefix) of ontology
public static HashMap<String, String> getLocalNamespacesClasses(VitroRequest vreq, HashMap<String, String> namespaces) {
HashMap<String, String> localClasses = new HashMap<String, String>();
List<VClass> allClasses = vreq.getWebappDaoFactory().getVClassDao().getAllVclasses();
for(VClass v: allClasses) {
String classNamespace = v.getNamespace();
String classUri = v.getURI();
System.out.println("uri is " + classUri + " and namespace is " + classNamespace);
if(namespaceHash.containsKey(classNamespace)){
localClasses.add(v);
if(namespaces.containsKey(classNamespace)){
localClasses.put(classUri, v.getName() + " ( " + namespaces.get(classNamespace) + ")");
}
}
return localClasses;
@ -43,24 +46,32 @@ public class LocalNamespaceClassUtils {
//Retrieve all VClasses and sort into local namespaces
//TODO: Check better mechanism utilizing sparql query
public static List<String> getLocalOntologyNamespaces(VitroRequest vreq) {
//Can't depend on retrieval of classes b/c an ontology may not have any classes yet
//Display name and URI, with URI being key
public static HashMap<String, String> getLocalOntologyNamespaces(VitroRequest vreq) {
HashMap<String, String> foundNamespaces = new HashMap<String, String>();
String defaultNamespacePattern = getDefaultOntologyNamespace(vreq);
List<String> localNamespaces = new ArrayList<String>();
List<VClass> allClasses = vreq.getWebappDaoFactory().getVClassDao().getAllVclasses();
for(VClass v: allClasses) {
String namespace = v.getNamespace();
if(namespace.startsWith(defaultNamespacePattern) && !foundNamespaces.containsKey(namespace)) {
foundNamespaces.put(namespace, "true");
//Get all namespacs
//There's an APP for that!
OntologyDao dao = vreq.getFullWebappDaoFactory().getOntologyDao();
List<Ontology> onts = dao.getAllOntologies();
for(Ontology on: onts) {
String uri = on.getURI();
if(uri.startsWith(defaultNamespacePattern)) {
String name = on.getName();
String prefix = on.getPrefix();
foundNamespaces.put(uri, name + " (" + prefix + ")");
}
}
localNamespaces.addAll(foundNamespaces.keySet());
return localNamespaces;
}
return foundNamespaces;
}
public static String getDefaultOntologyNamespace(VitroRequest vreq) {
String defaultNamespace= vreq.getWebappDaoFactory().getDefaultNamespace();
defaultNamespace = defaultNamespace.substring(0, defaultNamespace.lastIndexOf("/")) + "ontology/";
//Assuming following linked data approach so expects /individual at end
defaultNamespace = defaultNamespace.substring(0, defaultNamespace.lastIndexOf("/individual")) + "/ontology/";
return defaultNamespace;
}
}

View file

@ -6,9 +6,15 @@ import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.rdf.model.RDFNode;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -18,6 +24,7 @@ import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.ManageMenu
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.RedirectResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
@ -36,8 +43,8 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
private static final String EDIT_FORM = "/processInstitutionalInternalClass";
public final static Actions REQUIRED_ACTIONS = new Actions(new ManageMenus());
private static final String DISPLAY_FORM = "/institutionalInternalClassForm.ftl";
private static List<String> localNamespaces = new ArrayList<String>();
private static List<VClass> localNamespaceClasses = new ArrayList<VClass>();
private static HashMap<String, String> localNamespaces = new HashMap<String, String>();
private static HashMap<String, String> localNamespaceClasses = new HashMap<String, String>();
private static final String CREATE_CLASS_PARAM = "createClass";
private static final String REDIRECT_PAGE = "/siteAdmin";
@Override
@ -54,9 +61,12 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
Map<String, Object> data = new HashMap<String,Object>();
//Get all local classes and namespace information
retrieveLocalClasses(vreq, data);
if(isSelectExistingClass(vreq)) {
if(isSubmission(vreq)){
processSubmission(vreq, data);
} else if(isSelectExistingClass(vreq)) {
//Local namespace(s) exist and user can select an existing class
processSelectExistingClass(vreq, data);
} else if(isCreateNewClass(vreq)) {
//Local namespace(s) exist and user wishes to create a new class
//Either cmd = create new or no local classes exist at all and one must be created
@ -65,8 +75,6 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
//Not being handled expliclity but message will display indicating
//no local namespaces exist and one must be created
processCreateOntologies(vreq, data);
} else if(isSubmission(vreq)){
processSubmission(vreq, data);
} else {
}
@ -75,14 +83,18 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
//Check if existing local namespaces
data.put("formUrl", EDIT_FORM);
data.put("formUrl", vreq.getContextPath() + EDIT_FORM);
data.put("cancelUrl", vreq.getContextPath() + REDIRECT_PAGE);
//if no local namespaces, then provide message to display
//if existing namespace(s), then check
//if single namespace, retrieve all classes belonging to that local namespace
//if multiple namespaces, generate select list with namespaces
//for instertion: VClassDaoJena.insertVClass
//
if(isSubmission(vreq)){
return redirectToSiteAdmin();
}
return new TemplateResponseValues(DISPLAY_FORM, data);
}
@ -105,7 +117,7 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
private void processCreateNewClass(VitroRequest vreq, Map<String, Object> data) {
//this may need to be changed on the basis of how new classes interact with new ontologies
data.put("submitAction", "createClass");
data.put("submitAction", "Create Class");
data.put("createNewClass", true);
}
@ -121,7 +133,7 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
private void processSelectExistingClass(VitroRequest vreq, Map<String, Object> data) {
//Check if internal class is already set and be sure to include that in the data to be returned
data.put("useExistingInternalClass", true);
data.put("submitAction", "save");
data.put("submitAction", "Save");
}
private boolean isSelectExistingClass(VitroRequest vreq) {
@ -135,8 +147,8 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
localNamespaces = LocalNamespaceClassUtils.getLocalOntologyNamespaces(vreq);
//Get classes for local namespaces
localNamespaceClasses = LocalNamespaceClassUtils.getLocalNamespacesClasses(vreq, localNamespaces);
data.put("existingLocalClasses", localNamespaces);
data.put("existingLocalNamespaces", localNamespaceClasses);
data.put("existingLocalClasses", localNamespaceClasses);
data.put("existingLocalNamespaces", localNamespaces);
String noLocalOntologiesMessage = "There are currently no local ontologies. You must create a new ontology";
data.put("noLocalOntologiesMessage", noLocalOntologiesMessage);
if(localNamespaces.size() == 0) {
@ -146,7 +158,12 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
data.put("ontologiesExist", true);
if(localNamespaces.size() > 1) {
data.put("multipleLocalNamespaces", true);
} else {
data.put("multipleLocalNamespaces", false);
data.put("existingLocalNamespace", localNamespaces.keySet().iterator().next());
}
//Get current internal class if it exists
data.put("existingInternalClass", retrieveCurrentInternalClass());
}
}
@ -172,6 +189,12 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
Model writeModel = ModelContext.getBaseOntModelSelector(getServletContext()).getTBoxModel();
writeModel.enterCriticalSection(Lock.WRITE);
try {
//remove existing internal classes if there are any as assuming only one
writeModel.remove(
writeModel.listStatements(null,
ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT),
(RDFNode) null));
writeModel.add(
writeModel.createStatement(
ResourceFactory.createResource(classUri),
@ -205,4 +228,21 @@ public class InstitutionalInternalClassController extends FreemarkerHttpServlet
}
private RedirectResponseValues redirectToSiteAdmin() {
return new RedirectResponseValues(REDIRECT_PAGE, HttpServletResponse.SC_SEE_OTHER);
}
//Get current internal class
private String retrieveCurrentInternalClass() {
String internalClassUri = "";
OntModel mainModel = (OntModel) getServletContext().getAttribute("jenaOntModel");
StmtIterator internalIt = mainModel.listStatements(null,
ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT),
(RDFNode) null);
if(internalIt.hasNext()){
internalClassUri = internalIt.nextStatement().getResource().getURI();
}
return internalClassUri;
}
}

View file

@ -287,8 +287,10 @@ public class MenuManagementController extends FreemarkerHttpServlet {
}
private void retrieveIndividualsForClassesPage(OntModel writeModel,
Resource dataGetter, Map<String, Object> data) {
Resource dataGetter, Map<String, Object> data) {
data.put("isIndividualsForClassesPage", true);
data.put("isClassGroupPage", false);
data.put("includeAllClasses", false);
//Get the classes and put them here
this.getClassesForDataGetter(writeModel, dataGetter, data);
//Also save the class group for display

View file

@ -11,9 +11,9 @@ edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.Institu
<h3>Institutional Internal Class</h3>
<section id="introMessage" role="region">
This class will be used to designate those individuals internal to your institution.
<p>This class will be used to designate those individuals internal to your institution.
This will allow you to limit the individuals displayed on your menu pages (People, Research, etc.)
to only those within your institution.
to only those within your institution.</p>
</section>
<section>
@ -21,17 +21,19 @@ edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.Institu
<input type="hidden" name="submitForm" id="submitForm" value="true" />
<#if ontologiesExist = false>
<section id="noLocalOntologyExists">
${noLocalOntologiesMessage}
<p>${noLocalOntologiesMessage}</p>
</section>
<#elseif useExistingInternalClass?has_content>
<section id="existingLocalClass">
<#--Populated based on class list returned-->
<select id="existingLocalClasses" name="existingLocalClasses">
<#list localClasses as localClass>
<option value="${localClass.URI}" <#if existingInternalClass.URI = localClass.URI>selected</#if> >${localClass.name}</option>
<#assign classUris = existingLocalClasses?keys />
<#list classUris as localClassUri>
<option value="${localClassUri}" <#if existingInternalClass = localClassUri>selected</#if> >${existingLocalClasses[localClassUri]}</option>
</#list>
</select>
<p>Can't find an appropriate class? Create a <a href="${formUrl}?cmd=createClass">new one</a>.</p>
</section>
<#elseif createNewClass?has_content>
<section id="createNewLocalClass">
@ -42,20 +44,26 @@ edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.generators.Institu
<#--If more than one local namespace, generate select-->
<#if multipleLocalNamespaces = true>
<select id="existingLocalNamespaces" name="existingLocalNamespaces">
<#list existingLocalNamespaces as existingNamespace>
<option value="${existingNamespace.URI}">"${existingNamespace.URI}"</option>
<#assign namespaceUris = existingLocalNamespaces?keys />
<#list namespaceUris as existingNamespace>
<option value="${existingNamespace}">${existingLocalNamespaces[existingNamespace]}</option>
</#list>
</select>
<#else>
<input type="hidden" id="existingLocalNamespaces" name="existingLocalNamespaces" value="{existingLocalNamespaces[0]}"/>
<input type="hidden" id="existingLocalNamespaces" name="existingLocalNamespaces" value="${existingLocalNamespace}"/>
</#if>
</section>
<#else>
Problematic section as above should all have been handled
</#if>
<input type="submit" name="submit-internalClass" value="${submitAction}" class="submit" /> or <a class="cancel" href="${cancelUrl}">Cancel</a>
<p class="requiredHint">* required fields</p>
<#if ontologiesExist = true>
<input type="submit" name="submit-internalClass" value="${submitAction}" class="submit" /> or <a class="cancel" href="${cancelUrl}">Cancel</a>
<p class="requiredHint">* required fields</p>
</#if>
</form>
</section>

View file

@ -7,8 +7,10 @@
<section id="remove-menu-item" role="region">
<form method="POST" action="${formUrls}" class="customForm" role="remove menu item">
<input type="hidden" name="menuItem" id="menuItem" value="${menuItem}" role="input" />
<p>Are you sure you want to remove <em>${menuItem}</em> menu item?</p>
<input type="hidden" name="cmd" id="cmd" value="Remove" role="input" />
<input type="hidden" name="switchToDisplayModel" id="switchToDisplayModel" value="true" role="input" />
<p>Are you sure you want to remove <em>${menuName}</em> menu item?</p>
<input type="submit" name="removeMenuItem" value="Remove menu item" class="submit" role="input" /> or <a class="cancel" href="${cancelUrl}">Cancel</a>
</form>

View file

@ -74,14 +74,14 @@
</li>
<#list classGroup as classInClassGroup>
<li class="ui-state-default" role="menuitem">
<input type="checkbox" id="classInClassGroup" name="classInClassGroup" value="${classInClassGroup.URI}" <#if includeAllClasses = true>checked</#if> />
<input type="checkbox" id="classInClassGroup" name="classInClassGroup" value="${classInClassGroup.URI}" <#if includeAllClasses = true>checked</#if>
<#if isIndividualsForClassesPage?has_content>
<#list includeClasses as includeClass>
<#if includeClass = classInClassGroup.URI>
checked
</#if>
</#list>
</#if>
</#if> />
<label class="inline" for="${classInClassGroup.name}"> ${classInClassGroup.name}</label>
<span class="ui-icon-sortable"></span> <#--sortable icon for dragging and dropping menu items-->
</li>