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); }
public void RuntimeParameterParseTest4() { RuntimeParameter rp = RuntimeParameter.Parse("{attributeName:formatString}"); // Stuff that should be populated Assert.AreEqual(rp.AttributeName, "attributeName"); Assert.AreEqual(rp.StringFormatter, "formatString"); // Stuff that should not be populated Assert.AreEqual(rp.Conditional, new RuntimeParameter.ConditionalFormatter()); Assert.AreEqual(rp.DefaultValue, String.Empty); Assert.AreEqual(rp.ParentLookupName, String.Empty); }
public static RuntimeParameter Parse(string input) { var rp = new RuntimeParameter { ParameterText = input, AttributeName = input.Trim('{', '}') }; if (rp.AttributeName.Contains(':')) { string[] paramList = rp.AttributeName.Split(':'); rp.AttributeName = paramList[0]; if (rp.AttributeName == "rand") { rp.StringFormatter = paramList[1]; } else { if (paramList[1].Contains('?')) { rp.Conditional = ConditionalFormatter.Parse(paramList[1]); } else if (paramList.Length > 2) { rp.StringFormatter = paramList[1]; rp.Conditional = ConditionalFormatter.Parse(paramList[2]); } else { rp.StringFormatter = paramList[1]; } } } if (rp.AttributeName.Contains('|')) { rp.DefaultValue = rp.AttributeName.Split('|')[1]; rp.AttributeName = rp.AttributeName.Split('|')[0]; } if (rp.AttributeName.Contains('.')) { rp.ParentLookupName = rp.AttributeName.Split('.')[0]; rp.AttributeName = rp.AttributeName.Split('.')[1]; } return(rp); }
public void RuntimeParameterParseTest11() { RuntimeParameter rp = RuntimeParameter.Parse("{attributeName:<2015-1-1?trueValue|falseValue}"); // Stuff that should be populated Assert.AreEqual(rp.AttributeName, "attributeName"); // Stuff that should not be populated Assert.AreEqual(rp.DefaultValue, String.Empty); Assert.AreEqual(rp.ParentLookupName, String.Empty); Assert.AreEqual(rp.StringFormatter, String.Empty); // Conditional test cases Assert.AreEqual(rp.Conditional.GetResult(new DateTime(2014, 1, 1)), "trueValue"); Assert.AreEqual(rp.Conditional.GetResult(new DateTime(2016, 1, 1)), "falseValue"); }
public void RuntimeParameterParseTest9() { RuntimeParameter rp = RuntimeParameter.Parse("{attributeName|defaultValue:formatString:matchValue?trueValue|falseValue}"); // Stuff that should be populated Assert.AreEqual(rp.AttributeName, "attributeName"); Assert.AreEqual(rp.DefaultValue, "defaultValue"); Assert.AreEqual(rp.StringFormatter, "formatString"); // Stuff that should not be populated Assert.AreEqual(rp.ParentLookupName, String.Empty); // Conditional test cases Assert.AreEqual(rp.Conditional.GetResult("matchValue"), "trueValue"); Assert.AreEqual(rp.Conditional.GetResult("other"), "falseValue"); }
public void RuntimeParameterParseTest6() { RuntimeParameter rp = RuntimeParameter.Parse("{attributeName:match1?result1|match2?result2|match3?result3|elseResult}"); // Stuff that should be populated Assert.AreEqual(rp.AttributeName, "attributeName"); // Stuff that should not be populated Assert.AreEqual(rp.DefaultValue, String.Empty); Assert.AreEqual(rp.ParentLookupName, String.Empty); Assert.AreEqual(rp.StringFormatter, String.Empty); // Conditional test cases Assert.AreEqual(rp.Conditional.GetResult("match1"), "result1"); Assert.AreEqual(rp.Conditional.GetResult("match2"), "result2"); Assert.AreEqual(rp.Conditional.GetResult("match3"), "result3"); Assert.AreEqual(rp.Conditional.GetResult("other"), "elseResult"); }
public void RuntimeParameterParseTest12() { RuntimeParameter rp = RuntimeParameter.Parse("{attributeName:yyyy:2015?trueValue|falseValue}"); // Stuff that should be populated Assert.AreEqual(rp.AttributeName, "attributeName"); Assert.AreEqual(rp.StringFormatter, "yyyy"); // Stuff that should not be populated Assert.AreEqual(rp.DefaultValue, String.Empty); Assert.AreEqual(rp.ParentLookupName, String.Empty); // Conditional test cases Entity test = new Entity(); test["attributeName"] = new DateTime(2015, 1, 1); Assert.AreEqual(rp.GetParameterValue(test), "trueValue"); test["attributeName"] = new DateTime(2016, 1, 1); Assert.AreEqual(rp.GetParameterValue(test), "falseValue"); }
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")); }
public static RuntimeParameter Parse(string input) { RuntimeParameter rp = new RuntimeParameter(); rp.ParameterText = input; rp.AttributeName = input.Trim('{', '}'); if (rp.AttributeName.Contains(':')) { string[] paramList = rp.AttributeName.Split(':'); rp.AttributeName = paramList[0]; if (rp.AttributeName == "rand") { rp.StringFormatter = paramList[1]; } else { if (paramList[1].Contains('?')) { rp.Conditional = ConditionalFormatter.Parse(paramList[1]); } else if (paramList.Length > 2) { rp.StringFormatter = paramList[1]; rp.Conditional = ConditionalFormatter.Parse(paramList[2]); } else { rp.StringFormatter = paramList[1]; } } } if (rp.AttributeName.Contains('|')) { rp.DefaultValue = rp.AttributeName.Split('|')[1]; rp.AttributeName = rp.AttributeName.Split('|')[0]; } if (rp.AttributeName.Contains('.')) { rp.ParentLookupName = rp.AttributeName.Split('.')[0]; rp.AttributeName = rp.AttributeName.Split('.')[1]; } return rp; }