/// <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>();
            Dictionary <string, List <ExportSnippetFunction> > renderedFunctions = new Dictionary <string, List <ExportSnippetFunction> >();
            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 = await ExportUtil.BuildPlaceholderRegex(ExportConstants.ExportSnippetRegex, ExportConstants.ListIndentPrefix).ReplaceAsync(code, async(m) =>
            {
                string snippetName = m.Groups[2].Value;
                snippetName        = snippetName.ToLowerInvariant();

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

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

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

                return(await HasCodeSnippetAdditionalFunctions(renderedFunctions, exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject));
            });

            code = await ExportUtil.RenderPlaceholderIfFuncTrueAsync(code, Placeholder_ExportSnippet_HasCodeSnippet_NoAdditional_Functions_Start, Placeholder_ExportSnippet_HasCodeSnippet_NoAdditional_Functions_End, async (m) =>
            {
                string snippetName = m.Groups[1].Value;
                snippetName        = snippetName.ToLowerInvariant();

                return(!(await HasCodeSnippetAdditionalFunctions(renderedFunctions, exportSnippets.FirstOrDefault(e => e.SnippetName.ToLowerInvariant() == snippetName), flexFieldObject)));
            });

            code = await ExportUtil.BuildPlaceholderRegex(Placeholder_ExportSnippet_AdditionalFuntions, ExportConstants.ListIndentPrefix).ReplaceAsync(code, async(m) =>
            {
                string snippetName = m.Groups[2].Value;
                snippetName        = snippetName.ToLowerInvariant();

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

            ValidateAllExportSnippetsUsed(usedExportSnippets, exportSnippets);

            return(code);
        }
        /// <summary>
        /// Builds a condition element from parsed data
        /// </summary>
        /// <param name="parsedData">Parsed data</param>
        /// <param name="project">Project</param>
        /// <param name="errorCollection">Error Collection</param>
        /// <param name="flexFieldObject">Flex field object to which the dialog belongs</param>
        /// <param name="exportSettings">Export Settings</param>
        /// <returns>Condition string</returns>
        public override string BuildConditionElementFromParsedData(ValueConditionResolverBase.ValueFieldConditionData parsedData, GoNorthProject project, ExportPlaceholderErrorCollection errorCollection, FlexFieldObject flexFieldObject, ExportSettings exportSettings)
        {
            ExportTemplate       conditionTemplate = GetExportTemplate(project);
            IFlexFieldExportable valueObject       = GetValueObject(parsedData, flexFieldObject, errorCollection);

            if (valueObject == null)
            {
                return(string.Empty);
            }

            FlexField exportField = DialogFlexFieldUtil.GetFlexField(valueObject, parsedData.FieldId);

            if (exportField == null)
            {
                errorCollection.AddErrorFlexField(parsedData.FieldName, valueObject.Name);
                return(string.Empty);
            }

            string conditionCode = conditionTemplate.Code;

            conditionCode = ExportUtil.RenderPlaceholderIfTrue(conditionCode, Placeholder_IsOperatorPrimitiveStart, Placeholder_IsOperatorPrimitiveEnd, IsOperatorPrimitiveOperator(parsedData.Operator));
            conditionCode = ExportUtil.RenderPlaceholderIfTrue(conditionCode, Placeholder_IsOperatorNonPrimitiveStart, Placeholder_IsOperatorNonPrimitiveEnd, !IsOperatorPrimitiveOperator(parsedData.Operator));
            conditionCode = ExportUtil.BuildPlaceholderRegex(Placeholder_ValueName).Replace(conditionCode, m => {
                return(DialogFlexFieldUtil.GetFieldName(exportField, parsedData.FieldName, errorCollection));
            });
            conditionCode = ExportUtil.BuildPlaceholderRegex(Placeholder_Operator).Replace(conditionCode, GetOperatorFromTemplate(project, parsedData.Operator, errorCollection));
            conditionCode = ExportUtil.RenderPlaceholderIfTrue(conditionCode, Placeholder_ValueIsString_Start, Placeholder_ValueIsString_End, exportField.FieldType != ExportConstants.FlexFieldType_Number);
            conditionCode = ExportUtil.RenderPlaceholderIfTrue(conditionCode, Placeholder_ValueIsNumber_Start, Placeholder_ValueIsNumber_End, exportField.FieldType == ExportConstants.FlexFieldType_Number);
            conditionCode = ExportUtil.RenderPlaceholderIfFuncTrue(conditionCode, Placeholder_ValueNameEquals_Start, Placeholder_ValueNameEquals_End, m => {
                string searchedFieldName = m.Groups[1].Value;
                searchedFieldName        = searchedFieldName.ToLowerInvariant();
                string fieldName         = string.Empty;
                if (!string.IsNullOrEmpty(exportField.Name))
                {
                    fieldName = exportField.Name.ToLowerInvariant();
                }
                return(fieldName == searchedFieldName);
            });
            conditionCode = ExportUtil.RenderPlaceholderIfFuncTrue(conditionCode, Placeholder_ValueNameNotEquals_Start, Placeholder_ValueNameNotEquals_End, m => {
                string searchedFieldName = m.Groups[1].Value;
                searchedFieldName        = searchedFieldName.ToLowerInvariant();
                string fieldName         = string.Empty;
                if (!string.IsNullOrEmpty(exportField.Name))
                {
                    fieldName = exportField.Name.ToLowerInvariant();
                }
                return(fieldName != searchedFieldName);
            });
            conditionCode = ExportUtil.BuildPlaceholderRegex(Placeholder_CompareValue).Replace(conditionCode, m => {
                if (exportField.FieldType != ExportConstants.FlexFieldType_Number)
                {
                    return(ExportUtil.EscapeCharacters(parsedData.CompareValue, exportSettings.EscapeCharacter, exportSettings.CharactersNeedingEscaping, exportSettings.NewlineCharacter));
                }

                return(parsedData.CompareValue);
            });

            ExportObjectData flexFieldExportData = new ExportObjectData();

            flexFieldExportData.ExportData.Add(ExportConstants.ExportDataObject, valueObject);
            flexFieldExportData.ExportData.Add(ExportConstants.ExportDataObjectType, GetFlexFieldExportObjectType());

            _flexFieldPlaceholderResolver.SetErrorMessageCollection(errorCollection);
            conditionCode = _flexFieldPlaceholderResolver.FillPlaceholders(conditionCode, flexFieldExportData).Result;

            return(conditionCode);
        }
Esempio n. 3
0
        /// <summary>
        /// Fills the Flex Field Placeholders
        /// </summary>
        /// <param name="code">Code to fill</param>
        /// <param name="flexFieldObject">Flex Field Object</param>
        /// <param name="objectType">Object Type</param>
        /// <returns>Filled Code</returns>
        private async Task <string> FillFlexFieldPlaceholders(string code, IFlexFieldExportable flexFieldObject, string objectType)
        {
            GoNorthProject project = await _exportCachedDbAccess.GetDefaultProject();

            ExportSettings exportSettings = await _exportCachedDbAccess.GetExportSettings(project.Id);

            ExportTemplate attributeListTemplate = await _defaultTemplateProvider.GetDefaultTemplateByType(project.Id, TemplateType.ObjectAttributeList);

            HashSet <string> usedFields = new HashSet <string>();

            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_Name)).Replace(code, flexFieldObject.Name);
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_Field_Value_Equals_Start), BuildFinalPlaceholder(Placeholder_Field_Value_Equals_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field == null)
                {
                    return(false);
                }
                return(m.Groups[2].Value == field.Value);
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_Field_Value_NotEquals_Start), BuildFinalPlaceholder(Placeholder_Field_Value_NotEquals_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field == null)
                {
                    return(true);
                }
                return(m.Groups[2].Value != field.Value);
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_FlexField_HasField_Start), BuildFinalPlaceholder(Placeholder_FlexField_HasField_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                return(field != null);
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_FlexField_NotHasField_Start), BuildFinalPlaceholder(Placeholder_FlexField_NotHasField_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                return(field == null);
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_FlexField_FieldIsBlank_Start), BuildFinalPlaceholder(Placeholder_FlexField_FieldIsBlank_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field == null)
                {
                    return(true);
                }

                return(string.IsNullOrEmpty(field.Value));
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_FlexField_FieldIsNotBlank_Start), BuildFinalPlaceholder(Placeholder_FlexField_FieldIsNotBlank_End), m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field == null)
                {
                    return(false);
                }

                return(!string.IsNullOrEmpty(field.Value));
            });
            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_Name_LangKey)).Replace(code, m => {
                // Run in function to only create language key if required
                string key = _languageKeyGenerator.GetFlexFieldNameKey(flexFieldObject.Id, flexFieldObject.Name, objectType).Result;
                return(key);
            });
            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_Field_Value)).Replace(code, m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field != null)
                {
                    usedFields.Add(field.Id);
                    return(EscapedFieldValue(field, exportSettings));
                }

                _errorCollection.AddErrorFlexField(fieldName, flexFieldObject.Name);
                return("<<" + flexFieldObject.Name + "[" + fieldName + "] MISSING>>");
            });
            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_Field_LangKey)).Replace(code, m => {
                string fieldName = m.Groups[1].Value;
                FlexField field  = FindFlexField(flexFieldObject, fieldName);
                if (field != null && (field.FieldType == ExportConstants.FlexFieldType_String || field.FieldType == ExportConstants.FlexFieldType_Option))
                {
                    usedFields.Add(field.Id);
                    return(BuildFlexFieldLangKey(flexFieldObject, field, objectType));
                }

                _errorCollection.AddErrorFlexField(fieldName, flexFieldObject.Name);
                return("<<" + flexFieldObject.Name + "[" + fieldName + "] MISSING>>");
            });

            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_UnusedFields), ExportConstants.ListIndentPrefix).Replace(code, m => {
                return(BuildFlexFieldListTemplate(attributeListTemplate.Code, Placeholder_UnusedFields_Start, Placeholder_UnusedFields_End, m.Groups[1].Value));
            });
            code = ExportUtil.BuildPlaceholderRegex(BuildFinalPlaceholder(Placeholder_AllFields), ExportConstants.ListIndentPrefix).Replace(code, m => {
                return(BuildFlexFieldListTemplate(attributeListTemplate.Code, Placeholder_AllFields_Start, Placeholder_AllFields_End, m.Groups[1].Value));
            });
            code = ExportUtil.RenderPlaceholderIfFuncTrue(code, BuildFinalPlaceholder(Placeholder_HasUnusedFields_Start), BuildFinalPlaceholder(Placeholder_HasUnusedFields_End), m => {
                return(flexFieldObject.Fields.Where(f => !usedFields.Contains(f.Id)).Any());
            });
            code = ExportUtil.BuildRangePlaceholderRegex(BuildFinalPlaceholder(Placeholder_UnusedFields_Start), BuildFinalPlaceholder(Placeholder_UnusedFields_End)).Replace(code, m => {
                List <FlexField> fieldsToUse = flexFieldObject.Fields.Where(f => !usedFields.Contains(f.Id)).ToList();
                return(BuildFlexFieldList(m.Groups[1].Value, fieldsToUse, flexFieldObject, exportSettings, objectType));
            });
            code = ExportUtil.BuildRangePlaceholderRegex(BuildFinalPlaceholder(Placeholder_AllFields_Start), BuildFinalPlaceholder(Placeholder_AllFields_End)).Replace(code, m => {
                return(BuildFlexFieldList(m.Groups[1].Value, flexFieldObject.Fields, flexFieldObject, exportSettings, objectType));
            });

            return(code);
        }