private OperationResult <bool> SaveImageToFileStorage(AddBitmapForDocumentToProcess command)
        {
            try
            {
                var fileStoreCommand = new SavePageBitmapForDocumentToProcess
                {
                    DocumentId = command.DocumentId,
                    PageNumber = command.OrderedBitmap.Order,
                    FileData   = command.OrderedBitmap.FileData,
                    FileLabel  = command.OrderedBitmap.FileLabel,
                    FileType   = command.OrderedBitmap.FileType
                };

                var persistResult = _saveImageCommand.Handle(fileStoreCommand);

                if (!persistResult.Success)
                {
                    return(OperationResult <bool> .Failure(new FileStorageSaveFailed(fileStoreCommand)));
                }
            }
            catch (Exception ex)
            {
                return(OperationResult <bool> .Failure(new UncaughtException(ex)));
            }

            return(OperationResult <bool> .Success(true));
        }
        public SynapseResource Handle(IKey key, SynapseResource resource) // Just for the observation, temporary implementation -- can be removed later
        {
            var hl7Type = key.TypeName.GetHl7ModelType();

            var modelFactory = this._provider.GetService(typeof(ResourceCommandHandlerFactory)) as ResourceCommandHandlerFactory;

            IResourceCommandHandler commandHandler = modelFactory.GetHandler(hl7Type);

            var fhirParam = FHIRParam.Create(key.TypeName, key.ResourceId, key.VersionId);

            var synapseResource = commandHandler.Handle(resource);

            if (synapseResource == null)
            {
                throw new InterneuronBusinessException(StatusCodes.Status500InternalServerError);
            }

            return(synapseResource);
        }
        public OperationResult <TemplateDefinition> Handle(AddTemplateDefinitionPage command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            using (var context = new TemplatingContext(_persistenceConfiguration))
            {
                var templateFromDb = context.Templates
                                     .Include(t => t.DefinedPages)
                                     .SingleOrDefault(t => t.Id == command.TemplateId);

                var template = templateFromDb?.AsDomainModel();
                if (template == null)
                {
                    return(OperationResult <TemplateDefinition> .Failure(
                               new ObjectNotFoundById(typeof(TemplateDefinition), command.TemplateId)));
                }

                var templatePage = new TemplatePageDefinition((int)command.PageNumber);

                using (var bitmap = command.ReferenceCanvas.FileData.AsBitmap())
                {
                    var modificationResult = templatePage
                                             .ModifyReferenceCanvas(new Rectangle(0, 0, bitmap.Width, bitmap.Height));

                    if (!modificationResult.Successful)
                    {
                        return(OperationResult <TemplateDefinition> .Failure(modificationResult.Details));
                    }
                }

                var addPageResult = template.AddPageDefinition(templatePage);
                if (!addPageResult.Successful)
                {
                    return(OperationResult <TemplateDefinition> .Failure(addPageResult.Details));
                }

                templateFromDb.DefinedPages.Add(templatePage.AsPersistenceModel());
                context.Update(templateFromDb);
                context.SaveChanges();

                var saveBitmapCommand = new SaveBitmapForTemplatePageCanvasDefinition
                {
                    FileData       = command.ReferenceCanvas.FileData,
                    FileLabel      = command.ReferenceCanvas.FileLabel,
                    FileType       = command.ReferenceCanvas.FileType,
                    TemplatePageId = templatePage.Id,
                    TemplateId     = template.Id
                };
                var savedCanvas = _saveCanvasHandler.Handle(saveBitmapCommand);

                if (!savedCanvas.Success)
                {
                    return(OperationResult <TemplateDefinition> .Failure(new FileStorageSaveFailed(saveBitmapCommand)));
                }

                return(OperationResult <TemplateDefinition> .Success(template));
            }
        }