AipConfigService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. using Aip.Service.Aip.Helpers;
  2. using Aip.Service.Aip.Models;
  3. using Aip.Service.Aip.Serivces;
  4. using Aip.Service.Configurations;
  5. using Aip.Service.Entities;
  6. using Aip.Service.Repositories;
  7. using Aip.Service.Services.Interfaces;
  8. using System.Collections;
  9. using System.Diagnostics;
  10. namespace Aip.Service.Services;
  11. public class AipConfigService : IApiConfigService
  12. {
  13. private readonly ILogger<AipConfigService> _log;
  14. private readonly IAipDbRepository _repo;
  15. private readonly AipSettings _aipSetting;
  16. private readonly AipFileService _aipFileService;
  17. private Hashtable _aipLableMap = new Hashtable();
  18. private Hashtable _aipPolicyMap = new Hashtable();
  19. private Hashtable _aipProtectionMap = new Hashtable();
  20. private Hashtable _supportedFileExtMap = new Hashtable();
  21. private Hashtable _labelFileExtMap = new Hashtable();
  22. private Hashtable _deleteLabelFileExtMap = new Hashtable();
  23. private Hashtable _protectFileExtMap = new Hashtable();
  24. private Hashtable _deleteProtectFileExtMap = new Hashtable();
  25. private static volatile uint requestCount = 0;
  26. public AipSettings aipSetting { get => _aipSetting; }
  27. public AipConfigService(ILogger<AipConfigService> log, IAipDbRepository repo, AipSettings aipSetting, AipFileService aipFileService)
  28. {
  29. _log = log;
  30. _repo = repo;
  31. _aipSetting = aipSetting;
  32. _aipFileService = aipFileService;
  33. Inititialize();
  34. }
  35. private void Inititialize()
  36. {
  37. string supportedFileExt = _aipSetting.SupportedFileExt;
  38. string protectedFileExt = _aipSetting.ProtectedFileExt;
  39. string[] fileExts = supportedFileExt.Split(';');
  40. foreach (string ext in fileExts)
  41. {
  42. string fileExt = ext.Trim();
  43. if (fileExt.Trim() == "") continue;
  44. _supportedFileExtMap[fileExt] = fileExt;
  45. _labelFileExtMap[fileExt] = fileExt;
  46. _deleteLabelFileExtMap[fileExt] = fileExt;
  47. _protectFileExtMap[fileExt] = fileExt;
  48. _deleteProtectFileExtMap[fileExt] = fileExt;
  49. }
  50. string[] protectedExts = protectedFileExt.Split(';');
  51. foreach (string ext in protectedExts)
  52. {
  53. if (ext.Trim() == "") continue;
  54. string[] extFile = ext.Split('-');
  55. if (extFile.Length == 2)
  56. {
  57. string orgFileExt = extFile[0].Trim();
  58. string setFileExt = extFile[1].Trim();
  59. // supportedFileExtMap
  60. if (_supportedFileExtMap.Contains(orgFileExt))
  61. {
  62. _supportedFileExtMap.Remove(orgFileExt);
  63. }
  64. _supportedFileExtMap[orgFileExt] = orgFileExt;
  65. if (_supportedFileExtMap.Contains(setFileExt))
  66. {
  67. _supportedFileExtMap.Remove(setFileExt);
  68. }
  69. _supportedFileExtMap[setFileExt] = setFileExt;
  70. // labelFileExtMap
  71. if (_labelFileExtMap.Contains(orgFileExt))
  72. {
  73. _labelFileExtMap.Remove(orgFileExt);
  74. }
  75. _labelFileExtMap[orgFileExt] = orgFileExt;
  76. if (_labelFileExtMap.Contains(setFileExt))
  77. {
  78. _labelFileExtMap.Remove(setFileExt);
  79. }
  80. _labelFileExtMap[setFileExt] = setFileExt;
  81. // deleteLabelFileExtMap
  82. if (_deleteLabelFileExtMap.Contains(orgFileExt))
  83. {
  84. _deleteLabelFileExtMap.Remove(orgFileExt);
  85. }
  86. _deleteLabelFileExtMap[orgFileExt] = orgFileExt;
  87. if (_deleteLabelFileExtMap.Contains(setFileExt))
  88. {
  89. _deleteLabelFileExtMap.Remove(setFileExt);
  90. }
  91. _deleteLabelFileExtMap[setFileExt] = setFileExt;
  92. // protectFileExtMap
  93. if (_protectFileExtMap.Contains(orgFileExt))
  94. {
  95. _protectFileExtMap.Remove(orgFileExt);
  96. }
  97. _protectFileExtMap[orgFileExt] = setFileExt;
  98. // deleteProtectFileExtMap
  99. if (_deleteProtectFileExtMap.Contains(setFileExt))
  100. {
  101. _deleteProtectFileExtMap.Remove(setFileExt);
  102. }
  103. _deleteProtectFileExtMap[setFileExt] = orgFileExt;
  104. }
  105. }
  106. }
  107. public int GetAipPort()
  108. {
  109. return 0;
  110. }
  111. public string GetDispFileName(string dispFileName, string outputFileName)
  112. {
  113. return Path.GetFileNameWithoutExtension(dispFileName) + Path.GetExtension(outputFileName);
  114. }
  115. public string GetRequestFileName(string realFileName)
  116. {
  117. return _aipSetting.SourceFileDir + realFileName;
  118. }
  119. public string GetActualFileName(string fileName)
  120. {
  121. return _aipSetting.TargetFileDir + fileName;
  122. }
  123. public string? GetSupportedFileType(string fileName)
  124. {
  125. string fileExt = Path.GetExtension(fileName).ToLower();
  126. return _supportedFileExtMap[fileExt] as string;
  127. }
  128. public AipSettings GetAipSettings()
  129. {
  130. return _aipSetting;
  131. }
  132. public int DownloadAipFileInformations()
  133. {
  134. DownloadAipFileLabels();
  135. DownloadAipFilePolicies();
  136. DownloadAipFileProtections();
  137. return _aipLableMap.Count + _aipPolicyMap.Count + _aipProtectionMap.Count;
  138. }
  139. private void DownloadAipFileLabels()
  140. {
  141. //using var transaction = _dbContext.Database.BeginTransaction();
  142. try
  143. {
  144. Stopwatch sw = Stopwatch.StartNew();
  145. sw.Start();
  146. _log.LogInformation("*** AipFileService.DownloadAipFileLabels: Start.");
  147. Hashtable keyMap = new Hashtable();
  148. List<TbAipLabel> newLabels = new List<TbAipLabel>();
  149. List<TbAipLabel> updLabels = new List<TbAipLabel>();
  150. Hashtable labelMap = new Hashtable();
  151. List<AipLabel>? lavels = _aipFileService.SensitivityLabels();
  152. _log.LogInformation("AipFileService.DownloadAipFileLabels.SensitivityLabels(): {0} EA.", lavels?.Count);
  153. if (lavels == null)
  154. {
  155. return;
  156. }
  157. var result = _repo.LoadAipLabels().Result;
  158. if (result != null)
  159. {
  160. foreach (TbAipLabel label in result)
  161. {
  162. labelMap.Add(label.LabelGuid, label);
  163. }
  164. }
  165. foreach (AipLabel label in lavels)
  166. {
  167. keyMap.Add(label.Id, label);
  168. if (labelMap.ContainsKey(label.Id))
  169. {
  170. TbAipLabel? orgLabel = labelMap[label.Id] as TbAipLabel;
  171. if (orgLabel != null)
  172. {
  173. if (orgLabel.IsChanged(label))
  174. {
  175. _log.LogInformation("변경된 레벨 데이터: {0}", label.Id);
  176. orgLabel.LabelName = label.Name;
  177. orgLabel.LabelDesc = label.Description;
  178. updLabels.Add(orgLabel);
  179. }
  180. else
  181. {
  182. _log.LogInformation("동일한 레벨 데이터: {0}", label.Id);
  183. }
  184. }
  185. else
  186. {
  187. _log.LogInformation("Not Found Label In Map: {0}", label.Id);
  188. }
  189. }
  190. else
  191. {
  192. _log.LogInformation("새로운 레벨 데이터: {0}", label.Id);
  193. TbAipLabel obj = new TbAipLabel()
  194. {
  195. LabelGuid = label.Id,
  196. LabelName = label.Name,
  197. LabelDesc = label.Description,
  198. CreatedAt = DateTime.Now,
  199. DeletedAt = null,
  200. UseYn = true,
  201. };
  202. newLabels.Add(obj);
  203. }
  204. }
  205. _aipLableMap = keyMap;
  206. _log.LogInformation("AipFileService.DownloadAipFileLabels: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  207. int updateCount = _repo.UpdateAipLables(updLabels).Result;
  208. int insertCount = _repo.InsertAipLables(newLabels).Result;
  209. sw.Stop();
  210. _log.LogInformation("*** AipFileService.DownloadAipFileLabels: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
  211. sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
  212. }
  213. catch (Exception ex)
  214. {
  215. _log.LogError($"*** AipFileService.DownloadAipFileLabels: {ex}");
  216. }
  217. }
  218. private void DownloadAipFilePolicies()
  219. {
  220. //using var transaction = _dbContext.Database.BeginTransaction();
  221. try
  222. {
  223. Stopwatch sw = Stopwatch.StartNew();
  224. sw.Start();
  225. _log.LogInformation("*** AipFileService.DownloadAipFilePolicies: Start.");
  226. Hashtable keyMap = new Hashtable();
  227. List<TbAipPolicy> newLabels = new List<TbAipPolicy>();
  228. List<TbAipPolicy> updLabels = new List<TbAipPolicy>();
  229. Hashtable labelMap = new Hashtable();
  230. List<AipLabel>? lavels = _aipFileService.ListSensitivityLabels();
  231. _log.LogInformation("AipFileService.DownloadAipFilePolicies.ListSensitivityLabels(): {0} EA.", lavels?.Count);
  232. if (lavels == null)
  233. {
  234. return;
  235. }
  236. var result = _repo.LoadAipPolicies().Result;
  237. if (result != null)
  238. {
  239. foreach (TbAipPolicy policy in result)
  240. {
  241. labelMap.Add(policy.PolicyGuid, policy);
  242. }
  243. }
  244. foreach (AipLabel label in lavels)
  245. {
  246. keyMap.Add(label.Id, label);
  247. if (labelMap.ContainsKey(label.Id))
  248. {
  249. TbAipPolicy? orgPolicy = labelMap[label.Id] as TbAipPolicy;
  250. if (orgPolicy != null)
  251. {
  252. if (orgPolicy.IsChanged(label))
  253. {
  254. _log.LogInformation("변경된 정책 데이터: {0}", label.Id);
  255. orgPolicy.PolicyName = label.Name;
  256. orgPolicy.PolicyDesc = label.Description;
  257. updLabels.Add(orgPolicy);
  258. }
  259. else
  260. {
  261. _log.LogInformation("동일한 정책 데이터: {0}", label.Id);
  262. }
  263. }
  264. else
  265. {
  266. _log.LogInformation("Not Found Policy In Map: {0}", label.Id);
  267. }
  268. }
  269. else
  270. {
  271. _log.LogInformation("새로운 정책 데이터: {0}", label.Id);
  272. TbAipPolicy obj = new TbAipPolicy()
  273. {
  274. PolicyGuid = label.Id,
  275. PolicyName = label.Name,
  276. PolicyDesc = label.Description,
  277. CreatedAt = DateTime.Now,
  278. DeletedAt = null,
  279. UseYn = true,
  280. };
  281. newLabels.Add(obj);
  282. }
  283. }
  284. _aipPolicyMap = keyMap;
  285. _log.LogInformation("AipFileService.DownloadAipFilePolicies: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  286. int updateCount = _repo.UpdateAipPolicies(updLabels).Result;
  287. int insertCount = _repo.InsertAipPolicies(newLabels).Result;
  288. sw.Stop();
  289. _log.LogInformation("*** AipFileService.DownloadAipFilePolicies: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
  290. sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
  291. }
  292. catch (Exception ex)
  293. {
  294. _log.LogError($"*** AipFileService.DownloadAipFilePolicies: {ex}");
  295. }
  296. }
  297. private void DownloadAipFileProtections()
  298. {
  299. //using var transaction = _dbContext.Database.BeginTransaction();
  300. try
  301. {
  302. Stopwatch sw = Stopwatch.StartNew();
  303. sw.Start();
  304. _log.LogInformation("*** AipFileService.DownloadAipFileProtections: Start.");
  305. Hashtable keyMap = new Hashtable();
  306. List<TbAipProtection> newLabels = new List<TbAipProtection>();
  307. List<TbAipProtection> updLabels = new List<TbAipProtection>();
  308. Hashtable labelMap = new Hashtable();
  309. List<AipTemplate>? templates = _aipFileService.GetTemplates();
  310. _log.LogInformation("AipFileService.DownloadAipFileProtections.GetTemplates(): {0} EA.", templates?.Count);
  311. if (templates == null)
  312. {
  313. return;
  314. }
  315. var result = _repo.LoadAipTemplates().Result;
  316. if (result != null)
  317. {
  318. foreach (TbAipProtection policy in result)
  319. {
  320. labelMap.Add(policy.ProtectionGuid, policy);
  321. }
  322. }
  323. foreach (AipTemplate template in templates)
  324. {
  325. keyMap.Add(template.Id, template);
  326. if (labelMap.ContainsKey(template.Id))
  327. {
  328. TbAipProtection? orgProtection = labelMap[template.Id] as TbAipProtection;
  329. if (orgProtection != null)
  330. {
  331. if (orgProtection.IsChanged(template))
  332. {
  333. _log.LogInformation("변경된 보호 데이터: {0}", template.Id);
  334. orgProtection.ProtectionName = template.Name;
  335. orgProtection.ProtectionDesc = template.Description;
  336. updLabels.Add(orgProtection);
  337. }
  338. else
  339. {
  340. _log.LogInformation("동일한 보호 데이터: {0}", template.Id);
  341. }
  342. }
  343. else
  344. {
  345. _log.LogInformation("Not Found Policy In Map: {0}", template.Id);
  346. }
  347. }
  348. else
  349. {
  350. _log.LogInformation("새로운 보호 데이터: {0}", template.Id);
  351. TbAipProtection obj = new TbAipProtection()
  352. {
  353. ProtectionGuid = template.Id,
  354. ProtectionName = template.Name,
  355. ProtectionDesc = template.Description,
  356. CreatedAt = DateTime.Now,
  357. DeletedAt = null,
  358. UseYn = true,
  359. };
  360. newLabels.Add(obj);
  361. }
  362. }
  363. _aipProtectionMap = keyMap;
  364. _log.LogInformation("AipFileService.DownloadAipFileProtections: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  365. int updateCount = _repo.UpdateAipTemplates(updLabels).Result;
  366. int insertCount = _repo.InsertAipTemplates(newLabels).Result;
  367. sw.Stop();
  368. _log.LogInformation("*** AipFileService.DownloadAipFileProtections: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
  369. sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
  370. }
  371. catch (Exception ex)
  372. {
  373. _log.LogError($"*** AipFileService.DownloadAipFileProtections: {ex}");
  374. }
  375. }
  376. }