-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApplicationFormVersionAppService.cs
More file actions
311 lines (266 loc) · 14.5 KB
/
ApplicationFormVersionAppService.cs
File metadata and controls
311 lines (266 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Unity.GrantManager.Applications;
using Unity.GrantManager.Forms;
using Unity.GrantManager.Intakes;
using Unity.GrantManager.Integration.Chefs;
using Unity.GrantManager.Reporting.FieldGenerators;
using Unity.Modules.Shared.Features;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Features;
using Volo.Abp.Uow;
using Unity.GrantManager.Intakes.Mapping;
namespace Unity.GrantManager.ApplicationForms
{
public class ApplicationFormVersionAppService :
CrudAppService<
ApplicationFormVersion,
ApplicationFormVersionDto,
Guid,
PagedAndSortedResultRequestDto,
CreateUpdateApplicationFormVersionDto>,
IApplicationFormVersionAppService
{
private readonly IApplicationFormVersionRepository _formVersionRepository;
private readonly IIntakeFormSubmissionMapper _formSubmissionMapper;
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly IFormsApiService _formsApiService;
private readonly IApplicationFormSubmissionRepository _formSubmissionRepository;
private readonly IReportingFieldsGeneratorService _reportingFieldsGeneratorService;
private readonly IFeatureChecker _featureChecker;
public ApplicationFormVersionAppService(
IRepository<ApplicationFormVersion, Guid> repository,
IIntakeFormSubmissionMapper formSubmissionMapper,
IUnitOfWorkManager unitOfWorkManager,
IFormsApiService formsApiService,
IApplicationFormVersionRepository formVersionRepository,
IApplicationFormSubmissionRepository formSubmissionRepository,
IReportingFieldsGeneratorService reportingFieldsGeneratorService,
IFeatureChecker featureChecker)
: base(repository)
{
_formVersionRepository = formVersionRepository;
_formSubmissionMapper = formSubmissionMapper;
_unitOfWorkManager = unitOfWorkManager;
_formsApiService = formsApiService;
_formSubmissionRepository = formSubmissionRepository;
_reportingFieldsGeneratorService = reportingFieldsGeneratorService;
_featureChecker = featureChecker;
}
public override async Task<ApplicationFormVersionDto> CreateAsync(CreateUpdateApplicationFormVersionDto input) =>
await base.CreateAsync(input);
public override async Task<ApplicationFormVersionDto> UpdateAsync(Guid id, CreateUpdateApplicationFormVersionDto input) =>
await base.UpdateAsync(id, input);
public override async Task<ApplicationFormVersionDto> GetAsync(Guid id) =>
await base.GetAsync(id);
public async Task<bool> InitializePublishedFormVersion(dynamic chefsForm, Guid applicationFormId, bool initializePublishedOnly)
{
if (chefsForm == null) return false;
try
{
var versionsToken = GetFormVersionToken(chefsForm);
if (versionsToken == null) return false;
var childTokens = ((IEnumerable<JToken>)versionsToken.Children()).Where(t => t.Type == JTokenType.Object);
foreach (var childToken in childTokens)
{
if (TryParsePublished(childToken, out string? formVersionId, out bool published) &&
formVersionId != null &&
await FormVersionDoesNotExist(formVersionId) &&
(!initializePublishedOnly || published))
{
var applicationFormVersion = await TryInitializeApplicationFormVersionWithToken(childToken, applicationFormId, formVersionId, published);
if (applicationFormVersion != null)
{
await InsertApplicationFormVersion(applicationFormVersion);
return true;
}
}
}
}
catch (Exception ex)
{
Logger.LogError(ex, "Exception: {Exception}", ex);
}
return false;
}
private static JToken? GetFormVersionToken(dynamic chefsForm) =>
chefsForm == null ? null : JObject.Parse(chefsForm.ToString())?["versions"];
private static bool TryParsePublished(JToken token, out string? formVersionId, out bool published)
{
formVersionId = token.Value<string>("id");
return bool.TryParse(token.Value<string>("published"), out published);
}
private async Task<bool> FormVersionDoesNotExist(string formVersionId) =>
await GetApplicationFormVersion(formVersionId) == null;
public async Task<ApplicationFormVersionDto?> TryInitializeApplicationFormVersionWithToken(JToken token, Guid applicationFormId, string formVersionId, bool published)
{
try
{
var formId = token.Value<string>("formId");
var version = token.Value<int>("version");
return await TryInitializeApplicationFormVersion(formId, version, applicationFormId, formVersionId, published);
}
catch (Exception ex)
{
Logger.LogError(ex, "Initialization Exception: {Exception}", ex);
return null;
}
}
public async Task<ApplicationFormVersionDto?> TryInitializeApplicationFormVersion(string? formId, int version, Guid applicationFormId, string formVersionId, bool published)
{
if (formId == null) return null;
try
{
var applicationFormVersion = new ApplicationFormVersion
{
ApplicationFormId = applicationFormId,
ChefsApplicationFormGuid = formId,
Version = version,
Published = published,
ChefsFormVersionGuid = formVersionId
};
var formVersion = await _formsApiService.GetFormDataAsync(formId, formVersionId);
applicationFormVersion.AvailableChefsFields = _formSubmissionMapper.InitializeAvailableFormFields(formVersion);
if (formVersion is JObject formVersionObject)
{
var schema = formVersionObject.SelectToken("schema")?.ToString() ?? string.Empty;
applicationFormVersion.FormSchema = ChefsFormIOReplacement.ReplaceAdvancedFormIoControls(schema);
}
return ObjectMapper.Map<ApplicationFormVersion, ApplicationFormVersionDto>(applicationFormVersion);
}
catch (Exception ex)
{
Logger.LogError(ex, "Initialization Exception: {Exception}", ex);
return null;
}
}
private async Task InsertApplicationFormVersion(ApplicationFormVersionDto applicationFormVersionDto)
{
var applicationFormVersion = ObjectMapper.Map<ApplicationFormVersionDto, ApplicationFormVersion>(applicationFormVersionDto);
await _formVersionRepository.InsertAsync(applicationFormVersion);
}
public async Task<string?> GetFormVersionSubmissionMapping(string chefsFormVersionId)
{
var applicationFormVersion = (await _formVersionRepository.GetQueryableAsync())
.FirstOrDefault(s => s.ChefsFormVersionGuid == chefsFormVersionId);
return applicationFormVersion?.SubmissionHeaderMapping;
}
private async Task<ApplicationFormVersion?> GetApplicationFormVersion(string chefsFormVersionId) =>
(await _formVersionRepository.GetQueryableAsync())
.FirstOrDefault(s => s.ChefsFormVersionGuid == chefsFormVersionId);
public async Task<bool> FormVersionExists(string chefsFormVersionId) =>
await GetApplicationFormVersion(chefsFormVersionId) != null;
private async Task<bool> UnPublishFormVersions(Guid applicationFormId, string chefsFormVersionId)
{
using var uow = _unitOfWorkManager.Begin();
var applicationFormVersion = (await _formVersionRepository.GetQueryableAsync())
.FirstOrDefault(s => s.ChefsFormVersionGuid != chefsFormVersionId && s.ApplicationFormId == applicationFormId);
if (applicationFormVersion != null)
{
applicationFormVersion.Published = false;
await _formVersionRepository.UpdateAsync(applicationFormVersion);
await uow.SaveChangesAsync();
return true;
}
return false;
}
public async Task<ApplicationFormVersionDto> UpdateOrCreateApplicationFormVersion(
string chefsFormId,
string chefsFormVersionId,
Guid applicationFormId,
dynamic chefsFormVersion)
{
var applicationFormVersion = await GetOrCreateApplicationFormVersion(chefsFormId, chefsFormVersionId, applicationFormId);
await UpdateApplicationFormVersionFields(applicationFormVersion, chefsFormVersion, applicationFormId, chefsFormVersionId);
if (await _featureChecker.IsEnabledAsync(FeatureConsts.Reporting) &&
string.IsNullOrEmpty(applicationFormVersion.ReportViewName))
{
await _reportingFieldsGeneratorService.GenerateAndSetAsync(applicationFormVersion);
}
return ObjectMapper.Map<ApplicationFormVersion, ApplicationFormVersionDto>(applicationFormVersion);
}
private async Task<ApplicationFormVersion> GetOrCreateApplicationFormVersion(string chefsFormId, string chefsFormVersionId, Guid applicationFormId)
{
var applicationFormVersion = await GetApplicationFormVersion(chefsFormVersionId) ??
(await _formVersionRepository.GetQueryableAsync())
.FirstOrDefault(s => s.ChefsApplicationFormGuid == chefsFormId && s.ChefsFormVersionGuid == null) ??
new ApplicationFormVersion
{
ApplicationFormId = applicationFormId,
ChefsApplicationFormGuid = chefsFormId
};
applicationFormVersion.ChefsFormVersionGuid = chefsFormVersionId;
return applicationFormVersion;
}
private async Task UpdateApplicationFormVersionFields(ApplicationFormVersion applicationFormVersion, dynamic chefsFormVersion, Guid applicationFormId, string chefsFormVersionId)
{
if (chefsFormVersion == null)
throw new EntityNotFoundException("Application Form Not Registered");
var version = ((JObject)chefsFormVersion).SelectToken("version")?.ToString();
var published = ((JObject)chefsFormVersion).SelectToken("published")?.ToString();
var schema = ((JObject)chefsFormVersion).SelectToken("schema")?.ToString();
applicationFormVersion.AvailableChefsFields = _formSubmissionMapper.InitializeAvailableFormFields(chefsFormVersion);
applicationFormVersion.FormSchema = schema != null ? ChefsFormIOReplacement.ReplaceAdvancedFormIoControls(schema) ?? string.Empty : string.Empty;
if (version != null)
applicationFormVersion.Version = int.Parse(version);
if (published != null && bool.TryParse(published, out var isPublished))
{
if (isPublished)
await UnPublishFormVersions(applicationFormId, chefsFormVersionId);
applicationFormVersion.Published = isPublished;
}
if (applicationFormVersion.Id == Guid.Empty)
await _formVersionRepository.InsertAsync(applicationFormVersion);
else
await _formVersionRepository.UpdateAsync(applicationFormVersion);
}
public async Task<ApplicationFormVersionDto?> GetByChefsFormVersionId(Guid chefsFormVersionId)
{
var applicationFormVersion = await _formVersionRepository.GetByChefsFormVersionAsync(chefsFormVersionId);
return applicationFormVersion == null ? null : ObjectMapper.Map<ApplicationFormVersion, ApplicationFormVersionDto>(applicationFormVersion);
}
public async Task<int> GetFormVersionByApplicationIdAsync(Guid applicationId)
{
var formSubmission = await _formSubmissionRepository.GetByApplicationAsync(applicationId);
if (formSubmission.FormVersionId == null)
{
try
{
var submissionJson = JObject.Parse(formSubmission.Submission);
var tokenFormVersionId = submissionJson?.SelectToken("submission.formVersionId")?.ToString();
if (tokenFormVersionId == null) return 0;
var formVersionId = Guid.Parse(tokenFormVersionId);
formSubmission.FormVersionId = formVersionId;
await _formSubmissionRepository.UpdateAsync(formSubmission);
return await GetVersion(formVersionId);
}
catch
{
return 0;
}
}
return await GetVersion(formSubmission.FormVersionId ?? Guid.Empty);
}
public async Task DeleteWorkSheetMappingByFormName(string formName, Guid formVersionId)
{
var applicationFormVersion = await _formVersionRepository.GetAsync(formVersionId);
if (applicationFormVersion?.SubmissionHeaderMapping == null) return;
var pattern = $"(,\\s*\\\"{formName}.*\\\")|(\\\"{formName}.*\\\",)";
applicationFormVersion.SubmissionHeaderMapping = Regex.Replace(applicationFormVersion.SubmissionHeaderMapping, pattern, "", RegexOptions.None, TimeSpan.FromSeconds(30));
await _formVersionRepository.UpdateAsync(applicationFormVersion);
}
private async Task<int> GetVersion(Guid formVersionId)
{
var formVersion = await _formVersionRepository.GetByChefsFormVersionAsync(formVersionId);
return formVersion?.Version ?? 0;
}
}
}