public override SupportedTypes GetPropertyType(string propertyName)
        {
            Assert.ArgumentNotNullOrEmpty(propertyName, "propertyName");

            const string GetPropertyTypeKey = "getPropertyType";

            ConditionalLog.Info(String.Format("GetPropertyType({0}). Started.", propertyName), this, TimerAction.Start, GetPropertyTypeKey);

            SupportedTypes   propertyType;
            ContactAttribute contactAttribute;

            if (this.CacheService.MetadataCache.ContainsKey(propertyName))
            {
                contactAttribute = (ContactAttribute)this.CacheService.MetadataCache[propertyName];
                propertyType     = contactAttribute.Type;

                ConditionalLog.Info(String.Format("GetPropertyType({0}). Retrieved from cache.", propertyName), this, TimerAction.Tick, GetPropertyTypeKey);
            }
            else
            {
                var request = new RetrieveAttributeRequest
                {
                    EntityLogicalName = "contact",
                    LogicalName       = propertyName
                };

                AttributeMetadata attributeMetadata;
                try
                {
                    var response = (RetrieveAttributeResponse)this.organizationServiceCache.GetOrganizationService().Execute(request);
                    attributeMetadata = response.AttributeMetadata;

                    ConditionalLog.Info(String.Format("GetPropertyType({0}). Retrieved from CRM.", propertyName), this, TimerAction.Tick, GetPropertyTypeKey);
                }
                catch (Exception e)
                {
                    ConditionalLog.Error(String.Format("Couldn't retrieve metadata for the {0} attribute from CRM.", propertyName), e, this);
                    throw new ProviderException(String.Format("Couldn't retrieve metadata for the {0} attribute from CRM.", propertyName), e);
                }

                switch (attributeMetadata.AttributeType.Value)
                {
                case AttributeTypeCode.String:
                case AttributeTypeCode.Memo:
                    propertyType     = SupportedTypes.String;
                    contactAttribute = new ContactAttribute(propertyType);

                    break;

                case AttributeTypeCode.Boolean:
                    propertyType     = SupportedTypes.CrmBoolean;
                    contactAttribute = new ContactAttribute(propertyType);

                    break;

                case AttributeTypeCode.DateTime:
                    propertyType     = SupportedTypes.CrmDateTime;
                    contactAttribute = new ContactAttribute(propertyType);

                    break;

                case AttributeTypeCode.Double:
                    propertyType     = SupportedTypes.CrmFloat;
                    contactAttribute = new ContactAttribute(propertyType);

                    break;

                case AttributeTypeCode.Decimal:
                    propertyType     = SupportedTypes.CrmDecimal;
                    contactAttribute = new ContactAttribute(propertyType);

                    break;

                case AttributeTypeCode.Money:
                    propertyType     = SupportedTypes.CrmMoney;
                    contactAttribute = new ContactAttribute(propertyType);

                    break;

                case AttributeTypeCode.Integer:
                    propertyType     = SupportedTypes.CrmNumber;
                    contactAttribute = new ContactAttribute(propertyType);

                    break;

                case AttributeTypeCode.Picklist:
                    propertyType = SupportedTypes.Picklist;

                    var picklistAttributeMetadata = (PicklistAttributeMetadata)attributeMetadata;
                    var options = picklistAttributeMetadata.OptionSet.Options.ToDictionary(option => option.Label.UserLocalizedLabel.Label, option => option.Value.Value);

                    contactAttribute = new PicklistContactAttribute(propertyType, options);
                    break;

                default:
                    ConditionalLog.Info(String.Format("The {0} attribute is of unsupported ({1}) type.", propertyName, attributeMetadata.AttributeType.Value), this, TimerAction.Tick, GetPropertyTypeKey);
                    throw new ProviderException(String.Format("The {0} attribute is of unsupported ({1}) type.", propertyName, attributeMetadata.AttributeType.Value));
                }

                this.CacheService.MetadataCache.Add(propertyName, contactAttribute);
            }

            ConditionalLog.Info(String.Format("GetPropertyType({0}). Finished.", propertyName), this, TimerAction.Stop, GetPropertyTypeKey);
            return(propertyType);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds the property to collection.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyValue">Value of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <param name="properties">The properties.</param>
        protected void AddPropertyToCollection(string propertyName, object propertyValue, SupportedTypes propertyType, List <Property> properties)
        {
            var propertyStringValue = propertyValue as string;

            switch (propertyType)
            {
            case SupportedTypes.String:
                var stringProperty = new StringProperty();

                stringProperty.Name  = propertyName;
                stringProperty.Value = (string)propertyValue;

                properties.Add(stringProperty);

                break;

            case SupportedTypes.CrmBoolean:
                if (propertyStringValue != null || (propertyValue is bool))
                {
                    var booleanProperty = new CrmBooleanProperty();

                    booleanProperty.Name         = propertyName;
                    booleanProperty.Value        = new crm4.webservice.CrmBoolean();
                    booleanProperty.Value.IsNull = true;

                    if (propertyValue is bool)
                    {
                        booleanProperty.Value.Value = (bool)propertyValue;
                    }
                    else
                    {
                        booleanProperty.Value.Value = MainUtil.GetBool(propertyStringValue, false);
                    }
                    booleanProperty.Value.IsNull = false;

                    properties.Add(booleanProperty);
                }

                break;

            case SupportedTypes.CrmDateTime:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is DateTime))
                {
                    var datetimeProperty = new CrmDateTimeProperty();

                    datetimeProperty.Name         = propertyName;
                    datetimeProperty.Value        = new crm4.webservice.CrmDateTime();
                    datetimeProperty.Value.IsNull = true;

                    if (propertyValue is DateTime)
                    {
                        datetimeProperty.Value.Value  = ((DateTime)propertyValue).ToString("yyyy-MM-ddTHH:mm:sszzzz");
                        datetimeProperty.Value.IsNull = false;
                    }
                    else
                    {
                        DateTime dateTimeValue;
                        if (DateTime.TryParse(propertyStringValue, out dateTimeValue))
                        {
                            datetimeProperty.Value.Value  = dateTimeValue.ToString("yyyy-MM-ddTHH:mm:sszzzz");
                            datetimeProperty.Value.IsNull = false;
                        }
                    }

                    if (!datetimeProperty.Value.IsNull)
                    {
                        properties.Add(datetimeProperty);
                    }
                }

                break;

            case SupportedTypes.CrmFloat:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is float))
                {
                    var floatProperty = new CrmFloatProperty();

                    floatProperty.Name         = propertyName;
                    floatProperty.Value        = new crm4.webservice.CrmFloat();
                    floatProperty.Value.IsNull = true;

                    if (propertyValue is float)
                    {
                        floatProperty.Value.Value  = (float)propertyValue;
                        floatProperty.Value.IsNull = false;
                    }
                    else
                    {
                        float floatValue;
                        if (float.TryParse(propertyStringValue, out floatValue))
                        {
                            floatProperty.Value.Value  = floatValue;
                            floatProperty.Value.IsNull = false;
                        }
                    }

                    if (!floatProperty.Value.IsNull)
                    {
                        properties.Add(floatProperty);
                    }
                }

                break;

            case SupportedTypes.CrmDecimal:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is decimal))
                {
                    var decimalProperty = new CrmDecimalProperty();

                    decimalProperty.Name         = propertyName;
                    decimalProperty.Value        = new CrmDecimal();
                    decimalProperty.Value.IsNull = true;

                    if (propertyValue is decimal)
                    {
                        decimalProperty.Value.Value  = (decimal)propertyValue;
                        decimalProperty.Value.IsNull = false;
                    }
                    else
                    {
                        decimal decimalValue;
                        if (Decimal.TryParse(propertyStringValue, out decimalValue))
                        {
                            decimalProperty.Value.Value  = decimalValue;
                            decimalProperty.Value.IsNull = false;
                        }
                    }

                    if (!decimalProperty.Value.IsNull)
                    {
                        properties.Add(decimalProperty);
                    }
                }

                break;

            case SupportedTypes.CrmMoney:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is decimal))
                {
                    var moneyProperty = new CrmMoneyProperty();

                    moneyProperty.Name         = propertyName;
                    moneyProperty.Value        = new CrmMoney();
                    moneyProperty.Value.IsNull = true;

                    if (propertyValue is decimal)
                    {
                        moneyProperty.Value.Value  = (decimal)propertyValue;
                        moneyProperty.Value.IsNull = false;
                    }
                    else
                    {
                        decimal moneyValue;
                        if (Decimal.TryParse(propertyStringValue, out moneyValue))
                        {
                            moneyProperty.Value.Value  = moneyValue;
                            moneyProperty.Value.IsNull = false;
                        }
                    }

                    if (!moneyProperty.Value.IsNull)
                    {
                        properties.Add(moneyProperty);
                    }
                }

                break;

            case SupportedTypes.CrmNumber:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is int))
                {
                    var numberProperty = new CrmNumberProperty();

                    numberProperty.Name         = propertyName;
                    numberProperty.Value        = new crm4.webservice.CrmNumber();
                    numberProperty.Value.IsNull = true;

                    if (propertyValue is int)
                    {
                        numberProperty.Value.Value  = (int)propertyValue;
                        numberProperty.Value.IsNull = false;
                    }
                    else
                    {
                        int numberValue;
                        if (Int32.TryParse(propertyStringValue, out numberValue))
                        {
                            numberProperty.Value.Value  = numberValue;
                            numberProperty.Value.IsNull = false;
                        }
                    }

                    if (!numberProperty.Value.IsNull)
                    {
                        properties.Add(numberProperty);
                    }
                }

                break;

            case SupportedTypes.Picklist:
                PicklistContactAttribute picklistAttribute;
                if (this.CacheService.MetadataCache.ContainsKey(propertyName))
                {
                    picklistAttribute = (PicklistContactAttribute)this.CacheService.MetadataCache[propertyName];
                }
                else
                {
                    var attributeRequest = new RetrieveAttributeRequest();

                    attributeRequest.EntityLogicalName = EntityName.contact.ToString();
                    attributeRequest.LogicalName       = propertyName;

                    var attributeResponse = (RetrieveAttributeResponse)this.CrmMetadataService.Execute(attributeRequest);

                    var attributeMetadata = (PicklistAttributeMetadata)attributeResponse.AttributeMetadata;
                    var options           = attributeMetadata.Options.ToDictionary(option => option.Label.UserLocLabel.Label, option => option.Value.Value);

                    picklistAttribute = new PicklistContactAttribute(SupportedTypes.Picklist, options);
                    this.CacheService.MetadataCache.Add(propertyName, picklistAttribute);
                }

                if (!String.IsNullOrEmpty(propertyStringValue))
                {
                    var value = picklistAttribute.Options[propertyStringValue];
                    if (value >= 0)
                    {
                        var picklistProperty = new PicklistProperty();

                        picklistProperty.Name        = propertyName;
                        picklistProperty.Value       = new Picklist();
                        picklistProperty.Value.Value = value;

                        properties.Add(picklistProperty);
                    }
                    else
                    {
                        CrmHelper.ShowMessage(String.Format("The picklist value ({0}) of the '{1}' property couldn't be recognized", propertyValue, propertyName));
                        break;
                    }
                }

                break;
            }
        }
        /// <summary>
        /// Adds the property to collection.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="propertyValue">Value of the property.</param>
        /// <param name="propertyType">Type of the property.</param>
        /// <param name="properties">The properties.</param>
        protected void AddPropertyToCollection(string propertyName, object propertyValue, SupportedTypes propertyType, Dictionary <string, object> properties)
        {
            var propertyStringValue = propertyValue as string;

            switch (propertyType)
            {
            case SupportedTypes.String:
                properties.Add(propertyName, propertyValue);

                break;

            case SupportedTypes.CrmBoolean:
                if (propertyStringValue != null || (propertyValue is bool))
                {
                    if (propertyValue is bool)
                    {
                        properties.Add(propertyName, propertyValue);
                    }
                    else
                    {
                        properties.Add(propertyName, MainUtil.GetBool(propertyStringValue, false));
                    }
                }

                break;

            case SupportedTypes.CrmDateTime:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is DateTime))
                {
                    if (propertyValue is DateTime)
                    {
                        properties.Add(propertyName, propertyValue);
                    }
                    else
                    {
                        DateTime dateTimeValue;
                        if (DateTime.TryParse(propertyStringValue, out dateTimeValue))
                        {
                            properties.Add(propertyName, dateTimeValue);
                        }
                    }
                }

                break;

            case SupportedTypes.CrmFloat:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is double))
                {
                    if (propertyValue is double)
                    {
                        properties.Add(propertyName, propertyValue);
                    }
                    else
                    {
                        double doubleValue;
                        if (Double.TryParse(propertyStringValue, out doubleValue))
                        {
                            properties.Add(propertyName, doubleValue);
                        }
                    }
                }

                break;

            case SupportedTypes.CrmDecimal:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is decimal))
                {
                    if (propertyValue is decimal)
                    {
                        properties.Add(propertyName, propertyValue);
                    }
                    else
                    {
                        decimal decimalValue;
                        if (Decimal.TryParse(propertyStringValue, out decimalValue))
                        {
                            properties.Add(propertyName, decimalValue);
                        }
                    }
                }

                break;

            case SupportedTypes.CrmMoney:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is decimal))
                {
                    if (propertyValue is decimal)
                    {
                        properties.Add(propertyName, new Money((decimal)propertyValue));
                    }
                    else
                    {
                        decimal moneyValue;
                        if (Decimal.TryParse(propertyStringValue, out moneyValue))
                        {
                            properties.Add(propertyName, new Money(moneyValue));
                        }
                    }
                }

                break;

            case SupportedTypes.CrmNumber:
                if (!String.IsNullOrEmpty(propertyStringValue) || (propertyValue is int))
                {
                    if (propertyValue is int)
                    {
                        properties.Add(propertyName, propertyValue);
                    }
                    else
                    {
                        int intValue;
                        if (Int32.TryParse(propertyStringValue, out intValue))
                        {
                            properties.Add(propertyName, intValue);
                        }
                    }
                }

                break;

            case SupportedTypes.Picklist:
                PicklistContactAttribute picklistAttribute;
                if (this.CacheService.MetadataCache.ContainsKey(propertyName))
                {
                    picklistAttribute = (PicklistContactAttribute)this.CacheService.MetadataCache[propertyName];
                }
                else
                {
                    var attributeRequest = new RetrieveAttributeRequest();

                    attributeRequest.EntityLogicalName = "contact";
                    attributeRequest.LogicalName       = propertyName;

                    var attributeResponse = (RetrieveAttributeResponse)this.organizationServiceCache.GetOrganizationService().Execute(attributeRequest);

                    var attributeMetadata = (PicklistAttributeMetadata)attributeResponse.AttributeMetadata;
                    var options           = attributeMetadata.OptionSet.Options.ToDictionary(option => option.Label.UserLocalizedLabel.Label, option => option.Value.Value);

                    picklistAttribute = new PicklistContactAttribute(SupportedTypes.Picklist, options);
                    this.CacheService.MetadataCache.Add(propertyName, picklistAttribute);
                }

                if (!String.IsNullOrEmpty(propertyStringValue))
                {
                    var value = picklistAttribute.Options[propertyStringValue];
                    if (value >= 0)
                    {
                        properties.Add(propertyName, new OptionSetValue(value));
                    }
                    else
                    {
                        CrmHelper.ShowMessage(String.Format("The picklist value ({0}) of the '{1}' property couldn't be recognized", propertyValue, propertyName));
                        break;
                    }
                }

                break;
            }
        }