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 data support class.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Xml;
   6:  using System.Xml.Linq;
   7:   
   8:   
   9:  namespace Ia.Learning.Manhaj.Model.Data
  10:  {
  11:      ////////////////////////////////////////////////////////////////////////////
  12:   
  13:      /// <summary publish="true">
  14:      /// Default data support class.
  15:      /// </summary>
  16:      /// <value>
  17:      /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
  18:      /// </value>
  19:      /// <remarks> 
  20:      /// Copyright © 2008-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  21:      ///
  22:      /// 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
  23:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  24:      ///
  25:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  26:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  27:      /// 
  28:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  29:      /// 
  30:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  31:      /// </remarks> 
  32:      public static class Default
  33:      {
  34:          private static XDocument xd;
  35:          private static List<Level> levels;
  36:          private static List<Grade> grades;
  37:          private static List<Course> courses;
  38:          private static List<Book> books;
  39:   
  40:          ////////////////////////////////////////////////////////////////////////////
  41:   
  42:          /// <summary>
  43:          ///
  44:          /// </summary>
  45:          private static void Initialize()
  46:          {
  47:              xd = XDocument.Load(Ia.Cl.Model.Default.AbsolutePath() + @"app_data\school.xml");
  48:   
  49:              // below: levels
  50:              levels = (from q in xd.Elements("school").Elements("level")
  51:                       select new Level
  52:                       {
  53:                           Id = int.Parse(q.Attribute("id").Value),
  54:                           Name = q.Attribute("name").Value,
  55:                           ArabicName = q.Attribute("name_ar").Value
  56:                       }).ToList<Level>();
  57:   
  58:              // below: grades
  59:              grades = (from q in xd.Elements("school").Elements("level").Elements("grade")
  60:                        select new Grade
  61:                        {
  62:                            Id = int.Parse(q.Attribute("id").Value),
  63:                            LevelId = int.Parse(q.Parent.Attribute("id").Value),
  64:                            Name = q.Attribute("name").Value,
  65:                            ArabicName = q.Attribute("name_ar").Value
  66:                        }).ToList<Grade>();
  67:   
  68:              // below: courses
  69:              courses = (from q in xd.Elements("school").Elements("level").Elements("grade").Elements("course")
  70:                         select new Course
  71:                         {
  72:                             Id = int.Parse(q.Attribute("id").Value),
  73:                             GradeId = int.Parse(q.Parent.Attribute("id").Value),
  74:                             Name = q.Attribute("name").Value,
  75:                         }).ToList<Course>();
  76:   
  77:              // below: book
  78:              books = (from q in xd.Elements("school").Elements("level").Elements("grade").Elements("course").Elements("book")
  79:                       select new Book
  80:                       {
  81:                           Id = int.Parse(q.Attribute("id").Value),
  82:                           CourseId = int.Parse(q.Parent.Attribute("id").Value),
  83:                           Name = q.Attribute("name").Value,
  84:                           TotalPages = int.Parse(q.Attribute("total_pages").Value)
  85:                       }).ToList<Book>();
  86:   
  87:          }
  88:   
  89:          ////////////////////////////////////////////////////////////////////////////
  90:   
  91:          /// <summary>
  92:          ///
  93:          /// </summary>
  94:          public static XDocument School
  95:          {
  96:              get { return xd; }
  97:          }
  98:   
  99:          ////////////////////////////////////////////////////////////////////////////
 100:   
 101:          /// <summary>
 102:          ///
 103:          /// </summary>
 104:          public static List<Level> Levels()
 105:          {
 106:              return levels;
 107:          }
 108:   
 109:          ////////////////////////////////////////////////////////////////////////////
 110:   
 111:          /// <summary>
 112:          ///
 113:          /// </summary>
 114:          public static List<Grade> GradesForLevel(int levelId, out string levelName)
 115:          {
 116:              levelName = levels.Find(p => p.Id == levelId).ArabicName;
 117:   
 118:              return grades.Where(p => p.LevelId == levelId).ToList<Grade>();
 119:          }
 120:   
 121:          ////////////////////////////////////////////////////////////////////////////
 122:   
 123:          /// <summary>
 124:          ///
 125:          /// </summary>
 126:          public static List<Course> CoursesForGrade(int gradeId, out int levelId, out string levelName, out string gradeName)
 127:          {
 128:              int i;
 129:   
 130:              gradeName = grades.Find(p => p.Id == gradeId).ArabicName;
 131:   
 132:              i = levelId = grades.Find(p => p.Id == gradeId).LevelId;
 133:              levelName = levels.Find(p => p.Id == i).ArabicName;
 134:   
 135:              return courses.Where(p => p.GradeId == gradeId).ToList<Course>();
 136:          }
 137:   
 138:          ////////////////////////////////////////////////////////////////////////////
 139:   
 140:          /// <summary>
 141:          ///
 142:          /// </summary>
 143:          public static List<Book> BooksForCourse(int courseId, out int levelId, out string levelName, out int gradeId, out string gradeName, out string courseName)
 144:          {
 145:              int i;
 146:   
 147:              courseName = courses.Find(p => p.Id == courseId).Name;
 148:   
 149:              i = gradeId = courses.Find(p => p.Id == courseId).GradeId;
 150:              gradeName = grades.Find(p => p.Id == i).ArabicName;
 151:   
 152:              i = levelId = grades.Find(p => p.Id == i).LevelId;
 153:              levelName = levels.Find(p => p.Id == i).ArabicName;
 154:   
 155:              return books.Where(p => p.CourseId == courseId).ToList<Book>();
 156:          }
 157:   
 158:          ////////////////////////////////////////////////////////////////////////////
 159:   
 160:          /// <summary>
 161:          ///
 162:          /// </summary>
 163:          public static List<Page> PagesForBook(int bookId)
 164:          {
 165:              // below: we will generate a page list from the number of total pages in book
 166:              int totalPages;
 167:              List<Page> pages;
 168:   
 169:              totalPages = (books.Where(p => p.Id == bookId)).First().TotalPages;
 170:   
 171:              pages = new List<Page>();
 172:   
 173:              for (int i = 1; i <= totalPages; i++)
 174:              {
 175:                  pages.Add(new Page { Id = i, BookId = bookId, Number = i });
 176:              }
 177:   
 178:              return pages;
 179:          }
 180:   
 181:          ////////////////////////////////////////////////////////////////////////////
 182:          ////////////////////////////////////////////////////////////////////////////
 183:      }
 184:   
 185:      ////////////////////////////////////////////////////////////////////////////
 186:      ////////////////////////////////////////////////////////////////////////////
 187:  }