Example #1
0
        public object DeleteForm(DeleteFormRequest request)
        {
            // Variables.
            var result = default(object);


            // Catch all errors.
            try
            {
                // Variables.
                var formId  = GuidHelper.GetGuid(request.FormId);
                var configs = ConFormPersistence.RetrieveChildren(formId);


                // Delete the form and its configurations.
                foreach (var item in configs)
                {
                    ConFormPersistence.Delete(item.Id);
                }

                Persistence.Delete(formId);


                // Success.
                result = new
                {
                    Success = true
                };
            }
            catch (Exception ex)
            {
                // Error.
                Logger.Error <FormsController>(ex, DeleteFormError);
                result = new
                {
                    Success = false,
                    Reason  = UnhandledError
                };
            }


            // Return the result.
            return(result);
        }
Example #2
0
        public object MoveForm(MoveFormRequest request)
        {
            // Variables.
            var result   = default(object);
            var rootId   = CoreConstants.System.Root.ToInvariantString();
            var parentId = GuidHelper.GetGuid(request.NewParentId);


            // Catch all errors.
            try
            {
                // Declare list of anonymous type.
                var savedDescendants = new[]
                {
                    new
                    {
                        Id   = string.Empty,
                        Path = new string[] { }
                    }
                }.Take(0).ToList();


                // Variables.
                var formId        = GuidHelper.GetGuid(request.FormId);
                var form          = Persistence.Retrieve(formId);
                var parentPath    = Entities.Retrieve(parentId).Path;
                var oldFormPath   = form.Path;
                var oldParentPath = oldFormPath.Take(oldFormPath.Length - 1).ToArray();
                var configs       = ConFormPersistence.RetrieveChildren(formId);


                // Move form and configurations.
                var path = EntityHelper.GetClientPath(Entities.MoveEntity(form, parentPath));
                foreach (var config in configs)
                {
                    var descendantParentPath = config.Path.Take(config.Path.Length - 1);
                    var descendantPathEnd    = descendantParentPath.Skip(oldParentPath.Length);
                    var newParentPath        = parentPath.Concat(descendantPathEnd).ToArray();
                    var clientPath           = EntityHelper.GetClientPath(
                        Entities.MoveEntity(config, newParentPath));
                    savedDescendants.Add(new
                    {
                        Id   = GuidHelper.GetString(config.Id),
                        Path = clientPath
                    });
                }


                // Success.
                result = new
                {
                    Success     = true,
                    Id          = GuidHelper.GetString(formId),
                    Path        = path,
                    Descendants = savedDescendants.ToArray()
                };
            }
            catch (Exception ex)
            {
                // Error.
                Logger.Error <FormsController>(MoveFormError, ex);
                result = new
                {
                    Success = false,
                    Reason  = UnhandledError
                };
            }


            // Return result.
            return(result);
        }
