EchoTex_Payroll/HRM.UI/Controllers/DynamicPickerController.cs

179 lines
6.5 KiB
C#
Raw Normal View History

2024-10-14 10:01:49 +06:00
using System.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using HRM.BO;
using HRM.DA;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
namespace HRM.UI.Controllers
{
[ApiController]
[Route("api/DynamicPicker")]
[Authorize]
public class DynamicPickerController : ControllerBase
{
private string ServiceAssemblyName = "HRM.DA";
private string BOAssemblyName = "HRM.BO";
public DynamicPickerController()
{
}
[HttpPost]
[Route("PickerSearch")]
public ActionResult<ICollection> PickerSearch(DynamicPicker cty)
{
Type t = this.GetTypeofObject(cty.sourceObject, true);
var obj = Activator.CreateInstance(t); //return Ok();
string fName = "Get";
if (cty.searchFunctionName != null && cty.searchFunctionName != "undefined")
{
fName = cty.searchFunctionName;
}
#region function parameter setup
// var svalues = cty.seachControls.Where(x => x.value != null && x.value != "undefined" && x.value != "");
object[] param = new object[cty.searchControls.Count()];
Type[] paramtypes = new Type[cty.searchControls.Count()];
int i = 0;
bool emptyvalue = false;
foreach (DynamicPickerSearch item in cty.searchControls)
{
if (item.value == "undefined" && item.value == "")
{
emptyvalue = true;
}
if(item.PropertyName.ToUpper() == "PAYROLLTYPEID" )
{
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
if(currentUser.PayrollTypeID !=null)
param[i] = currentUser.PayrollTypeID;
else if (cty.PayrollTypeID !=null)
{
param[i] = cty.PayrollTypeID;
}
else
{
throw new Exception("Failed to get PayrollTypeID");
}
paramtypes[i] = typeof(int);
i = i + 1;
continue;
}
switch (item.controlType)
{
case EnumDynamicControlType.TextBox:
{
if (emptyvalue == true) param[i] = string.Empty;
else param[i] = item.value;
paramtypes[i] = typeof(string);
break;
}
case EnumDynamicControlType.DropDownEnumData:
{
paramtypes[i] = this.GetTypeofObject(item.PropertyName, false);
if (emptyvalue == true) param[i] = null;
else Enum.TryParse(paramtypes[i], item.value, out param[i]);
// This code is used to get only active status data for dynamic-picker
#region Active Status
param = new object[2];
param[i] = EnumStatus.Active;
param[i + 1] = (int)CurrentUser.GetCurrentUser(HttpContext.User).PayrollTypeID;
paramtypes = new Type[2];
paramtypes[i] = typeof(EnumStatus);
paramtypes[i + 1] = typeof(int);
#endregion Active Status
break;
}
case EnumDynamicControlType.DropDownServerData:
{
int Newintvlaue = 0;
if (emptyvalue == true) param[i] = null;
else Int32.TryParse(item.value, out Newintvlaue);
param[i] = Newintvlaue;
paramtypes[i] = ((string) param[i]).GetType();
break;
}
case EnumDynamicControlType.CheckBox:
paramtypes[i] = ((Boolean) param[i]).GetType();
break;
case EnumDynamicControlType.DateTime:
paramtypes[i] = ((DateTime) param[i]).GetType();
break;
case EnumDynamicControlType.NumText:
paramtypes[i] = ((int) param[i]).GetType();
break;
case EnumDynamicControlType.IntNumText:
paramtypes[i] = Convert.ToInt32(param[i]).GetType(); // ((int) param[i]).GetType();
int intvlaue = 0;
if (emptyvalue == true) param[i] = null;
else Int32.TryParse(item.value, out intvlaue);
param[i] = intvlaue;
break;
default:
break;
}
i = i + 1;
}
#endregion
MethodInfo voidMethodInfo = t.GetMethod(fName, paramtypes);
object ost = voidMethodInfo.Invoke(obj, param);
dynamic collection = ost as IEnumerable;
return collection;
}
private Type GetTypeofObject(string objectName, bool IsService)
{
string AssName = IsService == false
? this.BOAssemblyName + "." + objectName
: this.ServiceAssemblyName + "." + objectName + "Service";
Assembly assem = Assembly.Load(IsService == false ? this.BOAssemblyName : this.ServiceAssemblyName);
Type t = assem.GetType(AssName);
return t;
}
}
public class DynamicPicker
{
public string sourceObject { get; set; }
public List<DynamicPickerSearch> searchControls { get; set; }
public string IDColumnName { get; set; }
public string parentID { get; set; }
public string parentColumnName { get; set; }
public string searchFunctionName { get; set; }
public int? PayrollTypeID { get; set; }
public DynamicPicker()
{
}
}
public class DynamicPickerSearch
{
public string value { get; set; }
public string PropertyName { get; set; }
public string controlID { get; set; }
public EnumDynamicControlType controlType { get; set; }
}
}