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 class for TentPlay Trek data model

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Data;
   5:  using System.Collections;
   6:  using System.IO;
   7:  using Ionic.Zip;
   8:  using System.Drawing.Imaging;
   9:   
  10:  namespace Ia.TentPlay.Cl.Model.Data.Trek
  11:  {
  12:      ////////////////////////////////////////////////////////////////////////////
  13:   
  14:      /// <summary publish="true">
  15:      /// Default class for TentPlay Trek data model
  16:      /// </summary>
  17:      /// 
  18:      /// <remarks> 
  19:      /// Copyright © 2006-2017 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  20:      ///
  21:      /// 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
  22:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  23:      ///
  24:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  25:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  26:      /// 
  27:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  28:      /// 
  29:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  30:      /// </remarks> 
  31:      public static class Default
  32:      {
  33:          ////////////////////////////////////////////////////////////////////////////
  34:          ////////////////////////////////////////////////////////////////////////////
  35:   
  36:          /// <summary>
  37:          ///
  38:          /// </summary>
  39:          public struct Drawable
  40:          {
  41:              ////////////////////////////////////////////////////////////////////////////
  42:   
  43:              /// <summary>
  44:              ///
  45:              /// </summary>
  46:              public Drawable(string name, int maxWidth)
  47:              {
  48:                  this.Name = name;
  49:                  this.MaxWidth = maxWidth;
  50:              }
  51:   
  52:              /// <summary/>
  53:              public string Name;
  54:              /// <summary/>
  55:              public int MaxWidth;
  56:          }
  57:   
  58:          ////////////////////////////////////////////////////////////////////////////
  59:   
  60:          /// <summary>
  61:          ///
  62:          /// </summary>
  63:          public static List<Ia.TentPlay.Cl.Model.Business.Trek.FeatureClass> FeatureClassNameAndCountWithinDatabaseList(string countryIso2, Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Translation translation, out Ia.Cl.Model.Result result)
  64:          {
  65:              List<Ia.TentPlay.Cl.Model.Business.Trek.FeatureClass> featureClassList;
  66:              Dictionary<string, int> featureClassCountDictionary;
  67:   
  68:              result = new Ia.Cl.Model.Result();
  69:              featureClassCountDictionary = new Dictionary<string, int>();
  70:   
  71:              featureClassList = Ia.TentPlay.Cl.Model.Data.Trek.FeatureClass.List;
  72:   
  73:              using (var db = new Ia.TentPlay.Db.Trek())
  74:              {
  75:                  featureClassCountDictionary = (from ncw in db.Features
  76:                                                 where ncw.FileCountry == countryIso2
  77:                                                 group ncw.Class by ncw.Class into g
  78:                                                 select new Ia.TentPlay.Cl.Model.Business.Trek.FeatureClass() { Id = g.Key, Count = g.Count() }).ToDictionary(f => f.Id, f => f.Count);
  79:              }
  80:   
  81:              foreach (var fc in featureClassList)
  82:              {
  83:                  fc.Name = (from fc2 in translation.FeatureClassList where fc2.Id == fc.Id select fc2.Name).SingleOrDefault();
  84:                  fc.Caption = (from fc2 in translation.FeatureClassList where fc2.Id == fc.Id select fc2.Caption).SingleOrDefault();
  85:                  fc.Description = (from fc2 in translation.FeatureClassList where fc2.Id == fc.Id select fc2.Description).SingleOrDefault();
  86:   
  87:                  if (featureClassCountDictionary.ContainsKey(fc.Id)) fc.Count = featureClassCountDictionary[fc.Id];
  88:                  else fc.Count = 0;
  89:              }
  90:   
  91:              return featureClassList;
  92:          }
  93:   
  94:          ////////////////////////////////////////////////////////////////////////////
  95:   
  96:          /// <summary>
  97:          ///
  98:          /// </summary>
  99:          public static List<Ia.TentPlay.Cl.Model.Business.Trek.FeatureDesignation> FeatureDesignationNameAndCountWithinDatabaseList(string countryIso2, Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Translation translation, out Ia.Cl.Model.Result result)
 100:          {
 101:              List<Ia.TentPlay.Cl.Model.Business.Trek.FeatureDesignation> featureDesignationList;
 102:   
 103:              result = new Ia.Cl.Model.Result();
 104:   
 105:              using (var db = new Ia.TentPlay.Db.Trek())
 106:              {
 107:                  featureDesignationList = (from ncw in db.Features
 108:                                            where ncw.FileCountry == countryIso2
 109:                                            group ncw.Designation.Id by ncw.Designation.Id into g
 110:                                            select new Ia.TentPlay.Cl.Model.Business.Trek.FeatureDesignation() { Id = g.Key, Count = g.Count() }).ToList();
 111:              }
 112:   
 113:              foreach (var fd in featureDesignationList)
 114:              {
 115:                  fd.Name = Ia.TentPlay.Cl.Model.Data.Trek.FeatureDesignation.DesignationIdToDesignationCaptionHashtable(translation.Iso6391)[fd.Id].ToString();
 116:                  fd.FeatureClass = Ia.TentPlay.Cl.Model.Data.Trek.FeatureDesignation.DesignationIdToClassIdHashtable[fd.Id].ToString();
 117:              }
 118:   
 119:              return featureDesignationList;
 120:          }
 121:   
 122:          ////////////////////////////////////////////////////////////////////////////
 123:   
 124:          /// <summary>
 125:          ///
 126:          /// </summary>
 127:          public static void GenerateSpecificFeatureSqliteDatabaseFileFromSqlServer(Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Country applicationCountry, out Ia.Cl.Model.Result result)
 128:          {
 129:              int featureDesignationCount, featuresCount;
 130:              string fileName, sql, filePath, scalar;
 131:              DataTable dataTable;
 132:              FileInfo file;
 133:              Ia.Cl.Model.Db.Sqlite sqlite;
 134:              List<Ia.TentPlay.Cl.Model.Trek.FeatureDesignation> featureDesignationList;
 135:              List<Ia.TentPlay.Cl.Model.Trek.Feature> featureList;
 136:   
 137:              fileName = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileName(applicationCountry.Iso2, applicationCountry.VersionName);
 138:   
 139:              filePath = global::Ia.Cl.Model.Default.AbsolutePath(true) + fileName;
 140:   
 141:              file = new FileInfo(filePath);
 142:   
 143:              if (file.Exists)
 144:              {
 145:                  // delete file if it exists
 146:                  File.Delete(filePath);
 147:              }
 148:   
 149:              sqlite = new Ia.Cl.Model.Db.Sqlite(filePath); //, filePassword);
 150:   
 151:              // below: drop table if exists
 152:              sql = "drop table if exists features"; // first because of foreign keys
 153:              sqlite.Sql(sql);
 154:              sql = "drop table if exists featuresFts";
 155:              sqlite.Sql(sql);
 156:   
 157:              sql = "drop table if exists featureDesignations";
 158:              sqlite.Sql(sql);
 159:   
 160:              // create and populate non-virtual table FeatureDesignations
 161:              sql = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileCreateFeatureDesignationTableString(false, applicationCountry.Languages);
 162:              sqlite.Sql(sql);
 163:   
 164:              featureDesignationList = Ia.TentPlay.Cl.Model.Data.Trek.FeatureDesignation.List;
 165:   
 166:              dataTable = Ia.Cl.Model.Default.GenerateDataTableFromGenericClassList<Ia.TentPlay.Cl.Model.Trek.FeatureDesignation>(featureDesignationList, "FeatureDesignations");
 167:   
 168:              // delete irrelevant entries from dataTable
 169:              RemoveUnNeededColumns(ref dataTable, applicationCountry.Languages);
 170:   
 171:              sqlite.SqlBulkCopy(dataTable, out result);
 172:   
 173:              sql = "select count(0) from featureDesignations";
 174:              scalar = sqlite.Scalar(sql);
 175:   
 176:              if (!string.IsNullOrEmpty(scalar))
 177:              {
 178:                  featureDesignationCount = int.Parse(scalar);
 179:   
 180:                  if (featureDesignationCount == featureDesignationList.Count)
 181:                  {
 182:                      // regular table
 183:                      sql = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileCreateFeaturesTableString(false);
 184:                      sqlite.Sql(sql);
 185:   
 186:                      // You can not use index with virtual tables
 187:                      sqlite.Sql("create index if not exists noDiacriticLowerCaseNameIndex on features (noDiacriticLowerCaseName)");
 188:                      sqlite.Sql("create index if not exists fullTextSearchNameIndex on features (fullTextSearchName)");
 189:   
 190:                      // virtual table
 191:                      sql = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileCreateFeaturesTableString(true);
 192:                      sqlite.Sql(sql);
 193:   
 194:                      //sqlList = Ia.TentPlay.Cl.Model.Business.Default.SqliteDatabaseFileInsertListSpecificFeaturesTableListString(fileCountry);
 195:                      //sqlite.Sql(sqlList);
 196:                      featureList = Ia.TentPlay.Cl.Model.Data.Trek.Feature.List(applicationCountry.Iso2, out result);
 197:   
 198:                      featureList = LimitNumberOfFeaturesHenceSizeOfApp(applicationCountry.Iso2, featureList);
 199:   
 200:                      dataTable = Ia.Cl.Model.Default.GenerateDataTableFromGenericClassList<Ia.TentPlay.Cl.Model.Trek.Feature>(featureList, "Features");
 201:   
 202:                      dataTable.Columns.Remove("Deleted");
 203:                      dataTable.Columns.Remove("Created");
 204:                      dataTable.Columns.Remove("Updated");
 205:   
 206:                      sqlite.SqlBulkCopy(dataTable, out result);
 207:   
 208:                      // the FTS table
 209:                      dataTable.TableName = "featuresFts";
 210:                      sqlite.SqlBulkCopy(dataTable, out result);
 211:   
 212:                      sql = "select count(0) from features";
 213:                      scalar = sqlite.Scalar(sql);
 214:   
 215:                      if (!string.IsNullOrEmpty(scalar))
 216:                      {
 217:                          featuresCount = int.Parse(scalar);
 218:   
 219:                          if (featuresCount == featureList.Count)
 220:                          {
 221:                              result.AddSuccess("Feature designation code count: " + featureDesignationCount + "; country trek count: " + featuresCount + ". ");
 222:                          }
 223:                      }
 224:                      else
 225:                      {
 226:                          result.AddError("dt is null or dt.Rows.Count == 0 for Features. ");
 227:                      }
 228:                  }
 229:                  else
 230:                  {
 231:                      result.AddError("dt.Rows.Count != sqlList.Count for FeatureDesignations. ");
 232:                  }
 233:              }
 234:              else
 235:              {
 236:                  result.AddError("dt is null or dt.Rows.Count == 0 for FeatureDesignations. ");
 237:              }
 238:          }
 239:   
 240:          ////////////////////////////////////////////////////////////////////////////
 241:   
 242:          /// <summary>
 243:          /// This function will attempt the lower the number of features and limit the size of the database for very large numbers of features
 244:          /// </summary>
 245:          public static List<Ia.TentPlay.Cl.Model.Trek.Feature> LimitNumberOfFeaturesHenceSizeOfApp(string fileCountry, List<Ia.TentPlay.Cl.Model.Trek.Feature> featureList)
 246:          {
 247:              int r;
 248:              double specialCases;
 249:   
 250:              List<Ia.TentPlay.Cl.Model.Trek.Feature> list;
 251:   
 252:              list = new List<Ia.TentPlay.Cl.Model.Trek.Feature>();
 253:   
 254:              if (fileCountry == "ir") specialCases = .8; // I think this is because farsi feature has lots of utf8 arabic scripts
 255:              else specialCases = 1;
 256:   
 257:              if (featureList.Count > Ia.TentPlay.Cl.Model.Business.Trek.Default.MaximumNumberOfFeaturesThatWouldAllowForAcceptableAppSizeInAndroidPlayStore * specialCases)
 258:              {
 259:                  foreach(var f in featureList)
 260:                  {
 261:                      r = Ia.Cl.Model.Default.Random(featureList.Count);
 262:   
 263:                      if(r < Ia.TentPlay.Cl.Model.Business.Trek.Default.MaximumNumberOfFeaturesThatWouldAllowForAcceptableAppSizeInAndroidPlayStore * specialCases)
 264:                      {
 265:                          list.Add(f);
 266:                      }
 267:                      else
 268:                      {
 269:                          // ignore item to reduce size
 270:                      }
 271:                  }
 272:              }
 273:              else list = featureList;
 274:   
 275:              return list;
 276:          }
 277:   
 278:          ////////////////////////////////////////////////////////////////////////////
 279:   
 280:          /// <summary>
 281:          ///
 282:          /// </summary>
 283:          public static void GenerateDrawableVersionOfNavigationViewBackground(Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Country applicationCountry, out Ia.Cl.Model.Result result)
 284:          {
 285:              /*
 286:      drawable-ldpi
 287:      drawable-hdpi
 288:      drawable-mdpi
 289:      drawable-xhdpi
 290:      drawable-xxhdpi
 291:      drawable-xxxhdpi
 292:  
 293:      Maximum width: 320dp
 294:  
 295:      ldpi    @ 320.00dp    = 240.00px
 296:      mdpi    @ 320.00dp    = 320.00px
 297:      hdpi    @ 320.00dp    = 480.00px
 298:      xhdpi    @ 320.00dp    = 640.00px
 299:      xxhdpi    @ 320.00dp    = 960.00px
 300:      xxxhdpi    @ 320.00dp    = 1280.00px
 301:               */
 302:   
 303:              bool filesMatch;
 304:              int c;
 305:              string originalImageFtpLocation, destinationImageFtpLocation, newFile;
 306:              string temporaryDirectory, temporaryZipFile;
 307:              System.Drawing.Image newImage;
 308:              Hashtable ht;
 309:              ZipFile zipFile;
 310:              Ia.Cl.Model.Ftp ftp;
 311:              Ia.Cl.Model.FtpDirectory serverImageFtpDirectory;
 312:              List<string> imageList, serverImageFtpDirectoryFileNameList, serverImageFtpDirectoryNameOnlyList;
 313:              List<Drawable> drawableList;
 314:   
 315:              temporaryDirectory = Ia.Cl.Model.Default.AbsoluteTempPath() + applicationCountry.Iso2; // + @"\" + DateTime.UtcNow.AddHours(3).Ticks;
 316:              temporaryZipFile = Ia.Cl.Model.Default.AbsoluteTempPath() + applicationCountry.Iso2 + ".zip";
 317:   
 318:              originalImageFtpLocation = Ia.TentPlay.Cl.Model.Business.Trek.Default.OriginalImageFtpLocation(applicationCountry.Iso2);
 319:              destinationImageFtpLocation = Ia.TentPlay.Cl.Model.Business.Trek.Default.DestinationImageFtpLocation(applicationCountry.Iso2);
 320:   
 321:              ht = new Hashtable();
 322:   
 323:              zipFile = new ZipFile();
 324:   
 325:              drawableList = new List<Drawable>();
 326:              drawableList.Add(new Drawable("ldpi", 240));
 327:              drawableList.Add(new Drawable("hdpi", 320));
 328:              drawableList.Add(new Drawable("mdpi", 480));
 329:              drawableList.Add(new Drawable("xhdpi", 640));
 330:              drawableList.Add(new Drawable("xxhdpi", 960));
 331:              drawableList.Add(new Drawable("xxxhdpi", 1280));
 332:   
 333:              result = new Ia.Cl.Model.Result();
 334:   
 335:              try
 336:              {
 337:                  ftp = new Ia.Cl.Model.Ftp(originalImageFtpLocation, Ia.TentPlay.Cl.Model.Business.Trek.Default.FtpUser, Ia.TentPlay.Cl.Model.Business.Trek.Default.FtpPassword);
 338:   
 339:                  Directory.CreateDirectory(temporaryDirectory);
 340:                  Directory.CreateDirectory(temporaryDirectory + @"\images");
 341:   
 342:                  foreach (Drawable d in drawableList)
 343:                  {
 344:                      Directory.CreateDirectory(temporaryDirectory + @"\drawable-" + d.Name);
 345:                  }
 346:   
 347:                  // check that the files on server match the count and names of files in XML
 348:                  serverImageFtpDirectory = ftp.ListDirectoryDetail("/*.*");
 349:   
 350:                  serverImageFtpDirectoryFileNameList = (from s in serverImageFtpDirectory select s.Filename).ToList();
 351:                  serverImageFtpDirectoryNameOnlyList = (from s in serverImageFtpDirectory select s.NameOnly).ToList();
 352:   
 353:                  imageList = (from i in applicationCountry.NavigationHeaderImages select i.FileName).ToList();
 354:   
 355:                  filesMatch = true;
 356:   
 357:                  if (serverImageFtpDirectory.Count == 0 || imageList.Count == 0)
 358:                  {
 359:                      result.AddError("serverImageFtpDirectory.Count = 0 or imageList.Count = 0. ");
 360:   
 361:                      filesMatch = false;
 362:                  }
 363:                  else if (serverImageFtpDirectory.Count != imageList.Count)
 364:                  {
 365:                      result.AddError("serverImageFtpDirectory.Count (" + serverImageFtpDirectory.Count + ") != imageList.Count (" + imageList.Count + "). ");
 366:   
 367:                      filesMatch = false;
 368:                  }
 369:                  else
 370:                  {
 371:                      // check all files have *.jpg extension
 372:                      foreach (Ia.Cl.Model.FtpFileInfo ffi in serverImageFtpDirectory)
 373:                      {
 374:                          if (ffi.Extension != "jpg")
 375:                          {
 376:                              result.AddError(@"serverImageFtpDirectory's """ + ffi.Filename + @""" extension is not the required ""jpg"". ");
 377:   
 378:                              filesMatch = false;
 379:                          }
 380:   
 381:                          // check all files are at least 1M
 382:                          if (ffi.Size < 300000)
 383:                          {
 384:                              result.AddError(@"serverImageFtpDirectory's """ + ffi.Filename + @""" size is smaller than the required 300K. ");
 385:   
 386:                              filesMatch = false;
 387:                          }
 388:   
 389:                          if (!imageList.Contains(ffi.NameOnly))
 390:                          {
 391:                              result.AddError(@"serverImageFtpDirectory's """ + ffi.Filename + @""" does not exist in imageList. ");
 392:   
 393:                              filesMatch = false;
 394:                          }
 395:                      }
 396:   
 397:                      // below: check if imageList has duplicates
 398:                      foreach (string i in imageList)
 399:                      {
 400:                          if (ht.ContainsKey(i))
 401:                          {
 402:                              result.AddError(@"imageList's """ + i + @""" appears more than once in XML file. ");
 403:   
 404:                              filesMatch = false;
 405:                          }
 406:                          else ht[i] = 1;
 407:                      }
 408:   
 409:                      foreach (string i in imageList)
 410:                      {
 411:                          if (!serverImageFtpDirectoryNameOnlyList.Contains(i))
 412:                          {
 413:                              result.AddError(@"imageList's """ + i + @""" does not exist in serverImageFtpDirectoryNameOnlyList. ");
 414:   
 415:                              filesMatch = false;
 416:                          }
 417:                      }
 418:   
 419:                      if (filesMatch)
 420:                      {
 421:                          // download all original image files and create drawables
 422:                          c = 0;
 423:   
 424:                          foreach (string fileName in serverImageFtpDirectoryFileNameList)
 425:                          {
 426:                              newFile = fileName.Replace("-", "_");
 427:   
 428:                              Ia.Cl.Model.Default.DownloadFile(originalImageFtpLocation + @"/" + fileName, temporaryDirectory + @"\images\" + newFile);
 429:   
 430:                              using (var image = System.Drawing.Image.FromFile(temporaryDirectory + @"\images\" + newFile))
 431:                              {
 432:                                  foreach (Drawable d in drawableList)
 433:                                  {
 434:                                      newImage = Ia.Cl.Model.Image.Resize(image, d.MaxWidth, (image.Height * d.MaxWidth) / image.Width);
 435:   
 436:                                      newImage.Save(temporaryDirectory + @"\drawable-" + d.Name + @"\" + newFile, ImageFormat.Jpeg);
 437:   
 438:                                      c++;
 439:   
 440:                                      newImage.Dispose();
 441:                                  }
 442:   
 443:                                  image.Dispose();
 444:                              }
 445:                          }
 446:   
 447:                          // delete images so it will not be added to zip
 448:                          Directory.Delete(temporaryDirectory + @"\images", true);
 449:   
 450:                          // Zip all image files and prepare them for download
 451:                          zipFile.AddDirectory(temporaryDirectory);
 452:   
 453:                          zipFile.Save(temporaryZipFile);
 454:   
 455:                          Directory.Delete(temporaryDirectory, true);
 456:   
 457:                          result.AddSuccess("Generated " + c + " drawables for " + serverImageFtpDirectoryNameOnlyList.Count + " *.jpg original images. ");
 458:                      }
 459:                      else
 460:                      {
 461:                          result.AddError("File mismatch between server and XML file. ");
 462:                      }
 463:                  }
 464:              }
 465:              catch (Exception ex)
 466:              {
 467:                  result.AddError("Exception: " + ex.ToString());
 468:              }
 469:          }
 470:   
 471:          ////////////////////////////////////////////////////////////////////////////
 472:   
 473:          /// <summary>
 474:          ///
 475:          /// </summary>
 476:          public static void DownloadSpecificFeatureSqliteDatabaseFileFromTempFolder(Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Country applicationCountry, System.Web.UI.Page page, out Ia.Cl.Model.Result result)
 477:          {
 478:              string fileName, filePath;
 479:              FileInfo file;
 480:   
 481:              result = new Ia.Cl.Model.Result();
 482:   
 483:              fileName = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileName(applicationCountry.Iso2, applicationCountry.VersionName);
 484:   
 485:              filePath = global::Ia.Cl.Model.Default.AbsolutePath(true) + fileName;
 486:   
 487:              file = new FileInfo(filePath);
 488:   
 489:              if (file.Exists)
 490:              {
 491:                  page.Response.Clear();
 492:                  page.Response.ClearHeaders();
 493:                  page.Response.ClearContent();
 494:                  page.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
 495:                  page.Response.AddHeader("Content-Length", file.Length.ToString());
 496:                  page.Response.ContentType = "text/plain";
 497:                  page.Response.Flush();
 498:                  page.Response.TransmitFile(file.FullName);
 499:                  page.Response.End();
 500:   
 501:                  result.AddSuccess("Download complete. ");
 502:              }
 503:              else
 504:              {
 505:                  result.AddError("Download file does not exists. ");
 506:              }
 507:          }
 508:   
 509:          ////////////////////////////////////////////////////////////////////////////
 510:   
 511:          /// <summary>
 512:          ///
 513:          /// </summary>
 514:          public static void DownloadSpecificDrawableImageFileFromTempFolder(Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Country applicationCountry, System.Web.UI.Page page, out Ia.Cl.Model.Result result)
 515:          {
 516:              string fileName, filePath;
 517:              FileInfo file;
 518:   
 519:              result = new Ia.Cl.Model.Result();
 520:   
 521:              fileName = applicationCountry.Iso2 + ".zip";
 522:   
 523:              filePath = global::Ia.Cl.Model.Default.AbsolutePath(true) + fileName;
 524:   
 525:              file = new FileInfo(filePath);
 526:   
 527:              if (file.Exists)
 528:              {
 529:                  page.Response.Clear();
 530:                  page.Response.ClearHeaders();
 531:                  page.Response.ClearContent();
 532:                  page.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
 533:                  page.Response.AddHeader("Content-Length", file.Length.ToString());
 534:                  page.Response.ContentType = "application/x-zip-compressed";
 535:                  page.Response.Flush();
 536:                  page.Response.TransmitFile(file.FullName);
 537:                  page.Response.End();
 538:   
 539:                  result.AddSuccess("Download complete. ");
 540:              }
 541:              else
 542:              {
 543:                  result.AddError("Download file does not exists. ");
 544:              }
 545:          }
 546:   
 547:          ////////////////////////////////////////////////////////////////////////////
 548:   
 549:          /// <summary>
 550:          ///
 551:          /// </summary>
 552:          public static void WriteFeatureDesignationRecordListXmlToDatabase(out Ia.Cl.Model.Result result)
 553:          {
 554:              int c;
 555:              result = new Ia.Cl.Model.Result();
 556:   
 557:              c = 0;
 558:   
 559:              try
 560:              {
 561:                  using (var db = new Ia.TentPlay.Db.Trek())
 562:                  {
 563:                      if (db.Database.Exists())
 564:                      {
 565:                          // below: first will delete all current records
 566:                          var all = from nfdc in db.FeatureDesignations select nfdc;
 567:                          db.FeatureDesignations.RemoveRange(all);
 568:                          db.SaveChanges();
 569:                      }
 570:   
 571:                      foreach (Ia.TentPlay.Cl.Model.Trek.FeatureDesignation featureDesignation in Ia.TentPlay.Cl.Model.Data.Trek.FeatureDesignation.List)
 572:                      {
 573:                          var p = new Ia.TentPlay.Cl.Model.Trek.FeatureDesignation
 574:                          {
 575:                              Id = featureDesignation.Id,
 576:   
 577:                              En = featureDesignation.En,
 578:                              Ar = featureDesignation.Ar,
 579:                              Da = featureDesignation.Da,
 580:                              Zh_Simplified = featureDesignation.Zh_Simplified,
 581:                              Zh_Traditional = featureDesignation.Zh_Traditional,
 582:                              Hi = featureDesignation.Hi,
 583:                              Ja = featureDesignation.Ja,
 584:                              De = featureDesignation.De,
 585:                              Ru = featureDesignation.Ru,
 586:                              Pt = featureDesignation.Pt,
 587:                              _Id = featureDesignation._Id,
 588:                              Fr = featureDesignation.Fr,
 589:                              Es = featureDesignation.Es,
 590:                              It = featureDesignation.It,
 591:                              Ko = featureDesignation.Ko,
 592:                              Tr = featureDesignation.Tr,
 593:                              Fa = featureDesignation.Fa,
 594:                              Th = featureDesignation.Th,
 595:                              Ha = featureDesignation.Ha,
 596:                              Ig = featureDesignation.Ig,
 597:                              Yo = featureDesignation.Yo,
 598:                              Pl = featureDesignation.Pl,
 599:                              Ur = featureDesignation.Ur,
 600:                              Nl = featureDesignation.Nl,
 601:                              Ms = featureDesignation.Ms,
 602:                              Tl = featureDesignation.Tl,
 603:                              Af = featureDesignation.Af,
 604:                              Zu = featureDesignation.Zu,
 605:                              Xh = featureDesignation.Xh,
 606:                              Bn = featureDesignation.Bn,
 607:                              Vi = featureDesignation.Vi,
 608:                              Kk = featureDesignation.Kk,
 609:                              Ro = featureDesignation.Ro,
 610:                              No = featureDesignation.No,
 611:                              Uk = featureDesignation.Uk,
 612:                              Cs = featureDesignation.Cs,
 613:                              He = featureDesignation.He,
 614:                              El = featureDesignation.El,
 615:                              My = featureDesignation.My,
 616:                              Hu = featureDesignation.Hu,
 617:                              Ga = featureDesignation.Ga,
 618:                              Si = featureDesignation.Si,
 619:                              Ta = featureDesignation.Ta,
 620:                              Fi = featureDesignation.Fi,
 621:                              Uz = featureDesignation.Uz,
 622:                              Az = featureDesignation.Az,
 623:                              Be = featureDesignation.Be,
 624:                              Sk = featureDesignation.Sk,
 625:                              Am = featureDesignation.Am,
 626:                              Sw = featureDesignation.Sw,
 627:                              Bg = featureDesignation.Bg,
 628:                              Hr = featureDesignation.Hr,
 629:                              Lt = featureDesignation.Lt,
 630:                              Ne = featureDesignation.Ne,
 631:                              Sl = featureDesignation.Sl,
 632:                              Ps = featureDesignation.Ps,
 633:                              Lb = featureDesignation.Lb,
 634:                              Km = featureDesignation.Km,
 635:                              Lv = featureDesignation.Lv,
 636:                              Bs = featureDesignation.Bs,
 637:                              Et = featureDesignation.Et,
 638:                              Lo = featureDesignation.Lo,
 639:                              Mn = featureDesignation.Mn,
 640:                              Mg = featureDesignation.Mg,
 641:                              Ka = featureDesignation.Ka,
 642:                              Sq = featureDesignation.Sq,
 643:                              Mk = featureDesignation.Mk,
 644:                              Hy = featureDesignation.Hy,
 645:                              Tg = featureDesignation.Tg,
 646:                              Ky = featureDesignation.Ky,
 647:                              Is = featureDesignation.Is,
 648:                              Mt = featureDesignation.Mt,
 649:                              Ca = featureDesignation.Ca,
 650:   
 651:                              Description = featureDesignation.Description,
 652:                              Class = featureDesignation.Class,
 653:                              Deleted = featureDesignation.Deleted,
 654:   
 655:                              Created = featureDesignation.Created,
 656:                              Updated = featureDesignation.Updated
 657:                          };
 658:   
 659:                          db.FeatureDesignations.Add(p);
 660:                          c++;
 661:                      }
 662:   
 663:                      db.SaveChanges();
 664:   
 665:                      result.AddSuccess("Records added: " + c + ". ");
 666:                  }
 667:              }
 668:              catch (Exception ex)
 669:              {
 670:                  result.AddError(ex.ToString());
 671:              }
 672:          }
 673:   
 674:          ////////////////////////////////////////////////////////////////////////////
 675:   
 676:          /// <summary>
 677:          ///
 678:          /// </summary>
 679:          public static void DeleteFeatureDesignationRecordListFromDatabase(out Ia.Cl.Model.Result result)
 680:          {
 681:              result = new Ia.Cl.Model.Result();
 682:   
 683:              try
 684:              {
 685:                  using (var db = new Ia.TentPlay.Db.Trek())
 686:                  {
 687:                      if (db.Database.Exists())
 688:                      {
 689:                          // below: first will delete all current records
 690:                          var all = from nfdc in db.FeatureDesignations select nfdc;
 691:                          db.FeatureDesignations.RemoveRange(all);
 692:                          db.SaveChanges();
 693:                      }
 694:   
 695:                      db.SaveChanges();
 696:   
 697:                      result.AddSuccess("Records deleted. ");
 698:                  }
 699:              }
 700:              catch (Exception ex)
 701:              {
 702:                  result.AddError(ex.ToString());
 703:              }
 704:          }
 705:   
 706:          ////////////////////////////////////////////////////////////////////////////
 707:   
 708:          /// <summary>
 709:          ///
 710:          /// </summary>
 711:          public static void RemoveUnNeededColumns(ref DataTable dataTable, List<Ia.Cl.Model.Language> countrySpokenLanguageList)
 712:          {
 713:              List<string> columnsToRemove;
 714:   
 715:              dataTable.Columns.Remove("Deleted");
 716:              dataTable.Columns.Remove("Description");
 717:              dataTable.Columns.Remove("Created");
 718:              dataTable.Columns.Remove("Updated");
 719:   
 720:              columnsToRemove = new List<string>();
 721:   
 722:              foreach (DataColumn column in dataTable.Columns)
 723:              {
 724:                  if (countrySpokenLanguageList.Find(u => u.Symbol == column.ColumnName.ToLower()) == null)
 725:                  {
 726:                      if (column.ColumnName.ToLower() != "id" && column.ColumnName.ToLower() != "class")
 727:                      {
 728:                          columnsToRemove.Add(column.ColumnName);
 729:                      }
 730:                  }
 731:              }
 732:   
 733:              if (columnsToRemove.Count > 0)
 734:              {
 735:                  foreach (string columnName in columnsToRemove)
 736:                  {
 737:                      dataTable.Columns.Remove(columnName);
 738:                  }
 739:              }
 740:          }
 741:   
 742:          ////////////////////////////////////////////////////////////////////////////
 743:          ////////////////////////////////////////////////////////////////////////////
 744:      }
 745:  }