public IEnumerable <object> GetEntitiesByIds(FluiditySectionConfig section, FluidityCollectionConfig collection, object[] ids)
        {
            // Construct where clause
            LambdaExpression whereClauseExp = null;

            // Create shared expressions
            var parameter  = Expression.Parameter(collection.EntityType);
            var idPropInfo = collection.IdProperty.PropertyInfo;

            // Loop through ids
            foreach (var id in ids)
            {
                // Create id comparrisons
                var property = Expression.Property(parameter, idPropInfo);
                var idsConst = Expression.Constant(TypeDescriptor.GetConverter(idPropInfo.PropertyType).ConvertFrom(id), idPropInfo.PropertyType);
                var compare  = Expression.Equal(property, idsConst);
                var lambda   = Expression.Lambda(compare, parameter);

                // Combine clauses
                whereClauseExp = whereClauseExp == null
                    ? lambda
                    : Expression.Lambda(Expression.OrElse(whereClauseExp.Body, lambda.Body), parameter);
            }

            // Perform the query
            PagedResult <object> result;

            using (var repo = RepositoryFactory.GetRepository(collection))
            {
                result = repo?.GetPaged(1, ids.Length, whereClauseExp, null, SortDirection.Ascending);
            }

            // Return the results
            return(result?.Items);
        }
 public FluiditySectionDisplayModel ToDisplayModel(FluiditySectionConfig section)
 {
     return(new FluiditySectionDisplayModel
     {
         Alias = section.Alias,
         Name = section.Name,
         Tree = section.Tree.Alias
     });
 }
        public IEnumerable <FluidityEntityDisplayModel> GetEntityDisplayModelsByIds(FluiditySectionConfig section, FluidityCollectionConfig collection, object[] ids)
        {
            // Get the entities
            var entities = GetEntitiesByIds(section, collection, ids);

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

            // Map the results to the view display
            var mapper = new FluidityEntityMapper();

            return(entities.Select(x => mapper.ToDisplayModel(section, collection, x)));
        }
Exemple #4
0
        public FluidityEntityEditModel GetEntityEditModel(FluiditySectionConfig section, FluidityCollectionConfig collection, object entityOrId = null)
        {
            var repo = RepositoryFactory.GetRepository(collection);

            object entity = null;

            if (entityOrId != null)
            {
                entity = (entityOrId.GetType() == collection.EntityType ? entityOrId : null) ?? repo.Get(entityOrId);
            }

            var mapper   = new FluidityEntityMapper();
            var scaffold = mapper.ToEditModel(section, collection, entity);

            return(scaffold);
        }
