-
Notifications
You must be signed in to change notification settings - Fork 804
Lookups RowLookupScript
Victor Tomaili edited this page May 3, 2021
·
1 revision
This is default base class for all lookup classed. As you see this get id from IIdRow and description from INameRow.
[TBD]
namespace Serenity.Web
{
public class RowLookupScript<TRow> : LookupScript
where TRow: Row, new()
{
protected virtual void ApplyOrder(SqlQuery query)
{
var row = (Row)((query as ISqlQueryExtensible).FirstIntoRow);
if (row is INameRow)
query.OrderBy(((INameRow)row).NameField);
else if (row is IIdRow)
query.OrderBy((Field)((IIdRow)row).IdField);
}
protected virtual void PrepareQuery(SqlQuery query)
{
var row = (Row)((query as ISqlQueryExtensible).FirstIntoRow);
if (row is IIdRow)
query.Select((Field)((IIdRow)row).IdField);
if (row is INameRow)
query.Select(((INameRow)row).NameField);
var list = new List<object>();
foreach (var f in row.GetFields().Where(x =>
x.CustomAttributes != null &&
x.CustomAttributes.OfType<LookupIncludeAttribute>().Any()))
{
query.Select(f);
}
}
public RowLookupScript()
: base()
{
var row = new TRow();
Field field;
var idRow = row as IIdRow;
if (idRow != null)
{
field = ((Field)idRow.IdField);
this.IdField = field.PropertyName ?? field.Name;
}
var nameRow = row as INameRow;
if (nameRow != null)
{
field = ((Field)nameRow.NameField);
this.TextField = field.PropertyName ?? field.Name;
}
var treeRow = row as IParentIdRow;
if (treeRow != null)
{
field = ((Field)treeRow.ParentIdField);
this.ParentIdField = field.PropertyName ?? field.Name;
}
var readPermission = typeof(TRow).GetCustomAttribute<ReadPermissionAttribute>(true);
if (readPermission != null)
this.Permission = readPermission.Permission ?? "?";
this.GroupKey = row.GetFields().GenerationKey;
this.getItems = GetItems;
}
protected virtual List<TRow> GetItems()
{
var list = new List<TRow>();
var loader = new TRow();
var query = new SqlQuery()
.From(loader);
PrepareQuery(query);
ApplyOrder(query);
using (var connection = SqlConnections.NewByKey(RowRegistry.GetConnectionKey(loader)))
{
query.ForEach(connection, delegate()
{
list.Add(loader.Clone());
});
}
return list;
}
}
}
Copyright © Serenity Platform 2017-present. All rights reserved.
Documentation | Serene Template | Live Demo | Premium Support | Issues | Discussions