diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/DataGetterUtils.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/DataGetterUtils.java index 05461853f..263393fe1 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/DataGetterUtils.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/pageDataGetter/DataGetterUtils.java @@ -40,16 +40,16 @@ import freemarker.ext.beans.BeansWrapper; import freemarker.template.TemplateModel; public class DataGetterUtils { - protected static final String DATA_GETTER_MAP = "pageTypeToDataGetterMap"; + protected static final String DATA_GETTER_MAP = "pageTypeToDataGetterMap"; private static final Log log = LogFactory.getLog(DataGetterUtils.class); - public static Map getDataForPage(String pageUri, VitroRequest vreq, ServletContext context) { - //Based on page type get the appropriate data getter - Map page = getMapForPage(vreq, pageUri); - //Get data getters map - Map dataGetterMap = getPageDataGetterMap(context); - //Get types associated with page - Map data = new HashMap(); + public static Map getDataForPage(String pageUri, VitroRequest vreq, ServletContext context) { + //Based on page type get the appropriate data getter + Map page = getMapForPage(vreq, pageUri); + //Get data getters map + Map dataGetterMap = getPageDataGetterMap(context); + //Get types associated with page + Map data = new HashMap(); List dataGetters = (List)page.get("dataGetters"); if( dataGetters != null ){ for( String dataGetter : dataGetters){ @@ -64,21 +64,21 @@ public class DataGetterUtils { } } } - return data; - } + return data; + } - /** - * - * Convert data to JSON for page uri based on type and related datagetters - * TODO: How to handle different data getters? Will this replace json fields or add to them? - */ - public static JSONObject covertDataToJSONForPage(String pageUri, Map data, VitroRequest vreq, ServletContext context) { - //Based on page type get the appropriate data getter - Map page = getMapForPage(vreq, pageUri); - //Get data getters map - Map dataGetterMap = getPageDataGetterMap(context); - //Get types associated with page - JSONObject rObj = null; + /** + * + * Convert data to JSON for page uri based on type and related datagetters + * TODO: How to handle different data getters? Will this replace json fields or add to them? + */ + public static JSONObject covertDataToJSONForPage(String pageUri, Map data, VitroRequest vreq, ServletContext context) { + //Based on page type get the appropriate data getter + Map page = getMapForPage(vreq, pageUri); + //Get data getters map + Map dataGetterMap = getPageDataGetterMap(context); + //Get types associated with page + JSONObject rObj = null; List types = (List)page.get("types"); if( types != null ){ for( String type : types){ @@ -87,14 +87,14 @@ public class DataGetterUtils { try{ typeObj = getter.convertToJSON(data, vreq); if( typeObj != null) { - //Copy over everything from this type Obj to - //TODO: Review how to handle duplicate keys, etc. - if(rObj != null) { - //For now, just nests as separate entry - rObj.put(type, typeObj); - } else { - rObj = typeObj; - } + //Copy over everything from this type Obj to + //TODO: Review how to handle duplicate keys, etc. + if(rObj != null) { + //For now, just nests as separate entry + rObj.put(type, typeObj); + } else { + rObj = typeObj; + } } }catch(Throwable th){ @@ -102,32 +102,33 @@ public class DataGetterUtils { } } } - return rObj; - } - /* - * Returns map with all page attributes from display model - */ - public static Map getMapForPage(VitroRequest vreq, String pageUri) { - //do a query to the display model for attributes of this page. + return rObj; + } + /* + * Returns map with all page attributes from display model + */ + public static Map getMapForPage(VitroRequest vreq, String pageUri) { + //do a query to the display model for attributes of this page. return vreq.getWebappDaoFactory().getPageDao().getPage(pageUri); - } - - public static void setupDataGetters(ServletContext context){ - if( context != null && context.getAttribute(DATA_GETTER_MAP) == null ){ - context.setAttribute(DATA_GETTER_MAP, new HashMap()); - - /* register all page data getters with the PageController servlet. - * There should be a better way of doing this. */ - ClassGroupPageData cgpd = new ClassGroupPageData(); - getPageDataGetterMap(context).put(cgpd.getType(), cgpd); - BrowseDataGetter bdg = new BrowseDataGetter(); - getPageDataGetterMap(context).put(bdg.getType(), bdg); - //TODO: Check if can include by type here - IndividualsForClassesDataGetter cidg = new IndividualsForClassesDataGetter(); - getPageDataGetterMap(context).put(cidg.getType(), cidg); - - } - } + } + + public static void setupDataGetters(ServletContext context){ + if( context != null && context.getAttribute(DATA_GETTER_MAP) == null ){ + context.setAttribute(DATA_GETTER_MAP, new HashMap()); + + /* register all page data getters with the PageController servlet. + * There should be a better way of doing this. */ + ClassGroupPageData cgpd = new ClassGroupPageData(); + getPageDataGetterMap(context).put(cgpd.getType(), cgpd); + BrowseDataGetter bdg = new BrowseDataGetter(); + getPageDataGetterMap(context).put(bdg.getType(), bdg); + //TODO: Check if can include by type here + IndividualsForClassesDataGetter cidg = new IndividualsForClassesDataGetter(); + getPageDataGetterMap(context).put(cidg.getType(), cidg); + + } + } + public static Map getPageDataGetterMap(ServletContext sc){ setupDataGetters(sc); return (Map)sc.getAttribute(DATA_GETTER_MAP); @@ -135,9 +136,9 @@ public class DataGetterUtils { /* //Based on page Uri, do conversions public static PageDataGetter getDataGetterForType(String type, ServletContext sc) { - Map map = getPageDataGetterMap(sc); - PageDataGetter pdg = (PageDataGetter) map.get(type); - return pdg; + Map map = getPageDataGetterMap(sc); + PageDataGetter pdg = (PageDataGetter) map.get(type); + return pdg; }*/ protected static Map getAdditionalData( @@ -162,143 +163,144 @@ public class DataGetterUtils { * Process results related to VClass or vclasses. Handles both single and multiple vclasses being sent. */ public static JSONObject processVclassResultsJSON(Map map, VitroRequest vreq, boolean multipleVclasses) { - JSONObject rObj = new JSONObject(); - VClass vclass=null; - String errorMessage = null; - try{ - DataProperty fNameDp = (new DataProperty()); - fNameDp.setURI("http://xmlns.com/foaf/0.1/firstName"); - DataProperty lNameDp = (new DataProperty()); - lNameDp.setURI("http://xmlns.com/foaf/0.1/lastName"); - - //this property is vivo specific - DataProperty preferredTitleDp = (new DataProperty()); - preferredTitleDp.setURI("http://vivoweb.org/ontology/core#preferredTitle"); - - - if( log.isDebugEnabled() ){ - @SuppressWarnings("unchecked") - Enumeration e = vreq.getParameterNames(); - while(e.hasMoreElements()){ - String name = (String)e.nextElement(); - log.debug("parameter: " + name); - for( String value : vreq.getParameterValues(name) ){ - log.debug("value for " + name + ": '" + value + "'"); - } - } - } - - //need an unfiltered dao to get firstnames and lastnames - WebappDaoFactory fullWdf = vreq.getFullWebappDaoFactory(); - - String[] vitroClassIdStr = vreq.getParameterValues("vclassId"); - if ( vitroClassIdStr != null && vitroClassIdStr.length > 0){ - for(String vclassId: vitroClassIdStr) { - vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassId); - if (vclass == null) { - log.debug("Couldn't retrieve vclass "); - throw new Exception (errorMessage = "Class " + vclassId + " not found"); - } - } - }else{ - log.debug("parameter vclassId URI parameter expected "); - throw new Exception("parameter vclassId URI parameter expected "); - } - List vclassIds = Arrays.asList(vitroClassIdStr); - //if single vclass expected, then include vclass. This relates to what the expected behavior is, not size of list - if(!multipleVclasses) { - //currently used for ClassGroupPage - rObj.put("vclass", - new JSONObject().put("URI",vclass.getURI()) + JSONObject rObj = new JSONObject(); + VClass vclass=null; + String errorMessage = null; + + try { + + // Properties from ontologies used by VIVO - should not be in vitro + DataProperty fNameDp = (new DataProperty()); + fNameDp.setURI("http://xmlns.com/foaf/0.1/firstName"); + DataProperty lNameDp = (new DataProperty()); + lNameDp.setURI("http://xmlns.com/foaf/0.1/lastName"); + DataProperty preferredTitleDp = (new DataProperty()); + preferredTitleDp.setURI("http://vivoweb.org/ontology/core#preferredTitle"); + + if( log.isDebugEnabled() ){ + @SuppressWarnings("unchecked") + Enumeration e = vreq.getParameterNames(); + while(e.hasMoreElements()){ + String name = (String)e.nextElement(); + log.debug("parameter: " + name); + for( String value : vreq.getParameterValues(name) ){ + log.debug("value for " + name + ": '" + value + "'"); + } + } + } + + //need an unfiltered dao to get firstnames and lastnames + WebappDaoFactory fullWdf = vreq.getFullWebappDaoFactory(); + + String[] vitroClassIdStr = vreq.getParameterValues("vclassId"); + if ( vitroClassIdStr != null && vitroClassIdStr.length > 0){ + for(String vclassId: vitroClassIdStr) { + vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassId); + if (vclass == null) { + log.debug("Couldn't retrieve vclass "); + throw new Exception (errorMessage = "Class " + vclassId + " not found"); + } + } + }else{ + log.debug("parameter vclassId URI parameter expected "); + throw new Exception("parameter vclassId URI parameter expected "); + } + List vclassIds = Arrays.asList(vitroClassIdStr); + //if single vclass expected, then include vclass. This relates to what the expected behavior is, not size of list + if(!multipleVclasses) { + //currently used for ClassGroupPage + rObj.put("vclass", + new JSONObject().put("URI",vclass.getURI()) + .put("name",vclass.getName())); + } else { + //For now, utilize very first first VClass (assume that that is the one to be employed) + //TODO: Find more general way of dealing with this + //put multiple ones in? + if(vclassIds.size() > 0) { + vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassIds.get(0)); + rObj.put("vclass", new JSONObject().put("URI",vclass.getURI()) .put("name",vclass.getName())); - } else { - //For now, utilize very first first VClass (assume that that is the one to be employed) - //TODO: Find more general way of dealing with this - //put multiple ones in? - if(vclassIds.size() > 0) { - vclass = vreq.getWebappDaoFactory().getVClassDao().getVClassByURI(vclassIds.get(0)); - rObj.put("vclass", new JSONObject().put("URI",vclass.getURI()) - .put("name",vclass.getName())); - } - // rObj.put("vclasses", new JSONObject().put("URIs",vitroClassIdStr) - // .put("name",vclass.getName())); - } - if (vclass != null) { - - rObj.put("totalCount", map.get("totalCount")); - rObj.put("alpha", map.get("alpha")); - - List inds = (List)map.get("entities"); - - JSONArray jInds = new JSONArray(); - for(Individual ind : inds ){ - JSONObject jo = new JSONObject(); - jo.put("URI", ind.getURI()); - jo.put("label",ind.getRdfsLabel()); - jo.put("name",ind.getName()); - jo.put("thumbUrl", ind.getThumbUrl()); - jo.put("imageUrl", ind.getImageUrl()); - jo.put("profileUrl", UrlBuilder.getIndividualProfileUrl(ind, vreq)); + } + // rObj.put("vclasses", new JSONObject().put("URIs",vitroClassIdStr) + // .put("name",vclass.getName())); + } + if (vclass != null) { - jo.put("vclassName", SolrJsonServlet.getMostSpecificTypeName(ind,fullWdf)); - - jo.put("preferredTitle", SolrJsonServlet.getDataPropertyValue(ind, preferredTitleDp, fullWdf)); - jo.put("firstName", SolrJsonServlet.getDataPropertyValue(ind, fNameDp, fullWdf)); - jo.put("lastName", SolrJsonServlet.getDataPropertyValue(ind, lNameDp, fullWdf)); + rObj.put("totalCount", map.get("totalCount")); + rObj.put("alpha", map.get("alpha")); + + List inds = (List)map.get("entities"); + + JSONArray jInds = new JSONArray(); + for(Individual ind : inds ){ + JSONObject jo = new JSONObject(); + jo.put("URI", ind.getURI()); + jo.put("label",ind.getRdfsLabel()); + jo.put("name",ind.getName()); + jo.put("thumbUrl", ind.getThumbUrl()); + jo.put("imageUrl", ind.getImageUrl()); + jo.put("profileUrl", UrlBuilder.getIndividualProfileUrl(ind, vreq)); + + jo.put("vclassName", SolrJsonServlet.getMostSpecificTypeName(ind,fullWdf)); + + jo.put("preferredTitle", SolrJsonServlet.getDataPropertyValue(ind, preferredTitleDp, fullWdf)); + jo.put("firstName", SolrJsonServlet.getDataPropertyValue(ind, fNameDp, fullWdf)); + jo.put("lastName", SolrJsonServlet.getDataPropertyValue(ind, lNameDp, fullWdf)); + + jInds.put(jo); + } + rObj.put("individuals", jInds); - jInds.put(jo); - } - rObj.put("individuals", jInds); - - JSONArray wpages = new JSONArray(); - //Made sure that PageRecord here is SolrIndividualListController not IndividualListController - List pages = (List)map.get("pages"); - for( PageRecord pr: pages ){ - JSONObject p = new JSONObject(); - p.put("text", pr.text); - p.put("param", pr.param); - p.put("index", pr.index); - wpages.put( p ); - } - rObj.put("pages",wpages); - - JSONArray jletters = new JSONArray(); - List letters = Controllers.getLetters(); - for( String s : letters){ - JSONObject jo = new JSONObject(); - jo.put("text", s); - jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8")); - jletters.put( jo ); - } - rObj.put("letters", jletters); - } - } catch(Exception ex) { - log.error("Error occurred in processing JSON object", ex); - } - return rObj; + JSONArray wpages = new JSONArray(); + //Made sure that PageRecord here is SolrIndividualListController not IndividualListController + List pages = (List)map.get("pages"); + for( PageRecord pr: pages ){ + JSONObject p = new JSONObject(); + p.put("text", pr.text); + p.put("param", pr.param); + p.put("index", pr.index); + wpages.put( p ); + } + rObj.put("pages",wpages); + + JSONArray jletters = new JSONArray(); + List letters = Controllers.getLetters(); + for( String s : letters){ + JSONObject jo = new JSONObject(); + jo.put("text", s); + jo.put("param", "alpha=" + URLEncoder.encode(s, "UTF-8")); + jletters.put( jo ); + } + rObj.put("letters", jletters); + } + } catch(Exception ex) { + log.error("Error occurred in processing JSON object", ex); + } + return rObj; } /* * Copied from JSONServlet as expect this to be related to VitroClassGroup */ public static JSONObject processVClassGroupJSON(VitroRequest vreq, ServletContext context, VClassGroup vcg) { - JSONObject map = new JSONObject(); - try { - ArrayList classes = new ArrayList(vcg.size()); - for( VClass vc : vcg){ - JSONObject vcObj = new JSONObject(); - vcObj.put("name", vc.getName()); - vcObj.put("URI", vc.getURI()); - vcObj.put("entityCount", vc.getEntityCount()); - classes.add(vcObj); - } - map.put("classes", classes); - map.put("classGroupName", vcg.getPublicName()); - map.put("classGroupUri", vcg.getURI()); - } catch(Exception ex) { - log.error("Error occurred in processing VClass group ", ex); - } - return map; + JSONObject map = new JSONObject(); + try { + ArrayList classes = new ArrayList(vcg.size()); + for( VClass vc : vcg){ + JSONObject vcObj = new JSONObject(); + vcObj.put("name", vc.getName()); + vcObj.put("URI", vc.getURI()); + vcObj.put("entityCount", vc.getEntityCount()); + classes.add(vcObj); + } + map.put("classes", classes); + map.put("classGroupName", vcg.getPublicName()); + map.put("classGroupUri", vcg.getURI()); + + } catch(Exception ex) { + log.error("Error occurred in processing VClass group ", ex); + } + return map; } } \ No newline at end of file