Example #1
0
 /// <summary>
 /// Converts a server-side entity path to a client-side entity path.
 /// </summary>
 /// <param name="path">
 /// The server-side entity path.
 /// </param>
 /// <returns>
 /// The client-side entity path.
 /// </returns>
 /// <remarks>
 /// The client-side entity path expects an extra root ID of "-1",
 /// which this method includes.
 /// </remarks>
 public static string[] GetClientPath(Guid[] path)
 {
     var rootId = CoreConstants.System.Root.ToInvariantString();
     var clientPath = new[] { rootId }
         .Concat(path.Select(x => GuidHelper.GetString(x)))
         .ToArray();
     return clientPath;
 }
        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.
                LogHelper.Error<FormsController>(MoveFormError, ex);
                result = new
                {
                    Success = false,
                    Reason = UnhandledError
                };

            }

            // Return result.
            return result;
        }
        public object GetFormInfo([FromUri] GetFormInfoRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();

            // Catch all errors.
            try
            {

                // Variables.
                var id = GuidHelper.GetGuid(request.FormId);
                var form = Persistence.Retrieve(id);
                var fullPath = new[] { rootId }
                    .Concat(form.Path.Select(x => GuidHelper.GetString(x)))
                    .ToArray();

                // Set result.
                result = new
                {
                    Success = true,
                    FormId = GuidHelper.GetString(form.Id),
                    Path = fullPath,
                    Alias = form.Alias,
                    Name = form.Name,
                    Fields = form.Fields.MakeSafe().Select(x => new
                    {
                        Id = GuidHelper.GetString(x.Id),
                        x.Alias,
                        x.Name,
                        x.Label,
                        Validations = x.Validations.MakeSafe()
                            .Select(y => Validations.Retrieve(y))
                            .WithoutNulls()
                            .Select(y => new
                            {
                                Id = GuidHelper.GetString(y.Id),
                                Name = y.Name
                            }).ToArray(),
                        Configuration = JsonHelper.Deserialize<object>(
                            x.FieldConfiguration),
                        Directive = x.GetDirective(),
                        Icon = x.GetIcon(),
                        TypeLabel = x.GetTypeLabel(),
                        TypeFullName = x.GetFieldType().AssemblyQualifiedName
                    }).ToArray(),
                    Handlers = form.Handlers.MakeSafe().Select(x => new
                    {
                        Id = GuidHelper.GetString(x.Id),
                        x.Alias,
                        x.Name,
                        Configuration = JsonHelper.Deserialize<object>(
                            x.HandlerConfiguration),
                        Directive = x.GetDirective(),
                        Icon = x.GetIcon(),
                        TypeLabel = x.GetTypeLabel(),
                        TypeFullName = x.GetHandlerType().AssemblyQualifiedName
                    }).ToArray()
                };

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
        public object PersistValidation(PersistValidationRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();
            var validationsRootId = GuidHelper.GetGuid(ValidationConstants.Id);
            var parentId = GuidHelper.GetGuid(request.ParentId);
            var kindId = GuidHelper.GetGuid(request.KindId);

            // Catch all errors.
            try
            {

                // Parse or create the validation ID.
                var validationId = string.IsNullOrWhiteSpace(request.ValidationId)
                    ? Guid.NewGuid()
                    : GuidHelper.GetGuid(request.ValidationId);

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

                // Create validation.
                var validation = new Validation()
                {
                    KindId = kindId,
                    Id = validationId,
                    Path = path,
                    Name = request.ValidationName,
                    Alias = request.ValidationAlias,
                    Data = JsonHelper.Serialize(request.Data)
                };

                // Persist validation.
                Persistence.Persist(validation);

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

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

            }
            catch(Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
        public object MoveValidation(MoveValidationRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();
            var parentId = GuidHelper.GetGuid(request.NewParentId);

            // Catch all errors.
            try
            {

                // Parse the validation ID.
                var validationId = GuidHelper.GetGuid(request.ValidationId);

                // Get the ID path.
                var path = Entities.Retrieve(parentId).Path
                    .Concat(new[] { validationId }).ToArray();

                // Get validation and update path.
                var validation = Persistence.Retrieve(validationId);
                validation.Path = path;

                // Persist validation.
                Persistence.Persist(validation);

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

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

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
        public object GetValidationsInfo(
            [FromUri] GetValidationsInfoRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();
            var validations = new List<object>();

            // Catch all errors.
            try
            {

                // Variables.
                var ids = request.ValidationIds
                    .MakeSafe()
                    .Select(x => GuidHelper.GetGuid(x)).ToList();
                var kinds = ValidationHelper.GetAllValidationKinds();

                // Get information about each validation.
                foreach (var id in ids)
                {

                    // Get validation.
                    var validation = Persistence.Retrieve(id);
                    if (validation == null)
                    {
                        continue;
                    }

                    // Get path to validation.
                    var partialPath = validation.Path
                        .Select(x => GuidHelper.GetString(x));
                    var fullPath = new[] { rootId }
                        .Concat(partialPath).ToArray();

                    // Get directive.
                    var directive = kinds.Where(x => x.Id == validation.KindId)
                        .Select(x => x.Directive).FirstOrDefault();

                    // Store validation info.
                    validations.Add(new
                    {
                        ValidationId = GuidHelper.GetString(validation.Id),
                        KindId = GuidHelper.GetString(validation.KindId),
                        Path = fullPath,
                        Alias = validation.Alias,
                        Name = validation.Name,
                        Directive = directive,
                        Data = JsonHelper.Deserialize<object>(validation.Data)
                    });

                }

                // Set result.
                result = new
                {
                    Success = true,
                    Validations = validations.ToArray()
                };

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
        public object GetValidationInfo(
            [FromUri] GetValidationInfoRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();

            // Catch all errors.
            try
            {

                // Variables.
                var id = GuidHelper.GetGuid(request.ValidationId);
                var validation = Persistence.Retrieve(id);
                var fullPath = new[] { rootId }
                    .Concat(validation.Path.Select(x => GuidHelper.GetString(x)))
                    .ToArray();
                var kinds = ValidationHelper.GetAllValidationKinds();
                var directive = kinds.Where(x => x.Id == validation.KindId)
                    .Select(x => x.Directive).FirstOrDefault();

                // Set result.
                result = new
                {
                    Success = true,
                    ValidationId = GuidHelper.GetString(validation.Id),
                    KindId = GuidHelper.GetString(validation.KindId),
                    Path = fullPath,
                    Alias = validation.Alias,
                    Name = validation.Name,
                    Directive = directive,
                    Data = JsonHelper.Deserialize<object>(validation.Data)
                };

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
        public object GetFolderInfo([FromUri] GetFolderInfoRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();

            // Catch all errors.
            try
            {

                // Variables.
                var id = GuidHelper.GetGuid(request.FolderId);
                var folder = Persistence.Retrieve(id);
                var partialPath = folder.Path
                    .Select(x => GuidHelper.GetString(x));
                var fullPath = new[] { rootId }
                    .Concat(partialPath)
                    .ToArray();

                // Set result.
                result = new
                {
                    Success = true,
                    FolderId = GuidHelper.GetString(folder.Id),
                    Path = fullPath,
                    Name = folder.Name
                };

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
        public object MoveFolder(MoveFolderRequest 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 folderId = GuidHelper.GetGuid(request.FolderId);
                var parentPath = Entities.Retrieve(parentId).Path;

                // Get folder and descendants.
                var folder = Persistence.Retrieve(folderId);
                var descendants = Entities.RetrieveDescendants(folderId);

                // Check if destination folder is under current folder.
                var oldFolderPath = folder.Path;
                if (parentPath.Any(x => x == folderId))
                {
                    result = new
                    {
                        Success = false,
                        Reason = FolderUnderItself
                    };
                    return result;
                }

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

                // Success.
                result = new
                {
                    Success = true,
                    Id = GuidHelper.GetString(folderId),
                    Path = path,
                    Descendants = savedDescendants.ToArray()
                };

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
        public object PersistConfiguredForm(PersistConfiguredFormRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();
            var parentId = GuidHelper.GetGuid(request.ParentId);

            // Catch all errors.
            try
            {

                // Parse or create the configured form ID.
                var conFormId = string.IsNullOrWhiteSpace(request.ConFormId)
                    ? Guid.NewGuid()
                    : GuidHelper.GetGuid(request.ConFormId);

                // Get the ID path.
                var parent = Entities.Retrieve(parentId);
                var path = parent.Path.Concat(new[] { conFormId }).ToArray();

                // Create configured form.
                var configuredForm = new ConfiguredForm()
                {
                    Id = conFormId,
                    Path = path,
                    Name = request.Name,
                    TemplateId = string.IsNullOrWhiteSpace(request.TemplateId)
                        ? null as Guid?
                        : GuidHelper.GetGuid(request.TemplateId),
                    LayoutId = string.IsNullOrWhiteSpace(request.LayoutId)
                        ? null as Guid?
                        : GuidHelper.GetGuid(request.LayoutId)
                };

                // Persist configured form.
                Persistence.Persist(configuredForm);

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

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

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
        public object GetConfiguredFormInfo([FromUri] GetConfiguredFormInfoRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();

            // Catch all errors.
            try
            {

                // Variables.
                var id = GuidHelper.GetGuid(request.ConFormId);
                var configuredForm = Persistence.Retrieve(id);

                // Check for a null configured form.
                if (configuredForm == null)
                {
                    result = new
                    {
                        Success = false,
                        Reason = FormNotFoundError
                    };
                    return result;
                }

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

                // Set result.
                result = new
                {
                    Success = true,
                    ConFormId = GuidHelper.GetString(configuredForm.Id),
                    Path = fullPath,
                    Name = configuredForm.Name,
                    LayoutId = configuredForm.LayoutId.HasValue
                        ? GuidHelper.GetString(configuredForm.LayoutId.Value)
                        : null,
                    TemplateId = configuredForm.TemplateId.HasValue
                        ? GuidHelper.GetString(configuredForm.TemplateId.Value)
                        : null
                };

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
Example #12
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 path = parentId == Guid.Empty
                    ? new[] { layoutsRootId, layoutId }
                    : Entities.Retrieve(parentId).Path
                        .Concat(new[] { layoutId }).ToArray();

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

                // 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;
        }
Example #13
0
        public object GetLayoutInfo([FromUri] GetLayoutInfoRequest request)
        {
            // Variables.
            var result = default(object);
            var rootId = CoreConstants.System.Root.ToInvariantString();

            // Catch all errors.
            try
            {

                // Variables.
                var id = GuidHelper.GetGuid(request.LayoutId);
                var layout = Persistence.Retrieve(id);
                var fullPath = new[] { rootId }
                    .Concat(layout.Path.Select(x => GuidHelper.GetString(x)))
                    .ToArray();

                // Set result.
                result = new
                {
                    Success = true,
                    LayoutId = GuidHelper.GetString(layout.Id),
                    KindId = GuidHelper.GetString(layout.KindId),
                    Path = fullPath,
                    Alias = layout.Alias,
                    Name = layout.Name
                };

            }
            catch (Exception ex)
            {

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

            }

            // Return result.
            return result;
        }
 /// <summary>
 /// Ensure all GUIDs are formatted without hyphens
 /// </summary>
 /// <param name="controllerContext"></param>
 protected override void Initialize(System.Web.Http.Controllers.HttpControllerContext controllerContext)
 {
     base.Initialize(controllerContext);
     controllerContext.SetOutgoingNoHyphenGuidFormat();
 }