Ejemplo n.º 1
0
        public Model.ViewModel.GridVM GetGridViewModelForObject(IQueryable <object> collection, string objectId, string objectType, string bindingPath, CollectionViewModelParameters parameters)
        {
            var gv = new Model.ViewModel.GridVM();

            gv.PropertyValue = $"{collection.Cast<object>().FirstOrDefault()?.GetType().Name}[{collection.Count()}]";

            var firstElement = collection.Cast <object>().FirstOrDefault();

            if (firstElement == null)
            {
                return(gv);
            }

            var elementType           = firstElement.GetType();
            var elementIdPropertyName = dataService.GetIdPropertyName(elementType);
            var propertyInfos         = viewConfigurationService.GetGridCollumnInPropertyGrid(elementType, elementIdPropertyName);
            var idProperty            = elementType.GetProperty(elementIdPropertyName);

            bool isFirst = true;
            var  page    = collection.Skip(parameters.Page * parameters.PageSize).Take(parameters.PageSize);
            int  iRow    = 0;

            foreach (var element in page)
            {
                var row = new RowViewModel();
                row.Cells = new CellViewModel[propertyInfos.Length];
                var rowId = idProperty.GetValue(element);
                row.Id = rowId?.ToString();
                row.ElementTypeFullName = helper.GetTypeName(elementType);
                row.EditUrl             = helper.GetEditUrlForClass(row.Id, elementType);

                for (int i = 0; i < propertyInfos.Length; i++)
                {
                    var cellProperty     = propertyInfos[i];
                    var cellValue        = cellProperty.GetValue(element);
                    var cellPropertyType = cellProperty.PropertyType;
                    var cellType         = helper.CheckPropType(cellPropertyType);

                    if (isFirst)
                    {
                        var cvm = new ColumnViewModel()
                        {
                            BindingPath = cellProperty.Name, Header = cellProperty.Name
                        };
                        gv.Columns.Add(cvm);
                    }

                    var cell = new CellViewModel()
                    {
                        IsBigString = helper.IsBigString(cellValue, BaseService.EnumPreviewType.Grid),
                        Value       = helper.GetPreview(cellValue, BaseService.EnumPreviewType.Grid)
                    };
                    cell.MainObjectId   = objectId;
                    cell.MainObjectType = objectType;
                    cell.BindingPath    = $"{bindingPath}[{iRow}].{cellProperty.Name}";

                    row.Cells[i] = cell;

                    if (cellType == PropertyTypes.Class)
                    {
                        if (cellValue != null)
                        {
                            var valueType  = cellValue.GetType();
                            var idPropName = dataService.GetIdPropertyName(valueType);
                            var idProp     = valueType.GetProperty(idPropName);
                            var id         = idProp.GetValue(cellValue)?.ToString();

                            cell.EditUrl = helper.GetEditUrlForClass(id, valueType);
                        }
                    }
                    else if (cellType == PropertyTypes.Collection)
                    {
                        cell.EditUrl = helper.GetEditUrlForCollection(helper.GetCollectionElementType(cellPropertyType), rowId, elementType);
                    }
                }
                gv.Rows.Add(row);
                isFirst = false;
                iRow++;
            }

            return(gv);
        }
