123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- using AipGateway.AIP;
- using AipGateway.API.Application.Configurations;
- using AipGateway.API.Domain.Entities;
- using AipGateway.API.Repositories;
- using AipGateway.API.Services.Interfaces;
- using System.Collections;
- using System.Diagnostics;
- using AipGateway.API.Application.Utils;
- namespace AipGateway.API.Services
- {
- public class AipFileService : IAipFileService
- {
- private readonly ILogger<AipFileService> _log;
- private readonly IAipDbRepository _repo;
- private readonly AipSettings _aipSetting;
- private Hashtable _aipLableMap = new Hashtable();
- private Hashtable _aipPolicyMap = new Hashtable();
- private Hashtable _aipProtectionMap = new Hashtable();
- private Hashtable _supportedFileExtMap = new Hashtable();
- private Hashtable _labelFileExtMap = new Hashtable();
- private Hashtable _deleteLabelFileExtMap = new Hashtable();
- private Hashtable _protectFileExtMap = new Hashtable();
- private Hashtable _deleteProtectFileExtMap = new Hashtable();
- private AipFileManager _aipFileManager;
- public AipFileManager aipFileManager { get => _aipFileManager; }
- public AipSettings aipSetting { get => _aipSetting; }
- public AipFileService(ILogger<AipFileService> log, IAipDbRepository repo, AipFileManager aipFileManager, AipSettings aipSetting)
- {
- _log = log;
- _repo = repo;
- _aipSetting = aipSetting;
- _aipFileManager = aipFileManager;
- Inititialize();
- }
- private void Inititialize()
- {
- string supportedFileExt = _aipSetting.SupportedFileExt;
- string protectedFileExt = _aipSetting.ProtectedFileExt;
- string[] fileExts = supportedFileExt.Split(';');
- foreach (string ext in fileExts)
- {
- string fileExt = ext.Trim();
- if (fileExt.Trim() == "") continue;
- _supportedFileExtMap[fileExt] = fileExt;
- _labelFileExtMap[fileExt] = fileExt;
- _deleteLabelFileExtMap[fileExt] = fileExt;
- _protectFileExtMap[fileExt] = fileExt;
- _deleteProtectFileExtMap[fileExt] = fileExt;
- }
- string[] protectedExts = protectedFileExt.Split(';');
- foreach (string ext in protectedExts)
- {
- if (ext.Trim() == "") continue;
- string[] extFile = ext.Split('-');
- if (extFile.Length == 2)
- {
- string orgFileExt = extFile[0].Trim();
- string setFileExt = extFile[1].Trim();
- // supportedFileExtMap
- if (_supportedFileExtMap.Contains(orgFileExt))
- {
- _supportedFileExtMap.Remove(orgFileExt);
- }
- _supportedFileExtMap[orgFileExt] = orgFileExt;
- if (_supportedFileExtMap.Contains(setFileExt))
- {
- _supportedFileExtMap.Remove(setFileExt);
- }
- _supportedFileExtMap[setFileExt] = setFileExt;
- // labelFileExtMap
- if (_labelFileExtMap.Contains(orgFileExt))
- {
- _labelFileExtMap.Remove(orgFileExt);
- }
- _labelFileExtMap[orgFileExt] = orgFileExt;
- if (_labelFileExtMap.Contains(setFileExt))
- {
- _labelFileExtMap.Remove(setFileExt);
- }
- _labelFileExtMap[setFileExt] = setFileExt;
- // deleteLabelFileExtMap
- if (_deleteLabelFileExtMap.Contains(orgFileExt))
- {
- _deleteLabelFileExtMap.Remove(orgFileExt);
- }
- _deleteLabelFileExtMap[orgFileExt] = orgFileExt;
- if (_deleteLabelFileExtMap.Contains(setFileExt))
- {
- _deleteLabelFileExtMap.Remove(setFileExt);
- }
- _deleteLabelFileExtMap[setFileExt] = setFileExt;
- // protectFileExtMap
- if (_protectFileExtMap.Contains(orgFileExt))
- {
- _protectFileExtMap.Remove(orgFileExt);
- }
- _protectFileExtMap[orgFileExt] = setFileExt;
- // deleteProtectFileExtMap
- if (_deleteProtectFileExtMap.Contains(setFileExt))
- {
- _deleteProtectFileExtMap.Remove(setFileExt);
- }
- _deleteProtectFileExtMap[setFileExt] = orgFileExt;
- }
- }
- }
- public string GetDispFileName(string dispFileName, string outputFileName)
- {
- return Path.GetFileNameWithoutExtension(dispFileName) + Path.GetExtension(outputFileName);
- }
- public string GetRequestFileName(string realFileName)
- {
- return _aipSetting.SourceFileDir + realFileName;
- }
- public string GetActualFileName(string fileName)
- {
- return _aipSetting.TargetFileDir + fileName;
- }
- public string? GetSupportedFileType(string fileName)
- {
- string fileExt = Path.GetExtension(fileName).ToLower();
- return _supportedFileExtMap[fileExt] as string;
- }
- //public string? GetSetLabelFileExt(string reqFileName)
- //{
- // string fileExt = Path.GetExtension(reqFileName).ToLower();
- // return _labelFileExtMap[fileExt] as string;
- //}
- //public string? GetSetProtectFileExt(string reqFileName)
- //{
- // string fileExt = Path.GetExtension(reqFileName).ToLower();
- // return _protectFileExtMap[fileExt] as string;
- //}
- //public string? GetDeleteLabelFileExt(string fileName)
- //{
- // string fileExt = Path.GetExtension(fileName).ToLower();
- // return _deleteProtectFileExtMap[fileExt] as string;
- //}
- //public string? GetDeleteProtectFileExt(string fileName)
- //{
- // string fileExt = Path.GetExtension(fileName).ToLower();
- // return _deleteProtectFileExtMap[fileExt] as string;
- //}
- public AipSettings GetAipSettings()
- {
- return _aipSetting;
- }
- public int DownloadAipFileInformations()
- {
- DownloadAipFileLabels();
- DownloadAipFilePolicies();
- DownloadAipFileProtections();
- return _aipLableMap.Count + _aipPolicyMap.Count + _aipProtectionMap.Count;
- }
- private void DownloadAipFileLabels()
- {
- //using var transaction = _dbContext.Database.BeginTransaction();
- try
- {
- Stopwatch sw = Stopwatch.StartNew();
- sw.Start();
- _log.LogInformation("*** AipFileService.DownloadAipFileLabels: Start.");
- Hashtable keyMap = new Hashtable();
- List<TbAipLabel> newLabels = new List<TbAipLabel>();
- List<TbAipLabel> updLabels = new List<TbAipLabel>();
- Hashtable labelMap = new Hashtable();
- List<AipLabel>? lavels = _aipFileManager.SensitivityLabels();
- _log.LogInformation("AipFileService.DownloadAipFileLabels.SensitivityLabels(): {0} EA.", lavels?.Count);
- if (lavels == null)
- {
- return;
- }
- var result = _repo.LoadAipLabels().Result;
- if (result != null)
- {
- foreach (TbAipLabel label in result)
- {
- labelMap.Add(label.LabelGuid, label);
- }
- }
- foreach (AipLabel label in lavels)
- {
- keyMap.Add(label.Id, label);
- if (labelMap.ContainsKey(label.Id))
- {
- TbAipLabel? orgLabel = labelMap[label.Id] as TbAipLabel;
- if (orgLabel != null)
- {
- if (orgLabel.IsChanged(label))
- {
- _log.LogInformation("변경된 레벨 데이터: {0}", label.Id);
- orgLabel.LabelName = label.Name;
- orgLabel.LabelDesc = label.Description;
- updLabels.Add(orgLabel);
- }
- else
- {
- _log.LogInformation("동일한 레벨 데이터: {0}", label.Id);
- }
- }
- else
- {
- _log.LogInformation("Not Found Label In Map: {0}", label.Id);
- }
- }
- else
- {
- _log.LogInformation("새로운 레벨 데이터: {0}", label.Id);
- TbAipLabel obj = new TbAipLabel()
- {
- LabelGuid = label.Id,
- LabelName = label.Name,
- LabelDesc = label.Description,
- CreatedAt = DateTime.Now,
- DeletedAt = null,
- UseYn = true,
- };
- newLabels.Add(obj);
- }
- }
- _aipLableMap = keyMap;
- _log.LogInformation("AipFileService.DownloadAipFileLabels: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
- int updateCount = _repo.UpdateAipLables(updLabels).Result;
- int insertCount = _repo.InsertAipLables(newLabels).Result;
- sw.Stop();
- _log.LogInformation("*** AipFileService.DownloadAipFileLabels: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
- sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
- }
- catch (Exception ex)
- {
- _log.LogError($"*** AipFileService.DownloadAipFileLabels: {ex}");
- }
- }
- private void DownloadAipFilePolicies()
- {
- //using var transaction = _dbContext.Database.BeginTransaction();
- try
- {
- Stopwatch sw = Stopwatch.StartNew();
- sw.Start();
- _log.LogInformation("*** AipFileService.DownloadAipFilePolicies: Start.");
- Hashtable keyMap = new Hashtable();
- List<TbAipPolicy> newLabels = new List<TbAipPolicy>();
- List<TbAipPolicy> updLabels = new List<TbAipPolicy>();
- Hashtable labelMap = new Hashtable();
- List<AipLabel>? lavels = _aipFileManager.ListSensitivityLabels();
- _log.LogInformation("AipFileService.DownloadAipFilePolicies.ListSensitivityLabels(): {0} EA.", lavels?.Count);
- if (lavels == null)
- {
- return;
- }
- var result = _repo.LoadAipPolicies().Result;
- if (result != null)
- {
- foreach (TbAipPolicy policy in result)
- {
- labelMap.Add(policy.PolicyGuid, policy);
- }
- }
- foreach (AipLabel label in lavels)
- {
- keyMap.Add(label.Id, label);
- if (labelMap.ContainsKey(label.Id))
- {
- TbAipPolicy? orgPolicy = labelMap[label.Id] as TbAipPolicy;
- if (orgPolicy != null)
- {
- if (orgPolicy.IsChanged(label))
- {
- _log.LogInformation("변경된 정책 데이터: {0}", label.Id);
- orgPolicy.PolicyName = label.Name;
- orgPolicy.PolicyDesc = label.Description;
- updLabels.Add(orgPolicy);
- }
- else
- {
- _log.LogInformation("동일한 정책 데이터: {0}", label.Id);
- }
- }
- else
- {
- _log.LogInformation("Not Found Policy In Map: {0}", label.Id);
- }
- }
- else
- {
- _log.LogInformation("새로운 정책 데이터: {0}", label.Id);
- TbAipPolicy obj = new TbAipPolicy()
- {
- PolicyGuid = label.Id,
- PolicyName = label.Name,
- PolicyDesc = label.Description,
- CreatedAt = DateTime.Now,
- DeletedAt = null,
- UseYn = true,
- };
- newLabels.Add(obj);
- }
- }
- _aipPolicyMap = keyMap;
- _log.LogInformation("AipFileService.DownloadAipFilePolicies: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
- int updateCount = _repo.UpdateAipPolicies(updLabels).Result;
- int insertCount = _repo.InsertAipPolicies(newLabels).Result;
- sw.Stop();
- _log.LogInformation("*** AipFileService.DownloadAipFilePolicies: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
- sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
- }
- catch (Exception ex)
- {
- _log.LogError($"*** AipFileService.DownloadAipFilePolicies: {ex}");
- }
- }
- private void DownloadAipFileProtections()
- {
- //using var transaction = _dbContext.Database.BeginTransaction();
- try
- {
- Stopwatch sw = Stopwatch.StartNew();
- sw.Start();
- _log.LogInformation("*** AipFileService.DownloadAipFileProtections: Start.");
- Hashtable keyMap = new Hashtable();
- List<TbAipProtection> newLabels = new List<TbAipProtection>();
- List<TbAipProtection> updLabels = new List<TbAipProtection>();
- Hashtable labelMap = new Hashtable();
- List<AipTemplate>? templates = _aipFileManager.GetTemplates();
- _log.LogInformation("AipFileService.DownloadAipFileProtections.GetTemplates(): {0} EA.", templates?.Count);
- if (templates == null)
- {
- return;
- }
- var result = _repo.LoadAipTemplates().Result;
- if (result != null)
- {
- foreach (TbAipProtection policy in result)
- {
- labelMap.Add(policy.ProtectionGuid, policy);
- }
- }
- foreach (AipTemplate template in templates)
- {
- keyMap.Add(template.Id, template);
- if (labelMap.ContainsKey(template.Id))
- {
- TbAipProtection? orgProtection = labelMap[template.Id] as TbAipProtection;
- if (orgProtection != null)
- {
- if (orgProtection.IsChanged(template))
- {
- _log.LogInformation("변경된 보호 데이터: {0}", template.Id);
- orgProtection.ProtectionName = template.Name;
- orgProtection.ProtectionDesc = template.Description;
- updLabels.Add(orgProtection);
- }
- else
- {
- _log.LogInformation("동일한 보호 데이터: {0}", template.Id);
- }
- }
- else
- {
- _log.LogInformation("Not Found Policy In Map: {0}", template.Id);
- }
- }
- else
- {
- _log.LogInformation("새로운 보호 데이터: {0}", template.Id);
- TbAipProtection obj = new TbAipProtection()
- {
- ProtectionGuid = template.Id,
- ProtectionName = template.Name,
- ProtectionDesc = template.Description,
- CreatedAt = DateTime.Now,
- DeletedAt = null,
- UseYn = true,
- };
- newLabels.Add(obj);
- }
- }
- _aipProtectionMap = keyMap;
- _log.LogInformation("AipFileService.DownloadAipFileProtections: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
- int updateCount = _repo.UpdateAipTemplates(updLabels).Result;
- int insertCount = _repo.InsertAipTemplates(newLabels).Result;
- sw.Stop();
- _log.LogInformation("*** AipFileService.DownloadAipFileProtections: ..End. {0} ms. UPDATE {1}/{2}, NEW {3}/{4}.",
- sw.ElapsedMilliseconds, updLabels.Count, updateCount, newLabels.Count, insertCount);
- }
- catch (Exception ex)
- {
- _log.LogError($"*** AipFileService.DownloadAipFileProtections: {ex}");
- }
- }
- }
- }
|