/
BeginExportContentItemsToCsv.cs
224 lines (198 loc) · 9.11 KB
/
BeginExportContentItemsToCsv.cs
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
using MediatR;
using Raytha.Application.Common.Models;
using CSharpVitamins;
using Raytha.Application.Common.Interfaces;
using FluentValidation;
using Raytha.Application.Common.Exceptions;
using Raytha.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Raytha.Domain.ValueObjects;
using Raytha.Application.Common.Utils;
using Csv;
using Raytha.Domain.Common;
using Raytha.Domain.ValueObjects.FieldValues;
using System.Text.Json;
using Raytha.Application.MediaItems;
namespace Raytha.Application.ContentItems.Commands;
public class BeginExportContentItemsToCsv
{
public record Command : LoggableRequest<CommandResponseDto<ShortGuid>>
{
public ShortGuid ViewId { get; init; }
public bool ExportOnlyColumnsFromView { get; init; } = true;
}
public class Validator : AbstractValidator<Command>
{
public Validator()
{
RuleFor(x => x.ViewId).NotEmpty();
}
}
public class Handler : IRequestHandler<Command, CommandResponseDto<ShortGuid>>
{
private readonly IBackgroundTaskQueue _taskQueue;
private readonly IRaythaDbContext _entityFrameworkDb;
private readonly IContentTypeInRoutePath _contentTypeInRoutePath;
public Handler(
IBackgroundTaskQueue taskQueue,
IRaythaDbContext entityFrameworkDb,
IContentTypeInRoutePath contentTypeInRoutePath
)
{
_taskQueue = taskQueue;
_entityFrameworkDb = entityFrameworkDb;
_contentTypeInRoutePath = contentTypeInRoutePath;
}
public async Task<CommandResponseDto<ShortGuid>> Handle(Command request, CancellationToken cancellationToken)
{
View view = _entityFrameworkDb.Views
.Include(p => p.ContentType)
.ThenInclude(p => p.ContentTypeFields)
.FirstOrDefault(p => p.Id == request.ViewId.Guid);
if (view == null)
throw new NotFoundException("View", request.ViewId);
_contentTypeInRoutePath.ValidateContentTypeInRoutePathMatchesValue(view.ContentType.DeveloperName);
var backgroundJobId = await _taskQueue.EnqueueAsync<BackgroundTask>(request, cancellationToken);
return new CommandResponseDto<ShortGuid>(backgroundJobId);
}
}
public class BackgroundTask : IExecuteBackgroundTask
{
private readonly IRaythaDbJsonQueryEngine _db;
private readonly IRaythaDbContext _entityFrameworkDb;
private readonly IContentTypeInRoutePath _contentTypeInRoutePath;
private readonly ICurrentOrganization _currentOrganization;
private readonly IFileStorageProvider _fileStorageProvider;
private readonly FieldValueConverter _fieldValueConverter;
public BackgroundTask(
ICurrentOrganization currentOrganization,
IRaythaDbJsonQueryEngine db,
IRaythaDbContext entityFrameworkDb,
IFileStorageProvider fileStorageProvider,
FieldValueConverter fieldValueConverter)
{
_db = db;
_entityFrameworkDb = entityFrameworkDb;
_currentOrganization = currentOrganization;
_fileStorageProvider = fileStorageProvider;
_fieldValueConverter = fieldValueConverter;
}
public async Task Execute(Guid jobId, JsonElement args, CancellationToken cancellationToken)
{
IEnumerable<ContentItemDto> items;
Guid viewId = args.GetProperty("ViewId").GetProperty("Guid").GetGuid();
bool exportOnlyColumnsFromView = args.GetProperty("ExportOnlyColumnsFromView").GetBoolean();
View view = _entityFrameworkDb.Views
.Include(p => p.ContentType)
.ThenInclude(p => p.ContentTypeFields)
.FirstOrDefault(p => p.Id == viewId);
if (view == null)
throw new NotFoundException("View", viewId);
string[] filters = GetFiltersForView(view);
string finalOrderBy = GetSortForView(view);
int count = _db.CountContentItems(view.ContentTypeId, new string[0], string.Empty, filters);
var job = _entityFrameworkDb.BackgroundTasks.First(p => p.Id == jobId);
job.TaskStep = 1;
job.StatusInfo = $"Pulling {count} items for {view.ContentType.LabelPlural} - {view.Label}";
job.PercentComplete = 0;
_entityFrameworkDb.BackgroundTasks.Update(job);
await _entityFrameworkDb.SaveChangesAsync(cancellationToken);
var myExport = new CsvExport();
int currentIndex = 0;
foreach (var item in _db.QueryAllContentItemsAsTransaction(view.ContentTypeId,
new string[0],
string.Empty,
filters,
finalOrderBy))
{
var contentItemAsDict = _fieldValueConverter.MapToListItemValues(ContentItemDto.GetProjection(item));
myExport.AddRow();
if (exportOnlyColumnsFromView)
{
foreach (var column in view.Columns)
{
if (contentItemAsDict.ContainsKey(column))
{
myExport[column] = contentItemAsDict[column];
}
else
{
myExport[column] = string.Empty;
}
}
}
else
{
foreach (var column in BuiltInContentTypeField.ReservedContentTypeFields)
{
if (contentItemAsDict.ContainsKey(column))
{
myExport[column] = contentItemAsDict[column];
}
else
{
myExport[column] = string.Empty;
}
}
foreach (var column in view.ContentType.ContentTypeFields.Select(p => p.DeveloperName))
{
if (contentItemAsDict.ContainsKey(column))
{
myExport[column] = contentItemAsDict[column];
}
else
{
myExport[column] = string.Empty;
}
}
}
currentIndex++;
int percentDone = (currentIndex / count) * 100;
if (percentDone % 20 == 0)
{
job.TaskStep = 2;
job.PercentComplete = percentDone - 10;
_entityFrameworkDb.BackgroundTasks.Update(job);
await _entityFrameworkDb.SaveChangesAsync(cancellationToken);
}
}
job.TaskStep = 3;
job.StatusInfo = $"Items pulled. Generating csv...";
_entityFrameworkDb.BackgroundTasks.Update(job);
await _entityFrameworkDb.SaveChangesAsync(cancellationToken);
var csvAsBytes = myExport.ExportToBytes();
string fileName = $"{_currentOrganization.TimeZoneConverter.UtcToTimeZoneAsDateTimeFormat(DateTime.UtcNow)}-{view.DeveloperName}.csv";
var id = Guid.NewGuid();
var objectKey = FileStorageUtility.CreateObjectKeyFromIdAndFileName(id.ToString(), fileName);
var mediaItem = new MediaItem
{
Id = id,
ObjectKey = objectKey,
ContentType = "text/csv",
FileName = fileName,
FileStorageProvider = _fileStorageProvider.GetName(),
Length = csvAsBytes.Length
};
_entityFrameworkDb.MediaItems.Add(mediaItem);
await _fileStorageProvider.SaveAndGetDownloadUrlAsync(csvAsBytes, objectKey, fileName, "text/csv", DateTime.UtcNow.AddYears(999));
job.TaskStep = 4;
job.PercentComplete = 100;
job.StatusInfo = JsonSerializer.Serialize(MediaItemDto.GetProjection(mediaItem));
_entityFrameworkDb.BackgroundTasks.Update(job);
await _entityFrameworkDb.SaveChangesAsync(cancellationToken);
}
protected string GetSortForView(View view)
{
string finalOrderBy = $"{BuiltInContentTypeField.CreationTime.DeveloperName} {SortOrder.DESCENDING}";
var viewOrderBy = view.Sort.Select(p => $"{p.DeveloperName} {p.SortOrder.DeveloperName}").ToList();
finalOrderBy = viewOrderBy.Any() ? string.Join(",", viewOrderBy) : finalOrderBy;
return finalOrderBy;
}
protected string[] GetFiltersForView(View view)
{
var conditionToODataUtility = new FilterConditionToODataUtility(view.ContentType);
var oDataFromFilter = conditionToODataUtility.ToODataFilter(view.Filter);
return new string[] { oDataFromFilter };
}
}
}