﻿public class ServiceDS extends RpcDataSource<Map<String, String>> {
	
	private static ServiceDS instance = null;   
	  
	public static ServiceDS getInstance() {   
  	if (instance == null) {   
   		instance = new ServiceDS("ServiceDS");   
  	}   
    
    return instance;   
  }
	
  public static ServiceDS getInstance(String pId) {   
    return new ServiceDS(pId);
  } 
    
  public ServiceDS(final String id) {
		setID(id); 
  
    DataSourceEnumField productField = new DataSourceEnumField("Product", "<nobr>Product</nobr>", 50);
           
    setFields(productField);

    setFetchEndpoint(new RpcDataSourceEndpoint<Map<String, String>>() {
			@Override
			public void callEndpoint(AsyncCallback<RpcDataSourceResult<Map<String, String>>> pCallback, DSRequest pRequest) {
				JavaScriptObject jsObj = getAttributeAsJavaScriptObject("defaultParams");
								
				Map<String, String> conditions = JSOHelper.convertToMap(jsObj);
				
				ServiceRemoteServiceAsync service = (ServiceRemoteServiceAsync) GWT.create(ServiceRemoteService.class);		
							
				if(id.equals("ListGrid")) 
					service.fetch(conditions, pRequest.getStartRow(), pRequest.getEndRow(), "ListGrid", pCallback);
				if(id.equals("DynamicForm"))
					service.fetch(conditions, pRequest.getStartRow(), pRequest.getEndRow(), "DynamicForm", pCallback);
			}
		});
  }
    
  protected Object transformRequest(DSRequest dsRequest) {   
      return super.transformRequest(dsRequest);   
  }
}