Exemple #5
0
        public FluidityCollectionDisplayModel ToDisplayModel(FluiditySectionConfig section, FluidityCollectionConfig collection, bool includeListView)
        {
            var m = new FluidityCollectionDisplayModel
            {
                Section      = section.Alias,
                Tree         = section.Tree.Alias,
                Alias        = collection.Alias,
                NameSingular = collection.NameSingular,
                NamePlural   = collection.NamePlural,
                IconSingular = collection.IconSingular + (!collection.IconColor.IsNullOrWhiteSpace() ? " color-" + collection.IconColor : ""),
                IconPlural   = collection.IconPlural + (!collection.IconColor.IsNullOrWhiteSpace() ? " color-" + collection.IconColor : ""),
                Description  = collection.Description,
                IsReadOnly   = collection.IsReadOnly,
                IsSearchable = collection.SearchableProperties.Any(),
                HasListView  = collection.ViewMode == FluidityViewMode.List,
                Path         = collection.Path
            };

            if (includeListView)
            {
                m.ListView = new FluidityListViewDisplayModel
                {
                    PageSize   = collection.ListView.PageSize,
                    Properties = collection.ListView.Fields.Select(x =>
                    {
                        // Calculate heading
                        var heading = x.Heading;
                        if (heading.IsNullOrWhiteSpace())
                        {
                            var attr = x.Property.PropertyInfo.GetCustomAttribute <DisplayNameAttribute>(true);
                            if (attr != null)
                            {
                                heading = attr.DisplayName;
                            }
                            else
                            {
                                heading = x.Property.Name.SplitPascalCasing();
                            }
                        }

                        // Build property
                        return(new FluidityListViewPropertyDisplayModel // We don't include Name, as it's always automatically included
                        {
                            Alias = x.Property.Name,
                            Header = heading,
                            AllowSorting = true,
                            IsSystem = false
                        });
                    }),
                    Layouts = collection.ListView.Layouts.Select((x, idx) => new FluidityListViewLayoutDisplayModel
                    {
                        Icon     = x.Icon,
                        Name     = x.Name,
                        Path     = x.View,
                        IsSystem = x.IsSystem,
                        Selected = true
                    }),
                    DataViews   = collection.ListView.DataViewsBuilder?.GetDataViews() ?? new FluidityDataViewSummary[0],
                    BulkActions = collection.ListView.BulkActions.Select(x => new FluidityListViewBulkActionDisplayModel
                    {
                        Icon  = x.Icon,
                        Alias = x.Alias,
                        Name  = x.Name,
                        AngularServiceName = x.AngularServiceName
                    })
                };
            }

            return(m);
        }
        public PagedResult <FluidityEntityDisplayModel> GetEntityDisplayModels(FluiditySectionConfig section, FluidityCollectionConfig collection, int pageNumber = 1, int pageSize = 10, string query = null, string orderBy = null, string orderDirection = null, string dataView = null)
        {
            // Construct where clause
            LambdaExpression whereClauseExp = null;

            // Determine the data view where clause
            if (!dataView.IsNullOrWhiteSpace())
            {
                var wc = collection.ListView?.DataViewsBuilder?.GetDataViewWhereClause(dataView);
                if (wc != null)
                {
                    whereClauseExp = wc;
                }
            }

            // Construct a query where clause (and combind with the data view where clause if one exists)
            if (!query.IsNullOrWhiteSpace() && collection.ListView != null && collection.SearchableProperties.Any())
            {
                LambdaExpression queryExpression = null;

                // Create shared expressions
                var parameter = whereClauseExp != null
                    ? whereClauseExp.Parameters.First()
                    : Expression.Parameter(collection.EntityType);

                var queryConstantExpression = Expression.Constant(query, typeof(string));

                // Loop through searchable fields
                foreach (var searchProp in collection.SearchableProperties)
                {
                    // Create field starts with expression
                    var property       = Expression.Property(parameter, searchProp.PropertyInfo);
                    var startsWithCall = Expression.Call(property, "StartsWith", null, queryConstantExpression);
                    var lambda         = Expression.Lambda(startsWithCall, parameter);

                    // Combine query
                    queryExpression = queryExpression == null
                        ? lambda
                        : Expression.Lambda(Expression.OrElse(queryExpression.Body, lambda.Body), parameter);
                }

                // Combine query with any existing where clause
                if (queryExpression != null)
                {
                    whereClauseExp = whereClauseExp == null
                        ? queryExpression
                        : Expression.Lambda(Expression.AndAlso(whereClauseExp.Body, queryExpression.Body), parameter);
                }
            }

            // Parse the order by
            LambdaExpression orderByExp = null;

            if (!orderBy.IsNullOrWhiteSpace() && !orderBy.InvariantEquals("name"))
            {
                // Convert string into an Expression<Func<TEntityType, object>>
                var prop = collection.EntityType.GetProperty(orderBy);
                if (prop != null)
                {
                    var parameter    = Expression.Parameter(collection.EntityType);
                    var property     = Expression.Property(parameter, prop);
                    var castToObject = Expression.Convert(property, typeof(object));
                    orderByExp = Expression.Lambda(castToObject, parameter);
                }
            }

            var orderDir = !orderDirection.IsNullOrWhiteSpace()
                ? orderDirection.InvariantEquals("asc") ? SortDirection.Ascending : SortDirection.Descending
                : collection.SortDirection;

            PagedResult <object> result;

            using (var repo = RepositoryFactory.GetRepository(collection))
            {
                // Perform the query
                result = repo?.GetPaged(pageNumber, pageSize, whereClauseExp, orderByExp, orderDir);
            }

            // If we've got no results, return an empty result set
            if (result == null)
            {
                return(new PagedResult <FluidityEntityDisplayModel>(0, pageNumber, pageSize));
            }

            // Map the results to the view display
            var mapper = new FluidityEntityMapper();

            return(new PagedResult <FluidityEntityDisplayModel>(result.TotalItems, pageNumber, pageSize)
            {
                Items = result.Items.Select(x => mapper.ToDisplayModel(section, collection, x))
            });
        }
        public FluidityCollectionDisplayModel GetCollectionDisplayModel(FluiditySectionConfig section, FluidityCollectionConfig collection, bool includeListView)
        {
            var collectionMapper = new FluidityCollectionMapper();

            return(collectionMapper.ToDisplayModel(section, collection, includeListView));
        }
        public IEnumerable <FluidityCollectionDisplayModel> GetDashboardCollectionDisplayModels(FluiditySectionConfig section)
        {
            var collectionMapper = new FluidityCollectionMapper();

            return(section.Tree.FlattenedTreeItems.Values
                   .Where(x => x is FluidityCollectionConfig && ((FluidityCollectionConfig)x).IsVisibleOnDashboard)
                   .Select(x => collectionMapper.ToDisplayModel(section, (FluidityCollectionConfig)x, false)));
        }
        public FluiditySectionDisplayModel GetSectionDisplayModel(FluiditySectionConfig section)
        {
            var sectionMapper = new FluiditySectionMapper();

            return(sectionMapper.ToDisplayModel(section));
        }
