1: using Newtonsoft.Json;
2: using System;
3: using System.Collections.Generic;
4: using System.Linq;
5:
6: namespace Ia.Ngn.Cl.Model.Data
7: {
8: ////////////////////////////////////////////////////////////////////////////
9:
10: /// <summary publish="true">
11: /// Miscellaneous Entity Framework class for Next Generation Network (NGN) data model.
12: /// </summary>
13: ///
14: /// <remarks>
15: /// Copyright © 2006-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
16: ///
17: /// 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
18: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
19: ///
20: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
22: ///
23: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
24: ///
25: /// Copyright notice: This notice may not be removed or altered from any source distribution.
26: /// </remarks>
27: public partial class Miscellaneous
28: {
29: /// <summary/>
30: public Miscellaneous() { }
31:
32: ////////////////////////////////////////////////////////////////////////////
33:
34: /// <summary>
35: ///
36: /// </summary>
37: public static void Create(Ia.Ngn.Cl.Model.Miscellaneous item)
38: {
39: if (item != null)
40: {
41: using (var db = new Ia.Ngn.Cl.Model.Ngn())
42: {
43: item.Created = item.Updated = DateTime.UtcNow.AddHours(3);
44:
45: db.Miscellaneous.Add(item);
46: db.SaveChanges();
47: }
48: }
49: else throw new System.ArgumentOutOfRangeException("item is null");
50: }
51:
52: ////////////////////////////////////////////////////////////////////////////
53:
54: /// <summary>
55: ///
56: /// </summary>
57: public static void Create(string name, string value)
58: {
59: Ia.Ngn.Cl.Model.Miscellaneous misc;
60:
61: if (!string.IsNullOrEmpty(name))
62: {
63: misc = new Ia.Ngn.Cl.Model.Miscellaneous
64: {
65: Name = name,
66: Value = value
67: };
68:
69: using (var db = new Ia.Ngn.Cl.Model.Ngn())
70: {
71: misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
72:
73: db.Miscellaneous.Add(misc);
74: db.SaveChanges();
75: }
76: }
77: else throw new System.ArgumentOutOfRangeException("name is null or empty");
78: }
79:
80: ////////////////////////////////////////////////////////////////////////////
81:
82: /// <summary>
83: ///
84: /// </summary>
85: public static void Create(string name, bool flag)
86: {
87: Ia.Ngn.Cl.Model.Miscellaneous misc;
88:
89: if (!string.IsNullOrEmpty(name))
90: {
91: misc = new Ia.Ngn.Cl.Model.Miscellaneous
92: {
93: Name = name,
94: Flag = flag
95: };
96:
97: using (var db = new Ia.Ngn.Cl.Model.Ngn())
98: {
99: misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
100:
101: db.Miscellaneous.Add(misc);
102: db.SaveChanges();
103: }
104: }
105: else throw new System.ArgumentOutOfRangeException("name is null or empty");
106: }
107:
108: ////////////////////////////////////////////////////////////////////////////
109:
110: /// <summary>
111: ///
112: /// </summary>
113: public static Ia.Ngn.Cl.Model.Miscellaneous Read(long id)
114: {
115: Ia.Ngn.Cl.Model.Miscellaneous item;
116:
117: using (var db = new Ia.Ngn.Cl.Model.Ngn())
118: {
119: item = (from m in db.Miscellaneous where m.Id == id select m).SingleOrDefault();
120: }
121:
122: return item;
123: }
124:
125: ////////////////////////////////////////////////////////////////////////////
126:
127: /// <summary>
128: ///
129: /// </summary>
130: public static string Read(string name)
131: {
132: string value;
133:
134: if (!string.IsNullOrEmpty(name))
135: {
136: using (var db = new Ia.Ngn.Cl.Model.Ngn())
137: {
138: value = (from m in db.Miscellaneous where m.Name == name select m.Value).SingleOrDefault();
139: }
140: }
141: else throw new System.ArgumentOutOfRangeException("name is null or empty");
142:
143: return value;
144: }
145:
146: ////////////////////////////////////////////////////////////////////////////
147:
148: /// <summary>
149: ///
150: /// </summary>
151: public static T Read<T>(string name)
152: {
153: string json;
154: T t;
155:
156: if (!string.IsNullOrEmpty(name))
157: {
158: using (var db = new Ia.Ngn.Cl.Model.Ngn())
159: {
160: json = (from m in db.Miscellaneous where m.Name == name select m.Value).SingleOrDefault();
161:
162: if (json != null) t = JsonConvert.DeserializeObject<T>(json);
163: else t = default(T);
164: }
165: }
166: else throw new System.ArgumentOutOfRangeException("name is null or empty");
167:
168: return t;
169: }
170:
171: ////////////////////////////////////////////////////////////////////////////
172:
173: /// <summary>
174: ///
175: /// </summary>
176: public static DateTime ReadUpdatedDateTime(string name)
177: {
178: DateTime dateTime;
179:
180: if (!string.IsNullOrEmpty(name))
181: {
182: using (var db = new Ia.Ngn.Cl.Model.Ngn())
183: {
184: dateTime = (from m in db.Miscellaneous where m.Name == name select m.Updated).SingleOrDefault();
185: }
186: }
187: else throw new System.ArgumentOutOfRangeException("name is null or empty");
188:
189: return dateTime;
190: }
191:
192: ////////////////////////////////////////////////////////////////////////////
193:
194: /// <summary>
195: ///
196: /// </summary>
197: public static bool ReadFlag(string name)
198: {
199: bool flag;
200:
201: if (!string.IsNullOrEmpty(name))
202: {
203: using (var db = new Ia.Ngn.Cl.Model.Ngn())
204: {
205: flag = (from m in db.Miscellaneous where m.Name == name select m.Flag).SingleOrDefault();
206: }
207: }
208: else throw new System.ArgumentOutOfRangeException("name is null or empty");
209:
210: return flag;
211: }
212:
213: ////////////////////////////////////////////////////////////////////////////
214:
215: /// <summary>
216: ///
217: /// </summary>
218: public static List<Ia.Ngn.Cl.Model.Miscellaneous> ReadList()
219: {
220: List<Ia.Ngn.Cl.Model.Miscellaneous> itemList;
221:
222: using (var db = new Ia.Ngn.Cl.Model.Ngn())
223: {
224: itemList = (from m in db.Miscellaneous select m).ToList();
225: }
226:
227: return itemList;
228: }
229:
230: ////////////////////////////////////////////////////////////////////////////
231:
232: /// <summary>
233: ///
234: /// </summary>
235: public static void Update(Ia.Ngn.Cl.Model.Miscellaneous item)
236: {
237: if (item != null)
238: {
239: using (var db = new Ia.Ngn.Cl.Model.Ngn())
240: {
241: item = (from m in db.Miscellaneous where m.Id == item.Id select m).SingleOrDefault();
242:
243: item.Updated = DateTime.UtcNow.AddHours(3);
244:
245: db.Miscellaneous.Attach(item);
246:
247: db.Entry(item).State = System.Data.Entity.EntityState.Modified;
248: db.SaveChanges();
249: }
250: }
251: else throw new System.ArgumentOutOfRangeException("item is null");
252: }
253:
254: ////////////////////////////////////////////////////////////////////////////
255:
256: /// <summary>
257: ///
258: /// </summary>
259: public static void CreateOrUpdate(string name, string value)
260: {
261: Ia.Ngn.Cl.Model.Miscellaneous misc;
262:
263: if (!string.IsNullOrEmpty(name))
264: {
265: using (var db = new Ia.Ngn.Cl.Model.Ngn())
266: {
267: misc = (from m in db.Miscellaneous where m.Name == name select m).SingleOrDefault();
268:
269: if (misc == null)
270: {
271: misc = new Ia.Ngn.Cl.Model.Miscellaneous
272: {
273: Name = name,
274: Value = value
275: };
276: misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
277:
278: db.Miscellaneous.Add(misc);
279: }
280: else
281: {
282: misc.Updated = DateTime.UtcNow.AddHours(3);
283:
284: misc.Value = value;
285:
286: db.Miscellaneous.Attach(misc);
287:
288: db.Entry(misc).State = System.Data.Entity.EntityState.Modified;
289: }
290:
291: db.SaveChanges();
292: }
293: }
294: else throw new System.ArgumentOutOfRangeException("name is null or empty");
295: }
296:
297: ////////////////////////////////////////////////////////////////////////////
298:
299: /// <summary>
300: ///
301: /// </summary>
302: public static void CreateOrUpdate(string name, object value)
303: {
304: string json;
305: Ia.Ngn.Cl.Model.Miscellaneous misc;
306:
307: if (!string.IsNullOrEmpty(name))
308: {
309: json = JsonConvert.SerializeObject(value, Formatting.Indented);
310:
311: using (var db = new Ia.Ngn.Cl.Model.Ngn())
312: {
313: misc = (from m in db.Miscellaneous where m.Name == name select m).SingleOrDefault();
314:
315: if (misc == null)
316: {
317: misc = new Ia.Ngn.Cl.Model.Miscellaneous
318: {
319: Name = name,
320: Value = json
321: };
322: misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
323:
324: db.Miscellaneous.Add(misc);
325: }
326: else
327: {
328: misc.Updated = DateTime.UtcNow.AddHours(3);
329:
330: misc.Value = json;
331:
332: db.Miscellaneous.Attach(misc);
333:
334: db.Entry(misc).State = System.Data.Entity.EntityState.Modified;
335: }
336:
337: db.SaveChanges();
338: }
339: }
340: else throw new System.ArgumentOutOfRangeException("name is null or empty");
341: }
342:
343: ////////////////////////////////////////////////////////////////////////////
344:
345: /// <summary>
346: ///
347: /// </summary>
348: public static void CreateOrUpdateFlag(string name, bool flag)
349: {
350: Ia.Ngn.Cl.Model.Miscellaneous misc;
351:
352: if (!string.IsNullOrEmpty(name))
353: {
354: using (var db = new Ia.Ngn.Cl.Model.Ngn())
355: {
356: misc = (from m in db.Miscellaneous where m.Name == name select m).SingleOrDefault();
357:
358: if (misc == null)
359: {
360: misc = new Ia.Ngn.Cl.Model.Miscellaneous
361: {
362: Name = name,
363: Flag = flag
364: };
365: misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
366:
367: db.Miscellaneous.Add(misc);
368: }
369: else
370: {
371: misc.Updated = DateTime.UtcNow.AddHours(3);
372:
373: misc.Flag = flag;
374:
375: db.Miscellaneous.Attach(misc);
376:
377: db.Entry(misc).State = System.Data.Entity.EntityState.Modified;
378: }
379:
380: db.SaveChanges();
381: }
382: }
383: else throw new System.ArgumentOutOfRangeException("name is null or empty");
384: }
385:
386: ////////////////////////////////////////////////////////////////////////////
387:
388: /// <summary>
389: ///
390: /// </summary>
391: public static void SetFlag(string name)
392: {
393: CreateOrUpdateFlag(name, true);
394: }
395:
396: ////////////////////////////////////////////////////////////////////////////
397:
398: /// <summary>
399: ///
400: /// </summary>
401: public static void ResetFlag(string name)
402: {
403: CreateOrUpdateFlag(name, false);
404: }
405:
406: ////////////////////////////////////////////////////////////////////////////
407:
408: /// <summary>
409: ///
410: /// </summary>
411: public static void Delete(long id)
412: {
413: using (var db = new Ia.Ngn.Cl.Model.Ngn())
414: {
415: var v = (from m in db.Miscellaneous where m.Id == id select m).FirstOrDefault();
416:
417: db.Miscellaneous.Remove(v);
418: db.SaveChanges();
419: }
420: }
421:
422: ////////////////////////////////////////////////////////////////////////////
423: ////////////////////////////////////////////////////////////////////////////
424: }
425:
426: ////////////////////////////////////////////////////////////////////////////
427: ////////////////////////////////////////////////////////////////////////////
428: }