1: using System;
2: using System.Data;
3: using System.Collections.Generic;
4: using System.Linq;
5: using System.Text;
6: using System.Text.RegularExpressions;
7: using System.Collections;
8: using System.IO;
9:
10: namespace Ia.Ngn.Cl.Model.Nokia.Data
11: {
12: ////////////////////////////////////////////////////////////////////////////
13:
14: /// <summary publish="true">
15: /// Nokia AmsTransaction Entity Framework class for Next Generation Network (NGN) data model.
16: /// </summary>
17: ///
18: /// <remarks>
19: /// Copyright © 2006-2017 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 partial class AmsTransaction
32: {
33: ////////////////////////////////////////////////////////////////////////////
34:
35: /// <summary>
36: ///
37: /// </summary>
38: public AmsTransaction() { }
39:
40: ////////////////////////////////////////////////////////////////////////////
41: ////////////////////////////////////////////////////////////////////////////
42:
43: /// <summary>
44: ///
45: /// </summary>
46: public static void Process(StreamReader streamReader, out Ia.Cl.Model.Result result)
47: {
48: string streamLine;
49: StringBuilder text;
50: Ia.Cl.Model.Result parseResult, insertResult;
51: List<Ia.Ngn.Cl.Model.Nokia.AmsTransaction> amsTransactionList;
52:
53: result = new Ia.Cl.Model.Result();
54: text = new StringBuilder();
55:
56: //streamText = File.ReadAllText(file);
57: while ((streamLine = streamReader.ReadLine()) != null) text.Append(streamLine + "\r\n");
58:
59: Parse(text, out amsTransactionList, out parseResult);
60:
61: result.AddResult(parseResult);
62:
63: if (parseResult.IsSuccessful)
64: {
65: SqlBulkCopy(amsTransactionList, out insertResult);
66:
67: result.AddResult(insertResult);
68: }
69: }
70:
71: ////////////////////////////////////////////////////////////////////////////
72:
73: /// <summary>
74: ///
75: /// </summary>
76: public static void Process(string attachmentText, out Ia.Cl.Model.Result result)
77: {
78: StringBuilder text;
79: Ia.Cl.Model.Result parseResult, insertResult;
80: List<Ia.Ngn.Cl.Model.Nokia.AmsTransaction> amsTransactionList;
81:
82: result = new Ia.Cl.Model.Result();
83: text = new StringBuilder();
84:
85: text.Append(attachmentText);
86:
87: Parse(text, out amsTransactionList, out parseResult);
88:
89: result.AddResult(parseResult);
90:
91: if (parseResult.IsSuccessful)
92: {
93: SqlBulkCopy(amsTransactionList, out insertResult);
94:
95: result.AddResult(insertResult);
96: }
97: }
98:
99: ////////////////////////////////////////////////////////////////////////////
100:
101: /// <summary>
102: ///
103: /// </summary>
104: public static void Parse(StringBuilder text, out List<Ia.Ngn.Cl.Model.Nokia.AmsTransaction> amsTransactionList, out Ia.Cl.Model.Result result)
105: {
106: bool firstLineSkipped;
107: int lineCount, c;
108: string[] fieldList;
109: Ia.Ngn.Cl.Model.Nokia.AmsTransaction amsTransaction;
110: List<string> list = new List<string>();
111:
112: firstLineSkipped = false;
113:
114: result = new Ia.Cl.Model.Result();
115:
116: amsTransactionList = new List<Ia.Ngn.Cl.Model.Nokia.AmsTransaction>();
117:
118: //file = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\" + "log.csv";
119:
120: //Regex regex = new Regex("(.+?),,\r\n", RegexOptions.Compiled);
121:
122: //if (File.Exists(file))
123: //{
124:
125: // fixing errors in CSV file
126: text = text.Replace("\r\nnewValues", "newValues");
127: text = text.Replace("]\r\n", "]");
128: text = text.Replace("[\r\n", "[");
129:
130: //fileText = Regex.Replace(fileText, "[\r\n]+", "\r\n");
131: //fileText = fileText.Replace("\r\n\r\n", "__r__0__n__");
132: text = text.Replace("\r\n", "__r__0__n__");
133: text = text.Replace("\r", "");
134: text = text.Replace("\n", "");
135: text = text.Replace(Environment.NewLine, "");
136: text = text.Replace("__r__0__n__", "\r\n");
137:
138: lineCount = 0;
139:
140: foreach (string line in text.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
141: {
142: lineCount++;
143:
144: if (firstLineSkipped)
145: {
146: try
147: {
148: fieldList = new string[10];
149:
150: c = 0;
151: //fieldList = Regex.Split(line, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", RegexOptions.Singleline);
152: foreach (string s in Ia.Cl.Model.Default.CsvLineSplitter(line)) fieldList[c++] = s;
153:
154: if (fieldList.Length == 10)
155: {
156: amsTransaction = new Ia.Ngn.Cl.Model.Nokia.AmsTransaction()
157: {
158: Id = Math.Abs((int)Ia.Cl.Model.Default.TickSecondAndCountId(lineCount)), //.ToString(),
159: EventTime = DateTime.Parse(fieldList[0].Replace(@"""", "").Replace(" AST", "")),
160: User = fieldList[1],
161: Application = fieldList[2],
162: Operation = fieldList[3],
163: Agent = fieldList[4],
164: Object = fieldList[5],
165: Argument = fieldList[6],
166: Session = fieldList[7],
167: Result = fieldList[8],
168: };
169:
170: amsTransactionList.Add(amsTransaction);
171: }
172: else
173: {
174:
175: }
176: }
177: catch (Exception ex)
178: {
179: result.AddWarning("Exception: Parse(): " + ex.ToString() + ". Input line: " + line);
180: }
181: }
182: else firstLineSkipped = true;
183: }
184: }
185:
186: ////////////////////////////////////////////////////////////////////////////
187:
188: /// <summary>
189: ///
190: /// </summary>
191: public static void SqlBulkCopy(List<Ia.Ngn.Cl.Model.Nokia.AmsTransaction> amsTransactionList, out Ia.Cl.Model.Result result)
192: {
193: DataTable dt;
194: Ia.Cl.Model.Db.SqlServer sqlServer;
195:
196: result = new Ia.Cl.Model.Result();
197: sqlServer = new Ia.Cl.Model.Db.SqlServer();
198:
199: dt = Ia.Cl.Model.Default.GenerateDataTableFromGenericClassList<Ia.Ngn.Cl.Model.Nokia.AmsTransaction>(amsTransactionList, "AmsTransactions");
200:
201: sqlServer.SqlBulkCopy(dt, out result);
202: }
203:
204: ////////////////////////////////////////////////////////////////////////////
205: ////////////////////////////////////////////////////////////////////////////
206:
207: /// <summary>
208: ///
209: /// </summary>
210: public static bool Create(Ia.Ngn.Cl.Model.Nokia.AmsTransaction newItem, out int itemId)
211: {
212: bool b;
213:
214: b = false;
215:
216: using (var db = new Ia.Ngn.Cl.Model.Ngn())
217: {
218: db.AmsTransactions.Add(newItem);
219: db.SaveChanges();
220:
221: itemId = newItem.Id;
222:
223: b = true;
224: }
225:
226: return b;
227: }
228:
229: ////////////////////////////////////////////////////////////////////////////
230:
231: /// <summary>
232: ///
233: /// </summary>
234: public static Ia.Ngn.Cl.Model.Nokia.AmsTransaction Read(int id)
235: {
236: Ia.Ngn.Cl.Model.Nokia.AmsTransaction item;
237:
238: using (var db = new Ia.Ngn.Cl.Model.Ngn())
239: {
240: item = (from t in db.AmsTransactions where t.Id == id select t).SingleOrDefault();
241: }
242:
243: return item;
244: }
245:
246: ////////////////////////////////////////////////////////////////////////////
247:
248: /// <summary>
249: ///
250: /// </summary>
251: public static List<Ia.Ngn.Cl.Model.Nokia.AmsTransaction> ReadList()
252: {
253: List<Ia.Ngn.Cl.Model.Nokia.AmsTransaction> itemList;
254:
255: using (var db = new Ia.Ngn.Cl.Model.Ngn())
256: {
257: itemList = (from t in db.AmsTransactions select t).ToList();
258: }
259:
260: return itemList;
261: }
262:
263: ////////////////////////////////////////////////////////////////////////////
264: ////////////////////////////////////////////////////////////////////////////
265: }
266:
267: ////////////////////////////////////////////////////////////////////////////
268: ////////////////////////////////////////////////////////////////////////////
269: }