1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Runtime.Serialization;
5: using System.Text;
6: using System.Text.RegularExpressions;
7:
8: namespace Ia.Cl.Model
9: {
10: ////////////////////////////////////////////////////////////////////////////
11: /// <summary publish="true">
12: /// Result support class.
13: /// </summary>
14: /// <remarks>
15: /// Copyright © 2001-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
16: ///
17: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
18: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
19: ///
20: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
22: ///
23: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
24: ///
25: /// Copyright notice: This notice may not be removed or altered from any source distribution.
26: /// </remarks>
27: [DataContract(IsReference = true, Namespace = "kw.com.ia.api", Name = "apiResult")]
28: public class Result
29: {
30: private string title;
31: private readonly List<TextDateTime> list;
32:
33: /// <summary/>
34: public enum Type
35: {
36: /// <summary/>
37: Success,
38:
39: /// <summary/>
40: Warning,
41:
42: /// <summary/>
43: Error
44: };
45:
46: /// <summary/>
47: public class TextDateTime
48: {
49: /// <summary/>
50: public Type Type { get; set; }
51:
52: /// <summary/>
53: public string Text { get; set; }
54:
55: /// <summary/>
56: public DateTime DateTime { get; set; }
57:
58: /// <summary/>
59: public TextDateTime(string text, Type type, DateTime dateTime)
60: {
61: Type = type;
62: Text = text;
63: DateTime = dateTime;
64: }
65: }
66:
67: ////////////////////////////////////////////////////////////////////////////
68:
69: /// <summary>
70: ///
71: /// </summary>
72: public Result()
73: {
74: list = new List<TextDateTime>();
75: }
76:
77: ////////////////////////////////////////////////////////////////////////////
78:
79: /// <summary>
80: ///
81: /// </summary>
82: public Result(string title)
83: {
84: this.title = title;
85:
86: list = new List<TextDateTime>();
87: }
88:
89: ////////////////////////////////////////////////////////////////////////////
90:
91: /// <summary>
92: ///
93: /// </summary>
94: [DataMember(Name = "title")]
95: public string Title
96: {
97: get
98: {
99: return title;
100: }
101: set
102: {
103: title = value;
104: }
105: }
106:
107: ////////////////////////////////////////////////////////////////////////////
108:
109: /// <summary>
110: ///
111: /// </summary>
112: [DataMember(Name = "isSuccessful")]
113: public bool IsSuccessful
114: {
115: get
116: {
117: return !list.Any(u => u.Type == Type.Error);
118: }
119:
120: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
121: private set { }
122: }
123:
124: ////////////////////////////////////////////////////////////////////////////
125:
126: /// <summary>
127: ///
128: /// </summary>
129: [DataMember(Name = "hasWarning")]
130: public bool HasWarning
131: {
132: get
133: {
134: return list.Any(u => u.Type == Type.Warning);
135: }
136:
137: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
138: private set { }
139: }
140:
141: ////////////////////////////////////////////////////////////////////////////
142:
143: /// <summary>
144: ///
145: /// </summary>
146: [DataMember(Name = "hasError")]
147: public bool HasError
148: {
149: get
150: {
151: return list.Any(u => u.Type == Type.Error);
152: }
153:
154: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
155: private set { }
156: }
157:
158: ////////////////////////////////////////////////////////////////////////////
159:
160: /// <summary>
161: ///
162: /// </summary>
163: [DataMember(Name = "message")]
164: public string Message
165: {
166: get
167: {
168: string s;
169:
170: if (!string.IsNullOrEmpty(Success))
171: {
172: if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
173: {
174: s = "Success: " + Success + ". Warning: " + Warning + ". Error: " + Error + ". ";
175: }
176: else if (!string.IsNullOrEmpty(Warning))
177: {
178: s = "Success: " + Success + ". Warning: " + Warning + ". ";
179: }
180: else if (!string.IsNullOrEmpty(Error))
181: {
182: s = "Success: " + Success + ". Error: " + Error + ". ";
183: }
184: else
185: {
186: s = "Success: " + Success + ". ";
187: }
188: }
189: else
190: {
191: if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
192: {
193: s = "Warning: " + Warning + ". Error: " + Error + ". ";
194: }
195: else if (!string.IsNullOrEmpty(Warning))
196: {
197: s = "Warning: " + Warning + ". ";
198: }
199: else if (!string.IsNullOrEmpty(Error))
200: {
201: s = "Error: " + Error + ". ";
202: }
203: else
204: {
205: s = "";
206: }
207: }
208:
209: CleanUp(ref s);
210:
211: return s;
212: }
213:
214: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
215: private set { }
216: }
217:
218: ////////////////////////////////////////////////////////////////////////////
219:
220: /// <summary>
221: ///
222: /// </summary>
223: [DataMember(Name = "messageWithoutCaption")]
224: public string MessageWithoutCaption
225: {
226: get
227: {
228: string s;
229:
230: if (!string.IsNullOrEmpty(Success))
231: {
232: if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
233: {
234: s = Success + ". " + Warning + ". " + Error + ". ";
235: }
236: else if (!string.IsNullOrEmpty(Warning))
237: {
238: s = Success + ". " + Warning + ". ";
239: }
240: else if (!string.IsNullOrEmpty(Error))
241: {
242: s = Success + ". " + Error + ". ";
243: }
244: else
245: {
246: s = Success + ". ";
247: }
248: }
249: else
250: {
251: if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
252: {
253: s = Warning + ". " + Error + ". ";
254: }
255: else if (!string.IsNullOrEmpty(Warning))
256: {
257: s = Warning + ". ";
258: }
259: else if (!string.IsNullOrEmpty(Error))
260: {
261: s = Error + ". ";
262: }
263: else
264: {
265: s = "";
266: }
267: }
268:
269: CleanUp(ref s);
270:
271: return s;
272: }
273:
274: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
275: private set { }
276: }
277:
278: ////////////////////////////////////////////////////////////////////////////
279:
280: /// <summary>
281: ///
282: /// </summary>
283: [DataMember(Name = "coloredMessage")]
284: public string ColoredMessage
285: {
286: get
287: {
288: string s;
289: StringBuilder stringBuilder;
290:
291: stringBuilder = new StringBuilder();
292:
293: foreach(var v in list.OrderBy(u=>u.DateTime))
294: {
295: if (v.Type == Type.Error) s = @"<span class=""error"">Error: " + v.Text + @"</span><br/>";
296: else if (v.Type == Type.Warning) s = @"<span class=""warning"">Warning: " + v.Text + @"</span><br/>";
297: else if (v.Type == Type.Success) s = @"<span class=""success"">Success: " + v.Text + @"</span><br/>";
298: else s = string.Empty;
299:
300: if(!string.IsNullOrEmpty(s)) stringBuilder.AppendLine(s);
301: }
302:
303: stringBuilder = new StringBuilder(Regex.Replace(stringBuilder.ToString(), @"\.\s+\.", "."));
304:
305: return stringBuilder.ToString();
306: }
307:
308: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
309: private set { }
310: }
311:
312: ////////////////////////////////////////////////////////////////////////////
313:
314: /// <summary>
315: ///
316: /// </summary>
317: [DataMember(Name = "coloredMessageWithoutCaption")]
318: public string ColoredMessageWithoutCaption
319: {
320: get
321: {
322: string s;
323: StringBuilder stringBuilder;
324:
325: stringBuilder = new StringBuilder();
326:
327: foreach (var v in list.OrderBy(u => u.DateTime))
328: {
329: if (v.Type == Type.Error) s = @"<span class=""error"">" + v.Text + @"</span><br/>";
330: else if (v.Type == Type.Warning) s = @"<span class=""warning"">" + v.Text + @"</span><br/>";
331: else if (v.Type == Type.Success) s = @"<span class=""success"">" + v.Text + @"</span><br/>";
332: else s = string.Empty;
333:
334: if (!string.IsNullOrEmpty(s)) stringBuilder.AppendLine(s);
335: }
336:
337: stringBuilder = new StringBuilder(Regex.Replace(stringBuilder.ToString(), @"\.\s+\.", "."));
338:
339: return stringBuilder.ToString();
340: }
341:
342: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
343: private set { }
344: }
345:
346: ////////////////////////////////////////////////////////////////////////////
347:
348: /// <summary>
349: ///
350: /// </summary>
351: public string Success
352: {
353: get
354: {
355: string s;
356:
357: s = string.Empty;
358:
359: foreach (TextDateTime u in list.Where(u => u.Type == Type.Success)) s += u.Text + " ";
360:
361: if (!string.IsNullOrEmpty(s))
362: {
363: s = s.Remove(s.Length - 1, 1); // remove last ' '
364: //s += ". ";
365: }
366:
367: CleanUp(ref s);
368:
369: return s;
370: }
371: }
372:
373: ////////////////////////////////////////////////////////////////////////////
374:
375: /// <summary>
376: ///
377: /// </summary>
378: public string Warning
379: {
380: get
381: {
382: string s;
383:
384: s = string.Empty;
385:
386: foreach (TextDateTime u in list.Where(u=>u.Type == Type.Warning)) s += u.Text + " ";
387:
388: if (!string.IsNullOrEmpty(s))
389: {
390: s = s.Remove(s.Length - 1, 1); // remove last ' '
391: //s += ". ";
392: }
393:
394: CleanUp(ref s);
395:
396: return s;
397: }
398: }
399:
400: ////////////////////////////////////////////////////////////////////////////
401:
402: /// <summary>
403: ///
404: /// </summary>
405: public string Error
406: {
407: get
408: {
409: string s;
410:
411: s = string.Empty;
412:
413: foreach(TextDateTime u in list.Where(u => u.Type == Type.Error)) s += u.Text + " ";
414:
415: if (!string.IsNullOrEmpty(s))
416: {
417: s = s.Remove(s.Length - 1, 1); // remove last ' '
418: //s += ". ";
419: }
420:
421: CleanUp(ref s);
422:
423: return s;
424: }
425: }
426:
427: ////////////////////////////////////////////////////////////////////////////
428:
429: /// <summary>
430: ///
431: /// </summary>
432: private void CleanUp(ref string line)
433: {
434: line = Regex.Replace(line, @"\.\s+\.", ".");
435: line = line.Replace("?.", "?");
436: line = line.Replace("!.", "!");
437: line = line.Replace("..", ".");
438: }
439:
440: ////////////////////////////////////////////////////////////////////////////
441:
442: /// <summary>
443: ///
444: /// </summary>
445: public void AddSuccess(string message)
446: {
447: this.list.Add(new TextDateTime(message, Type.Success, DateTime.UtcNow.AddHours(3)));
448: }
449:
450: ////////////////////////////////////////////////////////////////////////////
451:
452: /// <summary>
453: ///
454: /// </summary>
455: public void AddSuccess(string title, string message)
456: {
457: this.title = title;
458:
459: this.list.Add(new TextDateTime(message, Type.Success, DateTime.UtcNow.AddHours(3)));
460: }
461:
462: ////////////////////////////////////////////////////////////////////////////
463:
464: /// <summary>
465: ///
466: /// </summary>
467: public void AddWarning(string message)
468: {
469: this.list.Add(new TextDateTime(message, Type.Warning, DateTime.UtcNow.AddHours(3)));
470: }
471:
472: ////////////////////////////////////////////////////////////////////////////
473:
474: /// <summary>
475: ///
476: /// </summary>
477: public void AddWarning(string title, string message)
478: {
479: this.title = title;
480:
481: this.list.Add(new TextDateTime(message, Type.Warning, DateTime.UtcNow.AddHours(3)));
482: }
483:
484: ////////////////////////////////////////////////////////////////////////////
485:
486: /// <summary>
487: ///
488: /// </summary>
489: public void AddError(string message)
490: {
491: this.list.Add(new TextDateTime(message, Type.Error, DateTime.UtcNow.AddHours(3)));
492: }
493:
494: ////////////////////////////////////////////////////////////////////////////
495:
496: /// <summary>
497: ///
498: /// </summary>
499: public void AddError(string title, string message)
500: {
501: this.title = title;
502:
503: this.list.Add(new TextDateTime(message, Type.Error, DateTime.UtcNow.AddHours(3)));
504: }
505:
506: ////////////////////////////////////////////////////////////////////////////
507:
508: /// <summary>
509: ///
510: /// </summary>
511: public void AddResult(Result result)
512: {
513: this.list.AddRange(result.list);
514: }
515:
516: ////////////////////////////////////////////////////////////////////////////
517:
518: /// <summary>
519: ///
520: /// </summary>
521: public void AddResult(string title, Result result)
522: {
523: this.title = title;
524:
525: this.list.AddRange(result.list);
526: }
527:
528: ////////////////////////////////////////////////////////////////////////////
529: ////////////////////////////////////////////////////////////////////////////
530: }
531:
532: ////////////////////////////////////////////////////////////////////////////
533: ////////////////////////////////////////////////////////////////////////////
534: }