Exemple #1
0
        public ValidateResponse FixAttributeNameToLabel(ServiceKey sk, ValidateResponse vr)
        {
            ValidateResponse resp = new ValidateResponse(true);
            var atts   = ServiceAttributes.Get(sk, false);
            var errors = vr.GetErrors();

            if (errors?.Count() > 0)
            {
                resp.IsValid = false;
                foreach (var err in errors)
                {
                    if (err.Description.Contains("[") && err.Description.Contains("]"))
                    {
                        var attName     = Regex.Match(err.Description, @"\[([^)]*)\]")?.Groups[1]?.Value;
                        var info        = atts.FirstOrDefault(x => x.Name == attName);
                        var description = err.Description.Replace("[" + attName + "]", info.Label);
                        resp.AddError(description, err.Category, err.Entity, err.Type, err.Reason, err.ErrorCode);
                    }
                    else
                    {
                        resp.AddError(err.Description, err.Category, err.Entity, err.Type, err.Reason, err.ErrorCode);
                    }
                }
            }
            return(resp);
        }
Exemple #2
0
        private static ServiceConfiguration GetFromDb(long id, DateTime effectiveDate)
        {
            ServiceConfiguration config;

            using (var connection = new OracleConnection(FscApplication.Current.Settings.FscConnectionString))
            {
                connection.Open();

                config = connection.QueryFirst <ServiceConfiguration>(
                    $"select SERVICE_ID Id, Version, FROM_EFF_DATE FromEffectiveDate, TO_EFF_DATE ToEffectiveDate," +
                    $"   DISPLAY_PATTERN DisplayPattern, SERVICE_LAYER_RULE ServiceLayerRule, ALLOW_EXISTING AllowExisting," +
                    $"   ALLOW_SHARED_STANDALONE AllowSharedStandalone, VALID_VALUE_RULE ValidValueRule" +
                    " from service_configuration where service_id = :id " +
                    "   and from_eff_date <= :effectiveDate " +
                    "   and (to_eff_date is null or to_eff_date > :effectiveDate) ", new { id, effectiveDate });
            }
            config.Attributes = ServiceAttributes.Get(id, effectiveDate);

            return(config);
        }
Exemple #3
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);
        }