Client.cs 20 KB

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