package com.ssdi.test.client;



import com.google.gwt.core.client.JavaScriptObject;
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.DataSourceDateTimeField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.util.JSOHelper;

public class CloningListDS extends RestDataSource{
  private static CloningListDS instance = null;

  public static CloningListDS getInstance() {
    if (null == instance) {
      instance = new CloningListDS();
    }
    return instance;
  }

  public CloningListDS() {
     setDataFormat(DSDataFormat.JSON);
    setRecordXPath("fileList"); // Root Node need to be considered in
    setDataURL("data/DeviceCloningListDS.js");// Dummy request
    addFields();
  }
  // Transform Request
  protected Object transformRequest(DSRequest dsRequest) {
     String str = "";
    return str;
  }
  public void addFields() {
   
    DataSourceTextField fileType = new DataSourceTextField(
        "fileType", "fileType");
    fileType.setValueXPath("fileType");
  
    DataSourceTextField fileId = new DataSourceTextField("fileId",
        "fileId");
    fileId.setValueXPath("fileId");

    DataSourceTextField fileName = new DataSourceTextField(
       "fileName", "fileName");
    fileName.setValueXPath("fileName");

    DataSourceTextField comment = new DataSourceTextField("comment",
        "comment");
    comment.setValueXPath("comment");

    DataSourceTextField executionStatus = new DataSourceTextField(
        "executionStatus", "executionStatus");
    executionStatus.setValueXPath("executionStatus");

    DataSourceTextField downloadFlag = new DataSourceTextField(
        "downloadFlag", "downloadFlag");
    downloadFlag.setValueXPath("downloadFlag");
    
  
    
    setFields(fileType,fileId, fileName, comment, executionStatus, downloadFlag);
  }
  // Transform Response
  @Override
  protected void transformResponse(DSResponse response, DSRequest request,
      Object data) {
    
    if (request.getOperationType().equals(DSOperationType.FETCH)) 
    {
    Record[] deviceRecords = response.getData();

    for (int i = 0; i < deviceRecords.length; i++) {
      JavaScriptObject rJSO = deviceRecords[i].getJsObj();
      String fileType = JSOHelper.getAttribute(rJSO, "fileType");
      if(fileType.equals("0"))
      {
        deviceRecords[i].setAttribute("fileType", fileType);
      }
      
      String executionStatus = JSOHelper.getAttribute(rJSO, "executionStatus");
      if(executionStatus.equals("0")) 
        deviceRecords[i].setAttribute("executionStatus", "-");
    }
    Integer totalDevices;

      JavaScriptObject responseJavaScriptObject = (JavaScriptObject) data;
       totalDevices = JSOHelper.getAttributeAsInt(
          responseJavaScriptObject, "totalCount");
       if (totalDevices == null) {
                totalDevices = 0;
        response.setTotalRows(totalDevices);
      }
       
     
    
          
  }
    
  }
 
}
