예제 #1
0
        /// <summary>
        /// Gets a list of selected Dynamics365Field objects.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="provider">The provider.</param>
        /// <param name="value">The value.</param>
        /// <returns>The selected objects.</returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            IDynamics365EntityFieldsProvider fieldsProvider = (IDynamics365EntityFieldsProvider)context.Instance;
            List <Dynamics365Field>          fields         = fieldsProvider.GetDynamics365EntityFields();

            BindingList <Dynamics365Field> currentValue = (BindingList <Dynamics365Field>)value;

            checkedListBox.Items.Clear();
            checkedListBox.Items.Add("Select All", false);
            checkedListBox.ItemCheck += CheckedListBox_ItemCheck;

            fields.ForEach(field => checkedListBox.Items.Add(field, currentValue.Contains(field)));

            IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            service.DropDownControl(checkedListBox);

            BindingList <Dynamics365Field> checkedFields = new BindingList <Dynamics365Field>();

            for (int itemIndex = 0; itemIndex < checkedListBox.CheckedIndices.Count; itemIndex++)
            {
                if (checkedListBox.CheckedIndices[itemIndex] != 0)
                {
                    Dynamics365Field field = (Dynamics365Field)checkedListBox.CheckedItems[itemIndex];
                    checkedFields.Add(field);
                }
            }

            return(checkedFields);
        }
예제 #2
0
        private void GenerateFieldMappings(CancellationToken cancel, IProgress <ExecutionProgress> progress)
        {
            progress?.Report(new ExecutionProgress(NotificationType.Information, Properties.Resources.Dynamics365FieldOperationGenerateMappingsReadingDataSourceFields));
            List <DataTableField> sourceFields = DataTableField.GetDataTableFields(DataSource?.GetDataColumns());

            cancel.ThrowIfCancellationRequested();

            progress?.Report(new ExecutionProgress(NotificationType.Information, Properties.Resources.Dynamics365FieldOperationGenerateMappingsReadingEntityFields));
            List <Field> destinationFields = Entity.GetFields(Connection).Where(field => ((Dynamics365Field)field).CanUpdate || ((Dynamics365Field)field).CanCreate).ToList();

            cancel.ThrowIfCancellationRequested();

            values?.Clear();

            foreach (DataTableField sourceField in sourceFields)
            {
                // Formats to match:
                // fieldLogicalName
                // entityLogicalName.fieldLogicalName
                // entityLogicalName.fieldLogicalName.Identifier
                // entityLogicalName.fieldLogicalName.Code
                // FieldDisplayName
                // FieldDisplayName (<other text>)
                // FieldDisplayName (Identifier)
                // FieldDisplayName (Code)

                string strippedColumnName = sourceField.ColumnName;

                if (strippedColumnName.EndsWith(".Identifier") || strippedColumnName.EndsWith(".Code"))
                {
                    strippedColumnName = strippedColumnName.Substring(0, strippedColumnName.LastIndexOf("."));
                }
                else if (strippedColumnName.EndsWith(" (Identifier)") || strippedColumnName.EndsWith(" (Code)"))
                {
                    strippedColumnName = strippedColumnName.Substring(0, strippedColumnName.LastIndexOf(" "));
                }

                // todo - also add option set support if just name is supplied
                Dynamics365Field destinationField = (Dynamics365Field)destinationFields.Find(field =>
                                                                                             strippedColumnName == ((Dynamics365Field)field).LogicalName ||
                                                                                             strippedColumnName == string.Concat(((Dynamics365Field)field).EntityLogicalName, ".", ((Dynamics365Field)field).LogicalName) ||
                                                                                             strippedColumnName == ((Dynamics365Field)field).DisplayName);

                if (destinationField != default(Dynamics365Field))
                {
                    // todo - use other fields ('name', 'entity') to correct set target and possibly create lookup data field
                    values.Add(new Dynamics365DataSourceValue(this)
                    {
                        DestinationField = destinationField,
                        SourceField      = sourceField
                    });
                }

                cancel.ThrowIfCancellationRequested();
            }

            progress?.Report(new ExecutionProgress(NotificationType.Information, string.Format(Properties.Resources.Dynamics365FieldOperationGenerateMappingsSuccessful, values.Count)));
        }
        public virtual List <Dynamics365Field> GetDynamics365EntityFields()
        {
            List <Dynamics365Field> fields = new List <Dynamics365Field>();

            try
            {
                fields.AddRange(Dynamics365Field.GetFields(Entity, Connection));
            }
            catch { }

            return(fields);
        }
        public List <Dynamics365Field> GetDynamics365EntityFields()
        {
            List <Dynamics365Field> fields = new List <Dynamics365Field>();

            try
            {
                fields.AddRange(Dynamics365Field.GetFields(Entity, (Dynamics365Connection)Parent)); // todo - should call entity.getfields??
            }
            catch { }

            return(fields);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value != default(object))
            {
                Dynamics365Field field = (Dynamics365Field)value;

                if (context?.Instance != default(object))
                {
                    IDataDestinationFieldsProvider provider = (IDataDestinationFieldsProvider)context.Instance;
                    List <Field>     fields       = provider.GetDataDestinationFields();
                    Dynamics365Field renamedField = (Dynamics365Field)fields.FirstOrDefault(f => ((Dynamics365Field)f).LogicalName == field.LogicalName);

                    if (renamedField.DisplayName != field.DisplayName)
                    {
                        field.DisplayName = renamedField.DisplayName;
                    }
                }

                return(string.Format("{0} ({1})", field.DisplayName, field.LogicalName));
            }

            return(null);
        }
        /// <summary>
        /// Gets a list of fields in an entity or link-entity node of a FetchXML query.
        /// </summary>
        /// <param name="entityNode">The entity node.</param>
        /// <returns>The fields.</returns>
        private List <Dynamics365Field> GetEntityNodeFields(XmlNode entityNode)
        {
            List <Dynamics365Field> fields         = new List <Dynamics365Field>();
            Dynamics365Entity       entity         = Dynamics365Entity.Create(entityNode.Attributes["name"].Value, (Dynamics365Connection)Parent);
            EntityMetadata          entityMetadata = entity.GetEntityMetadata((Dynamics365Connection)Parent);

            foreach (XmlNode node in entityNode.ChildNodes)
            {
                if (node.Name.ToLower() == "attribute")
                {
                    string            attributeName     = node.Attributes["name"].Value;
                    AttributeMetadata attributeMetadata = entityMetadata.Attributes.FirstOrDefault(findField => findField.LogicalName == attributeName);

                    if (attributeMetadata != default(AttributeMetadata))
                    {
                        fields.Add(Dynamics365Field.CreateFromMetadata(attributeMetadata, (Dynamics365Connection)Parent));
                    }
                    else
                    {
                        throw new ApplicationException(string.Format("The {0} field in {1} does not exist in Dynamics 365", attributeName, Name));
                    }
                }

                if (node.Name.ToLower() == "link-entity")
                {
                    List <Dynamics365Field> linkedFields = GetEntityNodeFields(node);

                    foreach (Dynamics365Field linkedField in linkedFields)
                    {
                        fields.Add(linkedField);
                    }
                }
            }

            return(fields);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            // convert from delimited string to field list
            if (!string.IsNullOrEmpty((string)value))
            {
                string   valueString  = (string)value;
                string[] fieldStrings = valueString.Split(new string[] { FIELD_DELIMITER }, StringSplitOptions.None);

                IDynamics365EntityFieldsProvider provider     = (IDynamics365EntityFieldsProvider)context.Instance;
                List <Dynamics365Field>          entityFields = provider.GetDynamics365EntityFields();
                BindingList <Dynamics365Field>   fields       = new BindingList <Dynamics365Field>();

                foreach (string fieldString in fieldStrings)
                {
                    string           id    = Regex.Match(valueString, CoreUtility.FieldMatchPattern).Groups[1].Value;
                    Dynamics365Field field = entityFields.FirstOrDefault(f => f.LogicalName == id);
                    fields.Add(field);
                }

                return(fields);
            }

            return(base.ConvertFrom(context, culture, value));
        }
