public ServiceRelationships GetDefinition(ServiceKey key)
        {
            if (ApplicableRule.Evaluate(key).Result)
            {
                var r = new ServiceRelationships
                {
                    Name        = Name,
                    Services    = new List <long>(),
                    Minimum     = GetRuletValue(Minimum, key),
                    Maximum     = GetRuletValue(Maximum, key),
                    ReverseName = ReverseName,
                    FilterRule  = FilterRule
                };
                r.Services.AddRange(Services);

                return(r);
            }
            return(null);
        }
Пример #2
0
        public ValidateServiceResponse Validate(ServiceKey key)
        {
            var response = new ValidateServiceResponse(true, key)
            {
                Errors = new List <IValidationError>()
            };
            var vr = new ValidateResponse(true, key)
            {
                Errors = new List <IValidationError>()
            };

            // let's make sure all required attributes are there
            vr.Errors.AddRange(from ad in Attributes.Values
                               where !IsOptional(ad.Name, key)
                               //checking if the attribute is supposed to have defaults
                               where !HasDefault(ad.Name)
                               where !ad.Type.Equals(AttributeType.Complex)
                               let value = key.GetAttributeValue(ad.Name, SearchOptions.ALL_TRUE)
                                           where string.IsNullOrEmpty(value)
                                           select new ValidationServiceError(string.Format("{0}: {1} is required.", key.ServiceInstanceId, ad.Label), ValidationError.SERVICE, ad.Name, ValidationError.MISSING, ad.Label, null, key.ServiceInstanceId));

            // if attributes are missing, let's stop there
            if (vr.Errors.Count == 0)
            {
                // valid value check
                var validValueRules = ValidValueRuleParser.GetRules(ValidValueRule);

                foreach (IValidValueRule rule in validValueRules)
                {
                    var v = key.GetAttributeValue(rule.GetAttributeName(), SearchOptions.NO_DEFAULTS);
                    //checking if attribute is applicable and is required
                    if (IsConfigurableAttribute(rule.GetAttributeName(), key) && !IsOptional(rule.GetAttributeName(), key))
                    {
                        vr.AddResponse(rule.ValidateAttributes(key));
                    }
                }

                // DataConstraints
                foreach (var a in key.Values)
                {
                    vr.AddResponse(IsValid(a.Key, a.Value.Value, key));
                }


                // service relationships
                vr.AddResponse(ServiceRelationships.ValidateRelationships(key));


                // children
                vr.AddResponse(ServiceHierarchy.Validate(key));



                //Since we have all of the needed values, we can now make sure that it meets all of the business rules.
                var           attributes = ServiceAttributes.Get(key, false);
                AttributeInfo attributeInfo;
                foreach (var attributeName in Attributes.Keys)
                {
                    attributeInfo = attributes.FirstOrDefault(a => a.Name.Equals(attributeName));
                    if (attributeInfo != null)
                    {
                        if (attributeInfo.GetValue() == null && !IsOptional(attributeName, key) && !HasDefault(attributeName))
                        {
                            vr.AddError(attributeInfo.Label + " does not have a valid value.", ValidationError.ATTRIBUTE, key.GetIdentifier(null), ValidationError.INVALID_VALUE, attributeName);
                        }
                        //If the value returned by the GetAttributes doesn't match the one returned by the key, we are not valid.
                        if (attributeInfo.Type.Equals(AttributeType.List))
                        {
                            string value = null;
                            if (!string.IsNullOrEmpty(attributeInfo.GetValue()))
                            {
                                value = attributeInfo.GetValue();
                            }
                            else if (!string.IsNullOrEmpty(attributeInfo.DefaultValue))
                            {
                                value = attributeInfo.DefaultValue;
                            }

                            var keyValue = key.GetAttributeValue(attributeName, SearchOptions.ALL_TRUE);
                            if (value != null && keyValue != null && !value.Equals(keyValue))
                            {
                                vr.AddError(string.Format("{0} ({1}) does not have a valid value. Should be ({2}).",
                                                          attributeInfo.Label, keyValue, value), ValidationError.ATTRIBUTE, key.GetIdentifier(null), ValidationError.INVALID_VALUE, attributeName);
                            }
                        }
                    }
                    else if (!HasDefault(attributeName) && !IsOptional(attributeName, key))
                    {
                        vr.AddError(
                            string.Format("{0} is required, does not have a default value, and '{1}' is not returned by GetAttributes.",
                                          attributeInfo.Label, key.GetAttributeValue(attributeName, SearchOptions.ALL_TRUE)),
                            ValidationError.ATTRIBUTE, key.GetIdentifier(null), ValidationError.INVALID_VALUE, attributeName);
                    }
                }
            }
            response.AddResponse(response.ToServiceResponse(FixAttributeNameToLabel(key, vr)));

            if (response.Errors.Count > 0)
            {
                response.IsValid = false;
                List <ValidationServiceError> vErrors = new List <ValidationServiceError>();

                foreach (var error in response.Errors)
                {
                    if (error is ValidationError)
                    {
                        vErrors.Add(new ValidationServiceError(error.Description, error.Category,
                                                               error.Entity, error.Type, error.Reason, error.ErrorCode, key.ServiceInstanceId));
                    }
                    else
                    {
                        if ((error as ValidationServiceError)?.InstanceId == null || (error as ValidationServiceError)?.InstanceId == 0)
                        {
                            vErrors.Add(new ValidationServiceError(error.Description, error.Category,
                                                                   error.Entity, error.Type, error.Reason, error.ErrorCode, key.ServiceInstanceId));
                        }
                        else
                        {
                            vErrors.Add(error as ValidationServiceError);
                        }
                    }
                }
                if (vErrors.Count > 0)
                {
                    response.Errors = null;
                    response.AddErrors(new List <ValidationServiceError>(vErrors));
                }
            }

            return(response);
        }