Projekt

Obecné

Profil

Stáhnout (23.2 KB) Statistiky
| Větev: | Tag: | Revize:
1
using Core.Contexts;
2
using Core.Entities;
3
using Models.Annotations;
4
using Models.Enums;
5
using Serilog;
6
using System;
7
using System.Collections.Generic;
8
using System.Linq;
9
using System.Text;
10
using System.Threading.Tasks;
11
using Microsoft.EntityFrameworkCore;
12
using AutoMapper;
13
using Models.Tags;
14
using Ganss.XSS;
15
using HtmlAgilityPack;
16
using System.Text.RegularExpressions;
17
using Newtonsoft.Json;
18
using Core.GraphUtils;
19

    
20
namespace Core.Services.AnnotationService
21
{
22
    public class AnnotationServiceEF : IAnnotationService
23
    {
24
        private readonly DatabaseContext context;
25
        private readonly ILogger logger;
26
        private readonly IMapper mapper;
27
        private readonly IHTMLService htmlService;
28

    
29

    
30
        public AnnotationServiceEF(DatabaseContext context, ILogger logger, IMapper mapper, IHTMLService htmlService)
31
        {
32
            this.context = context;
33
            this.logger = logger;
34
            this.mapper = mapper;
35
            this.htmlService = htmlService;
36
        }
37

    
38
        public void CreateDocumentAnnotations(AnnotationsAddRequest request, Guid clientUserId)
39
        {
40
            User addingUser = context.Users.Single(u => u.Id == clientUserId);
41

    
42
            // Check the documents exist
43
            var documents = context.Documents.Where(d => request.DocumentIdList.Contains(d.Id)).ToList();
44
            if (documents.Count() != request.DocumentIdList.Count)
45
            {
46
                logger.Information($"Received a non-existent Document ID when assigning documents to users");
47
                throw new InvalidOperationException($"{request.DocumentIdList.Count - documents.Count()} of the received documents do not exist");
48
            }
49

    
50
            var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList();
51
            foreach (var user in users)
52
            {
53
                var userAnnotatedDocuments = context.Annotations.Where(a => a.User == user).Select(a => a.Document).ToList();
54
                foreach (var doc in documents)
55
                {
56
                    if (userAnnotatedDocuments.Contains(doc))
57
                    {
58
                        logger.Information($"User {user.Username} has already been assigned the document {doc.Id}, ignoring");
59
                        continue;
60
                    }
61

    
62
                    context.Annotations.Add(new Annotation()
63
                    {
64
                        User = user,
65
                        UserAssigned = addingUser,
66
                        DateAssigned = DateTime.Now,
67
                        DateLastChanged = DateTime.Now,
68
                        Document = doc,
69
                        State = EState.NEW,
70
                        Note = ""
71
                    });
72
                }
73
            }
74

    
75
            context.SaveChanges();
76
        }
77

    
78
        public AnnotationListResponse GetUserAnnotations(Guid userId)
79
        {
80
            var annotations = context.Annotations
81
                .Where(a => a.User.Id == userId)
82
                .Include(a => a.Document)
83
                .ToList();
84
            var infos = new List<AnnotationListInfo>();
85
            foreach (var annotation in annotations)
86
            {
87
                infos.Add(new AnnotationListInfo()
88
                {
89
                    AnnotationId = annotation.Id,
90
                    DocumentName = annotation.Document.Name,
91
                    State = annotation.State
92
                });
93
            }
94

    
95
            return new AnnotationListResponse()
96
            {
97
                Annotations = infos
98
            };
99
        }
100

    
101
        public void AddNoteToAnnotation(Guid annotationId, Guid userId, ERole userRole, AddNoteToAnnotationRequest request)
102
        {
103
            Annotation annotation = null;
104
            try
105
            {
106
                annotation = context.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
107
            }
108
            catch (Exception)
109
            {
110
                throw new InvalidOperationException("Annotation not found");
111
            }
112

    
113
            if (userRole < ERole.ADMINISTRATOR && annotation.User.Id != userId)
114
            {
115
                throw new UnauthorizedAccessException("User does not have access to this annotation");
116
            }
117

    
118
            annotation.Note = request.Note;
119
            context.SaveChanges();
120
        }
121

    
122

    
123
        public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole)
124
        {
125
            var annotation = context.Annotations
126
                .Where(a => a.Id == annotationId)
127
                .Include(a => a.User)
128
                .Include(a => a.Document).ThenInclude(d => d.Content)
129
                .First();
130

    
131
            if (userRole < ERole.ADMINISTRATOR)
132
            {
133
                if (annotation.User.Id != userId)
134
                {
135
                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
136
                }
137
            }
138

    
139
            var documentContent = context.Documents.Where(d => d.Id == annotation.Document.Id).Select(d => d.Content).First();
140

    
141
            var tags = context.AnnotationTags.Where(at => at.Annotation.Id == annotationId)
142
                .Include(at => at.Tag)
143
                .ThenInclude(t => t.Category)
144
                .Include(at => at.SubTag)
145
                .OrderBy(at => at.Position)
146
                .Select(at => at as AnnotationTagGeneric)
147
                .ToList();
148

    
149
            List<TagInstanceInfo> tagInstanceInfos = new();
150
            foreach (var tag in tags)
151
            {
152
                var tagInstance = mapper.Map<TagInstanceInfo>(tag);
153
                tagInstanceInfos.Add(tagInstance);
154
            }
155

    
156
            var docToRender = "";
157
            HTMLService.CachedInfo cachedInfoToReturn = new(); 
158
            if (annotation.CachedDocumentHTML == "")
159
            {
160
                var result = htmlService.FullPreprocessHTML(documentContent.Content, tags);
161
                cachedInfoToReturn = result.Item2;
162
                docToRender = result.Item1;
163

    
164
                annotation.CachedStartPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartPositions);
165
                annotation.CachedLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartLengths);
166
                annotation.CachedClosingPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingPositions);
