package com.test.client;
/*
 * Browser: FireFox 20.0.1
 * GWT dev plugin: 1.20
 * SmartGWT build: 4.0d April 30, 2013
 * Java 7 Update 21
 * 
 * This test should fail in the RestDataSource override
 * of getClientOnlyResponse. The serverData coming into the
 * routine is fine. It crashes in the subsequent "super" call.
 * At least it crashes when I run it. Also note that the cached
 * data appears to be fine.
 * 
 */
import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceSequenceField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.layout.VLayout;

public class RestCacheAllData implements EntryPoint
{
  @Override
  public void onModuleLoad()
  {
//    SC.showConsole();
    
    final VLayout vlay = new VLayout();
    vlay.setWidth100();
    vlay.setHeight100();
    vlay.setBorder("3px solid blue");
    
    final RestDataSource projDS = new RestDataSource() {
      @Override
      protected DSResponse getClientOnlyResponse(DSRequest request, Record[] serverData)
      {
        System.out.println("In ClientOnlyResponse - Checking serverData...");
        System.out.println("\tserverData has " + serverData.length + " records.");
        for (Record rec : serverData)
        {
          System.out.println("\t\tGot: " + rec.getAttributeAsString("name"));
        }
        System.out.println("\tcalling super's getClientOnlyResponse...");

        DSResponse response = super.getClientOnlyResponse(request, serverData);

        System.out.println("\tchecking super's DSResponse...");
        System.out.println("\tDSResponse has " + response.getTotalRows() + " records.");
        for (Record rec : response.getData())
        {
          System.out.println("\t\tGot: " + rec.getAttributeAsString("name"));
        }
        System.out.println("Done ClientOnlyResponse...");

        return response;
      }     
    };
    
    DataSourceSequenceField id = new DataSourceSequenceField("id");
    id.setLength(5);
    id.setPrimaryKey(true);
    id.setHidden(true);
    
    DataSourceTextField project = new DataSourceTextField("name");
    project.setLength(50);
    
    projDS.setFields(id, project);
    projDS.setCacheAllData(true);
    projDS.setFetchDataURL("data/fetch_data.xml");
   
    projDS.fetchData(null, new DSCallback() {
      public void execute(DSResponse response, Object rawData, DSRequest request) {
        System.out.println("In fetchData/execute - checking response.");

        System.out.println("\tfetchData response length: " +
            response.getTotalRows());
        Record[] recs = response.getData();
        for (Record rec : recs)
        {
          System.out.println("\t\tIn response loop - name: " + 
              rec.getAttributeAsString("name"));
        }

        System.out.println("\tGetting RestDataSource cache");
        Record[] recs2 = projDS.getCacheData();
        for (Record rec : recs2)
        {
          System.out.println("\t\tIn getCache loop - name: " + 
              rec.getAttributeAsString("name"));
        }
      }
    });
    vlay.draw();
  }
}
