Client.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Timers;
  6. using InABox.Core;
  7. namespace InABox.Clients
  8. {
  9. public enum SerializerProtocol
  10. {
  11. Rest
  12. }
  13. public class QueryMultipleResults
  14. {
  15. private readonly Dictionary<string, CoreTable> Results;
  16. internal QueryMultipleResults(Dictionary<string, CoreTable> results)
  17. {
  18. Results = results;
  19. }
  20. public CoreTable this[string name] => Results[name];
  21. public CoreTable Get<T>() => Results[typeof(T).Name];
  22. public CoreTable Get(string name) => Results[name];
  23. }
  24. public abstract class Client
  25. {
  26. public abstract CoreTable Query(IFilter? filter = null, IColumns? columns = null, ISortOrder? sortOrder = null);
  27. public abstract void Save(Entity entity, string auditNote);
  28. public abstract void Save(IEnumerable<Entity> entity, string auditNote);
  29. private static IClient CheckClient()
  30. {
  31. using (new Profiler(true))
  32. return ClientFactory.CreateClient<User>();
  33. }
  34. public static Dictionary<string, CoreTable> QueryMultiple(Dictionary<string, IQueryDef> queries)
  35. {
  36. try
  37. {
  38. using var timer = new Profiler(false);
  39. var result = CheckClient().QueryMultiple(queries);
  40. timer.Log(result.Sum(x => x.Value.Rows.Count));
  41. return result;
  42. }
  43. catch (RequestException e)
  44. {
  45. ClientFactory.RaiseRequestError(e);
  46. throw;
  47. }
  48. }
  49. public static QueryMultipleResults QueryMultiple(params IKeyedQueryDef[] queries)
  50. {
  51. try
  52. {
  53. using var timer = new Profiler(false);
  54. var result = CheckClient().QueryMultiple(queries.ToDictionary(x => x.Key, x => x as IQueryDef));
  55. timer.Log(result.Sum(x => x.Value.Rows.Count));
  56. return new QueryMultipleResults(result);
  57. }
  58. catch (RequestException e)
  59. {
  60. ClientFactory.RaiseRequestError(e);
  61. throw;
  62. }
  63. }
  64. public static QueryMultipleResults QueryMultiple(IEnumerable<IKeyedQueryDef> queries)
  65. {
  66. try
  67. {
  68. using var timer = new Profiler(false);
  69. var result = CheckClient().QueryMultiple(queries.ToDictionary(x => x.Key, x => x as IQueryDef));
  70. timer.Log(result.Sum(x => x.Value.Rows.Count));
  71. return new QueryMultipleResults(result);
  72. }
  73. catch (RequestException e)
  74. {
  75. ClientFactory.RaiseRequestError(e);
  76. throw;
  77. }
  78. }
  79. public static IValidationData Validate(Guid session)
  80. {
  81. try
  82. {
  83. using (new Profiler(true))
  84. return CheckClient().Validate(session);
  85. }
  86. catch (RequestException e)
  87. {
  88. ClientFactory.RaiseRequestError(e);
  89. throw;
  90. }
  91. }
  92. public static IValidationData Validate(string pin, Guid session = default)
  93. {
  94. try
  95. {
  96. using (new Profiler(true))
  97. return CheckClient().Validate(pin, session);
  98. }
  99. catch (RequestException e)
  100. {
  101. ClientFactory.RaiseRequestError(e);
  102. throw;
  103. }
  104. }
  105. public static IValidationData Validate(string userid, string password, Guid session = default)
  106. {
  107. try
  108. {
  109. using (new Profiler(true))
  110. return CheckClient().Validate(userid, password, session);
  111. }
  112. catch (RequestException e)
  113. {
  114. ClientFactory.RaiseRequestError(e);
  115. throw;
  116. }
  117. }
  118. public static bool Check2FA(string code, Guid? session = null)
  119. {
  120. try
  121. {
  122. using (new Profiler(true))
  123. return CheckClient().Check2FA(code, session);
  124. }
  125. catch (RequestException e)
  126. {
  127. ClientFactory.RaiseRequestError(e);
  128. throw;
  129. }
  130. }
  131. public static bool Ping()
  132. {
  133. try
  134. {
  135. using (new Profiler(true))
  136. return CheckClient().Ping();
  137. }
  138. catch (RequestException e)
  139. {
  140. ClientFactory.RaiseRequestError(e);
  141. throw;
  142. }
  143. }
  144. public static DatabaseInfo Info()
  145. {
  146. try
  147. {
  148. using (new Profiler(true))
  149. return CheckClient().Info();
  150. }
  151. catch (RequestException e)
  152. {
  153. ClientFactory.RaiseRequestError(e);
  154. throw;
  155. }
  156. }
  157. public static Client Create(Type TEntity) =>
  158. (Activator.CreateInstance(typeof(Client<>).MakeGenericType(TEntity)) as Client)!;
  159. }
  160. public class Client<TEntity> : Client, IDisposable where TEntity : Entity, IPersistent, IRemotable, new()
  161. {
  162. private IClient<TEntity> _client;
  163. public Client()
  164. {
  165. _client = ClientFactory.CreateClient<TEntity>();
  166. }
  167. public void Dispose()
  168. {
  169. }
  170. private void CheckSupported()
  171. {
  172. if (!ClientFactory.IsSupported<TEntity>())
  173. throw new NotSupportedException(string.Format("{0} is not supported in this context", typeof(TEntity).EntityName()));
  174. }
  175. private string FilterToString(Filter<TEntity> filter)
  176. {
  177. return filter != null ? filter.AsOData() : "";
  178. }
  179. private string OrderToString(SortOrder<TEntity> order)
  180. {
  181. return order != null ? order.AsOData() : "";
  182. }
  183. public CoreTable Query(Filter<TEntity>? filter = null, Columns<TEntity>? columns = null, SortOrder<TEntity>? orderby = null)
  184. {
  185. try
  186. {
  187. using (var timer = new Profiler<TEntity>(false))
  188. {
  189. CheckSupported();
  190. var result = _client.Query(filter, columns, orderby);
  191. timer.Log(result.Rows.Count);
  192. return result;
  193. }
  194. }
  195. catch(RequestException e)
  196. {
  197. ClientFactory.RaiseRequestError(e);
  198. throw;
  199. }
  200. }
  201. public override CoreTable Query(IFilter? filter, IColumns? columns, ISortOrder? sortOrder)
  202. {
  203. return Query(filter as Filter<TEntity>, columns as Columns<TEntity>, sortOrder as SortOrder<TEntity>);
  204. }
  205. public void Query(Filter<TEntity>? filter, Columns<TEntity>? columns, SortOrder<TEntity>? sort, Action<CoreTable?, Exception?> callback)
  206. {
  207. try
  208. {
  209. var timer = new Profiler<TEntity>(false);
  210. CheckSupported();
  211. _client.Query(filter, columns, sort, (c, e) =>
  212. {
  213. timer.Dispose(c != null ? c.Rows.Count : -1);
  214. callback?.Invoke(c, e);
  215. });
  216. }
  217. catch (RequestException e)
  218. {
  219. ClientFactory.RaiseRequestError(e);
  220. throw;
  221. }
  222. }
  223. public TEntity[] Load(Filter<TEntity>? filter = null, SortOrder<TEntity>? sort = null)
  224. {
  225. try
  226. {
  227. using (var timer = new Profiler<TEntity>(false))
  228. {
  229. CheckSupported();
  230. var result = _client.Load(filter, sort);
  231. foreach (var entity in result)
  232. entity.CommitChanges();
  233. timer.Log(result.Length);
  234. return result;
  235. }
  236. }
  237. catch (RequestException e)
  238. {
  239. ClientFactory.RaiseRequestError(e);
  240. throw;
  241. }
  242. }
  243. public void Load(Filter<TEntity> filter, SortOrder<TEntity> sort, Action<TEntity[]?, Exception?> callback)
  244. {
  245. try
  246. {
  247. var timer = new Profiler<TEntity>(false);
  248. CheckSupported();
  249. _client.Load(filter, sort, (i, e) =>
  250. {
  251. timer.Dispose(i != null ? i.Length : -1);
  252. callback?.Invoke(i, e);
  253. });
  254. }
  255. catch (RequestException e)
  256. {
  257. ClientFactory.RaiseRequestError(e);
  258. throw;
  259. }
  260. }
  261. public override void Save(Entity entity, string auditNote)
  262. {
  263. try
  264. {
  265. Save((entity as TEntity)!, auditNote);
  266. }
  267. catch (RequestException e)
  268. {
  269. ClientFactory.RaiseRequestError(e);
  270. throw;
  271. }
  272. }
  273. public override void Save(IEnumerable<Entity> entities, string auditNote)
  274. {
  275. try
  276. {
  277. Save(entities.Cast<TEntity>(), auditNote);
  278. }
  279. catch (RequestException e)
  280. {
  281. ClientFactory.RaiseRequestError(e);
  282. throw;
  283. }
  284. }
  285. public void Save(TEntity entity, string auditnote)
  286. {
  287. try
  288. {
  289. using (new Profiler<TEntity>(true))
  290. {
  291. CheckSupported();
  292. entity.LastUpdate = DateTime.Now;
  293. entity.LastUpdateBy = ClientFactory.UserID;
  294. _client.Save(entity, auditnote);
  295. entity.CommitChanges();
  296. }
  297. }
  298. catch (RequestException e)
  299. {
  300. ClientFactory.RaiseRequestError(e);
  301. throw;
  302. }
  303. }
  304. public void Save(TEntity entity, string auditnote, Action<TEntity, Exception?> callback)
  305. {
  306. try
  307. {
  308. var timer = new Profiler<TEntity>(false);
  309. CheckSupported();
  310. _client.Save(entity, auditnote, (i, c) =>
  311. {
  312. timer.Dispose();
  313. callback?.Invoke(i, c);
  314. });
  315. }
  316. catch (RequestException e)
  317. {
  318. ClientFactory.RaiseRequestError(e);
  319. throw;
  320. }
  321. }
  322. public void Save(IEnumerable<TEntity> entities, string auditnote)
  323. {
  324. try
  325. {
  326. using var timer = new Profiler<TEntity>(false);
  327. CheckSupported();
  328. if (entities.Any())
  329. _client.Save(entities, auditnote);
  330. timer.Log(entities.Count());
  331. }
  332. catch (RequestException e)
  333. {
  334. ClientFactory.RaiseRequestError(e);
  335. throw;
  336. }
  337. }
  338. public void Save(IEnumerable<TEntity> entities, string auditnote, Action<IEnumerable<TEntity>, Exception?> callback)
  339. {
  340. try
  341. {
  342. var timer = new Profiler<TEntity>(false);
  343. CheckSupported();
  344. if (entities.Any())
  345. _client.Save(entities, auditnote, (i, e) =>
  346. {
  347. timer.Dispose(i.Count());
  348. callback?.Invoke(i, e);
  349. });
  350. }
  351. catch (RequestException e)
  352. {
  353. ClientFactory.RaiseRequestError(e);
  354. throw;
  355. }
  356. }
  357. public void Delete(TEntity entity, string auditnote)
  358. {
  359. try
  360. {
  361. using (new Profiler<TEntity>(true))
  362. {
  363. CheckSupported();
  364. _client.Delete(entity, auditnote);
  365. }
  366. }
  367. catch (RequestException e)
  368. {
  369. ClientFactory.RaiseRequestError(e);
  370. throw;
  371. }
  372. }
  373. public void Delete(TEntity entity, string auditnote, Action<TEntity, Exception?> callback)
  374. {
  375. try
  376. {
  377. var timer = new Profiler<TEntity>(true);
  378. CheckSupported();
  379. _client.Delete(entity, auditnote, (i, e) =>
  380. {
  381. timer.Dispose();
  382. callback?.Invoke(i, e);
  383. });
  384. }
  385. catch (RequestException e)
  386. {
  387. ClientFactory.RaiseRequestError(e);
  388. throw;
  389. }
  390. }
  391. public void Delete(IList<TEntity> entities, string auditnote)
  392. {
  393. try
  394. {
  395. using var timer = new Profiler<TEntity>(false);
  396. CheckSupported();
  397. _client.Delete(entities, auditnote);
  398. timer.Log(entities.Count());
  399. }
  400. catch (RequestException e)
  401. {
  402. ClientFactory.RaiseRequestError(e);
  403. throw;
  404. }
  405. }
  406. public void Delete(IList<TEntity> entities, string auditnote, Action<IList<TEntity>, Exception?> callback)
  407. {
  408. try
  409. {
  410. var timer = new Profiler<TEntity>(false);
  411. CheckSupported();
  412. _client.Delete(entities, auditnote, (i, e) =>
  413. {
  414. timer.Dispose(entities.Count());
  415. callback?.Invoke(i, e);
  416. });
  417. }
  418. catch (RequestException e)
  419. {
  420. ClientFactory.RaiseRequestError(e);
  421. throw;
  422. }
  423. }
  424. public IEnumerable<string> SupportedTypes()
  425. {
  426. try
  427. {
  428. using (new Profiler(true))
  429. return _client.SupportedTypes();
  430. }
  431. catch (RequestException e)
  432. {
  433. ClientFactory.RaiseRequestError(e);
  434. throw;
  435. }
  436. }
  437. public new DatabaseInfo Info()
  438. {
  439. try
  440. {
  441. using (new Profiler(true))
  442. return _client.Info();
  443. }
  444. catch (RequestException e)
  445. {
  446. ClientFactory.RaiseRequestError(e);
  447. throw;
  448. }
  449. }
  450. }
  451. }