Client.cs 20 KB

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