Beispiel #1
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));
        }
Beispiel #2
0
        public object Execute(ParameterList parameters, FunctionContextContainer context)
        {
            try
            {
                var dynamicMethod = DynamicMethodHelper.GetDynamicMethod("<C1 function> " + _functionToWrap.CompositeName());

                return(dynamicMethod(() => _functionToWrap.Execute(parameters, context)));
            }
            catch (Exception ex)
            {
                if (_functionToWrap.ReturnType == typeof(XhtmlDocument) || (_functionToWrap.ReturnType == typeof(void) && ex is HttpCompileException))
                {
                    XElement errorBoxHtml;
                    if (context.ProcessException(_functionToWrap.CompositeName(), ex, LogTitle, out errorBoxHtml))
                    {
                        XhtmlDocument errorInfoDocument = new XhtmlDocument();
                        errorInfoDocument.Body.Add(errorBoxHtml);
                        return(errorInfoDocument);
                    }
                }

                throw;
            }
        }
        public static string BuildXml(IEnumerable <KeyValuePair <string, string> > queries, ILookup <string, KeyValuePair <string, string> > parameters)
        {
            var usedQueryInfos =
                from query in queries
                select new { LocalName = query.Key, QueryInfo = FunctionFacade.GetFunction(query.Value) };

            var paramsDictionary = parameters.ToDictionary(f => f.Key);

            XElement rootElement = new XElement(ui + "treebody");

            foreach (var namedQueryInfo in usedQueryInfos.OrderBy(f => f.QueryInfo.Namespace + f.QueryInfo.Name + f.LocalName))
            {
                IFunction queryInfo = namedQueryInfo.QueryInfo;

                XElement currentNamespaceFolder = rootElement;

                if (string.IsNullOrEmpty(queryInfo.Namespace) == false)
                {
                    string piecemealNamespace = "";

                    foreach (string namespaceSegment in queryInfo.Namespace.Split('.'))
                    {
                        if (string.IsNullOrEmpty(piecemealNamespace) == false)
                        {
                            piecemealNamespace = piecemealNamespace + ".";
                        }
                        piecemealNamespace = piecemealNamespace + namespaceSegment;

                        XElement existingFolder =
                            (from folderElement in rootElement.Descendants(ui + "treenode")
                             where (string)folderElement.Attribute("binding") == "QueryFolderTreeNodeBinding" && (string)folderElement.Attribute("ElementId") == piecemealNamespace
                             select folderElement).FirstOrDefault();

                        if (existingFolder == null)
                        {
                            XElement subFolder = new XElement(ui + "treenode",
                                                              new XAttribute("binding", "QueryFolderTreeNodeBinding"),
                                                              new XAttribute("ElementId", piecemealNamespace),
                                                              new XAttribute("label", namespaceSegment));
                            currentNamespaceFolder.Add(subFolder);

                            currentNamespaceFolder = subFolder;
                        }
                        else
                        {
                            currentNamespaceFolder = existingFolder;
                        }
                    }
                }


                IGrouping <string, KeyValuePair <string, string> > currentQueryParams;
                bool hasParams = paramsDictionary.TryGetValue(namedQueryInfo.LocalName, out currentQueryParams);

                currentNamespaceFolder.Add(
                    new XElement(ui + "treenode",
                                 new XAttribute("binding", "QueryTreeNodeBinding"),
                                 new XAttribute("queryname", queryInfo.Name),
                                 new XAttribute("localname", namedQueryInfo.LocalName),
                                 new XAttribute("ElementId", queryInfo.CompositeName()),
                                 from parameter in queryInfo.ParameterProfiles
                                 orderby parameter.Name
                                 select new XElement(ui + "treenode",
                                                     new XAttribute("binding", "QueryParamTreeNodeBinding"),
                                                     new XAttribute("paramname", parameter.Name),
                                                     new XAttribute("hasvalue", hasParams && currentQueryParams.Count(f => f.Key == parameter.Name) == 1),
                                                     (hasParams && currentQueryParams.Count(f => f.Key == parameter.Name && f.Value != null) == 1 ? new XAttribute("paramvalue", currentQueryParams.First(f => f.Key == parameter.Name).Value) : null))));
            }

            XmlDocument output = new XmlDocument();

            output.LoadXml(rootElement.ToString());
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(output.NameTable);

            namespaceManager.AddNamespace("ui", ui.ToString());
            XmlNodeList allNodes = output.SelectNodes("//*");

            foreach (XmlNode node in allNodes)
            {
                node.Prefix = "ui";
            }
            output.DocumentElement.RemoveAttribute("xmlns");
            return(IndentedOuterXml(output));
        }
        /// <exclude />
        public override object GetValue(FunctionContextContainer contextContainer)
        {
            if (contextContainer == null)
            {
                throw new ArgumentNullException("contextContainer");
            }

            string functionName = _function.CompositeName() ?? "<unknown function>";

            using (TimerProfilerFacade.CreateTimerProfiler(functionName))
            {
                ValidateNotSelfCalling();

                try
                {
                    var parameters = new ParameterList(contextContainer);

                    foreach (ParameterProfile parameterProfile in _function.ParameterProfiles)
                    {
                        List <BaseParameterRuntimeTreeNode> parameterTreeNodes = this.Parameters.Where(ptn => ptn.Name == parameterProfile.Name).ToList();

                        if (parameterTreeNodes.Count > 0)
                        {
                            parameters.AddLazyParameter(parameterProfile.Name, parameterTreeNodes[0], parameterProfile.Type);
                            continue;
                        }

                        if (parameterProfile.Type.IsGenericType &&
                            parameterProfile.Type.GetGenericTypeDefinition() == typeof(NullableDataReference <>))
                        {
                            parameters.AddConstantParameter(parameterProfile.Name, null, parameterProfile.Type);
                            continue;
                        }

                        if (parameterProfile.IsRequired)
                        {
                            var injectedValue = TryGetInjectedValue(parameterProfile.Type);

                            if (injectedValue == null)
                            {
                                throw new ArgumentException("Missing parameter '{0}' (type of {1})".FormatWith(parameterProfile.Name, parameterProfile.Type.FullName));
                            }

                            parameters.AddConstantParameter(parameterProfile.Name, injectedValue, parameterProfile.Type);
                            continue;
                        }

                        BaseValueProvider valueProvider = parameterProfile.FallbackValueProvider;

                        object value;
                        try
                        {
                            value = valueProvider.GetValue(contextContainer);
                        }
                        catch (Exception ex)
                        {
                            throw new InvalidOperationException($"Failed to get value for parameter '{parameterProfile.Name}' in function '{functionName}'.", ex);
                        }
                        parameters.AddConstantParameter(parameterProfile.Name, value, parameterProfile.Type, true);
                    }

                    object result;

                    IDisposable measurement = null;
                    try
                    {
                        if (functionName != "Composite.Utils.GetInputParameter")
                        {
                            var nodeToLog = functionName;

                            if (_function is IDynamicFunction df && df.PreventFunctionOutputCaching)
                            {
                                nodeToLog += " (PreventCaching)";
                            }

                            measurement = Profiler.Measure(nodeToLog, () => _function.EntityToken);
                        }

                        result = _function.Execute(parameters, contextContainer);
                    }
                    finally
                    {
                        measurement?.Dispose();
                    }

                    return(result);
                }
                catch (ThreadAbortException)
                {
                    return(null); // Nothing will be returned as ThreadAbort will propagate
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException($"Failed to get value for function '{functionName}'", ex);
                }
            }
        }