(function(){"use strict";u8.Crm.ExpandSaveProvider=function(e){$.extend(this,e);this.bind(e)};u8.Crm.ExpandSaveProvider.makeClass({name:"ExpandSaveProvider",events:[{name:"PreSave",globalName:"u8.widgets.expand.onPreSave"},{name:"Save",globalName:"u8.widgets.expand.onSave"}]});$.extend(u8.Crm.ExpandSaveProvider.prototype,{save:function(e,i){var a=u8.Base.Request.createQueue({serverOptions:{transact:!e.expandModel.disableTransaction}});var s={options:e,requestQueue:a,crudOptions:{callee:"Expand",calleeId:e.expandModel.id,disableTransaction:e.expandModel.disableTransaction}};var n=this;this.raiseAsync("PreSave",s,t);function t(e,i){if(i.cancel){return r()}var a=i.options.expandModel.getItem("Root").businessObject.uid.recordId&&!i.options.expandModel.isModified()&&(i.requestQueue&&i.requestQueue.isEmpty());if(a){i.cancel=true;i.isModified=false;return r()}n._execute(i,r)}function r(e){s.error=e&&e.error;s.messages=e&&e.messages;delete s.requestQueue;n.raiseAsync("Save",s,d)}function d(e,a){return i&&i(n,a)}},_execute:function(i,a){var s=i.options;var n=i.requestQueue;var t=s.expandModel;var r=[];var d=t.getItem();var u=d.getLinks().map(e);var o=!u8.isValidRecordUid(t.getUid());if(o||t.isModified()){var l={links:u,disableTransaction:t.disableTransaction};if("enableCalculations"in this){l.enableCalculations=this.enableCalculations}r.push({type:"createOrUpdate",businessObject:d.businessObject,modifiedFieldsOnly:true,alias:d.alias,options:l})}this._saveChildItems(t,d,r);n.insertionPoint=0;u8.services.crud.executeBatch({requests:r,requestQueue:n,executeQueue:true,options:i.crudOptions},c);function c(e,i){if(i.error){return a&&a(i)}t.clearModified();return a&&a({messages:u8.Crm.Crud.buildMessages(i)})}},_saveChildItems:function(i,a,s){var n=this;i.eachItem(function(i){if(i.parentAlias!=a.alias){return}var n=i.businessObject;if(!u8.isValidRecordUid(n.uid)){return}if(!n.isModified()){return}s.push({type:"update",businessObject:n,modifiedFieldsOnly:true,alias:i.alias,options:{links:i.getLinks().map(e)},backReferenceLinks:[{alias:a.alias,linkId:i.linkId}]})});i.eachItem(function(e){if(e.parentAlias!=a.alias){return}n._saveChildItems(i,e,s)})}});function e(e){return{infoAreaId:e.infoAreaId,recordId:e.recordId,linkId:e.linkId}}})();