AipConfigService.cs 15 KB

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