288 lines
11 KiB
C#
288 lines
11 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Sarand.BusinessMgr.Net.Standard.Base.Bus.Control;
|
|
using Sarand.BusinessMgr.Web.Control.Models;
|
|
using Sarand.BusinessMgr.Web.Control.Models.Instance;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Sarand.BusinessMgr.Web.Control.Controllers.Instance
|
|
{
|
|
//=======================================================================
|
|
partial class OwnerController
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
|
|
// Owner data.
|
|
private class OwnerListData : AjaxResponse_Data.ItemData_Base
|
|
{
|
|
public String Name { get; set; }
|
|
public String ContactName { get; set; }
|
|
public String ContactEmailAddress { get; set; }
|
|
public String ContactPhone { get; set; }
|
|
public long? RepositoryCount { get; set; }
|
|
}
|
|
private class OwnerItemData : OwnerListData
|
|
{
|
|
public String ContactPhoneMain { get; set; }
|
|
public String ContactPhoneAlt { get; set; }
|
|
}
|
|
private void PopulateOwnerResponseListData(SbmModel_Control model, OwnerListData data, SbmcOwner owner)
|
|
{
|
|
data.Id = SysId.ToString(owner.Id);
|
|
data.Name = owner.Name;
|
|
data.ContactName = owner.ContactName;
|
|
data.ContactEmailAddress = owner.ContactEmailAddress;
|
|
data.ContactPhone = StringUtil.OnBlank(owner.ContactPhoneMain, owner.ContactPhoneAlt);
|
|
data.StatusCode = owner.StatusCode;
|
|
data.StatusDesc = owner.StatusDesc;
|
|
}
|
|
private void PopulateOwnerResponseItemData(SbmModel_Control model, OwnerItemData data, SbmcOwner owner)
|
|
{
|
|
PopulateOwnerResponseListData(model, data, owner);
|
|
data.ContactPhoneMain = owner.ContactPhoneMain;
|
|
data.ContactPhoneAlt = owner.ContactPhoneAlt;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Owner List ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_OwnerList : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_OwnerList(SbmModel_Control model) : base(model) { }
|
|
// Owner list.
|
|
public List<OwnerListData> Owners { get; } = new List<OwnerListData>();
|
|
// Reference data.
|
|
public List<CodeDesc> Statuses { get; } = new List<CodeDesc>();
|
|
public List<CodeDesc> StatusesCreate { get; } = new List<CodeDesc>();
|
|
public String StatusCodeDefault { get; set; }
|
|
}
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Instance/Owner/Ajax/GetList")]
|
|
[Route("Instance/Owner/Ajax/GetOwners")]
|
|
[Route("Instance/Owner/Ajax/GetOwnerList")]
|
|
public IActionResult Ajax_GetOwnerList(Model_Owner model)
|
|
{
|
|
String messageTitle = Localiser["label.owners"];
|
|
AjaxResponse_OwnerList responseData = new AjaxResponse_OwnerList(model);
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidReferer) return Forbid();
|
|
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
|
|
RefreshModel(model);
|
|
|
|
// Reference data - statuses.
|
|
foreach (SaStatus status in SbmcOwner.ValidStatuses)
|
|
{
|
|
responseData.Statuses.Add(SacsSystem.GetStatusCode(status), Localiser.GetStatusCaption(status));
|
|
if (status != SaStatus.Deleted)
|
|
responseData.StatusesCreate.Add(SacsSystem.GetStatusCode(status), Localiser.GetStatusCaption(status));
|
|
}
|
|
responseData.StatusCodeDefault = SacsSystem.GetStatusCode(SbmcOwner.DefaultStatus);
|
|
|
|
// Item filter.
|
|
SbmcOwner.Params prmOwner = new SbmcOwner.Params();
|
|
prmOwner.Search = model.Filter.Search;
|
|
prmOwner.StatusCat = model.Filter.GetStatusCat();
|
|
model.Paging.ItemTotal = (ulong)ControlInstanceMgr.GetOwnerCount(prmOwner);
|
|
model.PopulatePagingTo(prmOwner.ResultsPaging);
|
|
|
|
// Item columns.
|
|
ColumnDfn colId = ControlInstanceMgr.GetOwnerColumnDfn(SbmcOwner.Column.Id);
|
|
ColumnDfn colName = ControlInstanceMgr.GetOwnerColumnDfn(SbmcOwner.Column.Name);
|
|
ColumnDfn colContactName = ControlInstanceMgr.GetOwnerColumnDfn(SbmcOwner.Column.ContactName);
|
|
ColumnDfn colContactEmail = ControlInstanceMgr.GetOwnerColumnDfn(SbmcOwner.Column.ContactEmailAddress);
|
|
ColumnDfn colContactPhoneMain = ControlInstanceMgr.GetOwnerColumnDfn(SbmcOwner.Column.ContactPhoneMain);
|
|
ColumnDfn colContactPhoneAlt = ControlInstanceMgr.GetOwnerColumnDfn(SbmcOwner.Column.ContactPhoneAlt);
|
|
ColumnDfn colRepoCount = ControlInstanceMgr.GetOwnerColumnDfn(SbmcOwner.Column.RepositoryCount);
|
|
ColumnDfn colStatus = ControlInstanceMgr.GetOwnerColumnDfn(SbmcOwner.Column.Status);
|
|
List<ColumnDfn> columns = new List<ColumnDfn>()
|
|
{
|
|
colId, colName, colContactName, colContactEmail,
|
|
colContactPhoneMain, colContactPhoneAlt, colRepoCount, colStatus
|
|
};
|
|
|
|
// Query items.
|
|
using (DbResultSet resultSet = ControlInstanceMgr.ExecuteOwnerQuery(columns, prmOwner))
|
|
{
|
|
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
|
|
{
|
|
NamedValue.Map values = resultSet.GetRecordValues();
|
|
OwnerListData ownerData = new OwnerListData()
|
|
{
|
|
Id = values.GetString(colId.DataName),
|
|
Name = values.GetString(colName.DataName),
|
|
ContactName = values.GetString(colContactName.DataName),
|
|
ContactEmailAddress = values.GetString(colContactEmail.DataName),
|
|
ContactPhone = StringUtil.OnBlank(values.GetString(colContactPhoneMain.DataName), values.GetString(colContactPhoneAlt.DataName)),
|
|
StatusCode = values.GetString(colStatus.DataName),
|
|
StatusDesc = SacsSystem.GetStatusDescForCode(values.GetString(colStatus.DataName))
|
|
};
|
|
long repositoryCount = values.GetLong(colRepoCount.DataName);
|
|
if (repositoryCount > 0)
|
|
ownerData.RepositoryCount = repositoryCount;
|
|
responseData.Owners.Add(ownerData);
|
|
}
|
|
}
|
|
if (model.Paging.IsPaged)
|
|
model.Paging.ItemCount = (ushort)responseData.Owners.Count;
|
|
|
|
// Complete response.
|
|
responseData.SetPaging(model);
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Owner ajax request.
|
|
//-------------------------------------------------------------------
|
|
private class AjaxResponse_Owner : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_Owner(SbmModel_Control model) : base(model) { }
|
|
// Owner details.
|
|
public OwnerItemData owner { get; } = new OwnerItemData();
|
|
}
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Instance/Owner/Ajax/GetItem")]
|
|
[Route("Instance/Owner/Ajax/GetOwner")]
|
|
public IActionResult Ajax_GetOwner(Model_Owner model)
|
|
{
|
|
String messageTitle = Localiser["label.owner"];
|
|
AjaxResponse_Owner responseData = new AjaxResponse_Owner(model);
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidReferer) return Forbid();
|
|
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
|
|
RefreshModel(model);
|
|
|
|
// Record disposable items as they are created so all can be disposed when needed.
|
|
using (DisposableCollection<SacsHierarchyItem> disposables = new DisposableCollection<SacsHierarchyItem>())
|
|
{
|
|
|
|
// Identify item.
|
|
SysId ownerId = SysId.ToSysId(model.Owner.Id);
|
|
if (!SysId.IsValid(ownerId))
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.specified"]));
|
|
|
|
// Retrieve item.
|
|
SbmcOwner owner = ControlInstanceMgr.GetOwnerById(ownerId);
|
|
disposables.Add(owner);
|
|
if (owner == null)
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Failed to retrieve owner for Id: '" + ownerId + "'");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
|
|
}
|
|
|
|
// Construct response.
|
|
PopulateOwnerResponseItemData(model, responseData.owner, owner);
|
|
responseData.Success = true;
|
|
}
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Store Owner ajax request.
|
|
//-------------------------------------------------------------------
|
|
[HttpPost]
|
|
[Route("Instance/Owner/Ajax/StoreItem")]
|
|
[Route("Instance/Owner/Ajax/StoreOwner")]
|
|
public IActionResult Ajax_StoreOwner(Model_Owner model)
|
|
{
|
|
String messageTitle = Localiser["label.owner"];
|
|
AjaxResponse_Owner responseData = new AjaxResponse_Owner(model);
|
|
|
|
// Validate session and referrer.
|
|
if (!ValidReferer) return Forbid();
|
|
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
|
|
RefreshModel(model);
|
|
|
|
// Validate item Id.
|
|
SysId ownerId = SysId.ToSysId(model.Owner.Id);
|
|
if (StringUtil.ToBool(model.Owner.Exists) && !SysId.IsValid(ownerId))
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Owner Id not supplied for existing item.");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
if (!StringUtil.ToBool(model.Owner.Exists) && SysId.IsValid(ownerId))
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Owner Id supplied for new item.");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.system"]));
|
|
}
|
|
|
|
// Validate item properties.
|
|
model.ValidateOwnerData(responseData);
|
|
if (responseData.HasControlErrors())
|
|
return Json(responseData);
|
|
|
|
// Record disposable items as they are created so all can be disposed when needed.
|
|
using (DisposableCollection<SacsHierarchyItem> disposables = new DisposableCollection<SacsHierarchyItem>())
|
|
{
|
|
|
|
// Retrieve existing item.
|
|
SbmcOwner owner = ControlInstanceMgr.GetOwnerById(ownerId);
|
|
disposables.Add(owner);
|
|
if (owner == null && SysId.IsValid(ownerId))
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Failed to retrieve owner for Id: '" + model.Owner.Id + "'");
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"]));
|
|
}
|
|
|
|
// Create new item if required.
|
|
if (owner == null)
|
|
{
|
|
owner = ControlInstanceMgr.CreateOwner();
|
|
}
|
|
|
|
// Populate item properties.
|
|
if (model.Owner.Name != null)
|
|
owner.Name = model.Owner.Name;
|
|
if (model.Owner.ContactName != null)
|
|
owner.ContactName = model.Owner.ContactName;
|
|
if (model.Owner.ContactEmailAddress != null)
|
|
owner.ContactEmailAddress = model.Owner.ContactEmailAddress;
|
|
if (model.Owner.ContactPhoneMain != null)
|
|
owner.ContactPhoneMain = model.Owner.ContactPhoneMain;
|
|
if (model.Owner.ContactPhoneAlt != null)
|
|
owner.ContactPhoneAlt = model.Owner.ContactPhoneAlt;
|
|
if (model.Owner.StatusCode != null)
|
|
owner.Status = model.Owner.GetStatus();
|
|
|
|
// Store item.
|
|
if (!SysId.IsValid(owner.Id))
|
|
{
|
|
if (!ControlInstanceMgr.InsertOwner(owner, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Failed to insert owner: " + ControlInstanceMgr.ErrorStateMessage);
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.insert.failed"] + "\n\n" + ControlInstanceMgr.ErrorStateMessage));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!ControlInstanceMgr.UpdateOwner(owner, DbUpdateInfo))
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Failed to update owner [Id:" + SysId.ToString(owner.Id) + "]: " + ControlInstanceMgr.ErrorStateMessage);
|
|
return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"] + "\n\n" + ControlInstanceMgr.ErrorStateMessage));
|
|
}
|
|
}
|
|
|
|
// Construct response.
|
|
PopulateOwnerResponseItemData(model, responseData.owner, owner);
|
|
responseData.Success = true;
|
|
}
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|