MaintenanceHelper.cs 7.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml;

namespace Coscine.Api.Notices.Utils
{
    /// <summary>
    /// This class helps to retrieve the relevant maintenance message for a certain service.
    /// </summary>
    public class MaintenanceHelper
    {
        /// <summary>
        /// How the undefined string should look.
        /// </summary>
        public string UndefinedString { get; set; }

        /// <summary>
        /// Order of relevance to be used.
        /// Only the most relevant (first in the list) maintenace message is later displayed.
        /// All non listed "Störungen" are sorted as "-NotDefined-".
        /// Use this list to adjust the order.
        /// Types which are not part of the list, will be set to the UndefinedString property.
        /// </summary>
        public List<string> RelevanceList { get; set; }

        /// <summary>
        /// Get the most relevant maintenance from the rss feed
        /// </summary>
        /// <param name="feedUrl">Url of the rss feed</param>
        /// <returns>The most relevant Maintenance</returns>
        internal Maintenance GetMaintenance(string feedUrl)
        {
            return GetMaintenance(feedUrl, RelevanceList);
        }

        /// <summary>
        /// Get the most relevant maintenance from the rss feed
        /// </summary>
        /// <param name="feedUrl">URL of the rss feed</param>
        /// <param name="order">Order of Importance</param>
        /// <returns>The most relevant Maintenance</returns>
        internal Maintenance GetMaintenance(string feedUrl, List<string> order)
        {
            Maintenance result = null;

            if (order.FindIndex(x => x.ToLower() == UndefinedString.ToLower()) == -1)
            {
                order.Add(UndefinedString);
            }

            int resultOrderPos = order.Count + 1;
            var rssfeed = SyndicationFeed.Load(XmlReader.Create(feedUrl));
            var syndicationItems = rssfeed.Items.OrderByDescending(x => x.PublishDate).ToList();

            foreach (var syndicationItem in syndicationItems)
            {
                try
                {
                    if (syndicationItem != null && syndicationItem.Summary != null && !string.IsNullOrWhiteSpace(syndicationItem.Summary.Text))
                    {
                        var current = new Maintenance
                        {
                            DisplayName = syndicationItem.Title.Text,
                            Url = syndicationItem.Links[0].Uri
                        };

                        string summary = syndicationItem.Summary.Text;
                        int splitter = summary.IndexOf('-');
                        if (splitter != -1)
                        {
                            current.Body = summary.Substring(splitter + 1, summary.Length - splitter - 1).Trim();
                            string firstPart = summary.Substring(0, splitter).Trim();
                            splitter = firstPart.IndexOf("von");
                            if (splitter != -1)
                            {
                                current.Type = firstPart.Substring(0, splitter).Trim();
                                string timespan = firstPart.Substring(splitter + 3, firstPart.Length - splitter - 3).Trim();

                                splitter = timespan.IndexOf("bis");
                                if (splitter != -1)
                                {
                                    string startsString = timespan.Substring(0, splitter).Trim();
                                    string endsString = timespan.Substring(splitter + 3, timespan.Length - splitter - 3).Trim();

                                    var dayList = new List<string> { "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag" };

                                    foreach (var dayString in dayList)
                                    {
                                        startsString = startsString.Replace(dayString, "").Trim();
                                        startsString = startsString.Replace(dayString.ToLower(), "").Trim();
                                    }

                                    foreach (var dayString in dayList)
                                    {
                                        endsString = endsString.Replace(dayString, "").Trim();
                                        endsString = endsString.Replace(dayString.ToLower(), "").Trim();
                                    }

                                    if (startsString.ToLower() == "unbekannt")
                                    {
                                        current.StartsDate = DateTime.MinValue;
                                    }
                                    else
                                    {
                                        current.StartsDate = DateTime.ParseExact(startsString, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
                                    }

                                    if (endsString.ToLower() == "unbekannt")
                                    {
                                        current.EndsDate = DateTime.MaxValue;
                                    }
                                    else
                                    {
                                        current.EndsDate = DateTime.ParseExact(endsString, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture);
                                    }

                                    // still active message
                                    if (current.EndsDate > DateTime.Now && current.StartsDate < DateTime.Now)
                                    {
                                        // first listed valid message
                                        if (result == null)
                                        {
                                            result = current;
                                            resultOrderPos = GetOrderPos(order, result.Type);
                                        }
                                        else
                                        {
                                            // check if the current message is higher ranged as the previosly result
                                            int currentOrderPos = GetOrderPos(order, current.Type);
                                            // higher rang of type
                                            if (currentOrderPos < resultOrderPos)
                                            {
                                                result = current;
                                                resultOrderPos = currentOrderPos;
                                                // same type but longer active
                                            }
                                            else if (currentOrderPos < resultOrderPos && current.EndsDate > result.EndsDate)
                                            {
                                                result = current;
                                                resultOrderPos = currentOrderPos;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    // ignore not correct parsed messages
                }
            }
            return result;
        }

        private int GetOrderPos(List<string> order, string type)
        {
            int res = order.FindIndex(x => x.ToLower() == type.ToLower());

            if (res == -1)
            {
                res = order.FindIndex(x => x.ToLower() == UndefinedString.ToLower());
            }

            if (res == -1)
            {
                res = order.Count + 1;
            }

            return res;
        }
    }
}