1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Data;
5: using System.Text.RegularExpressions;
6:
7: namespace Ia.Learning.Kanji.Model.Business
8: {
9: ////////////////////////////////////////////////////////////////////////////
10:
11: /// <summary publish="true">
12: /// Default business support class.
13: /// </summary>
14: /// <value>
15: /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
16: /// </value>
17: /// <remarks>
18: /// Copyright © 2008-2015 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 class Default
31: {
32: ////////////////////////////////////////////////////////////////////////////
33:
34: /// <summary>
35: ///
36: /// </summary>
37: public Default()
38: {
39: }
40:
41: ////////////////////////////////////////////////////////////////////////////
42:
43: /// <summary>
44: ///
45: /// </summary>
46: public static List<Ia.Learning.Cl.Model.Business.Kanji> Search(string searchText, out Ia.Cl.Model.Result result)
47: {
48: int strokeCount;
49: string s;
50: List<Ia.Learning.Cl.Model.Business.Kanji> list;
51:
52: list = null;
53: result = new Ia.Cl.Model.Result();
54:
55: // below: space characters from start and end
56: s = Regex.Replace(searchText, @"^\s+", "");
57: s = Regex.Replace(s, @"\s+$", "");
58: s = s.ToLower();
59:
60: // below: the regex checks the format of input:
61:
62: if (s != "")
63: {
64: if (Regex.IsMatch(s, @"^\w+$") && !Regex.IsMatch(s, @"\d+"))
65: {
66: if (s.Length > 20)
67: {
68: result.AddError("Input string " + s + " is invalid.");
69: }
70: else
71: {
72: try
73: {
74: // below: if word contains o|u|i we will add another that contains ō|ū|ī
75: // note: no need; SQL LIKE already does this
76: //u = s.Replace("o", "ō");
77: //if(u == s) u = s.Replace("u", "ū");
78: //if(u == s) u = s.Replace("i", "ī");
79:
80: list = (from q in Ia.Learning.Cl.Model.Data.Kanji.List
81: where q.Name.Contains(s)
82: || q.English.Contains(s)
83: || q.JapaneseOnyomi.Contains(s)
84: || q.EnglishOnyomi.Contains(s)
85: || q.JapaneseKunyomi.Contains(s)
86: || q.EnglishKunyomi.Contains(s)
87: select q).ToList();
88:
89: if (list.Count == 0)
90: {
91: result.AddWarning("No kanji were found that contain \"" + s + "\" in their related information.");
92: }
93: else
94: {
95: result.AddSuccess("Search found " + list.Count + " characters.");
96: }
97: }
98: catch (Exception ex)
99: {
100: result.AddError("Error during retrieval of data for \"" + s + "\": " + ex.Message);
101: }
102: }
103: }
104: else if (Regex.IsMatch(s, @"^\d{1,3}$"))
105: {
106: strokeCount = int.Parse(s);
107:
108: if (strokeCount == 0 || strokeCount > 50)
109: {
110: result.AddError("Stroke number " + strokeCount + " is invalid.");
111: }
112: else
113: {
114: try
115: {
116: list = (from k in Ia.Learning.Cl.Model.Data.Kanji.List where k.Stroke == strokeCount select k).ToList();
117:
118: if (list.Count == 0)
119: {
120: result.AddWarning("No kanji were found that require " + strokeCount + " strokes.");
121: }
122: else
123: {
124: result.AddSuccess("Search found " + list.Count + " characters.");
125: }
126: }
127: catch (Exception ex)
128: {
129: result.AddError("Error during retrieval of data for \"" + strokeCount + "\": " + ex.Message);
130: }
131: }
132: }
133: else
134: {
135: result.AddError("The input string \"" + s + "\" is not in a correct format.");
136: }
137: }
138: else
139: {
140: result.AddError("No input was entered.");
141: }
142:
143: return list;
144: }
145:
146: ////////////////////////////////////////////////////////////////////////////
147: ////////////////////////////////////////////////////////////////////////////
148: }
149: }