Exemplo n.º 1
0
        /// <summary>
        /// Collects the values for an export
        /// </summary>
        /// <param name="templateType">Template type</param>
        /// <param name="parsedTemplate">Parsed scriban template</param>
        /// <param name="scriptObject">Scriban script object to fill</param>
        /// <param name="data">Export Data</param>
        /// <returns>Task</returns>
        public override async Task CollectValues(TemplateType templateType, Template parsedTemplate, ScriptObject scriptObject, ExportObjectData data)
        {
            FlexFieldObject inputObject = data.ExportData[ExportConstants.ExportDataObject] as FlexFieldObject;

            if (inputObject == null)
            {
                return;
            }

            _exportSnippetFunctionRenderer.SetErrorCollection(_errorCollection);

            List <ObjectExportSnippet> exportSnippets = await _exportCachedDbAccess.GetObjectExportSnippetsByObject(inputObject.Id);

            Dictionary <string, ScribanExportSnippet> mappedSnippets = await MapExportSnippets(exportSnippets, inputObject);

            ScribanExportSnippetsData scribanExportSnippets = new ScribanExportSnippetsData();

            scribanExportSnippets.Snippets = new ScribanExportSnippetDictionary(mappedSnippets);
            scriptObject.AddOrUpdate(ExportConstants.ScribanExportSnippetsObjectKey, scribanExportSnippets);

            scriptObject.AddOrUpdate(ExportSnippetFunctionPipeRenderer.ExportSnippetFunctionName, new ExportSnippetFunctionPipeRenderer(_templatePlaceholderResolver, _exportCachedDbAccess, _defaultTemplateProvider, _errorCollection));
        }
        /// <summary>
        /// Fills the export snippet placeholders
        /// </summary>
        /// <param name="code">Code to fill</param>
        /// <param name="exportObject">Export object</param>
        /// <param name="flexFieldObject">Flex field object</param>
        /// <returns>Updated code</returns>
        private async Task <string> FillExportSnippetPlaceholders(string code, IExportSnippetExportable exportObject, FlexFieldObject flexFieldObject)
        {
            HashSet <string>           usedExportSnippets = new HashSet <string>();
            List <ObjectExportSnippet> exportSnippets     = await _cachedDbAccess.GetObjectExportSnippetsByObject(exportObject.Id);

            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasAnyCodeSnippet_Start, Placeholder_HasAnyCodeSnippet_End, exportSnippets.Count > 0);
            code = ExportUtil.RenderPlaceholderIfTrue(code, Placeholder_HasNotAnyCodeSnippet_Start, Placeholder_HasNotAnyCodeSnippet_End, exportSnippets.Count == 0);
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, Placeholder_HasCodeSnippet_Start, Placeholder_HasCodeSnippet_End, (m) =>
            {
                string snippetName = m.Groups[1].Value;
                snippetName        = snippetName.ToLowerInvariant();

                return(exportSnippets.Any(e => e.SnippetName.ToLowerInvariant() == snippetName));
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, Placeholder_HasNotCodeSnippet_Start, Placeholder_HasNotCodeSnippet_End, (m) =>
            {
                string snippetName = m.Groups[1].Value;
                snippetName        = snippetName.ToLowerInvariant();

                return(!exportSnippets.Any(e => e.SnippetName.ToLowerInvariant() == snippetName));
            });

            code = ExportUtil.BuildPlaceholderRegex(ExportConstants.ExportSnippetRegex, ExportConstants.ListIndentPrefix).Replace(code, (m) =>
            {
                string snippetName = m.Groups[2].Value;
                snippetName        = snippetName.ToLowerInvariant();

                if (!usedExportSnippets.Contains(snippetName))
                {
                    usedExportSnippets.Add(snippetName);
                }

                return(ExportCodeSnippetContent(exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject, m.Groups[1].Value));
            });

            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, Placeholder_ExportSnippet_HasCodeSnippet_Additional_Functions_Start, Placeholder_ExportSnippet_HasCodeSnippet_Additional_Functions_End, (m) =>
            {
                string snippetName = m.Groups[1].Value;
                snippetName        = snippetName.ToLowerInvariant();

                return(HasCodeSnippetAdditionalFunctions(exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject));
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, Placeholder_ExportSnippet_HasCodeSnippet_NoAdditional_Functions_Start, Placeholder_ExportSnippet_HasCodeSnippet_NoAdditional_Functions_End, (m) =>
            {
                string snippetName = m.Groups[1].Value;
                snippetName        = snippetName.ToLowerInvariant();

                return(!HasCodeSnippetAdditionalFunctions(exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject));
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, Placeholder_ExportSnippet_HasCodeSnippet_NoAdditional_Functions_Start, Placeholder_ExportSnippet_HasCodeSnippet_NoAdditional_Functions_End, (m) =>
            {
                string snippetName = m.Groups[1].Value;
                snippetName        = snippetName.ToLowerInvariant();

                return(HasCodeSnippetAdditionalFunctions(exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject));
            });
            code = ExportUtil.BuildPlaceholderRegex(Placeholder_ExportSnippet_AdditionalFuntions, ExportConstants.ListIndentPrefix).Replace(code, (m) =>
            {
                string snippetName = m.Groups[2].Value;
                snippetName        = snippetName.ToLowerInvariant();

                return(ExportCodeSnippetAdditionalFunctions(exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject, m.Groups[1].Value));
            });

            ValidateAllExportSnippetsUsed(usedExportSnippets, exportSnippets);

            return(code);
        }