/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojox.rpc.JsonRest"]){
dojo._hasResource["dojox.rpc.JsonRest"]=true;
dojo.provide("dojox.rpc.JsonRest");
dojo.require("dojox.json.ref");
dojo.require("dojox.rpc.Rest");
(function(){
var _1=[];
var _2=dojox.rpc.Rest;
var jr;
function _4(_5,_6,_7,_8){
var _9=_6.ioArgs&&_6.ioArgs.xhr&&_6.ioArgs.xhr.getResponseHeader("Last-Modified");
return _7&&dojox.json.ref.resolveJson(_7,{defaultId:_8,index:_2._index,timeStamps:_9&&_2._timeStamps,time:_9,idPrefix:_5.servicePath,idAttribute:jr.getIdAttribute(_5),schemas:jr.schemas,loader:jr._loader,assignAbsoluteIds:true});
};
jr=dojox.rpc.JsonRest={conflictDateHeader:"If-Unmodified-Since",commit:function(_a){
_a=_a||{};
var _b=[];
var _c={};
var _d=[];
for(var i=0;i<_1.length;i++){
var _f=_1[i];
var _10=_f.object;
var old=_f.old;
var _12=false;
if(!(_a.service&&(_10||old)&&(_10||old).__id.indexOf(_a.service.servicePath))&&_f.save){
delete _10.__isDirty;
if(_10){
if(old){
var _13;
if((_13=_10.__id.match(/(.*)#.*/))){
_10=_2._index[_13[1]];
}
if(!(_10.__id in _c)){
_c[_10.__id]=_10;
_b.push({method:"put",target:_10,content:_10});
}
}else{
_b.push({method:"post",target:{__id:jr.getServiceAndId(_10.__id).service.servicePath},content:_10});
}
}else{
if(old){
_b.push({method:"delete",target:old});
}
}
_d.push(_f);
_1.splice(i--,1);
}
}
dojo.connect(_a,"onError",function(){
var _14=_1;
_1=_d;
var _15=0;
jr.revert();
_1=_14;
});
jr.sendToServer(_b,_a);
return _b;
},sendToServer:function(_16,_17){
var _18;
var _19=dojo.xhr;
var _1a=_16.length;
var _1b;
var _1c;
var _1d=this.conflictDateHeader;
dojo.xhr=function(_1e,_1f){
_1f.headers=_1f.headers||{};
_1f.headers["X-Transaction"]=_16.length-1==i?"commit":"open";
if(_1d&&_1c){
_1f.headers[_1d]=_1c;
}
if(_1b){
_1f.headers["Content-ID"]="<"+_1b+">";
}
return _19.apply(dojo,arguments);
};
for(i=0;i<_16.length;i++){
var _20=_16[i];
dojox.rpc.JsonRest._contentId=_20.content&&_20.content.__id;
var _21=_20.method=="post";
_1c=_20.method=="put"&&_2._timeStamps[_20.content.__id];
if(_1c){
_2._timeStamps[_20.content.__id]=(new Date())+"";
}
_1b=_21&&dojox.rpc.JsonRest._contentId;
var _22=jr.getServiceAndId(_20.target.__id);
var _23=_22.service;
var dfd=_20.deferred=_23[_20.method](_22.id.replace(/#/,""),dojox.json.ref.toJson(_20.content,false,_23.servicePath,true));
(function(_25,dfd,_27){
dfd.addCallback(function(_28){
try{
var _29=dfd.ioArgs.xhr&&dfd.ioArgs.xhr.getResponseHeader("Location");
if(_29){
var _2a=_29.match(/(^\w+:\/\/)/)&&_29.indexOf(_27.servicePath);
_29=_2a>0?_29.substring(_2a):(_27.servicePath+_29).replace(/^(.*\/)?(\w+:\/\/)|[^\/\.]+\/\.\.\/|^.*\/(\/)/,"$2$3");
_25.__id=_29;
_2._index[_29]=_25;
}
_28=_4(_27,dfd,_28);
}
catch(e){
}
if(!(--_1a)){
if(_17.onComplete){
_17.onComplete.call(_17.scope);
}
}
return _28;
});
})(_21&&_20.content,dfd,_23);
dfd.addErrback(function(_2b){
_1a=-1;
_17.onError.call(_17.scope,_2b);
});
}
dojo.xhr=_19;
},getDirtyObjects:function(){
return _1;
},revert:function(_2c){
for(var i=_1.length;i>0;){
i--;
var _2e=_1[i];
var _2f=_2e.object;
var old=_2e.old;
if(!(_2c&&(_2f||old)&&(_2f||old).__id.indexOf(kwArgs.service.servicePath))){
if(_2f&&old){
for(var j in old){
if(old.hasOwnProperty(j)){
_2f[j]=old[j];
}
}
for(j in _2f){
if(!old.hasOwnProperty(j)){
delete _2f[j];
}
}
}
_1.splice(i,1);
}
}
},changing:function(_32,_33){
if(!_32.__id){
return;
}
_32.__isDirty=true;
for(var i=0;i<_1.length;i++){
var _35=_1[i];
if(_32==_35.object){
if(_33){
_35.object=false;
if(!this._saveNotNeeded){
_35.save=true;
}
}
return;
}
}
var old=_32 instanceof Array?[]:{};
for(i in _32){
if(_32.hasOwnProperty(i)){
old[i]=_32[i];
}
}
_1.push({object:!_33&&_32,old:old,save:!this._saveNotNeeded});
},deleteObject:function(_37){
this.changing(_37,true);
},getConstructor:function(_38,_39){
if(typeof _38=="string"){
var _3a=_38;
_38=new dojox.rpc.Rest(_38,true);
this.registerService(_38,_3a,_39);
}
if(_38._constructor){
return _38._constructor;
}
_38._constructor=function(_3b){
if(_38._schema){
var _3c=_38._schema.properties;
for(var i in _3c){
if("default" in _3c[i]){
this[i]=_3c[i]["default"];
}
}
}
if(_3b){
dojo.mixin(this,_3b);
}
var _3e=jr.getIdAttribute(_38);
_2._index[this.__id=this.__clientId=_38.servicePath+(this[_3e]||Math.random().toString(16).substring(2,14)+"@"+((dojox.rpc.Client&&dojox.rpc.Client.clientId)||"client"))]=this;
if(dojox.json.schema&&_3c){
dojox.json.schema.mustBeValid(dojox.json.schema.validate(this,_38._schema));
}
_1.push({object:this,save:true});
};
return dojo.mixin(_38._constructor,_38._schema,{load:_38});
},fetch:function(_3f){
var _40=jr.getServiceAndId(_3f);
return this.byId(_40.service,_40.id);
},getIdAttribute:function(_41){
var _42=_41._schema;
var _43;
if(_42){
if(!(_43=_42._idAttr)){
for(var i in _42.properties){
if(_42.properties[i].identity){
_42._idAttr=_43=i;
}
}
}
}
return _43||"id";
},getServiceAndId:function(_45){
var _46=_45.match(/^(.*\/)([^\/]*)$/);
var svc=jr.services[_46[1]]||new dojox.rpc.Rest(_46[1],true);
return {service:svc,id:_46[2]};
},services:{},schemas:{},registerService:function(_48,_49,_4a){
_49=_49||_48.servicePath;
_49=_48.servicePath=_49.match(/\/$/)?_49:(_49+"/");
_48._schema=jr.schemas[_49]=_4a||_48._schema||{};
jr.services[_49]=_48;
},byId:function(_4b,id){
var _4d,_4e=_2._index[(_4b.servicePath||"")+id];
if(_4e&&!_4e._loadObject){
_4d=new dojo.Deferred();
_4d.callback(_4e);
return _4d;
}
return this.query(_4b,id);
},query:function(_4f,id,_51){
var _52=_4f(id,_51);
_52.addCallback(function(_53){
if(_53.nodeType&&_53.cloneNode){
return _53;
}
return _4(_4f,_52,_53,typeof id!="string"||(_51&&(_51.start||_51.count))?undefined:id);
});
return _52;
},_loader:function(_54){
var _55=jr.getServiceAndId(this.__id);
var _56=this;
jr.query(_55.service,_55.id).addBoth(function(_57){
if(_57==_56){
delete _57.$ref;
delete _57._loadObject;
}else{
_56._loadObject=function(_58){
_58(_57);
};
}
_54(_57);
});
},isDirty:function(_59){
if(!_59){
return !!_1.length;
}
return _59.__isDirty;
}};
})();
}
