package com.mycompany.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.mycompany.services.StockService;
import com.mycompany.services.StockServiceAsync;
import com.smartgwt.client.data.fields.DataSourceFloatField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class StockGWTRPCDataSource2 extends GwtRpcDataSource<StockPrice> {

   public StockGWTRPCDataSource2() {
      super();
      
      setFetchEndpoint(new GwtRpcDataSourceEndpoint<StockPrice>() {

         @Override
         public void callEndpoint(AsyncCallback callback, Object... parameters) {
            StockServiceAsync stockService = GWT.create(StockService.class);
            
            stockService.listStocks(callback);            
         }});
      
      setUpdateEndpoint(new GwtRpcDataSourceEndpoint<StockPrice>() {

         @Override
         public void callEndpoint(AsyncCallback callback, Object... parameters) {
            StockServiceAsync stockService = GWT.create(StockService.class);
            
            ListGridRecord record = (ListGridRecord) parameters[0];
            stockService.editStock(record.getAttributeAsInt("id"), record.getAttribute("symbol"), callback);            
         }});
      
      setRemoveEndpoint(new GwtRpcDataSourceEndpoint<StockPrice>() {

         @Override
         public void callEndpoint(AsyncCallback callback, Object... parameters) {
            StockServiceAsync stockService = GWT.create(StockService.class);
            
            ListGridRecord record = (ListGridRecord) parameters[0];
            stockService.removeStock(record.getAttributeAsInt("id"), callback);            
         }});
      
      setAddEndpoint(new GwtRpcDataSourceEndpoint<StockPrice>() {

         @Override
         public void callEndpoint(AsyncCallback callback, Object... parameters) {
            StockServiceAsync stockService = GWT.create(StockService.class);
            
            ListGridRecord record = (ListGridRecord) parameters[0];
            stockService.addStock(record.getAttribute("symbol"), callback);            
         }});
      
      DataSourceIntegerField idDataField = new DataSourceIntegerField("id");
      idDataField.setPrimaryKey(true);
      DataSourceTextField symbolDataField = new DataSourceTextField("symbol", "Symbol");
      DataSourceFloatField priceDataField = new DataSourceFloatField("price", "Price");
      priceDataField.setCanEdit(false);
      DataSourceFloatField changeDataField = new DataSourceFloatField("change", "Change");
      changeDataField.setCanEdit(false);

      setFields(idDataField, symbolDataField, priceDataField, changeDataField);
   }

   @Override
   protected void populateRecord(ListGridRecord record, StockPrice stockPrice) {
      record.setAttribute("id", stockPrice.getId());
      record.setAttribute("symbol", stockPrice.getSymbol());
      record.setAttribute("price", stockPrice.getPrice());
      record.setAttribute("change", stockPrice.getChange()); 
   }
}
