Files
raven/server/AyaNova/biz/ObjectFields.cs
2020-01-15 23:24:29 +00:00

225 lines
12 KiB
C#

using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Text;
namespace AyaNova.Biz
{
//************************************************
// This contains all the fields that are:
// - Customizable on forms
// - In grid list templates
//In addition it serves as a source for valid object keys in AvailableObjectKeys, and a source of database column names (tolower)
//
public static class ObjectFields
{
//DEFINE VALID KEYS HERE
//For objects that are both list and edit form it's just the name of the object
//For objects that are compound list objects or reporting objects it's whatever uniquely but clearly identifies that
public const string WIDGET_KEY = "widget";
public const string USER_KEY = "user";
public static List<string> AvailableObjectKeys
{
get
{
List<string> l = new List<string>{
WIDGET_KEY, USER_KEY
};
return l;
}
}
public static bool IsValidObjectKey(string key)
{
return AvailableObjectKeys.Contains(key);
}
public static List<ObjectField> ObjectFieldsList(string key)
{
/*
***************************** WARNING: Be careful here, if a standard field is hideable and also it's DB SCHEMA is set to NON NULLABLE then the CLIENT end needs to set a default
***************************** Otherwise the hidden field can't be set and the object can't be saved EVER
Defaults of paramterless constructor:
SharedLTKey = false;
Hideable = true;
Custom = false;
Filterable = true;
Sortable = true;
MiniAvailable = true;
*/
List<ObjectField> l = new List<ObjectField>();
switch (key)
{
case WIDGET_KEY:
#region WIDGET_KEY
l.Add(new ObjectField { Key = "WidgetName", PropertyName = "Name", DataType = (int)AyaDataType.Text, Hideable = false });
l.Add(new ObjectField { Key = "WidgetSerial", PropertyName = "Serial", DataType = (int)AyaDataType.Integer });
l.Add(new ObjectField { Key = "WidgetDollarAmount", PropertyName = "DollarAmount", DataType = (int)AyaDataType.Currency });
l.Add(new ObjectField { Key = "WidgetCount", PropertyName = "Count", DataType = (int)AyaDataType.Integer });
l.Add(new ObjectField { Key = "WidgetRoles", PropertyName = "Roles", DataType = (int)AyaDataType.Enum, EnumType = typeof(AuthorizationRoles).ToString() });
l.Add(new ObjectField { Key = "WidgetStartDate", PropertyName = "StartDate", DataType = (int)AyaDataType.DateTime });
l.Add(new ObjectField { Key = "WidgetEndDate", PropertyName = "EndDate", DataType = (int)AyaDataType.DateTime });
l.Add(new ObjectField { Key = "WidgetNotes", PropertyName = "Notes", DataType = (int)AyaDataType.Text });
l.Add(new ObjectField { Key = "Active", PropertyName = "Active", DataType = (int)AyaDataType.Bool, Hideable = false, SharedLTKey = true });
l.Add(new ObjectField { Key = "Tags", PropertyName = "Tags", DataType = (int)AyaDataType.Tags, SharedLTKey = true });
l.Add(new ObjectField { Key = "WidgetCustom1", PropertyName = "WidgetCustom1", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom2", PropertyName = "WidgetCustom2", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom3", PropertyName = "WidgetCustom3", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom4", PropertyName = "WidgetCustom4", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom5", PropertyName = "WidgetCustom5", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom6", PropertyName = "WidgetCustom6", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom7", PropertyName = "WidgetCustom7", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom8", PropertyName = "WidgetCustom8", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom9", PropertyName = "WidgetCustom9", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom10", PropertyName = "WidgetCustom10", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom11", PropertyName = "WidgetCustom11", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom12", PropertyName = "WidgetCustom12", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom13", PropertyName = "WidgetCustom13", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom14", PropertyName = "WidgetCustom14", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom15", PropertyName = "WidgetCustom15", Custom = true });
l.Add(new ObjectField { Key = "WidgetCustom16", PropertyName = "WidgetCustom16", Custom = true });
break;
#endregion
case USER_KEY:
#region USER_KEY
l.Add(new ObjectField { Key = "Name", PropertyName = "Name", SharedLTKey = true, DataType = (int)AyaDataType.Text, Hideable = false });
l.Add(new ObjectField { Key = "UserEmployeeNumber", PropertyName = "EmployeeNumber", DataType = (int)AyaDataType.Text });
l.Add(new ObjectField { Key = "AuthorizationRoles", PropertyName = "Roles", Hideable = false, DataType = (int)AyaDataType.Enum, EnumType = typeof(AuthorizationRoles).ToString() });
l.Add(new ObjectField { Key = "UserNotes", PropertyName = "Notes", DataType = (int)AyaDataType.Text });
l.Add(new ObjectField { Key = "UserUserType", PropertyName = "UserType", Hideable = false, DataType = (int)AyaDataType.Enum, EnumType = typeof(UserType).ToString() });
l.Add(new ObjectField { Key = "Active", PropertyName = "Active", DataType = (int)AyaDataType.Bool, Hideable = false, SharedLTKey = true });
l.Add(new ObjectField { Key = "Tags", PropertyName = "Tags", DataType = (int)AyaDataType.Tags, SharedLTKey = true });
l.Add(new ObjectField { Key = "UserCustom1", PropertyName = "UserCustom1", Custom = true });
l.Add(new ObjectField { Key = "UserCustom2", PropertyName = "UserCustom2", Custom = true });
l.Add(new ObjectField { Key = "UserCustom3", PropertyName = "UserCustom3", Custom = true });
l.Add(new ObjectField { Key = "UserCustom4", PropertyName = "UserCustom4", Custom = true });
l.Add(new ObjectField { Key = "UserCustom5", PropertyName = "UserCustom5", Custom = true });
l.Add(new ObjectField { Key = "UserCustom6", PropertyName = "UserCustom6", Custom = true });
l.Add(new ObjectField { Key = "UserCustom7", PropertyName = "UserCustom7", Custom = true });
l.Add(new ObjectField { Key = "UserCustom8", PropertyName = "UserCustom8", Custom = true });
l.Add(new ObjectField { Key = "UserCustom9", PropertyName = "UserCustom9", Custom = true });
l.Add(new ObjectField { Key = "UserCustom10", PropertyName = "UserCustom10", Custom = true });
l.Add(new ObjectField { Key = "UserCustom11", PropertyName = "UserCustom11", Custom = true });
l.Add(new ObjectField { Key = "UserCustom12", PropertyName = "UserCustom12", Custom = true });
l.Add(new ObjectField { Key = "UserCustom13", PropertyName = "UserCustom13", Custom = true });
l.Add(new ObjectField { Key = "UserCustom14", PropertyName = "UserCustom14", Custom = true });
l.Add(new ObjectField { Key = "UserCustom15", PropertyName = "UserCustom15", Custom = true });
l.Add(new ObjectField { Key = "UserCustom16", PropertyName = "UserCustom16", Custom = true });
break;
#endregion
default:
throw new System.ArgumentOutOfRangeException($"ObjectFields: {key} is not a valid object key");
}
return l;
}
public static string TranslateLTCustomFieldToInternalCustomFieldName(string lTCustomFieldName)
{
var i = System.Convert.ToInt32(System.Text.RegularExpressions.Regex.Replace(
lTCustomFieldName, // Our input
"[^0-9]", // Select everything that is not in the range of 0-9
"" // Replace that with an empty string.
));
return $"c{i}";
}
//Standard mini COLUMN definition
public static string GenerateMINIListColumnsJSON(AyaType defaultLinkType)
{
return $"[ {{\"cm\":\"_df_\",\"dt\":0,\"ay\":{(int)defaultLinkType}}},{{\"cm\":\"Widget\",\"dt\":{(int)AyaDataType.Text},\"ay\":{(int)defaultLinkType}}}]";
}
//Accept a json template
//return a column list suitable for api list return
public static string GenerateListColumnsJSONFromTemplate(AyaType defaultLinkType, string ObjectKey, string template)
{
//parse the template
var jtemplate = JObject.Parse(template);
var fields = ObjectFieldsList(ObjectKey);
//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)defaultLinkType}}}");
foreach (string s in fullFields)
{
ObjectField o = fields.FirstOrDefault(x => x.Key == s);
if (o != null)
{//Here is where we can vet the field name, if it doesn't exist though, for now we'll just ignore those ones
sb.Append(",");
sb.Append("{");
//Build required part of column definition
sb.Append($"\"cm\":\"{o.Key}\",\"dt\":{(int)o.DataType}");
//Has a AyObjectType? (linkable / openable)
if (o.AyObjectType != 0)
sb.Append($",\"ay\":{(int)o.AyObjectType}");
sb.Append("}");
}
}
sb.Append("]");
return sb.ToString();
}
}//eoc ObjectFields
public class ObjectField
{
public string Key { get; set; }
public string PropertyName { get; set; }
public bool Hideable { get; set; }
public bool SharedLTKey { get; set; }
public bool Custom { get; set; }
public bool Filterable { get; set; }
public bool Sortable { get; set; }
public bool MiniAvailable { get; set; }
public int DataType { get; set; }
//If it's an enum DataType then this is the specific enum type which sb the name of the class that holds the enum in the server project
public string EnumType { get; set; }
//if field is a reference to another object (i.e. a client in a workorders list)
//then the type to open is set here
public int AyObjectType { get; set; }
public ObjectField()
{
//most common defaults
SharedLTKey = false;
Hideable = true;
Custom = false;
Filterable = true;
Sortable = true;
MiniAvailable = true;
//Set openable object type to no type which is the default and means it's not a link to another object
AyObjectType = (int)AyaType.NoType;
}
}
}//ens