/// <summary> /// Processes the supplied <see cref="Rule"/> object and produces the <see cref="MyGeotabAPIAdapter.Database.Models.DbRule"/> and <see cref="DbCondition"/> objects contained within it. /// </summary> /// <param name="rule">A <see cref="Rule"/> object</param> /// <param name="entityStatus">The <see cref="Database.Common.DatabaseRecordStatus"/> to be applied to the <see cref="MyGeotabAPIAdapter.Database.Models.DbRule"/>.</param> /// <param name="recordLastChanged">The timestamp to be applied to the <see cref="MyGeotabAPIAdapter.Database.Models.DbRule"/>.</param> /// <param name="operationType">The <see cref="Database.Common.DatabaseWriteOperationType"/> to be applied to the <see cref="MyGeotabAPIAdapter.Database.Models.DbRule"/>.</param> /// <returns>A correctly formatted <see cref="DbRuleObject"/></returns> public void BuildRuleObject(Rule rule, int entityStatus, DateTime recordLastChanged, DatabaseWriteOperationType operationType) { MethodBase methodBase = MethodBase.GetCurrentMethod(); logger.Trace($"Begin {methodBase.ReflectedType.Name}.{methodBase.Name}"); DbRule dbRule = new() { GeotabId = rule.Id.ToString(), Name = rule.Name.ToString(), BaseType = rule.BaseType.ToString(), ActiveFrom = rule.ActiveFrom, ActiveTo = rule.ActiveTo, Comment = rule.Comment, Version = rule.Version, EntityStatus = entityStatus, RecordLastChangedUtc = recordLastChanged, DatabaseWriteOperationType = operationType }; if (rule.Condition == null) { logger.Debug($"Rule '{rule.Id}' has no conditions."); } else { ProcessConditionHierarchy(rule.Condition, entityStatus, recordLastChanged, operationType); } this.DbRule = dbRule; logger.Trace($"End {methodBase.ReflectedType.Name}.{methodBase.Name}"); }
/// <summary> /// Enumerates the root condition and its hierarchy of children to produce a list of DbCondtion objects that are added to the <see cref="DbCondition"/> list object of the class. /// </summary> /// <param name="condition">Root condition of a rule</param> /// <returns>asynchronous task</returns> void ProcessConditionHierarchy(Condition condition, int entityStatus, DateTime recordLastChanged, DatabaseWriteOperationType operationType) { MethodBase methodBase = MethodBase.GetCurrentMethod(); logger.Trace($"Begin {methodBase.ReflectedType.Name}.{methodBase.Name}"); // Add the root condition - executed asynchronously to speed things up AddCondition(condition, entityStatus, recordLastChanged, operationType); if (condition.Children == null) { // No children to process; exit. logger.Debug($"Condition Id: {condition.Id} has no children to process."); return; } Condition currentCondition; int countSiblings = condition.Children.Count; for (int i = 0; i < countSiblings; i++) { currentCondition = condition.Children[i]; logger.Debug(currentCondition.Id.ToString()); // Process any child conditions - executed asynchronously to speed things up ProcessConditionHierarchy(currentCondition, entityStatus, recordLastChanged, operationType); } logger.Trace($"End {methodBase.ReflectedType.Name}.{methodBase.Name}"); }
/// <summary> /// Converts the supplied list of <see cref="Rule"/> objects into a list of <see cref="DbRule"/> objects. /// </summary> /// <param name="rules">The list of <see cref="Rule"/> objects to be converted.</param> /// <param name="entityStatus">The <see cref="Database.Common.DatabaseRecordStatus"/> to be applied to the <see cref="DbRule"/> objects.</param> /// <param name="recordLastChanged">The timestamp to be applied to the <see cref="DbRule"/> objects.</param> /// <param name="operationType">The <see cref="Database.Common.DatabaseWriteOperationType"/> to be applied to the <see cref="DbRule"/> objects.</param> /// <returns></returns> public static List <DbRule> GetDbRules(IList <Rule> rules, int entityStatus, DateTime recordLastChanged, DatabaseWriteOperationType operationType) { var dbRules = new List <DbRule>(); foreach (var rule in rules) { DbRule dbRule = GetDbRule(rule, entityStatus, recordLastChanged, operationType); dbRules.Add(dbRule); } return(dbRules); }
/// <summary> /// Converts the supplied list of <see cref="Condition"/> objects into a list of <see cref="DbCondition"/> objects. /// </summary> /// <param name="conditions">The list of <see cref="Condition"/> objects to be converted.</param> /// <param name="entityStatus">The <see cref="Database.Common.DatabaseRecordStatus"/> to be applied to the <see cref="DbCondition"/> objects.</param> /// <param name="recordLastChanged">The timestamp to be applied to the <see cref="DbCondition"/> objects.</param> /// <param name="operationType">The <see cref="Database.Common.DatabaseWriteOperationType"/> to be applied to the <see cref="DbCondition"/> objects.</param> /// <returns></returns> public static List <DbCondition> GetDbConditions(IList <Condition> conditions, int entityStatus, DateTime recordLastChanged, DatabaseWriteOperationType operationType) { var dbConditions = new List <DbCondition>(); foreach (var condition in conditions) { DbCondition dbCondition = GetDbCondition(condition, "", entityStatus, recordLastChanged, operationType); dbConditions.Add(dbCondition); } return(dbConditions); }
/// <summary> /// Converts the supplied <see cref="Rule"/> into a <see cref="DbRule"/>. /// </summary> /// <param name="rule">The <see cref="Rule"/> to be converted.</param> /// <param name="entityStatus">The <see cref="Database.Common.DatabaseRecordStatus"/> to be applied to the <see cref="DbRule"/>.</param> /// <param name="recordLastChanged">The timestamp to be applied to the <see cref="DbRule"/>.</param> /// <param name="operationType">The <see cref="Database.Common.DatabaseWriteOperationType"/> to be applied to the <see cref="DbRule"/>.</param> /// <returns></returns> public static DbRule GetDbRule(Rule rule, int entityStatus, DateTime recordLastChanged, DatabaseWriteOperationType operationType) { DbRule dbRule = new DbRule { GeotabId = rule.Id.ToString(), Name = rule.Name.ToString(), BaseType = rule.BaseType.ToString(), ActiveFrom = rule.ActiveFrom, ActiveTo = rule.ActiveTo, Comment = rule.Comment, Version = rule.Version, EntityStatus = entityStatus, RecordLastChangedUtc = recordLastChanged, DatabaseWriteOperationType = operationType }; return(dbRule); }
/// <summary> /// Converts the supplied <see cref="Condition"/> into a <see cref="DbCondition"/>. /// </summary> /// <param name="condition">The <see cref="Condition"/> to be converted.</param> /// <param name="entityStatus">The <see cref="Database.Common.DatabaseRecordStatus"/> to be applied to the <see cref="DbCondition"/>.</param> /// <param name="recordLastChanged">The timestamp to be applied to the <see cref="DbCondition"/>.</param> /// <param name="operationType">The <see cref="Database.Common.DatabaseWriteOperationType"/> to be applied to the <see cref="DbCondition"/>.</param> /// <returns></returns> public static DbCondition GetDbCondition(Condition condition, string parentId, int entityStatus, DateTime recordLastChanged, DatabaseWriteOperationType operationType) { DbCondition dbCondition = new DbCondition { GeotabId = condition.Id.ToString(), ParentId = parentId }; if (condition.Rule != null) { dbCondition.RuleId = condition.Rule.Id.ToString(); } if (condition.ConditionType != null) { dbCondition.ConditionType = condition.ConditionType.ToString(); } if (condition.Device != null) { dbCondition.DeviceId = condition.Device.Id.ToString(); } if (condition.Diagnostic != null) { dbCondition.DiagnosticId = condition.Diagnostic.Id.ToString(); } if (condition.Driver != null) { dbCondition.DriverId = condition.Driver.Id.ToString(); } dbCondition.Value = condition.Value; if (condition.WorkTime != null) { dbCondition.WorkTimeId = condition.WorkTime.Id.ToString(); } if (condition.Zone != null) { dbCondition.ZoneId = condition.Zone.Id.ToString(); } dbCondition.EntityStatus = entityStatus; dbCondition.RecordLastChangedUtc = recordLastChanged; dbCondition.DatabaseWriteOperationType = operationType; return(dbCondition); }
/// <summary> /// Adds a single <see cref="Condition"/> object to the <see cref="DbCondition"/> list object of the class. /// </summary> /// <param name="condition">The <see cref="Condition"/> object to add</param> /// <param name="entityStatus">The <see cref="Database.Common.DatabaseRecordStatus"/> to be applied to the <see cref="MyGeotabAPIAdapter.Database.Models.DbCondition"/>.</param> /// <param name="recordLastChanged">The timestamp to be applied to the <see cref="MyGeotabAPIAdapter.Database.Models.DbCondition"/>.</param> /// <param name="operationType">The <see cref="Database.Common.DatabaseWriteOperationType"/> to be applied to the <see cref="MyGeotabAPIAdapter.Database.Models.DbCondition"/>.</param> /// <returns></returns> void AddCondition(Condition condition, int entityStatus, DateTime recordLastChanged, DatabaseWriteOperationType operationType) { MethodBase methodBase = MethodBase.GetCurrentMethod(); logger.Trace($"Begin {methodBase.ReflectedType.Name}.{methodBase.Name}"); string parentId = ""; if (condition.Parent != null) { parentId = condition.Parent.Id.ToString(); } logger.Debug($"parentId: {parentId}"); DbCondition dbCondition = ObjectMapper.GetDbCondition(condition, parentId, entityStatus, recordLastChanged, operationType); DbConditions.Add(dbCondition); logger.Trace($"End {methodBase.ReflectedType.Name}.{methodBase.Name}"); }
/// <summary> /// Builds the <see cref="DbRuleObject"/> and then inserts it into the database /// </summary> /// <param name="ruleList">List of Geotab <see cref="Rule"/> objects</param> /// <param name="entityStatus"></param> /// <param name="recordLastChanged"></param> /// <param name="operationType"></param> /// <param name="cancellationTokenSource">The <see cref="CancellationTokenSource"/>.</param> /// <returns>The count of successful inserts</returns> public async static Task <int> InsertDbRuleObjectsFromRuleListAsync(IList <Rule> ruleList, int entityStatus, DateTime recordLastChanged, DatabaseWriteOperationType operationType, CancellationTokenSource cancellationTokenSource) { CancellationToken cancellationToken = cancellationTokenSource.Token; int insertCount = 0; foreach (Rule rule in ruleList) { using (DbRuleObject dbRuleObject = new()) { dbRuleObject.BuildRuleObject(rule, entityStatus, recordLastChanged, operationType); if (await InsertDbRuleObjectAsync(dbRuleObject, cancellationTokenSource)) { insertCount++; } } cancellationToken.ThrowIfCancellationRequested(); } return(insertCount); }