1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text.Json;
5:
6: namespace Ia.Ngn.Cl.Model.Data
7: {
8: ////////////////////////////////////////////////////////////////////////////
9:
10: /// <summary publish="true">
11: /// Miscellaneous Entity Framework class for Optical Fiber Network (OFN) 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 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 = JsonSerializer.Deserialize<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 = Microsoft.EntityFrameworkCore.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 = Microsoft.EntityFrameworkCore.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: var jsonSerializerOptions = new JsonSerializerOptions
310: {
311: WriteIndented = true
312: };
313:
314: json = JsonSerializer.Serialize(value, jsonSerializerOptions);
315:
316: using (var db = new Ia.Ngn.Cl.Model.Ngn())
317: {
318: misc = (from m in db.Miscellaneous where m.Name == name select m).SingleOrDefault();
319:
320: if (misc == null)
321: {
322: misc = new Ia.Ngn.Cl.Model.Miscellaneous
323: {
324: Name = name,
325: Value = json
326: };
327: misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
328:
329: db.Miscellaneous.Add(misc);
330: }
331: else
332: {
333: misc.Updated = DateTime.UtcNow.AddHours(3);
334:
335: misc.Value = json;
336:
337: db.Miscellaneous.Attach(misc);
338:
339: db.Entry(misc).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
340: }
341:
342: db.SaveChanges();
343: }
344: }
345: else throw new System.ArgumentOutOfRangeException("name is null or empty");
346: }
347:
348: ////////////////////////////////////////////////////////////////////////////
349:
350: /// <summary>
351: ///
352: /// </summary>
353: public static void CreateOrUpdateFlag(string name, bool flag)
354: {
355: Ia.Ngn.Cl.Model.Miscellaneous misc;
356:
357: if (!string.IsNullOrEmpty(name))
358: {
359: using (var db = new Ia.Ngn.Cl.Model.Ngn())
360: {
361: misc = (from m in db.Miscellaneous where m.Name == name select m).SingleOrDefault();
362:
363: if (misc == null)
364: {
365: misc = new Ia.Ngn.Cl.Model.Miscellaneous
366: {
367: Name = name,
368: Flag = flag
369: };
370: misc.Created = misc.Updated = DateTime.UtcNow.AddHours(3);
371:
372: db.Miscellaneous.Add(misc);
373: }
374: else
375: {
376: misc.Updated = DateTime.UtcNow.AddHours(3);
377:
378: misc.Flag = flag;
379:
380: db.Miscellaneous.Attach(misc);
381:
382: db.Entry(misc).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
383: }
384:
385: db.SaveChanges();
386: }
387: }
388: else throw new System.ArgumentOutOfRangeException("name is null or empty");
389: }
390:
391: ////////////////////////////////////////////////////////////////////////////
392:
393: /// <summary>
394: ///
395: /// </summary>
396: public static void SetFlag(string name)
397: {
398: CreateOrUpdateFlag(name, true);
399: }
400:
401: ////////////////////////////////////////////////////////////////////////////
402:
403: /// <summary>
404: ///
405: /// </summary>
406: public static void ResetFlag(string name)
407: {
408: CreateOrUpdateFlag(name, false);
409: }
410:
411: ////////////////////////////////////////////////////////////////////////////
412:
413: /// <summary>
414: ///
415: /// </summary>
416: public static void Delete(long id)
417: {
418: using (var db = new Ia.Ngn.Cl.Model.Ngn())
419: {
420: var v = (from m in db.Miscellaneous where m.Id == id select m).FirstOrDefault();
421:
422: db.Miscellaneous.Remove(v);
423: db.SaveChanges();
424: }
425: }
426:
427: ////////////////////////////////////////////////////////////////////////////
428: ////////////////////////////////////////////////////////////////////////////
429: }
430:
431: ////////////////////////////////////////////////////////////////////////////
432: ////////////////////////////////////////////////////////////////////////////
433: }