Strabon
changeset 1197:87598f13d5cf
Strabon endpoint now displays the results of queries with two variables in projection (string and number) in a pie chart using google charts
author | Konstantina Bereta <Konstantina.Bereta@di.uoa.gr> |
---|---|
date | Tue Jul 09 18:58:34 2013 +0300 (2013-07-09) |
parents | 662bd232ac11 |
children | faf2ee0185df |
files | endpoint/WebContent/query.jsp endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java |
line diff
1.1 --- a/endpoint/WebContent/query.jsp Mon Jul 08 17:06:37 2013 +0300 1.2 +++ b/endpoint/WebContent/query.jsp Tue Jul 09 18:58:34 2013 +0300 1.3 @@ -70,9 +70,9 @@ 1.4 // get the reference to StrabonBeanWrapper 1.5 StrabonBeanWrapper strabonWrapper; 1.6 //String arr = new String[2]; 1.7 - List<String[]> results = new ArrayList<String[]>(); 1.8 - String[] arr = new String[2]; 1.9 - 1.10 + int i; 1.11 + ArrayList<String> arr = new ArrayList<String>(2); 1.12 + String gChartString=null; 1.13 ServletContext context; 1.14 context = getServletContext(); 1.15 WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context); 1.16 @@ -220,31 +220,20 @@ 1.17 // instantiates the pie chart, passes in the data and 1.18 // draws it. 1.19 1.20 - 1.21 - 1.22 + 1.23 + 1.24 function drawChart() { 1.25 1.26 - 1.27 // Create the data table. 1.28 var data = new google.visualization.DataTable(); 1.29 - <% if (request.getAttribute("response") != null) { 1.30 + <% if (request.getParameter("format")!=null && request.getAttribute("response") != null) { 1.31 if (request.getParameter("format").equals("CHART")) { 1.32 - results= (List<String[]>)request.getAttribute("response"); 1.33 - arr[0] = results.get(0)[0]; 1.34 - arr[0] = results.get(0)[1]; 1.35 - 1.36 - int i=1; 1.37 - while(i <= results.size()){ 1.38 - arr = results.get(i); 1.39 - 1.40 - 1.41 - i++;} %> 1.42 - // Set chart options 1.43 - var options = {'title':'Displaying results in chart', 1.44 - 'width':400, 1.45 - 'height':300}; 1.46 + out.println(request.getAttribute("response")); 1.47 + %> 1.48 + 1.49 + var options = {'title':'Displaying results in chart','width':400, 'height':300}; 1.50 1.51 - // Instantiate and draw our chart, passing in some options. 1.52 + 1.53 var chart = new google.visualization.PieChart(document.getElementById('chart_div')); 1.54 chart.draw(data, options); 1.55 1.56 @@ -387,10 +376,12 @@ 1.57 <a name="#results"> </a> 1.58 <div id="divResultsStart"></div> 1.59 <!-- Response --> 1.60 -<% if (request.getAttribute("response") != null) { 1.61 - if (Common.getHTMLFormat().equals(request.getParameter("format"))) {%> 1.62 +<% 1.63 +if (request.getAttribute("response") != null && !request.getParameter("format").equals("CHART")) { 1.64 + if (!Common.getHTMLFormat().equals(request.getParameter("format"))) {%> 1.65 <%=request.getAttribute("response")%> 1.66 - <%} else { %> 1.67 + <%} else { 1.68 + %> 1.69 <PRE><%=request.getAttribute("response") %></PRE> 1.70 <%}%> 1.71 <%}%>
2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java Mon Jul 08 17:06:37 2013 +0300 2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/QueryBean.java Tue Jul 09 18:58:34 2013 +0300 2.3 @@ -93,7 +93,6 @@ 2.4 */ 2.5 private String appName; 2.6 2.7 - private List<String[]> results = new ArrayList(); 2.8 2.9 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2.10 doPost(request, response); 2.11 @@ -110,6 +109,7 @@ 2.12 2.13 // the the strabon wrapper 2.14 strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean"); 2.15 + 2.16 2.17 // get the name of this web application 2.18 appName = context.getContextPath().replace("/", ""); 2.19 @@ -332,11 +332,12 @@ 2.20 2.21 try { 2.22 strabonWrapper.query(query, format, bos); 2.23 + //System.out.println("FORMAT: "+format); 2.24 if (format.equals(Common.getHTMLFormat())) { 2.25 request.setAttribute(RESPONSE, bos.toString()); 2.26 } 2.27 - else if(format.equals(Format.CHART)){ 2.28 - request.setAttribute(RESPONSE, strabonWrapper.getResults()); 2.29 + else if(format.equals(Format.CHART.toString())){ 2.30 + request.setAttribute(RESPONSE, strabonWrapper.getgChartString()); 2.31 } 2.32 else { 2.33 request.setAttribute(RESPONSE, StringEscapeUtils.escapeHtml(bos.toString()));
3.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java Mon Jul 08 17:06:37 2013 +0300 3.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java Tue Jul 09 18:58:34 2013 +0300 3.3 @@ -52,8 +52,8 @@ 3.4 private String prefixes; 3.5 3.6 private Strabon strabon = null; 3.7 - 3.8 - private List<String> results = new ArrayList<String>(); 3.9 + 3.10 + private String gChartString =" "; 3.11 3.12 private boolean checkForLockTable; 3.13 private List<StrabonBeanWrapperConfiguration> entries; 3.14 @@ -197,20 +197,50 @@ 3.15 if(bindingNames.size() !=2 ){ 3.16 logger.error("Strabon endpoint: to display results in chart, exactly TWO variables must be projected"); 3.17 } 3.18 + else{ 3.19 + 3.20 + ArrayList<String> arr = new ArrayList<String>(2); 3.21 + arr.add(0, bindingNames.get(0)); 3.22 + arr.add(1, bindingNames.get(1)); 3.23 + 3.24 3.25 - //String arr = new String[2]; 3.26 - //arr[0] = bindingNames.get(0); 3.27 - //arr[1] = bindingNames.get(1); 3.28 - //results.add(0, arr); 3.29 - //while(result.hasNext()){ 3.30 - //BindingSet bindings = result.next(); 3.31 - //arr[0] = bindings.getValue(bindingNames.get(0)).toString(); 3.32 - //arr[1] =bindings.getValue(bindingNames.get(1)).toString(); 3.33 - //System.out.println("ARR1: "+arr[0]+" ARR2: "+arr[1]); 3.34 - //results.add(arr); 3.35 + gChartString = "data.addColumn('string',\'"+arr.get(0)+"');\n"; 3.36 + gChartString += "data.addColumn('number',\'"+arr.get(1)+"');\n"; 3.37 + 3.38 + int i=1; 3.39 + int index=0; 3.40 + while(result.hasNext()){ 3.41 + BindingSet bindings = result.next(); 3.42 + arr.add(0, bindings.getValue(bindingNames.get(0)).stringValue()); 3.43 + arr.add(1, bindings.getValue(bindingNames.get(1)).stringValue()); 3.44 +//.replace("<http://www.w3.org/2001/XMLSchema#integer>","") 3.45 + if(!arr.get(0).contains("http") ){ //plain literal case- no prefixes to remove 3.46 + gChartString += "data.addRow([\'"+arr.get(0)+"\', "+ 3.47 + arr.get(1).replace("\"", "").replace("^^","")+"]);\n"; 3.48 + i++; 3.49 + } 3.50 + else{ //URI case 3.51 + //removing prefixes so that they will not be displayed in the chart 3.52 + if(arr.get(0).lastIndexOf('#') > arr.get(0).lastIndexOf('/')){ 3.53 + index = arr.get(0).lastIndexOf('#')+1; 3.54 + } 3.55 + else{ 3.56 + index = arr.get(0).lastIndexOf("/")+1; 3.57 + } 3.58 + 3.59 + int endIndex= arr.get(0).length(); 3.60 + gChartString += "data.addRow([\'"+arr.get(0).subSequence(index, endIndex )+"\', "+ 3.61 + arr.get(1).replace("\"", "").replace("^^","").replace("<http://www.w3.org/2001/XMLSchema#integer>","")+"]);\n"; 3.62 + i++; 3.63 + } 3.64 + 3.65 + } 3.66 + /* gChartString += "var options = {'title':'Displaying results in chart','width':400, 'height':300};"; 3.67 + gChartString += "var chart = new google.visualization.PieChart(document.getElementById('chart_div'));"; 3.68 + gChartString += "chart.draw(data, options);";*/ 3.69 + //System.out.println(gChartString); 3.70 3.71 - //} 3.72 - } 3.73 + }} 3.74 else{ 3.75 strabon.query(queryString, Format.fromString(answerFormatStrabon), strabon.getSailRepoConnection(), out); 3.76 } 3.77 @@ -405,12 +435,14 @@ 3.78 return prefixes; 3.79 } 3.80 3.81 - public List<String> getResults() { 3.82 - return results; 3.83 + 3.84 + 3.85 + public String getgChartString() { 3.86 + return gChartString; 3.87 } 3.88 3.89 - public void setResults(List<String> result) { 3.90 - this.results = result; 3.91 + public void setgChartString(String gChartString) { 3.92 + this.gChartString = gChartString; 3.93 } 3.94 3.95