167
                annotation.CachedClosingLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingLengths);
168
                annotation.CachedCSS = JsonConvert.SerializeObject(cachedInfoToReturn.TagInstanceCSS);
169
                annotation.ModifiedType = EModified.NONE;
170
                annotation.CachedDocumentHTML = docToRender;
171
                context.SaveChanges();
172
            }
173
            else
174
            {
175
                docToRender = annotation.CachedDocumentHTML;
176
                cachedInfoToReturn = new()
177
                {
178
                    TagStartPositions = JsonConvert.DeserializeObject<List<int>>(annotation.CachedStartPositions),
179
                    TagStartLengths = JsonConvert.DeserializeObject<List<int>>(annotation.CachedLengths),
180
                    TagClosingPositions = JsonConvert.DeserializeObject<List<int>>(annotation.CachedClosingPositions),
181
                    TagClosingLengths = JsonConvert.DeserializeObject<List<int>>(annotation.CachedClosingLengths),
182
                    TagInstanceCSS = JsonConvert.DeserializeObject<List<TagInstanceCSSInfo>>(annotation.CachedCSS)
183
                };
184
                
185
                // The annotation has been modified and we need to either add the new tag or remove the tag
186
                if (annotation.ModifiedType != EModified.NONE)
187
                {
188
                    if (annotation.ModifiedType == EModified.ADDED)
189
                    {
190
                        var lastModifiedTag = context.AnnotationTags.Where(at => at.Id == annotation.LastModifiedTagId).First();
191
                        var result = htmlService.PartialPreprocessHTMLAddTag(docToRender, documentContent.Content, lastModifiedTag, tags, cachedInfoToReturn);
192
                        docToRender = result.Item1;
193
                        cachedInfoToReturn = result.Item2;
194
                    }
195
                    else if (annotation.ModifiedType == EModified.REMOVED)
196
                    {
197
                        var result = htmlService.PartialPreprocessHTMLRemoveTag(docToRender, documentContent.Content, new AnnotationTag() { Id = annotation.LastModifiedTagId.Value }, tags, cachedInfoToReturn);
198
                        docToRender = result.Item1;
199
                        cachedInfoToReturn = result.Item2;
200
                    }
201

    
202
                    annotation.ModifiedType = EModified.NONE;
203
                    annotation.CachedStartPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartPositions);
204
                    annotation.CachedLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartLengths);
205
                    annotation.CachedClosingPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingPositions);
206
                    annotation.CachedClosingLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingLengths);
207
                    annotation.CachedDocumentHTML = docToRender;
208
                    annotation.CachedCSS = JsonConvert.SerializeObject(cachedInfoToReturn.TagInstanceCSS);
209
                    context.SaveChanges();
210
                }
211
            }
212

    
213
            // We probably cannot use AutoMapper since we are dealing with too many different entities
214
            AnnotationInfo annotationInfo = new()
