Webremote with database query
dave2010 May 31, 2010 12:41 AMHi
I'm new to Seam and I'm trying to develop a visaulisation using google visualisation toolkit. The problem is that when I try to use webremote that calls a function to query the databse, it takes a very long time for the remote script to execute, this is my code
@Stateful
@Scope(ScopeType.SESSION)
@Name("GetWeightGraph")
public class GetWeightGraph implements IGetWeight{
@In(create = true)
EntityManager entityManager;
private String user_id = "BB07F3D3-D76B-4553-BAF1-37DA7E847F2F"; //test user
List<Object[]> weight_data;
@WebRemote
public List<Object[]> getWeight_data() {
getData();
return weight_data;
}
public void getData(){
weight_data = entityManager.createQuery("select user_data.dateacquired, user_data.valuenumeric from tmc_UserClinicalData user_data, tmc_clinicaldata clinical_data, tmc_codedictionary code " +
"where user_data.clinicaldataid = clinical_data.id and clinical_data.clinicaldatacodeid = 171 " +
"and user_data.userid = '"+user_id+"' " +
"order by user_data.dateacquired ASC").getResultList();
}
@WebRemote
public String test(){
return "webremote test";
}
@Remove @Destroy
public void destroy(){}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_id() {
return user_id;
}
}<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:rich="http://richfaces.org/rich"
xmlns:a="http://richfaces.org/a4j"
template="/template_style/new_template.xhtml">
<ui:define name="head">
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="seam/resource/remoting/resource/remote.js"></script>
<script type="text/javascript" src="seam/resource/remoting/interface.js?IGetWeight"></script>
</ui:define>
<ui:define name="body">
<s:div>
<rich:panel>
<f:facet name="header">#{TMC_User_Measurement_Search.patient_id} Measurement View</f:facet>
<a:outputPanel id="user_relationship_panel">
<h:outputText value="No Entries Found" rendered="user_mlist != null and user_mlist.rowCount==0}" />
<div class="section" align="center">
<h:form id="UMlistForm">
<rich:dataTable id="UMlist_table" rows="20" value="#{user_mlist}"
var="ucd" rendered="#{user_mlist.rowCount>0}">
<f:facet name="header">
<rich:columnGroup>
<h:column>
<h:outputText value="Data ID" />
</h:column>
<h:column>
<h:outputText value="Data Name" />
</h:column>
<h:column>
<h:outputText value="Date Acquired" />
</h:column>
</rich:columnGroup>
</f:facet>
<h:column>
<s:link value="#{ucd[0].clinicaldataid}" />
</h:column>
<h:column>
<h:outputText value="#{ucd[2].value}" />
</h:column>
<h:column>
<h:outputText value="#{ucd[0].dateacquired}" />
</h:column>
</rich:dataTable>
<br/>
<rich:datascroller id="ds" for="UMlist_table" maxPages="10" />
</h:form>
</div>
</a:outputPanel>
<br/>
<br/>
<ui:include src="weight_graph.xhtml" />
</rich:panel>
</s:div>
</ui:define>
</ui:composition>The XHTML page that contains the remote call
<s:div id="weight_graph_div"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:h="http://java.sun.com/jsf/html" align="center">
<s:remote include="GetWeightGraph"/>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['annotatedtimeline']});
var weightData = Seam.Component.getInstance("GetWeightGraph");
Seam.Remoting.displayLoadingMessage = function(){};
Seam.Remoting.hideLoadingMessage = function(){};
function drawVisualization() {
weightData.getWeight_data(getSearchCallback);
}
function getSearchCallback(wdata){
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Weight');
var len = wdata.length;
data.addRows(len);
for(var a=0; a < len ;a++){
data.setValue(a, 0, wdata[a][0]);
data.setValue(a, 1, wdata[a][1]);
}
var annotatedtimeline = new google.visualization.AnnotatedTimeLine(
document.getElementById('visualization'));
annotatedtimeline.draw(data, {'displayAnnotations': true});
}
google.setOnLoadCallback(drawVisualization);
</script>
<s:div id="visualization" style="width: 400px; height: 300px;"></s:div>
</s:div>I've been stuck on this problem for a couple of days, anyone have any suggestions ? Is this the best way to use the google visualisation api ?