شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة ش.ش.و.

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Result

Public general use code classes and xml files that we've compiled and used over the years:

Result support class.

   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:              list = new List<TextDateTime>();
  86:          }
  87:   
  88:          ////////////////////////////////////////////////////////////////////////////
  89:   
  90:          /// <summary>
  91:          ///
  92:          /// </summary>
  93:          [DataMember(Name = "title")]
  94:          public string Title
  95:          {
  96:              get { return title; }
  97:              set { title = value; }
  98:          }
  99:   
 100:          ////////////////////////////////////////////////////////////////////////////
 101:   
 102:          /// <summary>
 103:          ///
 104:          /// </summary>
 105:          [DataMember(Name = "isSuccessful")]
 106:          public bool IsSuccessful
 107:          {
 108:              get
 109:              {
 110:                  return !list.Any(u => u.Type == Type.Error); 
 111:              }
 112:   
 113:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
 114:              private set { }
 115:          }
 116:   
 117:          ////////////////////////////////////////////////////////////////////////////
 118:   
 119:          /// <summary>
 120:          ///
 121:          /// </summary>
 122:          [DataMember(Name = "hasWarning")]
 123:          public bool HasWarning
 124:          {
 125:              get
 126:              {
 127:                  return list.Any(u => u.Type == Type.Warning);
 128:              }
 129:   
 130:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
 131:              private set { }
 132:          }
 133:   
 134:          ////////////////////////////////////////////////////////////////////////////
 135:   
 136:          /// <summary>
 137:          ///
 138:          /// </summary>
 139:          [DataMember(Name = "hasError")]
 140:          public bool HasError
 141:          {
 142:              get
 143:              {
 144:                  return list.Any(u => u.Type == Type.Error);
 145:              }
 146:   
 147:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
 148:              private set { }
 149:          }
 150:   
 151:          ////////////////////////////////////////////////////////////////////////////
 152:   
 153:          /// <summary>
 154:          ///
 155:          /// </summary>
 156:          [DataMember(Name = "message")]
 157:          public string Message
 158:          {
 159:              get
 160:              {
 161:                  string s;
 162:   
 163:                  s = string.Empty;
 164:   
 165:                  if (!string.IsNullOrEmpty(Success))
 166:                  {
 167:                      if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
 168:                      {
 169:                          s = "Success: " + Success + ". Warning: " + Warning + ". Error: " + Error + ". ";
 170:                      }
 171:                      else if (!string.IsNullOrEmpty(Warning))
 172:                      {
 173:                          s = "Success: " + Success + ". Warning: " + Warning + ". ";
 174:                      }
 175:                      else if (!string.IsNullOrEmpty(Error))
 176:                      {
 177:                          s = "Success: " + Success + ". Error: " + Error + ". ";
 178:                      }
 179:                      else
 180:                      {
 181:                          s = "Success: " + Success + ". ";
 182:                      }
 183:                  }
 184:                  else
 185:                  {
 186:                      if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
 187:                      {
 188:                          s = "Warning: " + Warning + ". Error: " + Error + ". ";
 189:                      }
 190:                      else if (!string.IsNullOrEmpty(Warning))
 191:                      {
 192:                          s = "Warning: " + Warning + ". ";
 193:                      }
 194:                      else if (!string.IsNullOrEmpty(Error))
 195:                      {
 196:                          s = "Error: " + Error + ". ";
 197:                      }
 198:                      else
 199:                      {
 200:                          s = "";
 201:                      }
 202:                  }
 203:   
 204:                  CleanUp(ref s);
 205:   
 206:                  return s;
 207:              }
 208:   
 209:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
 210:              private set { }
 211:          }
 212:   
 213:          ////////////////////////////////////////////////////////////////////////////
 214:   
 215:          /// <summary>
 216:          ///
 217:          /// </summary>
 218:          [DataMember(Name = "coloredMessage")]
 219:          public string ColoredMessage
 220:          {
 221:              get
 222:              {
 223:                  string s;
 224:                  StringBuilder stringBuilder;
 225:   
 226:                  stringBuilder = new StringBuilder();
 227:   
 228:                  foreach(var v in list.OrderBy(u=>u.DateTime))
 229:                  {
 230:                      if (v.Type == Type.Error) s = @"<span class=""error"">Error: " + v.Text + @"</span><br/>";
 231:                      else if (v.Type == Type.Warning) s = @"<span class=""warning"">Warning: " + v.Text + @"</span><br/>";
 232:                      else if (v.Type == Type.Success) s = @"<span class=""success"">Success: " + v.Text + @"</span><br/>";
 233:                      else s = string.Empty;
 234:   
 235:                      if(!string.IsNullOrEmpty(s)) stringBuilder.AppendLine(s);
 236:                  }
 237:   
 238:                  stringBuilder = new StringBuilder(Regex.Replace(stringBuilder.ToString(), @"\.\s+\.", "."));
 239:   
 240:                  return stringBuilder.ToString();
 241:              }
 242:   
 243:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
 244:              private set { }
 245:          }
 246:   
 247:          ////////////////////////////////////////////////////////////////////////////
 248:   
 249:          /// <summary>
 250:          ///
 251:          /// </summary>
 252:          public string Success
 253:          {
 254:              get
 255:              {
 256:                  string s;
 257:   
 258:                  s = string.Empty;
 259:   
 260:                  foreach (TextDateTime u in list.Where(u => u.Type == Type.Success)) s += u.Text + " ";
 261:   
 262:                  if (!string.IsNullOrEmpty(s))
 263:                  {
 264:                      s = s.Remove(s.Length - 1, 1); // remove last ' '
 265:                      //s += ". ";
 266:                  }
 267:   
 268:                  CleanUp(ref s);
 269:   
 270:                  return s;
 271:              }
 272:          }
 273:   
 274:          ////////////////////////////////////////////////////////////////////////////
 275:   
 276:          /// <summary>
 277:          ///
 278:          /// </summary>
 279:          public string Warning
 280:          {
 281:              get
 282:              {
 283:                  string s;
 284:   
 285:                  s = string.Empty;
 286:   
 287:                  foreach (TextDateTime u in list.Where(u=>u.Type == Type.Warning)) s += u.Text + " ";
 288:   
 289:                  if (!string.IsNullOrEmpty(s))
 290:                  {
 291:                      s = s.Remove(s.Length - 1, 1); // remove last ' '
 292:                      //s += ". ";
 293:                  }
 294:   
 295:                  CleanUp(ref s);
 296:   
 297:                  return s;
 298:              }
 299:          }
 300:   
 301:          ////////////////////////////////////////////////////////////////////////////
 302:   
 303:          /// <summary>
 304:          ///
 305:          /// </summary>
 306:          public string Error
 307:          {
 308:              get
 309:              {
 310:                  string s;
 311:   
 312:                  s = string.Empty;
 313:   
 314:                  foreach(TextDateTime u in list.Where(u => u.Type == Type.Error)) s += u.Text + " ";
 315:   
 316:                  if (!string.IsNullOrEmpty(s))
 317:                  {
 318:                      s = s.Remove(s.Length - 1, 1); // remove last ' '
 319:                      //s += ". ";
 320:                  }
 321:   
 322:                  CleanUp(ref s);
 323:   
 324:                  return s;
 325:              }
 326:          }
 327:   
 328:          ////////////////////////////////////////////////////////////////////////////
 329:   
 330:          /// <summary>
 331:          ///
 332:          /// </summary>
 333:          private void CleanUp(ref string line)
 334:          {
 335:              line = Regex.Replace(line, @"\.\s+\.", ".");
 336:          }
 337:   
 338:          ////////////////////////////////////////////////////////////////////////////
 339:   
 340:          /// <summary>
 341:          ///
 342:          /// </summary>
 343:          public void AddSuccess(string message)
 344:          {
 345:              this.list.Add(new TextDateTime(message, Type.Success, DateTime.UtcNow.AddHours(3)));
 346:          }
 347:   
 348:          ////////////////////////////////////////////////////////////////////////////
 349:   
 350:          /// <summary>
 351:          ///
 352:          /// </summary>
 353:          public void AddSuccess(string title, string message)
 354:          {
 355:              this.title = title;
 356:              this.list.Add(new TextDateTime(message, Type.Success, DateTime.UtcNow.AddHours(3)));
 357:          }
 358:   
 359:          ////////////////////////////////////////////////////////////////////////////
 360:   
 361:          /// <summary>
 362:          ///
 363:          /// </summary>
 364:          public void AddWarning(string message)
 365:          {
 366:              this.list.Add(new TextDateTime(message, Type.Warning, DateTime.UtcNow.AddHours(3)));
 367:          }
 368:   
 369:          ////////////////////////////////////////////////////////////////////////////
 370:   
 371:          /// <summary>
 372:          ///
 373:          /// </summary>
 374:          public void AddWarning(string title, string message)
 375:          {
 376:              this.title = title;
 377:              this.list.Add(new TextDateTime(message, Type.Warning, DateTime.UtcNow.AddHours(3)));
 378:          }
 379:   
 380:          ////////////////////////////////////////////////////////////////////////////
 381:   
 382:          /// <summary>
 383:          ///
 384:          /// </summary>
 385:          public void AddError(string message)
 386:          {
 387:              this.list.Add(new TextDateTime(message, Type.Error, DateTime.UtcNow.AddHours(3)));
 388:          }
 389:   
 390:          ////////////////////////////////////////////////////////////////////////////
 391:   
 392:          /// <summary>
 393:          ///
 394:          /// </summary>
 395:          public void AddError(string title, string message)
 396:          {
 397:              this.title = title;
 398:              this.list.Add(new TextDateTime(message, Type.Error, DateTime.UtcNow.AddHours(3)));
 399:          }
 400:   
 401:          ////////////////////////////////////////////////////////////////////////////
 402:   
 403:          /// <summary>
 404:          ///
 405:          /// </summary>
 406:          public void AddResult(Result result)
 407:          {
 408:              this.list.AddRange(result.list);
 409:          }
 410:   
 411:          ////////////////////////////////////////////////////////////////////////////
 412:   
 413:          /// <summary>
 414:          ///
 415:          /// </summary>
 416:          public void AddResult(string title, Result result)
 417:          {
 418:              this.title = title;
 419:              this.list.AddRange(result.list);
 420:          }
 421:   
 422:          ////////////////////////////////////////////////////////////////////////////
 423:          ////////////////////////////////////////////////////////////////////////////
 424:      }
 425:   
 426:      ////////////////////////////////////////////////////////////////////////////
 427:      ////////////////////////////////////////////////////////////////////////////
 428:  }