/*
	Copyright (c) 2004-2008, 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=dojox.rpc.JsonRest={commit:function(_4){var _5;_4=_4||{};var _6=[];var _7={};var _8=[];for(var i=0;i<_1.length;i++){var _a=_1[i];var _b=_a.object;var _c=false;if(!(_4.service&&(_b||_a.old).__id.indexOf(_4.service.servicePath))){if(_b&&!_a.old){_6.push({method:"post",target:{__id:jr.getServiceAndId(_b.__id).service.servicePath},content:_b});}else{if(!_b&&_a.old){_6.push({method:"delete",target:_a.old});}else{while(!(dojox.json&&dojox.json.ref&&dojox.json.ref.useRefs)&&_b.__id.match(/[\[\.]/)){var _d=_b.__id.match(/^[^\[\.]*/)[0];_b=_7[_d]=_2._index[_d];}if(!(_b.__id in _7)){_7[_b.__id]=_b;_6.push({method:"put",target:_b,content:_b});}}}_8.push(_a);_1.splice(i--,1);}}var _e;var _f=dojo.xhr;_5=_6.length;var _10;dojo.xhr=function(_11,_12){_12.headers=_12.headers||{};_12.headers["X-Transaction"]=_6.length-1==i?"commit":"open";if(_10){_12.headers["Content-Location"]=_10;}return _f.apply(dojo,arguments);};for(i=0;i<_6.length;i++){var _13=_6[i];dojox.rpc.JsonRest._contentId=_13.content&&_13.content.__id;var _14=_13.method=="post";_10=_14&&dojox.rpc.JsonRest._contentId;var _15=jr.getServiceAndId(_13.target.__id);var _16=_15.service;var dfd=_13.deferred=_16[_13.method](_15.id,dojox.json.ref.toJson(_13.content,false,_16.servicePath,true));(function(_18,dfd){dfd.addCallback(function(_1a){try{var _1b=dfd.ioArgs.xhr.getResponseHeader("Location");_18.__id=_1b;_2._index[_1b]=_18;}catch(e){}if(!(--_5)){if(_4.onComplete){_4.onComplete.call(_4.scope);}}});})(_14&&_13.content,dfd);dfd.addErrback(function(_1c){_5=-1;var _1d=_1;dirtyObject=_8;numDirty=0;jr.revert();_1=_1d;if(_4.onError){_4.onError();}return _1c;});}dojo.xhr=_f;return _6;},getDirtyObjects:function(){return _1;},revert:function(){while(_1.length>0){var d=_1.pop();if(d.object&&d.old){for(var i in d.old){if(d.old.hasOwnProperty(i)){d.object[i]=d.old[i];}}for(i in d.object){if(!d.old.hasOwnProperty(i)){delete d.object[i];}}}}},changing:function(_20,_21){if(!_20.__id){return;}for(var i=0;i<_1.length;i++){if(_20==_1[i].object){return;}}var old=_20 instanceof Array?[]:{};for(i in _20){if(_20.hasOwnProperty(i)){old[i]=_20[i];}}_1.push({object:!_21&&_20,old:old});},deleteObject:function(_24){this.changing(_24,true);},getConstructor:function(_25,_26){if(typeof _25=="string"){var _27=_25;_25=new dojox.rpc.Rest(_25,true);this.registerService(_25,_27,_26);}if(_25._constructor){return _25._constructor;}_25._constructor=function(_28){if(_28){dojo.mixin(this,_28);}var _29=jr.getIdAttribute(_25);_2._index[this.__id=this.__clientId=_25.servicePath+(this[_29]||(this[_29]=Math.random().toString(16).substring(2,14)+Math.random().toString(16).substring(2,14)))]=this;_1.push({object:this});};return dojo.mixin(_25._constructor,_25._schema,{load:_25});},fetch:function(_2a){var _2b=jr.getServiceAndId(_2a);return this.byId(_2b.service,_2b.id);},getIdAttribute:function(_2c){var _2d=_2c._schema;var _2e;if(_2d){if(!(_2e=_2d._idAttr)){for(var i in _2d.properties){if(_2d.properties[i].identity){_2d._idAttr=_2e=i;}}}}return _2e||"id";},getServiceAndId:function(_30){var _31=_30.match(/^(.*\/)([^\/]*)$/);var svc=jr.services[_31[1]]||new dojox.rpc.Rest(_31[1]);return {service:svc,id:_31[2]};},services:{},schemas:{},registerService:function(_33,_34,_35){_34=_34||_33.servicePath;_34=_33.servicePath=_34.match(/\/$/)?_34:(_34+"/");jr.schemas[_34]=_35||_33._schema||{};jr.services[_34]=_33;},byId:function(_36,id){var _38,_39=_2._index[(_36.servicePath||"")+id];if(_39&&!_39._loadObject){_38=new dojo.Deferred();_38.callback(_39);return _38;}return this.query(_36,id);},query:function(_3a,id,_3c){var _3d=_3a(id,_3c);_3d.addCallback(function(_3e){if(_3e.nodeType&&_3e.cloneNode){return _3e;}return dojox.json.ref.resolveJson(_3e,{defaultId:typeof id!="string"||(_3c&&(_3c.start||_3c.count))?undefined:id,index:_2._index,idPrefix:_3a.servicePath,idAttribute:jr.getIdAttribute(_3a),schemas:jr.schemas,loader:jr._loader});});return _3d;},_loader:function(_3f){var _40=jr.getServiceAndId(this.__id);var _41=this;jr.query(_40.service,_40.id).addBoth(function(_42){if(_42==_41){delete _42.$ref;delete _42._loadObject;}else{_41._loadObject=function(_43){_43(_42);};}_3f(_42);});},isDirty:function(_44){for(var i=0,l=_1.length;i<l;i++){if(_1[i].object==_44){return true;}}return false;}};})();}