Client.cs 14 KB

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