215
            {
216
                SourceDocumentContent = documentContent.Content,
217
                DocumentToRender = docToRender,
218
                TagStartPositions = cachedInfoToReturn.TagStartPositions.ToArray(),
219
                TagLengths = cachedInfoToReturn.TagStartLengths.ToArray(),
220
                Note = annotation.Note,
221
                State = annotation.State,
222
                Type = IsHtml(documentContent.Content) ? EDocumentType.HTML : EDocumentType.TEXT,
223
                TagInstances = tagInstanceInfos,
224
                CSSInfo = cachedInfoToReturn.TagInstanceCSS
225
            };
226

    
227
            return annotationInfo;
228
        }
229

    
230
        // TODO temporary
231
        private bool IsHtml(string text)
232
        {
233
            return text.Contains("<html>");
234
        }
235

    
236
        public void AddAnnotationInstance(Guid annotationId, Guid userId, ERole userRole, AnnotationInstanceAddRequest request)
237
        {
238
            var annotation = context.Annotations
239
               .Where(a => a.Id == annotationId)
240
               .Include(a => a.User)
241
               .Include(a => a.Document).ThenInclude(d => d.Content)
242
               .First();
243

    
244
            if (userRole < ERole.ADMINISTRATOR)
245
            {
246
                if (annotation.User.Id != userId)
247
                {
248
                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
249
                }
250
            }
251

    
252
            AnnotationTag annotationTag = new()
253
            {
254
                Id = Guid.NewGuid(),
255
                Annotation = annotation,
256
                Instance = request.InstanceId == null ? Guid.NewGuid() : request.InstanceId.Value,
257
                Length = request.Length,
258
                Position = request.Position,
259
                SelectedText = request.SelectedText,
260
                Note = ""
261
            };
262

    
263
            if (request.Type == ETagType.TAG)
264
            {
265
                annotationTag.Tag = context.Tags.Where(t => t.Id == request.Id).Single();
266
                annotationTag.SubTag = null;
267

    
268
                if (annotationTag.Tag.SentimentEnabled)
269
                {
270
                    annotationTag.Sentiment = ETagSentiment.NEUTRAL;
271
                }
272

    
273
                // If for the same annotation exists a tag with same position and length and of the same type, ignore
274
                if (context.AnnotationTags.Any(at =>
275
                at.Position == annotationTag.Position &&
276
                at.Length == annotationTag.Length &&
277
                at.Annotation == annotation &&
278
                at.Tag == annotationTag.Tag))
279
                {
280
                    throw new InvalidOperationException("Duplicate tag");
281
                }
282

    
283
            }
284
            else if (request.Type == ETagType.SUBTAG)
285
            {
286
                var subTag = context.SubTags.Where(st => st.Id == request.Id).Include(st => st.Tag).Single();
287
                annotationTag.SubTag = subTag;
288
                annotationTag.Tag = subTag.Tag;
289

    
290
                if (annotationTag.SubTag.SentimentEnabled)
291
                {
292
                    annotationTag.Sentiment = ETagSentiment.NEUTRAL;
293
                }
294

    
295
                if (context.AnnotationTags.Any(at =>
296
                at.Position == annotationTag.Position &&
297
                at.Length == annotationTag.Length &&
298
                at.Annotation == annotation &&
299
                at.Tag == annotationTag.Tag &&
300
                at.SubTag == annotationTag.SubTag))
301
                {
302
                    throw new InvalidOperationException("Duplicate tag");
303
                }
304
            }
305
            else
306
            {
307
                throw new ArgumentException($"Unknown tag type {request.Type}");
308
            }
309

    
310
            if (annotation.State == EState.NEW)
311
            {
312
                annotation.State = EState.IN_PROGRESS;
313
            }
314

    
315
            annotation.LastModifiedTagId = annotationTag.Id;
316
            annotation.ModifiedType = EModified.ADDED;
317

    
318
            context.AnnotationTags.Add(annotationTag);
319
            context.SaveChanges();
320
        }
321

    
322
        public void DeleteAnnotationInstance(Guid annotationId, Guid tagInstanceId, Guid loggedUserId, ERole userRole)
