package com.ctest.client;

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.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;

public class CacheAllData implements EntryPoint
{
  @Override
  public void onModuleLoad()
  {
    final VLayout vlay = new VLayout();
    vlay.setWidth100();
    vlay.setHeight100();
    vlay.setBorder("3px solid blue");
    
    projDS.fetchData(null, new DSCallback() {
      public void execute(DSResponse response, Object rawData, DSRequest request) {
        System.out.println("In Project fetchData response.");
        System.out.println("Project response length: "+ response.getTotalRows());
        for (Record project : response.getData())
        {
          System.out.println("In LatestView loop - project: " + 
              project.getAttributeAsString("name"));
        }
      }
    });
    vlay.draw();
  }
  
  final DataSource projDS = new DataSource(){{
    
    final String dsName = "ClientOnlyDS";
    
    setID(dsName);
    
    System.out.println(dsName+": Start Instance");
    //
    setCacheAllData(true);
    //
    DataSourceTextField id = new DataSourceTextField("id");
    id.setPrimaryKey(true);
    id.setHidden(true);
    
    DataSourceTextField project = new DataSourceTextField("name");

    setFields(id, project);
    
    Record rec1 = new Record();
    Record rec2 = new Record();
//    ListGridRecord rec1 = new ListGridRecord();
//    ListGridRecord rec2 = new ListGridRecord();

    rec1.setAttribute("id", "123");
    rec1.setAttribute("name", "ABC");
    
    rec2.setAttribute("id", "456");
    rec2.setAttribute("name", "DEF");
    
    Record[] recs = {rec1, rec2};
    this.setCacheData(recs);
    this.setClientOnly(true);
    
    System.out.println(dsName+": End Instance");
  }};
}
