Ejemplo n.º 1
0
        public object PersistLayout(PersistLayoutRequest request)
        {
            // Variables.
            var result        = default(object);
            var rootId        = CoreConstants.System.Root.ToInvariantString();
            var layoutsRootId = GuidHelper.GetGuid(LayoutConstants.Id);
            var parentId      = GuidHelper.GetGuid(request.ParentId);
            var kindId        = GuidHelper.GetGuid(request.KindId);


            // Catch all errors.
            try
            {
                // Parse or create the layout ID.
                var layoutId = string.IsNullOrWhiteSpace(request.LayoutId)
                    ? Guid.NewGuid()
                    : GuidHelper.GetGuid(request.LayoutId);


                // Get the ID path.
                var parent = parentId == Guid.Empty ? null : Entities.Retrieve(parentId);
                var path   = parent == null
                    ? new[] { layoutsRootId, layoutId }
                    : parent.Path.Concat(new[] { layoutId }).ToArray();


                // Create layout.
                var serializedData = JsonHelper.Serialize(request.Data);
                var layout         = new Layout()
                {
                    KindId = kindId,
                    Id     = layoutId,
                    Path   = path,
                    Name   = request.LayoutName,
                    Alias  = request.LayoutAlias,
                    Data   = serializedData
                };


                // Automatically populate the layout based on the form?
                var isBasic = GuidHelper.GetGuid(request.KindId) == GuidHelper.GetGuid(LayoutBasicConstants.Id);
                var config  = isBasic
                    ? new LayoutBasic().DeserializeConfiguration(serializedData) as LayoutBasicConfiguration
                    : null;
                var shouldAutopopulate = (config?.Autopopulate).GetValueOrDefault(false);
                var hasFormId          = (config?.FormId.HasValue).GetValueOrDefault(false);
                var form = hasFormId
                    ? FormPersistence.Retrieve(config.FormId.Value)
                    : null;
                if (isBasic && shouldAutopopulate && form != null)
                {
                    var autoLayoutData = JsonHelper.Serialize(new
                    {
                        rows = new[]
                        {
                            new
                            {
                                cells = new []
                                {
                                    new
                                    {
                                        columnSpan = 12,
                                        fields     = form.Fields.Select(x => new
                                        {
                                            id = GuidHelper.GetString(x.Id)
                                        })
                                    }
                                }
                            }
                        },
                        formId       = GuidHelper.GetString(form.Id),
                        autopopulate = true
                    });
                    layout.Data = autoLayoutData;
                }


                // Persist layout.
                Persistence.Persist(layout);


                // Variables.
                var fullPath = new[] { rootId }
                .Concat(path.Select(x => GuidHelper.GetString(x)))
                .ToArray();


                // Success.
                result = new
                {
                    Success = true,
                    Id      = GuidHelper.GetString(layoutId),
                    Path    = fullPath
                };
            }
            catch (Exception ex)
            {
                // Error.
                Logger.Error <LayoutsController>(ex, PersistLayoutError);
                result = new
                {
                    Success = false,
                    Reason  = UnhandledError
                };
            }


            // Return result.
            return(result);
        }
Ejemplo n.º 2
0
        public object PersistLayout(PersistLayoutRequest request)
        {
            // Variables.
            var result        = default(object);
            var rootId        = CoreConstants.System.Root.ToInvariantString();
            var layoutsRootId = GuidHelper.GetGuid(LayoutConstants.Id);
            var parentId      = GuidHelper.GetGuid(request.ParentId);
            var kindId        = GuidHelper.GetGuid(request.KindId);


            // Catch all errors.
            try
            {
                // Parse or create the layout ID.
                var layoutId = string.IsNullOrWhiteSpace(request.LayoutId)
                    ? Guid.NewGuid()
                    : GuidHelper.GetGuid(request.LayoutId);


                // Get the ID path.
                var parent = parentId == Guid.Empty ? null : Entities.Retrieve(parentId);
                var path   = parent == null
                    ? new[] { layoutsRootId, layoutId }
                    : parent.Path.Concat(new[] { layoutId }).ToArray();


                // Create layout.
                var layout = new Layout()
                {
                    KindId = kindId,
                    Id     = layoutId,
                    Path   = path,
                    Name   = request.LayoutName,
                    Alias  = request.LayoutAlias,
                    Data   = JsonHelper.Serialize(request.Data)
                };


                // Persist layout.
                Persistence.Persist(layout);


                // Variables.
                var fullPath = new[] { rootId }
                .Concat(path.Select(x => GuidHelper.GetString(x)))
                .ToArray();


                // Success.
                result = new
                {
                    Success = true,
                    Id      = GuidHelper.GetString(layoutId),
                    Path    = fullPath
                };
            }
            catch (Exception ex)
            {
                // Error.
                Logger.Error <LayoutsController>(ex, PersistLayoutError);
                result = new
                {
                    Success = false,
                    Reason  = UnhandledError
                };
            }


            // Return result.
            return(result);
        }
Ejemplo n.º 3
0
        public object PersistLayout(PersistLayoutRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();
            var layoutsRootId = GuidHelper.GetGuid(LayoutConstants.Id);
            var parentId = GuidHelper.GetGuid(request.ParentId);
            var kindId = GuidHelper.GetGuid(request.KindId);

            // Catch all errors.
            try
            {

                // Parse or create the layout ID.
                var layoutId = string.IsNullOrWhiteSpace(request.LayoutId)
                    ? Guid.NewGuid()
                    : GuidHelper.GetGuid(request.LayoutId);

                // Get the ID path.
                var parent = parentId == Guid.Empty ? null : Entities.Retrieve(parentId);
                var path = parent == null
                    ? new[] { layoutsRootId, layoutId }
                    : parent.Path.Concat(new[] { layoutId }).ToArray();

                // Create layout.
                var layout = new Layout()
                {
                    KindId = kindId,
                    Id = layoutId,
                    Path = path,
                    Name = request.LayoutName,
                    Alias = request.LayoutAlias,
                    Data = JsonHelper.Serialize(request.Data)
                };

                // Persist layout.
                Persistence.Persist(layout);

                // Variables.
                var fullPath = new[] { rootId }
                    .Concat(path.Select(x => GuidHelper.GetString(x)))
                    .ToArray();

                // Success.
                result = new
                {
                    Success = true,
                    Id = GuidHelper.GetString(layoutId),
                    Path = fullPath
                };

            }
            catch(Exception ex)
            {

                // Error.
                LogHelper.Error<LayoutsController>(PersistLayoutError, ex);
                result = new
                {
                    Success = false,
                    Reason = UnhandledError
                };

            }

            // Return result.
            return result;
        }