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.Memorise
11: {
12: ////////////////////////////////////////////////////////////////////////////
13:
14: /// <summary publish="true">
15: /// Default class for TentPlay Memorise data model
16: /// </summary>
17: ///
18: /// <remarks>
19: /// Copyright © 2006-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated 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: /// <summary>
36: ///
37: /// </summary>
38: public static void GenerateSpecificApplicationSqliteDatabaseFileFromSqlServer(Ia.TentPlay.Cl.Model.Memorise.ApplicationInformation.Application application, out Ia.Cl.Model.Result result)
39: {
40: int count;
41: string fileName, sql, filePath, scalar;
42: DataTable dataTable;
43: FileInfo file;
44: Ia.Cl.Model.Db.Sqlite sqlite;
45: List<Ia.TentPlay.Cl.Model.Memorise.Score> scoreList;
46:
47: fileName = Ia.TentPlay.Cl.Model.Memorise.Default.SqliteDatabaseFileName(application.Id, application.VersionName);
48:
49: filePath = global::Ia.Cl.Model.Default.AbsolutePath(true) + fileName;
50:
51: file = new FileInfo(filePath);
52:
53: if (file.Exists)
54: {
55: // delete file if it exists
56: File.Delete(filePath);
57: }
58:
59: sqlite = new Ia.Cl.Model.Db.Sqlite(filePath); //, filePassword);
60:
61: // below: drop table if exists
62: sql = "drop table if exists scores"; // first because of foreign keys
63: sqlite.Sql(sql);
64:
65: // regular table
66: sql = Ia.TentPlay.Cl.Model.Memorise.Default.SqliteDatabaseFileCreateScoresTableString(false);
67: sqlite.Sql(sql);
68:
69: // You can not use index with virtual tables
70: //sqlite.Sql("create index if not exists noDiacriticLowerCaseNameIndex on features (noDiacriticLowerCaseName)");
71:
72: scoreList = Ia.TentPlay.Cl.Model.Memorise.Score.List(application.TestTopic, out result);
73:
74: dataTable = Ia.Cl.Model.Default.GenerateDataTableFromGenericClassList<Ia.TentPlay.Cl.Model.Memorise.Score>(scoreList, "Scores");
75:
76: sqlite.SqlBulkCopy(dataTable, out result);
77:
78: sql = "select count(0) from scores";
79: scalar = sqlite.Scalar(sql);
80:
81: if (!string.IsNullOrEmpty(scalar))
82: {
83: count = int.Parse(scalar);
84:
85: if (count == scoreList.Count)
86: {
87: result.AddSuccess("Score count: " + count + ". ");
88: }
89: }
90: else
91: {
92: result.AddError("dt is null or dt.Rows.Count == 0 for Scores. ");
93: }
94: }
95:
96: ////////////////////////////////////////////////////////////////////////////
97:
98: /// <summary>
99: /// Define the SQLite database file name according to the application file name
100: /// </summary>
101: public static string SqliteDatabaseFileName(string applicationId, string applicationVersion)
102: {
103: return applicationId + "." + applicationVersion + ".sqlite";
104: }
105:
106: ////////////////////////////////////////////////////////////////////////////
107:
108: /// <summary>
109: ///
110: /// </summary>
111: public static string SqliteDatabaseFileCreateScoresTableString(bool createVirtualTable)
112: {
113: string sql, entryList;
114:
115: // for Andriod, the database tables should use the identifier _id for the primary key of the table. Several Android functions rely on this standard.
116: // note the "Designation_Id" because Designation is an EF entity represented here by text Designation_Id
117:
118: entryList = "_id int primary key, testId int, typeId int, question text, answer text, numberOfTimesAsked int, cumulativeAnswerCorrectnessIndicator int, numberOfConsecutiveCorrects int, created dateTime, updated dateTime, viewed dateTime";
119:
120: if (createVirtualTable) sql = @"create virtual table scoresFts using fts3(" + entryList + ")";
121: else sql = @"create table scores (" + entryList + ")";
122:
123: return sql;
124: }
125:
126: ////////////////////////////////////////////////////////////////////////////
127:
128: /// <summary>
129: ///
130: /// </summary>
131: public static void DownloadSpecificApplicationSqliteDatabaseFileFromTempFolder(Ia.TentPlay.Cl.Model.Memorise.ApplicationInformation.Application application, System.Web.UI.Page page, out Ia.Cl.Model.Result result)
132: {
133: string fileName, filePath;
134: FileInfo file;
135:
136: result = new Ia.Cl.Model.Result();
137:
138: fileName = Ia.TentPlay.Cl.Model.Memorise.Default.SqliteDatabaseFileName(application.Id, application.VersionName);
139:
140: filePath = global::Ia.Cl.Model.Default.AbsolutePath(true) + fileName;
141:
142: file = new FileInfo(filePath);
143:
144: if (file.Exists)
145: {
146: page.Response.Clear();
147: page.Response.ClearHeaders();
148: page.Response.ClearContent();
149: page.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
150: page.Response.AddHeader("Content-Length", file.Length.ToString());
151: page.Response.ContentType = "text/plain";
152: page.Response.Flush();
153: page.Response.TransmitFile(file.FullName);
154: page.Response.End();
155:
156: result.AddSuccess("Download complete. ");
157: }
158: else
159: {
160: result.AddError("Download file does not exists. ");
161: }
162: }
163:
164: ////////////////////////////////////////////////////////////////////////////
165: ////////////////////////////////////////////////////////////////////////////
166: }
167: }