323
        {
324
            Annotation annotation = null;
325
            try
326
            {
327
                annotation = context.Annotations
328
                   .Where(a => a.Id == annotationId)
329
                   .Include(a => a.User)
330
                   .Include(a => a.Document).ThenInclude(d => d.Content)
331
                   .First();
332

    
333
            }
334
            catch (Exception ex)
335
            {
336
                throw new InvalidOperationException("Could not find annotation");
337
            }
338

    
339

    
340
            if (userRole < ERole.ADMINISTRATOR)
341
            {
342
                if (annotation.User.Id != loggedUserId)
343
                {
344
                    throw new UnauthorizedAccessException($"User {loggedUserId} does not have assigned annotation {annotationId}");
345
                }
346
            }
347

    
348
            if (!context.AnnotationTags.Any(at => at.Id == tagInstanceId))
349
            {
350
                throw new InvalidOperationException("Could not find tag instance");
351
            }
352

    
353

    
354
            var annotationTag = context.AnnotationTags.First(at => at.Id == tagInstanceId);
355
            annotation.LastModifiedTagId = annotationTag.Id;
356
            annotation.ModifiedType = EModified.REMOVED;
357

    
358
            context.AnnotationTags.Remove(annotationTag);
359

    
360
            context.SaveChanges();
361
        }
362

    
363
        public void SetTagInstanceSentiment(Guid annotationId, Guid instanceId, Guid userId, ERole userRole, ETagSentiment sentiment)
364
        {
365
            Annotation annotation = null;
366
            try
367
            {
368
                annotation = context.Annotations
369
                   .Where(a => a.Id == annotationId)
370
                   .Include(a => a.User)
371
                   .Include(a => a.Document).ThenInclude(d => d.Content)
372
                   .First();
373

    
374
            }
375
            catch (Exception ex)
376
            {
377
                throw new InvalidOperationException("Could not find annotation");
378
            }
379

    
380

    
381
            if (userRole < ERole.ADMINISTRATOR)
382
            {
383
                if (annotation.User.Id != userId)
384
                {
385
                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
386
                }
387
            }
388

    
389

    
390
            var tagInstances = context.AnnotationTags.Where(at => at.Instance == instanceId).ToList();
391
            if (tagInstances.Count() == 0)
392
            {
393
                throw new InvalidOperationException("No such instance found");
394
            }
395

    
396
            foreach (var tagInstance in tagInstances)
397
            {
398
                tagInstance.Sentiment = sentiment;
399
            }
400

    
401
            context.SaveChanges();
402
        }
403

    
404
        public void MarkAnnotationAsDone(Guid annotationId, Guid userId, ERole userRole, bool done)
405
        {
406
            Annotation annotation = null;
407
            try
408
            {
409
                annotation = context.Annotations
410
                   .Where(a => a.Id == annotationId)
411
                   .Include(a => a.User)
412
                   .Include(a => a.Document).ThenInclude(d => d.Content)
413
                   .First();
414

    
415
            }
416
            catch (Exception ex)
417
            {
418
                throw new InvalidOperationException("Could not find annotation");
419
            }
420

    
421

    
422
            if (userRole < ERole.ADMINISTRATOR)
423
            {
424
                if (annotation.User.Id != userId)
425
                {
426
                    throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
427
                }
428
            }
429

    
430
            annotation.State = done ? EState.DONE : EState.IN_PROGRESS;
431
            context.SaveChanges();
432
        }
433

    
434
        public Guid CreateFinalAnnotation(Guid documentId, Guid userId)
