Example #1
0
        /// <summary>
        /// Delete handle put the handle in the recycle bin
        /// </summary>
        /// <param name="e"></param>
        public void On(DocumentDeleted e)
        {
            var    documentReadModel = _documentWriter.FindOneById(e.Handle);
            var    data     = new Dictionary <String, Object>();
            String fileName = "";
            Dictionary <String, object> customData = null;

            if (documentReadModel != null)
            {
                if (documentReadModel.FileName != null)
                {
                    fileName = documentReadModel.FileName.FileName + "." + documentReadModel.FileName.Extension;
                }
                customData = documentReadModel.CustomData;
            }
            var documentDescriptorReadModel = _documentsDescriptorReader.AllUnsorted
                                              .SingleOrDefault(dd => dd.Id == e.DocumentDescriptorId);
            String blobId = null;

            if (documentDescriptorReadModel != null &&
                documentDescriptorReadModel.Formats.ContainsKey(new DocumentFormat("original")))
            {
                var originalFormat = documentDescriptorReadModel.Formats[new DocumentFormat("original")];
                blobId = originalFormat.BlobId;
            }

            _recycleBin.Delete(e.AggregateId, "Jarvis", e.CommitStamp,
                               new {
                Handle               = e.Handle,
                FileName             = fileName,
                CustomData           = customData,
                DocumentDescriptorId = e.DocumentDescriptorId,
                OriginalBlobId       = blobId,
            });
        }
        public void On(DocumentDescriptorCreated e)
        {
            _streamReadModelCollection.Insert(e, new StreamReadModel()
            {
                Id        = GetNewId(),
                Handle    = e.HandleInfo.Handle,
                EventType = HandleStreamEventTypes.DocumentCreated,
            });

            //Now doc is not duplicated anymore, we should generate format added to document events.
            var doc = _documentDescriptorReadModel
                      .AllUnsorted
                      .Where(r => r.Id == e.AggregateId)
                      .SingleOrDefault();

            if (doc.Documents == null || !doc.Documents.Any())
            {
                return; //no handle in this document descriptor
            }
            var allHandles = doc.Documents;
            var descriptor = _blobStore.GetDescriptor(e.BlobId);

            foreach (var handle in allHandles)
            {
                var handleReadMode = _documentWriter.FindOneById(handle);
                foreach (var format in doc.Formats)
                {
                    _streamReadModelCollection.Insert(e, new StreamReadModel()
                    {
                        Id                   = GetNewId(),
                        Handle               = handle,
                        Filename             = descriptor.FileNameWithExtension,
                        DocumentDescriptorId = (DocumentDescriptorId)e.AggregateId,
                        FormatInfo           = new FormatInfo()
                        {
                            BlobId         = e.BlobId,
                            DocumentFormat = format.Key,
                            PipelineId     = format.Value.PipelineId != PipelineId.Null
                                ? format.Value.PipelineId
                                : new PipelineId("original"),
                        },
                        EventType          = HandleStreamEventTypes.DocumentHasNewFormat,
                        DocumentCustomData = handleReadMode.CustomData,
                    });
                }
            }
        }
        public async Task should_deduplicate_twice()
        {
            CreateDocument(9, "handle", TestConfig.PathToDocumentPdf);
            CreateDocument(10, "handle", TestConfig.PathToDocumentPdf);
            CreateDocument(11, "handle", TestConfig.PathToDocumentPdf);
            await _projections.UpdateAndWait();

            var original = _documentReader.FindOneById(new DocumentDescriptorId(9));
            var copy     = _documentReader.FindOneById(new DocumentDescriptorId(10));
            var copy2    = _documentReader.FindOneById(new DocumentDescriptorId(11));

            var handle = _handleWriter.FindOneById(new DocumentHandle("handle"));

            Assert.IsNotNull(original);
            Assert.IsNull(copy);
            Assert.IsNull(copy2);

            Assert.IsNotNull(handle);
            Assert.AreEqual(handle.DocumentDescriptorId, new DocumentDescriptorId(9));
        }
