Exemple #1
0
        private void AddOrUpdateCRMEntityWithDataStructure(IOrganizationService serviceProxy, string crmEntityName, SimpleFlowStructure simpleFlowStructure)
        {
            RetrieveEntityResponse crmEntity = GetEntityInformationFromCRMClient(serviceProxy, crmEntityName);

            if (crmEntity != null)
            {
                log.Info("CRM entity fetched successfully from server");
                List <AttributeMetadata>         entityAttributes = crmEntity.EntityMetadata.Attributes.Where(t => t.IsValidForCreate == true).ToList();
                List <CRMEntityField>            entityFields;
                List <DefinedDataTypeDataMember> memberList;

                GetEntityFieldsFromCRMEntityAttributes(entityAttributes, crmEntityName, out entityFields, out memberList);

                simpleFlowStructure.Children = memberList.ToArray();
                CRMEntityFields = entityFields.ToArray();
                string displayName;
                if (crmEntity.EntityMetadata.DisplayName.LocalizedLabels != null && crmEntity.EntityMetadata.DisplayName.LocalizedLabels.Count > 0)
                {
                    displayName = crmEntity.EntityMetadata.DisplayName.LocalizedLabels[0].Label;
                }
                else
                {
                    displayName = crmEntity.EntityMetadata.LogicalName;
                }
                CRMEntityDisplayName = displayName;
                DataStructureService.Instance.AddDataStructure(UserContextHolder.GetRootUserContext(), simpleFlowStructure);
                log.Info(string.Format("Entity with name {0} added successfully to the database", crmEntityName));
            }
        }
Exemple #2
0
        private void GetEntityFieldsFromCRMEntityAttributes(List <AttributeMetadata> entityAttributes, string crmEntityName, out List <CRMEntityField> entityFields, out List <DefinedDataTypeDataMember> memberList)
        {
            entityFields = new List <CRMEntityField>();
            memberList   = new List <DefinedDataTypeDataMember>();
            if (entityAttributes.Count() > 0)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug(string.Join(", ", entityAttributes.Select(x => $"{x.LogicalName}({x.DisplayName})[{x.AttributeType}, ")));
                }

                foreach (AttributeMetadata attribute in entityAttributes)
                {
                    Type type = GetTypeFromAttributeType(attribute.AttributeType);
                    if (type != null)
                    {
                        string displayName;
                        if (attribute.DisplayName.LocalizedLabels != null && attribute.DisplayName.LocalizedLabels.Count > 0)
                        {
                            displayName = attribute.DisplayName.LocalizedLabels[0].Label;
                        }
                        else
                        {
                            displayName = attribute.LogicalName;
                        }
                        bool?validForUpdate = attribute.IsValidForUpdate;
                        DefinedDataTypeDataMember member = new DefinedDataTypeDataMember();
                        member.RelationshipName             = attribute.LogicalName;
                        member.DisplayLabelName             = CleanDisplayName(displayName);
                        member.IsOverrideDisplayInformation = true;
                        member.RelatedToDataType            = type.FullName;
                        member.AllowNull = true;

                        if (attribute.AttributeType == AttributeTypeCode.Uniqueidentifier || attribute.IsPrimaryId == true)
                        {
                            member.CustomAttributes = new CustomAttributeDefinition[] { new CustomAttributeDefinition("PropertyHidden", new string[0]) };
                            validForUpdate          = false;
                        }
                        CRMEntityField entityField = new CRMEntityField()
                        {
                            DisplayName      = CleanDisplayName(displayName),
                            FieldName        = attribute.LogicalName,
                            FieldType        = new DecisionsNativeType(type),
                            IsValidForUpdate = validForUpdate,
                            AttributeType    = attribute.AttributeType.ToString()
                        };

                        if (attribute.AttributeType == AttributeTypeCode.Picklist)
                        {
                            ORM <EnumDataType> orm = new ORM <EnumDataType>();

                            //Check this type of enum data is present in database or not
                            // and either create it or update it
                            string currentOptionSetDataTypeName = string.Format("{0}_{1}", crmEntityName, attribute.LogicalName);

                            EnumDataType optionsSet = orm.Fetch(new WhereCondition[]
                            {
                                new FieldWhereCondition("data_type_name", QueryMatchType.Equals, currentOptionSetDataTypeName),
                                new FieldWhereCondition("data_type_name_space", QueryMatchType.Equals, GetCrmEntityNamespace())
                            }).FirstOrDefault();

                            if (optionsSet == null)
                            {
                                log.Debug($"No enum type with name '{currentOptionSetDataTypeName}' and namespace '{GetCrmEntityNamespace()}' found, trying to create one:");
                                EnsureCrmEntityFolderExists();
                                optionsSet = new EnumDataType();
                                optionsSet.DataTypeName      = currentOptionSetDataTypeName;
                                optionsSet.DataTypeNameSpace = GetCrmEntityNamespace();
                                optionsSet.EntityFolderID    = GetCrmEntityFolderId();
                                optionsSet.EnumValues        = CleanEnumValues(((PicklistAttributeMetadata)attribute).OptionSet.Options.Select(t => t.Label.LocalizedLabels[0].Label).ToArray());
                                orm.Store(optionsSet);
                                log.Debug($"new enum type created for '{currentOptionSetDataTypeName}'");
                            }
                            else
                            {
                                log.Debug($"Enum type '{GetCrmEntityNamespace()}.{currentOptionSetDataTypeName}' already exists; updating its enum value list.");

                                string[] enumValues = CleanEnumValues(((PicklistAttributeMetadata)attribute).OptionSet.Options.Select(t => t.Label.LocalizedLabels[0].Label).ToArray());

                                if (log.IsDebugEnabled)
                                {
                                    log.Debug(string.Join(", ", ((PicklistAttributeMetadata)attribute).OptionSet.Options.Select(t => $"'{t.Label.LocalizedLabels[0].Label}':'{t.Value}'")));
                                }

                                DataStructureService.Instance.ChangeEnumValues(UserContextHolder.GetRootUserContext(), GetCrmEntityNamespace(), currentOptionSetDataTypeName, enumValues);

                                log.Debug("Enum value list updated.");
                            }

                            string generatedEnumFullTypeName = string.Format("{0}.{1}", GetCrmEntityNamespace(), currentOptionSetDataTypeName);

                            member.RelatedToDataType = generatedEnumFullTypeName;
                            List <CRMOptionsSet> options = new List <CRMOptionsSet>();
                            foreach (var option in ((PicklistAttributeMetadata)attribute).OptionSet.Options)
                            {
                                options.Add(new CRMOptionsSet()
                                {
                                    OptionName = option.Label.LocalizedLabels[0].Label.Replace(" ", ""), OptionValue = option.Value
                                });
                            }
                            entityField.CRMOptionSet = options.ToArray();
                        }

                        if (attribute.RequiredLevel.Value == AttributeRequiredLevel.ApplicationRequired || attribute.RequiredLevel.Value == AttributeRequiredLevel.SystemRequired)
                        {
                            entityField.IsRequired = true;
                            member.Required        = true;
                        }

                        entityFields.Add(entityField);
                        memberList.Add(member);
                    }
                }
            }
        }