Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
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.Text;
   5:  using System.Threading.Tasks;
   6:  using System.Runtime.Serialization;
   7:   
   8:  namespace Ia.Cl.Model
   9:  {
  10:      ////////////////////////////////////////////////////////////////////////////
  11:      /// <summary publish="true">
  12:      /// Result support class.
  13:      /// </summary>
  14:      /// <remarks> 
  15:      /// Copyright © 2001-2016 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet 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 List<string> successList;
  32:          private List<string> warningList;
  33:          private List<string> errorList;
  34:   
  35:          ////////////////////////////////////////////////////////////////////////////
  36:   
  37:          /// <summary>
  38:          ///
  39:          /// </summary>
  40:          public Result()
  41:          {
  42:              successList = new List<string>();
  43:              warningList = new List<string>();
  44:              errorList = new List<string>();
  45:          }
  46:   
  47:          ////////////////////////////////////////////////////////////////////////////
  48:   
  49:          /// <summary>
  50:          ///
  51:          /// </summary>
  52:          public Result(string title)
  53:          {
  54:              this.title = title;
  55:              successList = new List<string>();
  56:              warningList = new List<string>();
  57:              errorList = new List<string>();
  58:          }
  59:   
  60:          ////////////////////////////////////////////////////////////////////////////
  61:   
  62:          /// <summary>
  63:          ///
  64:          /// </summary>
  65:          [DataMember(Name = "title")]
  66:          public string Title
  67:          {
  68:              get { return title; }
  69:              set { title = value; }
  70:          }
  71:   
  72:          ////////////////////////////////////////////////////////////////////////////
  73:   
  74:          /// <summary>
  75:          ///
  76:          /// </summary>
  77:          [DataMember(Name = "isSuccessful")]
  78:          public bool IsSuccessful
  79:          {
  80:              get
  81:              {
  82:                  return (this.errorList.Count == 0);
  83:              }
  84:   
  85:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
  86:              private set { }
  87:          }
  88:   
  89:          ////////////////////////////////////////////////////////////////////////////
  90:   
  91:          /// <summary>
  92:          ///
  93:          /// </summary>
  94:          [DataMember(Name = "hasWarning")]
  95:          public bool HasWarning
  96:          {
  97:              get
  98:              {
  99:                  return (this.warningList.Count != 0);
 100:              }
 101:   
 102:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
 103:              private set { }
 104:          }
 105:   
 106:          ////////////////////////////////////////////////////////////////////////////
 107:   
 108:          /// <summary>
 109:          ///
 110:          /// </summary>
 111:          [DataMember(Name = "hasError")]
 112:          public bool HasError
 113:          {
 114:              get
 115:              {
 116:                  return (this.errorList.Count != 0);
 117:              }
 118:   
 119:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
 120:              private set { }
 121:          }
 122:   
 123:          ////////////////////////////////////////////////////////////////////////////
 124:   
 125:          /// <summary>
 126:          ///
 127:          /// </summary>
 128:          [DataMember(Name = "message")]
 129:          public string Message
 130:          {
 131:              get
 132:              {
 133:                  string s, success, warning, error;
 134:   
 135:                  s = string.Empty;
 136:   
 137:                  success = Success;
 138:                  warning = Warning;
 139:                  error = Error;
 140:   
 141:                  if (!string.IsNullOrEmpty(Success))
 142:                  {
 143:                      if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
 144:                      {
 145:                          s = "Success: " + Success + ". Warning: " + Warning + ". Error: " + Error + ". ";
 146:                      }
 147:                      else if (!string.IsNullOrEmpty(Warning))
 148:                      {
 149:                          s = "Success: " + Success + ". Warning: " + Warning + ". ";
 150:                      }
 151:                      else if (!string.IsNullOrEmpty(Error))
 152:                      {
 153:                          s = "Success: " + Success + ". Error: " + Error + ". ";
 154:                      }
 155:                      else
 156:                      {
 157:                          s = "Success: " + Success + ". ";
 158:                      }
 159:                  }
 160:                  else
 161:                  {
 162:                      if (!string.IsNullOrEmpty(Warning) && !string.IsNullOrEmpty(Error))
 163:                      {
 164:                          s = "Warning: " + Warning + ". Error: " + Error + ". ";
 165:                      }
 166:                      else if (!string.IsNullOrEmpty(Warning))
 167:                      {
 168:                          s = "Warning: " + Warning + ". ";
 169:                      }
 170:                      else if (!string.IsNullOrEmpty(Error))
 171:                      {
 172:                          s = "Error: " + Error + ". ";
 173:                      }
 174:                      else
 175:                      {
 176:                          s = "";
 177:                      }
 178:                  }
 179:   
 180:                  CleanUp(ref s);
 181:   
 182:                  return s;
 183:              }
 184:   
 185:              // http://stackoverflow.com/questions/2323277/wcf-chokes-on-properties-with-no-set-any-workaround
 186:              private set { }
 187:          }
 188:   
 189:          ////////////////////////////////////////////////////////////////////////////
 190:   
 191:          /// <summary>
 192:          ///
 193:          /// </summary>
 194:          public string Success
 195:          {
 196:              get
 197:              {
 198:                  string s;
 199:   
 200:                  s = string.Empty;
 201:   
 202:                  foreach (string u in successList) s += u + " ";
 203:   
 204:                  if (!string.IsNullOrEmpty(s))
 205:                  {
 206:                      s = s.Remove(s.Length - 1, 1); // remove last ' '
 207:                      //s += ". ";
 208:                  }
 209:   
 210:                  CleanUp(ref s);
 211:   
 212:                  return s;
 213:              }
 214:          }
 215:   
 216:          ////////////////////////////////////////////////////////////////////////////
 217:   
 218:          /// <summary>
 219:          ///
 220:          /// </summary>
 221:          public string Warning
 222:          {
 223:              get
 224:              {
 225:                  string s;
 226:   
 227:                  s = string.Empty;
 228:   
 229:                  foreach (string u in warningList) s += u + " ";
 230:   
 231:                  if (!string.IsNullOrEmpty(s))
 232:                  {
 233:                      s = s.Remove(s.Length - 1, 1); // remove last ' '
 234:                      //s += ". ";
 235:                  }
 236:   
 237:                  CleanUp(ref s);
 238:   
 239:                  return s;
 240:              }
 241:          }
 242:   
 243:          ////////////////////////////////////////////////////////////////////////////
 244:   
 245:          /// <summary>
 246:          ///
 247:          /// </summary>
 248:          public string Error
 249:          {
 250:              get
 251:              {
 252:                  string s;
 253:   
 254:                  s = string.Empty;
 255:   
 256:                  foreach (string u in errorList) s += u + " ";
 257:   
 258:                  if (!string.IsNullOrEmpty(s))
 259:                  {
 260:                      s = s.Remove(s.Length - 1, 1); // remove last ' '
 261:                      //s += ". ";
 262:                  }
 263:   
 264:                  CleanUp(ref s);
 265:   
 266:                  return s;
 267:              }
 268:          }
 269:   
 270:          ////////////////////////////////////////////////////////////////////////////
 271:   
 272:          /// <summary>
 273:          ///
 274:          /// </summary>
 275:          private void CleanUp(ref string line)
 276:          {
 277:              line = line.Replace("..", ".");
 278:              line = line.Replace(". .", ".");
 279:          }
 280:   
 281:          ////////////////////////////////////////////////////////////////////////////
 282:   
 283:          /// <summary>
 284:          ///
 285:          /// </summary>
 286:          public void AddSuccess(string message)
 287:          {
 288:              this.successList.Add(message);
 289:          }
 290:   
 291:          ////////////////////////////////////////////////////////////////////////////
 292:   
 293:          /// <summary>
 294:          ///
 295:          /// </summary>
 296:          public void AddSuccess(string title, string message)
 297:          {
 298:              this.title = title;
 299:              this.successList.Add(message);
 300:          }
 301:   
 302:          ////////////////////////////////////////////////////////////////////////////
 303:   
 304:          /// <summary>
 305:          ///
 306:          /// </summary>
 307:          public void AddWarning(string message)
 308:          {
 309:              this.warningList.Add(message);
 310:          }
 311:   
 312:          ////////////////////////////////////////////////////////////////////////////
 313:   
 314:          /// <summary>
 315:          ///
 316:          /// </summary>
 317:          public void AddWarning(string title, string message)
 318:          {
 319:              this.title = title;
 320:              this.warningList.Add(message);
 321:          }
 322:   
 323:          ////////////////////////////////////////////////////////////////////////////
 324:   
 325:          /// <summary>
 326:          ///
 327:          /// </summary>
 328:          public void AddError(string message)
 329:          {
 330:              this.errorList.Add(message);
 331:          }
 332:   
 333:          ////////////////////////////////////////////////////////////////////////////
 334:   
 335:          /// <summary>
 336:          ///
 337:          /// </summary>
 338:          public void AddError(string title, string message)
 339:          {
 340:              this.title = title;
 341:              this.errorList.Add(message);
 342:          }
 343:   
 344:          ////////////////////////////////////////////////////////////////////////////
 345:   
 346:          /// <summary>
 347:          ///
 348:          /// </summary>
 349:          public void AddResult(Result result)
 350:          {
 351:              this.errorList.AddRange(result.errorList);
 352:              this.warningList.AddRange(result.warningList);
 353:              this.successList.AddRange(result.successList);
 354:          }
 355:   
 356:          ////////////////////////////////////////////////////////////////////////////
 357:   
 358:          /// <summary>
 359:          ///
 360:          /// </summary>
 361:          public void AddResult(string title, Result result)
 362:          {
 363:              this.title = title;
 364:              this.errorList.AddRange(result.errorList);
 365:              this.warningList.AddRange(result.warningList);
 366:              this.successList.AddRange(result.successList);
 367:          }
 368:   
 369:          ////////////////////////////////////////////////////////////////////////////
 370:          ////////////////////////////////////////////////////////////////////////////
 371:      }
 372:   
 373:      ////////////////////////////////////////////////////////////////////////////
 374:      ////////////////////////////////////////////////////////////////////////////
 375:  }