Ejemplo n.º 2
0
        private void createPropertyGridFromObject_internal(object obj, PropertyGridVM parentPropertyGridVM, int level, string parentPath = "")
        {
            if (level <= 0)
            {
                return;
            }

            if (obj == null)
            {
                return;
            }

            var type             = obj.GetType();
            var objectProperties = type.GetProperties();

            foreach (var item in objectProperties)
            {
                var value        = item.GetValue(obj);
                var propertyType = item.PropertyType;
                var propType     = helper.CheckPropType(propertyType);
                var bindingPath  = $"{parentPath}.{item.Name}".TrimStart('.');

                if (propType == PropertyTypes.Collection)
                {
                    if (value != null)
                    {
                        var collection = (IEnumerable)value;
                        var gridVM     = gridService.GetGridViewModelForObject(collection?.Cast <object>().AsQueryable(),
                                                                               parentPropertyGridVM.MainObjectId, parentPropertyGridVM.MainObjectTypeFullname, bindingPath,
                                                                               new CollectionViewModelParameters());
                        gridVM.PropertyName           = item.Name;
                        gridVM.BindingPath            = bindingPath;
                        gridVM.PropertyValue          = helper.GetValue(value);
                        gridVM.IsStatic               = parentPropertyGridVM.IsStatic;
                        gridVM.MainObjectId           = parentPropertyGridVM.MainObjectId;
                        gridVM.MainObjectTypeFullname = parentPropertyGridVM.MainObjectTypeFullname;
                        gridVM.EditUrl = helper.GetEditUrlForCollection(propertyType, "", propertyType);//TODO

                        parentPropertyGridVM.Properties.Add(gridVM);
                    }
                    else
                    {
                        parentPropertyGridVM.Properties.Add(new Model.ViewModel.GridVM()
                        {
                            PropertyName = item.Name, PropertyValue = "NULL", BindingPath = bindingPath
                        });
                    }
                }
                else if (propType == PropertyTypes.Class)
                {
                    var propertyGridVM = new PropertyGridVM();
                    propertyGridVM.PropertyName           = item.Name;
                    propertyGridVM.BindingPath            = bindingPath;
                    propertyGridVM.IsStatic               = parentPropertyGridVM.IsStatic;
                    propertyGridVM.MainObjectId           = parentPropertyGridVM.MainObjectId;
                    propertyGridVM.MainObjectTypeFullname = parentPropertyGridVM.MainObjectTypeFullname;
                    propertyGridVM.PropertyValue          = value?.ToString();
                    if (value != null)
                    {
                        var idPropName = dataService.GetIdPropertyName(propertyType);
                        var idProp     = propertyType.GetProperty(idPropName);
                        if (idProp == null)
                        {
                            throw new Exception($"Brak zdefiniowanej nazwy properji z Id dla typu {propertyType.FullName}");
                        }
                        var id = idProp.GetValue(value).ToString();
                        propertyGridVM.EditUrl = helper.GetEditUrlForClass(id, propertyType);
                        parentPropertyGridVM.Properties.Add(propertyGridVM);

                        createPropertyGridFromObject_internal(value, propertyGridVM, level - 1, bindingPath);
                    }
                    else
                    {
                        parentPropertyGridVM.Properties.Add(new PropertyGridVM()
                        {
                            PropertyName = item.Name, PropertyValue = "NULL", BindingPath = bindingPath
                        });
                    }
                }
                else
                {
                    var pvm = new SimplePropertyVM()
                    {
                        Type = propType, Header = item.Name, BindingPath = bindingPath, Value = helper.GetValue(value)
                    };

                    if (propType == PropertyTypes.String)
                    {
                        //jeśli duży string to wyświetlam jako opis
                        pvm.IsBigString = helper.IsBigString(value, BaseService.EnumPreviewType.PropertyGrid);
                        if (pvm.IsBigString)
                        {
                            pvm.Value = helper.GetPreview(value, BaseService.EnumPreviewType.PropertyGrid);
                            //jeśli xml to wyświetlam jako xml
                            try
                            {
                                var xml = value?.ToString() ?? "NULL";

                                //tu jest jakiś problem z kodowaniem, i bez tego hacka parser się wywala
                                xml = $"<Complex><Properties>{xml}</Properties></Complex>";

                                var complex = Serializer.DeserializeXml(xml);

                                //dodaje jeszcze propertygrida
                                var propertyGridElementViewModel = new PropertyGridVM();
                                propertyGridElementViewModel.IsStatic      = true;
                                propertyGridElementViewModel.PropertyName  = $"{item.Name}";
                                propertyGridElementViewModel.BindingPath   = $"{bindingPath}.autogenerated";
                                propertyGridElementViewModel.PropertyValue = " - autogenerated from xml";
                                createPropertyGridFromXml(complex, propertyGridElementViewModel);
                                parentPropertyGridVM.Properties.Add(propertyGridElementViewModel);
                            }
                            catch (Exception ex)
                            {
                                //nie jest to serizlizowany obiekt (<Complex> ... </Complex>)
                            }

                            //sprawdzam czy zwykły xml i formatuje go
                            try
                            {
                                var xml = pvm.Value?.ToString();
                                xml = $@"<?xml version=""1.0""?>
<Root>  
    {xml} 
</Root>";
                                var sb  = new StringBuilder();
                                var doc = XDocument.Parse(xml);
                                var tr  = new StringWriter(sb);
                                doc.Save(tr);
                                pvm.Value = (sb.ToString());
                            }
                            catch (Exception ex2)
                            {
                            }
                        }
                    }

                    pvm.Order       = item.GetPropertyOrder();
                    pvm.Description = item.GetPropertyDescription();
                    pvm.Group       = item.GetPropertyGroup();

                    parentPropertyGridVM.Properties.Add(pvm);
                }
            }
        }