Exemplo n.º 1
0
        private object GetValue(IPersistEntity entity, ExpressType type, string path, EntityContext context)
        {
            while (true)
            {
                if (string.IsNullOrWhiteSpace(path))
                {
                    return(null);
                }

                //if it is parent, skip to the root of the context
                //optimization: check first letter before StartsWith() function.
                if (path[0] == 'p' && path.StartsWith("parent."))
                {
                    if (context == null)
                    {
                        return(null);
                    }

                    path    = path.Substring(7); //trim "parent." from the beginning
                    entity  = context.RootEntity;
                    type    = entity.ExpressType;
                    context = null;
                    continue;
                }

                //one level up in the context hierarchy
                //optimization: check first letter before StartsWith() function.
                if (path[0] == '(' && path.StartsWith("()."))
                {
                    if (context == null)
                    {
                        return(null);
                    }

                    path    = path.Substring(3); //trim "()." from the beginning
                    entity  = context.Parent.Entity;
                    type    = entity.ExpressType;
                    context = context.Parent;
                    continue;
                }

                if (string.Equals(path, "[table]", StringComparison.Ordinal))
                {
                    var mapping = GetTable(type);
                    return(mapping.TableName);
                }

                if (string.Equals(path, "[type]", StringComparison.Ordinal))
                {
                    return(entity.ExpressType.ExpressName);
                }

                var parts       = path.Split('.');
                var multiResult = new List <string>();
                for (var i = 0; i < parts.Length; i++)
                {
                    var value = GetPropertyValue(parts[i], entity, type);

                    if (value == null)
                    {
                        return(null);
                    }

                    var ent = value as IPersistEntity;
                    if (ent != null)
                    {
                        entity = ent;
                        type   = ent.ExpressType;
                        continue;
                    }

                    var expVal = value as IExpressValueType;
                    if (expVal != null)
                    {
                        //if the type of the value is what we want
                        if (i < parts.Length - 1 && parts[parts.Length - 1] == "[type]")
                        {
                            return(expVal.GetType().Name);
                        }
                        //return actual value as an underlying system type
                        return(expVal.Value);
                    }

                    var expValEnum = value as IEnumerable <IExpressValueType>;
                    if (expValEnum != null)
                    {
                        return(expValEnum.Select(v => v.Value));
                    }

                    var entEnum = value as IEnumerable <IPersistEntity>;
                    //it must be a simple value
                    if (entEnum == null)
                    {
                        return(value);
                    }

                    //it is a multivalue result
                    var subParts = parts.ToList().GetRange(i + 1, parts.Length - i - 1);
                    var subPath  = string.Join(".", subParts);
                    foreach (var persistEntity in entEnum)
                    {
                        var subValue = GetValue(persistEntity, persistEntity.ExpressType, subPath, null);
                        if (subValue == null)
                        {
                            continue;
                        }
                        var subString = subValue as string;
                        if (subString != null)
                        {
                            multiResult.Add(subString);
                            continue;
                        }
                        multiResult.Add(subValue.ToString());
                    }
                    return(multiResult);
                }

                //if there is only entity itself to return, try to get 'Name' or 'Value' property as a fallback
                return(GetFallbackValue(entity, type));
            }
        }
Exemplo n.º 2
0
 public EntityContext Add(EntityContext child)
 {
     child.Parent = this;
     Children.Add(child);
     return(child);
 }
Exemplo n.º 3
0
        private void Store(ISheet sheet, IPersistEntity entity, ClassMapping mapping, ExpressType expType, EntityContext context)
        {
            var             multiRow     = -1;
            List <string>   multiValues  = null;
            PropertyMapping multiMapping = null;
            var             row          = GetRow(sheet);

            //fix on "Special Case" Assembly Row to Entity mapping
            if ((context?.RootEntity != null) && (expType?.ExpressNameUpper == "TYPEORCOMPONENT"))  //without CobieExpress reference and not using reflection this is as good as it gets to ID Assembly
            {
                RowNoToEntityLabelLookup[sheet.SheetName].Add(row.RowNum, context.RootEntity.EntityLabel);
            }
            else
            {
                RowNoToEntityLabelLookup[sheet.SheetName].Add(row.RowNum, entity.EntityLabel);
            }

            foreach (var propertyMapping in mapping.PropertyMappings)
            {
                object value = null;
                foreach (var path in propertyMapping.Paths)
                {
                    value = GetValue(entity, expType, path, context);
                    if (value != null)
                    {
                        break;
                    }
                }
                if (value == null && propertyMapping.Status == DataStatus.Required)
                {
                    value = propertyMapping.DefaultValue ?? "n/a";
                }

                var isMultiRow = IsMultiRow(value, propertyMapping);
                if (isMultiRow)
                {
                    multiRow = row.RowNum;
                    var values     = new List <string>();
                    var enumerable = value as IEnumerable <string>;
                    if (enumerable != null)
                    {
                        values.AddRange(enumerable);
                    }

                    //get only first value and store it
                    var first = values.First();
                    Store(row, first, propertyMapping);

                    //set the rest for the processing as multiValue
                    values.Remove(first);
                    multiValues  = values;
                    multiMapping = propertyMapping;
                }
                else
                {
                    Store(row, value, propertyMapping);
                }
            }

            //adjust width of the columns after the first and the eight row
            //adjusting fully populated workbook takes ages. This should be almost all right
            if (row.RowNum == 1 || row.RowNum == 8)
            {
                AdjustAllColumns(sheet, mapping);
            }

            //it is not a multi row so return
            if (multiRow <= 0 || multiValues == null || !multiValues.Any())
            {
                return;
            }

            //add repeated rows if necessary
            foreach (var value in multiValues)
            {
                var rowNum = GetNextRowNum(sheet);
                var copy   = sheet.CopyRow(multiRow, rowNum);
                Store(copy, value, multiMapping);
                RowNoToEntityLabelLookup[sheet.SheetName].Add(rowNum, entity.EntityLabel);
            }
        }