Files
raven/server/AyaNova/DataList/AyaDataList.cs
2020-02-27 20:26:51 +00:00

187 lines
6.7 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Text;
using AyaNova.Biz;
using Newtonsoft.Json.Linq;
using Microsoft.EntityFrameworkCore;
namespace AyaNova.DataList
{
/// <summary>
/// DataList object base class
/// </summary>
internal abstract class AyaDataList : IAyaDataList
{
public AyaDataList()
{
}
public string SQLFrom { get; set; }
public List<AyaDataListFieldDefinition> FieldDefinitions { get; set; }
public AuthorizationRoles AllowedRoles { get; set; }
public AyaType DefaultListObjectType { get; set; }
public string DefaultListView { get; set; }
//return array of field keys in list view
public List<string> GetFieldListFromListView(JArray listViewArray)
{
// [{key:"COLUMN UNIQUE KEY ID",sort:"-" or "+",filter:{any:true/false,items:[{FILTER OBJECT SEE BELOW}]} }, {key:"second column unique key"},{...etc...}]
List<string> ret = new List<string>();
for (int i = 0; i < listViewArray.Count; i++)
{
var cm = listViewArray[i];
ret.Add(cm["fld"].Value<string>());
}
return ret;
}
public Newtonsoft.Json.Linq.JArray GenerateListColumnsJSONFromListView(JArray listViewArray)
{
var ListViewFieldKeys = GetFieldListFromListView(listViewArray);
//Generate JSON fragment to return with column definitions
StringBuilder sb = new StringBuilder();
sb.Append("[");
bool FirstColumnAdded = false;
foreach (string s in ListViewFieldKeys)
{
AyaDataListFieldDefinition o = FieldDefinitions.FirstOrDefault(x => x.FieldKey == s);
#if (DEBUG)
//Developers little helper
if (o == null)
{
throw new System.ArgumentNullException($"DEV ERROR in AyaDataList::GenerateListColumnsJSONFromListView - field {s} specified in ListView was NOT found in ObjectFields list");
}
#endif
if (o != null)
{//Here is where we can vet the field name, if it doesn't exist. For production we'll just ignore those ones
if (FirstColumnAdded)
sb.Append(",");
sb.Append("{");
//Build required part of column definition
if (!o.IsCustomField)
sb.Append($"\"cm\":\"{o.LtKey}\",\"dt\":{(int)o.UiFieldDataType}");
else
{
//insert specific type for this custom field
}
//Has a AyObjectType? (linkable / openable)
if (o.AyaObjectType != 0)
sb.Append($",\"ay\":{(int)o.AyaObjectType}");
//Row ID column?
if (o.IsRowId)
{
sb.Append($",\"rid\":1");
}
//Has a Enumtype?
if (!string.IsNullOrEmpty(o.EnumType))
sb.Append($",\"et\":\"{AyaNova.Util.StringUtil.TrimTypeName(o.EnumType)}\"");
sb.Append("}");
FirstColumnAdded = true;
}
}
sb.Append("]");
return JArray.Parse(sb.ToString());
}
// //make sure the template parses and all the fields specified are really existant
// //this is more for dev errors or api users becuase the client shouldn't generate bad templates
// public bool ValidateTemplate(string template)
// {
// try
// {
// //parse the template
// var jtemplate = JObject.Parse(template);
// var fullFields = ((JArray)jtemplate["full"]).ToObject<string[]>();
// var miniFields = ((JArray)jtemplate["mini"]).ToObject<string[]>();
// foreach (string s in fullFields)
// {
// AyaDataListFieldDefinition o = FieldDefinitions.FirstOrDefault(x => x.FieldKey == s);
// if (o == null)
// {
// return false;
// }
// }
// foreach (string s in miniFields)
// {
// AyaDataListFieldDefinition o = FieldDefinitions.FirstOrDefault(x => x.FieldKey == s);
// if (o == null)
// {
// return false;
// }
// }
// }
// catch
// {
// return false;
// }
// return true;
// }
//Find and return a dictionary of all custom fields definitions for all types in list
//used to build the column array and define specific type defined for custom fields so client datatable
//knows how to format it
private Dictionary<string, JArray> GetCustomFieldDefinitionsForList()
{
Dictionary<string, JArray> ret = new Dictionary<string, JArray>();
//custom fields handling
foreach (AyaDataListFieldDefinition d in this.FieldDefinitions)
{
if (d.IsCustomField)
{
//this relies on the convention I'm using of AyaType name as the first part of all custom fields lT keys, e.g.
//WidgetCustom1 -> Widget
var ayatypename = d.LtKey.Split("Custom")[0];
if (!ret.ContainsKey(ayatypename))
{
//fetch it and set it
using (var ct = AyaNova.Util.ServiceProviderProvider.DBContext)
{
var fc = ct.FormCustom.AsNoTracking().SingleOrDefault(x => x.FormKey == ayatypename);
#if (DEBUG)
if (fc == null)
{
throw new System.ArgumentNullException($"AyaDataList:GetCustomFieldDefinitionsForList, Custom field object type {ayatypename} has no FormCustom defined");
}
#endif
if (fc != null)
ret.Add(ayatypename, JArray.Parse(fc.Template));
}
}
}
}
return ret;
}
}//eoc
}//eons