435
        {
436
            var document = context.Documents.Single(d => d.Id == documentId);
437
            var user = context.Users.Single(u => u.Id == userId);
438

    
439
            // Remove existing 
440
            if (context.FinalAnnotations.Any(fa => fa.Document == document))
441
            {
442
                var finalAnnotationOld = context.FinalAnnotations.Single(fa => fa.Document == document);
443
                context.FinalAnnotations.Remove(finalAnnotationOld);
444
            }
445

    
446
            var annotations = context.Annotations
447
                .Include(a => a.Document)
448
                .Where(a => a.Document == document && a.State == EState.DONE)
449
                .ToList();
450

    
451
            var finalAnnotation = new FinalAnnotation()
452
            {
453
                Id = Guid.NewGuid(),
454
                DateAssigned = DateTime.Now,
455
                DateLastChanged = DateTime.Now,
456
                Document = document,
457
                User = user,
458
                UserAssigned = user,
459
                Annotations = annotations,
460
                State = EState.NEW
461
            };
462

    
463
            List<AnnotationTag> annotationTagsAll = new();
464
            foreach (var annotation in annotations)
465
            {
466
                annotationTagsAll.AddRange(context.AnnotationTags
467
                    .Where(at => at.Annotation == annotation)
468
                    .Include(at => at.Tag)
469
                    .Include(at => at.SubTag)
470
                    .Include(at => at.Annotation)
471
                    .ThenInclude(a => a.User)
472
                    .ToList());
473
            }
474
            annotationTagsAll = annotationTagsAll.OrderBy(at => at.Position).ToList();
475

    
476
            Dictionary<Guid, List<AnnotationTag>> occurenceDict = new();
477
            foreach (var annotationTag in annotationTagsAll)
478
            {
479
                if (occurenceDict.ContainsKey(annotationTag.Instance))
480
                {
481
                    occurenceDict[annotationTag.Instance].Add(annotationTag);
482
                }
483
                else
484
                {
485
                    occurenceDict[annotationTag.Instance] = new();
486
                    occurenceDict[annotationTag.Instance].Add(annotationTag);
487
                }
488
            }
489

    
490
            List<List<AnnotationTag>> occurenceLists = new();
491
            foreach (var key in occurenceDict.Keys)
492
            {
493
                occurenceLists.Add(occurenceDict[key]);
494
            }
495

    
496
            List<List<AnnotationTag>> annotationTagsProcessed = new();
497
            List<FinalAnnotationTag> finalAnnotationTags = new();
498
            for (int i = 0; i < occurenceLists.Count; i++)
499
            {
500
                var occurrenceList1 = occurenceLists[i];
501
                List<List<AnnotationTag>> sameLists = new();
502

    
503
                for (int j = 0; j < occurenceLists.Count; j++)
504
                {
505
                    var occurrenceList2 = occurenceLists[j];
506

    
507
                    if (annotationTagsProcessed.Contains(occurrenceList2))
508
                    {
509
                        continue;
510
                    }
511

    
512
                    if (SelectionsAreSame(occurrenceList1, occurrenceList2))
513
                    {
514
                        sameLists.Add(occurrenceList2);
515
                    }
516
                }
517

    
518
                // This means that this occurrence ahs already been processed as matching a previous tag
519
                if (sameLists.Count() == 0)
520
                {
521
                    continue;
522
                }
523

    
524
                List<User> relatedUsers = new();
525
                foreach (var list in sameLists)
526
                {
527
                    relatedUsers.Add(list[0].Annotation.User);
528
                }
529

    
530
                foreach (var tag in sameLists[0])
531
                {
532
                    finalAnnotationTags.Add(new()
533
                    {
534
                        Id = Guid.NewGuid(),
535
                        Tag = tag.Tag,
536
                        SubTag = tag.SubTag,
537
                        Annotation = finalAnnotation,
538
                        SelectedText = tag.SelectedText,
539
                        Sentiment = tag.Sentiment,
540
                        Instance = tag.Instance,
541
                        IsFinal = sameLists.Count == annotations.Count,
542
                        Length = tag.Length,
543
                        Position = tag.Position,
544
                        Note = "",
545
                        Users = relatedUsers
546
                    });
547
                }
548

    
549
                annotationTagsProcessed.AddRange(sameLists);
550
            }
551

    
552
            context.FinalAnnotations.Add(finalAnnotation);
553
            context.SaveChanges();
554
            context.FinalAnnotationTags.AddRange(finalAnnotationTags);
555

    
556
            context.SaveChanges();
557
            return finalAnnotation.Id;
558
        }
559

    
560
        private bool SelectionsAreSame(List<AnnotationTag> list1, List<AnnotationTag> list2)
561
        {
562
            if (list1.Count != list2.Count)
563
            {
564
                return false;
565
            }
566

    
567
            bool sentimentEnabled = list1[0].Tag.SentimentEnabled;
568

    
569
            if (sentimentEnabled)
570
            {
571
                for (int i = 0; i < list1.Count; i++)
572
                {
573
                    var tag1 = list1[i];
574
                    var tag2 = list2[i];
575
                    if (tag1.Position == tag2.Position &&
576
                        tag1.Length == tag2.Length &&
577
                        tag1.Sentiment == tag2.Sentiment &&
578
                        tag1.Tag == tag2.Tag &&
579
                        tag1.SubTag == tag2.SubTag)
580
                    {
581
                        continue;
582
                    }
583
                    else
584
                    {
585
                        return false;
586
                    }
587
                }
588
            }
589
            else
590
            {
591
                for (int i = 0; i < list1.Count; i++)
592
                {
593
                    var tag1 = list1[i];
594
                    var tag2 = list2[i];
595
                    if (tag1.Position == tag2.Position &&
596
                        tag1.Length == tag2.Length &&
597
                        tag1.Tag == tag2.Tag &&
598
                        tag1.SubTag == tag2.SubTag)
599
                    {
600
                        continue;
601
                    }
602
                    else
603
                    {
604
                        return false;
605
                    }
606
                }
607
            }
608

    
609
            return true;
610
        }
611
    }
612
}
(1-1/2)