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

//>>built
define("dojo/data/ObjectStore","../_base/lang ../Evented ../_base/declare ../_base/Deferred ../promise/all ../_base/array ../_base/connect ../regexp".split(" "),function(p,t,u,q,v,r,w,x){function y(a){return"*"==a?".*":"?"==a?".":a}return u("dojo.data.ObjectStore",[t],{objectStore:null,constructor:function(a){this._dirtyObjects=[];a.labelAttribute&&(a.labelProperty=a.labelAttribute);p.mixin(this,a)},labelProperty:"label",getValue:function(a,c,b){return"function"===typeof a.get?a.get(c):c in a?a[c]:
b},getValues:function(a,c){var b=this.getValue(a,c);return b instanceof Array?b:void 0===b?[]:[b]},getAttributes:function(a){var c=[],b;for(b in a)!a.hasOwnProperty(b)||"_"==b.charAt(0)&&"_"==b.charAt(1)||c.push(b);return c},hasAttribute:function(a,c){return c in a},containsValue:function(a,c,b){return-1<r.indexOf(this.getValues(a,c),b)},isItem:function(a){return"object"==typeof a&&a&&!(a instanceof Date)},isItemLoaded:function(a){return a&&"function"!==typeof a.load},loadItem:function(a){var c;"function"===
typeof a.item.load?q.when(a.item.load(),function(b){c=b;var d=b instanceof Error?a.onError:a.onItem;d&&d.call(a.scope,b)}):a.onItem&&a.onItem.call(a.scope,a.item);return c},close:function(a){return a&&a.abort&&a.abort()},fetch:function(a){function c(f){a.onError&&a.onError.call(d,f,a)}a=p.delegate(a,a&&a.queryOptions);var b=this,d=a.scope||b,e=a.query;if("object"==typeof e){e=p.delegate(e);for(var n in e){var m=e[n];"string"==typeof m&&(e[n]=RegExp("^"+x.escapeString(m,"*?\\").replace(/\\.|\*|\?/g,
y)+"$",a.ignoreCase?"mi":"m"),e[n].toString=function(f){return function(){return f}}(m))}}var g=this.objectStore.query(e,a);q.when(g.total,function(f){q.when(g,function(h){a.onBegin&&a.onBegin.call(d,f||h.length,a);if(a.onItem)for(var l=0;l<h.length;l++)a.onItem.call(d,h[l],a);a.onComplete&&a.onComplete.call(d,a.onItem?null:h,a);return h},c)},c);a.abort=function(){g.cancel&&g.cancel()};g.observe&&(this.observing&&this.observing.cancel(),this.observing=g.observe(function(f,h,l){if(-1==r.indexOf(b._dirtyObjects,
f))if(-1==h)b.onNew(f);else if(-1==l)b.onDelete(f);else for(var k in f)if(k!=b.objectStore.idProperty)b.onSet(f,k,null,f[k])},!0));this.onFetch(g);a.store=this;return a},getFeatures:function(){return{"dojo.data.api.Read":!!this.objectStore.get,"dojo.data.api.Identity":!0,"dojo.data.api.Write":!!this.objectStore.put,"dojo.data.api.Notification":!0}},getLabel:function(a){if(this.isItem(a))return this.getValue(a,this.labelProperty)},getLabelAttributes:function(a){return[this.labelProperty]},getIdentity:function(a){return this.objectStore.getIdentity?
this.objectStore.getIdentity(a):a[this.objectStore.idProperty||"id"]},getIdentityAttributes:function(a){return[this.objectStore.idProperty]},fetchItemByIdentity:function(a){var c;q.when(this.objectStore.get(a.identity),function(b){c=b;a.onItem.call(a.scope,b)},function(b){a.onError.call(a.scope,b)});return c},newItem:function(a,c){if(c){var b=this.getValue(c.parent,c.attribute,[]);b=b.concat([a]);a.__parent=b;this.setValue(c.parent,c.attribute,b)}this._dirtyObjects.push({object:a,save:!0});this.onNew(a);
return a},deleteItem:function(a){this.changing(a,!0);this.onDelete(a)},setValue:function(a,c,b){var d=a[c];this.changing(a);a[c]=b;this.onSet(a,c,d,b)},setValues:function(a,c,b){if(!p.isArray(b))throw Error("setValues expects to be passed an Array object as its value");this.setValue(a,c,b)},unsetAttribute:function(a,c){this.changing(a);var b=a[c];delete a[c];this.onSet(a,c,b,void 0)},changing:function(a,c){a.__isDirty=!0;for(var b=0;b<this._dirtyObjects.length;b++){var d=this._dirtyObjects[b];if(a==
d.object){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]);this._dirtyObjects.push({object:!c&&a,old:d,save:!this._saveNotNeeded})},save:function(a){a=a||{};var c=[],b=[],d=this,e=this._dirtyObjects;try{w.connect(a,"onError",function(){if(!1!==a.revertOnError){var k=e;e=b;d.revert();d._dirtyObjects=k}else d._dirtyObjects=e.concat(b)});var n;this.objectStore.transaction&&(n=this.objectStore.transaction());for(var m=0;m<
e.length;m++){var g=e[m],f=g.object,h=g.old;delete f.__isDirty;if(f){var l=this.objectStore.put(f,{overwrite:!!h});c.push(l)}else"undefined"!=typeof h&&(l=this.objectStore.remove(this.getIdentity(h)),c.push(l));b.push(g);e.splice(m--,1)}v(c).then(function(k){a.onComplete&&a.onComplete.call(a.scope,k)},function(k){a.onError&&a.onError.call(a.scope,k)});n&&n.commit()}catch(k){a.onError.call(a.scope,value)}},revert:function(){for(var a=this._dirtyObjects,c=a.length;0<c;){c--;var b=a[c],d=b.object;b=
b.old;if(d&&b){for(var e in b)b.hasOwnProperty(e)&&d[e]!==b[e]&&(this.onSet(d,e,d[e],b[e]),d[e]=b[e]);for(e in d)b.hasOwnProperty(e)||(this.onSet(d,e,d[e]),delete d[e])}else if(b)this.onNew(b);else this.onDelete(d);delete (d||b).__isDirty;a.splice(c,1)}},isDirty:function(a){return a?a.__isDirty:!!this._dirtyObjects.length},onSet:function(){},onNew:function(){},onDelete:function(){},onFetch:function(a){}})});