Files
raven/server/AyaNova/biz/AyaObjectFieldDefinitions.cs
2020-01-20 23:28:47 +00:00

409 lines
22 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
//SQL code uses this:
// - a source of database column names and ID column names
// - Essentially this replaces all the attribute decoration in v7 and the individual classes with objects
public static class AyaObjectFieldDefinitions
{
//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 and ends in _LIST
public const string WIDGET_KEY = "widget";
public const string USER_KEY = "user";
public const string TEST_WIDGET_USER_EMAIL_ADDRESS_LIST_KEY = "TEST_WIDGET_USER_EMAIL_ADDRESS_LIST";//for development testing, not a real thing going forward
public static List<string> AyaObjectFieldDefinitionKeys
{
get
{
List<string> l = new List<string>{
WIDGET_KEY, USER_KEY
};
return l;
}
}
public static bool IsValidObjectKey(string key)
{
return AyaObjectFieldDefinitionKeys.Contains(key);
}
public static List<AyaObjectFieldDefinition> AyaObjectFields(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<AyaObjectFieldDefinition> l = new List<AyaObjectFieldDefinition>();
switch (key)
{
/*
TODO: going to need some SQL hints added here
- SqlIDColumn indicating what the ID column is to fetch the underlying value from
- SqlColumn indicating an alternative column name as known to the sql server if it varies from the propertyname (in lowercase)
Also: going to need a definition of the table name and the default identity column
Maybe decorate with an INTERNAL ONLY bool so that we see it at the server but not at the client or when fetched as possible fields
hypothetical to help develop this:
Client and head office list:
{key=ClientName, propertyname=Name, idcolumn=id,}
In v7 this is what the attribute decorations looked like:
[SqlColumnNameAttribute("aClient.aName", "aClient.aID")]
public GridNameValueCellItem LT_O_Client
{
get
{
return mClient;
}
}
[SqlColumnNameAttribute("aHeadOffice.aName","aHeadOffice.aID")]
public GridNameValueCellItem LT_O_HeadOffice
{
get
{
return mHeadOffice;
}
}
And the query (from clientlistdetailed)
string q =
"SELECT ~MAXRECS~ ACLIENT.aID, ACLIENT.aName, ACLIENT.aModified, " +
" ACLIENT.AACTIVE, ACLIENT.aUsesBanking, " +
" ACLIENT.aBillHeadOffice, ACLIENT.aWebAddress, " +
" ACLIENT.aDispatchZoneID, ACLIENT.aClientGroupID, " +
" ACLIENT.aLastWorkorderID, " +
" ACLIENT.aLastServiceDate, (SELECT TOP 1 aSBANK.aHoursBalance " +
"FROM aServiceBank aSBANK WHERE aSBANK.AAPPLIESTOROOTOBJECTID " +
"= ACLIENT.aID ORDER " +
"BY aSBANK.aCreated DESC) AS HOURSBAL, (SELECT TOP " +
"1 aSBANK.aIncidentsBalance FROM aServiceBank aSBANK " +
"WHERE aSBANK.AAPPLIESTOROOTOBJECTID = ACLIENT.aID " +
"ORDER BY aSBANK.aCreated DESC) AS INCIDENTSBAL, " +
" (SELECT TOP 1 aSBANK.aCurrencyBalance FROM " +
"aServiceBank aSBANK WHERE aSBANK.AAPPLIESTOROOTOBJECTID " +
"= ACLIENT.aID ORDER BY aSBANK.aCreated DESC) " +
"AS CURRENCYBAL, " +
" ACLIENT.AACCOUNTNUMBER, aHeadOffice.aName " +
"AS aHeadOfficeName, aHeadOffice.aID " +
"AS aHeadOfficeID, aDispatchZone.aName AS aDispatchZoneName, " +
" ACLIENT.aRegionID, aRegion.aName " +
"AS aRegionName, aClientGroup.aName AS " +
"aClientGroupName, aPHYSICAL.aDeliveryAddress, " +
"aPHYSICAL.aCity, aPHYSICAL.aStateProv, aPHYSICAL.aCountry, " +
" aPHYSICAL.aPostal, aPHYSICAL.AADDRESSTYPE, " +
" aPHYSICAL.aLongitude, aPHYSICAL.aLatitude, " +
" aWorkorderService.aServiceNumber, " +
" ACLIENT.aContractID, aContract.aName AS " +
"aContractName, ACLIENT.aContractExpires, aPHYSICAL.aCountryCode, " +
" aPOSTAL.AADDRESSTYPE AS aPAddressType, " +
" aPOSTAL.aDeliveryAddress AS aPDeliveryAddress, " +
" aPOSTAL.aCity AS aPCity, aPOSTAL.aStateProv " +
"AS aPStateProv, aPOSTAL.aCountryCode AS aPCountryCode, " +
" aPOSTAL.aCountry AS aPCountry, aPOSTAL.aPostal " +
"AS aPPostal, ACLIENT.aCustom1, ACLIENT.aCustom2, " +
" ACLIENT.aCustom3, ACLIENT.aCustom4, " +
" ACLIENT.aCustom5, ACLIENT.aCustom6, " +
" ACLIENT.aCustom7, ACLIENT.aCustom8, ACLIENT.aCustom9, " +
" ACLIENT.aCustom0, ACLIENT.aTechNotes, " +
" ACLIENT.aNotes, ACLIENT.aPopUpNotes, " +
" ACLIENT.ACONTACT, ACLIENT.AEMAIL, ACLIENT.APHONE1, ACLIENT.APHONE2, ACLIENT.APHONE3, ACLIENT.APHONE4, ACLIENT.APHONE5, ACLIENT.ACONTACTNOTES " +
"FROM " +
" ACLIENT ACLIENT " +
" LEFT OUTER JOIN ADISPATCHZONE ON (ACLIENT.ADISPATCHZONEID=ADISPATCHZONE.AID) " +
" LEFT OUTER JOIN ACLIENTGROUP ON (ACLIENT.ACLIENTGROUPID=ACLIENTGROUP.AID) " +
" LEFT OUTER JOIN AADDRESS aPHYSICAL ON (ACLIENT.AID=aPHYSICAL.AROOTOBJECTID) " +
" LEFT OUTER JOIN AADDRESS aPOSTAL ON (ACLIENT.AID=aPOSTAL.AROOTOBJECTID) " +
" LEFT OUTER JOIN ACONTRACT ON (ACLIENT.ACONTRACTID=ACONTRACT.AID) " +
" LEFT OUTER JOIN AHEADOFFICE ON (ACLIENT.AHEADOFFICEID=AHEADOFFICE.AID) " +
" LEFT OUTER JOIN AWORKORDERSERVICE ON (ACLIENT.ALASTWORKORDERID=AWORKORDERSERVICE.AWORKORDERID) " +
" LEFT OUTER JOIN AREGION ON (ACLIENT.AREGIONID=AREGION.AID) " +
"WHERE (aPHYSICAL.AADDRESSTYPE " +
"IS NULL OR aPHYSICAL.AADDRESSTYPE " +
"= 2) AND (aPOSTAL.AADDRESSTYPE IS NULL OR aPOSTAL.AADDRESSTYPE " +
"= 1) \r\n ";
*/
case WIDGET_KEY:
#region WIDGET_KEY
//first column is the non visible Default Id column so that the client knows what to open when there is no field with ID selected that matches underlying record type
//in this case the default of id is sufficient for this list
l.Add(new AyaObjectFieldDefinition { LtKey = "df", AyaObjectType = (int)AyaType.Widget });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetName", FieldKey = "Name", UiFieldDataType = (int)AyaUiFieldDataType.Text, Hideable = false });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetSerial", FieldKey = "Serial", UiFieldDataType = (int)AyaUiFieldDataType.Integer });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetDollarAmount", FieldKey = "DollarAmount", UiFieldDataType = (int)AyaUiFieldDataType.Currency });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCount", FieldKey = "Count", UiFieldDataType = (int)AyaUiFieldDataType.Integer });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetRoles", FieldKey = "Roles", UiFieldDataType = (int)AyaUiFieldDataType.Enum, EnumType = typeof(AuthorizationRoles).ToString() });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetStartDate", FieldKey = "StartDate", UiFieldDataType = (int)AyaUiFieldDataType.DateTime });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetEndDate", FieldKey = "EndDate", UiFieldDataType = (int)AyaUiFieldDataType.DateTime });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetNotes", FieldKey = "Notes", UiFieldDataType = (int)AyaUiFieldDataType.Text });
//More to do on this, maybe the datatype should be a LINK or something for UI purposes
//circle back on this when there is enough infrastructure to test
l.Add(new AyaObjectFieldDefinition { LtKey = "User", FieldKey = "userid", UiFieldDataType = (int)AyaUiFieldDataType.Text, AyaObjectType = (int)AyaType.User });
l.Add(new AyaObjectFieldDefinition { LtKey = "Active", FieldKey = "Active", UiFieldDataType = (int)AyaUiFieldDataType.Bool, Hideable = false });
l.Add(new AyaObjectFieldDefinition { LtKey = "Tags", FieldKey = "Tags", UiFieldDataType = (int)AyaUiFieldDataType.Tags });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom1", FieldKey = "WidgetCustom1", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom2", FieldKey = "WidgetCustom2", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom3", FieldKey = "WidgetCustom3", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom4", FieldKey = "WidgetCustom4", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom5", FieldKey = "WidgetCustom5", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom6", FieldKey = "WidgetCustom6", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom7", FieldKey = "WidgetCustom7", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom8", FieldKey = "WidgetCustom8", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom9", FieldKey = "WidgetCustom9", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom10", FieldKey = "WidgetCustom10", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom11", FieldKey = "WidgetCustom11", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom12", FieldKey = "WidgetCustom12", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom13", FieldKey = "WidgetCustom13", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom14", FieldKey = "WidgetCustom14", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom15", FieldKey = "WidgetCustom15", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "WidgetCustom16", FieldKey = "WidgetCustom16", IsCustomField = true });
break;
#endregion
case USER_KEY:
#region USER_KEY
l.Add(new AyaObjectFieldDefinition { LtKey = "df", AyaObjectType = (int)AyaType.User });
l.Add(new AyaObjectFieldDefinition { LtKey = "Name", FieldKey = "Name", UiFieldDataType = (int)AyaUiFieldDataType.Text, Hideable = false });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserEmployeeNumber", FieldKey = "EmployeeNumber", UiFieldDataType = (int)AyaUiFieldDataType.Text });
l.Add(new AyaObjectFieldDefinition { LtKey = "AuthorizationRoles", FieldKey = "Roles", Hideable = false, UiFieldDataType = (int)AyaUiFieldDataType.Enum, EnumType = typeof(AuthorizationRoles).ToString() });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserNotes", FieldKey = "Notes", UiFieldDataType = (int)AyaUiFieldDataType.Text });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserUserType", FieldKey = "UserType", Hideable = false, UiFieldDataType = (int)AyaUiFieldDataType.Enum, EnumType = typeof(UserType).ToString() });
l.Add(new AyaObjectFieldDefinition { LtKey = "Active", FieldKey = "Active", UiFieldDataType = (int)AyaUiFieldDataType.Bool, Hideable = false });
l.Add(new AyaObjectFieldDefinition { LtKey = "Tags", FieldKey = "Tags", UiFieldDataType = (int)AyaUiFieldDataType.Tags });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom1", FieldKey = "UserCustom1", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom2", FieldKey = "UserCustom2", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom3", FieldKey = "UserCustom3", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom4", FieldKey = "UserCustom4", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom5", FieldKey = "UserCustom5", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom6", FieldKey = "UserCustom6", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom7", FieldKey = "UserCustom7", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom8", FieldKey = "UserCustom8", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom9", FieldKey = "UserCustom9", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom10", FieldKey = "UserCustom10", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom11", FieldKey = "UserCustom11", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom12", FieldKey = "UserCustom12", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom13", FieldKey = "UserCustom13", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom14", FieldKey = "UserCustom14", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom15", FieldKey = "UserCustom15", IsCustomField = true });
l.Add(new AyaObjectFieldDefinition { LtKey = "UserCustom16", FieldKey = "UserCustom16", IsCustomField = true });
break;
#endregion
case TEST_WIDGET_USER_EMAIL_ADDRESS_LIST_KEY:
#region TEST_WIDGET_USER_EMAIL_ADDRESS_LIST_KEY
l.Add(new AyaObjectFieldDefinition { FieldKey = "df", AyaObjectType = (int)AyaType.Widget, SqlIdColumnName = "awidget.id" });
l.Add(new AyaObjectFieldDefinition
{
FieldKey = "widgetname",
LtKey = "WidgetName",
UiFieldDataType = (int)AyaUiFieldDataType.Text,
AyaObjectType = (int)AyaType.Widget,
SqlIdColumnName = "awidget.id",
SqlValueColumnName = "awidget.name"
});
l.Add(new AyaObjectFieldDefinition
{
FieldKey = "username",
LtKey = "User",
UiFieldDataType = (int)AyaUiFieldDataType.Text,
AyaObjectType = (int)AyaType.User,
SqlIdColumnName = "auser.id",
SqlValueColumnName = "auser.name"
});
l.Add(new AyaObjectFieldDefinition
{
LtKey = "UserEmailAddress",
FieldKey = "emailaddress",
SqlValueColumnName = "auseroptions.emailaddress",
UiFieldDataType = (int)AyaUiFieldDataType.EmailAddress
});
l.Add(new AyaObjectFieldDefinition
{
LtKey = "Active",
FieldKey = "widgetactive",
SqlValueColumnName = "awidget.active",
UiFieldDataType = (int)AyaUiFieldDataType.Bool
});
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)AyaUiFieldDataType.Text},\"ay\":{(int)defaultLinkType}}}]";
}
//Accept a json template
//return a column list suitable for api list return
public static string GenerateListColumnsJSONFromTemplate(AyaType defaultLinkType, List<AyaObjectFieldDefinition> objectFields, 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)defaultLinkType}}}");
foreach (string s in fullFields)
{
AyaObjectFieldDefinition o = objectFields.FirstOrDefault(x => x.FieldKey == s);
#if (DEBUG)
//Developers little helper
if (o == null)
{
throw new System.ArgumentNullException($"DEV ERROR in objectFields::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 ObjectFields
public class AyaObjectFieldDefinition
{
//CLIENT / SERVER Unique identifier used at BOTH client and server
//also the sql displaycolumnname if identical
public string FieldKey { get; set; }
//CLIENT Use only for display
public string LtKey { get; set; }
//CLIENT form customization
public bool Hideable { get; set; }
//CLIENT / SERVER - client display server validation purposes
public bool IsCustomField { get; set; }
//CLIENT / SERVER - client display server validation purposes
public bool IsFilterable { get; set; }
//CLIENT / SERVER - client display server validation purposes
public bool IsSortable { get; set; }
//CLIENT Use only for display
public int UiFieldDataType { get; set; }
//CLIENT Use only for display
public string EnumType { get; set; }
//CLIENT / SERVER - client display and to indicate what object to open , Server for formatting return object
public int AyaObjectType { get; set; }
//SERVER - for building sql queries
public string SqlIdColumnName { get; set; }
public string SqlValueColumnName { get; set; }
public AyaObjectFieldDefinition()
{
//most common defaults
Hideable = true;
IsCustomField = false;
IsFilterable = true;
IsSortable = true;
//Set openable object type to no type which is the default and means it's not a link to another object
AyaObjectType = (int)AyaType.NoType;
}
//Get column to query for display name or use FieldName if there is no difference
public string GetSqlValueColumnName()
{
if (string.IsNullOrEmpty(SqlValueColumnName))
{
return FieldKey.ToLowerInvariant();
}
else
{
return SqlValueColumnName;
}
}
}
}//ens