Client.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. RPC
  13. }
  14. public class QueryMultipleResults
  15. {
  16. private readonly Dictionary<string, CoreTable> Results;
  17. internal QueryMultipleResults(Dictionary<string, CoreTable> results)
  18. {
  19. Results = results;
  20. }
  21. public CoreTable this[string name] => Results[name];
  22. public CoreTable Get<T>() => Results[typeof(T).Name];
  23. /// <summary>
  24. /// Like <see cref="Get{T}"/>, but calls <see cref="CoreTable.ToObjects{T}"/> on the table.
  25. /// </summary>
  26. /// <typeparam name="T"></typeparam>
  27. /// <returns></returns>
  28. public IEnumerable<T> GetObjects<T>()
  29. where T: BaseObject, new()
  30. => Results[typeof(T).Name].ToObjects<T>();
  31. public CoreTable Get(string name) => Results[name];
  32. }
  33. public abstract class Client
  34. {
  35. public abstract CoreTable Query(IFilter? filter = null, IColumns? columns = null, ISortOrder? sortOrder = null);
  36. public abstract void Save(Entity entity, string auditNote);
  37. public abstract void Save(IEnumerable<Entity> entity, string auditNote);
  38. private static IClient CheckClient()
  39. {
  40. using (new Profiler(true))
  41. return ClientFactory.CreateClient<User>();
  42. }
  43. public static Dictionary<string, CoreTable> QueryMultiple(Dictionary<string, IQueryDef> queries)
  44. {
  45. try
  46. {
  47. using var timer = new Profiler(false);
  48. var result = CheckClient().QueryMultiple(queries);
  49. timer.Log(result.Sum(x => x.Value.Rows.Count));
  50. return result;
  51. }
  52. catch (RequestException e)
  53. {
  54. ClientFactory.RaiseRequestError(e);
  55. throw;
  56. }
  57. }
  58. private static IClient<TEntity> CheckClient<TEntity>() where TEntity : Entity, IRemotable, IPersistent, new()
  59. {
  60. return ClientFactory.CreateClient<TEntity>();
  61. }
  62. public static CoreTable Query<TEntity>(Filter<TEntity>? filter = null, Columns<TEntity>? columns = null, SortOrder<TEntity>? orderby = null)
  63. where TEntity : Entity, IRemotable, IPersistent, new()
  64. {
  65. return new Client<TEntity>().Query(filter, columns, orderby);
  66. }
  67. public static void Query<TEntity>(Filter<TEntity>? filter, Columns<TEntity>? columns, SortOrder<TEntity>? orderby, Action<CoreTable?, Exception?> callback)
  68. where TEntity : Entity, IRemotable, IPersistent, new()
  69. {
  70. new Client<TEntity>().Query(filter, columns, orderby, callback);
  71. }
  72. public static void Save<TEntity>(TEntity entity, string auditNote)
  73. where TEntity : Entity, IRemotable, IPersistent, new()
  74. {
  75. new Client<TEntity>().Save(entity, auditNote);
  76. }
  77. public static void Save<TEntity>(IEnumerable<TEntity> entities, string auditNote)
  78. where TEntity : Entity, IRemotable, IPersistent, new()
  79. {
  80. new Client<TEntity>().Save(entities, auditNote);
  81. }
  82. public static void Save<TEntity>(TEntity entity, string auditNote, Action<TEntity, Exception?> callback)
  83. where TEntity : Entity, IRemotable, IPersistent, new()
  84. {
  85. new Client<TEntity>().Save(entity, auditNote, callback);
  86. }
  87. public static void Save<TEntity>(IEnumerable<TEntity> entities, string auditNote, Action<IEnumerable<TEntity>, Exception?> callback)
  88. where TEntity : Entity, IRemotable, IPersistent, new()
  89. {
  90. new Client<TEntity>().Save(entities, auditNote, callback);
  91. }
  92. public static void Delete<TEntity>(TEntity entity, string auditNote)
  93. where TEntity : Entity, IRemotable, IPersistent, new()
  94. {
  95. new Client<TEntity>().Delete(entity, auditNote);
  96. }
  97. public static void Delete<TEntity>(IEnumerable<TEntity> entities, string auditNote)
  98. where TEntity : Entity, IRemotable, IPersistent, new()
  99. {
  100. new Client<TEntity>().Delete(entities, auditNote);
  101. }
  102. public static void QueryMultiple(
  103. Action<Dictionary<string, CoreTable>?, Exception?> callback,
  104. Dictionary<string, IQueryDef> queries)
  105. {
  106. try
  107. {
  108. using var timer = new Profiler(false);
  109. CheckClient().QueryMultiple((result, e) =>
  110. {
  111. timer.Dispose(result != null ? result.Sum(x => x.Value.Rows.Count) : -1);
  112. callback?.Invoke(result, e);
  113. }, queries);
  114. }
  115. catch (RequestException e)
  116. {
  117. ClientFactory.RaiseRequestError(e);
  118. throw;
  119. }
  120. }
  121. public static QueryMultipleResults QueryMultiple(params IKeyedQueryDef[] queries) =>
  122. new QueryMultipleResults(QueryMultiple(queries.ToDictionary(x => x.Key, x => x as IQueryDef)));
  123. public static void QueryMultiple(Action<QueryMultipleResults?, Exception?> callback, params IKeyedQueryDef[] queries) =>
  124. QueryMultiple((results, e) =>
  125. {
  126. if (results != null)
  127. {
  128. callback?.Invoke(new QueryMultipleResults(results), e);
  129. }
  130. else
  131. {
  132. callback?.Invoke(null, e);
  133. }
  134. }, queries.ToDictionary(x => x.Key, x => x as IQueryDef));
  135. public static QueryMultipleResults QueryMultiple(IEnumerable<IKeyedQueryDef> queries) =>
  136. new QueryMultipleResults(QueryMultiple(queries.ToDictionary(x => x.Key, x => x as IQueryDef)));
  137. public static void QueryMultiple(Action<QueryMultipleResults?, Exception?> callback, IEnumerable<IKeyedQueryDef> queries) =>
  138. QueryMultiple((results, e) =>
  139. {
  140. if(results != null)
  141. {
  142. callback?.Invoke(new QueryMultipleResults(results), e);
  143. }
  144. else
  145. {
  146. callback?.Invoke(null, e);
  147. }
  148. }, queries.ToDictionary(x => x.Key, x => x as IQueryDef));
  149. public static IValidationData Validate(Guid session)
  150. {
  151. try
  152. {
  153. using (new Profiler(true))
  154. return CheckClient().Validate(session);
  155. }
  156. catch (RequestException e)
  157. {
  158. ClientFactory.RaiseRequestError(e);
  159. throw;
  160. }
  161. }
  162. public static IValidationData Validate(string pin, Guid session = default)
  163. {
  164. try
  165. {
  166. using (new Profiler(true))
  167. return CheckClient().Validate(pin, session);
  168. }
  169. catch (RequestException e)
  170. {
  171. ClientFactory.RaiseRequestError(e);
  172. throw;
  173. }
  174. }
  175. public static IValidationData Validate(string userid, string password, Guid session = default)
  176. {
  177. try
  178. {
  179. using (new Profiler(true))
  180. return CheckClient().Validate(userid, password, session);
  181. }
  182. catch (RequestException e)
  183. {
  184. ClientFactory.RaiseRequestError(e);
  185. throw;
  186. }
  187. }
  188. public static bool Check2FA(string code, Guid? session = null)
  189. {
  190. try
  191. {
  192. using (new Profiler(true))
  193. return CheckClient().Check2FA(code, session);
  194. }
  195. catch (RequestException e)
  196. {
  197. ClientFactory.RaiseRequestError(e);
  198. throw;
  199. }
  200. }
  201. public static bool Ping()
  202. {
  203. try
  204. {
  205. using (new Profiler(true))
  206. return CheckClient().Ping();
  207. }
  208. catch (RequestException e)
  209. {
  210. ClientFactory.RaiseRequestError(e);
  211. throw;
  212. }
  213. }
  214. public static DatabaseInfo Info()
  215. {
  216. try
  217. {
  218. using (new Profiler(true))
  219. return CheckClient().Info();
  220. }
  221. catch (RequestException e)
  222. {
  223. ClientFactory.RaiseRequestError(e);
  224. throw;
  225. }
  226. }
  227. public static string Version()
  228. {
  229. try
  230. {
  231. using (new Profiler(true))
  232. return CheckClient().Version();
  233. }
  234. catch (RequestException e)
  235. {
  236. ClientFactory.RaiseRequestError(e);
  237. throw;
  238. }
  239. }
  240. public static string ReleaseNotes()
  241. {
  242. try
  243. {
  244. using (new Profiler(true))
  245. return CheckClient().ReleaseNotes();
  246. }
  247. catch (RequestException e)
  248. {
  249. ClientFactory.RaiseRequestError(e);
  250. throw;
  251. }
  252. }
  253. public static byte[]? Installer()
  254. {
  255. try
  256. {
  257. using (new Profiler(true))
  258. return CheckClient().Installer();
  259. }
  260. catch (RequestException e)
  261. {
  262. ClientFactory.RaiseRequestError(e);
  263. throw;
  264. }
  265. }
  266. public static Client Create(Type TEntity) =>
  267. (Activator.CreateInstance(typeof(Client<>).MakeGenericType(TEntity)) as Client)!;
  268. }
  269. public class Client<TEntity> : Client, IDisposable where TEntity : Entity, IPersistent, IRemotable, new()
  270. {
  271. private IClient<TEntity> _client;
  272. public Client()
  273. {
  274. _client = ClientFactory.CreateClient<TEntity>();
  275. }
  276. public void Dispose()
  277. {
  278. }
  279. private void CheckSupported()
  280. {
  281. if (!ClientFactory.IsSupported<TEntity>())
  282. throw new NotSupportedException(string.Format("{0} is not supported in this context", typeof(TEntity).EntityName()));
  283. }
  284. private string FilterToString(Filter<TEntity> filter)
  285. {
  286. return filter != null ? filter.AsOData() : "";
  287. }
  288. private string OrderToString(SortOrder<TEntity> order)
  289. {
  290. return order != null ? order.AsOData() : "";
  291. }
  292. public CoreTable Query(Filter<TEntity>? filter = null, Columns<TEntity>? columns = null, SortOrder<TEntity>? orderby = null)
  293. {
  294. try
  295. {
  296. using var timer = new Profiler<TEntity>(false);
  297. CheckSupported();
  298. var result = _client.Query(filter, columns, orderby);
  299. timer.Log(result.Rows.Count);
  300. return result;
  301. }
  302. catch(RequestException e)
  303. {
  304. ClientFactory.RaiseRequestError(e);
  305. throw;
  306. }
  307. }
  308. public override CoreTable Query(IFilter? filter, IColumns? columns, ISortOrder? sortOrder)
  309. {
  310. return Query(filter as Filter<TEntity>, columns as Columns<TEntity>, sortOrder as SortOrder<TEntity>);
  311. }
  312. public void Query(Filter<TEntity>? filter, Columns<TEntity>? columns, SortOrder<TEntity>? sort, Action<CoreTable?, Exception?> callback)
  313. {
  314. try
  315. {
  316. var timer = new Profiler<TEntity>(false);
  317. CheckSupported();
  318. _client.Query(filter, columns, sort, (c, e) =>
  319. {
  320. timer.Log(c != null ? c.Rows.Count : -1);
  321. callback?.Invoke(c, e);
  322. });
  323. }
  324. catch (RequestException e)
  325. {
  326. ClientFactory.RaiseRequestError(e);
  327. throw;
  328. }
  329. }
  330. public TEntity[] Load(Filter<TEntity>? filter = null, SortOrder<TEntity>? sort = null)
  331. {
  332. try
  333. {
  334. using (var timer = new Profiler<TEntity>(false))
  335. {
  336. CheckSupported();
  337. var result = _client.Load(filter, sort);
  338. foreach (var entity in result)
  339. entity.CommitChanges();
  340. timer.Log(result.Length);
  341. return result;
  342. }
  343. }
  344. catch (RequestException e)
  345. {
  346. ClientFactory.RaiseRequestError(e);
  347. throw;
  348. }
  349. }
  350. public void Load(Filter<TEntity> filter, SortOrder<TEntity> sort, Action<TEntity[]?, Exception?> callback)
  351. {
  352. try
  353. {
  354. var timer = new Profiler<TEntity>(false);
  355. CheckSupported();
  356. _client.Load(filter, sort, (i, e) =>
  357. {
  358. timer.Dispose(i != null ? i.Length : -1);
  359. callback?.Invoke(i, e);
  360. });
  361. }
  362. catch (RequestException e)
  363. {
  364. ClientFactory.RaiseRequestError(e);
  365. throw;
  366. }
  367. }
  368. public override void Save(Entity entity, string auditNote)
  369. {
  370. try
  371. {
  372. Save((entity as TEntity)!, auditNote);
  373. }
  374. catch (RequestException e)
  375. {
  376. ClientFactory.RaiseRequestError(e);
  377. throw;
  378. }
  379. }
  380. public override void Save(IEnumerable<Entity> entities, string auditNote)
  381. {
  382. try
  383. {
  384. Save(entities.Cast<TEntity>(), auditNote);
  385. }
  386. catch (RequestException e)
  387. {
  388. ClientFactory.RaiseRequestError(e);
  389. throw;
  390. }
  391. }
  392. public void Save(TEntity entity, string auditnote)
  393. {
  394. try
  395. {
  396. using (new Profiler<TEntity>(true))
  397. {
  398. CheckSupported();
  399. entity.LastUpdate = DateTime.Now;
  400. entity.LastUpdateBy = ClientFactory.UserID;
  401. _client.Save(entity, auditnote);
  402. entity.CommitChanges();
  403. }
  404. }
  405. catch (RequestException e)
  406. {
  407. ClientFactory.RaiseRequestError(e);
  408. throw;
  409. }
  410. }
  411. public void Save(TEntity entity, string auditnote, Action<TEntity, Exception?> callback)
  412. {
  413. try
  414. {
  415. var timer = new Profiler<TEntity>(false);
  416. CheckSupported();
  417. _client.Save(entity, auditnote, (i, c) =>
  418. {
  419. timer.Dispose();
  420. callback?.Invoke(i, c);
  421. });
  422. }
  423. catch (RequestException e)
  424. {
  425. ClientFactory.RaiseRequestError(e);
  426. throw;
  427. }
  428. }
  429. public void Save(IEnumerable<TEntity> entities, string auditnote)
  430. {
  431. try
  432. {
  433. using var timer = new Profiler<TEntity>(false);
  434. CheckSupported();
  435. var items = entities.AsArray();
  436. if (items.Any())
  437. _client.Save(items, auditnote);
  438. timer.Log(items.Length);
  439. }
  440. catch (RequestException e)
  441. {
  442. ClientFactory.RaiseRequestError(e);
  443. throw;
  444. }
  445. }
  446. public void Save(IEnumerable<TEntity> entities, string auditnote, Action<IEnumerable<TEntity>, Exception?> callback)
  447. {
  448. try
  449. {
  450. var timer = new Profiler<TEntity>(false);
  451. CheckSupported();
  452. var items = entities.AsArray();
  453. if (items.Any())
  454. _client.Save(items, auditnote, (i, e) =>
  455. {
  456. timer.Dispose(i.Count());
  457. callback?.Invoke(i, e);
  458. });
  459. }
  460. catch (RequestException e)
  461. {
  462. ClientFactory.RaiseRequestError(e);
  463. throw;
  464. }
  465. }
  466. public void Delete(TEntity entity, string auditnote)
  467. {
  468. try
  469. {
  470. using (new Profiler<TEntity>(true))
  471. {
  472. CheckSupported();
  473. _client.Delete(entity, auditnote);
  474. }
  475. }
  476. catch (RequestException e)
  477. {
  478. ClientFactory.RaiseRequestError(e);
  479. throw;
  480. }
  481. }
  482. public void Delete(TEntity entity, string auditnote, Action<TEntity, Exception?> callback)
  483. {
  484. try
  485. {
  486. var timer = new Profiler<TEntity>(true);
  487. CheckSupported();
  488. _client.Delete(entity, auditnote, (i, e) =>
  489. {
  490. timer.Dispose();
  491. callback?.Invoke(i, e);
  492. });
  493. }
  494. catch (RequestException e)
  495. {
  496. ClientFactory.RaiseRequestError(e);
  497. throw;
  498. }
  499. }
  500. public void Delete(IEnumerable<TEntity> entities, string auditnote)
  501. {
  502. try
  503. {
  504. using var timer = new Profiler<TEntity>(false);
  505. CheckSupported();
  506. var items = entities.AsArray();
  507. _client.Delete(items, auditnote);
  508. timer.Log(items.Length);
  509. }
  510. catch (RequestException e)
  511. {
  512. ClientFactory.RaiseRequestError(e);
  513. throw;
  514. }
  515. }
  516. public void Delete(IEnumerable<TEntity> entities, string auditnote, Action<IList<TEntity>, Exception?> callback)
  517. {
  518. try
  519. {
  520. var timer = new Profiler<TEntity>(false);
  521. CheckSupported();
  522. var items = entities.AsArray();
  523. _client.Delete(items, auditnote, (i, e) =>
  524. {
  525. timer.Dispose(i.Count);
  526. callback?.Invoke(i, e);
  527. });
  528. }
  529. catch (RequestException e)
  530. {
  531. ClientFactory.RaiseRequestError(e);
  532. throw;
  533. }
  534. }
  535. public IEnumerable<string> SupportedTypes()
  536. {
  537. try
  538. {
  539. using (new Profiler(true))
  540. return _client.SupportedTypes();
  541. }
  542. catch (RequestException e)
  543. {
  544. ClientFactory.RaiseRequestError(e);
  545. throw;
  546. }
  547. }
  548. public new DatabaseInfo Info()
  549. {
  550. try
  551. {
  552. using (new Profiler(true))
  553. return _client.Info();
  554. }
  555. catch (RequestException e)
  556. {
  557. ClientFactory.RaiseRequestError(e);
  558. throw;
  559. }
  560. }
  561. }
  562. }