Files
raven/server/AyaNova/DataList/AyaDataList.cs
2020-01-21 23:18:29 +00:00

82 lines
2.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Text;
using AyaNova.Biz;
using Newtonsoft.Json.Linq;
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 ListKey { get; set; }
public string DefaultDataListDisplayTemplate { get; set; }
public string GenerateMINIListColumnsJSON()
{
return $"[ {{\"cm\":\"df\",\"dt\":0,\"ay\":{(int)DefaultListObjectType}}},{{\"cm\":\"Widget\",\"dt\":{(int)AyaUiFieldDataType.Text},\"ay\":{(int)DefaultListObjectType}}}]";
}
public string GenerateListColumnsJSONFromTemplate(string template)
{
//parse the template
var jtemplate = JObject.Parse(template);
//convert to strings (https://stackoverflow.com/a/33836599/8939)
var fullFields = ((JArray)jtemplate["full"]).ToObject<string[]>();
//Generate JSON fragment to return with column definitions
StringBuilder sb = new StringBuilder();
sb.Append("[");
//df First column is always the df column
sb.Append($"{{\"cm\":\"df\",\"dt\":0,\"ay\":{(int)DefaultListObjectType}}}");
foreach (string s in fullFields)
{
AyaDataListFieldDefinition o = FieldDefinitions.FirstOrDefault(x => x.FieldKey == s);
#if (DEBUG)
//Developers little helper
if (o == null)
{
throw new System.ArgumentNullException($"DEV ERROR in AyaDataList::GenerateListColumnsJSONFromTemplate - field {s} specified in template 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
sb.Append(",");
sb.Append("{");
//Build required part of column definition
sb.Append($"\"cm\":\"{o.LtKey}\",\"dt\":{(int)o.UiFieldDataType}");
//Has a AyObjectType? (linkable / openable)
if (o.AyaObjectType != 0)
sb.Append($",\"ay\":{(int)o.AyaObjectType}");
sb.Append("}");
}
}
sb.Append("]");
return sb.ToString();
}
}//eoc
}//eons