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:

Koran Reference Network Class Library support functions: Data model

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Data;
   6:  using System.Collections;
   7:  using System.Xml;
   8:  using System.Xml.Linq;
   9:  using System.Text;
  10:  using System.Text.RegularExpressions;
  11:  using System.IO;
  12:  using System.Threading;
  13:  using System.Reflection;
  14:   
  15:  namespace Ia.Islamic.Cl.Model.Data
  16:  {
  17:      ////////////////////////////////////////////////////////////////////////////
  18:   
  19:      /// <summary publish="true">
  20:      /// Koran Reference Network Class Library support functions: Data model
  21:      /// </summary>
  22:      /// <value>
  23:      /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
  24:      /// </value>
  25:      /// <remarks> 
  26:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  27:      ///
  28:      /// 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
  29:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  30:      ///
  31:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  32:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  33:      /// 
  34:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  35:      /// 
  36:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  37:      /// </remarks> 
  38:      public class Default
  39:      {
  40:          private static XDocument faithXDocument;
  41:          private static XDocument kashAlShubuhatFiAlTawheed;
  42:   
  43:          ////////////////////////////////////////////////////////////////////////////
  44:   
  45:          /// <summary>
  46:          ///
  47:          /// </summary>
  48:          public Default() { }
  49:   
  50:          ////////////////////////////////////////////////////////////////////////////
  51:   
  52:          /// <summary>
  53:          ///
  54:          /// </summary>
  55:          public static XDocument KashAlShubuhatFiAlTawheed
  56:          {
  57:              get 
  58:              {
  59:                  kashAlShubuhatFiAlTawheed = Ia.Cl.Model.Xml.Load(@"app_data\\tawheed.xml");
  60:   
  61:                  return kashAlShubuhatFiAlTawheed; 
  62:              }
  63:          }
  64:   
  65:          ////////////////////////////////////////////////////////////////////////////
  66:   
  67:          /// <summary>
  68:          ///
  69:          /// </summary>
  70:          public static string[] UsedKoranLanguageSymbolList
  71:          {
  72:              get
  73:              {
  74:                  string[] p = { "en", "es", "fr", "de", "nl", "ja", "ko", "ar", "zh", "ru" };
  75:   
  76:                  return p;
  77:              }
  78:          }
  79:   
  80:          ////////////////////////////////////////////////////////////////////////////
  81:   
  82:          /// <summary>
  83:          /// 
  84:          /// </summary>
  85:          public static Hashtable FaithHashtable()
  86:          {
  87:              int id;
  88:              string name;
  89:   
  90:              Hashtable ht;
  91:   
  92:              ht = new Hashtable();
  93:   
  94:              foreach (XElement xe in FaithXDocument.Elements("faithTopic").Elements("topic"))
  95:              {
  96:                  id = int.Parse(xe.Attribute("id").Value);
  97:                  name = xe.Attribute("name").Value;
  98:   
  99:                  ht[id] = name;
 100:              }
 101:   
 102:              return ht;
 103:          }
 104:   
 105:          ////////////////////////////////////////////////////////////////////////////
 106:   
 107:          /// <summary>
 108:          /// 
 109:          /// </summary>
 110:          public static void CreateVerseTopic(Ia.Islamic.Cl.Model.VerseTopic verseTopic, string verseId, out int op, out string result)
 111:          {
 112:              op = 0;
 113:              result = "";
 114:   
 115:              using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
 116:              {
 117:                  if (db.VerseTopics.Find(verseTopic.Id) == null)
 118:                  {
 119:                      db.VerseTopics.Add(verseTopic);
 120:   
 121:                      op = 1;
 122:                      result = "Success: record added. ";
 123:   
 124:                      db.SaveChanges();
 125:                  }
 126:                  else
 127:                  {
 128:                      op = -1;
 129:                      result = "Error: record aready exists. ";
 130:                  }
 131:              }
 132:          }
 133:   
 134:          ////////////////////////////////////////////////////////////////////////////
 135:   
 136:          /// <summary>
 137:          /// 
 138:          /// </summary>
 139:          public static List<Ia.Islamic.Cl.Model.Chapter> ChapterListByLanguageSymbol(string languageSymbol, out int op, out string result)
 140:          {
 141:              List<Ia.Islamic.Cl.Model.Chapter> chapterList;
 142:   
 143:              op = 0;
 144:              result = "";
 145:              chapterList = null;
 146:   
 147:              using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
 148:              {
 149:                  chapterList = (from c in db.Chapters where c.Koran.LanguageIso == languageSymbol select c).OrderBy(p => p.Id).ToList();
 150:   
 151:                  if (chapterList.Count() > 0)
 152:                  {
 153:                      op = 1;
 154:                  }
 155:                  else
 156:                  {
 157:                      op = -1;
 158:                      result = "Error: ReturnChapterListUsingLanguage()";
 159:                  }
 160:              }
 161:   
 162:              return chapterList;
 163:          }
 164:   
 165:      ////////////////////////////////////////////////////////////////////////////
 166:   
 167:      /// <summary>
 168:      /// 
 169:      /// </summary>
 170:      public static List<Ia.Islamic.Cl.Model.Ui.VerseUi> VerseListByLanguageSymbolAndChapter(string languageSymbol, int chapterNumber, out int op, out string result)
 171:          {
 172:              List<Ia.Islamic.Cl.Model.Ui.VerseUi> verseList;
 173:   
 174:              op = 0;
 175:              result = "";
 176:              verseList = null;
 177:   
 178:              try
 179:              {
 180:                  using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
 181:                  {
 182:                      verseList = (from q in db.Verses
 183:                                   where q.Chapter.Koran.LanguageIso == languageSymbol && q.Chapter.Number == chapterNumber
 184:                                   select new Ia.Islamic.Cl.Model.Ui.VerseUi
 185:                                   {
 186:                                       Id = q.Id,
 187:                                       Number = q.Number,
 188:                                       ChapterNumber = q.Chapter.Number,
 189:                                       Content = q.Content
 190:                                   }).OrderBy(p => p.Id).ToList();
 191:   
 192:                      if (verseList.Count() > 0)
 193:                      {
 194:                          op = 1;
 195:                      }
 196:                      else
 197:                      {
 198:                          op = -1;
 199:                          result = "Error: ReturnVerseListUsingLanguageSymbolAndChapter()";
 200:                      }
 201:                  }
 202:              }
 203:              catch (Exception ex)
 204:              {
 205:                  op = -1;
 206:                  result = "Exception: " + ex.Message + ". ";
 207:                  verseList = null;
 208:              }
 209:   
 210:              return verseList;
 211:          }
 212:   
 213:          ////////////////////////////////////////////////////////////////////////////
 214:   
 215:          /// <summary>
 216:          /// 
 217:          /// How to embed and access resources by using Visual C# http://support.microsoft.com/kb/319292/en-us
 218:          /// 
 219:          /// 1. Change the "Build Action" property of your XML file from "Content" to "Embedded Resource".
 220:          /// 2. Add "using System.Reflection".
 221:          /// 3. See sample below.
 222:          /// 
 223:          /// </summary>
 224:   
 225:          public static XDocument FaithXDocument
 226:          {
 227:              get
 228:              {
 229:                  Assembly _assembly;
 230:                  StreamReader streamReader;
 231:   
 232:                  faithXDocument = null;
 233:                  _assembly = Assembly.GetExecutingAssembly();
 234:                  streamReader = new StreamReader(_assembly.GetManifestResourceStream("Ia.Islamic.Cl.model.faith-topic.xml"));
 235:   
 236:                  try
 237:                  {
 238:                      if (streamReader.Peek() != -1)
 239:                      {
 240:                          faithXDocument = System.Xml.Linq.XDocument.Load(streamReader);
 241:                      }
 242:                  }
 243:                  catch (Exception)
 244:                  {
 245:                  }
 246:                  finally
 247:                  {
 248:                  }
 249:   
 250:                  return faithXDocument;
 251:              }
 252:          }
 253:   
 254:          ////////////////////////////////////////////////////////////////////////////
 255:          ////////////////////////////////////////////////////////////////////////////
 256:   
 257:          /// <summary>
 258:          /// 
 259:          /// </summary>
 260:          public static List<Ia.Islamic.Cl.Model.Ui.VerseUi> VerseList(Ia.Cl.Model.Language language, Hashtable wordVariants, int maximumLengthOfVerseList, out int op, out string result)
 261:          {
 262:              Hashtable id_ht;
 263:              List<Ia.Islamic.Cl.Model.Ui.VerseUi> verseList;
 264:   
 265:              var wordTest = new string[10];
 266:   
 267:              op = 0;
 268:              result = "";
 269:   
 270:              id_ht = new Hashtable(2 * wordVariants.Count);
 271:   
 272:              if (wordVariants.Count > 0)
 273:              {
 274:                  int i = 0;
 275:                  foreach (string s in wordVariants.Values) wordTest[i++] = s;
 276:   
 277:                  // below: add variants
 278:                  foreach (string u in wordVariants.Keys) id_ht[Ia.Cl.Model.Default.ByteToHex(Encoding.UTF8.GetBytes(language.Id + u))] = 1;
 279:   
 280:                  try
 281:                  {
 282:                      using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
 283:                      {
 284:                          verseList = (from q in db.Verses
 285:                                       from word in db.Words
 286:                                       where /*wordTest.Any(word.Content.Contains)*/  wordTest.Contains(word.Content) //if(stringArray.Any(stringToCheck.Contains))
 287:                                       && word.Verses.Contains(q)
 288:                                       && q.Chapter.Koran.LanguageIso == language.Id
 289:                                       select new Ia.Islamic.Cl.Model.Ui.VerseUi
 290:                                       {
 291:                                           Id = q.Id,
 292:                                           Number = q.Number,
 293:                                           ChapterNumber = q.Chapter.Number,
 294:                                           Content = q.Content
 295:                                       }).OrderBy(p => p.Id).Take(maximumLengthOfVerseList).ToList();
 296:   
 297:                          if (verseList.Count() > 0)
 298:                          {
 299:                          }
 300:                          else
 301:                          {
 302:                              op = -1;
 303:                              result = "Error: ReturnVersesWithAllWordVariantsAndLanguage()";
 304:                          }
 305:   
 306:                          return verseList.ToList();
 307:                      }
 308:                  }
 309:                  catch (Exception ex)
 310:                  {
 311:                      op = -1;
 312:                      result = "Exception: " + ex.Message + ". ";
 313:                  }
 314:              }
 315:              else
 316:              {
 317:                  op = -1;
 318:                  result = "No word variants. ";
 319:              }
 320:   
 321:              return null;
 322:          }
 323:   
 324:          ////////////////////////////////////////////////////////////////////////////
 325:   
 326:          /// <summary>
 327:          /// 
 328:          /// </summary>
 329:          public static Ia.Islamic.Cl.Model.Ui.VerseUi Verse(Ia.Cl.Model.Language language, int chapterNumber, int verseNumber, out int op, out string result)
 330:          {
 331:              op = 0;
 332:              result = "";
 333:              Ia.Islamic.Cl.Model.Ui.VerseUi verse;
 334:   
 335:              try
 336:              {
 337:                  using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
 338:                  {
 339:                      verse = (from q in db.Verses
 340:                               where q.Chapter.Koran.LanguageIso == language.Id
 341:                               && q.Chapter.Number == chapterNumber
 342:                               && q.Number == verseNumber
 343:                               select new Ia.Islamic.Cl.Model.Ui.VerseUi
 344:                               {
 345:                                   Id = q.Id,
 346:                                   Number = q.Number,
 347:                                   ChapterNumber = q.Chapter.Number,
 348:                                   Content = q.Content
 349:                               }).FirstOrDefault();
 350:   
 351:                      if (verse != null)
 352:                      {
 353:                      }
 354:                      else
 355:                      {
 356:                          op = -1;
 357:                          result = "Error: ReturnSingleVerseUsingLanguageAndChapterNumberAndVerseNumber()";
 358:                      }
 359:   
 360:                      return verse;
 361:                  }
 362:              }
 363:              catch (Exception ex)
 364:              {
 365:                  op = -1;
 366:                  result = "Exception: " + ex.Message + ". ";
 367:              }
 368:   
 369:              return null;
 370:          }
 371:   
 372:          ////////////////////////////////////////////////////////////////////////////
 373:          ////////////////////////////////////////////////////////////////////////////
 374:      }
 375:  }