Ejemplo n.º 1
0
        private IXPSimpleObject GetExistingOrCreateNewObject(XPObjectSpace objectSpace, string keyPropertyName,
                                                             Row excelRow, Type type)
        {
            Mapping         idMapping = ImportMap.Mappings.SingleOrDefault(p => p.MapedTo == keyPropertyName);
            IXPSimpleObject newObj    = null;

            if (idMapping != null && ImportUtils.GetQString(excelRow[idMapping.Column].Value) != string.Empty)
            {
                try{
                    //find existing object
                    Cell val  = excelRow[idMapping.Column];
                    var  gwid = new Guid(ImportUtils.GetQString(val.Value));
                    newObj =
                        objectSpace.FindObject(type, new BinaryOperator(keyPropertyName, gwid), true) as IXPSimpleObject;
                }
                catch {
                }
            }
            return(newObj ?? (objectSpace.CreateObject(type) as IXPSimpleObject));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Defines how string is converted into NOT nullable value type
        /// </summary>
        /// <param name="prop">property that needs the converted value</param>
        /// <param name="value">string value to be converted</param>
        /// <param name="numberFormatInfo">Number formatting info</param>
        /// <returns>Converted value</returns>
        protected virtual object MapStringToValueType(XPMemberInfo prop, string value,
                                                      NumberFormatInfo numberFormatInfo = null)
        {
            object result = null;

            if (prop.MemberType.IsEnum)
            {
                result = Enum.Parse(prop.MemberType, value);
            }

            else if (prop.MemberType == typeof(char))
            {
                result = Convert.ChangeType(ImportUtils.GetQString(value), prop.MemberType);
            }

            else if (prop.StorageType == typeof(int))
            {
                int number;
                if (value != String.Empty && Int32.TryParse(value, out number))
                {
                    result = number;
                }
                else
                {
                    result = 0;
                }
            }
            else if (prop.MemberType == typeof(Guid))
            {
                result = new Guid(ImportUtils.GetQString(value));
            }
            else if (prop.StorageType == typeof(DateTime))
            {
                if (value != string.Empty)
                {
                    //Include validate
                    DateTime dt = DateTime.FromOADate(Convert.ToDouble(value));
                    result = dt;
                }
            }
            else if (prop.MemberType == typeof(double))
            {
                double number;

                if (Double.TryParse(value, NumberStyles.Number,
                                    numberFormatInfo ?? new NumberFormatInfo {
                    NumberDecimalSeparator = "."
                }, out number))
                {
                    result = number;
                }
            }
            else if (prop.MemberType == typeof(bool))
            {
                if (value != string.Empty &&
                    (value.Length == 1 || value.ToLower() == @"true" || value.ToLower() == @"false"))
                {
                    bool truefalse;
                    if (value.ToLower() == @"true" || value.ToLower() == @"false")
                    {
                        truefalse = Convert.ToBoolean(value);
                    }
                    else
                    {
                        truefalse = Convert.ToBoolean(Convert.ToInt32(value));
                    }
                    result = truefalse;
                }
            }
            else
            {
                result = Convert.ChangeType(value, prop.MemberType, CultureInfo.InvariantCulture);
            }
            return(result);
        }