Client.cs 17 KB

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