GetParametersFromString() public static méthode

public static GetParametersFromString ( string text ) : IEnumerable
text string
Résultat IEnumerable
Exemple #1
0
        private string ReplaceParameters(string text, Entity Target, IOrganizationService Service)
        {
            if (String.IsNullOrWhiteSpace(text))
            {
                return("");
            }

            foreach (RuntimeParameter param in RuntimeParameter.GetParametersFromString(text))
            {
                if (!param.IsParentParameter())
                {
                    text = text.Replace(param.ParameterText, param.GetParameterValue(Target));
                }
                else
                {
                    if (Target.Contains(param.ParentLookupName))
                    {
                        var parentRecord = Service.Retrieve(Target.GetAttributeValue <EntityReference>(param.ParentLookupName).LogicalName, Target.GetAttributeValue <EntityReference>(param.ParentLookupName).Id, new ColumnSet(param.AttributeName));
                        text = text.Replace(param.ParameterText, param.GetParameterValue(parentRecord));
                    }
                    else                      // Target record has no parent, so use default value
                    {
                        text = text.Replace(param.ParameterText, param.DefaultValue);
                    }
                }
            }

            return(text);
        }
Exemple #2
0
        protected void Execute(LocalPluginContext context)
        {
            Context = context;

            Trace("Getting Target entity");
            Entity Target = Context.GetInputParameters <CreateInputParameters>().Target;

            Trace("Validate the Entity name");
            Trace("Get Attribute List");
            List <AttributeMetadata> attributeList = GetEntityMetadata(Target.GetAttributeValue <string>("cel_entityname"));

            Trace("Validate the Attribute name");
            if (!attributeList.Select(a => a.LogicalName).Contains(Target.GetAttributeValue <string>("cel_attributename")))
            {
                throw new InvalidPluginExecutionException("Specified Attribute does not exist.");
            }

            Trace("Validate the Attribute type");
            if (attributeList.Single(a => a.LogicalName.Equals(Target.GetAttributeValue <string>("cel_attributename"))).AttributeType != AttributeTypeCode.String && attributeList.Single(a => a.LogicalName.Equals(Target.GetAttributeValue <string>("cel_attributename"))).AttributeType != AttributeTypeCode.Memo)
            {
                throw new InvalidPluginExecutionException("Attribute must be a text field.");
            }

            #region test parameters
#if VALIDATEPARAMETERS
            Dictionary <string, string> fields = new Dictionary <string, string>()
            {
                { "cel_prefix", "Prefix" }, { "cel_suffix", "Suffix" }
            };

            foreach (string field in fields.Keys)
            {
                if (Target.Contains(field) && Target.GetAttributeValue <string>(field).Contains('{'))
                {
                    if (Target.GetAttributeValue <string>(field).Count(c => c.Equals('{')) != Target.GetAttributeValue <string>(field).Count(c => c.Equals('}')))
                    {
                        throw new InvalidPluginExecutionException(String.Format("Invalid parameter formatting in {0}", fields[field]));
                    }

                    foreach (string p in Regex.Matches(Target.GetAttributeValue <string>(field), @"{(.*?)}").OfType <Match>().Select(m => m.Groups[0].Value).Distinct())
                    {
                        if (p.Substring(1).Contains('{'))
                        {
                            throw new InvalidPluginExecutionException(String.Format("Invalid parameter formatting in {0}", fields[field]));
                        }
                    }

                    try
                    {
                        foreach (RuntimeParameter param in RuntimeParameter.GetParametersFromString(Target.GetAttributeValue <string>(field)))
                        {
                            if (!param.IsParentParameter())
                            {
                                if (!attributeList.Select(a => a.LogicalName).Contains(param.AttributeName))
                                {
                                    throw new InvalidPluginExecutionException(String.Format("{0} is not a valid attribute name in {1} value", param.AttributeName, fields[field]));
                                }
                            }
                            else
                            {
                                if (!attributeList.Select(a => a.LogicalName).Contains(param.ParentLookupName))
                                {
                                    throw new InvalidPluginExecutionException(String.Format("{0} is not a valid attribute name in {1} value", param.ParentLookupName, fields[field]));
                                }

                                if (attributeList.Single(a => a.LogicalName.Equals(param.ParentLookupName)).AttributeType != AttributeTypeCode.Lookup && attributeList.Single(a => a.LogicalName.Equals(param.ParentLookupName)).AttributeType != AttributeTypeCode.Customer && attributeList.Single(a => a.LogicalName.Equals(param.ParentLookupName)).AttributeType != AttributeTypeCode.Owner)
                                {
                                    throw new InvalidPluginExecutionException(String.Format("{0} must be a Lookup attribute type in {1} value", param.ParentLookupName, fields[field]));
                                }

                                var parentLookupAttribute = (LookupAttributeMetadata)GetAttributeMetadata(Target.GetAttributeValue <string>("cel_entityname"), param.ParentLookupName);
                                if (!parentLookupAttribute.Targets.Any(e => GetEntityMetadata(e).Select(a => a.LogicalName).Contains(param.AttributeName)))
                                {
                                    throw new InvalidPluginExecutionException(String.Format("invalid attribute on {0} parent entity, in {1} value", param.ParentLookupName, fields[field]));
                                }
                            }
                        }
                    }
                    catch (InvalidPluginExecutionException)
                    {
                        throw;
                    }
                    catch
                    {
                        throw new InvalidPluginExecutionException(String.Format("Failed to parse Runtime Parameters in {0} value.", fields[field]));
                    }
                }
            }
#endif
            #endregion

            if (Target.Contains("cel_conditionaloptionset"))
            {
                Trace("Validate Conditional OptionSet");
                if (!attributeList.Select(a => a.LogicalName).Contains(Target.GetAttributeValue <string>("cel_conditionaloptionset")))
                {
                    throw new InvalidPluginExecutionException("Specified Conditional OptionSet does not exist");
                }

                if (attributeList.Single(a => a.LogicalName.Equals(Target.GetAttributeValue <string>("cel_conditionaloptionset"))).AttributeType != AttributeTypeCode.Picklist)
                {
                    throw new InvalidPluginExecutionException("Conditional Attribute must be an OptionSet");
                }

                Trace("Validate Conditional Value");
                PicklistAttributeMetadata optionSetMetadata = (PicklistAttributeMetadata)GetAttributeMetadata(Target.GetAttributeValue <string>("cel_entityname"), Target.GetAttributeValue <string>("cel_conditionaloptionset"));              //attributeResponse.AttributeMetadata;
                if (!optionSetMetadata.OptionSet.Options.Select(o => o.Value).Contains(Target.GetAttributeValue <int>("cel_conditionalvalue")))
                {
                    throw new InvalidPluginExecutionException("Conditional Value does not exist in OptionSet");
                }
            }

            #region Duplicate Check
#if DUPLICATECHECK
            Trace("Validate there are no duplicates");
            // TODO: Fix this. duplicate detection works when all fields contain data, but fails when some fields are empty
            var autoNumberList = Context.OrganizationDataContext.CreateQuery("cel_autonumber")
                                 .Where(a => a.GetAttributeValue <string>("cel_entityname").Equals(Target.GetAttributeValue <string>("cel_entityname")) && a.GetAttributeValue <string>("cel_attributename").Equals(Target.GetAttributeValue <string>("cel_attributename")))
                                 .Select(a => new { Id = a.GetAttributeValue <Guid>("cel_autonumberid"), ConditionalOption = a.GetAttributeValue <string>("cel_conditionaloptionset"), ConditionalValue = a.GetAttributeValue <int>("cel_conditionalvalue") })
                                 .ToList();


            if (!Target.Contains("cel_conditionaloptionset") && autoNumberList.Any())
            {
                throw new InvalidPluginExecutionException("Duplicate AutoNumber record exists.");
            }
            else if (autoNumberList.Where(a => a.ConditionalOption.Equals(Target.GetAttributeValue <string>("cel_conditionaloptionset")) && a.ConditionalValue.Equals(Target.GetAttributeValue <int>("cel_conditionalvalue"))).Any())
            {
                throw new InvalidPluginExecutionException("Duplicate AutoNumber record exists.");
            }
#endif
            #endregion

            Trace("Insert the autoNumber Name attribute");
            Target["cel_name"] = String.Format("AutoNumber for {0}, {1}", Target.GetAttributeValue <string>("cel_entityname"), Target.GetAttributeValue <string>("cel_attributename"));
        }