1 |
eff8ec56
|
Vojtěch Bartička
|
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 |
6bdb3d95
|
Vojtěch Bartička
|
using Microsoft.EntityFrameworkCore;
|
12 |
a6675a6d
|
Vojtěch Bartička
|
using AutoMapper;
|
13 |
|
|
using Models.Tags;
|
14 |
3c185841
|
Vojtěch Bartička
|
using Ganss.XSS;
|
15 |
|
|
using HtmlAgilityPack;
|
16 |
|
|
using System.Text.RegularExpressions;
|
17 |
553486ad
|
Vojtěch Bartička
|
using Newtonsoft.Json;
|
18 |
8dc25caa
|
Vojtěch Bartička
|
using Core.GraphUtils;
|
19 |
eff8ec56
|
Vojtěch Bartička
|
|
20 |
|
|
namespace Core.Services.AnnotationService
|
21 |
|
|
{
|
22 |
|
|
public class AnnotationServiceEF : IAnnotationService
|
23 |
|
|
{
|
24 |
|
|
private readonly DatabaseContext context;
|
25 |
|
|
private readonly ILogger logger;
|
26 |
a6675a6d
|
Vojtěch Bartička
|
private readonly IMapper mapper;
|
27 |
2c9afc72
|
Vojtěch Bartička
|
private readonly IHTMLService htmlService;
|
28 |
eff8ec56
|
Vojtěch Bartička
|
|
29 |
0a9f9349
|
Vojtěch Bartička
|
|
30 |
2c9afc72
|
Vojtěch Bartička
|
public AnnotationServiceEF(DatabaseContext context, ILogger logger, IMapper mapper, IHTMLService htmlService)
|
31 |
eff8ec56
|
Vojtěch Bartička
|
{
|
32 |
|
|
this.context = context;
|
33 |
|
|
this.logger = logger;
|
34 |
a6675a6d
|
Vojtěch Bartička
|
this.mapper = mapper;
|
35 |
2c9afc72
|
Vojtěch Bartička
|
this.htmlService = htmlService;
|
36 |
eff8ec56
|
Vojtěch Bartička
|
}
|
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 |
153d77a8
|
Vojtěch Bartička
|
var documents = context.Documents.Where(d => request.DocumentIdList.Contains(d.Id)).ToList();
|
44 |
|
|
if (documents.Count() != request.DocumentIdList.Count)
|
45 |
eff8ec56
|
Vojtěch Bartička
|
{
|
46 |
153d77a8
|
Vojtěch Bartička
|
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 |
eff8ec56
|
Vojtěch Bartička
|
}
|
49 |
|
|
|
50 |
6bdb3d95
|
Vojtěch Bartička
|
var users = context.Users.Where(u => request.UserIdList.Contains(u.Id)).ToList();
|
51 |
153d77a8
|
Vojtěch Bartička
|
foreach (var user in users)
|
52 |
eff8ec56
|
Vojtěch Bartička
|
{
|
53 |
8c9ce202
|
Vojtěch Bartička
|
var userAnnotatedDocuments = context.Annotations
|
54 |
|
|
.Where(a => !(a is FinalAnnotation))
|
55 |
|
|
.Where(a => a.User == user)
|
56 |
|
|
.Select(a => a.Document)
|
57 |
|
|
.ToList();
|
58 |
153d77a8
|
Vojtěch Bartička
|
foreach (var doc in documents)
|
59 |
eff8ec56
|
Vojtěch Bartička
|
{
|
60 |
153d77a8
|
Vojtěch Bartička
|
if (userAnnotatedDocuments.Contains(doc))
|
61 |
eff8ec56
|
Vojtěch Bartička
|
{
|
62 |
153d77a8
|
Vojtěch Bartička
|
logger.Information($"User {user.Username} has already been assigned the document {doc.Id}, ignoring");
|
63 |
|
|
continue;
|
64 |
eff8ec56
|
Vojtěch Bartička
|
}
|
65 |
|
|
|
66 |
|
|
context.Annotations.Add(new Annotation()
|
67 |
|
|
{
|
68 |
|
|
User = user,
|
69 |
|
|
UserAssigned = addingUser,
|
70 |
|
|
DateAssigned = DateTime.Now,
|
71 |
|
|
DateLastChanged = DateTime.Now,
|
72 |
153d77a8
|
Vojtěch Bartička
|
Document = doc,
|
73 |
eff8ec56
|
Vojtěch Bartička
|
State = EState.NEW,
|
74 |
|
|
Note = ""
|
75 |
|
|
});
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
context.SaveChanges();
|
80 |
|
|
}
|
81 |
6bdb3d95
|
Vojtěch Bartička
|
|
82 |
|
|
public AnnotationListResponse GetUserAnnotations(Guid userId)
|
83 |
|
|
{
|
84 |
a49028dd
|
Vojtěch Bartička
|
var annotations = context.Annotations
|
85 |
8c9ce202
|
Vojtěch Bartička
|
.Where(a => !(a is FinalAnnotation))
|
86 |
a49028dd
|
Vojtěch Bartička
|
.Where(a => a.User.Id == userId)
|
87 |
|
|
.Include(a => a.Document)
|
88 |
|
|
.ToList();
|
89 |
8c9ce202
|
Vojtěch Bartička
|
|
90 |
6bdb3d95
|
Vojtěch Bartička
|
var infos = new List<AnnotationListInfo>();
|
91 |
a49028dd
|
Vojtěch Bartička
|
foreach (var annotation in annotations)
|
92 |
6bdb3d95
|
Vojtěch Bartička
|
{
|
93 |
|
|
infos.Add(new AnnotationListInfo()
|
94 |
|
|
{
|
95 |
a49028dd
|
Vojtěch Bartička
|
AnnotationId = annotation.Id,
|
96 |
|
|
DocumentName = annotation.Document.Name,
|
97 |
|
|
State = annotation.State
|
98 |
6bdb3d95
|
Vojtěch Bartička
|
});
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
return new AnnotationListResponse()
|
102 |
|
|
{
|
103 |
|
|
Annotations = infos
|
104 |
|
|
};
|
105 |
|
|
}
|
106 |
a6675a6d
|
Vojtěch Bartička
|
|
107 |
83d4581e
|
Vojtěch Bartička
|
public void AddNoteToAnnotation(Guid annotationId, Guid userId, ERole userRole, AddNoteToAnnotationRequest request, bool isFinal)
|
108 |
f2275185
|
Vojtěch Bartička
|
{
|
109 |
|
|
Annotation annotation = null;
|
110 |
|
|
try
|
111 |
|
|
{
|
112 |
83d4581e
|
Vojtěch Bartička
|
if (!isFinal)
|
113 |
|
|
{
|
114 |
|
|
annotation = context.Annotations.Include(a => a.User).First(a => a.Id == annotationId);
|
115 |
|
|
}
|
116 |
|
|
else
|
117 |
|
|
{
|
118 |
|
|
annotation = context.FinalAnnotations.Include(a => a.User).First(a => a.Id == annotationId);
|
119 |
|
|
}
|
120 |
f2275185
|
Vojtěch Bartička
|
}
|
121 |
|
|
catch (Exception)
|
122 |
|
|
{
|
123 |
|
|
throw new InvalidOperationException("Annotation not found");
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
if (userRole < ERole.ADMINISTRATOR && annotation.User.Id != userId)
|
127 |
|
|
{
|
128 |
|
|
throw new UnauthorizedAccessException("User does not have access to this annotation");
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
annotation.Note = request.Note;
|
132 |
|
|
context.SaveChanges();
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
|
136 |
83d4581e
|
Vojtěch Bartička
|
public AnnotationInfo GetAnnotation(Guid annotationId, Guid userId, ERole userRole, bool isFinal)
|
137 |
a6675a6d
|
Vojtěch Bartička
|
{
|
138 |
83d4581e
|
Vojtěch Bartička
|
Annotation annotation = new();
|
139 |
|
|
if (!isFinal)
|
140 |
|
|
{
|
141 |
|
|
annotation = context.Annotations
|
142 |
8c9ce202
|
Vojtěch Bartička
|
.Where(a => !(a is FinalAnnotation))
|
143 |
83d4581e
|
Vojtěch Bartička
|
.Where(a => a.Id == annotationId)
|
144 |
|
|
.Include(a => a.User)
|
145 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
146 |
|
|
.First();
|
147 |
|
|
}
|
148 |
|
|
else
|
149 |
|
|
{
|
150 |
|
|
annotation = context.FinalAnnotations
|
151 |
c2a89232
|
Vojtěch Bartička
|
.Where(a => a.Id == annotationId)
|
152 |
|
|
.Include(a => a.User)
|
153 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
154 |
|
|
.First();
|
155 |
83d4581e
|
Vojtěch Bartička
|
}
|
156 |
|
|
|
157 |
3c185841
|
Vojtěch Bartička
|
|
158 |
a6675a6d
|
Vojtěch Bartička
|
if (userRole < ERole.ADMINISTRATOR)
|
159 |
|
|
{
|
160 |
|
|
if (annotation.User.Id != userId)
|
161 |
|
|
{
|
162 |
|
|
throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
|
166 |
c2a89232
|
Vojtěch Bartička
|
var documentContent = context.Documents.Where(d => d.Id == annotation.Document.Id).Select(d => d.Content).First();
|
167 |
a6675a6d
|
Vojtěch Bartička
|
|
168 |
8c9ce202
|
Vojtěch Bartička
|
IQueryable<AnnotationTagGeneric> tagsQuery;
|
169 |
|
|
List<AnnotationTagGeneric> tags;
|
170 |
|
|
if (!isFinal)
|
171 |
|
|
{
|
172 |
|
|
tagsQuery = context.AnnotationTags.Where(at => at.Annotation.Id == annotationId);
|
173 |
|
|
}
|
174 |
|
|
else
|
175 |
|
|
{
|
176 |
|
|
tagsQuery = context.FinalAnnotationTags.Where(at => at.Annotation.Id == annotationId);
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
tags = tagsQuery.Include(at => at.Tag)
|
180 |
553486ad
|
Vojtěch Bartička
|
.ThenInclude(t => t.Category)
|
181 |
c2a89232
|
Vojtěch Bartička
|
.Include(at => at.SubTag)
|
182 |
3c89e947
|
Vojtěch Bartička
|
.OrderBy(at => at.Position)
|
183 |
8c9ce202
|
Vojtěch Bartička
|
.Select(at => at)
|
184 |
c2a89232
|
Vojtěch Bartička
|
.ToList();
|
185 |
|
|
|
186 |
0a9f9349
|
Vojtěch Bartička
|
List<TagInstanceInfo> tagInstanceInfos = new();
|
187 |
a6675a6d
|
Vojtěch Bartička
|
foreach (var tag in tags)
|
188 |
|
|
{
|
189 |
|
|
var tagInstance = mapper.Map<TagInstanceInfo>(tag);
|
190 |
0a9f9349
|
Vojtěch Bartička
|
tagInstanceInfos.Add(tagInstance);
|
191 |
a6675a6d
|
Vojtěch Bartička
|
}
|
192 |
|
|
|
193 |
553486ad
|
Vojtěch Bartička
|
var docToRender = "";
|
194 |
83d4581e
|
Vojtěch Bartička
|
HTMLService.CachedInfo cachedInfoToReturn = new();
|
195 |
553486ad
|
Vojtěch Bartička
|
if (annotation.CachedDocumentHTML == "")
|
196 |
|
|
{
|
197 |
2c9afc72
|
Vojtěch Bartička
|
var result = htmlService.FullPreprocessHTML(documentContent.Content, tags);
|
198 |
|
|
cachedInfoToReturn = result.Item2;
|
199 |
|
|
docToRender = result.Item1;
|
200 |
|
|
|
201 |
|
|
annotation.CachedStartPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartPositions);
|
202 |
|
|
annotation.CachedLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartLengths);
|
203 |
|
|
annotation.CachedClosingPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingPositions);
|
204 |
|
|
annotation.CachedClosingLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingLengths);
|
205 |
|
|
annotation.CachedCSS = JsonConvert.SerializeObject(cachedInfoToReturn.TagInstanceCSS);
|
206 |
553486ad
|
Vojtěch Bartička
|
annotation.ModifiedType = EModified.NONE;
|
207 |
|
|
annotation.CachedDocumentHTML = docToRender;
|
208 |
|
|
context.SaveChanges();
|
209 |
|
|
}
|
210 |
|
|
else
|
211 |
|
|
{
|
212 |
|
|
docToRender = annotation.CachedDocumentHTML;
|
213 |
ceadebfe
|
Vojtěch Bartička
|
cachedInfoToReturn = new()
|
214 |
2c9afc72
|
Vojtěch Bartička
|
{
|
215 |
|
|
TagStartPositions = JsonConvert.DeserializeObject<List<int>>(annotation.CachedStartPositions),
|
216 |
|
|
TagStartLengths = JsonConvert.DeserializeObject<List<int>>(annotation.CachedLengths),
|
217 |
|
|
TagClosingPositions = JsonConvert.DeserializeObject<List<int>>(annotation.CachedClosingPositions),
|
218 |
ceadebfe
|
Vojtěch Bartička
|
TagClosingLengths = JsonConvert.DeserializeObject<List<int>>(annotation.CachedClosingLengths),
|
219 |
|
|
TagInstanceCSS = JsonConvert.DeserializeObject<List<TagInstanceCSSInfo>>(annotation.CachedCSS)
|
220 |
2c9afc72
|
Vojtěch Bartička
|
};
|
221 |
83d4581e
|
Vojtěch Bartička
|
|
222 |
553486ad
|
Vojtěch Bartička
|
// The annotation has been modified and we need to either add the new tag or remove the tag
|
223 |
|
|
if (annotation.ModifiedType != EModified.NONE)
|
224 |
|
|
{
|
225 |
|
|
if (annotation.ModifiedType == EModified.ADDED)
|
226 |
|
|
{
|
227 |
|
|
var lastModifiedTag = context.AnnotationTags.Where(at => at.Id == annotation.LastModifiedTagId).First();
|
228 |
ceadebfe
|
Vojtěch Bartička
|
var result = htmlService.PartialPreprocessHTMLAddTag(docToRender, documentContent.Content, lastModifiedTag, tags, cachedInfoToReturn);
|
229 |
2c9afc72
|
Vojtěch Bartička
|
docToRender = result.Item1;
|
230 |
|
|
cachedInfoToReturn = result.Item2;
|
231 |
553486ad
|
Vojtěch Bartička
|
}
|
232 |
|
|
else if (annotation.ModifiedType == EModified.REMOVED)
|
233 |
|
|
{
|
234 |
ceadebfe
|
Vojtěch Bartička
|
var result = htmlService.PartialPreprocessHTMLRemoveTag(docToRender, documentContent.Content, new AnnotationTag() { Id = annotation.LastModifiedTagId.Value }, tags, cachedInfoToReturn);
|
235 |
2c9afc72
|
Vojtěch Bartička
|
docToRender = result.Item1;
|
236 |
|
|
cachedInfoToReturn = result.Item2;
|
237 |
553486ad
|
Vojtěch Bartička
|
}
|
238 |
|
|
|
239 |
|
|
annotation.ModifiedType = EModified.NONE;
|
240 |
2c9afc72
|
Vojtěch Bartička
|
annotation.CachedStartPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartPositions);
|
241 |
|
|
annotation.CachedLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagStartLengths);
|
242 |
|
|
annotation.CachedClosingPositions = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingPositions);
|
243 |
|
|
annotation.CachedClosingLengths = JsonConvert.SerializeObject(cachedInfoToReturn.TagClosingLengths);
|
244 |
553486ad
|
Vojtěch Bartička
|
annotation.CachedDocumentHTML = docToRender;
|
245 |
2c9afc72
|
Vojtěch Bartička
|
annotation.CachedCSS = JsonConvert.SerializeObject(cachedInfoToReturn.TagInstanceCSS);
|
246 |
553486ad
|
Vojtěch Bartička
|
context.SaveChanges();
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
0a9f9349
|
Vojtěch Bartička
|
|
250 |
|
|
// We probably cannot use AutoMapper since we are dealing with too many different entities
|
251 |
|
|
AnnotationInfo annotationInfo = new()
|
252 |
|
|
{
|
253 |
|
|
SourceDocumentContent = documentContent.Content,
|
254 |
|
|
DocumentToRender = docToRender,
|
255 |
2c9afc72
|
Vojtěch Bartička
|
TagStartPositions = cachedInfoToReturn.TagStartPositions.ToArray(),
|
256 |
|
|
TagLengths = cachedInfoToReturn.TagStartLengths.ToArray(),
|
257 |
0a9f9349
|
Vojtěch Bartička
|
Note = annotation.Note,
|
258 |
|
|
State = annotation.State,
|
259 |
|
|
Type = IsHtml(documentContent.Content) ? EDocumentType.HTML : EDocumentType.TEXT,
|
260 |
26671569
|
Vojtěch Bartička
|
TagInstances = tagInstanceInfos,
|
261 |
2c9afc72
|
Vojtěch Bartička
|
CSSInfo = cachedInfoToReturn.TagInstanceCSS
|
262 |
0a9f9349
|
Vojtěch Bartička
|
};
|
263 |
|
|
|
264 |
a6675a6d
|
Vojtěch Bartička
|
return annotationInfo;
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
// TODO temporary
|
268 |
|
|
private bool IsHtml(string text)
|
269 |
|
|
{
|
270 |
0a9f9349
|
Vojtěch Bartička
|
return text.Contains("<html>");
|
271 |
a6675a6d
|
Vojtěch Bartička
|
}
|
272 |
be4deff8
|
Vojtěch Bartička
|
|
273 |
83d4581e
|
Vojtěch Bartička
|
public void AddAnnotationInstance(Guid annotationId, Guid userId, ERole userRole, AnnotationInstanceAddRequest request, bool isFinal)
|
274 |
be4deff8
|
Vojtěch Bartička
|
{
|
275 |
83d4581e
|
Vojtěch Bartička
|
Annotation annotation = new();
|
276 |
|
|
if (!isFinal)
|
277 |
|
|
{
|
278 |
|
|
annotation = context.Annotations
|
279 |
8c9ce202
|
Vojtěch Bartička
|
.Where(a => !(a is FinalAnnotation))
|
280 |
83d4581e
|
Vojtěch Bartička
|
.Where(a => a.Id == annotationId)
|
281 |
|
|
.Include(a => a.User)
|
282 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
283 |
|
|
.First();
|
284 |
|
|
}
|
285 |
|
|
else
|
286 |
|
|
{
|
287 |
|
|
annotation = context.FinalAnnotations
|
288 |
|
|
.Where(a => a.Id == annotationId)
|
289 |
|
|
.Include(a => a.User)
|
290 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
291 |
|
|
.First();
|
292 |
|
|
}
|
293 |
be4deff8
|
Vojtěch Bartička
|
|
294 |
|
|
if (userRole < ERole.ADMINISTRATOR)
|
295 |
|
|
{
|
296 |
|
|
if (annotation.User.Id != userId)
|
297 |
|
|
{
|
298 |
|
|
throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
|
299 |
|
|
}
|
300 |
|
|
}
|
301 |
|
|
|
302 |
83d4581e
|
Vojtěch Bartička
|
if (annotation.State == EState.NEW)
|
303 |
82dede50
|
Vojtěch Bartička
|
{
|
304 |
83d4581e
|
Vojtěch Bartička
|
annotation.State = EState.IN_PROGRESS;
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
AnnotationTagGeneric annotationTag = new();
|
308 |
|
|
if (isFinal)
|
309 |
|
|
{
|
310 |
|
|
annotationTag = new FinalAnnotationTag()
|
311 |
|
|
{
|
312 |
|
|
Id = Guid.NewGuid(),
|
313 |
|
|
Annotation = annotation as FinalAnnotation,
|
314 |
|
|
Instance = request.InstanceId == null ? Guid.NewGuid() : request.InstanceId.Value,
|
315 |
|
|
Length = request.Length,
|
316 |
|
|
Position = request.Position,
|
317 |
|
|
SelectedText = request.SelectedText,
|
318 |
|
|
Note = ""
|
319 |
|
|
};
|
320 |
|
|
}
|
321 |
|
|
else
|
322 |
|
|
{
|
323 |
|
|
annotationTag = new AnnotationTag()
|
324 |
|
|
{
|
325 |
|
|
Id = Guid.NewGuid(),
|
326 |
|
|
Annotation = annotation as Annotation,
|
327 |
|
|
Instance = request.InstanceId == null ? Guid.NewGuid() : request.InstanceId.Value,
|
328 |
|
|
Length = request.Length,
|
329 |
|
|
Position = request.Position,
|
330 |
|
|
SelectedText = request.SelectedText,
|
331 |
|
|
Note = ""
|
332 |
|
|
};
|
333 |
|
|
}
|
334 |
be4deff8
|
Vojtěch Bartička
|
|
335 |
|
|
if (request.Type == ETagType.TAG)
|
336 |
|
|
{
|
337 |
|
|
annotationTag.Tag = context.Tags.Where(t => t.Id == request.Id).Single();
|
338 |
|
|
annotationTag.SubTag = null;
|
339 |
15c88dc1
|
Vojtěch Bartička
|
|
340 |
|
|
if (annotationTag.Tag.SentimentEnabled)
|
341 |
|
|
{
|
342 |
|
|
annotationTag.Sentiment = ETagSentiment.NEUTRAL;
|
343 |
|
|
}
|
344 |
2f3821f0
|
Vojtěch Bartička
|
|
345 |
58363b44
|
Vojtěch Bartička
|
// If for the same annotation exists a tag with same position and length and of the same type, ignore
|
346 |
2f3821f0
|
Vojtěch Bartička
|
if (context.AnnotationTags.Any(at =>
|
347 |
58363b44
|
Vojtěch Bartička
|
at.Position == annotationTag.Position &&
|
348 |
|
|
at.Length == annotationTag.Length &&
|
349 |
|
|
at.Annotation == annotation &&
|
350 |
|
|
at.Tag == annotationTag.Tag))
|
351 |
|
|
{
|
352 |
|
|
throw new InvalidOperationException("Duplicate tag");
|
353 |
|
|
}
|
354 |
|
|
|
355 |
be4deff8
|
Vojtěch Bartička
|
}
|
356 |
|
|
else if (request.Type == ETagType.SUBTAG)
|
357 |
|
|
{
|
358 |
|
|
var subTag = context.SubTags.Where(st => st.Id == request.Id).Include(st => st.Tag).Single();
|
359 |
|
|
annotationTag.SubTag = subTag;
|
360 |
|
|
annotationTag.Tag = subTag.Tag;
|
361 |
15c88dc1
|
Vojtěch Bartička
|
|
362 |
|
|
if (annotationTag.SubTag.SentimentEnabled)
|
363 |
|
|
{
|
364 |
|
|
annotationTag.Sentiment = ETagSentiment.NEUTRAL;
|
365 |
|
|
}
|
366 |
2f3821f0
|
Vojtěch Bartička
|
|
367 |
|
|
if (context.AnnotationTags.Any(at =>
|
368 |
58363b44
|
Vojtěch Bartička
|
at.Position == annotationTag.Position &&
|
369 |
|
|
at.Length == annotationTag.Length &&
|
370 |
|
|
at.Annotation == annotation &&
|
371 |
|
|
at.Tag == annotationTag.Tag &&
|
372 |
|
|
at.SubTag == annotationTag.SubTag))
|
373 |
|
|
{
|
374 |
|
|
throw new InvalidOperationException("Duplicate tag");
|
375 |
|
|
}
|
376 |
be4deff8
|
Vojtěch Bartička
|
}
|
377 |
|
|
else
|
378 |
|
|
{
|
379 |
|
|
throw new ArgumentException($"Unknown tag type {request.Type}");
|
380 |
|
|
}
|
381 |
|
|
|
382 |
ceadebfe
|
Vojtěch Bartička
|
if (annotation.State == EState.NEW)
|
383 |
|
|
{
|
384 |
|
|
annotation.State = EState.IN_PROGRESS;
|
385 |
|
|
}
|
386 |
|
|
|
387 |
553486ad
|
Vojtěch Bartička
|
annotation.LastModifiedTagId = annotationTag.Id;
|
388 |
|
|
annotation.ModifiedType = EModified.ADDED;
|
389 |
|
|
|
390 |
83d4581e
|
Vojtěch Bartička
|
if (isFinal)
|
391 |
|
|
{
|
392 |
|
|
context.FinalAnnotationTags.Add(annotationTag as FinalAnnotationTag);
|
393 |
|
|
}
|
394 |
|
|
else
|
395 |
|
|
{
|
396 |
|
|
context.AnnotationTags.Add(annotationTag as AnnotationTag);
|
397 |
|
|
}
|
398 |
|
|
|
399 |
be4deff8
|
Vojtěch Bartička
|
context.SaveChanges();
|
400 |
|
|
}
|
401 |
0f8d6304
|
Vojtěch Bartička
|
|
402 |
83d4581e
|
Vojtěch Bartička
|
public void DeleteAnnotationInstance(Guid annotationId, Guid tagInstanceId, Guid loggedUserId, ERole userRole, bool isFinal)
|
403 |
0f8d6304
|
Vojtěch Bartička
|
{
|
404 |
|
|
Annotation annotation = null;
|
405 |
|
|
try
|
406 |
|
|
{
|
407 |
83d4581e
|
Vojtěch Bartička
|
if (!isFinal)
|
408 |
|
|
{
|
409 |
|
|
annotation = context.Annotations
|
410 |
8c9ce202
|
Vojtěch Bartička
|
.Where(a => !(a is FinalAnnotation))
|
411 |
83d4581e
|
Vojtěch Bartička
|
.Where(a => a.Id == annotationId)
|
412 |
|
|
.Include(a => a.User)
|
413 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
414 |
|
|
.First();
|
415 |
|
|
}
|
416 |
|
|
else
|
417 |
|
|
{
|
418 |
|
|
annotation = context.FinalAnnotations
|
419 |
|
|
.Where(a => a.Id == annotationId)
|
420 |
|
|
.Include(a => a.User)
|
421 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
422 |
|
|
.First();
|
423 |
|
|
}
|
424 |
|
|
|
425 |
0f8d6304
|
Vojtěch Bartička
|
|
426 |
|
|
}
|
427 |
|
|
catch (Exception ex)
|
428 |
|
|
{
|
429 |
|
|
throw new InvalidOperationException("Could not find annotation");
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
|
433 |
|
|
if (userRole < ERole.ADMINISTRATOR)
|
434 |
|
|
{
|
435 |
|
|
if (annotation.User.Id != loggedUserId)
|
436 |
|
|
{
|
437 |
|
|
throw new UnauthorizedAccessException($"User {loggedUserId} does not have assigned annotation {annotationId}");
|
438 |
|
|
}
|
439 |
|
|
}
|
440 |
|
|
|
441 |
83d4581e
|
Vojtěch Bartička
|
AnnotationTagGeneric annotationTag = new();
|
442 |
|
|
|
443 |
|
|
if (isFinal)
|
444 |
0f8d6304
|
Vojtěch Bartička
|
{
|
445 |
83d4581e
|
Vojtěch Bartička
|
if (!context.FinalAnnotationTags.Any(at => at.Id == tagInstanceId))
|
446 |
|
|
{
|
447 |
|
|
throw new InvalidOperationException("Could not find tag instance");
|
448 |
|
|
}
|
449 |
|
|
annotationTag = context.FinalAnnotationTags.First(at => at.Id == tagInstanceId);
|
450 |
0f8d6304
|
Vojtěch Bartička
|
|
451 |
83d4581e
|
Vojtěch Bartička
|
}
|
452 |
|
|
else
|
453 |
|
|
{
|
454 |
|
|
if (!context.AnnotationTags.Any(at => at.Id == tagInstanceId))
|
455 |
|
|
{
|
456 |
|
|
throw new InvalidOperationException("Could not find tag instance");
|
457 |
|
|
}
|
458 |
|
|
annotationTag = context.AnnotationTags.First(at => at.Id == tagInstanceId);
|
459 |
|
|
}
|
460 |
553486ad
|
Vojtěch Bartička
|
|
461 |
|
|
annotation.LastModifiedTagId = annotationTag.Id;
|
462 |
|
|
annotation.ModifiedType = EModified.REMOVED;
|
463 |
|
|
|
464 |
83d4581e
|
Vojtěch Bartička
|
if (isFinal)
|
465 |
|
|
{
|
466 |
|
|
context.FinalAnnotationTags.Remove(annotationTag as FinalAnnotationTag);
|
467 |
|
|
}
|
468 |
|
|
else
|
469 |
|
|
{
|
470 |
|
|
context.AnnotationTags.Remove(annotationTag as AnnotationTag);
|
471 |
|
|
}
|
472 |
0a9f9349
|
Vojtěch Bartička
|
|
473 |
0f8d6304
|
Vojtěch Bartička
|
context.SaveChanges();
|
474 |
|
|
}
|
475 |
15c88dc1
|
Vojtěch Bartička
|
|
476 |
83d4581e
|
Vojtěch Bartička
|
public void SetTagInstanceSentiment(Guid annotationId, Guid instanceId, Guid userId, ERole userRole, ETagSentiment sentiment, bool isFinal)
|
477 |
15c88dc1
|
Vojtěch Bartička
|
{
|
478 |
|
|
Annotation annotation = null;
|
479 |
|
|
try
|
480 |
|
|
{
|
481 |
83d4581e
|
Vojtěch Bartička
|
if (!isFinal)
|
482 |
|
|
{
|
483 |
|
|
annotation = context.Annotations
|
484 |
8c9ce202
|
Vojtěch Bartička
|
.Where(a => !(a is FinalAnnotation))
|
485 |
83d4581e
|
Vojtěch Bartička
|
.Where(a => a.Id == annotationId)
|
486 |
|
|
.Include(a => a.User)
|
487 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
488 |
|
|
.First();
|
489 |
|
|
}
|
490 |
|
|
else
|
491 |
|
|
{
|
492 |
|
|
annotation = context.FinalAnnotations
|
493 |
|
|
.Where(a => a.Id == annotationId)
|
494 |
|
|
.Include(a => a.User)
|
495 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
496 |
|
|
.First();
|
497 |
|
|
}
|
498 |
15c88dc1
|
Vojtěch Bartička
|
}
|
499 |
|
|
catch (Exception ex)
|
500 |
|
|
{
|
501 |
|
|
throw new InvalidOperationException("Could not find annotation");
|
502 |
|
|
}
|
503 |
|
|
|
504 |
|
|
|
505 |
|
|
if (userRole < ERole.ADMINISTRATOR)
|
506 |
|
|
{
|
507 |
|
|
if (annotation.User.Id != userId)
|
508 |
|
|
{
|
509 |
|
|
throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
|
510 |
|
|
}
|
511 |
|
|
}
|
512 |
|
|
|
513 |
|
|
|
514 |
83d4581e
|
Vojtěch Bartička
|
IEnumerable<AnnotationTagGeneric> tagInstances = null;
|
515 |
|
|
|
516 |
|
|
if (isFinal)
|
517 |
|
|
{
|
518 |
|
|
tagInstances = context.FinalAnnotationTags.Where(at => at.Instance == instanceId).ToList();
|
519 |
|
|
}
|
520 |
|
|
else
|
521 |
|
|
{
|
522 |
|
|
tagInstances = context.AnnotationTags.Where(at => at.Instance == instanceId).ToList();
|
523 |
|
|
}
|
524 |
|
|
|
525 |
15c88dc1
|
Vojtěch Bartička
|
if (tagInstances.Count() == 0)
|
526 |
|
|
{
|
527 |
|
|
throw new InvalidOperationException("No such instance found");
|
528 |
|
|
}
|
529 |
|
|
|
530 |
|
|
foreach (var tagInstance in tagInstances)
|
531 |
|
|
{
|
532 |
|
|
tagInstance.Sentiment = sentiment;
|
533 |
|
|
}
|
534 |
|
|
|
535 |
|
|
context.SaveChanges();
|
536 |
|
|
}
|
537 |
0ea30313
|
Vojtěch Bartička
|
|
538 |
83d4581e
|
Vojtěch Bartička
|
public void MarkAnnotationAsDone(Guid annotationId, Guid userId, ERole userRole, bool done, bool isFinal)
|
539 |
0ea30313
|
Vojtěch Bartička
|
{
|
540 |
83d4581e
|
Vojtěch Bartička
|
|
541 |
0ea30313
|
Vojtěch Bartička
|
Annotation annotation = null;
|
542 |
|
|
try
|
543 |
|
|
{
|
544 |
83d4581e
|
Vojtěch Bartička
|
if (!isFinal)
|
545 |
|
|
{
|
546 |
|
|
annotation = context.Annotations
|
547 |
8c9ce202
|
Vojtěch Bartička
|
.Where(a => !(a is FinalAnnotation))
|
548 |
0ea30313
|
Vojtěch Bartička
|
.Where(a => a.Id == annotationId)
|
549 |
|
|
.Include(a => a.User)
|
550 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
551 |
|
|
.First();
|
552 |
83d4581e
|
Vojtěch Bartička
|
}
|
553 |
|
|
else
|
554 |
|
|
{
|
555 |
|
|
annotation = context.FinalAnnotations
|
556 |
|
|
.Where(a => a.Id == annotationId)
|
557 |
|
|
.Include(a => a.User)
|
558 |
|
|
.Include(a => a.Document).ThenInclude(d => d.Content)
|
559 |
|
|
.First();
|
560 |
|
|
}
|
561 |
0ea30313
|
Vojtěch Bartička
|
}
|
562 |
|
|
catch (Exception ex)
|
563 |
|
|
{
|
564 |
|
|
throw new InvalidOperationException("Could not find annotation");
|
565 |
|
|
}
|
566 |
|
|
|
567 |
|
|
if (userRole < ERole.ADMINISTRATOR)
|
568 |
|
|
{
|
569 |
|
|
if (annotation.User.Id != userId)
|
570 |
|
|
{
|
571 |
|
|
throw new UnauthorizedAccessException($"User {userId} does not have assigned annotation {annotationId}");
|
572 |
|
|
}
|
573 |
|
|
}
|
574 |
|
|
|
575 |
|
|
annotation.State = done ? EState.DONE : EState.IN_PROGRESS;
|
576 |
|
|
context.SaveChanges();
|
577 |
|
|
}
|
578 |
440997e0
|
Vojtěch Bartička
|
|
579 |
|
|
public Guid CreateFinalAnnotation(Guid documentId, Guid userId)
|
580 |
|
|
{
|
581 |
|
|
var document = context.Documents.Single(d => d.Id == documentId);
|
582 |
|
|
var user = context.Users.Single(u => u.Id == userId);
|
583 |
|
|
|
584 |
|
|
// Remove existing
|
585 |
|
|
if (context.FinalAnnotations.Any(fa => fa.Document == document))
|
586 |
|
|
{
|
587 |
|
|
var finalAnnotationOld = context.FinalAnnotations.Single(fa => fa.Document == document);
|
588 |
|
|
context.FinalAnnotations.Remove(finalAnnotationOld);
|
589 |
|
|
}
|
590 |
|
|
|
591 |
|
|
var annotations = context.Annotations
|
592 |
8c9ce202
|
Vojtěch Bartička
|
.Where(a => !(a is FinalAnnotation))
|
593 |
440997e0
|
Vojtěch Bartička
|
.Include(a => a.Document)
|
594 |
|
|
.Where(a => a.Document == document && a.State == EState.DONE)
|
595 |
|
|
.ToList();
|
596 |
|
|
|
597 |
|
|
var finalAnnotation = new FinalAnnotation()
|
598 |
|
|
{
|
599 |
|
|
Id = Guid.NewGuid(),
|
600 |
|
|
DateAssigned = DateTime.Now,
|
601 |
|
|
DateLastChanged = DateTime.Now,
|
602 |
|
|
Document = document,
|
603 |
|
|
User = user,
|
604 |
|
|
UserAssigned = user,
|
605 |
|
|
Annotations = annotations,
|
606 |
|
|
State = EState.NEW
|
607 |
|
|
};
|
608 |
|
|
|
609 |
|
|
List<AnnotationTag> annotationTagsAll = new();
|
610 |
|
|
foreach (var annotation in annotations)
|
611 |
|
|
{
|
612 |
|
|
annotationTagsAll.AddRange(context.AnnotationTags
|
613 |
|
|
.Where(at => at.Annotation == annotation)
|
614 |
|
|
.Include(at => at.Tag)
|
615 |
|
|
.Include(at => at.SubTag)
|
616 |
|
|
.Include(at => at.Annotation)
|
617 |
|
|
.ThenInclude(a => a.User)
|
618 |
|
|
.ToList());
|
619 |
|
|
}
|
620 |
|
|
annotationTagsAll = annotationTagsAll.OrderBy(at => at.Position).ToList();
|
621 |
|
|
|
622 |
|
|
Dictionary<Guid, List<AnnotationTag>> occurenceDict = new();
|
623 |
|
|
foreach (var annotationTag in annotationTagsAll)
|
624 |
|
|
{
|
625 |
|
|
if (occurenceDict.ContainsKey(annotationTag.Instance))
|
626 |
|
|
{
|
627 |
|
|
occurenceDict[annotationTag.Instance].Add(annotationTag);
|
628 |
|
|
}
|
629 |
|
|
else
|
630 |
|
|
{
|
631 |
|
|
occurenceDict[annotationTag.Instance] = new();
|
632 |
|
|
occurenceDict[annotationTag.Instance].Add(annotationTag);
|
633 |
|
|
}
|
634 |
|
|
}
|
635 |
|
|
|
636 |
|
|
List<List<AnnotationTag>> occurenceLists = new();
|
637 |
|
|
foreach (var key in occurenceDict.Keys)
|
638 |
|
|
{
|
639 |
|
|
occurenceLists.Add(occurenceDict[key]);
|
640 |
|
|
}
|
641 |
|
|
|
642 |
|
|
List<List<AnnotationTag>> annotationTagsProcessed = new();
|
643 |
|
|
List<FinalAnnotationTag> finalAnnotationTags = new();
|
644 |
|
|
for (int i = 0; i < occurenceLists.Count; i++)
|
645 |
|
|
{
|
646 |
|
|
var occurrenceList1 = occurenceLists[i];
|
647 |
|
|
List<List<AnnotationTag>> sameLists = new();
|
648 |
|
|
|
649 |
|
|
for (int j = 0; j < occurenceLists.Count; j++)
|
650 |
|
|
{
|
651 |
|
|
var occurrenceList2 = occurenceLists[j];
|
652 |
|
|
|
653 |
|
|
if (annotationTagsProcessed.Contains(occurrenceList2))
|
654 |
|
|
{
|
655 |
|
|
continue;
|
656 |
|
|
}
|
657 |
|
|
|
658 |
|
|
if (SelectionsAreSame(occurrenceList1, occurrenceList2))
|
659 |
|
|
{
|
660 |
|
|
sameLists.Add(occurrenceList2);
|
661 |
|
|
}
|
662 |
|
|
}
|
663 |
|
|
|
664 |
|
|
// This means that this occurrence ahs already been processed as matching a previous tag
|
665 |
|
|
if (sameLists.Count() == 0)
|
666 |
|
|
{
|
667 |
|
|
continue;
|
668 |
|
|
}
|
669 |
|
|
|
670 |
|
|
List<User> relatedUsers = new();
|
671 |
|
|
foreach (var list in sameLists)
|
672 |
|
|
{
|
673 |
|
|
relatedUsers.Add(list[0].Annotation.User);
|
674 |
|
|
}
|
675 |
|
|
|
676 |
|
|
foreach (var tag in sameLists[0])
|
677 |
|
|
{
|
678 |
|
|
finalAnnotationTags.Add(new()
|
679 |
|
|
{
|
680 |
|
|
Id = Guid.NewGuid(),
|
681 |
|
|
Tag = tag.Tag,
|
682 |
|
|
SubTag = tag.SubTag,
|
683 |
|
|
Annotation = finalAnnotation,
|
684 |
|
|
SelectedText = tag.SelectedText,
|
685 |
|
|
Sentiment = tag.Sentiment,
|
686 |
|
|
Instance = tag.Instance,
|
687 |
|
|
IsFinal = sameLists.Count == annotations.Count,
|
688 |
|
|
Length = tag.Length,
|
689 |
|
|
Position = tag.Position,
|
690 |
|
|
Note = "",
|
691 |
|
|
Users = relatedUsers
|
692 |
|
|
});
|
693 |
|
|
}
|
694 |
|
|
|
695 |
|
|
annotationTagsProcessed.AddRange(sameLists);
|
696 |
|
|
}
|
697 |
|
|
|
698 |
|
|
context.FinalAnnotations.Add(finalAnnotation);
|
699 |
|
|
context.SaveChanges();
|
700 |
|
|
context.FinalAnnotationTags.AddRange(finalAnnotationTags);
|
701 |
|
|
|
702 |
|
|
context.SaveChanges();
|
703 |
|
|
return finalAnnotation.Id;
|
704 |
|
|
}
|
705 |
|
|
|
706 |
|
|
private bool SelectionsAreSame(List<AnnotationTag> list1, List<AnnotationTag> list2)
|
707 |
|
|
{
|
708 |
|
|
if (list1.Count != list2.Count)
|
709 |
|
|
{
|
710 |
|
|
return false;
|
711 |
|
|
}
|
712 |
|
|
|
713 |
|
|
bool sentimentEnabled = list1[0].Tag.SentimentEnabled;
|
714 |
|
|
|
715 |
|
|
if (sentimentEnabled)
|
716 |
|
|
{
|
717 |
|
|
for (int i = 0; i < list1.Count; i++)
|
718 |
|
|
{
|
719 |
|
|
var tag1 = list1[i];
|
720 |
|
|
var tag2 = list2[i];
|
721 |
|
|
if (tag1.Position == tag2.Position &&
|
722 |
|
|
tag1.Length == tag2.Length &&
|
723 |
|
|
tag1.Sentiment == tag2.Sentiment &&
|
724 |
|
|
tag1.Tag == tag2.Tag &&
|
725 |
|
|
tag1.SubTag == tag2.SubTag)
|
726 |
|
|
{
|
727 |
|
|
continue;
|
728 |
|
|
}
|
729 |
|
|
else
|
730 |
|
|
{
|
731 |
|
|
return false;
|
732 |
|
|
}
|
733 |
|
|
}
|
734 |
|
|
}
|
735 |
|
|
else
|
736 |
|
|
{
|
737 |
|
|
for (int i = 0; i < list1.Count; i++)
|
738 |
|
|
{
|
739 |
|
|
var tag1 = list1[i];
|
740 |
|
|
var tag2 = list2[i];
|
741 |
|
|
if (tag1.Position == tag2.Position &&
|
742 |
|
|
tag1.Length == tag2.Length &&
|
743 |
|
|
tag1.Tag == tag2.Tag &&
|
744 |
|
|
tag1.SubTag == tag2.SubTag)
|
745 |
|
|
{
|
746 |
|
|
continue;
|
747 |
|
|
}
|
748 |
|
|
else
|
749 |
|
|
{
|
750 |
|
|
return false;
|
751 |
|
|
}
|
752 |
|
|
}
|
753 |
|
|
}
|
754 |
|
|
|
755 |
|
|
return true;
|
756 |
|
|
}
|
757 |
eff8ec56
|
Vojtěch Bartička
|
}
|
758 |
|
|
}
|