154 lines
7.4 KiB
C#
154 lines
7.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using AyaNova.Biz;
|
|
using Newtonsoft.Json.Linq;
|
|
using AyaNova.Api.ControllerHelpers;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using AyaNova.Models;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using EnumsNET;
|
|
|
|
|
|
|
|
namespace AyaNova.PickList
|
|
{
|
|
internal static class PickListFetcher
|
|
{
|
|
|
|
|
|
internal static async Task<List<NameIdActiveItem>> GetResponseAsync(IAyaPickList PickList, string autoCompleteQuery, string tagSpecificQuery, bool includeInactive, AyContext ct)
|
|
{
|
|
|
|
|
|
|
|
|
|
//Template
|
|
string Template = null;
|
|
//Attempt to fetch custom template
|
|
var t = await ct.PickListTemplate.FirstOrDefaultAsync(m => m.Id == ((long)PickList.DefaultListObjectType));
|
|
if (t == null)
|
|
{
|
|
Template = PickList.DefaultTemplate;
|
|
}
|
|
else
|
|
{
|
|
Template = t.Template;
|
|
}
|
|
|
|
//parse the template
|
|
var jTemplate = JArray.Parse(Template);
|
|
|
|
|
|
//Get the field key names in a list from the template
|
|
List<string> TemplateColumnNames = PickList.GetFieldListFromTemplate(jTemplate);
|
|
|
|
//BUILD THE QUERY
|
|
|
|
var q = PickListSqlBuilder.Build(PickList, TemplateColumnNames, autoCompleteQuery, tagSpecificQuery, includeInactive);
|
|
|
|
|
|
|
|
//RETURN OBJECTS
|
|
var ret = new List<NameIdActiveItem>();
|
|
|
|
|
|
//QUERY THE DB
|
|
using (var command = ct.Database.GetDbConnection().CreateCommand())
|
|
{
|
|
await ct.Database.OpenConnectionAsync();
|
|
|
|
//GET DATA RETURN ROWS
|
|
command.CommandText = q;
|
|
using (var dr = await command.ExecuteReaderAsync())
|
|
{
|
|
while (dr.Read())
|
|
{
|
|
//query is always in the same order:
|
|
//plId, plActive, plName
|
|
ret.Add(new NameIdActiveItem
|
|
{
|
|
Id = dr.GetInt64(0),
|
|
Active = dr.GetBoolean(1),
|
|
Name = dr.GetString(2)
|
|
});
|
|
|
|
// List<AyaFieldData> row = new List<AyaFieldData>(returnRowColumnCount);
|
|
|
|
|
|
// //INSERT REMAINING FIELDS FROM TEMPLATE INTO THE RETURN ROWS LIST
|
|
// foreach (string TemplateField in ListViewFieldList)
|
|
// {
|
|
|
|
// //get the AyaObjectFieldDefinition
|
|
// AyaPickListFieldDefinition f = PickList.FieldDefinitions.FirstOrDefault(x => x.FieldKey == TemplateField);
|
|
// if (f.IsCustomField)
|
|
// {
|
|
|
|
// AyaFieldData AyaField = new AyaFieldData();
|
|
// var cust = dr.GetString(SelectBuild.map[f.GetSqlValueColumnName()]);
|
|
// if (!string.IsNullOrWhiteSpace(cust))
|
|
// {
|
|
// JObject j = JObject.Parse(cust);
|
|
// //convert field name to cust name then get value
|
|
// var InternalCustomFieldName = AyaFormFieldDefinitions.TranslateLTCustomFieldToInternalCustomFieldName(TemplateField);
|
|
// //Sometimes a custom field is specified but doesn't exist in the collection so don't assume it's there
|
|
// // AyaField.v = j[InternalCustomFieldName].Value<object>();
|
|
// JToken o = j[InternalCustomFieldName];
|
|
// if (o != null)
|
|
// AyaField.v = o.Value<object>();
|
|
// else
|
|
// AyaField.v = null;
|
|
|
|
// row.Add(AyaField);
|
|
// }
|
|
|
|
// /*
|
|
// TODO: Custom field handling
|
|
// GetName works just not with multipart identifiers
|
|
// I could force naming by making all fields and AS, or
|
|
// I could map the ordinal when generating the Select fields so that I have a map to refer to here
|
|
// mapping in advance actually makes a lot of sense, then no more of this fragility of going by pointer index and hoping for the best
|
|
// it would just be premapped out.
|
|
|
|
// dr.GetOrdinal(f.SqlValueColumnName)
|
|
// 'dr.GetOrdinal(f.SqlValueColumnName)' threw an exception of type 'System.IndexOutOfRangeException'
|
|
// f.SqlValueColumnName
|
|
// "awidget.customfields"
|
|
// dr.GetName(nCurrentColumnPointer)
|
|
// "customfields"
|
|
// dr.GetOrdinal("customfields");
|
|
// 5
|
|
|
|
// */
|
|
// }
|
|
// else
|
|
// {
|
|
// AyaFieldData AyaField = new AyaFieldData();
|
|
// AyaField.v = dr.GetValue(SelectBuild.map[f.GetSqlValueColumnName()]);
|
|
|
|
|
|
// if (f.SqlIdColumnName != null)
|
|
// {
|
|
// var ordinal = SelectBuild.map[f.SqlIdColumnName];
|
|
// if (!await dr.IsDBNullAsync(ordinal))
|
|
// AyaField.i = dr.GetInt64(ordinal);
|
|
|
|
// }
|
|
// row.Add(AyaField);
|
|
// }
|
|
// }
|
|
// rows.Add(row);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
}//eoc
|
|
}//eons |