Exemple #10
0
        public FluidityEntityDisplayModel ToDisplayModel(FluiditySectionConfig section, FluidityCollectionConfig collection, object entity)
        {
            var entityId          = entity?.GetPropertyValue(collection.IdProperty);
            var entityCompositeId = entityId != null
                ? collection.Alias + "!" + entityId
                : null;

            var name = "";

            if (collection.NameProperty != null)
            {
                name = entity.GetPropertyValue(collection.NameProperty).ToString();
            }
            else if (collection.NameFormat != null)
            {
                name = collection.NameFormat(entity);
            }
            else
            {
                name = entity?.ToString();
            }

            var display = new FluidityEntityDisplayModel
            {
                Id         = entity?.GetPropertyValue(collection.IdProperty),
                Name       = name,
                Icon       = collection.IconSingular + (collection.IconColor != null ? " color-" + collection.IconColor : ""),
                Section    = section.Alias,
                Tree       = section.Tree.Alias,
                Collection = collection.Alias,
                CreateDate = entity != null && collection.DateCreatedProperty != null ? (DateTime)entity.GetPropertyValue(collection.DateCreatedProperty) : DateTime.MinValue,
                UpdateDate = entity != null && collection.DateModifiedProperty != null ? (DateTime)entity.GetPropertyValue(collection.DateModifiedProperty) : DateTime.MinValue,
                EditPath   = $"{section.Alias}/fluidity/edit/{entityCompositeId}",
            };

            if (collection.ListView != null)
            {
                var properties = new List <ContentPropertyBasic>();

                foreach (var field in collection.ListView.Fields)
                {
                    var value = entity?.GetPropertyValue(field.Property);

                    var encryptedProp = collection.EncryptedProperties?.FirstOrDefault(x => x.Name == field.Property.Name);
                    if (encryptedProp != null)
                    {
                        value = SecurityHelper.Decrypt(value.ToString());
                    }

                    if (field.Format != null)
                    {
                        value = field.Format(value, entity);
                    }

                    var propertyScaffold = new ContentPropertyBasic
                    {
                        Id    = properties.Count,
                        Alias = field.Property.Name,
                        Value = value?.ToString()
                    };

                    properties.Add(propertyScaffold);
                }

                display.Properties = properties;
            }

            return(display);
        }
