if (!framework.isDeclared("org.iit.data.DataAdapter")){

framework.declareClass("org.iit.data.DataAdapter", null,
    function(config){
        this.store = config.store;
        this.config = config;
        this.supportSearch = typeof config.supportSearch!="undefined"?config.supportSearch:true;
        this.supportPagination = typeof config.supportPagination!="undefined"?config.supportPagination:true;
    },
    {
        getCount: function(){
        },
        getCountAsync: function(){
        },
        search: function(criteria){
        },
        searchAsync: function(criteria){
        },
        load: function(){
        },
        loadAsync: function(){
        }
    }
);

framework.declareClass("org.iit.data.JsonDataAdapter", org.iit.data.DataAdapter,
    function(config){
        org.iit.data.JsonDataAdapter.superclass.constructor.apply(this, arguments);
        if ( !config.countUrl && !config.countParams ) {
            config.countParams = {action: "count"};
        }
        if ( !config.searchUrl && !config.searchParams ) {
            config.countParams = {action: "search"};
        }
        if ( !config.loadUrl && !config.loadParams ) {
            config.loadParams = {action: "load"};
        }
        if ( !config.recordsPerPage ) {
            config.recordsPerPage = 10;
        }
    },
    {
        _getCountParams: function(){
            var params = {
                url: this.config.countUrl||this.config.url,
                handleAs: "json",
                content: {}
            };
            if ( !this.config.countUrl ){
                framework._mixin(params.content, this.config.countParams);
            }
            framework._mixin(params.content, this.config.additionalParams||{});
            return params;
        },
        _getLoadParams: function(p){
            var params = {
                url: this.config.loadUrl||this.config.url,
                handleAs: "json",
                content: {}
            };
            if ( !this.config.loadUrl ){
                framework._mixin(params.content, this.config.loadParams);
            }
            if ( p ) {
                framework._mixin(params.content, p);
            }
            framework._mixin(params.content, this.config.additionalParams||{});
            return params;
        },
        _getSearchParams: function(criteria){
            var params = {
                url: this.config.searchUrl||this.config.url,
                handleAs: "json",
                content: {}
            };
            if ( !this.config.searchUrl ){
                framework._mixin(params.content, this.config.searchParams);
            }
            framework._mixin(params.content, this.config.additionalParams||{});
            framework._mixin(params.content, {criteria: criteria.toJson()});
            return params;
        },
        getCount: function(){
        },
        getCountAsync: function(){
            var dfd = framework.Xhr.post(this._getCountParams()); 
            dfd.addCallback(framework.runInScope(this, function(res){
                return Number(res.count); 
            }));
            return dfd;
        },
        search: function(criteria){
        },
        searchAsync: function(criteria){
            var dfd = framework.Xhr.post(this._getSearchParams(criteria));
            dfd.addCallback(framework.runInScope(this, this._handleResult));
            return dfd;
        },
        load: function(){
        },
        loadAsync: function(page){
            var dfd = framework.Xhr.post(this._getLoadParams(page&&this.supportPagination?{recordsPerPage: this.config.recordsPerPage, page: page}:{}));
            if ( page && this.supportPagination ) {
                dfd.addCallbacks(framework.runInScope(this, this._handleResult), function(res) { if ( window.console ) console.log(res); });
            } else {
                dfd.addCallbacks(framework.runInScope(this, this._handleResult, true), function(res) { if ( window.console ) consloe.log(res); });
            }
            return dfd;
        },
        _handleResult: function(r, loadedCompletely){
            if ( this.store ) {
                var result = [], record, res;
                if ( r.data ) {
                    res = r.data;
                    result.haveToPaginate = r.haveToPaginate;
                    result.currentPage = r.currentPage;
                    result.lastPage = r.lastPage;
                } else {
                    if ( loadedCompletely && !this.store.isMeasured() ) {
                        this.store.setLength(r.length);
                    }
                    res = r;
                }
                for ( var i = 0, l = res.length; i < l; i++ ){
                    record = this.store._create();
                    record.fromArray(res[i]);
                    record.state = org.iit.data.DataRecord.STATE_CLEAN;
                    if ( !this.store._index[this.store._getPrimaryKeyValue(record)] ){
                        this.store._add(record);
                    } else {
                        record = this.store._index[this.store._getPrimaryKeyValue(record)];
                    }
                    result.push(record);
                }
                return result;
            } else {
                return r;
            }
        }
    }
);


}
