@page "/TaskProcessor/{EmployeeIdOptional:guid}/{TaskId:guid}"
@using Syncfusion.Blazor.Buttons;
@using Syncfusion.Blazor.DocumentEditor;
@using System.Net;
@using System.Text.Json;
@using Aspiration_Main.AspirationModels;
@inject IJSRuntime JSRuntime;
@inject NavigationManager NavManager;
@Title
@Title
@if(isShow)
{
}
@if (showGrid)
{
}
Save my Responses
Mark this activity as complete
Return to Contents Page
@code {
[CascadingParameter(Name = "loggedInUser")]
LoggedInUser? loggedInUser { get; set; }
[Parameter]
public Guid EmployeeIdOptional { get; set; }
[Parameter]
public Guid TaskId { get; set; }
LrActivityTask lrActivityTask = new();
EmpLearningActivity learningActivity = new();
LrActivity lrActivity = new();
SfToast ToastObj;
private List Toast = new List();
private string ToastPosition = "Right";
public string DocumentName { get; set; }
public bool ReadOnly { get; set; }
public bool ShowProperties { get; set; } = true;
public string Title { get; set; } = "";
SfDocumentEditorContainer container;
string sfdtString = "";
public bool isShow { get; set; } = false;
public bool showGrid { get; set; } = false;
AspirationContext _dbContext;
public string customeStyle { get; set; } = "margin-top: 1.5rem !important;height:auto !important;margin-left:278px;width:87%";
public List? LstResource
{
get; set;
}
SfGrid? grdResource;
protected override async Task OnInitializedAsync()
{
_dbContext = DbFactory.CreateDbContext();
lrActivityTask = await _dbContext.LrActivityTasks.FirstOrDefaultAsync(z => z.Id == TaskId);
if (lrActivityTask != null)
{
learningActivity = await _dbContext.EmpLearningActivities.FirstOrDefaultAsync(x => x.ActivityId == lrActivityTask.ActivityId && x.EmployeeId == EmployeeIdOptional);
lrActivity = await _dbContext.LrActivities.FirstOrDefaultAsync(x => x.Id == lrActivityTask.ActivityId);
Title = lrActivity.Name;
}
await CreateSfdtStr();
Toast = new List{
new ToastModel{ Title = "Success!", Content="Response Saved Succesfully.", CssClass="e-toast-success", Icon="e-success toast-icons" },
new ToastModel{ Title = "Error!", Content="There is some problem saving the response. Please try again.", CssClass="e-toast-danger", Icon="e-error toast-icons" },
new ToastModel{ Title = "Success!", Content="Activity marked as completed.", CssClass="e-toast-success", Icon="e-success toast-icons" },
new ToastModel{ Title = "Error!", Content="There is some problem processing. Please try again.", CssClass="e-toast-danger", Icon="e-error toast-icons" },
new ToastModel{ Title = "Error!", Content="There is some problem in generating document . Please try again.", CssClass="e-toast-danger", Icon="e-error toast-icons" },
};
await BindGrid();
StateHasChanged();
}
public async Task CreateSfdtStr()
{
_dbContext = DbFactory.CreateDbContext();
if (TaskId != Guid.Empty && EmployeeIdOptional != Guid.Empty)
{
string fileUrl = "";
EmpLearningActivityTask empActivityTask = await _dbContext.EmpLearningActivityTasks.FirstOrDefaultAsync(x => x.ActivityTaskId == TaskId && x.EmployeeId == EmployeeIdOptional && x.ClientId == loggedInUser.CurrentClientID);
if (empActivityTask != null)
{
EmpLearningActivityTaskDocument empDoc = await _dbContext.EmpLearningActivityTaskDocuments.FirstOrDefaultAsync(x => x.EmpLearningActivityTaskId == empActivityTask.Id);
var filename = empDoc.Id.ToString() + "_" + empDoc.FileName;
string uploaderFilePath = SystemSettings.ClientStorageFolder;
fileUrl = Path.Combine(uploaderFilePath, filename);
}
else
{
LrActivityTaskTemplate taskTempelate = await _dbContext.LrActivityTaskTemplates.FirstOrDefaultAsync(x => x.ActivityTaskId == TaskId);
var filename = taskTempelate.Id.ToString() + "_" + taskTempelate.TemplateFileName; ;
string uploaderFilePath = SystemSettings.ClientStorageFolder;
fileUrl = Path.Combine(uploaderFilePath, filename);
}
WebClient webClient = new WebClient();
byte[] byteArray = webClient.DownloadData(fileUrl);
Stream stream = new MemoryStream(byteArray);
//To observe the memory go down, null out the reference of byteArray variable.
byteArray = null;
WordDocument document = WordDocument.Load(stream, ImportFormatType.Docx);
stream.Dispose();
//To observe the memory go down, null out the reference of stream variable.
stream = null;
sfdtString = JsonSerializer.Serialize(document);
document.Dispose();
//To observe the memory go down, null out the reference of document variable.
document = null;
if (!string.IsNullOrEmpty(sfdtString))
isShow = true;
StateHasChanged();
}
}
private async Task BindGrid()
{
List lrResources = await _dbContext.LrActivityResources.Where(x => x.ActivityId == learningActivity.ActivityId && x.Active == true).ToListAsync();
LstResource = new();
if (lrResources != null && lrResources.Count > 0)
{
foreach (var item in lrResources)
{
LrResource lrResource = await _dbContext.LrResources.FirstOrDefaultAsync(x => x.Id == item.ResourceId);
if (lrResource != null)
{
LrResourceVM vM = new();
string Extension = "";
if (!string.IsNullOrEmpty(lrResource.Source))
{
string filename = lrResource.Source.ToString();
string uploaderFilePath = SystemSettings.ClientStorageFolder;
filename = Path.Combine(uploaderFilePath, filename);
Extension = Path.GetExtension(filename);
}
if (!string.IsNullOrEmpty(Extension))
{
vM.Extension = Extension;
}
else
{
vM.Extension = "";
}
vM.Id = lrResource.Id;
vM.ClientId = lrResource.ClientId;
vM.ResourceTypeId = lrResource.ResourceTypeId;
vM.RecourceCode = lrResource.RecourceCode;
vM.Title = lrResource.Title;
vM.Description = lrResource.Description;
vM.Author = lrResource.Author;
vM.Comments = lrResource.Comments;
vM.IsWebSource = lrResource.IsWebSource;
vM.Source = lrResource.Source;
vM.HyperlinkDescription = lrResource.HyperlinkDescription;
vM.Active = lrResource.Active;
LstResource.Add(vM);
}
}
showGrid = true;
StateHasChanged();
}
}
public async Task OnCreated(object args)
{
await Task.Delay(3000);
try
{
if (!string.IsNullOrEmpty(sfdtString))
{
isShow = true;
StateHasChanged();
SfDocumentEditor editor = container.DocumentEditor;
await editor.OpenAsync(sfdtString);
await editor.FitPageAsync(PageFitType.FitPageWidth);
//To observe the memory go down, null out the reference of sfdtString variable.
sfdtString = null;
StateHasChanged();
}
}
catch (Exception Ex)
{
await this.ToastObj.ShowAsync(Toast[1]);
}
}
public async Task OnSave()
{
SfDocumentEditor editor = container.DocumentEditor;
string base64Data = "";
byte[] data;
Stream stream = new MemoryStream();
bool flag = true;
EmpLearningActivityTaskDocument empDoc = new();
try{
base64Data = await editor.SaveAsBlobAsync(FormatType.Docx);
data = Convert.FromBase64String(base64Data);
//To observe the memory go down, null out the reference of base64Data variable.
base64Data = null;
//Word document file stream
stream = new MemoryStream(data);
//To observe the memory go down, null out the reference of data variable.
data = null;
}
catch(Exception Ex)
{
await this.ToastObj.ShowAsync(Toast[4]);
flag = false;
}
if (flag)
{
if (lrActivityTask != null)
{
EmpLearningActivityTask empLearningActivity = await _dbContext.EmpLearningActivityTasks.FirstOrDefaultAsync(x => x.ActivityTaskId == lrActivityTask.Id && x.EmployeeId == EmployeeIdOptional && x.ClientId == loggedInUser.CurrentClientID);
if (empLearningActivity == null)
{
empLearningActivity = new();
empLearningActivity.ClientId = loggedInUser.CurrentClientID;
empLearningActivity.Id = Guid.NewGuid();
empLearningActivity.ActivityTaskId = TaskId;
empLearningActivity.ActivityTaskStatus = 0;
empLearningActivity.ActivityId = lrActivityTask.ActivityId;
empLearningActivity.Active = true;
empLearningActivity.DateStarted = DateOnly.FromDateTime(DateTime.Today);
empLearningActivity.EmployeeId = EmployeeIdOptional;
try
{
await _dbContext.EmpLearningActivityTasks.AddAsync(empLearningActivity);
await _dbContext.SaveChangesAsync();
}
catch (Exception Ex)
{
empLearningActivity.Id = Guid.Empty;
await this.ToastObj.ShowAsync(Toast[1]);
}
if (empLearningActivity.Id != Guid.Empty)
{
empDoc = new();
empDoc.Active = true;
empDoc.UploadDate = DateTime.Today;
empDoc.Id = Guid.NewGuid();
empDoc.EmpLearningActivityTaskId = empLearningActivity.Id;
empDoc.FileName = lrActivityTask.ElementName;
try
{
await _dbContext.EmpLearningActivityTaskDocuments.AddAsync(empDoc);
await _dbContext.SaveChangesAsync();
}
catch (Exception Ex)
{
empDoc.Id = Guid.Empty;
await this.ToastObj.ShowAsync(Toast[1]);
}
}
}
else
{
empDoc = await _dbContext.EmpLearningActivityTaskDocuments.FirstOrDefaultAsync(x => x.EmpLearningActivityTaskId == empLearningActivity.Id);
if (empDoc == null)
{
empDoc.Active = true;
empDoc.UploadDate = DateTime.Today;
empDoc.Id = Guid.NewGuid();
empDoc.EmpLearningActivityTaskId = empLearningActivity.Id;
empDoc.FileName = lrActivityTask.ElementName;
try
{
await _dbContext.EmpLearningActivityTaskDocuments.AddAsync(empDoc);
await _dbContext.SaveChangesAsync();
}
catch (Exception Ex)
{
empDoc.Id = Guid.Empty;
await this.ToastObj.ShowAsync(Toast[1]);
}
}
}
}
if (empDoc.Id != Guid.Empty)
{
string filename = empDoc.Id.ToString() + "_" + empDoc.FileName;
string uploaderFilePath = SystemSettings.ClientStorageFolder;
uploaderFilePath = Path.Combine(uploaderFilePath, filename);
using (var fileStream = new FileStream(uploaderFilePath, FileMode.Create, FileAccess.Write))
{
//await editor.SaveAsync(fileStream);
//Saving the new file in root path of application
stream.CopyTo(fileStream);
fileStream.Close();
}
stream.Close();
stream = null;
await this.ToastObj.ShowAsync(Toast[0]);
await CreateSfdtStr();
StateHasChanged();
}
}
}
public async Task uploadFile(Guid Value, string fileName)
{
var filename = Value.ToString() + "_" + fileName;
string uploaderFilePath = SystemSettings.ClientStorageFolder;
uploaderFilePath = Path.Combine(uploaderFilePath, filename);
SfDocumentEditor editor = container.DocumentEditor;
string base64Data = await editor.SaveAsBlobAsync(FormatType.Docx);
byte[] data = Convert.FromBase64String(base64Data);
//To observe the memory go down, null out the reference of base64Data variable.
base64Data = null;
//Word document file stream
Stream stream = new MemoryStream(data);
//To observe the memory go down, null out the reference of data variable.
data = null;
using (var fileStream = new FileStream(uploaderFilePath, FileMode.Create, FileAccess.Write))
{
//await editor.SaveAsync(fileStream);
//Saving the new file in root path of application
stream.CopyTo(fileStream);
fileStream.Close();
}
editor.Dispose();
stream.Close();
stream = null;
//To observe the memory go down, null out the reference of stream variable.
}
public class LrResourceVM : LrResource
{
public string Extension { get; set; } = "";
}
//Downloading evidence
public async Task DownloadFileNow(Guid EvidenceId, string fileName, string Res = "")
{
var filename = "";
if (string.IsNullOrEmpty(Res))
{
filename = EvidenceId.ToString() + "_" + fileName;
}
else
{
filename = EvidenceId.ToString() + Res;
}
string uploaderFilePath = SystemSettings.ClientStorageFolder;
filename = Path.Combine(uploaderFilePath, filename);
var base64String = await GetFileBytes(filename);
string Extension = Path.GetExtension(filename).Replace(".", "");
switch (Extension)
{
case "pdf":
await JSRuntime.InvokeVoidAsync("downloadFile", "application/pdf", base64String, fileName);
break;
case "doc":
await JSRuntime.InvokeVoidAsync("downloadFile", "application/msword", base64String, fileName);
break;
case "docx":
await JSRuntime.InvokeVoidAsync("downloadFile", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", base64String, fileName);
break;
case "xls":
await JSRuntime.InvokeVoidAsync("downloadFile", "application/vnd.ms-excel", base64String, fileName);
break;
case "xlsx":
await JSRuntime.InvokeVoidAsync("downloadFile", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", base64String, fileName);
break;
case "ppt":
await JSRuntime.InvokeVoidAsync("downloadFile", "application/vnd.ms-powerpoint", base64String, fileName);
break;
case "pptx":
await JSRuntime.InvokeVoidAsync("downloadFile", "application/vnd.openxmlformats-officedocument.presentationml.presentation", base64String, fileName);
break;
case "jpg":
await JSRuntime.InvokeVoidAsync("downloadFile", "image/jpeg", base64String, fileName);
break;
case "jpeg":
await JSRuntime.InvokeVoidAsync("downloadFile", "image/jpeg", base64String, fileName);
break;
}
//call javascript function to download the file
}
public async Task GetFileBytes(string filepathdata)
{
var filePath = filepathdata;
using (var fileInput = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
MemoryStream memoryStream = new MemoryStream();
await fileInput.CopyToAsync(memoryStream);
var buffer = memoryStream.ToArray();
return Convert.ToBase64String(buffer);
}
}
private async Task OnBack()
{
isShow = false;
NavManager.NavigateTo(@"/completeActivityTask/"+EmployeeIdOptional);
}
public void OnDocumentContentChange()
{
}
private async Task CompleteActivity()
{
_dbContext = DbFactory.CreateDbContext();
if (learningActivity != null)
{
try
{
learningActivity.DateSelfCompletion = DateOnly.FromDateTime(DateTime.Today);
_dbContext.Update(learningActivity);
await _dbContext.SaveChangesAsync();
await this.ToastObj.ShowAsync(Toast[2]);
}
catch(Exception Ex)
{
await this.ToastObj.ShowAsync(Toast[3]);
}
}
}
}