Desktop.razor.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. using HyperCube.Models;
  2. using Microsoft.AspNetCore.Components;
  3. using Microsoft.AspNetCore.Components.Authorization;
  4. using Microsoft.AspNetCore.Components.Forms;
  5. using Microsoft.AspNetCore.Identity;
  6. using Microsoft.JSInterop;
  7. using Pullenti.Unitext;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel.DataAnnotations;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Reflection;
  14. using System.Security.Cryptography;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Xml;
  18. using Console = HyperCube.Utils.AdvConsole;
  19. namespace HyperCube.Pages
  20. {
  21. public partial class Desktop : ComponentBase
  22. {
  23. //[Parameter]
  24. //public int DocID { get; set; }
  25. [Inject]
  26. NavigationManager NavigationManager { get; set; }
  27. [Inject]
  28. AuthenticationStateProvider AuthenticationStateProvider { get; set; }
  29. [Inject]
  30. UserManager<IdentityUser> UserManager { get; set; }
  31. [Inject]
  32. RoleManager<IdentityRole> RoleManager { get; set; }
  33. [Inject]
  34. AppData AppData { get; set; }
  35. [Inject]
  36. public IJSRuntime JsRuntime { get; set; }
  37. const string STORAGE_FOLDER_NAME = "articles_storage";
  38. const long MAX_FILE_SIZE = 5120000; //bytes
  39. const string ACTIVE_BUTTON_CLASS = "btn_white tab-button active";
  40. const string ACTIVE_TAB_CLASS = "second-block__form visible";
  41. const string BUTTON_CLASS = "btn_white tab-button";
  42. const string TAB_CLASS = "second-block__form";
  43. string _uploadButtonClass = ACTIVE_BUTTON_CLASS;
  44. string _uploadTabClass = ACTIVE_TAB_CLASS;
  45. string _verifyButtonClass = BUTTON_CLASS;
  46. string _verifyTabClass = TAB_CLASS;
  47. string _otherButtonClass = BUTTON_CLASS;
  48. string _otherTabClass = TAB_CLASS;
  49. int _counter = 1;
  50. //string _event = "";
  51. string _status;
  52. string _articleDropdownOption = "";
  53. //string _storageFolderPath;
  54. MemoryStream _memoryStream;
  55. ModalInfo _modalInfo { get; set; }
  56. ModalLoading _modalLoading { get; set; }
  57. ArticleModel _articleClone;
  58. ArticleModel _article;
  59. Dictionary<int, ArticleModel> _articles = new();
  60. ReportModel _report = new();
  61. UnitextDocument _document;
  62. AccountModel _currentAccount;
  63. VerificationPoint _verificationPoint = new();
  64. bool loadButtonDisable { get; set; }
  65. bool verifyButtonDisable { get; set; }
  66. protected override async Task OnInitializedAsync()
  67. {
  68. ///tmp
  69. await AppData.LoadArticles();
  70. _article = AppData.CurrentArticle ?? (new());
  71. _articleClone = AppData.CurrentArticleClone ?? (new());
  72. _currentAccount = (AppData.CurrentAccount != null) ? AppData.CurrentAccount : await GetCurrentAcc();
  73. _currentAccount.LoadRoles();
  74. Console.WriteLine($"Desktop OnInitializedAsync, CurrentAccount: {_currentAccount.Name}");
  75. if (_currentAccount.Roles.Contains(Role.Admin))
  76. {
  77. loadButtonDisable = false;
  78. verifyButtonDisable = false;
  79. _articles = AppData.Articles;
  80. }
  81. else if (_currentAccount.Roles.Contains(Role.Expert))
  82. {
  83. loadButtonDisable = true;
  84. verifyButtonDisable = false;
  85. foreach (ArticleModel article in AppData.Articles.Values)
  86. {
  87. if (article.Status == ArticleStatus.AwatingVerify || article.Status == ArticleStatus.Verifying)
  88. _articles.Add(article.ID, article);
  89. }
  90. SwitchDesktopTab(1);
  91. }
  92. else if (_currentAccount.Roles.Contains(Role.Miner))
  93. {
  94. loadButtonDisable = false;
  95. verifyButtonDisable = true;
  96. foreach (ArticleModel article in AppData.Articles.Values)
  97. {
  98. if (article.Status == ArticleStatus.New || article.Status == ArticleStatus.Saved)
  99. _articles.Add(article.ID, article);
  100. }
  101. }
  102. else
  103. {
  104. loadButtonDisable = true;
  105. verifyButtonDisable = true;
  106. _articles = AppData.Articles;
  107. SwitchDesktopTab(2);
  108. }
  109. }
  110. protected override void OnAfterRender(bool firstRender) => _counter = 1;
  111. void SwitchDesktopTab(int tabIndex)
  112. {
  113. switch(tabIndex)
  114. {
  115. case 0:
  116. _uploadButtonClass = ACTIVE_BUTTON_CLASS;
  117. _uploadTabClass = ACTIVE_TAB_CLASS;
  118. _verifyButtonClass = BUTTON_CLASS;
  119. _verifyTabClass = TAB_CLASS;
  120. _otherButtonClass = BUTTON_CLASS;
  121. _otherTabClass = TAB_CLASS;
  122. break;
  123. case 1:
  124. _uploadButtonClass = BUTTON_CLASS;
  125. _uploadTabClass = TAB_CLASS;
  126. _verifyButtonClass = ACTIVE_BUTTON_CLASS;
  127. _verifyTabClass = ACTIVE_TAB_CLASS;
  128. _otherButtonClass = BUTTON_CLASS;
  129. _otherTabClass = TAB_CLASS;
  130. break;
  131. case 2:
  132. _uploadButtonClass = BUTTON_CLASS;
  133. _uploadTabClass = TAB_CLASS;
  134. _verifyButtonClass = BUTTON_CLASS;
  135. _verifyTabClass = TAB_CLASS;
  136. _otherButtonClass = ACTIVE_BUTTON_CLASS;
  137. _otherTabClass = ACTIVE_TAB_CLASS;
  138. break;
  139. }
  140. }
  141. async Task WidgetMenuClick(string menuname, string elementid)
  142. {
  143. await JsRuntime.InvokeVoidAsync("WidgetMenuClick", menuname, elementid);
  144. _counter = 1;
  145. }
  146. async Task HandleUpload(InputFileChangeEventArgs e)
  147. {
  148. _modalLoading.Open();
  149. IBrowserFile file = e.File;
  150. if (file != null)
  151. {
  152. Stream stream = file.OpenReadStream(MAX_FILE_SIZE);
  153. _memoryStream = new();
  154. await stream.CopyToAsync(_memoryStream);
  155. _status = $"Finished loading {_memoryStream.Length} bytes from {file.Name}";
  156. Console.WriteLine(_status);
  157. /// calculating hash
  158. string hash = await CalculateHashSum(_memoryStream);
  159. Console.WriteLine($"Hash: {hash}");
  160. /// checking hash
  161. MySQLConnector dbCon = MySQLConnector.Instance();
  162. string stringSQL;
  163. stringSQL = $"SELECT COUNT(*) FROM articles WHERE file_hash='{hash}'";
  164. int count = await dbCon.SQLSelectCount(stringSQL);
  165. if (count < 1)
  166. {
  167. _report = new();
  168. _report.FileName = file.Name;
  169. _report.FileSize = _memoryStream.Length.ToString();
  170. _memoryStream.Position = 0;
  171. byte[] content = _memoryStream.ToArray();
  172. _document = UnitextService.CreateDocument(null, content, null);
  173. if (_document.ErrorMessage != null)
  174. {
  175. // скорее всего, этот формат не поддерживается на данный момент
  176. Console.WriteLine($"error, sorry: {_document.ErrorMessage}");
  177. _memoryStream.Close();
  178. stream.Close();
  179. _modalLoading.Close();
  180. _modalInfo.Open("Не удается прочитать документ, формат не поддерживается или файл поврежден.");
  181. return;
  182. }
  183. // восстанавливаем имя исходного файла, извлечённого из ресурсов
  184. _document.SourceFileName = file.Name;
  185. for (int i = file.Name.Length - 7; i > 0; i--)
  186. {
  187. if (file.Name[i] == '.')
  188. {
  189. _document.SourceFileName = file.Name.Substring(i + 1);
  190. break;
  191. }
  192. }
  193. //// записываем результат в XML
  194. //using (FileStream fs = new(doc.SourceFileName + ".xml", FileMode.Create, FileAccess.Write))
  195. //{
  196. // XmlWriterSettings xmlParams = new();
  197. // xmlParams.Encoding = Encoding.UTF8;
  198. // xmlParams.Indent = true;
  199. // xmlParams.IndentChars = " ";
  200. // using (XmlWriter xml = XmlWriter.Create(fs, xmlParams))
  201. // {
  202. // xml.WriteStartDocument();
  203. // doc.GetXml(xml);
  204. // xml.WriteEndDocument();
  205. // }
  206. //}
  207. //Console.WriteLine("XML write done");
  208. // получаем плоский текст
  209. string plainText = _document.GetPlaintextString(null);
  210. if (plainText == null)
  211. plainText = "Текст не выделен";
  212. /// todo обработка, если статус != новый
  213. _articleClone = DocParse.GetBaseProperties(plainText);
  214. _articleClone.Filename = file.Name;
  215. _articleClone.HashSum = hash;
  216. _article = (ArticleModel)_articleClone.Clone();
  217. ///tmp
  218. AppData.CurrentArticle = _article;
  219. Console.WriteLine($"Initializing SDK Pullenti ver {Pullenti.Sdk.Version} ({Pullenti.Sdk.VersionDate})... ");
  220. Pullenti.Sdk.InitializeAll();
  221. //Console.WriteLine($"OK (by ... ms), version {Pullenti.Ner.ProcessorService.Version}");
  222. List<string> npt_tokens = new();
  223. // запускаем обработку на пустом процессоре (без анализаторов NER)
  224. Pullenti.Ner.AnalysisResult are = Pullenti.Ner.ProcessorService.EmptyProcessor.Process(new Pullenti.Ner.SourceOfAnalysis(plainText), null, null);
  225. //System.Console.Write("Noun groups: ");
  226. // перебираем токены
  227. for (Pullenti.Ner.Token t = are.FirstToken; t != null; t = t.Next)
  228. {
  229. // выделяем именную группу с текущего токена
  230. Pullenti.Ner.Core.NounPhraseToken npt = Pullenti.Ner.Core.NounPhraseHelper.TryParse(t, Pullenti.Ner.Core.NounPhraseParseAttr.No, 0, null);
  231. // не получилось
  232. if (npt == null)
  233. continue;
  234. // получилось, выводим в нормализованном виде
  235. //System.Console.Write($"[{npt.GetSourceText()}=>{npt.GetNormalCaseText(null, Pullenti.Morph.MorphNumber.Singular, Pullenti.Morph.MorphGender.Undefined, false)}] ");
  236. //_report.NounGroups += $"[{npt.GetSourceText()}=>{npt.GetNormalCaseText(null, Pullenti.Morph.MorphNumber.Singular, Pullenti.Morph.MorphGender.Undefined, false)}] ";
  237. _article.NounGroups += $"[{npt.GetSourceText()}=>{npt.GetNormalCaseText(null, Pullenti.Morph.MorphNumber.Singular, Pullenti.Morph.MorphGender.Undefined, false)}] ";
  238. npt_tokens.Add(npt.GetNormalCaseText(null, Pullenti.Morph.MorphNumber.Singular, Pullenti.Morph.MorphGender.Undefined, false));
  239. // указатель на последний токен именной группы
  240. t = npt.EndToken;
  241. }
  242. using (Pullenti.Ner.Processor proc = Pullenti.Ner.ProcessorService.CreateProcessor())
  243. {
  244. // анализируем текст
  245. Pullenti.Ner.AnalysisResult ar = proc.Process(new Pullenti.Ner.SourceOfAnalysis(plainText), null, null);
  246. // результирующие сущности
  247. //Console.WriteLine("\r\n==========================================\r\nEntities: ");
  248. foreach (Pullenti.Ner.Referent en in ar.Entities)
  249. {
  250. //Console.WriteLine($"{en.TypeName}: {en}");
  251. //_report.Entities += $"{en.TypeName}: {en}\r\n";
  252. _article.Entities += $"{en.TypeName}: {en}\r\n";
  253. foreach (Pullenti.Ner.Slot s in en.Slots)
  254. {
  255. //Console.WriteLine($" {s.TypeName}: {s.Value}");
  256. //_report.Entities += $" {s.TypeName}: {s.Value}<br>";
  257. _article.Entities += $" {s.TypeName}: {s.Value}<br>";
  258. }
  259. }
  260. // пример выделения именных групп
  261. //Console.WriteLine("\r\n==========================================\r\nNoun groups: ");
  262. for (Pullenti.Ner.Token t = ar.FirstToken; t != null; t = t.Next)
  263. {
  264. // токены с сущностями игнорируем
  265. if (t.GetReferent() != null)
  266. continue;
  267. // пробуем создать именную группу
  268. Pullenti.Ner.Core.NounPhraseToken npt = Pullenti.Ner.Core.NounPhraseHelper.TryParse(t, Pullenti.Ner.Core.NounPhraseParseAttr.AdjectiveCanBeLast, 0, null);
  269. // не получилось
  270. if (npt == null)
  271. continue;
  272. //Console.WriteLine(npt.ToString());
  273. //_report.EntitiesNounGroups += $"{npt}<br>";
  274. _article.Morph += $"{npt}<br>";
  275. // указатель перемещаем на последний токен группы
  276. t = npt.EndToken;
  277. }
  278. }
  279. using (Pullenti.Ner.Processor proc = Pullenti.Ner.ProcessorService.CreateSpecificProcessor(Pullenti.Ner.Keyword.KeywordAnalyzer.ANALYZER_NAME))
  280. {
  281. Pullenti.Ner.AnalysisResult ar = proc.Process(new Pullenti.Ner.SourceOfAnalysis(plainText), null, null);
  282. //Console.WriteLine("\r\n==========================================\r\nKeywords1: ");
  283. foreach (Pullenti.Ner.Referent en in ar.Entities)
  284. {
  285. if (en is Pullenti.Ner.Keyword.KeywordReferent)
  286. //Console.WriteLine(en.ToString());
  287. //_report.Keywords1 += $"{en}<br>";
  288. _article.Keywords1 += $"{en}<br>";
  289. }
  290. //Console.WriteLine("\r\n==========================================\r\nKeywords2: ");
  291. for (Pullenti.Ner.Token t = ar.FirstToken; t != null; t = t.Next)
  292. {
  293. if (t is Pullenti.Ner.ReferentToken)
  294. {
  295. Pullenti.Ner.Keyword.KeywordReferent kw = t.GetReferent() as Pullenti.Ner.Keyword.KeywordReferent;
  296. if (kw == null)
  297. continue;
  298. string kwstr = Pullenti.Ner.Core.MiscHelper.GetTextValueOfMetaToken(t as Pullenti.Ner.ReferentToken, Pullenti.Ner.Core.GetTextAttr.FirstNounGroupToNominativeSingle | Pullenti.Ner.Core.GetTextAttr.KeepRegister);
  299. //Console.WriteLine($"{kwstr} = {kw}");
  300. //_report.Keywords2 += $"{kwstr} = {kw}<br>";
  301. _article.Keywords2 += $"{kwstr} = {kw}<br>";
  302. }
  303. }
  304. }
  305. int res = (from x in npt_tokens
  306. select x).Distinct().Count();
  307. Console.WriteLine($"npt_tokens.count={npt_tokens.Count}, distinct.Count={res}");
  308. Console.WriteLine("Analysis is over!");
  309. var query = from x in npt_tokens
  310. group x by x into g
  311. let count1 = g.Count()
  312. orderby count1 descending
  313. select new { Name = g.Key, Count = count1 };
  314. foreach (var result in query)
  315. {
  316. _report.NounGroupsSorted += $"{result.Name}, Count: {result.Count}<br>";
  317. //Console.WriteLine($"Name: {result.Name}, Count: {result.Count}");
  318. }
  319. //AppData.Report = _report;
  320. }
  321. else
  322. {
  323. _status = $"File duplicate founded, hash: {hash}.";
  324. Console.WriteLine(_status);
  325. _article = new();
  326. _articleClone = new();
  327. _document = null;
  328. _memoryStream.Close();
  329. _modalInfo.Open("Загрузка не удалась, такой документ уже есть в системе.");
  330. }
  331. file = null;
  332. stream.Close();
  333. }
  334. _modalLoading.Close();
  335. }
  336. void NewDocument()
  337. {
  338. _article = new();
  339. _articleClone = new();
  340. _document = null;
  341. _memoryStream = null;
  342. _status = "Blank document created";
  343. }
  344. async Task SaveDocument_OnClick(ArticleStatus articleNewStatus)
  345. {
  346. Console.WriteLine($"SaveDocument_OnClick. DocID: {_article.ID}, Status: {_article.Status}");
  347. if (_article.Name == null || _article.Name.Length == 0)
  348. {
  349. Console.WriteLine($"SaveDocument, empty article name. DocID: {_article.ID}, Status: {_article.Status}, filename: {_article.Filename}");
  350. _modalInfo.Open( $"Для сохранения документа необходимо ввести название.");
  351. return;
  352. }
  353. if (_article.Status == ArticleStatus.New && _memoryStream == null)
  354. {
  355. Console.WriteLine($"SaveDocument, empty source file. DocID: {_article.ID}, Status: {_article.Status}, filename: {_article.Filename}");
  356. _modalInfo.Open($"Для сохранения документа необходимо прикрепить исходный файл.");
  357. return;
  358. }
  359. if (_article.Status == ArticleStatus.New && articleNewStatus == ArticleStatus.Verifying
  360. || _article.Status == ArticleStatus.AwatingVerify && articleNewStatus == ArticleStatus.Saved
  361. || _article.Status == ArticleStatus.Verifying && articleNewStatus == ArticleStatus.Saved
  362. || _article.Status == ArticleStatus.Verified && articleNewStatus == ArticleStatus.Saved
  363. || _article.Status == ArticleStatus.Verified && articleNewStatus == ArticleStatus.Verifying)
  364. {
  365. Console.WriteLine($"SaveDocument, wrong status. DocID: {_article.ID}, Status: {_article.Status}");
  366. _modalInfo.Open($"Текущий статус документа не позволяет сохранение.<br>DocID: {_article.ID}, Status: {GetDisplayName(_article.Status)}");
  367. }
  368. else
  369. {
  370. /// all is fine, continue
  371. MySQLConnector dbCon = MySQLConnector.Instance();
  372. long id;
  373. string stringSQL;
  374. if (_article.Status == ArticleStatus.New)
  375. {
  376. _modalLoading.Open();
  377. stringSQL = $"INSERT INTO articles (filename, article_name, authors, date_publish, annotation, keywords, file_hash, " +
  378. $"doc_noungroups, doc_entities, doc_morph, doc_keywords1, doc_keywords2) " +
  379. $"VALUES ('{_article.Filename}', '{_article.Name}', '{_article.Authors}', '{_article.PublishDate:yyyy-MM-dd}'," +
  380. $"'{_article.Annotation}', '{_article.Keywords}', '{_article.HashSum}'," +
  381. $"'{_article.NounGroups}', '{_article.Entities}', '{_article.Morph}', '{_article.Keywords1}', '{_article.Keywords2}' )";
  382. id = await dbCon.SQLInsert(stringSQL);
  383. _article.ID = (int)id;
  384. ///todo добавлять новый док в массив AppData.Articles
  385. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
  386. $"VALUES ('{id}', '{(int)articleNewStatus}', '{_currentAccount.UUID}')";
  387. await dbCon.SQLInsert(stringSQL);
  388. await SaveFiles((int)id);
  389. _article.Status = ArticleStatus.Saved;
  390. _articleClone = (ArticleModel)_article.Clone();
  391. /// reloading articles
  392. await AppData.LoadArticles();
  393. _counter = 1;
  394. _modalLoading.Close();
  395. _modalInfo.Open("Документ успешно создан.");
  396. }
  397. else
  398. {
  399. await UpdateDocument(articleNewStatus);
  400. //await SaveFiles(_article.ID);
  401. if (_article.Status == ArticleStatus.AwatingVerify || _article.Status == ArticleStatus.Verifying)
  402. await _article.SaveLastVerificationPoint(_verificationPoint, _currentAccount.UUID);
  403. }
  404. }
  405. }
  406. async Task LoadDocument(int docid)
  407. {
  408. ///todo загружать из массива AppData.Articles
  409. Console.WriteLine($"LoadDocument, docid: {docid}.");
  410. _modalLoading.Open();
  411. if (docid > 0)
  412. {
  413. MySQLConnector dbCon = MySQLConnector.Instance();
  414. string stringSQL = $"SELECT articles.id, filename, article_name, authors, date_publish, annotation, keywords, action_type, rating, file_hash, " +
  415. $"doc_noungroups, doc_entities, doc_morph, doc_keywords1, doc_keywords2 " +
  416. $"FROM articles " +
  417. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  418. $"WHERE articles.id={docid} " +
  419. $"ORDER BY actions_history.id DESC LiMIT 1";
  420. AppData.CurrentArticleClone = await dbCon.SQLSelectArticle(stringSQL);
  421. await AppData.CurrentArticleClone.GetVerificationHistory(_currentAccount.UUID);
  422. if (AppData.CurrentArticleClone.VerificationHistory.Count > 0)
  423. _verificationPoint = AppData.CurrentArticleClone.VerificationHistory.Values.Last();
  424. else
  425. _verificationPoint = new();
  426. AppData.CurrentArticle = (ArticleModel)AppData.CurrentArticleClone.Clone();
  427. //string initiator = await _article.GetInitiatorUUID();
  428. //initiatorAcc = AccountModel.Find(initiator);
  429. //_status = $"Article ID={DocID} loaded, status: {_article.Status}, initiator: {initiatorAcc.Name}";
  430. _status = $"Article ID={docid} loaded, status: {AppData.CurrentArticle.Status}.";
  431. _article = AppData.CurrentArticle ?? (new());
  432. _articleClone = AppData.CurrentArticleClone ?? (new());
  433. }
  434. _modalLoading.Close();
  435. }
  436. async Task SendToVerify_OnClick()
  437. {
  438. if (_article.Status == ArticleStatus.New || _article.Status == ArticleStatus.Saved)
  439. {
  440. Console.WriteLine($"SendToVerify, DocID: {_article.ID}, Status: {_article.Status}");
  441. _modalLoading.Open();
  442. /// form validation
  443. List<string> errorFields = ValidateForm<ArticleModel>(_article);
  444. if (errorFields.Count > 0)
  445. {
  446. _modalLoading.Close();
  447. string invalid_fields = string.Join(", ", errorFields);
  448. _modalInfo.Open($"Не заполнены поля: {invalid_fields}");
  449. Console.WriteLine($"SendToVerify. Required fields: '{invalid_fields}' is not filled.");
  450. return;
  451. }
  452. await UpdateDocument(ArticleStatus.AwatingVerify);
  453. _modalLoading.Close();
  454. }
  455. else
  456. {
  457. Console.WriteLine($"SendToVerify, wrong status. DocID: {_article.ID}, Status: {_article.Status}");
  458. _modalInfo.Open($"Документ не может быть отправлен на верификацию.<br>DocID: {_article.ID}, Status: {GetDisplayName(_article.Status)}");
  459. }
  460. }
  461. async Task Verify_OnClick()
  462. {
  463. if (_article.Status == ArticleStatus.AwatingVerify || _article.Status == ArticleStatus.Verifying)
  464. {
  465. Console.WriteLine($"Verify, DocID: {_article.ID}, Status: {_article.Status}");
  466. _modalLoading.Open();
  467. /// form validation
  468. List<string> errorFields = ValidateForm<ArticleModel>(_article);
  469. if (errorFields.Count > 0)
  470. {
  471. _modalLoading.Close();
  472. string invalid_fields = string.Join(", ", errorFields);
  473. _modalInfo.Open($"Не заполнены поля: {invalid_fields}");
  474. Console.WriteLine($"Verify. Required fields: '{invalid_fields}' is not filled.");
  475. return;
  476. }
  477. await UpdateDocument(ArticleStatus.Verified);
  478. _modalLoading.Close();
  479. }
  480. else
  481. {
  482. Console.WriteLine($"Verify, wrong status. DocID: {_article.ID}, Status: {_article.Status}");
  483. _modalInfo.Open($"Документ не может быть верифицирован.<br>DocID: {_article.ID}, Status: {GetDisplayName(_article.Status)}");
  484. }
  485. }
  486. async Task UpdateDocument(ArticleStatus articleStatus)
  487. {
  488. _modalLoading.Open();
  489. MySQLConnector dbCon = MySQLConnector.Instance();
  490. string stringSQL;
  491. string rating = (_article.Rating == null) ? "NULL" : _article.Rating.ToString();
  492. stringSQL = $"UPDATE articles " +
  493. $"SET filename='{_article.Filename}', article_name='{_article.Name}', authors='{_article.Authors}', " +
  494. $"date_publish='{_article.PublishDate:yyyy-MM-dd}', annotation='{_article.Annotation}', " +
  495. $"keywords='{_article.Keywords}', rating={rating}, file_hash='{_article.HashSum}' " +
  496. $"WHERE id={_article.ID}";
  497. await dbCon.SQLInsert(stringSQL);
  498. ///todo обновлять док в массиве AppData.Articles
  499. stringSQL = $"INSERT INTO actions_history (article_id, action_type, acc_id) " +
  500. $"VALUES ('{_article.ID}', '{(int)articleStatus}', '{_currentAccount.UUID}')";
  501. await dbCon.SQLInsert(stringSQL);
  502. ///если нужно фиксировать изменение статуса в поле (табл articles_edit_log)
  503. //_article.Status = articleStatus;
  504. Dictionary<string, PropertyInfo> propDict = Compare.SimpleCompare<ArticleModel>(_article, _articleClone);
  505. foreach (KeyValuePair<string, PropertyInfo> prop in propDict)
  506. {
  507. //Console.WriteLine($"property name: {prop.Key}, value: {prop.Value.GetValue(articleModel, null)}");
  508. stringSQL = $"INSERT INTO articles_edit_log (article_id, acc_id, field_name, field_prevvalue, field_newvalue) " +
  509. $"VALUES ('{_article.ID}', '{_currentAccount.UUID}', '{prop.Key}', '{prop.Value.GetValue(_articleClone, null)}', '{prop.Value.GetValue(_article, null)}')";
  510. await dbCon.SQLInsert(stringSQL);
  511. }
  512. _article.Status = articleStatus;
  513. _articleClone = (ArticleModel)_article.Clone();
  514. /// reloading articles
  515. await AppData.LoadArticles();
  516. _modalLoading.Close();
  517. string message = propDict.Count > 0 ? "Все изменения успешно сохранены." : "Изменений не найдено.";
  518. _modalInfo.Open(message);
  519. }
  520. async Task DocSelect_OnChange(ChangeEventArgs e)
  521. {
  522. int docid = int.Parse(e.Value.ToString());
  523. Console.WriteLine($"DocSelect_OnChange. docid: {docid}");
  524. await LoadDocument(docid);
  525. }
  526. async Task SaveFiles(int docid)
  527. {
  528. if (_document != null )
  529. {
  530. /// получаем html, сохраняем файлы
  531. GetHtmlParam htmlParams = new();
  532. htmlParams.OutHtmlAndBodyTags = true;
  533. string html = _document.GetHtmlString(htmlParams);
  534. string fullpath;
  535. string htmldirectorypath;
  536. string docdirectorypath;
  537. #if DEBUG
  538. htmldirectorypath = Path.Combine(Environment.CurrentDirectory, "wwwroot", STORAGE_FOLDER_NAME, "html");
  539. docdirectorypath = Path.Combine(Environment.CurrentDirectory, "wwwroot", STORAGE_FOLDER_NAME, "source");
  540. #else
  541. htmldirectorypath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", STORAGE_FOLDER_NAME, "html");
  542. docdirectorypath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", STORAGE_FOLDER_NAME, "source");
  543. #endif
  544. ///html
  545. fullpath = Path.Combine(htmldirectorypath, $"{docid}_{_document.SourceFileName}.html");
  546. Console.WriteLine($"Saving file [{fullpath}]");
  547. Directory.CreateDirectory(htmldirectorypath);
  548. File.WriteAllBytes(fullpath, Encoding.UTF8.GetBytes(html));
  549. ///original files
  550. fullpath = Path.Combine(docdirectorypath, $"{docid}_{_article.Filename}");
  551. Console.WriteLine($"Saving file [{fullpath}]");
  552. Directory.CreateDirectory(docdirectorypath);
  553. FileStream fs = new(fullpath, FileMode.Create, FileAccess.Write);
  554. _memoryStream.Position = 0;
  555. await _memoryStream.CopyToAsync(fs);
  556. _status = $"User has saved new article data: [{docid}_{_article.Filename}], memory size:{_memoryStream.Length}b, file size: {fs.Length}b";
  557. Console.WriteLine(_status);
  558. _memoryStream.Close();
  559. fs.Close();
  560. _document = null;
  561. }
  562. }
  563. async Task<string> CalculateHashSum(MemoryStream ms)
  564. {
  565. MD5CryptoServiceProvider md5Provider = new();
  566. ms.Position = 0;
  567. byte[] hash = await md5Provider.ComputeHashAsync(ms);
  568. return Convert.ToBase64String(hash);
  569. }
  570. List<string> ValidateForm<T>(T obj)
  571. {
  572. var props = typeof(T).GetProperties().Where(pi => Attribute.IsDefined(pi, typeof(RequiredAttribute)));
  573. List<string> result = new();
  574. foreach (var prop in props)
  575. {
  576. var val = prop.GetValue(obj, null);
  577. if (val == null || val?.ToString().Length == 0)
  578. result.Add(prop.Name);
  579. //Console.WriteLine($"Required field '{prop.Name}' is not filled.");
  580. }
  581. return result;
  582. }
  583. static string GetDisplayName(Enum enumValue)
  584. {
  585. return enumValue.GetType()
  586. .GetMember(enumValue.ToString())
  587. .First()
  588. .GetCustomAttribute<DisplayAttribute>()
  589. .GetName();
  590. }
  591. async Task<AccountModel> GetCurrentAcc()
  592. {
  593. Console.WriteLine($"Desktop GetCurrentAcc");
  594. if (_currentAccount == null)
  595. {
  596. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  597. var user = authState.User;
  598. if (user.Identity.IsAuthenticated)
  599. {
  600. ///tmp
  601. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  602. var currentUser = await UserManager.GetUserAsync(user);
  603. if (accounts.ContainsKey(currentUser.Id))
  604. _currentAccount = accounts[currentUser.Id];
  605. /// asp roles
  606. //var role1 = new IdentityRole { Name = "admin" };
  607. //var result = await RoleManager.CreateAsync(role1);
  608. //Console.WriteLine($"RoleManager create result: {result}");
  609. //result = await UserManager.AddToRoleAsync(currentUser, role1.Name);
  610. //Console.WriteLine($"RoleManager add role result: {result}");
  611. }
  612. }
  613. return _currentAccount;
  614. }
  615. }
  616. }