package com.oc.smartgwt.test.client;

import java.util.Date;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;

public class myDS  extends DataSource {

    public static final String EmployeeId = "EmployeeId";    
    public static final String Name = "Name";
    public static final String GroupID = "GroupID";    
    public static final String GroupType = "GroupType";    
    public static final String GroupTypeValue = "group";    
    
    private static myDS instance = null;

    public static myDS getInstance(String fileName) {
        if (instance == null) {
            instance = new myDS("myDS", fileName);
        }
        return instance;
    }

    public myDS(String dsID, String fileName) {
    	
        setID(dsID);
        setTitleField("Name");
        setRecordXPath("/List/employee");
        DataSourceTextField nameField = new DataSourceTextField("Name", "Name", 128);

        DataSourceIntegerField employeeIdField = new DataSourceIntegerField("EmployeeId", "EmployeeId");
        employeeIdField.setPrimaryKey(true);
        employeeIdField.setRequired(true);

        DataSourceIntegerField groupIdField = new DataSourceIntegerField("GroupID", "GroupID");
        groupIdField.setForeignKey(dsID + ".EmployeeId");
        groupIdField.setRootValue("-1");

        DataSourceTextField groupTypeField = new DataSourceTextField("GroupType", "GroupType", 128);
        
        setFields(nameField, employeeIdField, groupIdField, groupTypeField);
        
    	if (fileName != null && fileName.length() > 0)
    		setDataURL(fileName);
        setClientOnly(true);
    }
	
	protected void transformResponse(DSResponse response, DSRequest request, Object data) {
		System.out.println(new Date() + "EmployeeDS.transformResponse.OperationType = " + request.getOperationType() + " | TotalRows = " + response.getTotalRows() + " | StartRow = " + response.getStartRow() + " | endRow = " + response.getEndRow());
		super.transformResponse(response, request, data);
	}
}
