Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Getting Data from Server

    Hello,

    I am evaluating smartgwt framework and I think I have some misunderstandings or lack of knowhow.

    What I want to do is to run a database query. Something like query.getSingleResult().

    I have done the .ds.xml file and I did a OperationBinding like
    Code:
    <operationBindings>
    		<operationBinding operationId="queryAnzahlStatus"
    			operationType="fetch">
    			<customSQL>
    				SELECT count(*) AS S from SW where (SW.STATUS = $criteria.status)
    			</customSQL>
    		</operationBinding>
    	</operationBindings>
    But I can call now this binding from my java code?? I couldnt find any sample for this. The docs and sample show the assignement of a datasource but how to use criterias and querying on server side? I tried many things like DSRequest and CallBack but nothing worked.

    In the javadoc I found a method how to use NamedQuery and OperationBinding but also I miss the past how to use this from java code.

    Please give me a hint or sample. I stuck here and cant accomplish the evaluation.

    Thx a lot

    #2
    Hello,

    the DSRequest-approach is correct. You don't need a CallBack, yet.
    Set the operationId of the fetch request of your DataBoundItem to "queryAnzahlStatus" and set the correct DataSource on the DataBoundItem (most likely a ListGrid or DynamicForm). Set the Criteria for the fetch-Request and fetch.

    Please also see this blog post which shows a even more declarative way of defining aggregates.

    Also make sure to read the Quick Start Guide PDF and to have the Developer Console open in order to see the RPC Requests that leave the browser.

    Best regards,
    Blama

    Comment


      #3
      Get data from Server

      Hi Blama,

      Thx for your quick reply however can you provide me an example or a link to an example using DSRequest to query data? This would be very helpful. As I said, I tried something with DSREquest but it failed. Also the problem for me is that the databinding also uses components like Grids to fetch data. But for the case I need to work with the data before I bound it, I cant see that this is possible.

      In my example, I would like to count data rows and put the result in a Label to show to the user. I cant bind the fetch to Labels (at least I dont know how).

      I have the docs opened and I read the docs. Theoretically the flow is clear but I cant manage it to have a working sample. Until now I used Vaadin as framework with JPA and the issues are more clear. I need to move the dark fog away in SmartGWT ;-)

      Maybe you can help me again,

      Thx so much


      Originally posted by Blama View Post
      Hello,

      the DSRequest-approach is correct. You don't need a CallBack, yet.
      Set the operationId of the fetch request of your DataBoundItem to "queryAnzahlStatus" and set the correct DataSource on the DataBoundItem (most likely a ListGrid or DynamicForm). Set the Criteria for the fetch-Request and fetch.

      Please also see this blog post which shows a even more declarative way of defining aggregates.

      Also make sure to read the Quick Start Guide PDF and to have the Developer Console open in order to see the RPC Requests that leave the browser.

      Best regards,
      Blama

      Comment


        #4
        An example:

        Code:
        DataSource.get("yourDatasource").fetchData(criteria, new DSCallback() {
        			
        			@Override
        			public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) {
        				int numberOfRecs = dsResponse.getData().length;
        				while (Record rec: dsResponse.getData()) {
        					// do something with rec
        				}
        			}
        		});
        And if you need the "queryAnzahlStatus" fetch operation, you can add it as a property:
        Code:
        DSRequest properties = new DSRequest();
        properties.setOperationId("queryAnzahlStatus");
        
        DataSource.get("yourDatasource").fetchData(criteria, new DSCallback() {
        			
        			@Override
        			public void execute(DSResponse dsResponse, Object data, DSRequest dsRequest) {
        				int numberOfRecs = dsResponse.getData().length;
        				while (Record rec: dsResponse.getData()) {
        					// do something with rec
        				}
        			}
        		}, properties);
        Edit: this is for the client-side.
        For the server-side:
        Code:
        DSRequest request = new DSRequest("yourDatasource",
        				DataSource.OP_FETCH);
        request.setCriteria(criteria);
        DSResponse response = request.execute();
        response.getDataList() ...
        Last edited by edulid; 22 Sep 2014, 05:19.

        Comment


          #5
          Hi,

          do as edulid suggested. In order to display the results, you can either use the CallBack and program some value-setting or have it handled for you automatically.
          To do so, do the following:
          • Create your .ds.xml (whole file with your operationBinding)
          • Have your field "S" in the .ds.xml fieldlist as type="integer"
          • Have a DynamicForm bound to your DS
          • Have a TextItem (or StaticTextItem, so that does not look like an input element) as only member of the DynamicForm.
          • Do a yourDF.fetch(....see edulids code...)
          • Watch that happens in Developer Console (copy & paste code (5 lines of code maximum) from BuiltInDS-sample to show the Developer Console on Ctrl-D-Press)


          Best regards,
          Blama

          Comment


            #6
            Hi,

            Blama's points 1 and 2 are necessary.
            But point 3 is not,.. inside the code you can do whatever you want with the records,.. That's why I use the given code so much...

            Of course, if you want to show the results in a dynamicForm, Blama's way would be the correct way to do it... but if you want to take *any* other action, you can use the record directly (// do *anything* with rec).

            Comment


              #7
              Get data from Server

              Thx so much Folks, with your help it works now!

              Have a nice day

              Comment

              Working...
              X