Example #3
0
        public object PersistForm(PersistFormRequest request)
        {
            // Variables.
            var result      = default(object);
            var formsRootId = GuidHelper.GetGuid(FormConstants.Id);
            var parentId    = GuidHelper.GetGuid(request.ParentId);


            // Catch all errors.
            try
            {
                // Parse or create the form ID.
                var isNew  = string.IsNullOrWhiteSpace(request.FormId);
                var formId = isNew
                    ? Guid.NewGuid()
                    : GuidHelper.GetGuid(request.FormId);


                // Get the fields.
                var fields = request.Fields.MakeSafe()
                             .Select(x =>
                {
                    var fieldType         = Type.GetType(x.TypeFullName);
                    var fieldTypeInstance = FormFieldTypeCollection
                                            .FirstOrDefault(y => y.GetType() == fieldType);

                    var field = new FormField(fieldTypeInstance)
                    {
                        Id = string.IsNullOrWhiteSpace(x.Id)
                                ? Guid.NewGuid()
                                : GuidHelper.GetGuid(x.Id),
                        Alias       = x.Alias,
                        Name        = x.Name,
                        Label       = x.Label,
                        Category    = x.Category,
                        Validations = x.Validations.MakeSafe()
                                      .Select(y => GuidHelper.GetGuid(y)).ToArray(),
                        FieldConfiguration = JsonHelper.Serialize(x.Configuration)
                    };
                    return(field);
                })
                             .ToArray();


                // Get the handlers.
                var handlers = request.Handlers.MakeSafe().Select(x =>
                {
                    var handlerType         = Type.GetType(x.TypeFullName);
                    var handlerTypeInstance = FormHandlerTypeCollection
                                              .FirstOrDefault(y => y.GetType() == handlerType);

                    var handler = new FormHandler(handlerTypeInstance)
                    {
                        Id = string.IsNullOrWhiteSpace(x.Id)
                            ? Guid.NewGuid()
                            : GuidHelper.GetGuid(x.Id),
                        Alias   = x.Alias,
                        Name    = x.Name,
                        Enabled = x.Enabled,
                        HandlerConfiguration = JsonHelper.Serialize(x.Configuration)
                    };

                    return(handler);
                }).ToArray();


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


                // Create the form.
                var form = new Form()
                {
                    Id       = formId,
                    Path     = path,
                    Alias    = request.Alias,
                    Name     = request.Name,
                    Fields   = fields,
                    Handlers = handlers
                };


                // Persist the form.
                Persistence.Persist(form);


                // For new forms, automatically create a layout and a form configuration.
                var layoutNamePrefix  = "Layout for ";
                var layoutNameSuffix  = " (Autogenerated)";
                var layoutAliasPrefix = "layout_";
                var layoutAliasSuffix = "_autogenerated";
                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
                });
                if (isNew)
                {
                    // Create new layout.
                    var layoutId = Guid.NewGuid();
                    var layout   = new Layout()
                    {
                        KindId = GuidHelper.GetGuid(app.Constants.Layouts.LayoutBasic.Id),
                        Id     = layoutId,
                        Path   = new[] { GuidHelper.GetGuid(LayoutConstants.Id), layoutId },
                        Name   = layoutNamePrefix + request.Name + layoutNameSuffix,
                        Alias  = layoutAliasPrefix + request.Alias + layoutAliasSuffix,
                        Data   = autoLayoutData
                    };


                    // Persist layout.
                    LayoutPersistence.Persist(layout);


                    // Create a new form configuration.
                    var plainJsTemplateId = GuidHelper.GetGuid("f3fb1485c1d14806b4190d7abde39530");
                    var template          = Config.Templates.FirstOrDefault(x => x.Id == plainJsTemplateId)
                                            ?? Config.Templates.FirstOrDefault();
                    var configId       = Guid.NewGuid();
                    var configuredForm = new ConfiguredForm()
                    {
                        Id         = configId,
                        Path       = path.Concat(new[] { configId }).ToArray(),
                        Name       = request.Name,
                        TemplateId = template?.Id,
                        LayoutId   = layoutId
                    };


                    // Persist form configuration.
                    ConFormPersistence.Persist(configuredForm);
                }


                // Get existing layouts that should autopopulate.
                var layouts = GetFormLayouts(null)
                              .Select(x => new
                {
                    Layout        = x,
                    Configuration = x.DeserializeConfiguration() as LayoutBasicConfiguration
                })
                              .Where(x => x.Configuration != null)
                              .Where(x => x.Configuration.FormId.HasValue && x.Configuration.FormId.Value == formId)
                              .Where(x => x.Configuration.Autopopulate);


                //: Autopopulate the layouts.
                foreach (var existingLayout in layouts)
                {
                    existingLayout.Layout.Data = autoLayoutData;
                    var layoutName  = existingLayout.Layout.Name ?? string.Empty;
                    var layoutAlias = existingLayout.Layout.Alias ?? string.Empty;
                    if (layoutName.StartsWith(layoutNamePrefix) && layoutName.EndsWith(layoutNameSuffix))
                    {
                        existingLayout.Layout.Name = layoutNamePrefix + form.Name + layoutNameSuffix;
                    }
                    if (layoutAlias.StartsWith(layoutAliasPrefix) && layoutAlias.EndsWith(layoutAliasSuffix))
                    {
                        existingLayout.Layout.Alias = layoutAliasPrefix + form.Name + layoutAliasSuffix;
                    }
                    LayoutPersistence.Persist(existingLayout.Layout);
                }


                // Success.
                result = new
                {
                    Success = true,
                    FormId  = GuidHelper.GetString(formId)
                };
            }
            catch (Exception ex)
            {
                // Error.
                Logger.Error <FormsController>(ex, PersistFormError);
                result = new
                {
                    Success = false,
                    Reason  = UnhandledError
                };
            }


            // Return the result.
            return(result);
        }