Exemple #11
0
        public object FromPostModel(FluiditySectionConfig section, FluidityCollectionConfig collection, FluidityEntityPostModel postModel, object entity)
        {
            var editorProps = collection.Editor.Tabs.SelectMany(x => x.Fields).ToArray();

            // Update the name property
            if (collection.NameProperty != null)
            {
                entity.SetPropertyValue(collection.NameProperty, postModel.Name);
            }

            // Update the individual properties
            foreach (var prop in postModel.Properties)
            {
                // Get the prop config
                var propConfig = editorProps.First(x => x.Property.Name == prop.Alias);
                if (!propConfig.IsReadOnly)
                {
                    // Create additional data for file handling
                    var additionalData = new Dictionary <string, object>();

                    // Grab any uploaded files and add them to the additional data
                    var files = postModel.UploadedFiles.Where(x => x.PropertyAlias == prop.Alias).ToArray();
                    if (files.Length > 0)
                    {
                        additionalData.Add("files", files);
                    }

                    // Ensure safe filenames
                    foreach (var file in files)
                    {
                        file.FileName = file.FileName.ToSafeFileName();
                    }

                    // Add extra things needed to figure out where to put the files
                    // Looking into the core code, these are not actually used for any lookups,
                    // rather they are used to generate a unique path, so we just use the nearest
                    // equivilaants from the fluidity api.
                    var cuid = $"{section.Alias}_{collection.Alias}_{entity.GetPropertyValue(collection.IdProperty)}";
                    var puid = $"{section.Alias}_{collection.Alias}_{propConfig.Property.Name}";

                    additionalData.Add("cuid", ObjectExtensions.EncodeAsGuid(cuid));
                    additionalData.Add("puid", ObjectExtensions.EncodeAsGuid(puid));

                    var dataTypeInfo = _dataTypeHelper.ResolveDataType(propConfig, collection.IsReadOnly);
                    var data         = new ContentPropertyData(prop.Value, dataTypeInfo.PreValues, additionalData);

                    if (!dataTypeInfo.PropertyEditor.ValueEditor.IsReadOnly)
                    {
                        var currentValue = entity.GetPropertyValue(propConfig.Property);

                        var encryptedProp = collection.EncryptedProperties?.FirstOrDefault(x => x.Name == propConfig.Property.Name);
                        if (encryptedProp != null)
                        {
                            currentValue = SecurityHelper.Decrypt(currentValue.ToString());
                        }

                        if (propConfig.ValueMapper != null)
                        {
                            currentValue = propConfig.ValueMapper.ModelToEditor(currentValue);
                        }

                        var propVal = dataTypeInfo.PropertyEditor.ValueEditor.ConvertEditorToDb(data, currentValue);
                        var supportTagsAttribute = TagExtractor.GetAttribute(dataTypeInfo.PropertyEditor);
                        if (supportTagsAttribute != null)
                        {
                            var dummyProp = new Property(new PropertyType(dataTypeInfo.DataTypeDefinition), propVal);
                            TagExtractor.SetPropertyTags(dummyProp, data, propVal, supportTagsAttribute);
                            propVal = dummyProp.Value;
                        }

                        if (propConfig.ValueMapper != null)
                        {
                            propVal = propConfig.ValueMapper.EditorToModel(propVal);
                        }

                        if (encryptedProp != null)
                        {
                            propVal = SecurityHelper.Encrypt(propVal.ToString());
                        }

                        if (propVal != null && propVal.GetType() != propConfig.Property.Type)
                        {
                            var convert = propVal.TryConvertTo(propConfig.Property.Type);
                            if (convert.Success)
                            {
                                propVal = convert.Result;
                            }
                        }

                        entity.SetPropertyValue(propConfig.Property, propVal);
                    }
                }
            }

            return(entity);
        }
