Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Default

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

Default business support class.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Xml.Linq;
   5:  using System.Web;
   6:  using System.Data;
   7:  using System.Text;
   8:  using System.Xml;
   9:  using System.Text.RegularExpressions;
  10:   
  11:  namespace Ia.Learning.Kanji.Model.Business
  12:  {
  13:      ////////////////////////////////////////////////////////////////////////////
  14:   
  15:      /// <summary publish="true">
  16:      /// Default business support class.
  17:      /// </summary>
  18:      /// <value>
  19:      /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
  20:      /// </value>
  21:      /// <remarks> 
  22:      /// Copyright © 2008-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  23:      ///
  24:      /// 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
  25:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  26:      ///
  27:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  28:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  29:      /// 
  30:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  31:      /// 
  32:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  33:      /// </remarks> 
  34:      public class Default
  35:      {
  36:          ////////////////////////////////////////////////////////////////////////////
  37:   
  38:          /// <summary>
  39:          ///
  40:          /// </summary>
  41:          public Default()
  42:          {
  43:          }
  44:   
  45:          ////////////////////////////////////////////////////////////////////////////
  46:   
  47:          /// <summary>
  48:          ///
  49:          /// </summary>
  50:          public static List<Ia.Learning.Cl.Model.Kanji> Search(string searchText, out int op, out string result)
  51:          {
  52:              int strokeCount;
  53:              string s;
  54:              List<Ia.Learning.Cl.Model.Kanji> kanjiList;
  55:   
  56:              kanjiList = null;
  57:   
  58:              // below: space characters from start and end
  59:              s = Regex.Replace(searchText, @"^\s+", "");
  60:              s = Regex.Replace(s, @"\s+$", "");
  61:              s = s.ToLower();
  62:   
  63:              // below: the regex checks the format of input:
  64:   
  65:              if (s != "")
  66:              {
  67:                  if (Regex.IsMatch(s, @"^\w+$") && !Regex.IsMatch(s, @"\d+"))
  68:                  {
  69:                      if (s.Length > 20)
  70:                      {
  71:                          result = "Input string " + s + " is invalid. ";
  72:                          op = -1;
  73:                      }
  74:                      else
  75:                      {
  76:                          try
  77:                          {
  78:                              // below: if word contains o|u|i we will add another that contains ō|ū|ī
  79:                              // note: no need; SQL LIKE already does this
  80:                              //u = s.Replace("o", "ō");
  81:                              //if(u == s) u = s.Replace("u", "ū");
  82:                              //if(u == s) u = s.Replace("i", "ī");
  83:   
  84:                              kanjiList = (from q in Ia.Learning.Cl.Model.Data.Kanji.List
  85:                                           where q.Name.Contains(s)
  86:                                           || q.English.Contains(s)
  87:                                           || q.JapaneseOnyomi.Contains(s)
  88:                                           || q.EnglishOnyomi.Contains(s)
  89:                                           || q.JapaneseKunyomi.Contains(s)
  90:                                           || q.EnglishKunyomi.Contains(s)
  91:                                           select q).ToList();
  92:   
  93:                              if (kanjiList.Count == 0)
  94:                              {
  95:                                  result = "No kanji were found that contain \"" + s + "\" in their related information. ";
  96:                                  op = -1;
  97:                              }
  98:                              else
  99:                              {
 100:                                  result = "Search found " + kanjiList.Count + " characters. ";
 101:                                  op = 1;
 102:                              }
 103:                          }
 104:                          catch (Exception ex)
 105:                          {
 106:                              result = "Error during retrieval of data for \"" + s + "\": " + ex.Message + ". ";
 107:                              op = -1;
 108:                          }
 109:                      }
 110:                  }
 111:                  else if (Regex.IsMatch(s, @"^\d{1,3}$"))
 112:                  {
 113:                      strokeCount = int.Parse(s);
 114:   
 115:                      if (strokeCount == 0 || strokeCount > 50)
 116:                      {
 117:                          result = "Stroke number " + strokeCount + " is invalid. ";
 118:                          op = -1;
 119:                      }
 120:                      else
 121:                      {
 122:                          try
 123:                          {
 124:                              kanjiList = (from k in Ia.Learning.Cl.Model.Data.Kanji.List where k.Stroke == strokeCount select k).ToList();
 125:   
 126:                              if (kanjiList.Count == 0)
 127:                              {
 128:                                  result = "No kanji were found that require " + strokeCount + " strokes. ";
 129:                                  op = -1;
 130:                              }
 131:                              else
 132:                              {
 133:                                  result = "Search found " + kanjiList.Count + " characters. ";
 134:                                  op = 1;
 135:                              }
 136:                          }
 137:                          catch (Exception ex)
 138:                          {
 139:                              result = "Error during retrieval of data for \"" + strokeCount + "\": " + ex.Message + ". ";
 140:                              op = -1;
 141:                          }
 142:                      }
 143:                  }
 144:                  else
 145:                  {
 146:                      result = "The input string \"" + s + "\" is not in a correct format. ";
 147:                      op = -1;
 148:                  }
 149:              }
 150:              else
 151:              {
 152:                  result = "No input was entered. ";
 153:                  op = -1;
 154:              }
 155:   
 156:              return kanjiList;
 157:          }
 158:   
 159:          ////////////////////////////////////////////////////////////////////////////
 160:          ////////////////////////////////////////////////////////////////////////////
 161:      }
 162:  }