package com.hover.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.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;

public class TestDS extends DataSource {
	
	  private static TestDS instance = null;

	  public static TestDS getInstance() {
	      if (instance == null) {
	          instance = new TestDS("testDS");
	      }
	      return instance;
	  }
	
	public TestDS(String id) {
		setID(id);
		DataSourceTextField t1 = new DataSourceTextField("title", "title");
		setFields(t1);
		setClientOnly(true);
	}
	
	@Override
	protected Object transformRequest(DSRequest request) {
		String requestId = request.getRequestId();
		DSResponse response = new DSResponse();
		response.setAttribute("clientContext", request.getAttributeAsObject("clientContext"));
		// Asume success
		response.setStatus(0);
		switch (request.getOperationType()) {
			case ADD:
				Record recordToAdd = creatRecordInstance(request.getData());
				// ADD client side only
				Record[] list = new Record[1];
				// Return record from the request.
				list[0] = recordToAdd;
				response.setData(list);
				processResponse(requestId, response);
				break;
			default:
				break;
		}
		return request.getData();
	}

	private Record creatRecordInstance(JavaScriptObject data) {
		return new TestListGridRecord(data);
	}
}
