public void GetClassPropertyValueNull()
            {
                Application application = new Application();

                object value = application.GetTokenValue("ApplicationId");
                Assert.IsNull(value);
            }
            public void GetDataPropertyValueUnknownToken()
            {
                Application application = new Application();

                object value = application.GetTokenValue("Foo");
                Assert.IsNull(value);
            }
        /// <summary>
        /// Populates <paramref name="target"/> with static and <paramref name="source"/> values
        /// as defined by <paramref name="fieldMap"/>.
        /// </summary>
        /// <param name="source">The application from which to get field values.</param>
        /// <param name="fieldMap">A definition of field mappings.</param>
        /// <param name="target">The target object to populate with mapped key/values.</param>
        internal void Map(Application source, MappedFieldList fieldMap, JObject target)
        {
            foreach (MappedField map in fieldMap)
            {
                switch (map.MapType)
                {
                    case MapType.Value:
                    case MapType.PrivateValue:
                        target.Add(map.Target, map.Source);
                        break;

                    case MapType.Field:
                        object tokenValue = source.GetTokenValue(map.Source);
                        if (tokenValue == null)
                        {
                            target.Add(map.Target, string.Empty);
                        }
                        else if (tokenValue is IEnumerable<object>)
                        {
                            target.Add(map.Target, JArray.FromObject(tokenValue));
                        }
                        else
                        {
                            target.Add(map.Target, tokenValue.ToString());
                        }

                        break;

                    default:
                        throw new InvalidOperationException(string.Format(ExceptionMessages.InvalidMapType, map.MapType));
                }
            }
        }
            public void GetDataPropertyValueNull()
            {
                Application application = new Application();
                application.ApplicationData.Add("Foo", null);

                object value = application.GetTokenValue("Foo");
                Assert.IsNull(value);
            }
            public void GetDataPropertyValue()
            {
                Application application = new Application();
                application.ApplicationData.Add("Foo", "Bar");

                string value = application.GetTokenValue("Foo").ToString();
                Assert.AreEqual("Bar", value);
            }
            public void GetClassPropertyValue()
            {
                Application application = new Application
                {
                    ApplicationId = "GetMe"
                };

                string value = application.GetTokenValue("ApplicationId").ToString();
                Assert.AreEqual("GetMe", value);
            }
            public void GetClassPropertyValueNested()
            {
                Application application = new Application
                {
                    CreatedBy = new AuthenticatedApplicationUser
                    {
                        DisplayName = "Foo Bar"
                    }
                };

                string value = application.GetTokenValue("CreatedBy.DisplayName").ToString();
                Assert.AreEqual("Foo Bar", value);
            }
        /// <summary>
        /// Populates <paramref name="target"/> with static and <paramref name="source"/> values
        /// as defined by <paramref name="fieldMap"/>.
        /// </summary>
        /// <param name="source">The application from which to get field values.</param>
        /// <param name="fieldMap">A definition of field mappings.</param>
        /// <param name="target">The target object to populate with mapped key/values.</param>
        internal void Map(Application source, MappedFieldList fieldMap, XmlDocument target)
        {
            XmlNode root = target.FirstChild;
            foreach (MappedField map in fieldMap)
            {
                switch (map.MapType)
                {
                    case MapType.Value:
                    case MapType.PrivateValue:
                        root.AddElement(map.Target, map.Source);
                        break;

                    case MapType.Field:
                        object tokenValue = source.GetTokenValue(map.Source);
                        if (tokenValue == null)
                        {
                            root.AddElement(map.Target, string.Empty);
                        }
                        else
                        {
                            var value = tokenValue as IEnumerable<object>;
                            if (value != null)
                            {
                                this.GetNestedValue(value, map, root);
                            }
                            else
                            {
                                root.AddElement(map.Target, tokenValue.ToString());
                            }
                        }

                        break;

                    default:
                        throw new InvalidOperationException(string.Format(ExceptionMessages.InvalidMapType, map.MapType));
                }
            }
        }
            public void TraverseTreeBottom()
            {
                Application application = new Application();
                application.ApplicationData.Add("RootChild", "RootChild Value");

                var outerRepeater = new List<Dictionary<string, object>> { new Dictionary<string, object>() };
                var innerRepeater = new List<Dictionary<string, object>> { new Dictionary<string, object>() };

                innerRepeater[0].Add("InnerChild", "InnerChild Value");
                outerRepeater[0].Add("InnerRepeater", innerRepeater.ToArray());
                outerRepeater[0].Add("OuterChild", "OuterChild Value");
                application.ApplicationData.Add("OuterRepeater", outerRepeater.ToArray());

                ApplicationDataPath path = new ApplicationDataPath();
                path.Prepend("InnerRepeater", 0);
                path.Prepend("OuterRepeater", 0);
                string value = application.GetTokenValue("InnerChild", path).ToString();

                Assert.AreEqual("InnerChild Value", value);
            }
            public void OrderOfPrecedence()
            {
                Application application = new Application
                {
                    ApplicationId = "GetMe"
                };
                application.ApplicationData.Add("ApplicationId", "NotMe");

                string value = application.GetTokenValue("ApplicationId").ToString();
                Assert.AreEqual("GetMe", value);
            }
示例#11
0
        /// <summary>
        /// Gets the value of a token.
        /// </summary>
        /// <param name="token">The token being replaced.</param>
        /// <param name="application">The application to source data from.</param>
        /// <param name="control">The control.</param>
        /// <param name="path">Specifies the path to the token.</param>
        /// <returns>
        /// The value of the token.
        /// </returns>
        /// <remarks>
        /// Order of precedence: (1) Timestamp; (2) Application URL (3) <paramref name="application" /> object; (4) application data.
        /// </remarks>
        private object GetTokenValue(string token, Application application, Control control, ApplicationDataPath path)
        {
            if (token.StartsWith(FormatterResources.TimestampToken, StringComparison.CurrentCultureIgnoreCase))
            {
                return DateTime.Now;
            }

            if (token.StartsWith(FormatterResources.ApplicationUrlToken, StringComparison.CurrentCultureIgnoreCase))
            {
                string url = string.Format(FormatterResources.ApplicationUrlFormat, this.BaseUrl, application.FormId, application.Id);
                return string.Format(FormatterResources.ApplicationAnchor, url);
            }

            if (application == null)
            {
                return null;
            }

            string valueStr = application.GetTokenValue(token, path) as string;
            if (control is DateControl)
            {
                DateTime dateValue;
                if (valueStr != null && DateTime.TryParseExact(valueStr, DateControl.SYSTEM_FORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateValue))
                {
                    return dateValue.ToString(DateControl.USER_FORMAT);
                }
            }

            var calculationControl = control as CalculationControl;
            if (calculationControl == null || !calculationControl.FormatAsCurrency || valueStr == null)
            {
                return application.GetTokenValue(token, path);
            }

            decimal decimalVal;
            bool parsed = decimal.TryParse(valueStr, out decimalVal);
            if (parsed)
            {
                return string.Format("{0:C}", decimalVal);
            }

            return application.GetTokenValue(token, path);
        }