public void Initialize() { _documentTypeRepositoryMock = DocumentTypeRepositoryMock.GetDocumentTypeRepositoryMock(); _generatedDocumentRepositoryMock = GeneratedDocumentRepositoryMock.GetGeneratedDocumentRepositoryMock(); jsonContent = new GenerateDocumentRequest(); htmlContent = new GenerateDocumentRequest(); nsiContext = new NsiContext(); htmlGenerator = new NSI.DocumentGenerator.Implementations.HtmlGenerator(new NSI.DocumentGenerator.Implementations.Helpers.HtmlGeneratorHelper()); pdfGenerator = new NSI.DocumentGenerator.Implementations.PdfGenerator(); _docxGeneratorMock = DocxGeneratorMock.GetDocxGeneratorMock(); _odtGeneratorMock = OdtGeneratorMock.GetOdtGeneratorMock(); generatedDocumentLogger = new NSI.DocumentGenerator.Implementations.Helpers.GeneratedDocumentLogger(_generatedDocumentRepositoryMock.Object); templateGenerator = new TemplateGenerator(new NSI.DocumentGenerator.Implementations.PdfGenerator(), new NSI.DocumentGenerator.Implementations.HtmlGenerator(new NSI.DocumentGenerator.Implementations.Helpers.HtmlGeneratorHelper())); documentGenerator = new NSI.DocumentGenerator.Implementations.Generators.DocumentGenerator(_documentTypeRepositoryMock.Object, generatedDocumentLogger, htmlGenerator, pdfGenerator, _odtGeneratorMock.Object, _docxGeneratorMock.Object, templateGenerator); jsonDocumentTypeDomain = new DocumentTypeDomain() { Name = "json", Code = "json", Version = "1.0", Encoding = "utf-8" }; htmlDocumentTypeDomain = new DocumentTypeDomain() { Name = "html", Code = "html", Version = "1.0", Encoding = "utf-8" }; }
private GeneratedDocumentDomain GenerateRegularDocument(DocumentTypeDomain inputDocType, DocumentTypeDomain outputDocTypeDomain, string inputContent, GeneratedDocumentDomain generatedDoc) { if (CompareDocTypes(inputDocType, outputDocTypeDomain, DocumentTypeEnum.Html, DocumentTypeEnum.Pdf)) { generatedDoc.ByteContent = _pdfGenerator.GeneratePdfFromHtml(inputContent); } else if (CompareDocTypes(inputDocType, outputDocTypeDomain, DocumentTypeEnum.Json, DocumentTypeEnum.Pdf)) { generatedDoc.ByteContent = _pdfGenerator.GeneratePdfFromJson(inputContent); } else if (CompareDocTypes(inputDocType, outputDocTypeDomain, DocumentTypeEnum.Html, DocumentTypeEnum.Odt)) { generatedDoc.ByteContent = _odtGenerator.GenerateOdtFromHtml(inputContent); } else if (CompareDocTypes(inputDocType, outputDocTypeDomain, DocumentTypeEnum.Html, DocumentTypeEnum.Docx)) { generatedDoc.ByteContent = _docxGenerator.GenerateDocxFromHtml(inputContent); } else if (CompareDocTypes(inputDocType, outputDocTypeDomain, DocumentTypeEnum.Json, DocumentTypeEnum.Html)) { generatedDoc.Content = _htmlGenerator.GenerateHtmlFromJson(inputContent, generatedDoc.Name); } else { generatedDoc.Success = false; generatedDoc.Content = DocumentMessages.ArgumentInvalidDocType; generatedDoc.Name = DocumentMessages.UndefinedDocumentName; } return(generatedDoc); }
public int Add(DocumentTypeDomain documentType) { var documentTypeDb = new DocumentType().FromDomainModel(documentType); _context.DocumentType.Add(documentTypeDb); _context.SaveChanges(); return(documentTypeDb.DocumentTypeId); }
public GeneratedDocumentDomain Generate(string inputContent, string fileName, DocumentTypeEnum inputDocType, DocumentTypeEnum outputDocTypeDomain, Nullable <int> templateVersionId) { DocumentTypeDomain inputType = _documentTypeRepository.GetByName(inputDocType.ToString("g")); DocumentTypeDomain outputType = _documentTypeRepository.GetByName(outputDocTypeDomain.ToString("g")); return(Generate(inputContent, fileName, inputType, outputType, templateVersionId)); }
// Helper method for readability // Returns true if input == expectedInput AND output == expectedOutput private bool CompareDocTypes(DocumentTypeDomain inputDocType, DocumentTypeDomain outputDocTypeDomain, DocumentTypeEnum expectedInput, DocumentTypeEnum expectedOutput) { // "g" means that the conversion of enum to string will be the most intuitive one // For example, if the enum value is MyValue then ToString("g") will return "MyValue" return(string.Equals(inputDocType.Name, expectedInput.ToString("g"), StringComparison.CurrentCultureIgnoreCase) && string.Equals(outputDocTypeDomain.Name, expectedOutput.ToString("g"), StringComparison.CurrentCultureIgnoreCase)); }
public GeneratedDocumentDomain Generate(string inputContent, string fileName, DocumentTypeDomain inputDocType, DocumentTypeDomain outputDocTypeDomain, Nullable <int> templateVersionId) { if (string.IsNullOrEmpty(inputContent)) { throw new NsiArgumentNullException(DocumentMessages.DocumentContentNotFound); } if (inputDocType == null || outputDocTypeDomain == null) { throw new NsiArgumentNullException(DocumentMessages.DocumentContentNotFound); } GeneratedDocumentDomain generatedDoc = new GeneratedDocumentDomain() { DateCreated = DateTime.Now, DocumentType = outputDocTypeDomain, DocumentTypeId = outputDocTypeDomain.Id, ExternalId = System.Guid.NewGuid(), Name = fileName, TemplateVersionId = templateVersionId }; if (fileName == null) { generatedDoc.Name = string.Format(@"{0}", DateTime.Now.Ticks); } try { generatedDoc.Success = true; if (templateVersionId == null) { generatedDoc = GenerateRegularDocument(inputDocType, outputDocTypeDomain, inputContent, generatedDoc); } else { generatedDoc = GenerateTemplateDocument(inputDocType, outputDocTypeDomain, inputContent, generatedDoc); } _documentLogger.Log(generatedDoc); return(generatedDoc); } catch (Exception e) { generatedDoc.Success = false; generatedDoc.ByteContent = null; generatedDoc.Content = e.Message; generatedDoc.Name = DocumentMessages.UndefinedDocumentName; _documentLogger.Log(generatedDoc); throw; } }
public void GenerateDocument_UnsupportedTypes() { DocumentTypeDomain inputType = new DocumentTypeDomain() { Name = "Test", Code = "Test", Version = "1.0", Encoding = "utf-8" }; var result = documentGenerator.Generate("Test", "TestFile", inputType, htmlDocumentTypeDomain, null); Assert.IsFalse(result.Success); Assert.AreEqual(result.Content, "There isn't any generator for given input and output data type."); Assert.AreEqual(result.Name, "Undefined document"); }
public static DocumentType FromDomainModel(this DocumentType obj, DocumentTypeDomain domain) { if (obj == null) { obj = new DocumentType(); } obj.DocumentTypeId = domain.Id; obj.Name = domain.Name; obj.Code = domain.Code; obj.Encoding = domain.Encoding; obj.Version = domain.Version; return(obj); }
private GeneratedDocumentDomain GenerateTemplateDocument(DocumentTypeDomain inputDocType, DocumentTypeDomain outputDocTypeDomain, string inputContent, GeneratedDocumentDomain generatedDoc) { if (CompareDocTypes(inputDocType, outputDocTypeDomain, DocumentTypeEnum.Json, DocumentTypeEnum.Pdf)) { generatedDoc.ByteContent = _templateGenerator.GeneratePdfFromTemplate(inputContent, generatedDoc.Name); } else if (CompareDocTypes(inputDocType, outputDocTypeDomain, DocumentTypeEnum.Json, DocumentTypeEnum.Html)) { generatedDoc.Content = _templateGenerator.GenerateHtmlFromTemplate(inputContent, generatedDoc.Name); } else { generatedDoc.Success = false; generatedDoc.Content = DocumentMessages.ArgumentInvalidDocType; generatedDoc.Name = DocumentMessages.UndefinedDocumentName; } return(generatedDoc); }
/// <summary> /// For the document generation to work there has to be predefined 'document types' in database. /// If there aren't any this method creates types: pdf, html, odt, json, docx /// </summary> public void AddBasicDocTypesToDb() { List <string> list = new List <string> { "pdf", "html", "json", "odt", "docx" }; foreach (string item in list) { DocumentTypeDomain docType = _documentTypeRepository.GetByName(item); if (docType == null) { _documentTypeRepository.Add(new DocumentTypeDomain() { Name = item, Code = item, Version = "1.0", Encoding = "utf-8" }); } } }