using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; namespace PRS.Mobile { public class FormPickerQueryLoader : IFormPickerQueryLoader where TEntity : Entity, IRemotable, IPersistent, new() where TEntityLink : EntityLink, new() where TInstance : Entity, IRemotable, IPersistent, IDigitalFormInstance, new() { Filter incompleteFilter = new Filter(x => x.FormCompletedBy.ID).IsEqualTo(ClientFactory.UserGuid) .And(x => x.FormCompleted).IsEqualTo(DateTime.MinValue); Filter completeFilter = new Filter(x => x.FormCompletedBy.ID).IsEqualTo(ClientFactory.UserGuid) .And(x => x.FormCompleted).IsNotEqualTo(DateTime.MinValue); public List QueryIncomplete() { return DoQuery(incompleteFilter); } public List QueryComplete() { return DoQuery(completeFilter); } private List DoQuery(Filter filter) { List shells = new List(); CoreTable table = QueryTable(filter); while(table == null) table = QueryTable(filter); if (table.Rows.Any()) { foreach (CoreRow row in table.Rows) { shells.Add(CreateShell(row)); } //OrderShells(shells, filter); } return shells; } private CoreTable QueryTable(Filter filter) { try { return new Client().Query ( filter, new Columns ( x => x.ID, x => x.Parent.ID, x => x.Form.Description, x => x.FormStarted, x => x.FormCompleted, x => x.Form.ID )); } catch (Exception ex) { InABox.Mobile.MobileLogging.Log(ex); return null; } } //private List OrderShells(List shells, Filter filter) //{ // if (filter == completeFilter) // shells.Sort((x, y) => y.DateCompleted.CompareTo(x.DateCompleted)); //descending // else if (filter == incompleteFilter) // shells.Sort((x, y) => x.DateStarted.CompareTo(y.DateStarted)); // return shells; //} private ExistingFormShell CreateShell(CoreRow row) { var instance = row.ToObject(); ExistingFormShell shell = new ExistingFormShell(); shell.ID = instance.ID; shell.Description = instance.Form.Description; if (shell.Description == null) shell.Description = ""; shell.ParentID = instance.Parent.ID; shell.DateStarted = instance.FormStarted; shell.DateCompleted = instance.FormCompleted; shell.Started = "Started: " + shell.DateStarted.ToString("dd MMM yy hh:mm tt"); if (shell.DateCompleted != DateTime.MinValue) shell.Completed = "Completed: " + shell.DateCompleted.ToString("dd MMM yy hh:mm tt"); shell.Type = typeof(TInstance); shell.FormID = instance.Form.ID; return shell; } } }