Client.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 CheckClient<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. CheckClient<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. CheckClient<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. CheckClient<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. CheckClient<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. CheckClient<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. CheckClient<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. CheckClient<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. {
  298. CheckSupported();
  299. var result = _client.Query(filter, columns, orderby);
  300. timer.Log(result.Rows.Count);
  301. return result;
  302. }
  303. }
  304. catch(RequestException e)
  305. {
  306. ClientFactory.RaiseRequestError(e);
  307. throw;
  308. }
  309. }
  310. public override CoreTable Query(IFilter? filter, IColumns? columns, ISortOrder? sortOrder)
  311. {
  312. return Query(filter as Filter<TEntity>, columns as Columns<TEntity>, sortOrder as SortOrder<TEntity>);
  313. }
  314. public void Query(Filter<TEntity>? filter, Columns<TEntity>? columns, SortOrder<TEntity>? sort, Action<CoreTable?, Exception?> callback)
  315. {
  316. try
  317. {
  318. var timer = new Profiler<TEntity>(false);
  319. CheckSupported();
  320. _client.Query(filter, columns, sort, (c, e) =>
  321. {
  322. timer.Dispose(c != null ? c.Rows.Count : -1);
  323. callback?.Invoke(c, e);
  324. });
  325. }
  326. catch (RequestException e)
  327. {
  328. ClientFactory.RaiseRequestError(e);
  329. throw;
  330. }
  331. }
  332. public TEntity[] Load(Filter<TEntity>? filter = null, SortOrder<TEntity>? sort = null)
  333. {
  334. try
  335. {
  336. using (var timer = new Profiler<TEntity>(false))
  337. {
  338. CheckSupported();
  339. var result = _client.Load(filter, sort);
  340. foreach (var entity in result)
  341. entity.CommitChanges();
  342. timer.Log(result.Length);
  343. return result;
  344. }
  345. }
  346. catch (RequestException e)
  347. {
  348. ClientFactory.RaiseRequestError(e);
  349. throw;
  350. }
  351. }
  352. public void Load(Filter<TEntity> filter, SortOrder<TEntity> sort, Action<TEntity[]?, Exception?> callback)
  353. {
  354. try
  355. {
  356. var timer = new Profiler<TEntity>(false);
  357. CheckSupported();
  358. _client.Load(filter, sort, (i, e) =>
  359. {
  360. timer.Dispose(i != null ? i.Length : -1);
  361. callback?.Invoke(i, e);
  362. });
  363. }
  364. catch (RequestException e)
  365. {
  366. ClientFactory.RaiseRequestError(e);
  367. throw;
  368. }
  369. }
  370. public override void Save(Entity entity, string auditNote)
  371. {
  372. try
  373. {
  374. Save((entity as TEntity)!, auditNote);
  375. }
  376. catch (RequestException e)
  377. {
  378. ClientFactory.RaiseRequestError(e);
  379. throw;
  380. }
  381. }
  382. public override void Save(IEnumerable<Entity> entities, string auditNote)
  383. {
  384. try
  385. {
  386. Save(entities.Cast<TEntity>(), auditNote);
  387. }
  388. catch (RequestException e)
  389. {
  390. ClientFactory.RaiseRequestError(e);
  391. throw;
  392. }
  393. }
  394. public void Save(TEntity entity, string auditnote)
  395. {
  396. try
  397. {
  398. using (new Profiler<TEntity>(true))
  399. {
  400. CheckSupported();
  401. entity.LastUpdate = DateTime.Now;
  402. entity.LastUpdateBy = ClientFactory.UserID;
  403. _client.Save(entity, auditnote);
  404. entity.CommitChanges();
  405. }
  406. }
  407. catch (RequestException e)
  408. {
  409. ClientFactory.RaiseRequestError(e);
  410. throw;
  411. }
  412. }
  413. public void Save(TEntity entity, string auditnote, Action<TEntity, Exception?> callback)
  414. {
  415. try
  416. {
  417. var timer = new Profiler<TEntity>(false);
  418. CheckSupported();
  419. _client.Save(entity, auditnote, (i, c) =>
  420. {
  421. timer.Dispose();
  422. callback?.Invoke(i, c);
  423. });
  424. }
  425. catch (RequestException e)
  426. {
  427. ClientFactory.RaiseRequestError(e);
  428. throw;
  429. }
  430. }
  431. public void Save(IEnumerable<TEntity> entities, string auditnote)
  432. {
  433. try
  434. {
  435. using var timer = new Profiler<TEntity>(false);
  436. CheckSupported();
  437. var items = entities.AsArray();
  438. if (items.Any())
  439. _client.Save(items, auditnote);
  440. timer.Log(items.Length);
  441. }
  442. catch (RequestException e)
  443. {
  444. ClientFactory.RaiseRequestError(e);
  445. throw;
  446. }
  447. }
  448. public void Save(IEnumerable<TEntity> entities, string auditnote, Action<IEnumerable<TEntity>, Exception?> callback)
  449. {
  450. try
  451. {
  452. var timer = new Profiler<TEntity>(false);
  453. CheckSupported();
  454. var items = entities.AsArray();
  455. if (items.Any())
  456. _client.Save(items, auditnote, (i, e) =>
  457. {
  458. timer.Dispose(i.Count());
  459. callback?.Invoke(i, e);
  460. });
  461. }
  462. catch (RequestException e)
  463. {
  464. ClientFactory.RaiseRequestError(e);
  465. throw;
  466. }
  467. }
  468. public void Delete(TEntity entity, string auditnote)
  469. {
  470. try
  471. {
  472. using (new Profiler<TEntity>(true))
  473. {
  474. CheckSupported();
  475. _client.Delete(entity, auditnote);
  476. }
  477. }
  478. catch (RequestException e)
  479. {
  480. ClientFactory.RaiseRequestError(e);
  481. throw;
  482. }
  483. }
  484. public void Delete(TEntity entity, string auditnote, Action<TEntity, Exception?> callback)
  485. {
  486. try
  487. {
  488. var timer = new Profiler<TEntity>(true);
  489. CheckSupported();
  490. _client.Delete(entity, auditnote, (i, e) =>
  491. {
  492. timer.Dispose();
  493. callback?.Invoke(i, e);
  494. });
  495. }
  496. catch (RequestException e)
  497. {
  498. ClientFactory.RaiseRequestError(e);
  499. throw;
  500. }
  501. }
  502. public void Delete(IEnumerable<TEntity> entities, string auditnote)
  503. {
  504. try
  505. {
  506. using var timer = new Profiler<TEntity>(false);
  507. CheckSupported();
  508. var items = entities.AsArray();
  509. _client.Delete(items, auditnote);
  510. timer.Log(items.Length);
  511. }
  512. catch (RequestException e)
  513. {
  514. ClientFactory.RaiseRequestError(e);
  515. throw;
  516. }
  517. }
  518. public void Delete(IEnumerable<TEntity> entities, string auditnote, Action<IList<TEntity>, Exception?> callback)
  519. {
  520. try
  521. {
  522. var timer = new Profiler<TEntity>(false);
  523. CheckSupported();
  524. var items = entities.AsArray();
  525. _client.Delete(items, auditnote, (i, e) =>
  526. {
  527. timer.Dispose(i.Count);
  528. callback?.Invoke(i, e);
  529. });
  530. }
  531. catch (RequestException e)
  532. {
  533. ClientFactory.RaiseRequestError(e);
  534. throw;
  535. }
  536. }
  537. public IEnumerable<string> SupportedTypes()
  538. {
  539. try
  540. {
  541. using (new Profiler(true))
  542. return _client.SupportedTypes();
  543. }
  544. catch (RequestException e)
  545. {
  546. ClientFactory.RaiseRequestError(e);
  547. throw;
  548. }
  549. }
  550. public new DatabaseInfo Info()
  551. {
  552. try
  553. {
  554. using (new Profiler(true))
  555. return _client.Info();
  556. }
  557. catch (RequestException e)
  558. {
  559. ClientFactory.RaiseRequestError(e);
  560. throw;
  561. }
  562. }
  563. }
  564. }