Exemple #1
0
        public static void ParseJson(string filepath, GenerateForm genForm,
                                     CheckBoxAction checkBoxAction, TextFieldAction textFieldAction)
        {
            using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
            {
                IDictionary <String, BookmarkStart> bookmarkMap = new Dictionary <String, BookmarkStart>();
                foreach (BookmarkStart bookmarkStart in wordprocessingDocument.MainDocumentPart.RootElement.Descendants <BookmarkStart>())
                {
                    bookmarkMap[bookmarkStart.Name] = bookmarkStart;
                }

                foreach (BookmarkStart bookmarkStart in bookmarkMap.Values)
                {
                    Run bookmarkFieldCode = bookmarkStart.NextSibling <Run>();
                    if (bookmarkFieldCode != null)
                    {
                        FieldCode fcode = bookmarkFieldCode.GetFirstChild <FieldCode>();
                        if (fcode != null && FormTypes.isFormType(fcode.Text))
                        {
                            if (FormTypes.FormCheckBox.Is(fcode.Text))
                            {
                                Run           checkboxRun   = bookmarkStart.PreviousSibling <Run>();
                                FieldChar     fieldChar     = checkboxRun?.GetFirstChild <FieldChar>();
                                FormFieldData formFieldData = fieldChar?.GetFirstChild <FormFieldData>();
                                CheckBox      checkbox      = formFieldData?.GetFirstChild <CheckBox>();
                                //Note: docs say Checked should appear however type is DefaultCheckBoxFormFieldState
                                //Checked checkboxChecked =  checkbox?.GetFirstChild<Checked>();
                                DefaultCheckBoxFormFieldState checkboxChecked = checkbox?.GetFirstChild <DefaultCheckBoxFormFieldState>();
                                if (checkboxChecked != null)
                                {
                                    PrintVerbose("" + (bool)checkboxChecked.Val);
                                    checkBoxAction(genForm, bookmarkStart.Name, checkboxChecked);
                                }
                            }
                            else if (FormTypes.FormText.Is(fcode.Text))
                            {
                                while (bookmarkFieldCode.NextSibling <Run>() != null)
                                {
                                    Text bookmarkText = bookmarkFieldCode.GetFirstChild <Text>();
                                    if (bookmarkText != null)
                                    {
                                        PrintVerbose(bookmarkText.Text);
                                        textFieldAction(genForm, bookmarkStart.Name, bookmarkText);
                                    }
                                    bookmarkFieldCode = bookmarkFieldCode.NextSibling <Run>();
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// This method called when user navigated to this page.
        /// </summary>
        /// <param name="e"></param>
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);

            progressRing.IsActive = true;

            // If UserInfo is null, then initialize the helper
            if (CRMHelper.UserInfo == null)
            {
                await CRMHelper.Initialize();
            }

            // Check if there is any temporaryData. if restored, then temporary data does not exist
            object[] parameters = CRMHelper.temporaryData as object[];
            if (parameters != null)
            {
                // if this if first navigation, then no formFieldData, otherwise its restored or back from new record
                if (formFieldData == null)
                {
                    this.formFieldData = parameters[0] as FormFieldData;
                }
                else // assume back after created new record or back just by clicking cancel button.
                {
                    Entity record = parameters[0] as Entity;
                    // If record is created, then filter result down to the created record.
                    if (record != null)
                    {
                        searchCriteria = record[lookForEntities.
                                                Where(x => x.EntityMetadata.LogicalName == record.LogicalName).First().EntityMetadata.PrimaryNameAttribute].ToString();
                    }
                }
            }

            //if restored, then temporary data does not exist
            if (lookForEntities == null || lookForEntities.Count() == 0)
            {
                List <EntityMetadataEx> targetList = new List <EntityMetadataEx>();
                targetList.AddRange(CRMHelper.EntityMetadataExCollection.Where(x => (parameters[1] as string[]).Contains(x.EntityMetadata.LogicalName)));
                foreach (var target in targetList)
                {
                    lookForEntities.Add(target);
                }
            }

            Initialize();

            progressRing.IsActive = false;
        }
 /// <summary>
 /// This method called when page is loaded to restore state
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
 {
     if (e.PageState != null && e.PageState.ContainsKey("formFieldData"))
     {
         formFieldData = e.PageState["formFieldData"] as FormFieldData;
     }
     if (e.PageState != null && e.PageState.ContainsKey("lookForEntities"))
     {
         lookForEntities = e.PageState["lookForEntities"] as EntityMetadataExCollection;
     }
     if (e.PageState != null && e.PageState.ContainsKey("cbLookFor_SelectedIndex"))
     {
         cbLookFor_SelectedIndex = int.Parse(e.PageState["cbLookFor_SelectedIndex"].ToString());
     }
     if (e.PageState != null && e.PageState.ContainsKey("cbLookIn_SelectedIndex"))
     {
         cbLookIn_SelectedIndex = int.Parse(e.PageState["cbLookIn_SelectedIndex"].ToString());
     }
 }
        /// <summary>
        /// This method called when user navigated to this page.
        /// </summary>
        /// <param name="e"></param>
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);

            progressRing.IsActive = true;

            // If UserInfo is null, then initialize the helper
            if (CRMHelper.UserInfo == null)
            {
                await CRMHelper.Initialize();
            }

            if (fields == null) // If fields are not yet has value, means just navigated to this page
            {
                // Check if there is any temporaryData
                object[] parameters = CRMHelper.temporaryData as object[];
                if (parameters.Count() != 3)
                {
                    new Exception("RecordModifyPage needs 3 parameters. Record, Fields, EntityMetadataEx");
                }
                this.record           = parameters[0] as Entity;               // record for update/related new
                this.fields           = parameters[1] as List <FormFieldData>; // form fields
                this.entityMetadataEx = parameters[2] as EntityMetadataEx;     // target entity
            }
            else // It has been restored of page back from lookup page
            {
                // Assign lookup value when retruning from lookup page.
                if (CRMHelper.temporaryData != null &&
                    CRMHelper.temporaryData.GetType().Equals(typeof(FormFieldData)))
                {
                    FormFieldData formfieldData = CRMHelper.temporaryData as FormFieldData;
                    fields.Where(x => x.FieldMetadata.LogicalName == formfieldData.FieldMetadata.LogicalName).First().FieldData = formfieldData.FieldData;
                }
            }

            // Then initialize page
            Initialize();

            progressRing.IsActive = false;
        }
        public void TestSimpleChoice3()
        {
            ValidationContext validationContext = new ValidationContext();
            OpenXmlElement    errorChild;

            FormFieldData ffData             = new FormFieldData();
            var           particleConstraint = ffData.ParticleConstraint.Build(Version);
            var           target             = particleConstraint.ParticleValidator as ChoiceParticleValidator;

            validationContext.Element = ffData;
            var expected = ffData;

            //<xs:complexType name="CT_FFData">
            //  <xs:choice maxOccurs="unbounded">
            //    <xs:element name="name" type="CT_FFName">
            //    <xs:element name="enabled" type="CT_OnOff">
            //    <xs:element name="calcOnExit" type="CT_OnOff">
            //    <xs:element name="entryMacro" type="CT_MacroName" minOccurs="0" maxOccurs="1">
            //    <xs:element name="exitMacro" type="CT_MacroName" minOccurs="0" maxOccurs="1">
            //    <xs:element name="helpText" type="CT_FFHelpText" minOccurs="0" maxOccurs="1">
            //    <xs:element name="statusText" type="CT_FFStatusText" minOccurs="0" maxOccurs="1">
            //    <xs:choice>
            //      <xs:element name="checkBox" type="CT_FFCheckBox">
            //      <xs:element name="ddList" type="CT_FFDDList">
            //      <xs:element name="textInput" type="CT_FFTextInput">
            //    </xs:choice>
            //  </xs:choice>
            //</xs:complexType>

            // ***** good case ******

            // empty is ok, as there are child with minOccurs="0"
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            ffData.AppendChild(new FormFieldName());
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            // dup element
            ffData.AppendChild(new FormFieldName());
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            ffData.RemoveAllChildren();
            ffData.AppendChild(new StatusText());
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            // 2 element
            ffData.AppendChild(new HelpText());
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            // dup element
            ffData.AppendChild(new HelpText());
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            // more element
            ffData.AppendChild(new Enabled());
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            // more element
            ffData.AppendChild(new CheckBox());
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            // more element
            ffData.AppendChild(new TextInput());
            target.Validate(validationContext);
            Assert.True(validationContext.Valid);

            // ***** error case ******
            ffData.RemoveAllChildren();
            ffData.AppendChild(new HelpText());
            ffData.AppendChild(new CheckBox());

            //first is invalid
            errorChild = ffData.PrependChild(new Paragraph());
            target.Validate(validationContext);
            Assert.False(validationContext.Valid);
            Assert.Single(validationContext.Errors);
            Assert.Same(expected, validationContext.Errors[0].Node);
            Assert.Same(errorChild, validationContext.Errors[0].RelatedNode);
            Assert.Equal(ValidationErrorType.Schema, validationContext.Errors[0].ErrorType);
            Assert.Equal("Sch_InvalidElementContentExpectingComplex", validationContext.Errors[0].Id);
            Assert.Contains(":name", validationContext.Errors[0].Description);
            Assert.Contains(":textInput", validationContext.Errors[0].Description);

            validationContext.Clear();

            //invalid child in middle
            ffData.RemoveChild(errorChild);
            errorChild = ffData.InsertBefore(new Paragraph(), ffData.LastChild);
            target.Validate(validationContext);
            Assert.False(validationContext.Valid);
            Assert.Single(validationContext.Errors);
            Assert.Same(expected, validationContext.Errors[0].Node);
            Assert.Same(errorChild, validationContext.Errors[0].RelatedNode);
            Assert.Equal(ValidationErrorType.Schema, validationContext.Errors[0].ErrorType);
            Assert.Equal("Sch_InvalidElementContentExpectingComplex", validationContext.Errors[0].Id);
            Assert.Contains(":name", validationContext.Errors[0].Description);
            Assert.Contains(":textInput", validationContext.Errors[0].Description);

            validationContext.Clear();

            //invalid child in last
            ffData.RemoveChild(errorChild);
            errorChild = ffData.AppendChild(new Paragraph());
            target.Validate(validationContext);
            Assert.False(validationContext.Valid);
            Assert.Single(validationContext.Errors);
            Assert.Same(expected, validationContext.Errors[0].Node);
            Assert.Same(errorChild, validationContext.Errors[0].RelatedNode);
            Assert.Equal(ValidationErrorType.Schema, validationContext.Errors[0].ErrorType);
            Assert.Equal("Sch_InvalidElementContentExpectingComplex", validationContext.Errors[0].Id);
            Assert.Contains(":name", validationContext.Errors[0].Description);
            Assert.Contains(":textInput", validationContext.Errors[0].Description);
        }
        public void CompositeElementGetSetChildrenTest()
        {
            // test case for OpenXmlCompositeType.OneSequence
            RunProperties target = new RunProperties();
            Bold          bold   = new Bold();

            target.Bold = bold;

            Assert.Same(bold, target.Bold);
            Assert.Same(bold, target.FirstChild);

            Italic italic = new Italic();

            target.AppendChild(italic);
            Assert.Same(bold, target.Bold);
            Assert.Same(italic, target.Italic);
            Assert.Same(italic, target.LastChild);

            OpenXmlMiscNode miscNod = new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, @"<!-- my comment --> ");

            target.InsertAfter(miscNod, italic);
            Assert.Same(bold, target.Bold);
            Assert.Same(italic, target.Italic);
            Assert.Same(miscNod, target.LastChild);

            Caps caps = new Caps();

            target.Caps = caps;
            Assert.Same(bold, target.Bold);
            Assert.Same(italic, target.Italic);
            Assert.Same(caps, target.Caps);
            Assert.Same(miscNod, target.LastChild);

            Shadow shadow = new Shadow();

            target.InsertAfter(shadow, miscNod);
            Assert.Same(bold, target.Bold);
            Assert.Same(italic, target.Italic);
            Assert.Same(caps, target.Caps);
            Assert.Same(shadow, target.Shadow);
            Assert.Same(shadow, target.LastChild);

            // test case for OpenXmlCompositeType.OneChoice
            FieldChar fieldChar = new FieldChar();

            FieldData fldData = new FieldData();

            fieldChar.FieldData = fldData;
            Assert.Same(fldData, fieldChar.FieldData);

            FormFieldData ffData = new FormFieldData();

            fieldChar.FormFieldData = ffData;
            Assert.Same(ffData, fieldChar.FormFieldData);
            Assert.Null(fieldChar.FieldData);

            miscNod = new OpenXmlMiscNode(System.Xml.XmlNodeType.Comment, @"<!-- OpenXmlCompositeType.OneChoice --> ");
            fieldChar.PrependChild(miscNod);
            Assert.Same(miscNod, fieldChar.FirstChild);
            Assert.Same(ffData, fieldChar.LastChild);
            Assert.Same(ffData, fieldChar.FormFieldData);
            Assert.Null(fieldChar.FieldData);

            NumberingChange numChange = new NumberingChange();

            fieldChar.NumberingChange = numChange;
            Assert.Same(miscNod, fieldChar.FirstChild);
            Assert.Same(numChange, fieldChar.LastChild);
            Assert.Same(numChange, fieldChar.NumberingChange);
            Assert.Null(fieldChar.FieldData);
        }
Exemple #7
0
        public ApiResult <bool> SaveParams(FormFieldData model)
        {
            var fields = _context.DynamicFormFieldInfos.
                         Where(c => c.FormId == model.Id && c.FieldStatus > -1).ToList();

            if (fields != null)
            {
                var typeinfo = _context.BaseDictionaries.Where(c => c.DictionaryGroupId == 2 && c.DictionaryStatus > 0)
                               .ToList();
                _context.DynamicFormFieldInfos.Where(c => c.FormId == model.Id &&
                                                     c.FieldStatus > -1).ToList()
                .ForEach(x =>
                {
                    if (model.propdata.All(c => c.FieldId != x.FieldId))
                    {
                        x.FieldStatus = -1;
                        _context.DynamicFormFieldInfos.Update(x);
                        _context.SaveChanges();
                    }
                });
                foreach (var item in model.propdata)
                {
                    var field =
                        fields.FirstOrDefault(c => c.FieldId == item.FieldId);
                    if (field != null)
                    {
                        field.FieldStatus       = 1;
                        field.FieldPocoTypeName = typeinfo
                                                  .FirstOrDefault(c => c.DictionaryValue == item.FieldValueType.ToString())?.DictionaryTag;
                        field.FieldCode            = item.FieldCode;
                        field.FieldName            = item.FieldName;
                        field.IsRequired           = item.IsRequired;
                        field.FieldValue           = item.FieldValue;
                        field.FieldValueType       = item.FieldValueType;
                        field.FieldMaxLength       = item.FieldMaxLength;
                        field.FieldI18nKey         = item.FieldI18nKey;
                        field.FieldPattern         = item.FieldPattern;
                        field.FieldValueDataSource = item.FieldValueDataSource;
                        field.FieldUIElementSchema = item.FieldUIElementSchema;
                        field.FieldUIElement       = item.FieldUIElement;
                        field.FieldUnit            = item.FieldUnit;

                        field.FieldCreateDate = DateTime.Now;

                        switch (item.FieldUIElement)
                        {
                        case 1:
                            field.FieldValueTypeName = "number";
                            break;

                        case 5:
                            field.FieldValueTypeName = "boolean";
                            break;

                        case 16:
                            field.FieldValueType     = 14;
                            field.FieldValueTypeName = "number";
                            break;

                        case 19:
                            field.FieldValueTypeName = "number";
                            break;

                        default:
                            field.FieldValueTypeName = "string";
                            break;
                        }

                        _context.DynamicFormFieldInfos.Update(field);
                        _context.SaveChanges();
                    }
                    else
                    {
                        field = new DynamicFormFieldInfo()
                        {
                            FieldCode         = item.FieldCode,
                            FieldId           = item.FieldId,
                            FieldStatus       = 1,
                            IsRequired        = item.IsRequired,
                            FormId            = model.Id,
                            FieldPocoTypeName = typeinfo
                                                .FirstOrDefault(c => c.DictionaryValue == item.FieldValueType.ToString())?.DictionaryTag,
                            FieldName            = item.FieldName,
                            FieldValue           = item.FieldValue,
                            FieldValueType       = item.FieldValueType,
                            FieldMaxLength       = item.FieldMaxLength,
                            FieldI18nKey         = item.FieldI18nKey,
                            FieldPattern         = item.FieldPattern,
                            FieldValueDataSource = item.FieldValueDataSource,
                            FieldUIElementSchema = item.FieldUIElementSchema,
                            FieldUIElement       = item.FieldUIElement,
                            FieldUnit            = item.FieldUnit
                        };
                        switch (item.FieldUIElement)
                        {
                        case 1:
                            field.FieldValueTypeName = "number";
                            break;

                        case 5:
                            field.FieldValueTypeName = "boolean";
                            break;

                        case 19:
                            field.FieldValueTypeName = "number";
                            break;

                        default:
                            field.FieldValueTypeName = "string";
                            break;
                        }
                        _context.DynamicFormFieldInfos.Add(field);
                        _context.SaveChanges();
                    }
                }
                var allfields = _context.DynamicFormFieldInfos.Where(c => c.FormId == model.Id &&
                                                                     c.FieldStatus > -1).ToList();

                // 土味代码生成
                StringBuilder builder = new StringBuilder("public class FormData" + model.Id + "{\n");
                allfields.ForEach(x =>
                {
                    builder.Append("public ").Append(Type.GetType(x.FieldPocoTypeName)?.Name).Append(" ").Append(x.FieldCode)
                    .Append("{ get; set;}\n");
                });
                builder.Append("}");
                var form = _context.DynamicFormInfos.FirstOrDefault(c => c.FormId == model.Id);
                form.ModelClass = builder.ToString();
                _context.DynamicFormInfos.Update(form);
                _context.SaveChanges();

                return(new ApiResult <bool>(ApiCode.Success, "OK", true));
            }
            return(new ApiResult <bool>(ApiCode.CantFindObject, "can't find any fields", false));
        }
        private void TestSimpleChoice3(SdbSchemaDatas sdbSchemaDatas)
        {
            ValidationContext validationContext = new ValidationContext();
            ValidationResult actual = new ValidationResult();
            validationContext.ValidationErrorEventHandler += actual.OnValidationError;
            OpenXmlElement errorChild;

            FormFieldData ffData = new FormFieldData();
            var particleConstraint = sdbSchemaDatas.GetSchemaTypeData(ffData).ParticleConstraint;
            var target = particleConstraint.ParticleValidator as ChoiceParticleValidator;
            validationContext.Element = ffData;
            var expected = ffData;
            
            //<xs:complexType name="CT_FFData">
            //  <xs:choice maxOccurs="unbounded">
            //    <xs:element name="name" type="CT_FFName">
            //    <xs:element name="enabled" type="CT_OnOff">
            //    <xs:element name="calcOnExit" type="CT_OnOff">
            //    <xs:element name="entryMacro" type="CT_MacroName" minOccurs="0" maxOccurs="1">
            //    <xs:element name="exitMacro" type="CT_MacroName" minOccurs="0" maxOccurs="1">
            //    <xs:element name="helpText" type="CT_FFHelpText" minOccurs="0" maxOccurs="1">
            //    <xs:element name="statusText" type="CT_FFStatusText" minOccurs="0" maxOccurs="1">
            //    <xs:choice>
            //      <xs:element name="checkBox" type="CT_FFCheckBox">
            //      <xs:element name="ddList" type="CT_FFDDList">
            //      <xs:element name="textInput" type="CT_FFTextInput">
            //    </xs:choice>
            //  </xs:choice>
            //</xs:complexType>

            // ***** good case ******

            // empty is ok, as there are child with minOccurs="0"
            target.Validate(validationContext);
            Assert.True(actual.Valid);
  
            ffData.AppendChild(new FormFieldName());
            target.Validate(validationContext);
            Assert.True(actual.Valid);

            // dup element
            ffData.AppendChild(new FormFieldName());
            target.Validate(validationContext);
            Assert.True(actual.Valid);

            ffData.RemoveAllChildren();
            ffData.AppendChild(new StatusText());
            target.Validate(validationContext);
            Assert.True(actual.Valid);

            // 2 element
            ffData.AppendChild(new HelpText());
            target.Validate(validationContext);
            Assert.True(actual.Valid);

            // dup element
            ffData.AppendChild(new HelpText());
            target.Validate(validationContext);
            Assert.True(actual.Valid);

            // more element
            ffData.AppendChild(new Enabled());
            target.Validate(validationContext);
            Assert.True(actual.Valid);

            // more element
            ffData.AppendChild(new CheckBox());
            target.Validate(validationContext);
            Assert.True(actual.Valid);

            // more element
            ffData.AppendChild(new TextInput());
            target.Validate(validationContext);
            Assert.True(actual.Valid);
            
            // ***** error case ******
            ffData.RemoveAllChildren();
            ffData.AppendChild(new HelpText());
            ffData.AppendChild(new CheckBox());

            //first is invalid
            errorChild = ffData.PrependChild(new Paragraph());
            target.Validate(validationContext);
            Assert.False(actual.Valid);
            Assert.Equal(1, actual.Errors.Count);
            Assert.Same(expected, actual.Errors[0].Node);
            Assert.Same(errorChild, actual.Errors[0].RelatedNode);
            Assert.Equal(ValidationErrorType.Schema, actual.Errors[0].ErrorType);
            Assert.Equal("Sch_InvalidElementContentExpectingComplex", actual.Errors[0].Id);
            Assert.True(actual.Errors[0].Description.Contains(":name"));
            Assert.True(actual.Errors[0].Description.Contains(":textInput"));

            actual.Clear();
            //invalid child in middle
            ffData.RemoveChild(errorChild);
            errorChild = ffData.InsertBefore(new Paragraph(), ffData.LastChild);
            target.Validate(validationContext);
            Assert.False(actual.Valid);
            Assert.Equal(1, actual.Errors.Count);
            Assert.Same(expected, actual.Errors[0].Node);
            Assert.Same(errorChild, actual.Errors[0].RelatedNode);
            Assert.Equal(ValidationErrorType.Schema, actual.Errors[0].ErrorType);
            Assert.Equal("Sch_InvalidElementContentExpectingComplex", actual.Errors[0].Id);
            Assert.True(actual.Errors[0].Description.Contains(":name"));
            Assert.True(actual.Errors[0].Description.Contains(":textInput"));

            actual.Clear();
            //invalid child in last
            ffData.RemoveChild(errorChild);
            errorChild = ffData.AppendChild(new Paragraph());
            target.Validate(validationContext);
            Assert.False(actual.Valid);
            Assert.Equal(1, actual.Errors.Count);
            Assert.Same(expected, actual.Errors[0].Node);
            Assert.Same(errorChild, actual.Errors[0].RelatedNode);
            Assert.Equal(ValidationErrorType.Schema, actual.Errors[0].ErrorType);
            Assert.Equal("Sch_InvalidElementContentExpectingComplex", actual.Errors[0].Id);
            Assert.True(actual.Errors[0].Description.Contains(":name"));
            Assert.True(actual.Errors[0].Description.Contains(":textInput"));
        }
Exemple #9
0
        private void UpdateFormField(FormFieldData field, string value)
        {
            var text = field.Descendants <TextInput>().First();

            WordHelpers.SetFormFieldValue(text, value);
        }