(function(){"use strict";u8.Crm.AdministrationService=function(e){$.extend(this,u8.Crm.AdministrationService.defaults,e);this._designerWindows=[]};u8.services.add("administration",u8.Crm.AdministrationService);u8.Crm.AdministrationService.defaults={};$.extend(u8.Crm.AdministrationService.prototype,{design:function(e){var i=this;var n;if(e.unitName){n=e}else if(e.source){var t=u8.services.commands.getTargets(e.source,function(e){return!!e["designable.getUnits"]});var r=t[0];n=r&&r["designable.getUnits"]()[0]}if(!n){return}u8.request("AdministrationChannel.Design",function(t,r){if(r.error){return}var s=new u8.Base.Uri(e.designerUrl||r.response.designerUrl);s.path+="/api/authentication/connect";s.params.ticket=r.response.ticket;if(u8.debug.level){s.params.debug=u8.debug.level}s.params.unitType=n.unitType;s.params.unitName=n.unitName;s.params.unitSubName=n.unitSubName;var a=window.open(s.toString_());i._designerWindows.push(a);i._waitForUnitChanges()})},invalidateCache:function(e,i){e=$.extend({reload:"Cache",client:true,server:true,verbose:true},e);Function.chain({onError:"error"},this._invalidateServerCache.bind(this,e),this._invalidateClientCache.bind(this,e),this._reloadClientCache.bind(this,e))(function(){if(e.verbose){u8.services.notification.notice("TXT_invalidate_cache_txt_0")}if(i){i()}})},lockSystem:function(e,i){var n=this;var t=$.extend({dueInMinutes:0,text:"",repIds:null,lockType:"LogoutAndLock"},e);u8.request("SystemTrackingChannel.SetLock",{request:t},function(e,t){if(!t.error){u8.services.notification.notice("TXT_search_text_28")}if(i){i(n,t.error?t:t.response)}})},unlockSystem:function(e,i){u8.request("SystemTrackingChannel.ReleaseLock",{lockUid:e},function(e,n){if(!n.error){u8.services.notification.notice("TXT_search_text_27")}if(i){i(self,n.error?n:n.response)}})},isSystemLocked:function(e,i){var n=this;u8.request("SystemTrackingChannel.IsLocked",{lockUid:e},function(e,t){if(i){i(n,{isLocked:!!(t.response&&t.response.isLocked)})}})},cleanupActiveProcesses:function(e){var i=this;u8.request("SystemTrackingChannel.Cleanup",function(n,t){if(!t.error){u8.services.crud.notify("Delete",{uid:{infoAreaId:"T3"},caller:"Administration",callerOperation:"cleanupActiveProcesses",type:"delete",refreshRecommended:true});var r=t.response||0;u8.services.notification.info({text:"TXT_Admin_Text_22",arguments:{number:r}})}if(e){e(i,t)}})},getLogFileTail:function(e,i){if(!e){e=10*1024*1024}var n=new u8.Base.Request({endPoint:"AdministrationChannel.GetLogFileTail",arguments:{numberOfBytesFromEnd:e}});n.executeExport(i)},_invalidateServerCache:function(e,i){if(!e.server){return i()}if(e.server=="UserConfig"){u8.request("ConfigurationChannel.InvalidateUserConfig",null,i)}else{u8.request("AdministrationChannel.InvalidateCache",null,i)}},_invalidateClientCache:function(e,i){if(!e.client){return i()}u8.session.refresh();u8.services.events.raise("u8.services.administration.onInvalidateCache",this,{});u8.services.cache.invalidate();if(i){i()}},_reloadClientCache:function(e,i){var n=e.reload;if(!n){return i()}u8.services.events.raiseAsync("u8.services.administration.onReloadCache",this,{},t);function t(){var e;if(n=="WorkWindow"){e=$w("work")}else if(n=="MainWindow"||n=="MainFrame"){e=$w("main")}else if(n!="Cache"){e=$w("top")}if(e){e.document.body.innerHTML="";e.location.reload();i()}else{u8.services.menus.load({menuName:"$Main"},i)}}},_waitForUnitChanges:function(){if(this._waiter){return}var e="../api-r/designer/waitForUnitChange";var i={dataType:"json",data:{duration:"00:00:10"}};var n=this;function t(){n._waiter=$.ajax(e,i).fail(r).done(s)}function r(){n._waiter=null}function s(e){if(e.error){n._waiter=null;return}e.forEach(function(e){u8.services.events.raise("u8.services.administration.onUnitChange",n,e)});n._waiter=null;n._designerWindows.removeWhere(u8._.eq("closed",true));if(!n._designerWindows.length){return}t()}t()}})})();