コード例 #1
0
        public ActionResult Create(string startDate, string endDate, int itemId)
        {
            DateTime?startDateTime = ParseDateString.GetDateForString(startDate);
            DateTime?endDateTime   = ParseDateString.GetDateForString(endDate);

            if (startDateTime == null || endDateTime == null || endDateTime < startDateTime)
            {
                return(JsonError("Make sure Start Date and End Date are valid dates"));
            }

            //If selection range is bigger than 3 years set the last date to max 3 years
            if (((endDateTime.Value - DateTime.Today).TotalDays / 365) > 3)
            {
                endDateTime = DateTime.Today.AddYears(3);

                if (startDateTime > endDateTime)
                {
                    startDateTime = endDateTime;
                }
            }

            var closedStatuses = MetaManager.StatusGetClosed();

            List <IssueLinkType> linkTypes        = IssueManager.GeminiContext.Meta.LinkTypeGet();
            IssueLinkType        repeatedLinkType = linkTypes.Find(t => string.Compare(t.Label, "Repeated", true) == 0);

            if (repeatedLinkType == null && linkTypes.Count > 0)
            {
                repeatedLinkType = linkTypes[0];
            }

            var issue = IssueManager.Get(itemId);

            RepeatParser repeat = new RepeatParser(issue.Repeated);

            List <IssueDto> repeatedIssues = IssueManager.GetItemsForOriginator(IssueOriginatorType.Repeat, issue.Id.ToString());

            if (repeatedIssues.Count > 0)
            {
                var previousItemsToDelete = repeatedIssues.FindAll(c => c.Created.Date() >= startDateTime.Value && c.Created.Date() <= endDateTime.Value && !closedStatuses.Contains(c.Entity.StatusId));

                foreach (var item in previousItemsToDelete)
                {
                    IssueManager.Delete(item.Entity.Id);
                }
            }



            for (DateTime date = startDateTime.Value; date <= endDateTime.Value; date = date.AddDays(1))
            {
                repeat.CurrentDateTime = date.Date();

                IssueDto lastRepeated = IssueManager.GetLastCreatedIssueForOriginator(IssueOriginatorType.Repeat, issue.Id.ToString());

                DateTime lastRepeatedDate = issue.Created;

                if (lastRepeated != null && lastRepeated.Entity.IsExisting)
                {
                    lastRepeatedDate = lastRepeated.Created;
                }

                if (repeat.MaximumRepeats > 0)
                {
                    repeatedIssues = IssueManager.GetItemsForOriginator(IssueOriginatorType.Repeat, issue.Id.ToString());

                    if (repeatedIssues != null && repeatedIssues.Count >= repeat.MaximumRepeats)
                    {
                        continue;
                    }
                }

                //If last item was created into the future do this
                if (lastRepeatedDate > date.Date())
                {
                    List <IssueDto> tmpRepeatedIssues = IssueManager.GetItemsForOriginator(IssueOriginatorType.Repeat, issue.Id.ToString());

                    List <IssueDto> ItemsBeforeStartDate = tmpRepeatedIssues.FindAll(i => i.Created < date.Date());

                    if (ItemsBeforeStartDate.Count == 0)
                    {
                        lastRepeatedDate = issue.Created;
                    }
                    else
                    {
                        lastRepeatedDate = ItemsBeforeStartDate.OrderBy("Created").Last().Created;
                    }
                }

                if (repeat.NeedsToRepeat(lastRepeatedDate))
                {
                    var customFields = issue.CustomFields;

                    issue.Attachments = new List <IssueAttachmentDto>();

                    issue.Entity.Repeated = string.Empty;

                    issue.Entity.OriginatorData = issue.Entity.Id.ToString();

                    issue.Entity.OriginatorType = IssueOriginatorType.Repeat;

                    issue.Entity.ParentIssueId = null;
                    issue.Entity.IsParent      = false;

                    issue.Entity.StatusId = 0;

                    issue.Entity.ResolutionId = 0;

                    if (issue.Entity.StartDate.HasValue && issue.Entity.DueDate.HasValue &&
                        issue.Entity.StartDate != new DateTime() && issue.Entity.DueDate != new DateTime())
                    {
                        TimeSpan tsDates = issue.Entity.DueDate.Value - issue.Entity.StartDate.Value;

                        issue.Entity.DueDate = date.AddDays(tsDates.TotalDays);

                        issue.Entity.StartDate = date.Date();
                    }
                    else
                    {
                        issue.Entity.StartDate = null;

                        issue.Entity.DueDate = null;
                    }

                    int issueId = issue.Id;

                    issue.Entity.Created = date;

                    IssueDto repeated = IssueManager.Create(issue.Entity);

                    if (repeated.Entity.Id > 0)
                    {
                        string statment = "update gemini_issues set created = @created where issueid = @issueid";

                        SQLService.Instance.ExecuteQuery(statment, new { created = new DateTime(date.Year, date.Month, date.Day, 8, 0, 0).ToUtc(UserContext.User.TimeZone), issueid = repeated.Entity.Id });
                    }

                    if (customFields != null && customFields.Count > 0)
                    {
                        foreach (var field in customFields)
                        {
                            try
                            {
                                //Find the existing ID to 'replace', if exists.
                                var existingCF = repeated.CustomFields
                                                 .SingleOrDefault(s => s.Entity.CustomFieldId == field.Entity.CustomFieldId);
                                field.Entity.Id = existingCF == null ? 0 : existingCF.Entity.Id;

                                field.Entity.IssueId = repeated.Entity.Id;

                                field.Entity.ProjectId = repeated.Entity.ProjectId;

                                CustomFieldManager.Update(new CustomFieldData(field.Entity));
                            }
                            catch (Exception ex)
                            {
                                LogException(ex);
                            }
                        }
                    }

                    if (repeatedLinkType != null)
                    {
                        IssueManager.IssueLinkCreate(repeated.Entity.Id, issueId, repeatedLinkType.Id);
                    }
                }
            }

            return(JsonSuccess());
        }
