)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Default (Ia.Islamic.Koran.Wfa.Model.Business)

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

Koran Reference Network Windows Form support functions: Business model

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Linq;
    4: using System.Xml.Linq;
    5: using System.Text;
    6: using System.Text.RegularExpressions;
    7: using System.Data;
    8: using Microsoft.EntityFrameworkCore;
    9: using System.Linq.Expressions;
   10:  
   11: namespace Ia.Islamic.Koran.Wfa.Model.Business
   12: {
   13:     ////////////////////////////////////////////////////////////////////////////
   14:  
   15:     /// <summary publish="true">
   16:     /// Koran Reference Network Windows Form support functions: Business model
   17:     /// </summary>
   18:     /// <value>
   19:     /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
   20:     /// </value>
   21:     /// <remarks> 
   22:     /// Copyright © 1995-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated 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:         private static readonly string websiteFilePath = @"C:\Users\Jasem\Documents\Visual Studio 2022\Projects\Islamic\krn.org\";
   37:  
   38:         ////////////////////////////////////////////////////////////////////////////
   39:  
   40:         /// <summary>
   41:         ///
   42:         /// </summary>
   43:         public Default()
   44:         {
   45:         }
   46:  
   47:         ////////////////////////////////////////////////////////////////////////////
   48:  
   49:         /// <summary>
   50:         ///
   51:         /// </summary>
   52:         public static void BuildLanguageComboBox(ref System.Windows.Forms.ComboBox cb)
   53:         {
   54:             cb.Items.Clear();
   55:  
   56:             var list = (from l in Ia.Cl.Models.Language.List
   57:                         where Ia.Islamic.Cl.Model.Data.Default.UsedKoranLanguageSymbolList.Contains(l.Iso6391)
   58:                         orderby l.Name
   59:                         select new
   60:                         {
   61:                             Symbol = l.Iso6391,
   62:                             l.Name
   63:                         }).ToList();
   64:  
   65:             cb.DataSource = list;
   66:             cb.DisplayMember = "Name";
   67:             cb.ValueMember = "Symbol";
   68:  
   69:             cb.SelectedIndex = 0;
   70:         }
   71:  
   72:         ////////////////////////////////////////////////////////////////////////////
   73:  
   74:         /// <summary>
   75:         /// 
   76:         /// </summary>
   77:         public static void CreateDatabaseTableRecords(Ia.Cl.Models.Language language, out Ia.Cl.Models.Result result)
   78:         {
   79:             int chapterNumber, verseNumber;
   80:             string name, arabicName, revealed, introduction;
   81:             string regularExpression, word0, content;
   82:             MatchCollection matchCollection;
   83:             Ia.Islamic.Cl.Model.Koran koran;
   84:             Ia.Islamic.Cl.Model.Chapter chapter;
   85:             Ia.Islamic.Cl.Model.Verse verse;
   86:             Ia.Islamic.Cl.Model.Word word;
   87:             Ia.Islamic.Cl.Model.WordVerse wordVerse;
   88:             List<Ia.Islamic.Cl.Model.Word> words;
   89:             List<Ia.Islamic.Cl.Model.WordVerse> wordVerses;
   90:  
   91:             arabicName = revealed = string.Empty;
   92:  
   93:             result = new Ia.Cl.Models.Result();
   94:  
   95:             var koranXDocument = Ia.Islamic.Koran.Wfa.Model.Data.Default.ReturnKoranByLanguage(language);
   96:  
   97:             var wordToVerseIdDictionary = new Dictionary<string, string>(10000); // assuming max num of words and derivatives in any one version of koran
   98:             var verseIdToVerseDictionary = new Dictionary<string, Ia.Islamic.Cl.Model.Verse>(10000);
   99:  
  100:             using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
  101:             {
  102:                 db.Database.EnsureCreated();
  103:  
  104:                 db.Database.SetCommandTimeout(120); // process is complex, so I needed to increase timeout from 30 to 120
  105:  
  106:                 db.Database.ExecuteSqlRaw("delete from Words where Id like '" + Ia.Islamic.Cl.Model.Business.Word.WordId(language.Id, "") + "%'");
  107:  
  108:                 db.Database.ExecuteSqlRaw("delete from WordVerses where VerseId like '" + language.Id + "%'");
  109:                 db.Database.ExecuteSqlRaw("delete from WordVerses where WordId like '" + language.Id + "%'");
  110:  
  111:                 db.Database.ExecuteSqlRaw("delete from Verses where Id like '" + language.Id + "%'");
  112:                 db.Database.ExecuteSqlRaw("delete from Chapters where Id like '" + language.Id + "%'");
  113:                 db.Database.ExecuteSqlRaw("delete from Korans where Id like '" + language.Id + "%'");
  114:  
  115:                 if (language.Id == "ja" || language.Id == "ko" || language.Id == "zh")
  116:                 {
  117:                     regularExpression = Ia.Cl.Models.Language.BasicWordsRegularExpression(language.Symbol) + "|[" + Ia.Cl.Models.Language.Ideograph(language.Symbol) + "]{1}";
  118:                     regularExpression = regularExpression.Replace("|[]{1}", "");
  119:                 }
  120:                 else regularExpression = Ia.Cl.Models.Language.BasicWordsRegularExpression(language.Symbol);
  121:  
  122:                 /*
  123:                 // below: words regular
  124:                 mc = Regex.Matches(content, @"(" + s + ")", RegexOptions.Compiled);
  125:                 foreach (Match m in mc)
  126:                 {
  127:                     word0 = m.Groups[1].Captures[0].Value;
  128:  
  129:                     if (ht[word0] == null) ht[word0] = verse.Id + " ";
  130:                     else if (ht[word0].ToString().IndexOf(verse.Id) == -1) ht[word0] = ht[word0].ToString() + verse.Id + " ";
  131:                     // above: exclude duplicate verse location values
  132:                 }
  133:                 */
  134:  
  135:                 /*
  136:                 mc = Regex.Matches(Ia.Cl.Models.Language.RemoveAccent(content), @"(" + s + ")", RegexOptions.Compiled);
  137:                 foreach (Match m in mc)
  138:                 {
  139:                     word0 = m.Groups[1].Captures[0].Value;
  140:  
  141:                     if (ht[word0] == null) ht[word0] = verse.Id + " ";
  142:                     else if (ht[word0].ToString().IndexOf(verse.Id) == -1) ht[word0] = ht[word0].ToString() + verse.Id + " ";
  143:                 }
  144:                 */
  145:  
  146:                 koran = new Ia.Islamic.Cl.Model.Koran
  147:                 {
  148:                     Id = language.Id,
  149:                     Name = koranXDocument.Descendants("قرءان").Attributes("name").First().Value,
  150:                     Introduction = string.Empty,
  151:                     LanguageIso = language.Id
  152:                 };
  153:  
  154:                 db.Korans.Add(koran);
  155:  
  156:                 chapterNumber = 1;
  157:  
  158:                 foreach (XElement xe in koranXDocument.Descendants("قرءان").Descendants("سورة"))
  159:                 {
  160:                     if (chapterNumber == int.Parse(xe.Attributes("id").First().Value))
  161:                     {
  162:                         name = xe.Attributes("name").First().Value;
  163:  
  164:                         if (language.Symbol != "ar")
  165:                         {
  166:                             arabicName = xe.Attributes("arabicName").First().Value;
  167:                             revealed = xe.Attributes("revealed").First().Value;
  168:                         }
  169:  
  170:                         introduction = xe.Descendants("مقدمة").FirstOrDefault().Value;
  171:  
  172:                         chapter = new Ia.Islamic.Cl.Model.Chapter
  173:                         {
  174:                             Id = language.Id + xe.Attributes("id").First().Value.PadLeft(3, '0'),
  175:                             Number = chapterNumber,
  176:                             Name = name,
  177:                             Koran = (from q in db.Korans where q.Id == koran.Id select q).SingleOrDefault()
  178:                         };
  179:  
  180:                         // below: handle revealed according to language
  181:                         if (revealed == "before") chapter.Revealed = false;
  182:                         else chapter.Revealed = true;
  183:  
  184:                         if (language.Symbol != "ar") chapter.ArabicName = arabicName;
  185:  
  186:                         chapter.Introduction = introduction;
  187:  
  188:                         koran.Chapters.Add(chapter);
  189:  
  190:                         verseNumber = 1;
  191:  
  192:                         foreach (XElement xElement in xe.Descendants("ءاية"))
  193:                         {
  194:                             if (verseNumber == int.Parse(xElement.Attributes("id").First().Value))
  195:                             {
  196:                                 content = xElement.Value;
  197:  
  198:                                 verse = new Ia.Islamic.Cl.Model.Verse
  199:                                 {
  200:                                     Id = chapter.Id + xElement.Attributes("id").First().Value.PadLeft(3, '0'),
  201:                                     Chapter = chapter,
  202:                                     Number = verseNumber,
  203:                                     Content = content
  204:                                 };
  205:  
  206:                                 verseIdToVerseDictionary[verse.Id] = verse;
  207:  
  208:                                 // below: basic form lower case, no accents
  209:                                 matchCollection = Regex.Matches(Ia.Cl.Models.Language.BasicForm(content), @"(" + regularExpression + ")", RegexOptions.Compiled);
  210:  
  211:                                 foreach (Match match in matchCollection)
  212:                                 {
  213:                                     word0 = match.Groups[1].Captures[0].Value;
  214:  
  215:                                     if (!wordToVerseIdDictionary.ContainsKey(word0)) wordToVerseIdDictionary[word0] = verse.Id + " ";
  216:                                     else if (wordToVerseIdDictionary[word0].IndexOf(verse.Id) == -1) wordToVerseIdDictionary[word0] = wordToVerseIdDictionary[word0].ToString() + verse.Id + " ";
  217:                                     // above: this skips adding word if it already exists in this same verseId
  218:                                 }
  219:  
  220:                                 chapter.Verses.Add(verse);
  221:  
  222:                                 verseNumber++;
  223:                             }
  224:                             else
  225:                             {
  226:                                 throw new ArgumentException("Inconsistant verse counter verse id=" + xElement.Attributes("id").First().Value + ", chapter id=" + xe.Attributes("id").First().Value);
  227:                             }
  228:                         }
  229:  
  230:                         chapterNumber++;
  231:                     }
  232:                     else
  233:                     {
  234:                         throw new ArgumentException("Inconsistant chapter counter chapter id=" + xe.Attributes("id").First().Value);
  235:                     }
  236:                 }
  237:  
  238:                 words = new List<Ia.Islamic.Cl.Model.Word>();
  239:                 wordVerses = new List<Ia.Islamic.Cl.Model.WordVerse>();
  240:  
  241:                 // below: populate the word table
  242:                 foreach (string content2 in wordToVerseIdDictionary.Keys)
  243:                 {
  244:                     word = new Ia.Islamic.Cl.Model.Word
  245:                     {
  246:                         Id = Ia.Islamic.Cl.Model.Business.Word.WordId(language.Id, content2),
  247:                         Content = content2
  248:                     };
  249:  
  250:                     // below: remove last " "
  251:                     regularExpression = wordToVerseIdDictionary[content2].ToString().Trim();
  252:  
  253:                     //foreach (string t in regularExpression.Split(' ')) word.Verses.Add((Ia.Islamic.Cl.Model.Verse)verseHashtable[t]);
  254:                     foreach (string verseId in regularExpression.Split(' '))
  255:                     {
  256:                         wordVerse = new Ia.Islamic.Cl.Model.WordVerse
  257:                         {
  258:                             WordId = word.Id,
  259:                             Word = word,
  260:                             VerseId = verseId,
  261:                             Verse = verseIdToVerseDictionary[verseId]
  262:                         };
  263:  
  264:                         wordVerses.Add(wordVerse);
  265:                     }
  266:  
  267:                     words.Add(word);
  268:                 }
  269:  
  270:                 db.Korans.Add(koran);
  271:                 db.Words.AddRange(words);
  272:                 db.WordVerses.AddRange(wordVerses);
  273:  
  274:                 db.SaveChanges();
  275:             }
  276:         }
  277:  
  278:         ////////////////////////////////////////////////////////////////////////////
  279:  
  280:         /// <summary>
  281:         ///
  282:         /// </summary>
  283:         public static void DeleteDatabase(out Ia.Cl.Models.Result result)
  284:         {
  285:             bool databaseDeleted;
  286:  
  287:             result = new Ia.Cl.Models.Result();
  288:  
  289:             try
  290:             {
  291:                 using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
  292:                 {
  293:                     databaseDeleted = db.Database.EnsureDeleted();
  294:  
  295:                     if (databaseDeleted) result.AddSuccess("Database deleted");
  296:                     else result.AddError("Database delete error");
  297:                 }
  298:             }
  299:             catch (Exception ex)
  300:             {
  301:                 result.AddError("Database delete error. Exception: " + ex.ToString());
  302:             }
  303:         }
  304:  
  305:         ////////////////////////////////////////////////////////////////////////////
  306:  
  307:         /// <summary>
  308:         ///
  309:         /// </summary>
  310:         public static void WriteChapterFiles(Ia.Cl.Models.Language language, out Ia.Cl.Models.Result result)
  311:         {
  312:             bool b;
  313:             string className, fileName, u, content;
  314:  
  315:             result = new Ia.Cl.Models.Result();
  316:             var sb = new StringBuilder();
  317:  
  318:             var translation = new Ia.Islamic.Cl.Model.Translation(language.Id);
  319:             u = Ia.Cl.Models.Default.FirstLetterToUpper(language.Symbol);
  320:  
  321:             using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
  322:             {
  323:                 // db.Database.Connection.Open();
  324:  
  325:                 var chapters = (from q in db.Chapters where q.Koran.LanguageIso == language.Id select q).ToList<Ia.Islamic.Cl.Model.Chapter>();
  326:  
  327:                 if (chapters.Count() > 0)
  328:                 {
  329:                     foreach (Ia.Islamic.Cl.Model.Chapter chapter in chapters)
  330:                     {
  331:                         sb.Length = 0;
  332:  
  333:                         className = u + "_" + chapter.Number;
  334:  
  335:                         fileName = chapter.Number + ".aspx.cs";
  336:                         sb.Append(ChapterFileHeader(className, fileName, translation.MainHome, translation.MainTitle, translation.MainBismillah, language, chapter.Id, chapter.Number, chapter.Name, chapter.ArabicName, chapter.Revealed, chapter.Introduction));
  337:  
  338:                         // below: handle revealed according to language
  339:                         //if (revealed == "before") revealed = "1";
  340:                         //else if (revealed == "after") revealed = "2";
  341:                         //else revealed = "0";
  342:  
  343:                         var verses = (from q in db.Verses where q.Chapter.Koran.LanguageIso == language.Id && q.Chapter.Id == chapter.Id select q).ToList<Ia.Islamic.Cl.Model.Verse>();
  344:  
  345:                         foreach (Ia.Islamic.Cl.Model.Verse verse in verses)
  346:                         {
  347:                             content = verse.Content;
  348:  
  349:                             sb.Append(ChapterFileVerse(language, verse.Number, content));
  350:  
  351:                             // below: if a حزب, ربع, نصف, ثلاث occures here will will add a space
  352:                             // جزء/حزب/ربع/نصف/ثلاث/
  353:  
  354:                             /*
  355:                             if (verseString != "1")
  356:                             {
  357:                                 //xd = jza_xd.SelectSingleNode("جزء/جزء/descendant::*[@سورة='" + chapter + "' and @ءاية='" + verse + "']");
  358:  
  359:                                 verseStartsHizb = data.Jza.Elements("جزء").Elements("جزء").DescendantsAndSelf().Where(x => x.HasAttributes && x.Attribute("سورة").Value == chapter && x.Attribute("ءاية").Value == verseString).Any();
  360:  
  361:                                 if (verseStartsHizb) sb.Append(ChapterFileSpace(language));
  362:                             }
  363:                              */
  364:  
  365:                             //dr["id"] = long.Parse(ch_id.ToString().PadLeft(3, '0') + v_id.ToString().PadLeft(3, '0'));
  366:                         }
  367:  
  368:                         sb.Append(ChapterFileFooter(language));
  369:  
  370:                         // below: write to webpage file
  371:  
  372:                         fileName = websiteFilePath + language.Symbol + @"\" + chapter.Number + ".aspx";
  373:  
  374:                         b = Ia.Cl.Models.File.Write(fileName, sb.ToString());
  375:  
  376:                         if (!b) { result.AddError("WriteChapterFiles: Can't write file " + fileName); break; }
  377:  
  378:                         // below: write *.aspx.cs file in webpage
  379:                         sb = new StringBuilder(100000)
  380:                         {
  381:                             Length = 0
  382:                         };
  383:  
  384:                         sb.Append(ChapterFileCs(language, className));
  385:                         fileName = websiteFilePath + language.Symbol + @"\" + chapter.Number + ".aspx.cs";
  386:  
  387:                         b = Ia.Cl.Models.File.Write(fileName, sb.ToString());
  388:  
  389:                         if (!b) { result.AddError("WriteChapterFiles: Can't write file " + fileName); break; }
  390:                     }
  391:                 }
  392:                 else
  393:                 {
  394:                     result.AddError("No chapter files read from database");
  395:                 }
  396:             }
  397:         }
  398:  
  399:         ////////////////////////////////////////////////////////////////////////////
  400:  
  401:         /// <summary>
  402:         ///
  403:         /// </summary>
  404:         public static string ChapterFileHeader(string className, string fileName, string home, string koran, string bismilla, Ia.Cl.Models.Language language, string ch_id, int chapter, string name, string arabicName, bool revealed, string intro)
  405:         {
  406:             string s, title;
  407:  
  408:             title = "";
  409:  
  410:             if (language.Id == "ar") title = name;
  411:             else
  412:             {
  413:                 if (name.Length > 0 && arabicName.Length > 0) title = name + " (" + arabicName + ")";
  414:                 else if (name.Length > 0) title = name;
  415:                 else if (arabicName.Length > 0) title = arabicName;
  416:             }
  417:  
  418:             s = @"<%@ Page Language=""C#"" AutoEventWireup=""true"" Inherits=""" + className + @""" MasterPageFile=""~/1_" + language.Symbol + @".master"" Codebehind=""" + fileName + @""" %>
  419: <asp:Content ContentPlaceHolderID=""contentContentPlaceHolder"" runat=""server"">
  420:  
  421: <asp:HyperLink NavigateUrl=""~/default.aspx"" Text=""" + home + @""" runat=""server""/> » <asp:HyperLink NavigateUrl=""~/" + language.Symbol + @"/default.aspx"" Text=""" + koran + @""" runat=""server""/> » " + title;
  422:  
  423:             if (intro.Length > 0) s += @"<div id=""intro"">" + intro + @"</div>";
  424:  
  425:             s += @"<div id=""chapter"">";
  426:  
  427:             if (chapter != 1 && chapter != 9)
  428:             {
  429:                 s += @"<span class=""bismilla"">" + bismilla + @"</span>&nbsp;&nbsp;";
  430:             }
  431:  
  432:             // <p><a name=""1"">1</a>. In the name of Allah, Most Gracious, Most Merciful.</p>";
  433:  
  434:             return s;
  435:         }
  436:  
  437:         ////////////////////////////////////////////////////////////////////////////
  438:  
  439:         /// <summary>
  440:         ///
  441:         /// </summary>
  442:         public static string ChapterFileVerse(Ia.Cl.Models.Language language, int verse, string content)
  443:         {
  444:             string s;
  445:  
  446:             // below: I will keep no space between verse and number
  447:             if (language.Symbol == "ja") s = @"<div class=""verse"">" + content + @"<span class=""number""><a id=""" + verse + @""">" + verse + @"</a></span></div>&nbsp;&nbsp;&nbsp;";
  448:             else s = @"<div class=""verse"">" + content + @" <span class=""number""><a id=""" + verse + @""">" + verse + @"</a></span></div>&nbsp;&nbsp;";
  449:  
  450:             return s;
  451:         }
  452:  
  453:         ////////////////////////////////////////////////////////////////////////////
  454:  
  455:         /// <summary>
  456:         ///
  457:         /// </summary>
  458:         public string ChapterFileSpace(Ia.Cl.Models.Language language)
  459:         {
  460:             string s;
  461:  
  462:             s = @"<p><br/></p>";
  463:  
  464:             return s;
  465:         }
  466:  
  467:         ////////////////////////////////////////////////////////////////////////////
  468:  
  469:         /// <summary>
  470:         ///
  471:         /// </summary>
  472:         public static string ChapterFileFooter(Ia.Cl.Models.Language language)
  473:         {
  474:             string s;
  475:  
  476:             s = @"</div>
  477: </asp:Content>";
  478:  
  479:             return s;
  480:         }
  481:  
  482:         ////////////////////////////////////////////////////////////////////////////
  483:  
  484:         /// <summary>
  485:         ///
  486:         /// </summary>
  487:         public static string ChapterFileCs(Ia.Cl.Models.Language language, string className)
  488:         {
  489:             string s;
  490:  
  491:             s = @"using System;
  492: using System.Web;
  493:  
  494: ////////////////////////////////////////////////////////////////////////////
  495:  
  496: /// <summary>
  497: ///
  498: /// </summary>
  499: public partial class " + className + @" : System.Web.UI.Page
  500: {
  501:     ////////////////////////////////////////////////////////////////////////////
  502:  
  503:     /// <summary>
  504:     ///
  505:     /// </summary>
  506:     void Page_PreInit(object sender, System.EventArgs e)
  507:     {
  508:         Ia.Islamic.Koran.Wa.Model.Ui.Default.Theme(this, HttpContext.Current.Profile);
  509:     }
  510:  
  511:     ////////////////////////////////////////////////////////////////////////////
  512:  
  513:     /// <summary>
  514:     ///
  515:     /// </summary>
  516:     protected void Page_Load(object sender, EventArgs e)
  517:     {
  518:     }
  519:  
  520:     ////////////////////////////////////////////////////////////////////////////
  521:     ////////////////////////////////////////////////////////////////////////////
  522: }
  523: ";
  524:  
  525:             return s;
  526:         }
  527:  
  528:         ////////////////////////////////////////////////////////////////////////////
  529:         ////////////////////////////////////////////////////////////////////////////
  530:  
  531:  
  532:  
  533:  
  534:  
  535:         ////////////////////////////////////////////////////////////////////////////
  536:         ////////////////////////////////////////////////////////////////////////////
  537:  
  538:         /// <summary>
  539:         ///
  540:         /// </summary>
  541:         public static void WriteDefaultAndSearchFiles(Ia.Cl.Models.Language language, out Ia.Cl.Models.Result result)
  542:         {
  543:             bool b;
  544:             string u, fileName;
  545:             StringBuilder sb;
  546:  
  547:             result = new Ia.Cl.Models.Result();
  548:             sb = new StringBuilder(100000);
  549:  
  550:             Ia.Islamic.Cl.Model.Translation translation = new Ia.Islamic.Cl.Model.Translation(language.Id);
  551:  
  552:             u = Ia.Cl.Models.Default.FirstLetterToUpper(language.Symbol);
  553:  
  554:             /*
  555:             - "chapter": (id,chapter,language_id,name,arabicName,revealed_id,introduction)
  556:             */
  557:  
  558:             // below: write default.aspx file in webpage
  559:             sb = new StringBuilder(100000);
  560:             sb.Append(BuildDefaultFile(language, translation, "" + u + "_Default"));
  561:             fileName = websiteFilePath + language.Symbol + @"\default.aspx";
  562:             b = Ia.Cl.Models.File.Write(fileName, sb.ToString());
  563:             if (!b) result.AddError("WriteDefaultAndSearchFiles: Can't write file " + fileName);
  564:  
  565:             // below: write default.aspx.cs file in webpage
  566:             sb = new StringBuilder(100000);
  567:             sb.Append(DefaultFileCs(language, translation, "" + u + "_Default"));
  568:             fileName = websiteFilePath + language.Symbol + @"\default.aspx.cs";
  569:             b = Ia.Cl.Models.File.Write(fileName, sb.ToString());
  570:             if (!b) result.AddError("WriteDefaultAndSearchFiles: Can't write file " + fileName);
  571:  
  572:             // below: write search.aspx file in webpage
  573:             sb = new StringBuilder(100000);
  574:             sb.Append(SearchFile(language, translation, "" + u + "_Search"));
  575:             fileName = websiteFilePath + language.Symbol + @"\search.aspx";
  576:             b = Ia.Cl.Models.File.Write(fileName, sb.ToString());
  577:             if (!b) result.AddError("WriteDefaultAndSearchFiles: Can't write file " + fileName);
  578:  
  579:             // below: write search.aspx.cs file in webpage
  580:             sb = new StringBuilder(100000);
  581:             sb.Append(SearchFileCs(language, translation, "" + u + "_Search"));
  582:             fileName = websiteFilePath + language.Symbol + @"\search.aspx.cs";
  583:             b = Ia.Cl.Models.File.Write(fileName, sb.ToString());
  584:             if (!b) result.AddError("WriteDefaultAndSearchFiles: Can't write file " + fileName);
  585:         }
  586:  
  587:         ////////////////////////////////////////////////////////////////////////////
  588:  
  589:         /// <summary>
  590:         ///
  591:         /// </summary>
  592:         public static string DefaultFileHeader(Ia.Cl.Models.Language language, Ia.Islamic.Cl.Model.Translation translation, string className)
  593:         {
  594:             string s;
  595:  
  596:             s = @"<%@ Page Language=""C#"" AutoEventWireup=""True"" Inherits=""" + className + @""" MasterPageFile=""~/1_" + language.Symbol + @".master"" CodeBehind=""default.aspx.cs"" %>
  597:  
  598: <asp:Content ContentPlaceHolderID=""contentContentPlaceHolder"" runat=""server"">
  599:  
  600: <asp:HyperLink NavigateUrl=""~/default.aspx"" Text=""" + translation.MainHome + @""" runat=""server""/> » " + translation.MainTitle + @"
  601:  
  602: <p id=""enter-words"">" + translation.SearchInstruction + @"</p>
  603:  
  604: <p id=""text-box""><asp:TextBox id=""tb"" Maxlength=""32"" Columns=""16"" runat=""server""/>&nbsp;<asp:Button Text="" " + translation.SearchButton + @" "" runat=""server"" /></p>
  605:  
  606: <p id=""hint""><span id=""head"">" + translation.SearchHintName + @"</span>: " + translation.SearchHint + @"</p>
  607:  
  608: <p id=""search-result""><asp:Label id=""searchResultLabel"" EnableViewState=""false"" runat=""server""/></p>
  609:  
  610: <ol>
  611: ";
  612:  
  613:             return s;
  614:         }
  615:  
  616:         ////////////////////////////////////////////////////////////////////////////
  617:  
  618:         /// <summary>
  619:         ///
  620:         /// </summary>
  621:         public static string DefaultAndSearchFileChapterTitle(Ia.Cl.Models.Language language, Ia.Islamic.Cl.Model.Translation translation, Ia.Islamic.Cl.Model.Chapter chapter)
  622:         {
  623:             string s;
  624:  
  625:             if (language.Id == "ar")
  626:             {
  627:                 s = @"<li><asp:HyperLink NavigateUrl=""~/" + language.Symbol + "/" + chapter.Number + @".aspx"" Text=""" + chapter.Name + @""" runat=""server""/></li>" + "\r\n";
  628:  
  629:                 //if (language.Symbol == "ar" && chapter % 38 == 0) s += @"</td><td style=""vertical-align:top;"">";
  630:                 //else if (language != "ar" && n % 57 == 0) s += @"</td><td style=""vertical-align:top;"">";
  631:             }
  632:             else
  633:             {
  634:                 if (chapter.Name.Length > 0 && chapter.ArabicName.Length > 0) s = @"<li><asp:HyperLink NavigateUrl=""~/" + language.Symbol + "/" + chapter.Number + @".aspx"" Text=""" + chapter.Name + @""" runat=""server""/> (<asp:HyperLink NavigateUrl=""~/" + language.Symbol + "/" + chapter.Number + @".aspx"" Text=""" + chapter.ArabicName + @""" runat=""server""/>)</li>" + "\r\n";
  635:                 else if (chapter.Name.Length > 0) s = @"<li><asp:HyperLink NavigateUrl=""~/" + language.Symbol + "/" + chapter.Number + @".aspx"" Text=""" + chapter.Name + @""" runat=""server""/></li>" + "\r\n";
  636:                 else /*if (arabicName.Length > 0)*/ s = @"<li><asp:HyperLink NavigateUrl=""~/" + language.Symbol + "/" + chapter.Number + @".aspx"" Text=""" + chapter.ArabicName + @""" runat=""server""/></li>" + "\r\n";
  637:             }
  638:  
  639:             return s;
  640:         }
  641:  
  642:         ////////////////////////////////////////////////////////////////////////////
  643:  
  644:         /// <summary>
  645:         ///
  646:         /// </summary>
  647:         public static string DefaultFileFooter(Ia.Cl.Models.Language language, Ia.Islamic.Cl.Model.Translation translation)
  648:         {
  649:             string s;
  650:  
  651:             s = @"
  652: </ol>
  653: </asp:Content>";
  654:  
  655:             return s;
  656:         }
  657:  
  658:         ////////////////////////////////////////////////////////////////////////////
  659:  
  660:         /// <summary>
  661:         ///
  662:         /// </summary>
  663:         public static string BuildDefaultFile(Ia.Cl.Models.Language language, Ia.Islamic.Cl.Model.Translation translation, string className)
  664:         {
  665:             StringBuilder sb;
  666:             List<Ia.Islamic.Cl.Model.Chapter> chapters;
  667:  
  668:             sb = new StringBuilder(100000);
  669:             sb.Length = 0;
  670:  
  671:             sb.Append(DefaultFileHeader(language, translation, className));
  672:  
  673:             using (var db = new Ia.Islamic.Cl.Model.Context.Koran())
  674:             {
  675:                 chapters = (from q in db.Chapters where q.Koran.LanguageIso == language.Id select q).ToList<Ia.Islamic.Cl.Model.Chapter>();
  676:  
  677:                 foreach (Ia.Islamic.Cl.Model.Chapter chapter in chapters)
  678:                 {
  679:                     sb.Append(DefaultAndSearchFileChapterTitle(language, translation, chapter));
  680:                 }
  681:             }
  682:  
  683:             sb.Append(DefaultFileFooter(language, translation));
  684:  
  685:             return sb.ToString();
  686:         }
  687:  
  688:         ////////////////////////////////////////////////////////////////////////////
  689:  
  690:         /// <summary>
  691:         ///
  692:         /// </summary>
  693:         public static string DefaultFileCs(Ia.Cl.Models.Language language, Ia.Islamic.Cl.Model.Translation translation, string className)
  694:         {
  695:             string s;
  696:  
  697:             s = @"using System;
  698: using System.Web;
  699:  
  700: ////////////////////////////////////////////////////////////////////////////
  701:  
  702: /// <summary>
  703: ///
  704: /// </summary>
  705: public partial class " + className + @" : System.Web.UI.Page
  706: {
  707:     ////////////////////////////////////////////////////////////////////////////
  708:  
  709:     /// <summary>
  710:     ///
  711:     /// </summary>
  712:     void Page_PreInit(object sender, System.EventArgs e)
  713:     {
  714:         Ia.Islamic.Koran.Wa.Model.Ui.Default.Theme(this, HttpContext.Current.Profile);
  715:     }
  716:  
  717:     ////////////////////////////////////////////////////////////////////////////
  718:  
  719:     /// <summary>
  720:     ///
  721:     /// </summary>
  722:     protected void Page_Load(object sender, EventArgs e)
  723:     {
  724:         if (!IsPostBack)
  725:         {
  726:         }
  727:         else
  728:         {
  729:             Response.Redirect(""search.aspx?tb="" + tb.Text);
  730:         }
  731:     }
  732:  
  733:     ////////////////////////////////////////////////////////////////////////////
  734:     ////////////////////////////////////////////////////////////////////////////
  735: }
  736: ";
  737:  
  738:             return s;
  739:         }
  740:  
  741:         ////////////////////////////////////////////////////////////////////////////
  742:  
  743:         /// <summary>
  744:         ///
  745:         /// </summary>
  746:         public static string SearchFile(Ia.Cl.Models.Language language, Ia.Islamic.Cl.Model.Translation translation, string className)
  747:         {
  748:             string s;
  749:  
  750:             s = @"<%@ Page Language=""C#"" AutoEventWireup=""True"" Inherits=""" + className + @""" MasterPageFile=""~/1_" + language.Symbol + @".master"" CodeBehind=""search.aspx.cs"" %>
  751:  
  752: <asp:Content ContentPlaceHolderID=""contentContentPlaceHolder"" runat=""server"">
  753:  
  754: <asp:HyperLink NavigateUrl=""~/default.aspx"" Text=""" + translation.MainHome + @""" runat=""server""/> » <asp:HyperLink NavigateUrl=""~/" + language.Symbol + @"/default.aspx"" Text=""" + translation.MainTitle + @""" runat=""server""/> » " + translation.SearchTitle + @"
  755:  
  756: <p id=""enter-words"">" + translation.SearchInstruction + @"</p>
  757:  
  758: <p id=""text-box""><asp:TextBox id=""tb"" Maxlength=""32"" Columns=""16"" runat=""server""/>&nbsp;<asp:Button Text="" " + translation.SearchButton + @" "" runat=""server"" /></p>
  759:  
  760: <p id=""hint""><span id=""head"">" + translation.SearchHintName + @"</span>: " + translation.SearchHint + @"</p>
  761:  
  762: <p id=""search-result""><asp:Label id=""searchResultLabel"" EnableViewState=""false"" runat=""server""/></p>
  763:  
  764: <asp:Repeater id=""verseRepeater"" OnItemDataBound=""Repeater_ItemDataBound"" EnableViewState=""false"" Visible=""false"" runat=""server"">
  765:  
  766: <ItemTemplate>
  767: <p><asp:HyperLink id=""hyperLink"" runat=""server"" />&nbsp;<asp:Label id=""label"" runat=""server""/></p>
  768: </ItemTemplate>
  769:  
  770: <AlternatingItemTemplate>
  771: <p><asp:HyperLink id=""hyperLink"" runat=""server"" />&nbsp;<asp:Label id=""label"" runat=""server""/></p>
  772: </AlternatingItemTemplate>
  773:  
  774: </asp:Repeater>
  775:  
  776: </asp:Content>
  777: ";
  778:  
  779:             return s;
  780:         }
  781:  
  782:         ////////////////////////////////////////////////////////////////////////////
  783:  
  784:         /// <summary>
  785:         ///
  786:         /// </summary>
  787:         public static string SearchFileCs(Ia.Cl.Models.Language language, Ia.Islamic.Cl.Model.Translation translation, string className)
  788:         {
  789:             string s;
  790:  
  791:             s = @"using System;
  792: using System.Web;
  793: using System.Web.UI.WebControls;
  794:  
  795: ////////////////////////////////////////////////////////////////////////////
  796:  
  797: /// <summary>
  798: ///
  799: /// </summary>
  800: public partial class " + className + @" : System.Web.UI.Page
  801: {
  802:     private string language;
  803:  
  804:     ////////////////////////////////////////////////////////////////////////////
  805:  
  806:     /// <summary>
  807:     ///
  808:     /// </summary>
  809:     void Page_PreInit(object sender, System.EventArgs e)
  810:     {
  811:         Ia.Islamic.Koran.Wa.Model.Ui.Default.Theme(this, HttpContext.Current.Profile);
  812:     }
  813:  
  814:     ////////////////////////////////////////////////////////////////////////////
  815:  
  816:     /// <summary>
  817:     ///
  818:     /// </summary>
  819:     protected void Page_Init(object sender, EventArgs e)
  820:     {
  821:         language = """ + language.Symbol + @""";
  822:     }
  823:  
  824:     ////////////////////////////////////////////////////////////////////////////
  825:  
  826:     /// <summary>
  827:     ///
  828:     /// </summary>
  829:     protected void Page_Load(object sender, EventArgs e)
  830:     {
  831:         if (!IsPostBack)
  832:         {
  833:             if (Request.QueryString[""tb""] != null)
  834:             {
  835:                 tb.Text = Server.HtmlEncode(Request.QueryString[""tb""].ToString());
  836:  
  837:                 Search(sender, e);
  838:             }
  839:             else
  840:             {
  841:                 tb.Text = null;
  842:             }
  843:         }
  844:         else
  845:         {
  846:             Search(sender, e);
  847:         }
  848:     }
  849:  
  850:     ////////////////////////////////////////////////////////////////////////////
  851:  
  852:     /// <summary>
  853:     ///
  854:     /// </summary>
  855:     public void Search(object sender, EventArgs e)
  856:     {
  857:         verseRepeater.Visible = false;
  858:  
  859:         var verseList = Ia.Islamic.Cl.Model.Business.Default.Search(Server.HtmlEncode(tb.Text), language, out Ia.Cl.Models.Result result);
  860:  
  861:         if (result.IsSuccessful)
  862:         {
  863:             verseRepeater.Visible = true;
  864:  
  865:             verseRepeater.DataSource = verseList;
  866:             verseRepeater.DataBind();
  867:  
  868:             if (!result.HasWarning)
  869:             {
  870:                 searchResultLabel.Text = result.MessageWithoutCaption;
  871:                 searchResultLabel.CssClass = ""success"";
  872:             }
  873:             else
  874:             {
  875:                 searchResultLabel.Text = result.MessageWithoutCaption;
  876:                 searchResultLabel.CssClass = ""warning"";
  877:             }
  878:         }
  879:         else
  880:         {
  881:             searchResultLabel.Text = result.MessageWithoutCaption;
  882:             searchResultLabel.CssClass = ""error"";
  883:         }
  884:     }
  885:  
  886:     ////////////////////////////////////////////////////////////////////////////
  887:  
  888:     /// <summary>
  889:     ///
  890:     /// </summary>
  891:     public void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
  892:     {
  893:         ListItemType itemType = e.Item.ItemType;
  894:         Ia.Islamic.Cl.Model.Ui.VerseUi verseUi;
  895:  
  896:         verseUi = (Ia.Islamic.Cl.Model.Ui.VerseUi)e.Item.DataItem;
  897:  
  898:         if (itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem)
  899:         {
  900:             for (int i = 0; i < e.Item.Controls.Count; i++)
  901:             {
  902:                 try
  903:                 {
  904:                     if (e.Item.Controls[i].GetType().ToString() == ""System.Web.UI.WebControls.Label"")
  905:                     {
  906:                         Label label = (Label)e.Item.Controls[i];
  907:  
  908:                         if (label.ID == ""label"") label.Text = verseUi.Content;
  909:                     }
  910:                     else if (e.Item.Controls[i].GetType().ToString() == ""System.Web.UI.WebControls.HyperLink"")
  911:                     {
  912:                         HyperLink hyperLink = (HyperLink)e.Item.Controls[i];
  913:  
  914:                         if (hyperLink.ID == ""hyperLink"")
  915:                         {
  916:                             hyperLink.Text = verseUi.ChapterNumber + "":"" + verseUi.Number;
  917:                             hyperLink.NavigateUrl = verseUi.ChapterNumber + "".aspx#"" + verseUi.Number;
  918:                         }
  919:                     }
  920:                 }
  921:                 catch { }
  922:             }
  923:         }
  924:     }
  925:  
  926:     ////////////////////////////////////////////////////////////////////////////
  927:     ////////////////////////////////////////////////////////////////////////////
  928: }
  929: ";
  930:  
  931:             return s;
  932:         }
  933:  
  934:         ////////////////////////////////////////////////////////////////////////////
  935:         ////////////////////////////////////////////////////////////////////////////
  936:     }
  937:  
  938:     ////////////////////////////////////////////////////////////////////////////
  939:     ////////////////////////////////////////////////////////////////////////////
  940: }