Ejemplo n.º 1
0
        public void ChangeFolderCounters(
            IDaoFactory daoFactory,
            FolderType folder,
            uint?userFolder    = null,
            int?unreadMessDiff = null,
            int?totalMessDiff  = null,
            int?unreadConvDiff = null,
            int?totalConvDiff  = null)
        {
            if (folder == FolderType.UserFolder && !userFolder.HasValue)
            {
                throw new ArgumentException(@"ChangeFolderCounters failed", "userFolder");
            }

            try
            {
                var dao = daoFactory.CreateFolderDao(Tenant, User);

                var res = dao
                          .ChangeFolderCounters(folder, unreadMessDiff, totalMessDiff, unreadConvDiff, totalConvDiff);

                if (res == 0)
                {
                    var totalCount       = totalMessDiff.GetValueOrDefault(0);
                    var unreadCount      = unreadMessDiff.GetValueOrDefault(0);
                    var unreadChainCount = unreadConvDiff.GetValueOrDefault(0);
                    var totalChainCount  = totalConvDiff.GetValueOrDefault(0);

                    if (totalCount < 0 || unreadCount < 0 || unreadChainCount < 0 || totalChainCount < 0)
                    {
                        throw new Exception("Need recalculation");
                    }

                    var f = dao.GetFolder(folder);

                    if (f == null)
                    {
                        // Folder is not found

                        res = dao.Save(new Folder
                        {
                            FolderType       = folder,
                            Tenant           = Tenant,
                            UserId           = User,
                            TotalCount       = totalCount,
                            UnreadCount      = unreadCount,
                            UnreadChainCount = unreadChainCount,
                            TotalChainCount  = totalChainCount,
                            TimeModified     = DateTime.UtcNow
                        });

                        if (res == 0)
                        {
                            throw new Exception("Need recalculation");
                        }
                    }
                    else
                    {
                        throw new Exception("Need recalculation");
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("ChangeFolderCounters() Exception: {0}", ex.ToString());
                var engine = new EngineFactory(Tenant, User);
                engine.OperationEngine.RecalculateFolders();
            }

            if (!userFolder.HasValue)
            {
                return;
            }

            Factory.UserFolderEngine.ChangeFolderCounters(daoFactory, userFolder.Value, unreadMessDiff, totalMessDiff, unreadConvDiff, totalConvDiff);
        }