public bool Connect()
        {
            if (_connectionStatus == ConnectionStatus.Connected || _connectionStatus == ConnectionStatus.Inactive)
            {
                return(true);
            }

            try
            {
                using (StoreKeeperServiceProxy proxy = new StoreKeeperServiceProxy(_clientConfiguration))
                {
                    _sessionId        = proxy.Connect(CreateTicket());
                    _connectionStatus = _sessionId != null ? ConnectionStatus.Connected : ConnectionStatus.Disconnected;
                    if (_connectionStatus == ConnectionStatus.Connected)
                    {
                        string userId = proxy.GetCurrentUserInfo(_sessionId).Id.ToString().ToUpper();
                        UserContext.Initialize(userId);
                        ClientRepository.CheckDatabaseStatus();
                    }
                }
            }
            catch (Exception e)
            {
                ApplicationContext.Log.Error(GetType(), e);
                _connectionStatus = ConnectionStatus.Disconnected;
            }

            return(_connectionStatus == ConnectionStatus.Connected);
        }
 public bool RemoveUser(IServerUser user)
 {
     using (StoreKeeperServiceProxy proxy = new StoreKeeperServiceProxy(_clientConfiguration))
     {
         return(proxy.RemoveUser(_sessionId, user.UserId));
     }
 }
 private bool ChangeUserName(Guid userId, string newUserName)
 {
     using (StoreKeeperServiceProxy proxy = new StoreKeeperServiceProxy(_clientConfiguration))
     {
         return(proxy.ChangeUserName(_sessionId, userId, newUserName));
     }
 }
        private void ProcessAccountingDataSync(IAsyncResult asyncResult)
        {
            try
            {
                using (_currentProxy)
                {
                    bool syncResult = _currentProxy.EndGetCurrentAccountingData(asyncResult);
                    if (!syncResult)
                    {
                        _longOperationHandler.OperationFailed("Cannot perform synchronization with Pohoda");
                        return;
                    }

                    LongOperationResult result = new LongOperationResult {
                        RefreshAll = true
                    };
                    _longOperationHandler.End(result);
                    Logger.Info("Accounting data synchronization finished succesfully.");
                    ClientRepository.ResetDatabaseStatus();
                }
                _currentProxy = null;
            }
            catch (Exception ex)
            {
                _longOperationHandler.OperationFailed(ex.Message);
            }
        }
        public bool Disconnect()
        {
            if (_connectionStatus == ConnectionStatus.Disconnected || _connectionStatus == ConnectionStatus.Inactive)
            {
                return(true);
            }

            try
            {
                using (StoreKeeperServiceProxy proxy = new StoreKeeperServiceProxy(_clientConfiguration))
                {
                    if (proxy.Disconnect(_sessionId))
                    {
                        _connectionStatus = ConnectionStatus.Disconnected;
                        UserContext.Close();
                    }
                }
            }
            catch (Exception e)
            {
                ApplicationContext.Log.Error(GetType(), e);
                _connectionStatus = ConnectionStatus.Disconnected;
                return(false);
            }

            return(_connectionStatus == ConnectionStatus.Disconnected);
        }
 public IServerUser CreateUser(string username)
 {
     using (StoreKeeperServiceProxy proxy = new StoreKeeperServiceProxy(_clientConfiguration))
     {
         UserData newUser = proxy.CreateUser(_sessionId, username);
         return(new ServerUser(newUser, ChangeUserName));
     }
 }
        public void CalculationAndSaveAsync()
        {
            Task.Factory.StartNew(() =>
            {
                GetLock();

                _longOperationHandler.Start("Calculation", "Calculating");

                try
                {
                    _currentProxy = new StoreKeeperServiceProxy(_clientConfiguration);
                    _currentProxy.BeginCalculationAndSave(_sessionId, ProcessCalculation, null);
                }
                catch (Exception ex)
                {
                    _longOperationHandler.OperationFailed(ex.Message);
                }
            });
        }
        public void GetCurrentAccountingDataAsync(bool reloadAll)
        {
            Task.Factory.StartNew(() =>
            {
                GetLock();

                _longOperationHandler.Start("AccountingDataSync");

                try
                {
                    _currentProxy = new StoreKeeperServiceProxy(_clientConfiguration);
                    _currentProxy.BeginGetCurrentAccountingData(_sessionId, reloadAll, ProcessAccountingDataSync, null);
                }
                catch (Exception ex)
                {
                    _longOperationHandler.OperationFailed(ex.Message);
                }
            });
        }
 private void ProcessCalculation(IAsyncResult asyncResult)
 {
     try
     {
         using (_currentProxy)
         {
             _currentProxy.EndCalculationAndSave(asyncResult);
             ReloadAllData(false);
             _longOperationHandler.End(new LongOperationResult {
                 RefreshAll = true
             });
             ClientRepository.ResetDatabaseStatus();
             Messenger.CalculationRequested = false;
         }
         _currentProxy = null;
     }
     catch (Exception ex)
     {
         _longOperationHandler.OperationFailed(ex.Message);
         throw;
     }
 }
        public void GetLock()
        {
            if (DatabaseStatus == DatabaseStatus.Locked || DatabaseStatus == DatabaseStatus.NotConnected)
            {
                return;
            }

            if (DatabaseStatus == DatabaseStatus.Blocked)
            {
                throw new DatabaseLockedException(GetType(), "DatabaseLockedException");
            }

            try
            {
                using (StoreKeeperServiceProxy proxy = new StoreKeeperServiceProxy(_clientConfiguration))
                {
                    bool result = proxy.GetLock(_sessionId);
                    if (!result)
                    {
                        Logger.Error("Cannot get database lock!");
                        throw new DatabaseLockedException(GetType(), "DatabaseLockedException");
                    }
                }

                Logger.Info("Lock acquired succesfully.");
                ClientRepository.CheckDatabaseStatus();
                Messenger.DatabaseStatusChanged();
            }
            catch (DatabaseLockedException)
            {
                throw;
            }
            catch (Exception ex)
            {
                _longOperationHandler.OperationFailed(ex.Message);
            }
        }