-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWorksheetAppService.cs
More file actions
221 lines (184 loc) · 8.78 KB
/
WorksheetAppService.cs
File metadata and controls
221 lines (184 loc) · 8.78 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
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unity.Flex.Domain.Services;
using Unity.Flex.Domain.Settings;
using Unity.Flex.Domain.Utils;
using Unity.Flex.Domain.Worksheets;
using Unity.Flex.Reporting.FieldGenerators;
using Unity.Modules.Shared.Features;
using Volo.Abp;
using Volo.Abp.Features;
namespace Unity.Flex.Worksheets
{
[Authorize]
public partial class WorksheetAppService(IWorksheetRepository worksheetRepository,
WorksheetsManager worksheetsManager,
IReportingFieldsGeneratorService<Worksheet> reportingFieldsGeneratorService,
IFeatureChecker featureChecker) : FlexAppService, IWorksheetAppService
{
public virtual async Task<WorksheetDto> GetAsync(Guid id)
{
return ObjectMapper.Map<Worksheet, WorksheetDto>(await worksheetRepository.GetAsync(id, true));
}
public virtual async Task<List<WorksheetDto>> GetListByCorrelationAsync(Guid correlationId, string correlationProvider)
{
var worksheets = await worksheetRepository.GetListOrderedAsync(correlationId, correlationProvider, true);
return ObjectMapper.Map<List<Worksheet>, List<WorksheetDto>>(worksheets);
}
public virtual async Task<List<WorksheetDto>> GetListByCorrelationAnchorAsync(Guid correlationId, string correlationProvider, string uiAnchor)
{
var worksheets = await worksheetRepository.GetListByCorrelationAnchorAsync(correlationId, correlationProvider, uiAnchor, true);
return ObjectMapper.Map<List<Worksheet>, List<WorksheetDto>>(worksheets);
}
public virtual async Task<WorksheetDto> CreateAsync(CreateWorksheetDto dto)
{
// move to domain manager class
var worksheetName = dto.Name.SanitizeWorksheetName();
var existingWorksheet = await worksheetRepository.GetByNameAsync(worksheetName, false);
if (existingWorksheet != null)
{
throw new UserFriendlyException("Worksheet names must be unique");
}
var newWorksheet = new Worksheet(Guid.NewGuid(), worksheetName, dto.Title);
foreach (var section in dto.Sections.OrderBy(s => s.Order))
{
newWorksheet.AddSection(new WorksheetSection(Guid.NewGuid(), section.Name));
foreach (var field in section.Fields)
{
newWorksheet
.Sections[^1]
.AddField(new CustomField(Guid.NewGuid(),
field.Key,
newWorksheet.Name,
field.Label,
field.Type,
field.Definition));
}
}
var dbWorksheet = await worksheetRepository.InsertAsync(newWorksheet);
return ObjectMapper.Map<Worksheet, WorksheetDto>(dbWorksheet);
}
public virtual async Task<WorksheetSectionDto> CreateSectionAsync(Guid id, CreateSectionDto dto)
{
var worksheet = await worksheetRepository.GetAsync(id, true);
if (worksheet.Published) { throw new UserFriendlyException("Cannot add sections to a published worksheet"); }
var newWorksheetSection = new WorksheetSection(Guid.NewGuid(), dto.Name);
worksheet.AddSection(newWorksheetSection);
return ObjectMapper.Map<WorksheetSection, WorksheetSectionDto>(newWorksheetSection);
}
public virtual async Task<List<WorksheetDto>> GetListAsync()
{
return ObjectMapper.Map<List<Worksheet>, List<WorksheetDto>>(await worksheetRepository.GetListAsync(true));
}
public virtual async Task<WorksheetDto> EditAsync(Guid id, EditWorksheetDto dto)
{
var worksheet = await worksheetRepository.GetAsync(id);
worksheet.SetTitle(dto.Title);
return ObjectMapper.Map<Worksheet, WorksheetDto>(worksheet);
}
public virtual async Task<WorksheetDto> CloneAsync(Guid id)
{
var worksheet = await worksheetsManager.CloneWorksheetAsync(id);
return ObjectMapper.Map<Worksheet, WorksheetDto>(worksheet);
}
public virtual async Task<bool> PublishAsync(Guid id)
{
var worksheet = await worksheetRepository.GetAsync(id);
_ = worksheet.SetPublished(true);
if (await featureChecker.IsEnabledAsync(FeatureConsts.Reporting))
{
_ = reportingFieldsGeneratorService.GenerateAndSet(worksheet);
}
return await Task.FromResult(true);
}
public virtual async Task DeleteAsync(Guid id)
{
await worksheetRepository.DeleteAsync(id);
}
public virtual async Task ResequenceSectionsAsync(Guid id, uint oldIndex, uint newIndex)
{
if (oldIndex == newIndex) return;
var worksheet = await worksheetRepository.GetAsync(id);
var sections = worksheet.Sections.OrderBy(s => s.Order).ToList();
var movedSection = sections[(int)oldIndex];
movedSection.SetOrder(newIndex + 1);
if (oldIndex < newIndex)
{
foreach (var field in sections[(int)oldIndex..((int)newIndex + 1)].Where(s => s.Id != movedSection.Id))
{
field.SetOrder(movedSection.Order - 1);
}
}
else if (oldIndex > newIndex)
{
foreach (var field in sections[(int)newIndex..(int)oldIndex].Where(s => s.Id != movedSection.Id))
{
field.SetOrder(movedSection.Order + 1);
}
}
}
public async Task<bool> ExistsAsync(Guid worksheetId)
{
return await worksheetRepository.FindAsync(worksheetId, false) != null;
}
public async Task<ExportWorksheetDto> ExportWorksheet(Guid worksheetId)
{
var worksheet = await worksheetRepository.GetAsync(worksheetId, true);
var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new WorksheetContractResolver()
};
var json = JsonConvert.SerializeObject(worksheet, settings);
var byteArray = System.Text.Encoding.UTF8.GetBytes(json);
return new ExportWorksheetDto { Content = byteArray, ContentType = "application/json", Name = "worksheet_" + worksheet.Title + "_" + worksheet.Name + ".json" };
}
public async Task ImportWorksheetAsync(WorksheetImportDto worksheetImportDto)
{
if (worksheetImportDto.Content == null || worksheetImportDto.Content.Length == 0)
{
throw new UserFriendlyException("No file content provided.");
}
var json = worksheetImportDto.Content;
var worksheet = JsonConvert.DeserializeObject<Worksheet>(json, new JsonSerializerSettings
{
ContractResolver = new PrivateSetterContractResolver(),
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore
}) ?? throw new UserFriendlyException("Invalid JSON content.");
string? name;
var worksheets = await worksheetRepository.GetByNameStartsWithAsync(SheetParserFunctions.RemoveTrailingNumbers(worksheet.Name));
uint maxVersion = 0;
uint newVersion = 0;
if (worksheets.Count > 0)
{
maxVersion = worksheets.Max(s => s.Version);
newVersion = maxVersion + 1;
}
else
{
newVersion = worksheet.Version;
}
name = worksheet.Name.Replace($"-v{worksheet.Version}", $"-v{newVersion}");
var newWorksheet = new Worksheet(Guid.NewGuid(), name, worksheet.Title);
newWorksheet.SetVersion(newVersion);
newWorksheet.SetPublished(false);
foreach (var section in worksheet.Sections)
{
var clonedSection = new WorksheetSection(Guid.NewGuid(), section.Name);
clonedSection.SetOrder(section.Order);
foreach (var field in section.Fields.OrderBy(s => s.Order))
{
var clonedField = new CustomField(Guid.NewGuid(), field.Key, newWorksheet.Name, field.Label, field.Type, field.Definition);
clonedSection.CloneField(clonedField);
}
newWorksheet.CloneSection(clonedSection);
}
await worksheetRepository.InsertAsync(newWorksheet);
}
}
}