Example #4
0
        public object DuplicateForm(DuplicateFormRequest request)
        {
            // Variables.
            var result = default(object);


            // Catch all errors.
            try
            {
                // Variables.
                var oldFormId  = GuidHelper.GetGuid(request.FormId);
                var parentId   = GuidHelper.GetGuid(request.ParentId);
                var oldForm    = Persistence.Retrieve(oldFormId);
                var oldConfigs = ConFormPersistence.RetrieveChildren(oldFormId);
                var newFormId  = Guid.NewGuid();
                var layoutMap  = new Dictionary <Guid, Guid>();


                // Get the new form ID path.
                var newFormPath = oldForm.Path
                                  .Take(oldForm.Path.Length - 1)
                                  .Concat(new[] { newFormId })
                                  .ToArray();


                // Create the duplicate form.
                var newForm = new Form()
                {
                    Id       = newFormId,
                    Path     = newFormPath,
                    Alias    = oldForm.Alias,
                    Name     = $"{oldForm.Name} (Copy)",
                    Fields   = oldForm.Fields,
                    Handlers = oldForm.Handlers
                };


                // Persist the duplicate form.
                Persistence.Persist(newForm);


                // Duplicate the existing layouts (ensuring each gets a new layout ID).
                var oldLayoutIds = oldConfigs
                                   .Select(x => x.LayoutId)
                                   .Where(x => x.HasValue)
                                   .Select(x => x.Value);
                foreach (var layoutId in oldLayoutIds)
                {
                    // Duplicate layout.
                    var oldLayout = LayoutPersistence.Retrieve(layoutId);
                    var newLayout = oldLayout.Duplicate(newFormId);


                    // Persist duplicated layout.
                    LayoutPersistence.Persist(newLayout);


                    // Map old layout ID to new layout ID.
                    layoutMap[layoutId] = newLayout.Id;
                }


                // Duplicate form configurations.
                foreach (var oldConfig in oldConfigs)
                {
                    // Duplicate the form configuration.
                    var newConfigId = Guid.NewGuid();
                    var newConfig   = new ConfiguredForm()
                    {
                        Id         = newConfigId,
                        Path       = newFormPath.Concat(new[] { newConfigId }).ToArray(),
                        Name       = oldConfig.Name,
                        TemplateId = oldConfig.TemplateId,
                        LayoutId   = oldConfig.LayoutId.HasValue
                            ? layoutMap[oldConfig.LayoutId.Value]
                            : default(Guid?)
                    };


                    // Persist form configuration.
                    ConFormPersistence.Persist(newConfig);
                }


                // Success.
                result = new
                {
                    Success = true,
                    FormId  = GuidHelper.GetString(newFormId),
                    Path    = newFormPath
                };
            }
            catch (Exception ex)
            {
                // Error.
                Logger.Error <FormsController>(ex, DuplicateFormError);
                result = new
                {
                    Success = false,
                    Reason  = UnhandledError
                };
            }


            // Return the result.
            return(result);
        }