1: using System.Data;
2:
3: namespace Ia.TentPlay.Cl.Model.Memorise
4: {
5: ////////////////////////////////////////////////////////////////////////////
6:
7: /// <summary publish="true">
8: /// Default class for TentPlay Memorise data model
9: /// </summary>
10: ///
11: /// <remarks>
12: /// Copyright © 2006-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
13: ///
14: /// 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
15: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
16: ///
17: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19: ///
20: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
21: ///
22: /// Copyright notice: This notice may not be removed or altered from any source distribution.
23: /// </remarks>
24: public static class Default
25: {
26: ////////////////////////////////////////////////////////////////////////////
27:
28: /// <summary>
29: ///
30: /// </summary>
31: public static void GenerateSpecificApplicationSqliteDatabaseFileFromSqlServer(Ia.TentPlay.Cl.Model.Memorise.ApplicationInformation.Application application, out Ia.Cl.Model.Result result)
32: {
33: int count;
34: string fileName, sql, filePath, scalar;
35: DataTable dataTable;
36: FileInfo file;
37: Ia.Cl.Model.Db.Sqlite sqlite;
38: List<Ia.TentPlay.Cl.Model.Memorise.Score> scoreList;
39:
40: fileName = Ia.TentPlay.Cl.Model.Memorise.Default.SqliteDatabaseFileName(application.Id, application.VersionName);
41:
42: filePath = global::Ia.Cl.Model.Default.AbsolutePath(true) + fileName;
43:
44: file = new FileInfo(filePath);
45:
46: if (file.Exists)
47: {
48: // delete file if it exists
49: File.Delete(filePath);
50: }
51:
52: sqlite = new Ia.Cl.Model.Db.Sqlite(filePath); //, filePassword);
53:
54: // below: drop table if exists
55: sql = "drop table if exists scores"; // first because of foreign keys
56: sqlite.Sql(sql);
57:
58: // regular table
59: sql = Ia.TentPlay.Cl.Model.Memorise.Default.SqliteDatabaseFileCreateScoresTableString(false);
60: sqlite.Sql(sql);
61:
62: // You can not use index with virtual tables
63: //sqlite.Sql("create index if not exists noDiacriticLowerCaseNameIndex on features (noDiacriticLowerCaseName)");
64:
65: scoreList = Ia.TentPlay.Cl.Model.Memorise.Score.List(application.TestTopic, out result);
66:
67: dataTable = Ia.Cl.Model.Default.GenerateDataTableFromGenericClassList<Ia.TentPlay.Cl.Model.Memorise.Score>(scoreList, "Scores");
68:
69: sqlite.SqlBulkCopy(dataTable, out result);
70:
71: sql = "select count(0) from scores";
72: scalar = sqlite.Scalar(sql);
73:
74: if (!string.IsNullOrEmpty(scalar))
75: {
76: count = int.Parse(scalar);
77:
78: if (count == scoreList.Count)
79: {
80: result.AddSuccess("Score count: " + count + ". ");
81: }
82: }
83: else
84: {
85: result.AddError("dt is null or dt.Rows.Count == 0 for Scores. ");
86: }
87: }
88:
89: ////////////////////////////////////////////////////////////////////////////
90:
91: /// <summary>
92: /// Define the SQLite database file name according to the application file name
93: /// </summary>
94: public static string SqliteDatabaseFileName(string applicationId, string applicationVersion)
95: {
96: return applicationId + "." + applicationVersion + ".sqlite";
97: }
98:
99: ////////////////////////////////////////////////////////////////////////////
100:
101: /// <summary>
102: ///
103: /// </summary>
104: public static string SqliteDatabaseFileCreateScoresTableString(bool createVirtualTable)
105: {
106: string sql, entryList;
107:
108: // for Andriod, the database tables should use the identifier _id for the primary key of the table. Several Android functions rely on this standard.
109: // note the "Designation_Id" because Designation is an EF entity represented here by text Designation_Id
110:
111: 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";
112:
113: if (createVirtualTable) sql = @"create virtual table scoresFts using fts3(" + entryList + ")";
114: else sql = @"create table scores (" + entryList + ")";
115:
116: return sql;
117: }
118:
119: ////////////////////////////////////////////////////////////////////////////
120:
121: /// <summary>
122: ///
123: /// </summary>
124: public static void DownloadSpecificApplicationSqliteDatabaseFileFromTempFolder(Ia.TentPlay.Cl.Model.Memorise.ApplicationInformation.Application application, /*System.Web.UI.Page page,*/ out Ia.Cl.Model.Result result)
125: {
126: string fileName, filePath;
127: FileInfo file;
128:
129: result = new Ia.Cl.Model.Result();
130:
131: fileName = Ia.TentPlay.Cl.Model.Memorise.Default.SqliteDatabaseFileName(application.Id, application.VersionName);
132:
133: filePath = global::Ia.Cl.Model.Default.AbsolutePath(true) + fileName;
134:
135: file = new FileInfo(filePath);
136:
137: if (file.Exists)
138: {
139: /*
140: page.Response.Clear();
141: page.Response.ClearHeaders();
142: page.Response.ClearContent();
143: page.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
144: page.Response.AddHeader("Content-Length", file.Length.ToString());
145: page.Response.ContentType = "text/plain";
146: page.Response.Flush();
147: page.Response.TransmitFile(file.FullName);
148: page.Response.End();
149: */
150:
151: result.AddSuccess("Download complete. ");
152: }
153: else
154: {
155: result.AddError("Download file does not exists. ");
156: }
157: }
158:
159: ////////////////////////////////////////////////////////////////////////////
160: ////////////////////////////////////////////////////////////////////////////
161: }
162: }