Example #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            int id;

            return(int.TryParse(Convert.ToString(reader.Value), out id)
                                ? ConditionID.Parse(id)
                                : null);
        }
Example #2
0
 private static void RemoveChild(IEnumerable <Condition> conditions, ConditionID id)
 {
     foreach (var parent in conditions.OfType <IParentCondition>())
     {
         parent.RemoveChild(id);
         RemoveChild(parent.Children, id);
     }
 }
Example #3
0
        public void Remove(ConditionID conditionID)
        {
            if (HasCondition(conditionID) == false)
            {
                throw new ConditionNotFoundException(conditionID);
            }

            RemoveChild(conditionID);
        }
Example #4
0
        private void RemoveChild(ConditionID childID)
        {
            var removed = _conditions.RemoveAll(c => c.ID == childID);

            if (removed > 0)
            {
                return;
            }

            RemoveChild(_conditions, childID);
        }
Example #5
0
        public Condition CreateCondition(ConditionID conditionID, Dictionary <string, object> conditionProperties)
        {
            conditionProperties = new Dictionary <string, object>(conditionProperties, StringComparer.OrdinalIgnoreCase);

            if (conditionProperties.TryGetValue(TypeKey, out var type) == false)
            {
                throw new ConditionException("The Type was not specified");
            }

            if (Conditions.Value.TryGetValue(Convert.ToString(type), out var create) == false)
            {
                throw new ConditionException($"Unknown condition type '{type}'");
            }

            var condition = create(conditionProperties);

            condition.ID = conditionID;

            return(condition);
        }
Example #6
0
        private static Condition FindCondition(IEnumerable <Condition> conditions, ConditionID id)
        {
            foreach (var condition in conditions)
            {
                if (condition.ID == id)
                {
                    return(condition);
                }

                if (condition is IParentCondition multi)
                {
                    var found = FindCondition(multi.Children, id);

                    if (found != null)
                    {
                        return(found);
                    }
                }
            }

            return(null);
        }
Example #7
0
        public void Add(Condition child, ConditionID parentConditionID = null)
        {
            if (parentConditionID == null)
            {
                _conditions.Add(child);
                return;
            }

            var condition = FindCondition(parentConditionID);

            if (condition == null)
            {
                throw new ConditionNotFoundException(parentConditionID);
            }

            var parent = condition as IParentCondition;

            if (parent is null)
            {
                throw new ConditionException($"{condition.GetType().Name} does not support children.");
            }

            parent.AddChild(child);
        }
Example #8
0
 protected override object Parse(string value) => ConditionID.Parse(Convert.ToInt32(value));
 public ConditionNotFoundException(ConditionID conditionID)
     : base($"Unable to find a condition with the ID '{conditionID}'")
 {
 }
Example #10
0
 public Condition FindCondition(ConditionID id) => FindCondition(_conditions, id);
Example #11
0
 public bool HasCondition(ConditionID id) => FindCondition(_conditions, id) != null;
Example #12
0
        public bool CanAdd(Condition child, ConditionID parentConditionID)
        {
            var condition = FindCondition(parentConditionID);

            return(condition is IParentCondition parent && parent.CanAdd(child));
        }