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: s = string.Empty;
171:
172: if (!string.IsNullOrEmpty(Success))
173: {
174: if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
175: {
176: s = "Success: " + Success + ". Warning: " + Warning + ". Error: " + Error + ". ";
177: }
178: else if (!string.IsNullOrEmpty(Warning))
179: {
180: s = "Success: " + Success + ". Warning: " + Warning + ". ";
181: }
182: else if (!string.IsNullOrEmpty(Error))
183: {
184: s = "Success: " + Success + ". Error: " + Error + ". ";
185: }
186: else
187: {
188: s = "Success: " + Success + ". ";
189: }
190: }
191: else
192: {
193: if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
194: {
195: s = "Warning: " + Warning + ". Error: " + Error + ". ";
196: }
197: else if (!string.IsNullOrEmpty(Warning))
198: {
199: s = "Warning: " + Warning + ". ";
200: }
201: else if (!string.IsNullOrEmpty(Error))
202: {
203: s = "Error: " + Error + ". ";
204: }
205: else
206: {
207: s = "";
208: }
209: }
210:
211: CleanUp(ref s);
212:
213: return s;
214: }
215:
216: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
217: private set { }
218: }
219:
220: ////////////////////////////////////////////////////////////////////////////
221:
222: /// <summary>
223: ///
224: /// </summary>
225: [DataMember(Name = "messageWithoutCaption")]
226: public string MessageWithoutCaption
227: {
228: get
229: {
230: string s;
231:
232: s = string.Empty;
233:
234: if (!string.IsNullOrEmpty(Success))
235: {
236: if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
237: {
238: s = Success + ". " + Warning + ". " + Error + ". ";
239: }
240: else if (!string.IsNullOrEmpty(Warning))
241: {
242: s = Success + ". " + Warning + ". ";
243: }
244: else if (!string.IsNullOrEmpty(Error))
245: {
246: s = Success + ". " + Error + ". ";
247: }
248: else
249: {
250: s = Success + ". ";
251: }
252: }
253: else
254: {
255: if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
256: {
257: s = Warning + ". " + Error + ". ";
258: }
259: else if (!string.IsNullOrEmpty(Warning))
260: {
261: s = Warning + ". ";
262: }
263: else if (!string.IsNullOrEmpty(Error))
264: {
265: s = Error + ". ";
266: }
267: else
268: {
269: s = "";
270: }
271: }
272:
273: CleanUp(ref s);
274:
275: return s;
276: }
277:
278: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
279: private set { }
280: }
281:
282: ////////////////////////////////////////////////////////////////////////////
283:
284: /// <summary>
285: ///
286: /// </summary>
287: [DataMember(Name = "coloredMessage")]
288: public string ColoredMessage
289: {
290: get
291: {
292: string s;
293: StringBuilder stringBuilder;
294:
295: stringBuilder = new StringBuilder();
296:
297: foreach(var v in list.OrderBy(u=>u.DateTime))
298: {
299: if (v.Type == Type.Error) s = @"<span class=""error"">Error: " + v.Text + @"</span><br/>";
300: else if (v.Type == Type.Warning) s = @"<span class=""warning"">Warning: " + v.Text + @"</span><br/>";
301: else if (v.Type == Type.Success) s = @"<span class=""success"">Success: " + v.Text + @"</span><br/>";
302: else s = string.Empty;
303:
304: if(!string.IsNullOrEmpty(s)) stringBuilder.AppendLine(s);
305: }
306:
307: stringBuilder = new StringBuilder(Regex.Replace(stringBuilder.ToString(), @"\.\s+\.", "."));
308:
309: return stringBuilder.ToString();
310: }
311:
312: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
313: private set { }
314: }
315:
316: ////////////////////////////////////////////////////////////////////////////
317:
318: /// <summary>
319: ///
320: /// </summary>
321: [DataMember(Name = "coloredMessageWithoutCaption")]
322: public string ColoredMessageWithoutCaption
323: {
324: get
325: {
326: string s;
327: StringBuilder stringBuilder;
328:
329: stringBuilder = new StringBuilder();
330:
331: foreach (var v in list.OrderBy(u => u.DateTime))
332: {
333: if (v.Type == Type.Error) s = @"<span class=""error"">" + v.Text + @"</span><br/>";
334: else if (v.Type == Type.Warning) s = @"<span class=""warning"">" + v.Text + @"</span><br/>";
335: else if (v.Type == Type.Success) s = @"<span class=""success"">" + v.Text + @"</span><br/>";
336: else s = string.Empty;
337:
338: if (!string.IsNullOrEmpty(s)) stringBuilder.AppendLine(s);
339: }
340:
341: stringBuilder = new StringBuilder(Regex.Replace(stringBuilder.ToString(), @"\.\s+\.", "."));
342:
343: return stringBuilder.ToString();
344: }
345:
346: // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
347: private set { }
348: }
349:
350: ////////////////////////////////////////////////////////////////////////////
351:
352: /// <summary>
353: ///
354: /// </summary>
355: public string Success
356: {
357: get
358: {
359: string s;
360:
361: s = string.Empty;
362:
363: foreach (TextDateTime u in list.Where(u => u.Type == Type.Success)) s += u.Text + " ";
364:
365: if (!string.IsNullOrEmpty(s))
366: {
367: s = s.Remove(s.Length - 1, 1); // remove last ' '
368: //s += ". ";
369: }
370:
371: CleanUp(ref s);
372:
373: return s;
374: }
375: }
376:
377: ////////////////////////////////////////////////////////////////////////////
378:
379: /// <summary>
380: ///
381: /// </summary>
382: public string Warning
383: {
384: get
385: {
386: string s;
387:
388: s = string.Empty;
389:
390: foreach (TextDateTime u in list.Where(u=>u.Type == Type.Warning)) s += u.Text + " ";
391:
392: if (!string.IsNullOrEmpty(s))
393: {
394: s = s.Remove(s.Length - 1, 1); // remove last ' '
395: //s += ". ";
396: }
397:
398: CleanUp(ref s);
399:
400: return s;
401: }
402: }
403:
404: ////////////////////////////////////////////////////////////////////////////
405:
406: /// <summary>
407: ///
408: /// </summary>
409: public string Error
410: {
411: get
412: {
413: string s;
414:
415: s = string.Empty;
416:
417: foreach(TextDateTime u in list.Where(u => u.Type == Type.Error)) s += u.Text + " ";
418:
419: if (!string.IsNullOrEmpty(s))
420: {
421: s = s.Remove(s.Length - 1, 1); // remove last ' '
422: //s += ". ";
423: }
424:
425: CleanUp(ref s);
426:
427: return s;
428: }
429: }
430:
431: ////////////////////////////////////////////////////////////////////////////
432:
433: /// <summary>
434: ///
435: /// </summary>
436: private void CleanUp(ref string line)
437: {
438: line = Regex.Replace(line, @"\.\s+\.", ".");
439: line = line.Replace("?.", "?");
440: line = line.Replace("!.", "!");
441: }
442:
443: ////////////////////////////////////////////////////////////////////////////
444:
445: /// <summary>
446: ///
447: /// </summary>
448: public void AddSuccess(string message)
449: {
450: this.list.Add(new TextDateTime(message, Type.Success, DateTime.UtcNow.AddHours(3)));
451: }
452:
453: ////////////////////////////////////////////////////////////////////////////
454:
455: /// <summary>
456: ///
457: /// </summary>
458: public void AddSuccess(string title, string message)
459: {
460: this.title = title;
461:
462: this.list.Add(new TextDateTime(message, Type.Success, DateTime.UtcNow.AddHours(3)));
463: }
464:
465: ////////////////////////////////////////////////////////////////////////////
466:
467: /// <summary>
468: ///
469: /// </summary>
470: public void AddWarning(string message)
471: {
472: this.list.Add(new TextDateTime(message, Type.Warning, DateTime.UtcNow.AddHours(3)));
473: }
474:
475: ////////////////////////////////////////////////////////////////////////////
476:
477: /// <summary>
478: ///
479: /// </summary>
480: public void AddWarning(string title, string message)
481: {
482: this.title = title;
483:
484: this.list.Add(new TextDateTime(message, Type.Warning, DateTime.UtcNow.AddHours(3)));
485: }
486:
487: ////////////////////////////////////////////////////////////////////////////
488:
489: /// <summary>
490: ///
491: /// </summary>
492: public void AddError(string message)
493: {
494: this.list.Add(new TextDateTime(message, Type.Error, DateTime.UtcNow.AddHours(3)));
495: }
496:
497: ////////////////////////////////////////////////////////////////////////////
498:
499: /// <summary>
500: ///
501: /// </summary>
502: public void AddError(string title, string message)
503: {
504: this.title = title;
505:
506: this.list.Add(new TextDateTime(message, Type.Error, DateTime.UtcNow.AddHours(3)));
507: }
508:
509: ////////////////////////////////////////////////////////////////////////////
510:
511: /// <summary>
512: ///
513: /// </summary>
514: public void AddResult(Result result)
515: {
516: this.list.AddRange(result.list);
517: }
518:
519: ////////////////////////////////////////////////////////////////////////////
520:
521: /// <summary>
522: ///
523: /// </summary>
524: public void AddResult(string title, Result result)
525: {
526: this.title = title;
527:
528: this.list.AddRange(result.list);
529: }
530:
531: ////////////////////////////////////////////////////////////////////////////
532: ////////////////////////////////////////////////////////////////////////////
533: }
534:
535: ////////////////////////////////////////////////////////////////////////////
536: ////////////////////////////////////////////////////////////////////////////
537: }