Ejemplo n.º 1
0
        public ReferenceContext(TableStore store, ClassMapping cMapping)
        {
            Store        = store;
            CMapping     = cMapping;
            Children     = new List <ReferenceContext>();
            ContextType  = ReferenceContextType.Root;
            PathTypeHint = Store.MetaData.ExpressType(cMapping.Class.ToUpper());

            if (cMapping.PropertyMappings == null || !cMapping.PropertyMappings.Any())
            {
                return;
            }

            var parentPath = ("parent." + cMapping.ParentPath).Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries).ToList();

            foreach (var mapping in cMapping.PropertyMappings)
            {
                var path = mapping.Paths.FirstOrDefault();
                if (string.IsNullOrWhiteSpace(path))
                {
                    continue;
                }

                var segments = path.Split('.').ToList();

                //if path uses upper context of parent, fix the path to be relative to parent
                if (path[0] == '(')
                {
                    var levelCount = path.Count(c => c == '(');
                    segments =
                        parentPath.GetRange(0, parentPath.Count - levelCount)
                        .Concat(segments.GetRange(levelCount, segments.Count - levelCount))
                        .ToList();
                }

                AddMapping(mapping, segments);
            }

            //add special mapping for a parent path
            if (!cMapping.IsRoot)
            {
                var pMap = new PropertyMapping {
                    _Paths = string.Join(".", parentPath), Status = DataStatus.Reference
                };
                AddMapping(pMap, parentPath);
            }
        }
Ejemplo n.º 2
0
        private void AddMapping(PropertyMapping pMapping, List <string> segments)
        {
            if (segments == null || !segments.Any())
            {
                //this is a leaf
                Mapping = pMapping;
                return;
            }

            var segment     = segments.First();
            var segmentName = segment.Split('\\')[0];

            //handle special cases - type hints
            switch (segment)
            {
            case "[table]":
                TableHintMapping = pMapping;
                return;

            case "[type]":
                TypeHintMapping = pMapping;
                return;
            }

            var existChild = Children.FirstOrDefault(c => c.Segment == segmentName);

            if (existChild != null)
            {
                existChild.AddMapping(pMapping, segments.GetRange(1, segments.Count - 1));
            }
            else
            {
                var child = new ReferenceContext(segment, this, Store, CMapping);
                child.AddMapping(pMapping, segments.GetRange(1, segments.Count - 1));
                Children.Add(child);
                //cache scalar children for optimization
                if (child.ContextType == ReferenceContextType.Scalar || child.ContextType == ReferenceContextType.ScalarList)
                {
                    _scalarChildren.Add(child);
                }
            }
        }
Ejemplo n.º 3
0
        private void Store(IRow row, object value, PropertyMapping mapping)
        {
            if (value == null)
            {
                return;
            }

            var cellIndex = CellReference.ConvertColStringToIndex(mapping.Column);
            var cell      = row.GetCell(cellIndex) ?? row.CreateCell(cellIndex);

            //set column style to cell
            cell.CellStyle = row.Sheet.GetColumnStyle(cellIndex);

            //simplify any eventual enumeration into a single string
            var enumVal = value as IEnumerable;

            if (enumVal != null && !(value is string))
            {
                var strValue = string.Join(Mapping.ListSeparator, enumVal.Cast <object>());
                cell.SetCellType(CellType.String);
                cell.SetCellValue(strValue);
                return;
            }

            //string value
            var str = value as string;

            if (str != null)
            {
                cell.SetCellType(CellType.String);
                cell.SetCellValue(str);
                return;
            }

            //numeric point types
            if (value is double || value is float || value is int || value is long || value is short || value is byte || value is uint || value is ulong ||
                value is ushort)
            {
                cell.SetCellType(CellType.Numeric);
                cell.SetCellValue(Convert.ToDouble(value));
                return;
            }

            //boolean value
            if (value is bool)
            {
                cell.SetCellType(CellType.Boolean);
                cell.SetCellValue((bool)value);
                return;
            }

            //enumeration
            if (value is Enum)
            {
                var eType  = value.GetType();
                var eValue = Enum.GetName(eType, value);
                cell.SetCellType(CellType.String);
                //try to get alias from configuration
                var alias = GetEnumAlias(eType, eValue);
                cell.SetCellValue(alias ?? eValue);
                return;
            }

            throw new NotSupportedException("Only base types are supported");
        }
Ejemplo n.º 4
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);
            }
        }