Exemple #12
0
        public FluidityEntityEditModel ToEditModel(FluiditySectionConfig section, FluidityCollectionConfig collection, object entity)
        {
            var isNew             = entity == null;
            var entityId          = entity?.GetPropertyValue(collection.IdProperty);
            var entityCompositeId = entityId != null
                ? collection.Alias + "!" + entityId
                : null;

            var display = new FluidityEntityEditModel
            {
                Id   = entity?.GetPropertyValue(collection.IdProperty) ?? collection.IdProperty.Type.GetDefaultValue(),
                Name = collection?.NameProperty != null?entity?.GetPropertyValue(collection.NameProperty).ToString() : collection.NameSingular,
                           HasNameProperty        = collection.NameProperty != null,
                           Section                = section.Alias,
                           Tree                   = section.Tree.Alias,
                           CollectionIsReadOnly   = collection.IsReadOnly,
                           Collection             = collection.Alias,
                           CollectionNameSingular = collection.NameSingular,
                           CollectionNamePlural   = collection.NamePlural,
                           CollectionIconSingular = collection.IconSingular,
                           CollectionIconPlural   = collection.IconPlural,
                           IsChildOfListView      = collection.ViewMode == FluidityViewMode.List,
                           IsChildOfTreeView      = collection.ViewMode == FluidityViewMode.Tree,
                           TreeNodeUrl            = "/umbraco/backoffice/fluidity/FluidityTree/GetTreeNode/" + entityCompositeId + "?application=" + section.Alias,
                           CreateDate             = entity != null && collection.DateCreatedProperty != null ? (DateTime)entity.GetPropertyValue(collection.DateCreatedProperty) : DateTime.MinValue,
                           UpdateDate             = entity != null && collection.DateModifiedProperty != null ? (DateTime)entity.GetPropertyValue(collection.DateCreatedProperty) : DateTime.MinValue,
                           Path                   = collection.Path + (entity != null ? FluidityConstants.PATH_SEPERATOR + collection.Alias + "!" + entity.GetPropertyValue(collection.IdProperty) : string.Empty)
            };

            if (collection.Editor?.Tabs != null)
            {
                var tabs = new List <Tab <ContentPropertyDisplay> >();
                foreach (var tab in collection.Editor.Tabs)
                {
                    var tabScaffold = new Tab <ContentPropertyDisplay>
                    {
                        Id       = tabs.Count,
                        Alias    = tab.Name,
                        Label    = tab.Name,
                        IsActive = tabs.Count == 0
                    };

                    var properties = new List <ContentPropertyDisplay>();
                    if (tab.Fields != null)
                    {
                        foreach (var field in tab.Fields)
                        {
                            var dataTypeInfo = _dataTypeHelper.ResolveDataType(field, collection.IsReadOnly);

                            dataTypeInfo.PropertyEditor.ValueEditor.ConfigureForDisplay(dataTypeInfo.PreValues);

                            var propEditorConfig = dataTypeInfo.PropertyEditor.PreValueEditor.ConvertDbToEditor(dataTypeInfo.PropertyEditor.DefaultPreValues,
                                                                                                                dataTypeInfo.PreValues);

                            // Calculate value
                            object value = !isNew
                                ? entity?.GetPropertyValue(field.Property)
                                : field.DefaultValueFunc != null?field.DefaultValueFunc() : field.Property.Type.GetDefaultValue();

                            var encryptedProp = collection.EncryptedProperties?.FirstOrDefault(x => x.Name == field.Property.Name);
                            if (encryptedProp != null)
                            {
                                value = SecurityHelper.Decrypt(value.ToString());
                            }

                            if (field.ValueMapper != null)
                            {
                                value = field.ValueMapper.ModelToEditor(value);
                            }

                            var dummyProp = new Property(new PropertyType(dataTypeInfo.DataTypeDefinition), value);
                            value = dataTypeInfo.PropertyEditor.ValueEditor.ConvertDbToEditor(dummyProp, dummyProp.PropertyType, _dataTypeService);

                            // Calculate label
                            var label = field.Label;
                            if (label.IsNullOrWhiteSpace())
                            {
                                var attr = field.Property.PropertyInfo.GetCustomAttribute <DisplayNameAttribute>(true);
                                if (attr != null)
                                {
                                    label = attr.DisplayName;
                                }
                                else
                                {
                                    label = field.Property.Name.SplitPascalCasing();
                                }
                            }

                            // Calculate description
                            var description = field.Description;
                            if (description.IsNullOrWhiteSpace())
                            {
                                var attr = field.Property.PropertyInfo.GetCustomAttribute <DescriptionAttribute>(true);
                                if (attr != null)
                                {
                                    description = attr.Description;
                                }
                            }

                            var propertyScaffold = new ContentPropertyDisplay
                            {
                                Id          = properties.Count,
                                Alias       = field.Property.Name,
                                Label       = label,
                                Description = description,
                                Editor      = dataTypeInfo.PropertyEditor.Alias,
                                View        = dataTypeInfo.PropertyEditor.ValueEditor.View,
                                Config      = propEditorConfig,
                                HideLabel   = dataTypeInfo.PropertyEditor.ValueEditor.HideLabel,
                                Value       = value
                            };

                            properties.Add(propertyScaffold);
                        }
                    }
                    tabScaffold.Properties = properties;

                    tabs.Add(tabScaffold);
                }

                display.Tabs = tabs;
            }

            return(display);
        }