Client.cs 16 KB

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