예제 #8
0
 public override List <Field> GetFields(IConnection connection)
 {
     return(Dynamics365Field.GetFields(this, (Dynamics365Connection)connection).ConvertAll(field => (Field)field));
 }
예제 #9
0
        /// <summary>
        /// Adds DataColumns to the specified dataTable for the specified field.
        /// </summary>
        /// <param name="dataTable">The data table.</param>
        /// <param name="field">The field.</param>
        /// <param name="attributeMetadata">The field attribute metadata.</param>
        protected void AddDataColumns(DataTable dataTable, Dynamics365Field field, AttributeMetadata attributeMetadata)
        {
            Dynamics365TypeConverter converter = new Dynamics365TypeConverter(attributeMetadata);

            if (DataTypes == DataTypes.Native)
            {
                DataColumn nativeColumn = new DataColumn()
                {
                    ColumnName = string.Format("{0}.{1}", field.EntityLogicalName, field.LogicalName),
                    Caption    = field.DisplayName,
                    DataType   = converter.Dynamics365Type
                };

                if (!dataTable.Columns.Contains(nativeColumn.ColumnName))
                {
                    dataTable.Columns.Add(nativeColumn);
                }
            }
            else if (DataTypes == DataTypes.Neutral)
            {
                if (converter.Dynamics365Type == typeof(EntityReference) || converter.Dynamics365Type == typeof(EntityCollection))
                {
                    if (LookupBehaviour == LookupBehaviour.Name || LookupBehaviour == LookupBehaviour.NameAndIdentifier || LookupBehaviour == LookupBehaviour.EntityAndNameAndIdentifier)
                    {
                        DataColumn nameColumn = new DataColumn()
                        {
                            ColumnName = string.Format("{0}.{1}{2}", field.EntityLogicalName, field.LogicalName, LookupBehaviour == LookupBehaviour.Name ? string.Empty : ".Name"),
                            Caption    = string.Format("{0}{1}", field.DisplayName, LookupBehaviour == LookupBehaviour.Name ? string.Empty : " (Name)"),
                            DataType   = typeof(string)
                        };

                        if (!dataTable.Columns.Contains(nameColumn.ColumnName)) // todo - fetch xml view on team entity caused duplication - investigate why
                        {
                            dataTable.Columns.Add(nameColumn);
                        }
                    }

                    if (LookupBehaviour == LookupBehaviour.Identifier || LookupBehaviour == LookupBehaviour.NameAndIdentifier || LookupBehaviour == LookupBehaviour.EntityAndNameAndIdentifier)
                    {
                        DataColumn identifierColumn = new DataColumn()
                        {
                            ColumnName = string.Format("{0}.{1}{2}", field.EntityLogicalName, field.LogicalName, LookupBehaviour == LookupBehaviour.Identifier ? string.Empty : ".Identifier"),
                            Caption    = string.Format("{0}{1}", field.DisplayName, LookupBehaviour == LookupBehaviour.Identifier ? string.Empty : " (Identifier)"),
                            DataType   = converter.Dynamics365Type == typeof(EntityReference) ? typeof(Guid) : typeof(string)
                        };
                        dataTable.Columns.Add(identifierColumn);
                    }

                    if (LookupBehaviour == LookupBehaviour.Entity || LookupBehaviour == LookupBehaviour.EntityAndIdentifier || LookupBehaviour == LookupBehaviour.EntityAndNameAndIdentifier)
                    {
                        DataColumn entityColumn = new DataColumn()
                        {
                            ColumnName = string.Format("{0}.{1}{2}", field.EntityLogicalName, field.LogicalName, LookupBehaviour == LookupBehaviour.Entity ? string.Empty : ".Entity"),
                            Caption    = string.Format("{0}{1}", field.DisplayName, LookupBehaviour == LookupBehaviour.Entity ? string.Empty : " (Entity)"),
                            DataType   = typeof(string)
                        };

                        if (!dataTable.Columns.Contains(entityColumn.ColumnName))
                        {
                            dataTable.Columns.Add(entityColumn);
                        }
                    }
                }
                else if (converter.Dynamics365Type == typeof(OptionSetValue) || converter.Dynamics365Type == typeof(OptionSetValueCollection))
                {
                    if (OptionSetBehaviour == OptionSetBehaviour.Name || OptionSetBehaviour == OptionSetBehaviour.NameAndCode)
                    {
                        DataColumn nameColumn = new DataColumn()
                        {
                            ColumnName = string.Format("{0}.{1}{2}", field.EntityLogicalName, field.LogicalName, OptionSetBehaviour == OptionSetBehaviour.Name ? string.Empty : ".Name"),
                            Caption    = string.Format("{0}{1}", field.DisplayName, OptionSetBehaviour == OptionSetBehaviour.Name ? string.Empty : " (Name)"),
                            DataType   = typeof(string)
                        };

                        if (!dataTable.Columns.Contains(nameColumn.ColumnName))
                        {
                            dataTable.Columns.Add(nameColumn);
                        }
                    }

                    if (OptionSetBehaviour == OptionSetBehaviour.Code || OptionSetBehaviour == OptionSetBehaviour.NameAndCode)
                    {
                        DataColumn codeColumn = new DataColumn()
                        {
                            ColumnName = string.Format("{0}.{1}{2}", field.EntityLogicalName, field.LogicalName, OptionSetBehaviour == OptionSetBehaviour.Code ? string.Empty : ".Code"),
                            Caption    = string.Format("{0}{1}", field.DisplayName, OptionSetBehaviour == OptionSetBehaviour.Code ? string.Empty : " (Code)"),
                            DataType   = converter.Dynamics365Type == typeof(OptionSetValue) ? typeof(int) : typeof(string)
                        };

                        if (!dataTable.Columns.Contains(codeColumn.ColumnName))
                        {
                            dataTable.Columns.Add(codeColumn);
                        }
                    }
                }
                else
                {
                    DataColumn neutralColumn = new DataColumn()
                    {
                        ColumnName = string.Format("{0}.{1}", field.EntityLogicalName, field.LogicalName),
                        Caption    = field.DisplayName,
                        DataType   = converter.NeutralType
                    };

                    if (!dataTable.Columns.Contains(neutralColumn.ColumnName))
                    {
                        dataTable.Columns.Add(neutralColumn);
                    }
                }
            }
        }