package com.mars.helios.heliosweb.client.datasource;

import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;

/**
 * Basic behaviour of our custom DS mechanism. This will be overridden by concrete DS clients.
 *
 * @author Yannick Kirschhoffer <yannick.kirschhoffer@cgi.com>
 */
public abstract class AbstractHeliosDS extends RestDataSource {

    private String extraJSONParameters = "";

    public AbstractHeliosDS(String url) {
        this.setDataFormat(DSDataFormat.JSON);
        this.setDataProtocol(DSProtocol.POSTMESSAGE);
        this.setDisableQueuing(true);
        this.setSendMetaData(true);
        this.setCacheAllData(false);
        this.setAutoCacheAllData(false);

        // Keep this attribute at data source global level as the bindings format does not seem to be
        // taken into account yet. See the forum thread below.
        this.setDataFormat(DSDataFormat.JSON);

        /*
         * Specific operation bindings override global data source protocol and format.
         * see http://forums.smartclient.com/showthread.php?t=25460
         */

        String dataUrl = url;

        OperationBinding fetchOperation = new OperationBinding();
        fetchOperation.setOperationType(DSOperationType.FETCH);
        fetchOperation.setDataProtocol(DSProtocol.POSTMESSAGE);
        fetchOperation.setDataFormat(DSDataFormat.JSON);
        fetchOperation.setDataURL(dataUrl);

        OperationBinding updateOperation = new OperationBinding();
        updateOperation.setOperationType(DSOperationType.UPDATE);
        updateOperation.setDataProtocol(DSProtocol.POSTMESSAGE);
        updateOperation.setDataFormat(DSDataFormat.JSON);
        updateOperation.setDataURL(dataUrl);

        OperationBinding addOperation = new OperationBinding();
        addOperation.setOperationType(DSOperationType.ADD);
        addOperation.setDataProtocol(DSProtocol.POSTMESSAGE);
        addOperation.setDataFormat(DSDataFormat.JSON);
        addOperation.setDataURL(dataUrl);

        this.setOperationBindings(fetchOperation, updateOperation, addOperation);
    }

    protected void setExtraJSONParameters(String extraJSONParameters) {
        this.extraJSONParameters = extraJSONParameters;
    }

    /**
     * This extension of the default request transformation appends grid informations to the message.
     *
     * @param dsRequest the DS request object coming from a SmartGWT component.
     * @return the JSON message that will be sent to the server.
     */
    @Override
    protected Object transformRequest(DSRequest dsRequest) {
        String message = (String) super.transformRequest(dsRequest);

        message = message.substring(0, message.length() - 1)
                + ", " + extraJSONParameters + " }";

        return message;
    }
}
