From fcdff9ea541e3b03fbd7ea09071326027268a424 Mon Sep 17 00:00:00 2001 From: hjkhjk54 Date: Tue, 2 Aug 2011 15:24:29 +0000 Subject: [PATCH] Updates for NIHVIVO-3086 and NIHVIVO-2980 and subtasks: updating page data getters to rely on java class name, separating out internal class-specific set up and processing for menu management into VIVO code. --- productMods/WEB-INF/ontologies/app/menu.n3 | 16 +- .../menuManagement--classIntersections.ftl | 2 +- .../InstitutionalInternalClassController.java | 261 ++++++++++++++++++ .../InternalClassesDataGetter.java | 89 ++++++ .../MenuManagementDataUtils.java | 85 ++++++ .../ProcessInternalClasses.java | 106 +++++++ .../pageDataGetter/SelectDataGetterUtils.java | 99 +++++++ 7 files changed, 651 insertions(+), 7 deletions(-) create mode 100644 src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/InstitutionalInternalClassController.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/InternalClassesDataGetter.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/MenuManagementDataUtils.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/ProcessInternalClasses.java create mode 100644 src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/SelectDataGetterUtils.java diff --git a/productMods/WEB-INF/ontologies/app/menu.n3 b/productMods/WEB-INF/ontologies/app/menu.n3 index a2cd977f..8acc8a52 100644 --- a/productMods/WEB-INF/ontologies/app/menu.n3 +++ b/productMods/WEB-INF/ontologies/app/menu.n3 @@ -59,7 +59,8 @@ display:Home a display:HomePage ; a display:Page ; display:title "Home" ; - display:urlMapping "/" . + display:urlMapping "/" ; + display:hasDataGetter display:homeDataGetter . display:Events a display:Page ; @@ -96,21 +97,24 @@ display:Research #The data getter objects used above - a ; + a ; . - a ; + a ; . - a ; + a ; . - a ; + a ; - . \ No newline at end of file + . + + + a . \ No newline at end of file diff --git a/productMods/templates/freemarker/edit/forms/menuManagement--classIntersections.ftl b/productMods/templates/freemarker/edit/forms/menuManagement--classIntersections.ftl index e9f1b609..1557cae2 100644 --- a/productMods/templates/freemarker/edit/forms/menuManagement--classIntersections.ftl +++ b/productMods/templates/freemarker/edit/forms/menuManagement--classIntersections.ftl @@ -9,7 +9,7 @@ <#assign disableClass = 'class="disable inline" disabled="disabled"' /> - checked role="input" /> + checked role="input" /> ${enableInternalClass} diff --git a/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/InstitutionalInternalClassController.java b/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/InstitutionalInternalClassController.java new file mode 100644 index 00000000..71856a6d --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/InstitutionalInternalClassController.java @@ -0,0 +1,261 @@ +/* $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 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 com.hp.hpl.jena.rdf.model.Statement; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + + +import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions; +import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.ManageMenus; +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; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; + +import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; +import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent; +import edu.cornell.mannlib.vitro.webapp.controller.edit.utils.LocalNamespaceClassUtils; +/* + * Custom controller for menu management. This will be replaced later once N3 Editing + * has been successfully refactored and integrated with menu management. + */ +public class InstitutionalInternalClassController extends FreemarkerHttpServlet { + private static final Log log = LogFactory.getLog(InstitutionalInternalClassController.class); + + 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 HashMap localNamespaces = new HashMap(); + private static HashMap localNamespaceClasses = new HashMap(); + private static final String CREATE_CLASS_PARAM = "createClass"; + private static final String REDIRECT_PAGE = "/siteAdmin"; + @Override + protected Actions requiredActions(VitroRequest vreq) { + return REQUIRED_ACTIONS; + } + + @Override + protected ResponseValues processRequest(VitroRequest vreq) { + + //Based on existing of local namespaces and number of local classes present + //as well as command parameter, execute command + + Map data = new HashMap(); + //Get all local classes and namespace information + retrieveLocalClasses(vreq, data); + if(isSubmission(vreq)){ + processSubmission(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 + processCreateNewClass(vreq, data); + } else if(isSelectExistingClass(vreq)) { + //Local namespace(s) exist and user can select an existing class + processSelectExistingClass(vreq, data); + } else if(isCreateOntologies(vreq)) { + //Not being handled expliclity but message will display indicating + //no local namespaces exist and one must be created + processCreateOntologies(vreq, data); + } else { + + } + //Retrieve local namespaces + + + //Check if existing local namespaces + + 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); + + } + + private boolean isSubmission(VitroRequest vreq) { + String submit = vreq.getParameter("submitForm"); + return(submit!= null && !submit.isEmpty()); + } + + private void processCreateOntologies(VitroRequest vreq, Map data) { + data.put("submitAction", ""); + + } + + private boolean isCreateOntologies(VitroRequest vreq) { + //no local namespaces + return (localNamespaces.size() == 0); + + } + + private void processCreateNewClass(VitroRequest vreq, Map data) { + //this may need to be changed on the basis of how new classes interact with new ontologies + data.put("submitAction", "Create Class"); + data.put("createNewClass", true); + } + + private boolean isCreateNewClass(VitroRequest vreq) { + String command = vreq.getParameter("cmd"); + if(command != null && command.equals(CREATE_CLASS_PARAM)) { + return true; + } + //If local namespace exists but no classes in local namespaces, then need to enable creation of new classes + return(localNamespaces.size() > 0 && localNamespaceClasses.size() == 0); + } + + private void processSelectExistingClass(VitroRequest vreq, Map data) { + //Check if local classes exist and use for selection + data.put("useExistingLocalClass", true); + data.put("submitAction", "Save"); + } + + private boolean isSelectExistingClass(VitroRequest vreq) { + //Local namespaces exist and there are existing classes within those namespaces + return (localNamespaces.size() > 0 && localNamespaceClasses.size() > 0); + } + + + + private void retrieveLocalClasses(VitroRequest vreq, Map data) { + localNamespaces = LocalNamespaceClassUtils.getLocalOntologyNamespaces(vreq); + //Get classes for local namespaces + localNamespaceClasses = LocalNamespaceClassUtils.getLocalNamespacesClasses(vreq, localNamespaces); + 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) { + data.put("ontologiesExist", false); + } + else { + 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()); + } + //Place default namespace within data to pass back to template + String defaultNamespace = vreq.getWebappDaoFactory().getDefaultNamespace(); + data.put("defaultNamespace", defaultNamespace); + } + + + //Process submission on submitting form + private void processSubmission(VitroRequest vreq, Map data) { + //If new class, need to generate new class + String classUri = null; + if(isNewClassSubmission(vreq)){ + VClass v= generateNewVClass(vreq.getParameter("localClassName"), vreq.getParameter("existingLocalNamespaces")); + classUri = v.getURI(); + try { + vreq.getWebappDaoFactory().getVClassDao().insertNewVClass(v); + } catch(Exception ex) { + log.error("Insertion of new class " + vreq.getParameter("name") + " resulted in error ", ex); + } + } else { + //Existing class so get URI from that + classUri = getExistingClassUri(vreq); + } + //If existing class, need to simply add a statement specifying existing class is an internal class + if(classUri != null && !classUri.isEmpty()) { + Model writeModel = ModelContext.getBaseOntModelSelector(getServletContext()).getTBoxModel(); + writeModel.enterCriticalSection(Lock.WRITE); + writeModel.notifyEvent(new EditEvent(null,true)); + try { + log.debug("Should be removing these statements " + writeModel.listStatements(null, + ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT), + (RDFNode) null).toList().toString()); + //remove existing internal classes if there are any as assuming only one + writeModel.removeAll(null, + ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT), + (RDFNode) null); + log.debug("Are there any statements left for internal class annotation: " + writeModel.listStatements(null, + ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT), + (RDFNode) null).toList().toString()); + writeModel.add( + writeModel.createStatement( + ResourceFactory.createResource(classUri), + ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT), + writeModel.createLiteral("true"))); + } catch(Exception ex) { + log.error("Error occurred in adding statement for " + classUri + " becoming internal class", ex); + } finally { + writeModel.notifyEvent(new EditEvent(null,true)); + writeModel.leaveCriticalSection(); + } + } + } + + private VClass generateNewVClass(String newClassName, String namespace) { + VClass newClass = new VClass(); + newClass.setName(newClassName); + newClass.setNamespace(namespace); + String uri = namespace + newClassName.replaceAll(" ", ""); + newClass.setURI(uri); + //How to g + return newClass; + } + + private boolean isNewClassSubmission(VitroRequest vreq) { + String localName = vreq.getParameter("localClassName"); + return (localName != null && !localName.isEmpty()); + } + + private String getExistingClassUri(VitroRequest vreq) { + return vreq.getParameter("existingLocalClasses"); + + } + + private RedirectResponseValues redirectToSiteAdmin() { + return new RedirectResponseValues(REDIRECT_PAGE, HttpServletResponse.SC_SEE_OTHER); + } + + //Get current internal class + private String retrieveCurrentInternalClass() { + String internalClassUri = ""; + Model mainModel = ModelContext.getBaseOntModelSelector(getServletContext()).getTBoxModel(); + StmtIterator internalIt = mainModel.listStatements(null, + ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT), + (RDFNode) null); + while(internalIt.hasNext()){ + Statement s = internalIt.nextStatement(); + //The class IS an internal class so the subject is what we're looking for + internalClassUri = s.getSubject().getURI(); + log.debug("Found internal class uri " + internalClassUri); + } + return internalClassUri; + } + +} diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/InternalClassesDataGetter.java b/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/InternalClassesDataGetter.java new file mode 100644 index 00000000..9d93c248 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/InternalClassesDataGetter.java @@ -0,0 +1,89 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter; + +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; + +import org.apache.commons.lang.StringUtils; +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.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.RDFNode; +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 edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; +import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.PageDao; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; +import edu.cornell.mannlib.vitro.webapp.web.templatemodels.VClassGroupTemplateModel; +import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; + +/** + * This will pass these variables to the template: + * classGroupUri: uri of the classgroup associated with this page. + * vClassGroup: a data structure that is the classgroup associated with this page. + */ +public class InternalClassesDataGetter extends IndividualsForClassesDataGetter{ + private static final Log log = LogFactory.getLog(InternalClassesDataGetter.class); + + //Use different template name for internal class template + @Override + protected void setTemplateName() { + super.restrictClassesTemplateName = "internalClass"; + } + + //Retrieve classes and check whether or not page to be filtered by internal class only + @Override + protected Map getClassIntersectionsMap(PageDao pageDao, + String pageUri) { + // TODO Auto-generated method stub + return pageDao.getClassesAndCheckInternal(pageUri); + } + + + //Retrieve current internal class uri to restrict by + @Override + protected List retrieveRestrictClasses( + ServletContext context, Map classIntersectionsMap) { + List restrictClasses = new ArrayList(); + String internalClass = (String) classIntersectionsMap.get("isInternal"); + //if internal class restriction specified and is true + if(internalClass != null && internalClass.equals("true")) { + //Get internal class + Model mainModel = ModelContext.getBaseOntModelSelector(context).getTBoxModel();; + StmtIterator internalIt = mainModel.listStatements(null, ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT), (RDFNode) null); + //Checks for just one statement + if(internalIt.hasNext()){ + Statement s = internalIt.nextStatement(); + //The class IS an internal class so the subject is what we're looking for + String internalClassUri = s.getSubject().getURI(); + log.debug("Found internal class uri " + internalClassUri); + restrictClasses.add(internalClassUri); + } + } + + return restrictClasses; + } + + @Override + public String getType(){ + return DataGetterUtils.generateDataGetterTypeURI(InternalClassesDataGetter.class.getName()); + } + +} \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/MenuManagementDataUtils.java b/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/MenuManagementDataUtils.java new file mode 100644 index 00000000..6c5a0c00 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/MenuManagementDataUtils.java @@ -0,0 +1,85 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter; + +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; +import org.json.JSONObject; + +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.StmtIterator; +import com.hp.hpl.jena.vocabulary.RDF; + +import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; +import edu.cornell.mannlib.vitro.webapp.controller.Controllers; +import edu.cornell.mannlib.vitro.webapp.controller.JsonServlet; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.PageRecord; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; +import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; +import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; +import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; + +/* + * This class includes methods that help in selecting a data getter based on + * parameters, and VIVO will have its own version or extend this + */ +public class MenuManagementDataUtils { + private static final Log log = LogFactory.getLog(MenuManagementDataUtils.class); + + //Data that is to be returned to template that does not involve data getters + //e.g. what are the current class groups, etc. + public static void includeRequiredSystemData(ServletContext context, Map templateData) { + + checkInstitutionalInternalClass(context, templateData); + } + + //Check whether any classes exist with internal class restrictions + private static void checkInstitutionalInternalClass(ServletContext context, Map templateData) { + //TODO: replace with more generic ModelContext retrieval method + String internalClass = retrieveInternalClass(context); + if(internalClass != null) { + templateData.put("internalClass", internalClass); + templateData.put("internalClassUri", internalClass); + } else { + //need to initialize to empty string anyway + templateData.put("internalClassUri", ""); + } + + } + + private static String retrieveInternalClass(ServletContext context) { + OntModel mainModel = ModelContext.getBaseOntModelSelector(context).getTBoxModel(); + StmtIterator internalIt = mainModel.listStatements(null, ResourceFactory.createProperty(VitroVocabulary.IS_INTERNAL_CLASSANNOT), (RDFNode) null); + if(internalIt.hasNext()) { + String internalClass = internalIt.nextStatement().getSubject().getURI(); + return internalClass; + } + return null; + } + + +} \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/ProcessInternalClasses.java b/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/ProcessInternalClasses.java new file mode 100644 index 00000000..dcf34913 --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/ProcessInternalClasses.java @@ -0,0 +1,106 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter; + +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; +import org.json.JSONObject; + +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.StmtIterator; +import com.hp.hpl.jena.vocabulary.RDF; + +import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; +import edu.cornell.mannlib.vitro.webapp.controller.Controllers; +import edu.cornell.mannlib.vitro.webapp.controller.JsonServlet; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.PageRecord; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; +import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; +import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; + +/* + * Handle processing of data retrieved from IndividualsForClasses data getter to return to form template + * and handle processing of form submission to create the appropriate individuals for classes data getter + */ +public class ProcessInternalClasses extends ProcessIndividualsForClasses { + private static final Log log = LogFactory.getLog(ProcessInternalClasses.class); + + /**Retrieve and populate**/ + + //Based on institutional internal page and not general individualsForClasses + @Override + protected void populateRestrictedClasses(Map pageData, Map templateData) { + //for internal page, restrict results by internal is true or false, otherwise get + //actual restriction classes? + //Get internal class + String internalClassUris = (String) pageData.get("internalClass"); + if(internalClassUris != null && !internalClassUris.isEmpty()) { + templateData.put("isInternal", "true"); + } + } + + /**Process submission**/ + //Check and see if we should use this process + //Use this if either internal class is selected or all classes have been selected + public boolean useProcessor(VitroRequest vreq) { + return(internalClassSelected(vreq) || !allClassesSelected(vreq)); + } + public Model processSubmission(VitroRequest vreq, Resource dataGetterResource) { + String dataGetterTypeUri = new InternalClassesDataGetter().getType(); + String[] selectedClasses = vreq.getParameterValues("classInClassGroup"); + Model dgModel = ModelFactory.createDefaultModel(); + dgModel.add(dgModel.createStatement(dataGetterResource, + RDF.type, + ResourceFactory.createResource(dataGetterTypeUri))); + for(String classUri: selectedClasses) { + dgModel.add(dgModel.createStatement( + dataGetterResource, + ResourceFactory.createProperty(DisplayVocabulary.GETINDIVIDUALS_FOR_CLASS), + ResourceFactory.createResource(classUri))); + } + + //Also check if internal class checked + if(internalClassSelected(vreq)) { + dgModel.add(dgModel.createStatement( + dataGetterResource, + ResourceFactory.createProperty(DisplayVocabulary.RESTRICT_RESULTS_BY_INTERNAL), + dgModel.createLiteral("true"))); + } + return dgModel; + } + + private boolean allClassesSelected(VitroRequest vreq) { + String allClasses = vreq.getParameter("allSelected"); + return (allClasses != null && !allClasses.isEmpty()); + } + + private boolean internalClassSelected(VitroRequest vreq) { + String internalClass = vreq.getParameter("display-internalClass"); + return (internalClass != null && !internalClass.isEmpty()); + } + +} \ No newline at end of file diff --git a/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/SelectDataGetterUtils.java b/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/SelectDataGetterUtils.java new file mode 100644 index 00000000..449f072b --- /dev/null +++ b/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/SelectDataGetterUtils.java @@ -0,0 +1,99 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter; + +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; +import org.json.JSONObject; + +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.StmtIterator; +import com.hp.hpl.jena.vocabulary.RDF; + +import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; +import edu.cornell.mannlib.vitro.webapp.beans.Individual; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup; +import edu.cornell.mannlib.vitro.webapp.controller.Controllers; +import edu.cornell.mannlib.vitro.webapp.controller.JsonServlet; +import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController.PageRecord; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.IndividualListController; +import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder; +import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; +import edu.cornell.mannlib.vitro.webapp.dao.jena.VClassGroupCache; +import edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.*; + + +/* + * This class includes methods that help in selecting a data getter based on + * parameters, and VIVO will have its own version or extend this + */ +public class SelectDataGetterUtils { + private static final Log log = LogFactory.getLog(SelectDataGetterUtils.class); + + /**Get data for an existing page and set variables for the template accordingly**/ + + + public static void processAndRetrieveData(VitroRequest vreq, ServletContext context, Map pageData, String dataGetterClass, Map templateData) { + //The type of the data getter will show how to process the data from the data getter + ProcessDataGetter processor = selectProcessor(dataGetterClass); + processor.populateTemplate(context, pageData, templateData); + } + + //This will be different in VIVO than in VITRO + private static ProcessDataGetter selectProcessor(String dataGetterClass) { + if(dataGetterClass.equals(ClassGroupPageData.class.getName())) { + return new ProcessClassGroup(); + } else if(dataGetterClass.equals(InternalClassesDataGetter.class.getName())) { + return new ProcessInternalClasses(); + //below should be for vitro specific version + //return new ProcessIndividualsForClasses(); + } + return null; + } + + + /**Process parameters from form and select appropriate data getter on this basis **/ + public static Model createDataGetterModel(VitroRequest vreq, Resource dataGetterResource) { + Model dataGetterModel = null; + if(dataGetterResource != null) { + //If "All selected" then use class group else use individuals for classes + dataGetterModel = ModelFactory.createDefaultModel(); + + ProcessInternalClasses individualsProcess = new ProcessInternalClasses(); + + ProcessClassGroup classGroupProcess = new ProcessClassGroup(); + if(individualsProcess.useProcessor(vreq)) { + dataGetterModel = individualsProcess.processSubmission(vreq, dataGetterResource); + } else { + dataGetterModel = classGroupProcess.processSubmission(vreq, dataGetterResource); + } + + + } else { + log.error("Data getter is null "); + } + return dataGetterModel; + + } + +} \ No newline at end of file