Exemplo n.º 1
0
        public void RunJournals()
        {
            var closedTasks = new string[] { "Сделана", "Отклонена" };
            var db = new Model.dbEntities();

            var closedIssues = db.Issues.Include("Status").Where(i => closedTasks.Contains(i.Status.Name)).Select(i => new { i.Id, i.RedmineId }).ToList();
            var alreadySynchronized = db.Journals.GroupBy(i => i.IssueId, (i, j) => i).ToList();
            var openedIssues = db.Issues.Include("Status").Where(i => !closedTasks.Contains(i.Status.Name)).Select(i => new { i.Id, i.RedmineId }).ToList();
            var needSynchronized = closedIssues.Except(closedIssues.Where(i => alreadySynchronized.Contains(i.Id))).Union(openedIssues);

            IList<Model.Facet> facetsCache = db.Facets.ToList();
            IList<Model.JournalFacet> journalFacetsCache = db.JournalFacets.ToList();

            foreach (var issue in needSynchronized.OrderByDescending(i => i.RedmineId))
            {
                try
                {
                    var loadedJournals = db.Journals.Where(i => i.IssueId == issue.Id).Select(i => i.RedmineId).ToList();
                    var url = string.Format("{0}/issues/{1}.json?key={2}&include=journals", _configurator.Link, issue.RedmineId, _configurator.Key);
                    var json = GetStringFromUrl(url);
                    var jResult = JsonConvert.DeserializeObject<Model.JIssueSingle>(json);

                    if (jResult == null || jResult.issue == null || jResult.issue.journals == null || !jResult.issue.journals.Any())
                        continue;

                    foreach (var journal in jResult.issue.journals)
                    {
                        if (loadedJournals.Contains(journal.id))
                            continue;

                        var journalItem = new Model.Journal() { IssueId = issue.Id, RedmineId = journal.id, Date = journal.created_on, Created = DateTime.Now };
                        if (journal.user != null)
                            journalItem.AuthorId = GetFacetId("user", journal.user, facetsCache);
                        db.Journals.Add(journalItem);

                        foreach (var jDetails in journal.details)
                        {
                            var jDetailsItem = new Model.JournalDetail() { Journal = journalItem, OldValue = jDetails.old_value, NewValue = jDetails.new_value };
                            jDetailsItem.PropertyId = GetJournalFacetId(jDetails, journalFacetsCache);
                            db.JournalDetails.Add(jDetailsItem);
                        }
                    }

                    db.SaveChanges();
                    db.Dispose();
                    db = new Model.dbEntities();
                    Console.WriteLine("Journal for issue #{0} synchronized", issue.RedmineId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while synchronizinging journals for issue #{0}: {1}", issue.RedmineId, ex.Message);
                }
            }

            Console.WriteLine("Synchronize completed");
        }
Exemplo n.º 2
0
        public void RunJournals()
        {
            var closedTasks = new string[] { "Сделана", "Отклонена" };
            var db          = new Model.dbEntities();

            var closedIssues        = db.Issues.Include("Status").Where(i => closedTasks.Contains(i.Status.Name)).Select(i => new { i.Id, i.RedmineId }).ToList();
            var alreadySynchronized = db.Journals.GroupBy(i => i.IssueId, (i, j) => i).ToList();
            var openedIssues        = db.Issues.Include("Status").Where(i => !closedTasks.Contains(i.Status.Name)).Select(i => new { i.Id, i.RedmineId }).ToList();
            var needSynchronized    = closedIssues.Except(closedIssues.Where(i => alreadySynchronized.Contains(i.Id))).Union(openedIssues);

            IList <Model.Facet>        facetsCache        = db.Facets.ToList();
            IList <Model.JournalFacet> journalFacetsCache = db.JournalFacets.ToList();

            foreach (var issue in needSynchronized.OrderByDescending(i => i.RedmineId))
            {
                try
                {
                    var loadedJournals = db.Journals.Where(i => i.IssueId == issue.Id).Select(i => i.RedmineId).ToList();
                    var url            = string.Format("{0}/issues/{1}.json?key={2}&include=journals", _configurator.Link, issue.RedmineId, _configurator.Key);
                    var json           = GetStringFromUrl(url);
                    var jResult        = JsonConvert.DeserializeObject <Model.JIssueSingle>(json);

                    if (jResult == null || jResult.issue == null || jResult.issue.journals == null || !jResult.issue.journals.Any())
                    {
                        continue;
                    }

                    foreach (var journal in jResult.issue.journals)
                    {
                        if (loadedJournals.Contains(journal.id))
                        {
                            continue;
                        }

                        var journalItem = new Model.Journal()
                        {
                            IssueId = issue.Id, RedmineId = journal.id, Date = journal.created_on, Created = DateTime.Now
                        };
                        if (journal.user != null)
                        {
                            journalItem.AuthorId = GetFacetId("user", journal.user, facetsCache);
                        }
                        db.Journals.Add(journalItem);

                        foreach (var jDetails in journal.details)
                        {
                            var jDetailsItem = new Model.JournalDetail()
                            {
                                Journal = journalItem, OldValue = jDetails.old_value, NewValue = jDetails.new_value
                            };
                            jDetailsItem.PropertyId = GetJournalFacetId(jDetails, journalFacetsCache);
                            db.JournalDetails.Add(jDetailsItem);
                        }
                    }

                    db.SaveChanges();
                    db.Dispose();
                    db = new Model.dbEntities();
                    Console.WriteLine("Journal for issue #{0} synchronized", issue.RedmineId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error while synchronizinging journals for issue #{0}: {1}", issue.RedmineId, ex.Message);
                }
            }

            Console.WriteLine("Synchronize completed");
        }