Пример #1
0
        public static ICollection <AttributeInfo> Get(ServiceKey key, bool populateLists)
        {
            var config = ServiceConfiguration.Get(key.Id, key.EffectiveDate);

            var list = ValidValueRuleParser.GetRules(config.ValidValueRule);

            var set = new ValidValueRuleSet();

            set.AddRules(list);

            var attributeSet = set.GetAttributes(key, SearchOptions.ALL_FALSE);
            //If we have an attribute with  no valid options, clear the value and try again...
            var emptyAttributes = (from a in attributeSet where a.Values == null || a.Values.Count == 0 select a.Name).ToList();

            if (emptyAttributes.Count > 0)
            {
                foreach (var a in emptyAttributes)
                {
                    key.RemoveAttribute(a);
                }
                attributeSet = set.GetAttributes(key, SearchOptions.ALL_FALSE);
            }

            IDictionary <string, AttributeInfo> tempList = new Dictionary <string, AttributeInfo>();

            foreach (var a in attributeSet)
            {
                tempList[a.Name] = a;
            }

            key.AddMissingAttributes();
            //Next we need to look if there are non-list items that need to be collected.
            foreach (var pair in config.Attributes)
            {
                AttributeInfo a = null;
                if (tempList.ContainsKey(pair.Key))
                {
                    tempList.TryGetValue(pair.Key, out a);
                }

                tempList[pair.Key] = AttributeFactory.CreateAttribute(pair.Value, pair.Key, a, key);
                if ((pair.Value.Type == AttributeType.Parent || pair.Value.Type == AttributeType.Related) &&
                    key.HasAttribute(pair.Key))
                {
                    tempList[pair.Key].SetValue(key.GetAttributeValue(pair.Key, SearchOptions.ALL_TRUE));
                }
            }


            var ruleSet = new ServiceRuleSet();

            ruleSet.AddDefaults(key);  // add defaults so rules such as IsApplicable can use them

            //key doesn't have all the data we have generated so to use the latest we will build a ValueHolder that has what we need...
            string aValue;

            foreach (var a in tempList.Values)
            {
                aValue = a.GetValue();
                if (!string.IsNullOrEmpty(aValue)) // don't add empty values
                {
                    ruleSet.AddValue(a.Name, new RuleValue(aValue));
                }
            }
            //Determine which attributes we don't need
            var finalList = tempList.Values.Where(a => config.IsConfigurableAttribute(a.Name, ruleSet)).ToDictionary(a => a.Name);


            //Last we need to try and add a description to attributes that haven't had them added yet...
            //and flag them as optional or not.  We will also set the default value if there is one.
            foreach (var a in finalList.Values)
            {
                a.Optional = config.IsOptional(a.Name, ruleSet).ToString();

                a.Label = config.GetLabel(a.Name);
                if (config.HasDefault(a.Name))
                {
                    try
                    {
                        a.DefaultValue = config.GetDefaultValue(a.Name, key);
                    }
                    catch (Exception) { }
                }
                a.Hidden              = config.IsHidden(a.Name, key);
                a.MaxRepeats          = config.GetMaxRepeats(a.Name);
                a.RequiresRefresh     = config.GetRequiresRefresh(a.Name);
                a.ReadOnly            = config.IsReadOnly(a.Name, key);
                a.ApplicableForChange = config.GetApplicableForChange(a.Name);
                a.AffectsChildren     = config.AffectsChildren(a.Name);
                a.DesignImpact        = config.IsDesignImpact(a.Name, key);
                a.ProvisioningImpact  = config.IsProvisioningImpact(a.Name, key);

                var attribute = a as ListAttribute;
                if (attribute != null)
                {
                    var la = attribute;
                    if (populateLists && la.GetValue() != null && !la.ReadOnly && !la.Hidden)
                    {
                        //Since the value has been set, the list of options is empty.  If it is asked for, we will determine
                        //the list of options if this was not set.
                        var myKey = key.Clone(false);

                        myKey.AddValue(la.Name, null);
                        var myAtts = set.GetAttributes(myKey, SearchOptions.ALL_FALSE);

                        foreach (var av in
                                 from myAtt in myAtts
                                 where myAtt.Name.Equals(la.Name)
                                 from av in ((ListAttribute)myAtt).GetList()
                                 select av)
                        {
                            la.AddValue(av);
                        }
                    }
                }
            }

            return(config.SortList(finalList.Values));
        }
Пример #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);
        }