Client.cs 18 KB

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