Example #4
0
        public void with_promise_and_create_handle_should_be_linked_to_document_1()
        {
            _writer.Promise(_handle1, 1);
            _writer.CreateIfMissing(_handle1, null, 1);
            _writer.LinkDocument(_handle1, _doc1, 2);

            var h = _writer.FindOneById(_handle1);

            Assert.AreEqual(_doc1, h.DocumentDescriptorId);
        }
        private void SetHandleToReturn()
        {
            var customData = new DocumentCustomData()
            {
                { "handle1", "test" },
                { "handle2", new { isComplex = true, theTruth = 42 } },
            };

            handle = new DocumentReadModel(
                new DocumentHandle("rev_1"),
                new DocumentDescriptorId(1),
                new FileNameWithExtension("test.txt"),
                customData
                );
            _handleWriter
            .FindOneById(Arg.Any <DocumentHandle>())
            .Returns(handle);
            IBlobDescriptor stub = Substitute.For <IBlobDescriptor>();

            stub.FileNameWithExtension.Returns(new FileNameWithExtension("test.txt"));
            _blobStore.GetDescriptor(Arg.Any <BlobId>()).Returns(stub);
        }
        public async Task <HttpResponseMessage> AddFormatToDocument(TenantId tenantId, DocumentFormat format)
        {
            var errorMessage = await AddFormatFromHttpContent(Request.Content, format);

            Logger.DebugFormat("File {0} processed with message {1}", _blobId, errorMessage);

            if (errorMessage != null)
            {
                Logger.Error("Error Adding format To Document: " + errorMessage);
                return(Request.CreateErrorResponse(
                           HttpStatusCode.BadRequest,
                           errorMessage
                           ));
            }

            String queueName = _customData[AddFormatToDocumentParameters.QueueName] as String;
            String jobId     = _customData[AddFormatToDocumentParameters.JobId] as String;
            DocumentDescriptorId documentId;

            if (String.IsNullOrEmpty(queueName))
            {
                //user ask for handle, we need to grab the handle
                var documentHandle = new DocumentHandle(_customData[AddFormatToDocumentParameters.DocumentHandle] as String);
                var handle         = _handleWriter.FindOneById(documentHandle);
                documentId = handle.DocumentDescriptorId;
                if (documentId == null)
                {
                    Logger.ErrorFormat("Trying to add a format for Handle {0} with a null DocumentId", documentHandle);
                    return(Request.CreateErrorResponse(
                               HttpStatusCode.BadRequest,
                               ""
                               ));
                }
                Logger.DebugFormat("Add format {0} to handle {1} and document id {2}", format, handle, documentId);
            }
            else
            {
                var job = _queueDispatcher.GetJob(queueName, jobId);

                if (job == null)
                {
                    Logger.WarnFormat("Job id {0} not found in queue {1}", jobId, queueName);
                    return(Request.CreateErrorResponse(
                               HttpStatusCode.BadRequest,
                               String.Format("Job id {0} not found in queue {1}", jobId, queueName)));
                }
                documentId = job.DocumentDescriptorId;
                if (documentId == null)
                {
                    Logger.ErrorFormat("Trying to add a format for Job Id {0} queue {1} - Job has DocumentDescriptorId null", jobId, queueName);
                    return(Request.CreateErrorResponse(
                               HttpStatusCode.BadRequest,
                               ""
                               ));
                }
                //need to check if the descriptor is deleted
                var exists = _documentDescriptorReader
                             .AllUnsorted
                             .Where(d => d.Id == documentId)
                             .Any();
                if (!exists)
                {
                    Logger.ErrorFormat("Trying to add a format for Job Id {0} queue {1} - DocumentDescriptor does not exists or was deleted!", jobId, queueName);
                    return(Request.CreateErrorResponse(
                               HttpStatusCode.BadRequest,
                               ""
                               ));
                }
                Logger.DebugFormat("Add format {0} to job id {1} and document id {2}", format, job.Id, documentId);
            }

            if (format == "null")
            {
                var formatFromFileName = _documentFormatTranslator.GetFormatFromFileName(_fileName);
                if (formatFromFileName == null)
                {
                    String error = "Format not specified and no known format for file: " + _fileName;
                    Logger.Error(error);
                    return(Request.CreateErrorResponse(
                               HttpStatusCode.BadRequest,
                               error
                               ));
                }
                format = new DocumentFormat(formatFromFileName);
            }

            var createdById = new PipelineId(_customData[AddFormatToDocumentParameters.CreatedBy] as String);

            Logger.DebugFormat("Incoming new format for documentId {0}", documentId);

            var command = new AddFormatToDocumentDescriptor(documentId, format, _blobId, createdById);

            CommandBus.Send(command, "api");

            return(Request.CreateResponse(
                       HttpStatusCode.OK,
                       new AddFormatToDocumentResponse
            {
                Result = true,
            }
                       ));
        }
 protected void SetupDocumentHandle(DocumentHandleInfo handleInfo, DocumentDescriptorId documentId)
 {
     _handleWriter
     .FindOneById(handleInfo.Handle)
     .Returns(info => new DocumentReadModel(handleInfo.Handle, documentId, handleInfo.FileName));
 }