Files
raven/server/AyaNova/biz/ImportAyaNova7Biz.cs
2018-08-30 19:25:09 +00:00

222 lines
10 KiB
C#

using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using EnumsNET;
using AyaNova.Util;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using AyaNova.Models;
namespace AyaNova.Biz
{
internal class ImportAyaNova7Biz : BizObject, IJobObject
{
private readonly AyContext ct;
public readonly long userId;
private readonly AuthorizationRoles userRoles;
internal ImportAyaNova7Biz(AyContext dbcontext, long currentUserId, AuthorizationRoles UserRoles)
{
ct = dbcontext;
userId = currentUserId;
userRoles = UserRoles;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//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
//There might be future other job types so doing it like this for all biz job handlers for now
switch (job.JobType)
{
case JobType.ImportV7Data:
await ProcessImportV7JobAsync(job);
break;
default:
throw new System.ArgumentOutOfRangeException($"ImportAyaNovaBiz.HandleJob-> Invalid job type{job.JobType.ToString()}");
}
}
/// <summary>
/// /// Handle the test job
/// </summary>
/// <param name="job"></param>
private async Task ProcessImportV7JobAsync(OpsJob job)
{
//NOTE: If this code throws an exception the caller 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
List<ImportAyaNova7MapItem> importMap = new List<ImportAyaNova7MapItem>();
JobsBiz.UpdateJobStatus(job.GId, JobStatus.Running, ct);
JobsBiz.LogJob(job.GId, $"ImportAyaNova7 starting", ct);
//Get the import filename from the jsondata
JObject jobData = JObject.Parse(job.JobInfo);
var importFileName = jobData["ImportFileName"].Value<string>();
if (string.IsNullOrWhiteSpace(importFileName))
{
throw new System.ArgumentNullException("ImportAyaNova7 job failed due to no import filename being specified");
}
if (!FileUtil.UtilityFileExists(importFileName))
{
throw new System.ArgumentNullException("ImportAyaNova7 job failed due to import file specified not existing");
}
//get the contents of the archive
List<string> zipEntries = FileUtil.ZipGetUtilityFileEntries(importFileName);
//Iterate through the import items in the preferred order, checking for corresponding entries in the zip file
//In turn try to instantiate the type and id job that can handle that import, attempt to case or see if implements IImportAyaNova7Object
//, if null / not supported for import then skip and log as not currently supported
//Pass off the JSON data from the import file into the import job item by item
//NOTE: Many of these require a second pass - one to get the object imported and then another to set another imported object to that object
//for example scheduleable user groups are imported as tags, but then a second pass is required to tag the users of that group
//USERS
await DoImport("GZTW.AyaNova.BLL.User", AyaType.User, job.GId, importMap, importFileName, zipEntries);
//Now can do event log entries
await DoImport("GZTW.AyaNova.BLL.User-eventlog", AyaType.User, job.GId, importMap, importFileName, zipEntries);
//IMPORT UNIT MODEL CATEGORIES AS TAGS
await DoImport("GZTW.AyaNova.BLL.UnitModelCategory", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//IMPORT Unit service type AS TAGS
await DoImport("GZTW.AyaNova.BLL.UnitServiceType", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//IMPORT Workorder Item Type AS TAGS
await DoImport("GZTW.AyaNova.BLL.WorkorderItemType", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//IMPORT Client group AS TAGS
await DoImport("GZTW.AyaNova.BLL.ClientGroup", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//IMPORT Workorder category AS TAGS
await DoImport("GZTW.AyaNova.BLL.WorkorderCategory", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//IMPORT Part Category AS TAGS
await DoImport("GZTW.AyaNova.BLL.PartCategory", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//IMPORT Dispatch zones AS TAGS
await DoImport("GZTW.AyaNova.BLL.DispatchZone", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//IMPORT Scheduleable User Groups AS TAGS
await DoImport("GZTW.AyaNova.BLL.ScheduleableUserGroup", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//Now can set users to correct tag for scheduleable user group
await DoImport("GZTW.AyaNova.BLL.User-scheduleableusergrouptags", AyaType.User, job.GId, importMap, importFileName, zipEntries);
//IMPORT REGIONS AS TAGS
await DoImport("GZTW.AyaNova.BLL.Region", AyaType.Tag, job.GId, importMap, importFileName, zipEntries);
//IMPORT LOCALES
await DoImport("GZTW.AyaNova.BLL.Locale", AyaType.Locale, job.GId, importMap, importFileName, zipEntries);
//Now can do user locale settings properly
await DoImport("GZTW.AyaNova.BLL.User-locale", AyaType.User, job.GId, importMap, importFileName, zipEntries);
//CLIENT
//do import for client here
//Now can do user client settings properly
//await DoImport("GZTW.AyaNova.BLL.User-client", AyaType.User, job.GId, importMap, importFileName, zipEntries);
//HEADOFFICE
//do import for ho here
//Now can do user ho settings properly
//await DoImport("GZTW.AyaNova.BLL.User-headoffice", AyaType.User, job.GId, importMap, importFileName, zipEntries);
//TODO: second pass now that all objects are in the db, need to go through again and set properties based on all imported data, e.g. scheduleableUserGroups are tags that must now be set on users
//----------------
JobsBiz.LogJob(job.GId, "ImportAyaNova7 finished", ct);
JobsBiz.UpdateJobStatus(job.GId, JobStatus.Completed, ct);
}
/// <summary>
/// This method does the actual import by
/// - Fetching the list of entries in the zip archive that match the passed in startsWtih (folder name in zip archive)
/// - Instantiating the corresponding new biz object type to handle the import
/// - Passing the json parsed to the biz object one at a time to do the import
/// </summary>
/// <param name="entryStartsWith"></param>
/// <param name="importerType"></param>
/// <param name="jobId"></param>
/// <param name="importMap"></param>
/// <param name="importFileName"></param>
/// <param name="zipEntries"></param>
/// <returns></returns>
private async Task DoImport(string entryStartsWith, AyaType importerType, Guid jobId, List<ImportAyaNova7MapItem> importMap, string importFileName, List<string> zipEntries)
{
var zipObjectList = zipEntries.Where(m => m.StartsWith(entryStartsWith)).ToList();
long importCount = 0;
long notImportCount = 0;
if (zipObjectList.Count > 0)
{
JobsBiz.LogJob(jobId, $"Starting import of {entryStartsWith} objects", ct);
var jList = FileUtil.ZipGetUtilityArchiveEntriesAsJsonObjects(zipObjectList, importFileName);
IImportAyaNova7Object o = (IImportAyaNova7Object)BizObjectFactory.GetBizObject(importerType, ct);
foreach (JObject j in jList)
{
bool bImportSucceeded = false;
//some new types can import multiple old types and it might matter which is which to the importer
//so tag it with the original type
j.Add("V7_TYPE", JToken.FromObject(entryStartsWith));
bImportSucceeded = await o.ImportV7Async(j, importMap, jobId);
if (bImportSucceeded)
importCount++;
else
notImportCount++;
}
if (importCount > 0)
{
JobsBiz.LogJob(jobId, $"Successfully imported {importCount.ToString()} of {zipObjectList.Count.ToString()} {entryStartsWith} objects", ct);
}
if (notImportCount > 0)
{
JobsBiz.LogJob(jobId, $"Did not import {notImportCount.ToString()} of {zipObjectList.Count.ToString()} {entryStartsWith} objects", ct);
}
}
}
//Other job handlers here...
/////////////////////////////////////////////////////////////////////
}//eoc
}//eons