411 lines
18 KiB
C#
411 lines
18 KiB
C#
using System;
|
||
using System.Threading.Tasks;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using System.Linq;
|
||
using AyaNova.Util;
|
||
using AyaNova.Api.ControllerHelpers;
|
||
using AyaNova.Models;
|
||
using Newtonsoft.Json.Linq;
|
||
using System.Collections.Generic;
|
||
using Newtonsoft.Json;
|
||
|
||
namespace AyaNova.Biz
|
||
{
|
||
internal class ReminderBiz : BizObject, IJobObject, ISearchAbleObject, IReportAbleObject, IExportAbleObject, IImportAbleObject
|
||
{
|
||
internal ReminderBiz(AyContext dbcontext, long currentUserId, long userTranslationId, AuthorizationRoles UserRoles)
|
||
{
|
||
ct = dbcontext;
|
||
UserId = currentUserId;
|
||
UserTranslationId = userTranslationId;
|
||
CurrentUserRoles = UserRoles;
|
||
BizType = AyaType.Reminder;
|
||
}
|
||
|
||
internal static ReminderBiz GetBiz(AyContext ct, Microsoft.AspNetCore.Http.HttpContext httpContext = null)
|
||
{
|
||
if (httpContext != null)
|
||
return new ReminderBiz(ct, UserIdFromContext.Id(httpContext.Items), UserTranslationIdFromContext.Id(httpContext.Items), UserRolesFromContext.Roles(httpContext.Items));
|
||
else
|
||
return new ReminderBiz(ct, 1, ServerBootConfig.AYANOVA_DEFAULT_TRANSLATION_ID, AuthorizationRoles.BizAdminFull);
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//EXISTS
|
||
internal async Task<bool> ExistsAsync(long id)
|
||
{
|
||
return await ct.Reminder.AnyAsync(z => z.Id == id);
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//CREATE
|
||
//
|
||
internal async Task<Reminder> CreateAsync(Reminder newObject)
|
||
{
|
||
await ValidateAsync(newObject, null);
|
||
if (HasErrors)
|
||
return null;
|
||
else
|
||
{
|
||
newObject.Tags = TagBiz.NormalizeTags(newObject.Tags);
|
||
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||
await ct.Reminder.AddAsync(newObject);
|
||
await ct.SaveChangesAsync();
|
||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||
await SearchIndexAsync(newObject, true);
|
||
await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||
await NotifyEventProcessor.HandlePotentialNotificationEvent(AyaEvent.Created, newObject);
|
||
return newObject;
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//DUPLICATE
|
||
//
|
||
internal async Task<Reminder> DuplicateAsync(long id)
|
||
{
|
||
Reminder dbObject = await GetAsync(id, false);
|
||
if (dbObject == null)
|
||
{
|
||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||
return null;
|
||
}
|
||
Reminder newObject = new Reminder();
|
||
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||
string newUniqueName = string.Empty;
|
||
bool NotUnique = true;
|
||
long l = 1;
|
||
do
|
||
{
|
||
newUniqueName = Util.StringUtil.UniqueNameBuilder(dbObject.Name, l++, 255);
|
||
NotUnique = await ct.Reminder.AnyAsync(m => m.Name == newUniqueName);
|
||
} while (NotUnique);
|
||
newObject.Name = newUniqueName;
|
||
newObject.Id = 0;
|
||
newObject.Concurrency = 0;
|
||
await ct.Reminder.AddAsync(newObject);
|
||
await ct.SaveChangesAsync();
|
||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, newObject.Id, BizType, AyaEvent.Created), ct);
|
||
await SearchIndexAsync(newObject, true);
|
||
await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, newObject.Tags, null);
|
||
await NotifyEventProcessor.HandlePotentialNotificationEvent(AyaEvent.Created, newObject);
|
||
return newObject;
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//GET
|
||
//
|
||
internal async Task<Reminder> GetAsync(long id, bool logTheGetEvent = true)
|
||
{
|
||
var ret = await ct.Reminder.SingleOrDefaultAsync(m => m.Id == id);
|
||
if (logTheGetEvent && ret != null)
|
||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, id, BizType, AyaEvent.Retrieved), ct);
|
||
return ret;
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//UPDATE
|
||
//
|
||
internal async Task<Reminder> PutAsync(Reminder putObject)
|
||
{
|
||
Reminder dbObject = await ct.Reminder.SingleOrDefaultAsync(m => m.Id == putObject.Id);
|
||
if (dbObject == null)
|
||
{
|
||
AddError(ApiErrorCode.NOT_FOUND, "id");
|
||
return null;
|
||
}
|
||
Reminder SnapshotOfOriginalDBObj = new Reminder();
|
||
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||
CopyObject.Copy(putObject, dbObject, "Id");
|
||
dbObject.Tags = TagBiz.NormalizeTags(dbObject.Tags);
|
||
dbObject.CustomFields = JsonUtil.CompactJson(dbObject.CustomFields);
|
||
ct.Entry(dbObject).OriginalValues["Concurrency"] = putObject.Concurrency;
|
||
await ValidateAsync(dbObject, SnapshotOfOriginalDBObj);
|
||
if (HasErrors) return null;
|
||
try
|
||
{
|
||
await ct.SaveChangesAsync();
|
||
}
|
||
catch (DbUpdateConcurrencyException)
|
||
{
|
||
if (!await ExistsAsync(putObject.Id))
|
||
AddError(ApiErrorCode.NOT_FOUND);
|
||
else
|
||
AddError(ApiErrorCode.CONCURRENCY_CONFLICT);
|
||
return null;
|
||
}
|
||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, dbObject.Id, BizType, AyaEvent.Modified), ct);
|
||
await SearchIndexAsync(dbObject, false);
|
||
await TagBiz.ProcessUpdateTagsInRepositoryAsync(ct, dbObject.Tags, SnapshotOfOriginalDBObj.Tags);
|
||
await NotifyEventProcessor.HandlePotentialNotificationEvent(AyaEvent.Modified, dbObject, SnapshotOfOriginalDBObj);
|
||
return dbObject;
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//DELETE
|
||
//
|
||
internal async Task<bool> DeleteAsync(long id)
|
||
{
|
||
using (var transaction = await ct.Database.BeginTransactionAsync())
|
||
{
|
||
try
|
||
{
|
||
Reminder dbObject = await ct.Reminder.SingleOrDefaultAsync(m => m.Id == id);
|
||
if (dbObject == null){
|
||
AddError(ApiErrorCode.NOT_FOUND);
|
||
return false;
|
||
}
|
||
ValidateCanDelete(dbObject);
|
||
if (HasErrors)
|
||
return false;
|
||
if (HasErrors)
|
||
return false;
|
||
ct.Reminder.Remove(dbObject);
|
||
await ct.SaveChangesAsync();
|
||
|
||
//Log event
|
||
await EventLogProcessor.DeleteObjectLogAsync(UserId, BizType, dbObject.Id, dbObject.Name, ct);
|
||
await Search.ProcessDeletedObjectKeywordsAsync(dbObject.Id, BizType, ct);
|
||
await TagBiz.ProcessDeleteTagsInRepositoryAsync(ct, dbObject.Tags);
|
||
await FileUtil.DeleteAttachmentsForObjectAsync(BizType, dbObject.Id, ct);
|
||
await transaction.CommitAsync();
|
||
await NotifyEventProcessor.HandlePotentialNotificationEvent(AyaEvent.Deleted, dbObject);
|
||
}
|
||
catch
|
||
{
|
||
//Just re-throw for now, let exception handler deal, but in future may want to deal with this more here
|
||
throw;
|
||
}
|
||
return true;
|
||
}
|
||
}
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//SEARCH
|
||
//
|
||
private async Task SearchIndexAsync(Reminder obj, bool isNew)
|
||
{
|
||
var SearchParams = new Search.SearchIndexProcessObjectParameters(UserTranslationId, obj.Id, BizType);
|
||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||
if (isNew)
|
||
await Search.ProcessNewObjectKeywordsAsync(SearchParams);
|
||
else
|
||
await Search.ProcessUpdatedObjectKeywordsAsync(SearchParams);
|
||
}
|
||
|
||
public async Task<Search.SearchIndexProcessObjectParameters> GetSearchResultSummary(long id)
|
||
{
|
||
var obj = await ct.Reminder.SingleOrDefaultAsync(m => m.Id == id);
|
||
var SearchParams = new Search.SearchIndexProcessObjectParameters();
|
||
if (obj != null)
|
||
SearchParams.AddText(obj.Notes).AddText(obj.Name).AddText(obj.Wiki).AddText(obj.Tags).AddCustomFields(obj.CustomFields);
|
||
return SearchParams;
|
||
}
|
||
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//VALIDATION
|
||
//
|
||
|
||
private async Task ValidateAsync(Reminder proposedObj, Reminder currentObj)
|
||
{
|
||
|
||
bool isNew = currentObj == null;
|
||
|
||
//Name required
|
||
if (string.IsNullOrWhiteSpace(proposedObj.Name))
|
||
AddError(ApiErrorCode.VALIDATION_REQUIRED, "Name");
|
||
|
||
//Hexadecimal notation: #RGB[A] R (red), G (green), B (blue), and A (alpha) are hexadecimal characters (0–9, A–F). A is optional. The three-digit notation (#RGB) is a shorter version of the six-digit form (#RRGGBB). For example, #f09 is the same color as #ff0099. Likewise, the four-digit RGB notation (#RGBA) is a shorter version of the eight-digit form (#RRGGBBAA). For example, #0f38 is the same color as #00ff3388.
|
||
if (proposedObj.Color.Length > 12 || proposedObj.Color.Length < 4 || proposedObj.Color[0] != '#')
|
||
{
|
||
AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "UiColor", "UiColor must be valid HEX color value");
|
||
}
|
||
|
||
|
||
|
||
//Any form customizations to validate?
|
||
var FormCustomization = await ct.FormCustom.AsNoTracking().SingleOrDefaultAsync(x => x.FormKey == AyaType.Reminder.ToString());
|
||
if (FormCustomization != null)
|
||
{
|
||
//Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required
|
||
|
||
//validate users choices for required non custom fields
|
||
RequiredFieldsValidator.Validate(this, FormCustomization, proposedObj);
|
||
|
||
//validate custom fields
|
||
CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields);
|
||
}
|
||
|
||
}
|
||
|
||
private void ValidateCanDelete(Reminder inObj)
|
||
{
|
||
//whatever needs to be check to delete this object
|
||
}
|
||
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//REPORTING
|
||
//
|
||
public async Task<JArray> GetReportData(long[] idList)
|
||
{
|
||
JArray ReportData = new JArray();
|
||
while (idList.Any())
|
||
{
|
||
var batch = idList.Take(IReportAbleObject.REPORT_DATA_BATCH_SIZE);
|
||
idList = idList.Skip(IReportAbleObject.REPORT_DATA_BATCH_SIZE).ToArray();
|
||
//query for this batch, comes back in db natural order unfortunately
|
||
var batchResults = await ct.Reminder.Where(z => batch.Contains(z.Id)).ToArrayAsync();
|
||
//order the results back into original
|
||
var orderedList = from id in batch join z in batchResults on id equals z.Id select z;
|
||
foreach (Reminder w in orderedList)
|
||
{
|
||
var jo = JObject.FromObject(w);
|
||
if (!JsonUtil.JTokenIsNullOrEmpty(jo["CustomFields"]))
|
||
jo["CustomFields"] = JObject.Parse((string)jo["CustomFields"]);
|
||
ReportData.Add(jo);
|
||
}
|
||
}
|
||
return ReportData;
|
||
}
|
||
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
// IMPORT EXPORT
|
||
//
|
||
|
||
|
||
public async Task<JArray> GetExportData(long[] idList)
|
||
{
|
||
//for now just re-use the report data code
|
||
//this may turn out to be the pattern for most biz object types but keeping it seperate allows for custom usage from time to time
|
||
return await GetReportData(idList);
|
||
}
|
||
|
||
|
||
|
||
|
||
public async Task<List<string>> ImportData(JArray ja)
|
||
{
|
||
List<string> ImportResult = new List<string>();
|
||
string ImportTag = $"imported-{FileUtil.GetSafeDateFileName()}";
|
||
|
||
var jsset = JsonSerializer.CreateDefault(new JsonSerializerSettings { ContractResolver = new AyaNova.Util.JsonUtil.ShouldSerializeContractResolver(new string[] { "Concurrency", "Id", "CustomFields" }) });
|
||
foreach (JObject j in ja)
|
||
{
|
||
var w = j.ToObject<Reminder>(jsset);
|
||
if (j["CustomFields"] != null)
|
||
w.CustomFields = j["CustomFields"].ToString();
|
||
w.Tags.Add(ImportTag);//so user can find them all and revert later if necessary
|
||
var res = await CreateAsync(w);
|
||
if (res == null)
|
||
{
|
||
ImportResult.Add($"* {w.Name} - {this.GetErrorsAsString()}");
|
||
this.ClearErrors();
|
||
}
|
||
else
|
||
{
|
||
ImportResult.Add($"{w.Name} - ok");
|
||
}
|
||
}
|
||
return ImportResult;
|
||
}
|
||
|
||
|
||
|
||
|
||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||
//JOB / OPERATIONS
|
||
//
|
||
public async Task HandleJobAsync(OpsJob job)
|
||
{
|
||
//Hand off the particular job to the corresponding processing code
|
||
//NOTE: If this code throws an exception the caller (JobsBiz::ProcessJobsAsync) will automatically set the job to failed and log the exeption so
|
||
//basically any error condition during job processing should throw up an exception if it can't be handled
|
||
switch (job.JobType)
|
||
{
|
||
case JobType.BatchCoreObjectOperation:
|
||
await ProcessBatchJobAsync(job);
|
||
break;
|
||
default:
|
||
throw new System.ArgumentOutOfRangeException($"ReminderBiz.HandleJob-> Invalid job type{job.JobType.ToString()}");
|
||
}
|
||
}
|
||
|
||
|
||
|
||
private async Task ProcessBatchJobAsync(OpsJob job)
|
||
{
|
||
await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Running);
|
||
await JobsBiz.LogJobAsync(job.GId, $"LT:StartJob {job.SubType}");
|
||
List<long> idList = new List<long>();
|
||
long FailedObjectCount = 0;
|
||
JObject jobData = JObject.Parse(job.JobInfo);
|
||
if (jobData.ContainsKey("idList"))
|
||
idList = ((JArray)jobData["idList"]).ToObject<List<long>>();
|
||
else
|
||
idList = await ct.Reminder.Select(z => z.Id).ToListAsync();
|
||
bool SaveIt = false;
|
||
foreach (long id in idList)
|
||
{
|
||
try
|
||
{
|
||
SaveIt = false;
|
||
ClearErrors();
|
||
Reminder o = null;
|
||
//save a fetch if it's a delete
|
||
if (job.SubType != JobSubType.Delete)
|
||
o = await GetAsync(id, false);
|
||
switch (job.SubType)
|
||
{
|
||
case JobSubType.TagAddAny:
|
||
case JobSubType.TagAdd:
|
||
case JobSubType.TagRemoveAny:
|
||
case JobSubType.TagRemove:
|
||
case JobSubType.TagReplaceAny:
|
||
case JobSubType.TagReplace:
|
||
SaveIt = TagBiz.ProcessBatchTagOperation(o.Tags, (string)jobData["tag"], jobData.ContainsKey("toTag") ? (string)jobData["toTag"] : null, job.SubType);
|
||
break;
|
||
case JobSubType.Delete:
|
||
if (!await DeleteAsync(id))
|
||
{
|
||
await JobsBiz.LogJobAsync(job.GId, $"LT:Errors {GetErrorsAsString()} id {id}");
|
||
FailedObjectCount++;
|
||
}
|
||
break;
|
||
default:
|
||
throw new System.ArgumentOutOfRangeException($"ProcessBatchJobAsync -> Invalid job Subtype{job.SubType}");
|
||
}
|
||
if (SaveIt)
|
||
{
|
||
o = await PutAsync(o);
|
||
if (o == null)
|
||
{
|
||
await JobsBiz.LogJobAsync(job.GId, $"LT:Errors {GetErrorsAsString()} id {id}");
|
||
FailedObjectCount++;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
await JobsBiz.LogJobAsync(job.GId, $"LT:Errors id({id})");
|
||
await JobsBiz.LogJobAsync(job.GId, ExceptionUtil.ExtractAllExceptionMessages(ex));
|
||
}
|
||
}
|
||
await JobsBiz.LogJobAsync(job.GId, $"LT:BatchJob {job.SubType} {idList.Count}{(FailedObjectCount > 0 ? " - LT:Failed " + FailedObjectCount : "")}");
|
||
await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Completed);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/////////////////////////////////////////////////////////////////////
|
||
|
||
}//eoc
|
||
|
||
|
||
}//eons
|
||
|