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

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.Data;
    3: using System.Collections;
    4: using System.Collections.Generic;
    5: using System.Linq;
    6: using System.IO;
    7: using System.IO.Compression;
    8:  
    9: namespace Ia.TentPlay.Cl.Model.Data.Trek
   10: {
   11:     ////////////////////////////////////////////////////////////////////////////
   12:  
   13:     /// <summary publish="true">
   14:     /// Default class for TentPlay Trek data model
   15:     /// </summary>
   16:     /// 
   17:     /// <remarks> 
   18:     /// Copyright © 2006-2017 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   19:     ///
   20:     /// 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
   21:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   22:     ///
   23:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   24:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   25:     /// 
   26:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   27:     /// 
   28:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   29:     /// </remarks> 
   30:     public static class Default
   31:     {
   32:         public static List<string> GnsCountryCodeList = new List<string> { "ABW", "AFG", "AGO", "AIA", "ALB", "AND", "ARE", "ARG", "ARM", "ATF", "ATG", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BES", "BFA", "BGD", "BGR", "BHR", "BHS", "BIH", "BLM", "BLR", "BLZ", "BMU", "BOL", "BRA", "BRB", "BRN", "BTN", "BVT", "BWA", "CAF", "CAN", "CCK", "CHE", "CHL", "CHN", "CIV", "CMR", "COD", "COG", "COK", "COL", "COM", "CPT", "CPV", "CRI", "CUB", "CUW", "CXR", "CYM", "CYP", "CZE", "DEU", "DJI", "DMA", "DNK", "DOM", "DZA", "ECU", "EGY", "ERI", "ESP", "EST", "ETH", "FIN", "FJI", "FLK", "FRA", "FRO", "FSM", "GAB", "GBR", "GEO", "GGY", "GHA", "GIB", "GIN", "GLP", "GMB", "GNB", "GNQ", "GRC", "GRD", "GRL", "GTM", "GUF", "GUY", "HKG", "HMD", "HND", "HRV", "HTI", "HUN", "IDN", "IMN", "IND", "IOT", "IRL", "IRN", "IRQ", "ISL", "ISR", "ITA", "JAM", "JEY", "JOR", "JPN", "KAZ", "KEN", "KGZ", "KHM", "KIR", "KNA", "KOR", "KWT", "LAO", "LBN", "LBR", "LBY", "LCA", "LIE", "LKA", "LSO", "LTU", "LUX", "LVA", "MAC", "MAF", "MAR", "MCO", "MDA", "MDG", "MDV", "MEX", "MHL", "MKD", "MLI", "MLT", "MMR", "MNE", "MNG", "MOZ", "MRT", "MSR", "MTQ", "MUS", "MWI", "MYS", "MYT", "NAM", "NCL", "NER", "NFK", "NGA", "NIC", "NIU", "NLD", "NOR", "NPL", "NRU", "NZL", "OMN", "PAK", "PAN", "PCN", "PER", "PHL", "PLW", "PNG", "POL", "PRK", "PRT", "PRY", "PYF", "QAT", "QNM", "QOS", "QUF", "REU", "ROU", "RUS", "RWA", "SAU", "SDN", "SEN", "SGP", "SGS", "SHN", "SLB", "SLE", "SLV", "SMR", "SOM", "SPM", "SRB", "SSD", "STP", "SUR", "SVK", "SVN", "SWE", "SWZ", "SXM", "SYC", "SYR", "TCA", "TCD", "TGO", "THA", "TJK", "TKL", "TKM", "TLS", "TON", "TTO", "TUN", "TUR", "TUV", "TWN", "TZA", "UGA", "UKR", "URY", "UZB", "VAT", "VCT", "VEN", "VGB", "VNM", "VUT", "WLF", "WSM", "XAA", "XAC", "XCS", "XGZ", "XJM", "XKS", "XPR", "XQN", "XQO", "XQP", "XQQ", "XQR", "XQT", "XQV", "XQY", "XQZ", "XSP", "XSQ", "XSV", "XWB", "XXD", "XXF", "XXI", "XXN", "XXO", "XXX", "XXY", "XXZ", "XZZ", "YEM", "ZAF", "ZMB", "ZWE" };
   33:  
   34:         ////////////////////////////////////////////////////////////////////////////
   35:         ////////////////////////////////////////////////////////////////////////////
   36:  
   37:         /// <summary>
   38:         ///
   39:         /// </summary>
   40:         public struct Drawable
   41:         {
   42:             ////////////////////////////////////////////////////////////////////////////
   43:  
   44:             /// <summary>
   45:             ///
   46:             /// </summary>
   47:             public Drawable(string name, int maxWidth)
   48:             {
   49:                 this.Name = name;
   50:                 this.MaxWidth = maxWidth;
   51:             }
   52:  
   53:             /// <summary/>
   54:             public string Name;
   55:             /// <summary/>
   56:             public int MaxWidth;
   57:         }
   58:  
   59:         ////////////////////////////////////////////////////////////////////////////
   60:  
   61:         /// <summary>
   62:         ///
   63:         /// </summary>
   64:         public static List<Ia.TentPlay.Cl.Model.Business.Trek.FeatureClass> FeatureClassNameAndCountWithinDatabaseList(string countryIso3, Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Translation translation, out Ia.Cl.Models.Result result)
   65:         {
   66:             List<Ia.TentPlay.Cl.Model.Business.Trek.FeatureClass> featureClassList;
   67:             Dictionary<string, int> featureClassCountDictionary;
   68:  
   69:             result = new Ia.Cl.Models.Result();
   70:             featureClassCountDictionary = new Dictionary<string, int>();
   71:  
   72:             featureClassList = Ia.TentPlay.Cl.Model.Data.Trek.FeatureClass.List;
   73:  
   74:             using (var db = new Ia.TentPlay.Db())
   75:             {
   76:                 featureClassCountDictionary = (from ncw in db.Features
   77:                                                where ncw.FileCountry == countryIso3
   78:                                                group ncw.Class by ncw.Class into g
   79:                                                select new Ia.TentPlay.Cl.Model.Business.Trek.FeatureClass() { Id = g.Key, Count = g.Count() }).ToDictionary(f => f.Id, f => f.Count);
   80:             }
   81:  
   82:             foreach (var fc in featureClassList)
   83:             {
   84:                 fc.Name = (from fc2 in translation.FeatureClassList where fc2.Id == fc.Id select fc2.Name).SingleOrDefault();
   85:                 fc.Caption = (from fc2 in translation.FeatureClassList where fc2.Id == fc.Id select fc2.Caption).SingleOrDefault();
   86:                 fc.Description = (from fc2 in translation.FeatureClassList where fc2.Id == fc.Id select fc2.Description).SingleOrDefault();
   87:  
   88:                 if (featureClassCountDictionary.ContainsKey(fc.Id)) fc.Count = featureClassCountDictionary[fc.Id];
   89:                 else fc.Count = 0;
   90:             }
   91:  
   92:             return featureClassList;
   93:         }
   94:  
   95:         ////////////////////////////////////////////////////////////////////////////
   96:  
   97:         /// <summary>
   98:         ///
   99:         /// </summary>
  100:         public static List<Ia.TentPlay.Cl.Model.Business.Trek.FeatureDesignation> FeatureDesignationNameAndCountWithinDatabaseList(string countryIso3, Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Translation translation, out Ia.Cl.Models.Result result)
  101:         {
  102:             List<Ia.TentPlay.Cl.Model.Business.Trek.FeatureDesignation> featureDesignationList;
  103:  
  104:             result = new Ia.Cl.Models.Result();
  105:  
  106:             using (var db = new Ia.TentPlay.Db())
  107:             {
  108:                 featureDesignationList = (from ncw in db.Features
  109:                                           where ncw.FileCountry == countryIso3
  110:                                           group ncw.Designation.Id by ncw.Designation.Id into g
  111:                                           select new Ia.TentPlay.Cl.Model.Business.Trek.FeatureDesignation() { Id = g.Key, Count = g.Count() }).ToList();
  112:             }
  113:  
  114:             foreach (var fd in featureDesignationList)
  115:             {
  116:                 fd.Name = Ia.TentPlay.Cl.Model.Data.Trek.FeatureDesignation.DesignationIdToDesignationCaptionHashtable(translation.Iso6391)[fd.Id].ToString();
  117:                 fd.FeatureClass = Ia.TentPlay.Cl.Model.Data.Trek.FeatureDesignation.DesignationIdToClassIdHashtable[fd.Id].ToString();
  118:             }
  119:  
  120:             return featureDesignationList;
  121:         }
  122:  
  123:         ////////////////////////////////////////////////////////////////////////////
  124:  
  125:         /// <summary>
  126:         ///
  127:         /// </summary>
  128:         public static void GenerateSpecificFeatureSqliteDatabaseFileFromSqlServer(Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Country applicationCountry, out Ia.Cl.Models.Result result)
  129:         {
  130:             int featureDesignationCount, featuresCount;
  131:             string fileName, sql, filePath, scalar;
  132:             DataTable dataTable;
  133:             FileInfo file;
  134:             Ia.Cl.Models.Db.Sqlite sqlite;
  135:             List<Ia.TentPlay.Cl.Model.Trek.FeatureDesignation> featureDesignationList;
  136:             List<Ia.TentPlay.Cl.Model.Trek.Feature> featureList;
  137:  
  138:             fileName = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileName(applicationCountry.Iso3, applicationCountry.VersionName);
  139:  
  140:             filePath = Ia.TentPlay.Cl.Model.Business.Trek.Default.ApplicationTempDataDirectory /*+ applicationCountry.Iso3*/ + @"\" + fileName;
  141:  
  142:             file = new FileInfo(filePath);
  143:  
  144:             if (file.Exists)
  145:             {
  146:                 // delete file if it exists
  147:                 File.Delete(filePath);
  148:             }
  149:  
  150:             sqlite = new Ia.Cl.Models.Db.Sqlite(filePath); //, filePassword);
  151:  
  152:             // below: drop table if exists
  153:             sql = "drop table if exists features"; // first because of foreign keys
  154:             sqlite.Sql(sql);
  155:             sql = "drop table if exists featuresFts";
  156:             sqlite.Sql(sql);
  157:  
  158:             sql = "drop table if exists featureDesignations";
  159:             sqlite.Sql(sql);
  160:  
  161:             // create and populate non-virtual table FeatureDesignations
  162:             sql = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileCreateFeatureDesignationTableString(false, applicationCountry.Languages);
  163:             sqlite.Sql(sql);
  164:  
  165:             featureDesignationList = Ia.TentPlay.Cl.Model.Data.Trek.FeatureDesignation.List;
  166:  
  167:             dataTable = Ia.Cl.Models.Default.GenerateDataTableFromGenericClassList<Ia.TentPlay.Cl.Model.Trek.FeatureDesignation>(featureDesignationList, "FeatureDesignations");
  168:  
  169:             // delete irrelevant entries from dataTable
  170:             RemoveUnNeededColumns(ref dataTable, applicationCountry.Languages);
  171:  
  172:             sqlite.SqlBulkCopy(dataTable, out result);
  173:  
  174:             sql = "select count(0) from featureDesignations";
  175:             scalar = sqlite.Scalar(sql);
  176:  
  177:             if (!string.IsNullOrEmpty(scalar))
  178:             {
  179:                 featureDesignationCount = int.Parse(scalar);
  180:  
  181:                 if (featureDesignationCount == featureDesignationList.Count)
  182:                 {
  183:                     // regular table
  184:                     sql = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileCreateFeaturesTableString(false);
  185:                     sqlite.Sql(sql);
  186:  
  187:                     // You can not use index with virtual tables
  188:                     sqlite.Sql("create index if not exists noDiacriticLowerCaseNameIndex on features (noDiacriticLowerCaseName)");
  189:                     sqlite.Sql("create index if not exists fullTextSearchNameIndex on features (fullTextSearchName)");
  190:  
  191:                     // virtual table
  192:                     sql = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileCreateFeaturesTableString(true);
  193:                     sqlite.Sql(sql);
  194:  
  195:                     //sqlList = Ia.TentPlay.Cl.Model.Business.Default.SqliteDatabaseFileInsertListSpecificFeaturesTableListString(fileCountry);
  196:                     //sqlite.Sql(sqlList);
  197:                     featureList = Ia.TentPlay.Cl.Model.Data.Trek.Feature.List(applicationCountry.Iso3, out result);
  198:  
  199:                     featureList = LimitNumberOfFeaturesHenceSizeOfApp(applicationCountry.Iso3, featureList);
  200:  
  201:                     dataTable = Ia.Cl.Models.Default.GenerateDataTableFromGenericClassList<Ia.TentPlay.Cl.Model.Trek.Feature>(featureList, "Features");
  202:  
  203:                     dataTable.Columns.Remove("Deleted");
  204:                     dataTable.Columns.Remove("Created");
  205:                     dataTable.Columns.Remove("Updated");
  206:  
  207:                     sqlite.SqlBulkCopy(dataTable, out result);
  208:  
  209:                     // the FTS table
  210:                     dataTable.TableName = "featuresFts";
  211:                     sqlite.SqlBulkCopy(dataTable, out result);
  212:  
  213:                     sql = "select count(0) from features";
  214:                     scalar = sqlite.Scalar(sql);
  215:  
  216:                     if (!string.IsNullOrEmpty(scalar))
  217:                     {
  218:                         featuresCount = int.Parse(scalar);
  219:  
  220:                         if (featuresCount == featureList.Count)
  221:                         {
  222:                             result.AddSuccess("Feature designation code count: " + featureDesignationCount + "; country trek count: " + featuresCount + ". ");
  223:                         }
  224:                     }
  225:                     else
  226:                     {
  227:                         result.AddError("dt is null or dt.Rows.Count == 0 for Features. ");
  228:                     }
  229:                 }
  230:                 else
  231:                 {
  232:                     result.AddError("dt.Rows.Count != sqlList.Count for FeatureDesignations. ");
  233:                 }
  234:             }
  235:             else
  236:             {
  237:                 result.AddError("dt is null or dt.Rows.Count == 0 for FeatureDesignations. ");
  238:             }
  239:         }
  240:  
  241:         ////////////////////////////////////////////////////////////////////////////
  242:  
  243:         /// <summary>
  244:         ///
  245:         /// </summary>
  246:         public static void DownloadSpecificFeatureSqliteDatabaseFileFromTempFolder(Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Country applicationCountry, out Ia.Cl.Models.Result result)
  247:         {
  248:             string fileName, filePath;
  249:             FileInfo file;
  250:  
  251:             fileName = Ia.TentPlay.Cl.Model.Business.Trek.Default.SqliteDatabaseFileName(applicationCountry.Iso3, applicationCountry.VersionName);
  252:  
  253:             filePath = Ia.TentPlay.Cl.Model.Business.Trek.Default.ApplicationTempDataDirectory /*+ applicationCountry.Iso3*/ + @"\" + fileName;
  254:  
  255:             file = new FileInfo(filePath);
  256:  
  257:             result = new Ia.Cl.Models.Result();
  258:  
  259:             if (file.Exists)
  260:             {
  261:                 System.Web.HttpContext.Current.Response.Clear();
  262:                 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  263:                 System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
  264:                 System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
  265:                 System.Web.HttpContext.Current.Response.WriteFile(file.FullName);
  266:                 System.Web.HttpContext.Current.Response.End();
  267:  
  268:                 result.AddSuccess("File download of " + fileName + " was successfull. See the browser's download folder.");
  269:             }
  270:             else
  271:             {
  272:                 // Handle the case where the file does not exist
  273:                 System.Web.HttpContext.Current.Response.Write("File not found.");
  274:  
  275:                 result.AddError("File download of " + fileName + " failed. File was not found.");
  276:             }
  277:         }
  278:  
  279:         ////////////////////////////////////////////////////////////////////////////
  280:  
  281:         /// <summary>
  282:         /// This function will attempt the lower the number of features and limit the size of the database for very large numbers of features
  283:         /// </summary>
  284:         public static List<Ia.TentPlay.Cl.Model.Trek.Feature> LimitNumberOfFeaturesHenceSizeOfApp(string fileCountry, List<Ia.TentPlay.Cl.Model.Trek.Feature> featureList)
  285:         {
  286:             int r;
  287:             double specialCases;
  288:  
  289:             List<Ia.TentPlay.Cl.Model.Trek.Feature> list;
  290:  
  291:             list = new List<Ia.TentPlay.Cl.Model.Trek.Feature>();
  292:  
  293:             if (fileCountry == "ir") specialCases = .8; // I think this is because farsi feature has lots of utf8 arabic scripts
  294:             else if (fileCountry == "ru") specialCases = .48;
  295:             else specialCases = 1;
  296:  
  297:             if (featureList.Count > Ia.TentPlay.Cl.Model.Business.Trek.Default.MaximumNumberOfFeaturesThatWouldAllowForAcceptableAppSizeInAndroidPlayStore * specialCases)
  298:             {
  299:                 foreach (var f in featureList)
  300:                 {
  301:                     r = Ia.Cl.Models.Default.Random(featureList.Count);
  302:  
  303:                     if (r < Ia.TentPlay.Cl.Model.Business.Trek.Default.MaximumNumberOfFeaturesThatWouldAllowForAcceptableAppSizeInAndroidPlayStore * specialCases)
  304:                     {
  305:                         list.Add(f);
  306:                     }
  307:                     else
  308:                     {
  309:                         // ignore item to reduce size
  310:                     }
  311:                 }
  312:             }
  313:             else list = featureList;
  314:  
  315:             return list;
  316:         }
  317:  
  318:         ////////////////////////////////////////////////////////////////////////////
  319:  
  320:         /// <summary>
  321:         ///
  322:         /// </summary>
  323:         public static void GenerateDrawableVersionOfNavigationViewBackground(Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Country applicationCountry, out Ia.Cl.Models.Result result)
  324:         {
  325:             /*
  326:     drawable-ldpi
  327:     drawable-hdpi
  328:     drawable-mdpi
  329:     drawable-xhdpi
  330:     drawable-xxhdpi
  331:     drawable-xxxhdpi
  332:  
  333:     Maximum width: 320dp
  334:  
  335:     ldpi    @ 320.00dp    = 240.00px
  336:     mdpi    @ 320.00dp    = 320.00px
  337:     hdpi    @ 320.00dp    = 480.00px
  338:     xhdpi    @ 320.00dp    = 640.00px
  339:     xxhdpi    @ 320.00dp    = 960.00px
  340:     xxxhdpi    @ 320.00dp    = 1280.00px
  341:              */
  342:  
  343:             bool filesMatch;
  344:             int c;
  345:             string originalImageFtpSite, originalCreativeCommonsImageFtpLocation, destinationImageFtpLocation, newFile;
  346:             string temporaryDirectory, temporaryZipFile;
  347:             System.Drawing.Image newImage;
  348:             Hashtable ht;
  349:             Ia.Cl.Models.Ftp ftp;
  350:             Ia.Cl.Models.FtpDirectory serverImageFtpDirectory;
  351:             List<string> imageList, serverImageFtpDirectoryFileNameList, serverImageFtpDirectoryNameOnlyList;
  352:             List<Drawable> drawableList;
  353:  
  354:             temporaryDirectory = Ia.TentPlay.Cl.Model.Business.Trek.Default.ApplicationTempDataDirectory + applicationCountry.Iso3; // + @"\" + DateTime.UtcNow.AddHours(3).Ticks;
  355:             temporaryZipFile = Ia.TentPlay.Cl.Model.Business.Trek.Default.ApplicationTempDataDirectory + applicationCountry.Iso3 + ".zip";
  356:  
  357:             originalImageFtpSite = Ia.TentPlay.Cl.Model.Business.Trek.Default.OriginalImageFtpSite;
  358:             originalCreativeCommonsImageFtpLocation = Ia.TentPlay.Cl.Model.Business.Trek.Default.OriginalCreativeCommonsImageFtpLocation(applicationCountry.Iso3);
  359:             destinationImageFtpLocation = Ia.TentPlay.Cl.Model.Business.Trek.Default.DestinationImageFtpLocation(applicationCountry.Iso3);
  360:  
  361:             ht = new Hashtable();
  362:  
  363:             drawableList = new List<Drawable>();
  364:             drawableList.Add(new Drawable("ldpi", 240));
  365:             drawableList.Add(new Drawable("hdpi", 320));
  366:             drawableList.Add(new Drawable("mdpi", 480));
  367:             drawableList.Add(new Drawable("xhdpi", 640));
  368:             drawableList.Add(new Drawable("xxhdpi", 960));
  369:             drawableList.Add(new Drawable("xxxhdpi", 1280));
  370:  
  371:             result = new Ia.Cl.Models.Result();
  372:  
  373:             try
  374:             {
  375:                 ftp = new Ia.Cl.Models.Ftp(originalImageFtpSite, Ia.TentPlay.Cl.Model.Business.Trek.Default.FtpUser, Ia.TentPlay.Cl.Model.Business.Trek.Default.FtpPassword);
  376:  
  377:                 Directory.CreateDirectory(temporaryDirectory);
  378:                 Directory.CreateDirectory(temporaryDirectory + @"\images");
  379:  
  380:                 foreach (Drawable d in drawableList)
  381:                 {
  382:                     Directory.CreateDirectory(temporaryDirectory + @"\drawable-" + d.Name);
  383:                 }
  384:  
  385:                 // check that the files on server match the count and names of files in XML
  386:                 serverImageFtpDirectory = ftp.ListDirectoryDetail(originalCreativeCommonsImageFtpLocation);
  387:  
  388:                 serverImageFtpDirectoryFileNameList = (from s in serverImageFtpDirectory select s.Filename).ToList();
  389:                 serverImageFtpDirectoryNameOnlyList = (from s in serverImageFtpDirectory select s.NameOnly).ToList();
  390:  
  391:                 imageList = (from i in applicationCountry.NavigationHeaderImages select i.FileName).ToList();
  392:  
  393:                 filesMatch = true;
  394:  
  395:                 if (serverImageFtpDirectory.Count == 0 || imageList.Count == 0)
  396:                 {
  397:                     result.AddError("serverImageFtpDirectory.Count = 0 or imageList.Count = 0. ");
  398:  
  399:                     filesMatch = false;
  400:                 }
  401:                 else if (serverImageFtpDirectory.Count != imageList.Count)
  402:                 {
  403:                     result.AddError("serverImageFtpDirectory.Count (" + serverImageFtpDirectory.Count + ") != imageList.Count (" + imageList.Count + "). ");
  404:  
  405:                     filesMatch = false;
  406:                 }
  407:                 else
  408:                 {
  409:                     // check all files have *.jpg extension
  410:                     foreach (Ia.Cl.Models.FtpFileInfo ffi in serverImageFtpDirectory)
  411:                     {
  412:                         if (ffi.Extension != "jpg")
  413:                         {
  414:                             result.AddError(@"serverImageFtpDirectory's """ + ffi.Filename + @""" extension is not the required ""jpg"". ");
  415:  
  416:                             filesMatch = false;
  417:                         }
  418:  
  419:                         // check all files are at least 1M
  420:                         if (ffi.Size < 300000)
  421:                         {
  422:                             result.AddError(@"serverImageFtpDirectory's """ + ffi.Filename + @""" size is smaller than the required 300K. ");
  423:  
  424:                             filesMatch = false;
  425:                         }
  426:  
  427:                         if (!imageList.Contains(ffi.NameOnly))
  428:                         {
  429:                             result.AddError(@"serverImageFtpDirectory's """ + ffi.Filename + @""" does not exist in imageList. ");
  430:  
  431:                             filesMatch = false;
  432:                         }
  433:                     }
  434:  
  435:                     // below: check if imageList has duplicates
  436:                     foreach (string i in imageList)
  437:                     {
  438:                         if (ht.ContainsKey(i))
  439:                         {
  440:                             result.AddError(@"imageList's """ + i + @""" appears more than once in XML file. ");
  441:  
  442:                             filesMatch = false;
  443:                         }
  444:                         else ht[i] = 1;
  445:                     }
  446:  
  447:                     foreach (string i in imageList)
  448:                     {
  449:                         if (!serverImageFtpDirectoryNameOnlyList.Contains(i))
  450:                         {
  451:                             result.AddError(@"imageList's """ + i + @""" does not exist in serverImageFtpDirectoryNameOnlyList. ");
  452:  
  453:                             filesMatch = false;
  454:                         }
  455:                     }
  456:  
  457:                     if (filesMatch)
  458:                     {
  459:                         // download all original image files and create drawables
  460:                         c = 0;
  461:  
  462:                         foreach (string fileName in serverImageFtpDirectoryFileNameList)
  463:                         {
  464:                             newFile = fileName.Replace("-", "_");
  465:  
  466:                             ftp.Download(originalCreativeCommonsImageFtpLocation + @"/" + fileName, temporaryDirectory + @"\images\" + newFile, true);
  467:  
  468:                             using (var image = System.Drawing.Image.FromFile(temporaryDirectory + @"\images\" + newFile))
  469:                             {
  470:                                 foreach (Drawable d in drawableList)
  471:                                 {
  472:                                     newImage = Ia.Cl.Models.Image.Resize(image, d.MaxWidth, (image.Height * d.MaxWidth) / image.Width);
  473:  
  474:                                     newImage.Save(temporaryDirectory + @"\drawable-" + d.Name + @"\" + newFile, System.Drawing.Imaging.ImageFormat.Jpeg);
  475:  
  476:                                     c++;
  477:  
  478:                                     newImage.Dispose();
  479:                                 }
  480:  
  481:                                 image.Dispose();
  482:                             }
  483:                         }
  484:  
  485:                         // delete images so it will not be added to zip
  486:                         Directory.Delete(temporaryDirectory + @"\images", true);
  487:  
  488:                         // delete old zip if exists
  489:                         if (File.Exists(temporaryZipFile)) File.Delete(temporaryZipFile);
  490:  
  491:                         // Zip all image files and prepare them for download
  492:                         ZipFile.CreateFromDirectory(temporaryDirectory, temporaryZipFile);
  493:  
  494:                         Directory.Delete(temporaryDirectory, true);
  495:  
  496:                         result.AddSuccess("Generated " + c + " drawables for " + serverImageFtpDirectoryNameOnlyList.Count + " *.jpg original images. ");
  497:                     }
  498:                     else
  499:                     {
  500:                         result.AddError("File mismatch between server and XML file. ");
  501:                     }
  502:                 }
  503:             }
  504:             catch (Exception ex)
  505:             {
  506:                 result.AddError("Exception: " + ex.ToString());
  507:             }
  508:         }
  509:  
  510:         ////////////////////////////////////////////////////////////////////////////
  511:  
  512:         /// <summary>
  513:         ///
  514:         /// </summary>
  515:         public static void DownloadSpecificDrawableImageFileFromTempFolder(Ia.TentPlay.Cl.Model.Data.Trek.ApplicationInformation.Country applicationCountry, out Ia.Cl.Models.Result result)
  516:         {
  517:             string fileName, filePath;
  518:             FileInfo file;
  519:  
  520:             fileName = applicationCountry.Iso3 + ".zip";
  521:  
  522:             filePath = Ia.TentPlay.Cl.Model.Business.Trek.Default.ApplicationTempDataDirectory + @"\" + fileName;
  523:  
  524:             file = new FileInfo(filePath);
  525:  
  526:             result = new Ia.Cl.Models.Result();
  527:  
  528:             if (file.Exists)
  529:             {
  530:                 System.Web.HttpContext.Current.Response.Clear();
  531:                 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  532:                 System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
  533:                 System.Web.HttpContext.Current.Response.ContentType = "application/zip";
  534:                 System.Web.HttpContext.Current.Response.WriteFile(file.FullName);
  535:                 System.Web.HttpContext.Current.Response.End();
  536:  
  537:                 result.AddSuccess("File download of " + fileName + " was successfull. See the browser's download folder.");
  538:             }
  539:             else
  540:             {
  541:                 // Handle the case where the file does not exist
  542:                 System.Web.HttpContext.Current.Response.Write("File not found.");
  543:  
  544:                 result.AddError("File download of " + fileName + " failed. File was not found.");
  545:             }
  546:         }
  547:  
  548:         ////////////////////////////////////////////////////////////////////////////
  549:  
  550:         /// <summary>
  551:         ///
  552:         /// </summary>
  553:         public static void WriteFeatureDesignationRecordListXmlToDatabase(out Ia.Cl.Models.Result result)
  554:         {
  555:             int c, recordCount;
  556:             result = new Ia.Cl.Models.Result();
  557:  
  558:             c = 0;
  559:  
  560:             try
  561:             {
  562:                 using (var db = new Ia.TentPlay.Db())
  563:                 {
  564:                     db.Database.EnsureCreated();
  565:  
  566:                     recordCount = db.FeatureDesignations.Count();
  567:  
  568:                     if (recordCount == 0)
  569:                     {
  570:                         foreach (Ia.TentPlay.Cl.Model.Trek.FeatureDesignation featureDesignation in Ia.TentPlay.Cl.Model.Data.Trek.FeatureDesignation.List)
  571:                         {
  572:                             var p = new Ia.TentPlay.Cl.Model.Trek.FeatureDesignation
  573:                             {
  574:                                 Id = featureDesignation.Id,
  575:  
  576:                                 En = featureDesignation.En,
  577:                                 Ar = featureDesignation.Ar,
  578:                                 Da = featureDesignation.Da,
  579:                                 Zh_Simplified = featureDesignation.Zh_Simplified,
  580:                                 Zh_Traditional = featureDesignation.Zh_Traditional,
  581:                                 Hi = featureDesignation.Hi,
  582:                                 Ja = featureDesignation.Ja,
  583:                                 De = featureDesignation.De,
  584:                                 Ru = featureDesignation.Ru,
  585:                                 Pt = featureDesignation.Pt,
  586:                                 _Id = featureDesignation._Id,
  587:                                 Fr = featureDesignation.Fr,
  588:                                 Es = featureDesignation.Es,
  589:                                 It = featureDesignation.It,
  590:                                 Ko = featureDesignation.Ko,
  591:                                 Tr = featureDesignation.Tr,
  592:                                 Fa = featureDesignation.Fa,
  593:                                 Th = featureDesignation.Th,
  594:                                 Ha = featureDesignation.Ha,
  595:                                 Ig = featureDesignation.Ig,
  596:                                 Yo = featureDesignation.Yo,
  597:                                 Pl = featureDesignation.Pl,
  598:                                 Ur = featureDesignation.Ur,
  599:                                 Nl = featureDesignation.Nl,
  600:                                 Ms = featureDesignation.Ms,
  601:                                 Tl = featureDesignation.Tl,
  602:                                 Af = featureDesignation.Af,
  603:                                 Zu = featureDesignation.Zu,
  604:                                 Xh = featureDesignation.Xh,
  605:                                 Bn = featureDesignation.Bn,
  606:                                 Vi = featureDesignation.Vi,
  607:                                 Kk = featureDesignation.Kk,
  608:                                 Ro = featureDesignation.Ro,
  609:                                 No = featureDesignation.No,
  610:                                 Uk = featureDesignation.Uk,
  611:                                 Cs = featureDesignation.Cs,
  612:                                 He = featureDesignation.He,
  613:                                 El = featureDesignation.El,
  614:                                 My = featureDesignation.My,
  615:                                 Hu = featureDesignation.Hu,
  616:                                 Ga = featureDesignation.Ga,
  617:                                 Si = featureDesignation.Si,
  618:                                 Ta = featureDesignation.Ta,
  619:                                 Fi = featureDesignation.Fi,
  620:                                 Uz = featureDesignation.Uz,
  621:                                 Az = featureDesignation.Az,
  622:                                 Be = featureDesignation.Be,
  623:                                 Sk = featureDesignation.Sk,
  624:                                 Am = featureDesignation.Am,
  625:                                 Sw = featureDesignation.Sw,
  626:                                 Bg = featureDesignation.Bg,
  627:                                 Hr = featureDesignation.Hr,
  628:                                 Lt = featureDesignation.Lt,
  629:                                 Ne = featureDesignation.Ne,
  630:                                 Sl = featureDesignation.Sl,
  631:                                 Ps = featureDesignation.Ps,
  632:                                 Lb = featureDesignation.Lb,
  633:                                 Km = featureDesignation.Km,
  634:                                 Lv = featureDesignation.Lv,
  635:                                 Bs = featureDesignation.Bs,
  636:                                 Et = featureDesignation.Et,
  637:                                 Lo = featureDesignation.Lo,
  638:                                 Mn = featureDesignation.Mn,
  639:                                 Mg = featureDesignation.Mg,
  640:                                 Ka = featureDesignation.Ka,
  641:                                 Sq = featureDesignation.Sq,
  642:                                 Mk = featureDesignation.Mk,
  643:                                 Hy = featureDesignation.Hy,
  644:                                 Tg = featureDesignation.Tg,
  645:                                 Ky = featureDesignation.Ky,
  646:                                 Is = featureDesignation.Is,
  647:                                 Mt = featureDesignation.Mt,
  648:                                 Ca = featureDesignation.Ca,
  649:  
  650:                                 Description = featureDesignation.Description,
  651:                                 Class = featureDesignation.Class,
  652:                                 Deleted = featureDesignation.Deleted,
  653:  
  654:                                 Created = featureDesignation.Created,
  655:                                 Updated = featureDesignation.Updated
  656:                             };
  657:  
  658:                             db.FeatureDesignations.Add(p);
  659:                             c++;
  660:                         }
  661:  
  662:                         db.SaveChanges();
  663:  
  664:                         result.AddSuccess("Records added: " + c + ". ");
  665:                     }
  666:                     else
  667:                     {
  668:                         result.AddWarning(recordCount + " records exist already. ");
  669:                     }
  670:                 }
  671:             }
  672:             catch (Exception ex)
  673:             {
  674:                 result.AddError(ex.ToString());
  675:             }
  676:         }
  677:  
  678:         ////////////////////////////////////////////////////////////////////////////
  679:  
  680:         /// <summary>
  681:         ///
  682:         /// </summary>
  683:         public static void DeleteFeatureDesignationRecordListFromDatabase(out Ia.Cl.Models.Result result)
  684:         {
  685:             result = new Ia.Cl.Models.Result();
  686:  
  687:             try
  688:             {
  689:                 using (var db = new Ia.TentPlay.Db())
  690:                 {
  691:                     if (!db.Database.EnsureCreated())
  692:                     {
  693:                         // already exists
  694:                         var all = from nfdc in db.FeatureDesignations select nfdc;
  695:                         db.FeatureDesignations.RemoveRange(all);
  696:                         db.SaveChanges();
  697:  
  698:                         result.AddSuccess("Records deleted. ");
  699:                     }
  700:                     else result.AddWarning("Database, tables, or records do not exist. ");
  701:                 }
  702:             }
  703:             catch (Exception ex)
  704:             {
  705:                 result.AddError(ex.ToString());
  706:             }
  707:         }
  708:  
  709:         ////////////////////////////////////////////////////////////////////////////
  710:  
  711:         /// <summary>
  712:         ///
  713:         /// </summary>
  714:         public static void RemoveUnNeededColumns(ref DataTable dataTable, List<Ia.Cl.Models.Language> countrySpokenLanguageList)
  715:         {
  716:             List<string> columnsToRemove;
  717:  
  718:             dataTable.Columns.Remove("Deleted");
  719:             dataTable.Columns.Remove("Description");
  720:             dataTable.Columns.Remove("Created");
  721:             dataTable.Columns.Remove("Updated");
  722:  
  723:             columnsToRemove = new List<string>();
  724:  
  725:             foreach (DataColumn column in dataTable.Columns)
  726:             {
  727:                 if (countrySpokenLanguageList.Find(u => u.Symbol == column.ColumnName.ToLower()) == null)
  728:                 {
  729:                     if (column.ColumnName.ToLower() != "id" && column.ColumnName.ToLower() != "class")
  730:                     {
  731:                         columnsToRemove.Add(column.ColumnName);
  732:                     }
  733:                 }
  734:             }
  735:  
  736:             if (columnsToRemove.Count > 0)
  737:             {
  738:                 foreach (string columnName in columnsToRemove)
  739:                 {
  740:                     dataTable.Columns.Remove(columnName);
  741:                 }
  742:             }
  743:         }
  744:  
  745:         ////////////////////////////////////////////////////////////////////////////
  746:         ////////////////////////////////////////////////////////////////////////////
  747:     }
  748: }