Beispiel #1
0
        private static void HandleHistory(Ticket ticket, XmlNode xmlTicket)
        {
            var comments = xmlTicket.SelectNodes("comments/comment");

            foreach (var comment in comments.Cast <XmlElement>())
            {
                var history = new History(comment)
                {
                    userId = GetUserId(comment["author-id"].InnerText),
                };

                if (String.IsNullOrEmpty(history.userId))
                {
                    var person = GetPerson(comment["author-id"].InnerText);
                    if (person != null)
                    {
                        history.by = person.displayName;
                    }

                    history.by = "Onbekend";
                }

                //creation date
                history.creationDate = DateTimeParse.ParseNullableDateTime(comment["created-at"].InnerText);

                // Attachments
                var attachments = comment.SelectNodes("attachments/attachment");
                foreach (var xmlAttachment in attachments.Cast <XmlElement>())
                {
                    var uri = new Uri(xmlAttachment["url"].InnerText);


                    var hash = GetSha1HashString(uri.ToString());

                    var    cache = Path.Combine(_fileCache, hash);
                    byte[] data;
                    if (!File.Exists(cache))
                    {
                        Logger.Debug("Downloading file: {0}", uri);

                        var client = new WebClient();
                        data = client.DownloadData(uri);

                        Logger.Debug("Writing file to cache: {0}", cache);
                        File.WriteAllBytes(cache, data);
                    }
                    else
                    {
                        Logger.Trace("Retreiving file from cache: {0}", uri);
                        data = File.ReadAllBytes(cache);
                    }

                    var attachment = new Attachment
                    {
                        data     = Convert.ToBase64String(data),
                        filename = xmlAttachment["filename"].InnerText,
                        visibleForExternalnet = Boolean.Parse(comment["is-public"].InnerText)
                    };
                    ticket.attachments.Add(attachment);
                }

                ticket.history.Add(history);
            }

            //set history index
            var index = 1;

            foreach (var history in ticket.history.OrderBy(x => x.creationDate))
            {
                history.index = index++;
            }
        }
Beispiel #2
0
        private static void ParseXml()
        {
            Logger.Trace("Parsing xml");

            var tickets = Xml.SelectNodes("/tickets/ticket");

            if (tickets == null)
            {
                return;
            }

            Logger.Debug("{0} tickets found in xml", tickets.Count);



            var currentTicket = 0;

            foreach (var xmlTicket in tickets.Cast <XmlElement>())
            {
                currentTicket++;

                var ticket = new Ticket(xmlTicket);

                Logger.Info("Parsing ticket: {0}/{1} ({2}%)",

                            currentTicket,
                            tickets.Count,

                            ((currentTicket / (decimal)tickets.Count) * 100.0m).ToString("n", CultureInfo.InvariantCulture)

                            );

                ticket.organizationId = GetOrganizationId(xmlTicket["organization-id"].InnerText);
                // map to organization id
                ticket.createdByUserId = GetUserId(xmlTicket["requester-id"].InnerText);                   // map to the user id
                if (ticket.createdByUserId == null)
                {
                    var id     = xmlTicket["requester-id"].InnerText;
                    var person = GetPerson(id);
                    if (person != null)
                    {
                        ticket.personId             = person.id;
                        ticket.createdByDisplayname = person.displayName;
                    }
                }

                ticket.assignedUserId       = GetUserId(xmlTicket["assignee-id"].InnerText);             //Map to the user id
                ticket.assignedDepartmentId = GetDepartmentId(xmlTicket["group-id"].InnerText);
                //Map to the department id

                // Tags
                ticket.tagsPerTicket = new List <TagsPerTicket>();
                var tags = xmlTicket["current-tags"].InnerText.Split(new[]
                {
                    ' '
                },
                                                                     StringSplitOptions.RemoveEmptyEntries);
                foreach (var tag in tags.Select(x => x.Replace("_", " ")))
                {
                    var ticketTag = new TagsPerTicket();
                    if (Tags.ContainsKey(tag))
                    {
                        ticketTag.tagId = Tags[tag].id;
                    }
                    else
                    {
                        var created = CreateTag(new TicketTag(tag));
                        ticketTag.tagId = created.id;
                    }
                    ticket.tagsPerTicket.Add(ticketTag);
                }

                var ticketFields = xmlTicket.SelectNodes("ticket-field-entries/ticket-field-entry");
                // Category
                var category = ticketFields.Cast <XmlElement>().FirstOrDefault();
                if (category != null && !String.IsNullOrWhiteSpace(category["value"].InnerText))
                {
                    var cat = new TicketCategory
                    {
                        name = category["value"].InnerText
                    };
                    if (Categories.ContainsKey(cat.name))
                    {
                        ticket.categoryId = Categories[cat.name].id;
                    }
                    else
                    {
                        var ticketCategory = CreateCategory(cat);
                        ticket.categoryId = ticketCategory.id;
                    }
                }

                // Priority
                if (ticketFields.Count > 3)
                {
                    var priorityMoscow = ticketFields.Cast <XmlElement>().ElementAt(3).InnerText;
                    if (!String.IsNullOrWhiteSpace(priorityMoscow))
                    {
                        var prio = new TicketPriority(priorityMoscow);
                        if (Priorities.ContainsKey(prio.name))
                        {
                            ticket.priorityId = Priorities[prio.name].id;
                        }
                        else
                        {
                            prio = CreatePriority(prio);
                            Priorities.Add(prio.name, prio);
                            ticket.priorityId = prio.id;
                        }
                    }
                }

                HandleHistory(ticket, xmlTicket);

                //set status of ticket (if solved, status done)
                var solvedAt = DateTimeParse.ParseNullableDateTime(xmlTicket["solved-at"].InnerText);
                ticket.statusId = solvedAt.HasValue ? StatusDone.id : StatusNew.id;


                Result.Add(ticket);
            }
        }