public ActionResult Update(string userName, string key, string value)
 {
     return ExecApiMethod(() =>
         {
             var settings = ReviewUtil.GetSettingsDto(key, value);
             var userNameInfo = new UserName(userName, db);
             var id = db.SetUserContext(key, value, userNameInfo.userName, "", settings.version);
             return Index(id, userName, key);
         });
 }
        //
        // GET: /Reviewer/...
        public ActionResult Add(int id, string userName, string reviewerAlias, int changeListId, int status, int requestType, string CL)
        {
            return ExecApiMethod(() =>
                {
                    if (string.IsNullOrWhiteSpace(userName))
                    {
                        var userNameInfo = new UserName(reviewerAlias, db);
                        if (!string.IsNullOrWhiteSpace(userNameInfo.userName) && !string.IsNullOrWhiteSpace(userNameInfo.reviewerAlias))
                        {
                            userName = userNameInfo.userName;
                            reviewerAlias = userNameInfo.reviewerAlias;
                        }
                    }

                    if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(reviewerAlias))
                        throw new ApplicationException(string.Format("Invalid reviewerAlias: '{0}' or userName: '******'", reviewerAlias, userName));

                    var result = db.AddReviewer(id, userName, reviewerAlias, changeListId, status, requestType);
                    Broadcast(result, CL);
                    return Index(result);
                });
        }
        public static void ProcessMailRequests()
        {
            var context = new CodeReviewerContext();
            Dictionary<int, string> sourceControlRoots = new Dictionary<int, string>();

            var sourceControlsQuery = from sc in context.SourceControls select sc;
            foreach (CodeReviewer.Models.SourceControl sc in sourceControlsQuery)
            {
                string site = String.Empty;
                if (!String.IsNullOrEmpty(sc.WebsiteName))
                {
                    if (!sc.WebsiteName.StartsWith("/"))
                        site = "/" + sc.WebsiteName.Substring(1);
                    else
                        site = sc.WebsiteName;
                }
                sourceControlRoots[sc.Id] = site;
            }

            var mailChangeListQuery = from cl in context.MailChangeLists
                                      join rv in context.Reviewers on cl.ReviewerId equals rv.Id
                                      join ch in context.ChangeLists on cl.ChangeListId equals ch.Id
                                      select new { cl, ch, rv.ReviewerAlias };
            var reviewInviteQuery = (from ri in context.MailReviewRequests
                                     join ch in context.ChangeLists on ri.ChangeListId equals ch.Id
                                     select new { ri, ch }).ToArray();

            var itemGroups = mailChangeListQuery.GroupBy(item => item.cl.RequestType);
            var mailItems = new List<MailItem>();

            foreach (var itemGroup in itemGroups)
            {
                var request = itemGroup.First();
                var groupType = (MailType)request.cl.RequestType;
                var userNameInfo = new UserName(request.ch.ReviewerAlias);
                var body = String.Format((string) MailTemplates.Request, request.ch.CL, request.ch.Url, userNameInfo.displayName,
                                         request.ch.Description);

                switch (groupType)
                {
                    case MailType.Request:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.RequestSubject, request.ch.CL,
                                                             request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;

                    case MailType.Iteration:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.IterationSubject, request.ch.CL,
                                                             request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;

                    case MailType.WaitingOnAuthor:
                        {
                            itemGroup.ToList().ForEach(item =>
                            {
                                var mailItem = new MailItem();
                                mailItem.ToAliases.Add(userNameInfo.emailAddress);
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.CcAliases.Add(reviewerUserInfo.emailAddress);
                                mailItem.Subject = String.Format((string)MailTemplates.WaitingOnAuthorSubject, request.ch.CL,
                                                                 item.ReviewerAlias);
                                mailItem.Body = body;
                                mailItems.Add(mailItem);
                            });
                        }
                        break;

                    case MailType.SignedOff:
                        {
                            itemGroup.ToList().ForEach(item =>
                            {
                                var mailItem = new MailItem();
                                mailItem.ToAliases.Add(userNameInfo.emailAddress);
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.CcAliases.Add(reviewerUserInfo.emailAddress);
                                mailItem.Subject = String.Format((string)MailTemplates.SignedOffSubject, request.ch.CL,
                                                                 item.ReviewerAlias);
                                mailItem.Body = body;
                                mailItems.Add(mailItem);
                            });
                        }
                        break;

                    case MailType.SignedOffWithComments:
                        {
                            itemGroup.ToList().ForEach(item =>
                            {
                                var mailItem = new MailItem();
                                mailItem.ToAliases.Add(userNameInfo.emailAddress);
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.CcAliases.Add(reviewerUserInfo.emailAddress);
                                mailItem.Subject = String.Format((string)MailTemplates.SignedOffSubject, request.ch.CL,
                                                                 item.ReviewerAlias);
                                mailItem.Body = body;
                                mailItems.Add(mailItem);
                            });
                        }
                        break;

                    case MailType.Reminder:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.ReminderSubject, request.ch.CL, request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;

                    case MailType.Complete:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.CompleteSubject, request.ch.CL, request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;

                    case MailType.Deleted:
                        {
                            var mailItem = new MailItem();
                            itemGroup.ToList().ForEach(item =>
                            {
                                var reviewerUserInfo = new UserName(item.ReviewerAlias);
                                mailItem.ToAliases.Add(reviewerUserInfo.emailAddress);
                            });
                            mailItem.CcAliases.Add(userNameInfo.emailAddress);
                            mailItem.Subject = String.Format((string)MailTemplates.DeleteSubject, request.ch.CL, request.ch.Title);
                            mailItem.Body = body;
                            mailItems.Add(mailItem);
                        }
                        break;
                }
            }

            if (ExchangeMode)
            {
                var exchangeItems = new List<MessageType>();
                mailItems.ForEach(item => exchangeItems.Add(item.ExchangeItem));
                SendExchangeMail(Config, exchangeItems);
            }

            if (SmtpMode)
            {
                var smtpItems = new List<MailMessage>();
                mailItems.ForEach(item => smtpItems.Add(item.SmtpItem));
                SendSmtpMail(Config, smtpItems);
            }

            foreach (var item in mailChangeListQuery)
            {
                context.Entry(item.cl).State = EntityState.Deleted;
            }

            foreach (var item in reviewInviteQuery)
            {
                context.Entry(item.ri).State = EntityState.Deleted;
            }

            context.SaveChanges();
        }
        static void Main(string[] args)
        {
            Log.ConsoleMode = true;

            int id = 0;
            var changeFiles = new List<ChangeFile>();

            var options = new GenDiffArgs();
            if (Parser.ParseArgumentsWithUsage(args, options))
            {
                if (!string.IsNullOrEmpty(options.CL))
                {
                    //     insert application code here
                    var changeList = (from change in db.ChangeLists
                                      where change.CL == options.CL
                                      select change).FirstOrDefault();
                    if (changeList != null)
                        id = changeList.Id;
                }

                if (!string.IsNullOrEmpty(options.file))
                {
                    var fileName = options.file.ToLower();
                    var files = (from file in db.ChangeFiles
                                       where file.ServerFileName.ToLower().Contains(fileName) &&
                                            (id == 0 || file.ChangeListId == id)
                                       select file);
                    foreach (var file in files)
                    {
                        changeFiles.Add(file);
                    }
                }

                if (options.fileId > 0 && options.revisionId < 0)
                {
                    var fileName = options.file.ToLower();
                    var files = (from file in db.ChangeFiles
                                 where file.Id == options.fileId &&
                                      (id == 0 || file.ChangeListId == id)
                                 select file);
                    foreach (var file in files)
                    {
                        changeFiles.Add(file);
                    }
                }
            }

            var userName = Environment.GetEnvironmentVariable("USERNAME").Trim();
            UserName = new UserName(userName, db);
            UserSettings = ReviewUtil.UserSettings(0, UserName.userName, db);

            if (changeFiles.Count > 0)
            {
                foreach (var changeFile in changeFiles)
                {
                    DiffRevision(changeFile.ChangeListId, changeFile.Id, changeFile.ReviewRevision - 1, force);
                }
            }
            else if (id != 0)
            {
                if (options.fileId != 0 && options.revisionId >= 0)
                {
                    DiffRevision(id, options.fileId, options.revisionId, force);
                }
                else
                {
                    var gen = new DiffGenerator(db);
                    gen.GenDiffFiles(id, UserName.userName, UserSettings, force);
                }
            }
            else
            {
                var gen = new DiffGenerator(db);

                var changelists = (from item in db.ChangeLists.AsNoTracking()
                                   where (item.Stage != (int)ChangeListStatus.Deleted)
                                   select item).OrderByDescending(x => x.TimeStamp);
                foreach (var item in changelists)
                {
                    Log.Info("Generate diff files for {0} {1}", item.CL, item.Id);
                    gen.GenDiffFiles(item.Id, UserName.userName, UserSettings, force);
                }
            }
        }