コード例 #2
0
        public override bool Run(IssueManager issueManager)
        {
/*#if DEBUG
 *          Debugger.Launch();
 #endif*/
            LogDebugMessage("Repeat Processing");

            try
            {
                List <IssueDto> issues = issueManager.GetRepeated();

                if (issues == null || issues.Count == 0)
                {
                    return(true);
                }

                List <IssueLinkType> linkTypes = issueManager.GeminiContext.Meta.LinkTypeGet();

                IssueLinkType repeatedLinkType = linkTypes.Find(t => string.Compare(t.Label, "Repeated", true) == 0);

                if (repeatedLinkType == null && linkTypes.Count > 0)
                {
                    repeatedLinkType = linkTypes[0];
                }

                foreach (IssueDto issue in issues)
                {
                    RepeatParser repeat = new RepeatParser(issue.Repeated);

                    repeat.CurrentDateTime = DateTime.UtcNow.ToLocal(issueManager.UserContext.User.TimeZone);

                    IssueDto lastRepeated = issueManager.GetLastCreatedIssueForOriginator(IssueOriginatorType.Repeat, issue.Id.ToString());

                    DateTime lastRepeatedDate = issue.Created;

                    if (lastRepeated != null && lastRepeated.Entity.IsExisting)
                    {
                        lastRepeatedDate = lastRepeated.Created;
                    }

                    if (repeat.MaximumRepeats > 0)
                    {
                        List <IssueDto> repeatedIssues = issueManager.GetItemsForOriginator(IssueOriginatorType.Repeat, issue.Id.ToString());

                        if (repeatedIssues != null && repeatedIssues.Count >= repeat.MaximumRepeats)
                        {
                            continue;
                        }
                    }

                    if (repeat.NeedsToRepeat(lastRepeatedDate))
                    {
                        var customFields = issue.CustomFields;

                        issue.Attachments = new List <IssueAttachmentDto>();

                        issue.Entity.Repeated = string.Empty;

                        issue.Entity.OriginatorData = issue.Entity.Id.ToString();

                        issue.Entity.OriginatorType = IssueOriginatorType.Repeat;

                        issue.Entity.ParentIssueId = null;
                        issue.Entity.IsParent      = false;

                        issue.Entity.StatusId = 0;

                        issue.Entity.ResolutionId  = 0;
                        issue.Entity.LoggedHours   = 0;
                        issue.Entity.LoggedMinutes = 0;

                        if (issue.Entity.StartDate.HasValue && issue.Entity.DueDate.HasValue &&
                            issue.Entity.StartDate != new DateTime() && issue.Entity.DueDate != new DateTime())
                        {
                            TimeSpan tsDates = issue.Entity.DueDate.Value - issue.Entity.StartDate.Value;

                            issue.Entity.DueDate = DateTime.Today.AddDays(tsDates.TotalDays);

                            issue.Entity.StartDate = DateTime.Today;
                        }
                        else
                        {
                            issue.Entity.StartDate = null;

                            issue.Entity.DueDate = null;
                        }

                        int issueId = issue.Id;

                        //Set the ID to 0 so it does not steal the custom fields for that tissue
                        var originalIssueID = issue.Entity.Id;
                        issue.Entity.Id = 0;

                        IssueDto repeated = issueManager.Create(issue.Entity);

                        if (customFields != null && customFields.Count > 0)
                        {
                            var customFieldManager = new CustomFieldManager(issueManager);

                            foreach (var field in customFields)
                            {
                                try
                                {
                                    //Find the existing ID to 'replace', if exists.
                                    var existingCF = repeated.CustomFields
                                                     .SingleOrDefault(s => s.Entity.CustomFieldId == field.Entity.CustomFieldId);
                                    field.Entity.Id = existingCF == null ? 0 : existingCF.Entity.Id;

                                    field.Entity.IssueId = repeated.Entity.Id;

                                    field.Entity.ProjectId = repeated.Entity.ProjectId;

                                    customFieldManager.Update(new CustomFieldData(field.Entity));
                                }
                                catch (Exception ex)
                                {
                                    LogException(ex);
                                }
                            }
                        }

                        if (repeatedLinkType != null)
                        {
                            issueManager.IssueLinkCreate(repeated.Entity.Id, issueId, repeatedLinkType.Id);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
            }

            return(true);
        }