Client.cs 15 KB

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