Ejemplo n.º 1
0
        public bool TryDeserialize(XElement serializedObject, IXmlSerializer xmlSerializer, out object deserializedObject)
        {
            if (serializedObject == null)
            {
                throw new ArgumentNullException("serializedObject");
            }

            deserializedObject = null;

            if (serializedObject.Name.LocalName != "BaseRuntimeTreeNode")
            {
                return(false);
            }

            XElement valueElement = serializedObject.Element("Value");

            if (valueElement != null)
            {
                if (valueElement.Elements().Count() != 1)
                {
                    return(false);
                }

                deserializedObject = FunctionFacade.BuildTree(valueElement.Elements().Single());
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <exclude />
        public static bool FunctionExists(string namespaceName, string name)
        {
            IFunction fun;

            bool exists = FunctionFacade.TryGetFunction(out fun, StringExtensionMethods.CreateNamespace(namespaceName, name));

            return(exists);
        }
Ejemplo n.º 3
0
        /// <exclude />
        public static IFunction GetDefaultFunctionByType(Type type)
        {
            if (type == typeof(string))
            {
                return(StandardFunctions.StringFunction);
            }
            if (type == typeof(int) || type == typeof(int?))
            {
                return(StandardFunctions.IntegerFunction);
            }
            if (type == typeof(Decimal) || type == typeof(Decimal?))
            {
                return(StandardFunctions.DecimalFunction);
            }
            if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                return(StandardFunctions.DateTimeFunction);
            }
            if (type == typeof(Guid) || type == typeof(Guid?))
            {
                return(StandardFunctions.GuidFunction);
            }
            if (type == typeof(bool) || type == typeof(bool?))
            {
                return(StandardFunctions.BooleanFunction);
            }

            if (type == typeof(XhtmlDocument))
            {
                return(StandardFunctions.XhtmlDocumentFunction);
            }

            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(NullableDataReference <>))
                {
                    var       referenceType = type.GetGenericArguments().First();
                    var       functionName  = StringExtensionMethods.CreateNamespace(referenceType.FullName, "GetNullableDataReference");
                    IFunction function;
                    if (FunctionFacade.TryGetFunction(out function, functionName))
                    {
                        return(function);
                    }
                }
                else if (type.GetGenericTypeDefinition() == typeof(DataReference <>))
                {
                    var       referenceType = type.GetGenericArguments().First();
                    var       functionName  = StringExtensionMethods.CreateNamespace(referenceType.FullName, "GetDataReference");
                    IFunction function;
                    if (FunctionFacade.TryGetFunction(out function, functionName))
                    {
                        return(function);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 4
0
        private static FunctionRuntimeTreeNode BuildFunctionRuntimeNode(XElement element, bool ignoreUnusedParameters)
        {
            XAttribute nameAttribute = element.Attribute(FunctionTreeConfigurationNames.NameAttributeName);

            if (nameAttribute == null)
            {
                throw new InvalidOperationException(string.Format("Missing attribute named '{0}'", FunctionTreeConfigurationNames.NameAttributeName));
            }

            var parameters = new List <BaseParameterRuntimeTreeNode>();

            foreach (XElement childElement in element.Elements())
            {
                if (childElement.Name.LocalName == FunctionTreeConfigurationNames.ParamTagName)
                {
                    BaseParameterRuntimeTreeNode parameterTreeNode = BuildParameterFunctionRuntimeNode(childElement);

                    parameters.Add(parameterTreeNode);
                }
                else
                {
                    throw new InvalidOperationException(string.Format("Only '{0}' tags allowed inside '{1}' tags", FunctionTreeConfigurationNames.ParamTagName, FunctionTreeConfigurationNames.FunctionTagName));
                }
            }


            IFunction function = FunctionFacade.GetFunction(nameAttribute.Value);


            if (FunctionInitializedCorrectly(function))
            {
                for (int index = parameters.Count - 1; index >= 0; index--)
                {
                    BaseParameterRuntimeTreeNode parameter = parameters[index];
                    if (function.ParameterProfiles.All(pp => pp.Name != parameter.Name))
                    {
                        string message = "The parameter '{0}' is not defined in the function named '{1}' parameter profiles"
                                         .FormatWith(parameter.Name, function.CompositeName());

                        if (ignoreUnusedParameters)
                        {
                            Log.LogWarning(typeof(FunctionTreeBuilder).Name, message);

                            parameters.RemoveAt(index);
                            continue;
                        }

                        throw new InvalidOperationException(message);
                    }
                }
            }

            return(new FunctionRuntimeTreeNode(function, parameters));
        }
Ejemplo n.º 5
0
        /// <exclude />
        public static FunctionRuntimeTreeNode BuildFunction(string functionName, Dictionary <string, string> parameters)
        {
            var functionParams = new List <BaseParameterRuntimeTreeNode>();

            foreach (var parameter in parameters)
            {
                functionParams.Add(new ConstantParameterRuntimeTreeNode(parameter.Key, parameter.Value));
            }

            IFunction function = FunctionFacade.GetFunction(functionName);

            return(new FunctionRuntimeTreeNode(function, functionParams));
        }
Ejemplo n.º 6
0
        private static WidgetFunctionRuntimeTreeNode BuildWidgetFunctionRuntimeNode(XElement element)
        {
            XAttribute nameAttribute = element.Attribute(FunctionTreeConfigurationNames.NameAttributeName);

            if (nameAttribute == null)
            {
                throw new InvalidOperationException(string.Format("Missing attribute named '{0}'", FunctionTreeConfigurationNames.NameAttributeName));
            }

            string label             = AttributeValueOrEmpty(element, FunctionTreeConfigurationNames.LabelAttributeName);
            string bindingSourceName = AttributeValueOrEmpty(element, FunctionTreeConfigurationNames.BindingSourceNameAttributeName);

            HelpDefinition helpDefinition = null;
            var            parameters     = new List <BaseParameterRuntimeTreeNode>();

            foreach (XElement childElement in element.Elements())
            {
                if (childElement.Name.LocalName == FunctionTreeConfigurationNames.HelpDefinitionTagName)
                {
                    helpDefinition = HelpDefinition.Deserialize(childElement);
                }
                else if (childElement.Name.LocalName == FunctionTreeConfigurationNames.ParamTagName)
                {
                    BaseParameterRuntimeTreeNode parameterTreeNode = BuildParameterFunctionRuntimeNode(childElement);

                    parameters.Add(parameterTreeNode);
                }
                else
                {
                    throw new InvalidOperationException(string.Format("Only '{0}' tags allowed inside '{1}' tags", FunctionTreeConfigurationNames.ParamTagName, FunctionTreeConfigurationNames.FunctionTagName));
                }
            }

            if (helpDefinition == null)
            {
                helpDefinition = new HelpDefinition("");
            }

            IWidgetFunction widgetFunction = FunctionFacade.GetWidgetFunction(nameAttribute.Value);

            foreach (BaseParameterRuntimeTreeNode parameter in parameters)
            {
                if (widgetFunction.ParameterProfiles.All(pp => pp.Name != parameter.Name))
                {
                    throw new InvalidOperationException(string.Format("The parameter '{0}' is not defined in the function named '{1}' parameter profiles", parameter.Name, widgetFunction.CompositeName()));
                }
            }

            return(new WidgetFunctionRuntimeTreeNode(widgetFunction, label, helpDefinition, bindingSourceName, parameters));
        }
 private void Initialize()
 {
     if (_functionFunctionRuntimeNode == null)
     {
         lock (_lock)
         {
             if (_functionFunctionRuntimeNode == null)
             {
                 if (_serializedFunction == null)
                 {
                     IFunction function = FunctionFacade.GetFunction(_functionName);
                     _functionFunctionRuntimeNode = new FunctionRuntimeTreeNode(function, _parameters);
                 }
                 else
                 {
                     _functionFunctionRuntimeNode = (FunctionRuntimeTreeNode)FunctionTreeBuilder.Build(_serializedFunction);
                 }
             }
         }
     }
 }
Ejemplo n.º 8
0
        /// <exclude />
        public static WidgetFunctionRuntimeTreeNode BuildWidgetFunction(string widgetFunctionName, string label, HelpDefinition helpDefinition, string bindingSourceName)
        {
            IWidgetFunction widgetFunction = FunctionFacade.GetWidgetFunction(widgetFunctionName);

            return(new WidgetFunctionRuntimeTreeNode(widgetFunction, label, helpDefinition, bindingSourceName));
        }
        public bool TryDeserialize(XElement serializedObject, IXmlSerializer xmlSerializer, out object deserializedObject)
        {
            if (serializedObject == null)
            {
                throw new ArgumentNullException("serializedObject");
            }
            if (xmlSerializer == null)
            {
                throw new ArgumentNullException("xmlSerializer");
            }

            deserializedObject = null;

            if (serializedObject.Name.LocalName != "NamedFunctionCall")
            {
                return(false);
            }

            NamedFunctionCall namedFunctionCall = new NamedFunctionCall(null, null);

            XElement nameElement = serializedObject.Element("Name");

            if (nameElement != null)
            {
                if (nameElement.Elements().Count() != 1)
                {
                    return(false);
                }

                namedFunctionCall.Name = (string)xmlSerializer.Deserialize(nameElement.Elements().Single());
            }

            XElement valueElement = serializedObject.Element("Value");

            if (valueElement != null)
            {
                if (valueElement.Elements().Count() != 1)
                {
                    return(false);
                }

                object result;
                try
                {
                    result = FunctionFacade.BuildTree(valueElement.Elements().Single());
                }
                catch (Exception)
                {
                    return(false);
                }
                if ((result is BaseFunctionRuntimeTreeNode) == false)
                {
                    return(false);
                }

                namedFunctionCall.FunctionCall = (BaseFunctionRuntimeTreeNode)result;
            }

            deserializedObject = namedFunctionCall;
            return(true);
        }
Ejemplo n.º 10
0
 private void EnsureWidgetFunction()
 {
     lock (_lock)
     {
         if (_widgetFunction == null && _serializedWidgetFunction != null)
         {
             WidgetFunctionRuntimeTreeNode functionNode = (WidgetFunctionRuntimeTreeNode)FunctionFacade.BuildTree(_serializedWidgetFunction);
             _setParameters  = functionNode.GetSetParameters().ToList();
             _widgetFunction = functionNode.GetWidgetFunction();
         }
     }
 }
Ejemplo n.º 11
0
 public static T Execute <T>(this IFunction function, NameValueCollection parameters)
 {
     return(FunctionFacade.Execute <T>(function, parameters));;
 }
Ejemplo n.º 12
0
        /// <exclude />
        public static WidgetFunctionProvider GetDefaultWidgetFunctionProviderByType(Type type, bool required)
        {
            if (type == typeof(string))
            {
                return(StandardWidgetFunctions.TextBoxWidget);
            }
            if (type == typeof(int) || type == typeof(int?))
            {
                return(StandardWidgetFunctions.IntegerTextBoxWidget);
            }
            if (type == typeof(Decimal) || type == typeof(Decimal?))
            {
                return(StandardWidgetFunctions.DecimalTextBoxWidget);
            }
            if (type == typeof(DateTime) || type == typeof(DateTime?))
            {
                return(StandardWidgetFunctions.DateSelectorWidget);
            }
            if (type == typeof(Guid) || type == typeof(Guid?))
            {
                return(StandardWidgetFunctions.GuidTextBoxWidget);
            }
            if (type == typeof(bool) || type == typeof(bool?))
            {
                return(StandardWidgetFunctions.CheckBoxWidget);
            }

            if (type == typeof(DataReference <IImageFile>))
            {
                return(StandardWidgetFunctions.GetImageSelectorWidget(required));
            }
            if (type == typeof(NullableDataReference <IImageFile>))
            {
                return(StandardWidgetFunctions.GetImageSelectorWidget(false));
            }
            if (type == typeof(DataReference <IMediaFile>))
            {
                return(StandardWidgetFunctions.GetMediaFileSelectorWidget(required));
            }
            if (type == typeof(NullableDataReference <IMediaFile>))
            {
                return(StandardWidgetFunctions.GetMediaFileSelectorWidget(false));
            }

            if (type == typeof(XhtmlDocument))
            {
                return(StandardWidgetFunctions.VisualXhtmlDocumentEditorWidget);
            }

            IEnumerable <string> functionNames = FunctionFacade.GetWidgetFunctionNamesByType(type);

            foreach (string functionName in functionNames)
            {
                IWidgetFunction widgetFunction = FunctionFacade.GetWidgetFunction(functionName);
                bool            sameType       = widgetFunction.ReturnType == type;

                if (!sameType &&
                    !required &&
                    type.IsGenericType && type.GetGenericTypeDefinition() == typeof(DataReference <>) &&
                    type.IsAssignableFrom(widgetFunction.ReturnType) &&
                    widgetFunction.ReturnType == typeof(NullableDataReference <>).MakeGenericType(type.GetGenericArguments()))
                {
                    sameType = true;
                }

                if (sameType && !widgetFunction.ParameterProfiles.Any(p => p.IsRequired))
                {
                    return(new WidgetFunctionProvider(widgetFunction));
                }
            }

            if (type.IsLazyGenericType())
            {
                var lazyType = type.GetGenericArguments().First();

                var provider = GetDefaultWidgetFunctionProviderByType(lazyType, required);

                if (provider != null)
                {
                    return(provider);
                }
            }


            return(null);
        }