//>>built
define("dojox/rpc/JsonRest",["dojo","dojox","dojox/json/ref","dojox/rpc/Rest"],function(m,h){function q(a,c,b,d){(c=c.ioArgs&&c.ioArgs.xhr&&c.ioArgs.xhr.getResponseHeader("Last-Modified"))&&n._timeStamps&&(n._timeStamps[d]=c);var f=a._schema&&a._schema.hrefProperty;if(f)h.json.ref.refAttribute=f;b=b&&h.json.ref.resolveJson(b,{defaultId:d,index:n._index,timeStamps:c&&n._timeStamps,time:c,idPrefix:a.servicePath.replace(/[^\/]*$/,""),idAttribute:j.getIdAttribute(a),schemas:j.schemas,loader:j._loader,
idAsRef:a.idAsRef,assignAbsoluteIds:!0});h.json.ref.refAttribute="$ref";return b}var k=[],n=h.rpc.Rest,j;j=h.rpc.JsonRest={serviceClass:h.rpc.Rest,conflictDateHeader:"If-Unmodified-Since",commit:function(a){for(var a=a||{},c=[],b={},d=[],f=0;f<k.length;f++){var e=k[f],g=e.object,h=e.old;if((!a.service||!g&&!h||!(g||h).__id.indexOf(a.service.servicePath))&&e.save){delete g.__isDirty;if(g)if(h){var i;if(i=g.__id.match(/(.*)#.*/))g=n._index[i[1]];if(!(g.__id in b)){b[g.__id]=g;if(a.incrementalUpdates&&
!i)var l=("function"==typeof a.incrementalUpdates?a.incrementalUpdates:function(){l={};for(var a in g)if(g.hasOwnProperty(a))g[a]!==h[a]&&(l[a]=g[a]);else if(h.hasOwnProperty(a))return null;return l})(g,h);l?c.push({method:"post",target:g,content:l}):c.push({method:"put",target:g,content:g})}}else i=j.getServiceAndId(g.__id).service,j.getIdAttribute(i)in g&&!a.alwaysPostNewItems?c.push({method:"put",target:g,content:g}):c.push({method:"post",target:{__id:i.servicePath},content:g});else h&&c.push({method:"delete",
target:h});d.push(e);k.splice(f--,1)}}m.connect(a,"onError",function(){if(!1!==a.revertOnError){var b=k;k=d;j.revert();k=b}else m.forEach(d,function(a){j.changing(a.object,!a.object)})});j.sendToServer(c,a);return c},sendToServer:function(a,c){var b=m.xhr,d=a.length,f,e,g,k=this.conflictDateHeader;m.xhr=function(c,d){d.headers=d.headers||{};d.headers.Transaction=a.length-1==f?"commit":"open";k&&g&&(d.headers[k]=g);e&&(d.headers["Content-ID"]="<"+e+">");return b.apply(m,arguments)};for(f=0;f<a.length;f++){var i=
a[f];h.rpc.JsonRest._contentId=i.content&&i.content.__id;var l="post"==i.method;(g="put"==i.method&&n._timeStamps[i.content.__id])&&(n._timeStamps[i.content.__id]=new Date+"");e=l&&h.rpc.JsonRest._contentId;var o=j.getServiceAndId(i.target.__id),l=o.service,o=i.deferred=l[i.method](o.id.replace(/#/,""),h.json.ref.toJson(i.content,!1,l.servicePath,!0));(function(b,f,g){f.addCallback(function(h){try{var e=f.ioArgs.xhr&&f.ioArgs.xhr.getResponseHeader("Location");if(e){var i=e.match(/(^\w+:\/\/)/)&&e.indexOf(g.servicePath),
e=0<i?e.substring(i):(g.servicePath+e).replace(/^(.*\/)?(\w+:\/\/)|[^\/\.]+\/\.\.\/|^.*\/(\/)/,"$2$3");b.__id=e;n._index[e]=b}h=q(g,f,h,b&&b.__id)}catch(j){}--d||c.onComplete&&c.onComplete.call(c.scope,a);return h})})(i.content,o,l);o.addErrback(function(a){d=-1;c.onError.call(c.scope,a)})}m.xhr=b},getDirtyObjects:function(){return k},revert:function(a){for(var c=k.length;0<c;){c--;var b=k[c],d=b.object,b=b.old,f=h.data._getStoreForItem(d||b);if(!a||!d&&!b||!(d||b).__id.indexOf(a.servicePath)){if(d&&
b){for(var e in b)if(b.hasOwnProperty(e)&&d[e]!==b[e]){if(f)f.onSet(d,e,d[e],b[e]);d[e]=b[e]}for(e in d)if(!b.hasOwnProperty(e)){if(f)f.onSet(d,e,d[e]);delete d[e]}}else if(b){if(f)f.onNew(b)}else if(f)f.onDelete(d);delete (d||b).__isDirty;k.splice(c,1)}}},changing:function(a,c){if(a.__id){a.__isDirty=!0;for(var b=0;b<k.length;b++){var d=k[b];if(a==d.object){if(c&&(d.object=!1,!this._saveNotNeeded))d.save=!0;return}}d=a instanceof Array?[]:{};for(b in a)a.hasOwnProperty(b)&&(d[b]=a[b]);k.push({object:!c&&
a,old:d,save:!this._saveNotNeeded})}},deleteObject:function(a){this.changing(a,!0)},getConstructor:function(a,c){if("string"==typeof a){var b=a,a=new h.rpc.Rest(a,!0);this.registerService(a,b,c)}if(a._constructor)return a._constructor;a._constructor=function(b){function c(a){if(a){c(a["extends"]);p=a.properties;for(var b in p){var d=p[b];d&&"object"==typeof d&&"default"in d&&(e[b]=d["default"])}}a&&a.prototype&&a.prototype.initialize&&(i=!0,a.prototype.initialize.apply(e,g))}var e=this,g=arguments,
p,i;c(a._schema);!i&&b&&"object"==typeof b&&m.mixin(e,b);var l=j.getIdAttribute(a);n._index[this.__id=this.__clientId=a.servicePath+(this[l]||Math.random().toString(16).substring(2,14)+"@"+(h.rpc.Client&&h.rpc.Client.clientId||"client"))]=this;h.json.schema&&p&&h.json.schema.mustBeValid(h.json.schema.validate(this,a._schema));k.push({object:this,save:!0})};return m.mixin(a._constructor,a._schema,{load:a})},fetch:function(a){a=j.getServiceAndId(a);return this.byId(a.service,a.id)},getIdAttribute:function(a){var a=
a._schema,c;if(a&&!(c=a._idAttr))for(var b in a.properties)if(a.properties[b].identity||"self"==a.properties[b].link)a._idAttr=c=b;return c||"id"},getServiceAndId:function(a){var c="",b;for(b in j.services)a.substring(0,b.length)==b&&b.length>=c.length&&(c=b);if(c)return{service:j.services[c],id:a.substring(c.length)};a=a.match(/^(.*\/)([^\/]*)$/);return{service:new j.serviceClass(a[1],!0),id:a[2]}},services:{},schemas:{},registerService:function(a,c,b){c=a.servicePath=c||a.servicePath;a._schema=
j.schemas[c]=b||a._schema||{};j.services[c]=a},byId:function(a,c){var b,d=n._index[(a.servicePath||"")+c];return d&&!d._loadObject?(b=new m.Deferred,b.callback(d),b):this.query(a,c)},query:function(a,c,b){var d=a(c,b);d.addCallback(function(f){return f.nodeType&&f.cloneNode?f:q(a,d,f,"string"!=typeof c||b&&(b.start||b.count)?void 0:c)});return d},_loader:function(a){var c=j.getServiceAndId(this.__id),b=this;j.query(c.service,c.id).addBoth(function(c){c==b?(delete c.$ref,delete c._loadObject):b._loadObject=
function(a){a(c)};a(c)})},isDirty:function(a,c){return!a?c?m.some(k,function(a){return h.data._getStoreForItem(a.object||a.old)==c}):!!k.length:a.__isDirty}};return h.rpc.JsonRest});