package com.axs.sfamini.client.dataSources;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONString;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.XJSONDataSource;
import com.smartgwt.client.data.XMLTools;
import com.smartgwt.client.data.fields.DataSourcePasswordField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.rpc.RPCResponse;

public class LoginCrossDS extends XJSONDataSource {
	
	private static LoginCrossDS instance = null;

    public static LoginCrossDS getInstance() {
        if (instance == null) 
        	instance = new LoginCrossDS("LoginCrossDS");
        return instance;
    }
    
    public LoginCrossDS(String id) {
    	
		setRecordXPath("/response/data");
		
		DataSourceTextField userNameField = new DataSourceTextField("usuario", "Usuario", 50, true);
		//userNameField.setValueXPath("usuario");
		
        DataSourcePasswordField passwordField = new DataSourcePasswordField("password", "Password", 20, true);
        //passwordField.setValueXPath("password");
        
        setFields(userNameField, passwordField);
        setDataURL("http://axsbo/intranet/SirehServerMSSQLServer/json/emp_login_rest_out.php");
    }

    protected void transformResponse(DSResponse response, DSRequest request, Object jsonData) {
    	
    	JSONArray value = XMLTools.selectObjects(jsonData, "/response/status");
        String status = ((JSONString)value.get(0)).stringValue();
        //System.out.println(status);
        if(!status.equals("0")) {
            response.setStatus(RPCResponse.STATUS_VALIDATION_ERROR);
            
            JSONArray errors = XMLTools.selectObjects(jsonData, "/response/errors");
            response.setErrors(errors.getJavaScriptObject());
        }
    }     
}