Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browse filter results on page #473

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public class DisplayVocabulary {

/* URI of property for Fixed HTML Generator */
public static final String FIXED_HTML_VALUE = DISPLAY_NS + "htmlValue";
public static final String SEARCH_FILTER_VALUE = DISPLAY_NS + "searchFilter";


/* URI of property for Search Query Generator */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ 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));
data.put("searchFilter", new ArrayList<String>());
data.put("searchFilters", DataGetterUtils.getSearchFilters(vreq));
//for search individuals data get getter
data.put("classes", this.getAllVClasses(vreq));
data.put("availablePermissions", this.getAvailablePermissions(vreq));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,37 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.ClassGroupPageData;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.FixedHTMLDataGetter;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.IndividualsForClassesDataGetter;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SearchFilterValuesDataGetter;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SearchIndividualsDataGetter;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter;

/*
* This class determines what n3 should be returned for a particular data getter and can be overwritten or extended in VIVO.
*/
public class ProcessDataGetterN3Map {
private static final Log log = LogFactory.getLog(ProcessDataGetterN3Map.class);

private static HashMap<String, String> dataGetterMap;
private static HashMap<String, Class> dataGetterMap;

static {
dataGetterMap = new HashMap<String, String>();
dataGetterMap.put("edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter", "edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessSparqlDataGetterN3");
dataGetterMap.put("edu.cornell.mannlib.vitro.webapp.utils.dataGetter.ClassGroupPageData", "edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessClassGroupDataGetterN3");
dataGetterMap.put("edu.cornell.mannlib.vitro.webapp.utils.dataGetter.IndividualsForClassesDataGetter", "edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessIndividualsForClassesDataGetterN3");
dataGetterMap.put("edu.cornell.mannlib.vitro.webapp.utils.dataGetter.FixedHTMLDataGetter", "edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessFixedHTMLN3");
dataGetterMap.put("edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SearchIndividualsDataGetter", "edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils.ProcessSearchIndividualsDataGetterN3");
dataGetterMap = new HashMap<String, Class>();
dataGetterMap.put(SparqlQueryDataGetter.class.getCanonicalName(), ProcessSparqlDataGetterN3.class);
dataGetterMap.put(ClassGroupPageData.class.getCanonicalName(), ProcessClassGroupDataGetterN3.class);
dataGetterMap.put(SearchFilterValuesDataGetter.class.getCanonicalName(), ProcessSearchFilterValuesDataGetterN3.class);
dataGetterMap.put(IndividualsForClassesDataGetter.class.getCanonicalName(), ProcessIndividualsForClassesDataGetterN3.class);
dataGetterMap.put(FixedHTMLDataGetter.class.getCanonicalName(), ProcessFixedHTMLN3.class);
dataGetterMap.put(SearchIndividualsDataGetter.class.getCanonicalName(), ProcessSearchIndividualsDataGetterN3.class);
}

public static HashMap<String, String> getDataGetterTypeToProcessorMap() {
public static HashMap<String, Class> getDataGetterTypeToProcessorMap() {
return dataGetterMap;
}

public static void replaceDataGetterMap(HashMap<String, String> newMap) {
dataGetterMap = new HashMap<String, String>();
public static void replaceDataGetterMap(HashMap<String, Class> newMap) {
dataGetterMap = new HashMap<String, Class>();
dataGetterMap.putAll(newMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ public class ProcessDataGetterN3Utils {
private static final Log log = LogFactory.getLog(ProcessDataGetterN3Utils.class);

public static ProcessDataGetterN3 getDataGetterProcessorN3(String dataGetterClass, ObjectNode jsonObject) {
HashMap<String, String> map = ProcessDataGetterN3Map.getDataGetterTypeToProcessorMap();
HashMap<String, Class> map = ProcessDataGetterN3Map.getDataGetterTypeToProcessorMap();
//
if(map.containsKey(dataGetterClass)) {
String processorClass = map.get(dataGetterClass);
Class<?> processorClass = map.get(dataGetterClass);
try {
ProcessDataGetterN3 pn = instantiateClass(processorClass, jsonObject);
return pn;
} catch(Exception ex) {
log.error("Exception occurred in trying to get processor class for n3 for " + dataGetterClass, ex);
return null;
}
}
}
log.error(ProcessDataGetterN3Map.class.getSimpleName() + " doesn't contain processor class for n3 for " + dataGetterClass);
chenejac marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

private static ProcessDataGetterN3 instantiateClass(String processorClass, ObjectNode jsonObject) {
private static ProcessDataGetterN3 instantiateClass(Class<?> processorClass, ObjectNode jsonObject) {
ProcessDataGetterN3 pn = null;
try {
Class<?> clz = Class.forName(processorClass);
Constructor<?>[] ctList = clz.getConstructors();
Constructor<?>[] ctList = processorClass.getConstructors();
for (Constructor<?> ct: ctList) {
Class<?>[] parameterTypes =ct.getParameterTypes();
if(parameterTypes.length > 0 && parameterTypes[0].isAssignableFrom(jsonObject.getClass())) {
Expand All @@ -47,7 +47,7 @@ private static ProcessDataGetterN3 instantiateClass(String processorClass, Objec
}

} catch(Exception ex) {
log.error("Error occurred instantiating " + processorClass, ex);
log.error("Error occurred instantiating " + processorClass.getCanonicalName(), ex);
}
return pn;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/* $This file is distributed under the terms of the license in LICENSE$ */

package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.utils;

import static edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary.SEARCH_FILTER_VALUE;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletContext;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
import edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SearchFilterValuesDataGetter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Resource;

//Returns the appropriate n3 based on data getter
public class ProcessSearchFilterValuesDataGetterN3 extends ProcessDataGetterAbstract {
private static String classType = "java:" + SearchFilterValuesDataGetter.class.getCanonicalName();
public static String searchFilterVarBase = "filterUri";
private Log log = LogFactory.getLog(ProcessSearchFilterValuesDataGetterN3.class);

public ProcessSearchFilterValuesDataGetterN3() {
}

public List<String> retrieveN3Required(int counter) {
return retrieveN3ForTypeAndFilter(counter);
}

public List<String> retrieveN3Optional(int counter) {
return null;
}

public List<String> retrieveN3ForTypeAndFilter(int counter) {
String n3ForType = getN3ForTypePartial(counter);
String n3 = n3ForType + "; \n" + "<" + DisplayVocabulary.SEARCH_FILTER_VALUE + "> "
+ getN3VarName(searchFilterVarBase, counter) + " .";
List<String> n3List = new ArrayList<String>();
n3List.add(getPrefixes() + n3);
return n3List;
}

public String getN3ForTypePartial(int counter) {
String dataGetterVar = getDataGetterVar(counter);
String classTypeVar = getN3VarName(classTypeVarBase, counter);
String n3 = dataGetterVar + " a " + classTypeVar;
return n3;
}

public List<String> retrieveLiteralsOnForm(int counter) {
// no literals, just the class group URI
List<String> literalsOnForm = new ArrayList<String>();
return literalsOnForm;
}

public List<String> retrieveUrisOnForm(int counter) {
List<String> urisOnForm = new ArrayList<String>();
urisOnForm.add(getVarName("filterUri", counter));
urisOnForm.add(getVarName(classTypeVarBase, counter));
return urisOnForm;
}

public List<FieldVTwo> retrieveFields(int counter) {
List<FieldVTwo> fields = new ArrayList<FieldVTwo>();
fields.add(new FieldVTwo().setName(getVarName("filterUri", counter)));
fields.add(new FieldVTwo().setName(getVarName(classTypeVarBase, counter)));
return fields;
}

public List<String> getLiteralVarNamesBase() {
return Arrays.asList();
}

public List<String> getUriVarNamesBase() {
return Arrays.asList("filterUri", classTypeVarBase);
}

public String getClassType() {
return classType;
}

public void populateExistingValues(String dataGetterURI, int counter, OntModel queryModel) {
// First, put dataGetterURI within scope as well
this.populateExistingDataGetterURI(dataGetterURI, counter);
// Put in type
this.populateExistingClassType(this.getClassType(), counter);
// Sparql queries for values to be executed
// And then placed in the correct place/literal or uri
String querystr = getExistingValuesClassGroup(dataGetterURI);
QueryExecution qe = null;
try {
Query query = QueryFactory.create(querystr);
qe = QueryExecutionFactory.create(query, queryModel);
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution qs = results.nextSolution();
Resource classGroupResource = qs.getResource("filterUri");
// Put both literals in existing literals
existingUriValues.put(this.getVarName(searchFilterVarBase, counter),
new ArrayList<String>(Arrays.asList(classGroupResource.getURI())));
}
} catch (Exception ex) {
log.error("Exception occurred in retrieving existing values with query " + querystr, ex);
}
}

protected String getExistingValuesClassGroup(String dataGetterURI) {
String query = getSparqlPrefix() + "\n" +
"SELECT ?filterUri ?filterName WHERE {" +
"<" + dataGetterURI + "> <" + SEARCH_FILTER_VALUE + "> ?filterUri .\n" +
"?filterUri <" + VitroVocabulary.LABEL + "> ?filterName .\n" +
"}";
return query;
}

public ObjectNode getExistingValuesJSON(String dataGetterURI, OntModel queryModel, ServletContext context) {
ObjectNode jObject = new ObjectMapper().createObjectNode();
jObject.put("dataGetterClass", classType);
jObject.put(classTypeVarBase, classType);
getExistingSearchFilters(dataGetterURI, jObject, queryModel);
return jObject;
}

private void getExistingSearchFilters(String dataGetterURI, ObjectNode jObject, OntModel queryModel) {
String querystr = getExistingValuesClassGroup(dataGetterURI);
QueryExecution qe = null;
try {
Query query = QueryFactory.create(querystr);
qe = QueryExecutionFactory.create(query, queryModel);
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution qs = results.nextSolution();
Resource filterUri = qs.getResource("filterUri");
Literal name = qs.getLiteral("filterName");
jObject.put("searchFilterUri", filterUri.getURI());
jObject.put("searchFilterName", name.getLexicalForm());
}
} catch (Exception ex) {
log.error("Exception occurred in retrieving existing values with query " + querystr, ex);
}
}

}
Loading
Loading