public string ExtractFieldsStatic(Document image) { string result = string.Empty; var imageFields = image.Fields.OrderBy(y => y.Configuration.Weight).ThenBy(z => z.Configuration.Label); result += imageFields.Aggregate(string.Empty, (res, field) => res + field.GetValue() .ToString().PadRight(field.Configuration.Length.Value)) + Environment.NewLine; return result; }
public async Task <Domain.Document> GetDocumentUrl(Domain.Document document, string basePath) { var apiUrl = _configuration["ApiUrl"]; var url = apiUrl + $"{basePath}/{document.Question.Id}/document/{document.Url}"; document.Url = url; return(document); }
public DocumentViewModel GetDocumentViewModel(Document doc) { var docViewModel = new DocumentViewModel(); docViewModel.Id = doc.Id; docViewModel.Name = doc.Name; docViewModel.Date = doc.Date; docViewModel.Author = doc.Author; docViewModel.OriginalFileName = doc.OriginalFileName; docViewModel.UniqueFileName = doc.UniqueFileName; return docViewModel; }
public Document GetDocumentDomainModel(DocumentViewModel docViewModel) { var doc = new Document(); doc.Id = docViewModel.Id; doc.Name = docViewModel.Name; doc.Date = docViewModel.Date; doc.Author = docViewModel.Author; doc.OriginalFileName = docViewModel.OriginalFileName; doc.UniqueFileName = docViewModel.UniqueFileName; return doc; }
public async Task<Document> Map(string filepath, string configFilepath, DataStructure dataStructure = null, Definition definitionParam = null) { var appConfig = await AppConfigMapper.Map(configFilepath); if (appConfig == null) { throw new ArgumentNullException( "configFilepath", string.Format("Could not load configuration file from: {0}", configFilepath)); } var datastructure = appConfig.DataStructures.FirstOrDefault( d => (d.Format == ConfigurationStaticData.ConllxFormat) || (d.Format == ConfigurationStaticData.ConllFormat)); if (datastructure == null) { EventAggregator.GetEvent<StatusNotificationEvent>() .Publish( "Could not load CONLLX file because the structure is not defined in the configuration file."); return null; } definition = definitionParam ?? appConfig.Definitions.FirstOrDefault(); if (definition == null) { EventAggregator.GetEvent<StatusNotificationEvent>() .Publish( "Could not load XML file because the tree definitionParam is not defined in the configuration file."); return null; } wordPrototype = datastructure.Elements.OfType<Word>().Single(); sentencePrototype = datastructure.Elements.OfType<Sentence>().Single(); documentPrototype = datastructure.Elements.OfType<Document>().Single(); var document = await CreateDocument(filepath); document.FilePath = filepath; var filenameToPathMapping = AppConfig.GetConfigFileNameToFilePathMapping(); document.Attributes.Add( new Attribute { AllowedValuesSet = filenameToPathMapping.Values, Value = appConfig.Name, Name = "configuration", DisplayName = "Configuration", Entity = "attribute", IsEditable = true, IsOptional = false }); document.Attributes.Add( new Attribute { Value = appConfig.Filepath, Name = "configurationFilePath", DisplayName = "Configuration file path", Entity = "attribute", IsEditable = false, IsOptional = false }); return document; }
private void ProcessPreviousSentence(Document document) { var previousSentece = document.Sentences.LastOrDefault(); if ((previousSentece == null) || (previousSentece.Words.Count <= 1)) { return; } ProcessPreviousSentence(previousSentece); previousSentece.Words.Clear(); }
private async Task ParseDocument(string filepath, Document document) { using (var reader = new StreamReader(filepath)) { string line; document.Sentences.Add(ObjectCopier.Clone(sentencePrototype)); while ((line = await reader.ReadLineAsync()) != null) { switch (string.IsNullOrWhiteSpace(line)) { case true : ProcessPreviousSentence(document); document.Sentences.Add(ObjectCopier.Clone(sentencePrototype)); break; case false : AddSentenceToDocument(document, line); break; } } } }
// todo use abstract factory and inject the factory to encapsulate the persisting logic better public void Save(Document document, string documentFilePath, bool overwrite) { PersisterClient persister = null; var extension = Path.GetExtension(documentFilePath); if (extension != null) { var lowercaseExtension = extension.Substring(1).ToLowerInvariant(); if (lowercaseExtension.Equals(ConfigurationStaticData.XmlFormat)) { persister = new PersisterClient(new XmlPersister(eventAggregator)); } else if (lowercaseExtension.Equals(ConfigurationStaticData.ConllxFormat) || lowercaseExtension.Equals(ConfigurationStaticData.ConllFormat)) { persister = new PersisterClient(new ConllxPersister(eventAggregator)); } } if (persister != null) { persister.Save(document, documentFilePath, overwrite); } else { eventAggregator.GetEvent<StatusNotificationEvent>() .Publish( "Cannot save the document selected,because the format is not supported. Supported formats are XML and CONLLX."); } }
public void Save(Document document) { var documentFilePath = SelectedDocument != null ? SelectedDocument.Model.FilePath : saveDialogService.GetSaveFileLocation(FileFilters.XmlFilesOnlyFilter); if (!string.IsNullOrWhiteSpace(documentFilePath)) { eventAggregator.GetEvent<StatusNotificationEvent>() .Publish(string.Format("Saving document to file {0}", documentFilePath)); Save(document, documentFilePath, true); eventAggregator.GetEvent<StatusNotificationEvent>() .Publish(string.Format("Document saved: {0}", documentFilePath)); return; } eventAggregator.GetEvent<StatusNotificationEvent>() .Publish("Could not save file because there was no filepath set."); }
public async Task RemoveDocuments(Domain.Document document) { Console.WriteLine(document.Name + "Removed!"); }
private async Task ParseDocument(string filepath, DataStructure dataStructure, Document document) { using (var reader = new XmlTextReader(filepath)) { var queue = new List<ConfigurationPair>(); while (await Task.Run(() => reader.Read())) { switch (reader.NodeType) { case XmlNodeType.Element : var pair = new ConfigurationPair {ElementName = reader.Name}; var entityAttributes = new Dictionary<string, string>(); while (reader.MoveToNextAttribute()) { entityAttributes.Add(reader.Name, reader.Value); } pair.Attributes.Add(entityAttributes); queue.Add(pair); break; case XmlNodeType.EndElement : AddElementsToDocument(document, queue, dataStructure); break; } } } ProcessPreviousSentence(document); }
public void Save(Document document, string filepath, bool overwrite) { persister.Save(document, filepath, overwrite); }
private void AddSentenceToDocument(Document document, string line) { if (string.IsNullOrWhiteSpace(line) || line.StartsWith("#", StringComparison.Ordinal)) { return; } var lastSentence = document.Sentences.LastOrDefault(); if (lastSentence != null) { AddWordsToSentence(lastSentence, line); } }
private void AddElementsToDocument( Document document, ICollection<ConfigurationPair> queue, DataStructure dataStructure) { if (document == null) { return; } if (!queue.Any()) { return; } foreach (var item in queue) { var entityName = GetEntityNameByElementName(dataStructure, item); if (string.IsNullOrWhiteSpace(entityName)) { break; } var entity = EntityFactory.GetEntity(entityName); if (entity is Word) { ParseWord(document, item); } else if (entity is Sentence) { ParseSentence(document, item); } else if (entity is Document) { ParseDocument(document, item); } } queue.Clear(); }
private void NotifyIfAnyNonOptionalAttributeIsMissing( Document document, Element elementPrototype, Element newElement) { const string MissingNonOptionalAttributeErrorMessage = "Value missing for: attribute {0}, word id {1}, sentence id: {2}, document id: {3}"; foreach (var wordPrototypeAttribute in elementPrototype.Attributes) { if (!wordPrototypeAttribute.IsOptional) { var wordAttribute = newElement.Attributes.SingleOrDefault(atr => atr.Name.Equals(wordPrototypeAttribute.Name)); if ((wordAttribute == null) || string.IsNullOrEmpty(wordAttribute.Value)) { if ((document == null) || !document.Sentences.Any()) { continue; } var lastSentence = document.Sentences.Last(); var newWordId = newElement.GetAttributeByName("id"); var sentenceId = lastSentence.GetAttributeByName("id"); var documentId = document.GetAttributeByName("id"); EventAggregator.GetEvent<ValidationExceptionEvent>() .Publish( string.Format( MissingNonOptionalAttributeErrorMessage, wordPrototypeAttribute.Name, newWordId, sentenceId, documentId)); } } } }
private void ParseWord(Document document, ConfigurationPair item) { var newWord = ObjectCopier.Clone(wordPrototype); AddAttributesToElement(item, newWord); NotifyIfAnyNonOptionalAttributeIsMissing(document, wordPrototype, newWord); var lastSentence = document.Sentences.Last(); lastSentence.Words.Add(newWord); }
private void ParseSentence(Document document, ConfigurationPair item) { ProcessPreviousSentence(document); var newSentence = ObjectCopier.Clone(sentencePrototype); AddAttributesToElement(item, newSentence); NotifyIfAnyNonOptionalAttributeIsMissing(document, sentencePrototype, newSentence); document.Sentences.Add(newSentence); }
private void ParseDocument(Document document, ConfigurationPair item) { AddAttributesToElement(item, document); NotifyIfAnyNonOptionalAttributeIsMissing(document, documentPrototype, document); }
private void AddInternalAttributes(Document document) { foreach (var sentence in document.Sentences) { var sentenceContent = string.Empty; foreach (var word in sentence.Words) { var formAttribute = word.Attributes.SingleOrDefault(a => a.Name.Equals("form")); var formValue = formAttribute != null ? formAttribute.Value : string.Empty; sentenceContent += " " + formValue; word.Attributes.Add( new Attribute { Name = "content", DisplayName = "Content", Value = formValue, IsOptional = true, IsEditable = false }); } sentence.Attributes.Add( new Attribute { Name = "content", DisplayName = "Content", Value = sentenceContent, IsOptional = true, IsEditable = false }); } }
public async Task Save(Document document, string filepathToSaveTo = "", bool overwrite = true) { var filepath = string.IsNullOrWhiteSpace(filepathToSaveTo) ? document.FilePath : filepathToSaveTo; if (string.IsNullOrWhiteSpace(filepath)) { return; } var fileName = Path.GetFileName(filepath); var newFilepath = filepath.Replace(fileName, "New" + fileName); var mapper = new AppConfigMapper(); var configFilePath = document.GetAttributeByName("configurationFilePath"); if (string.IsNullOrWhiteSpace(configFilePath)) { return; } var appConfig = mapper.Map(configFilePath).GetAwaiter().GetResult(); var dataStructure = appConfig.DataStructures.FirstOrDefault( d => d.Format.Equals(ConfigurationStaticData.ConllxFormat) || d.Format.Equals(ConfigurationStaticData.ConllFormat)); if (dataStructure == null) { return; } wordPrototype = dataStructure.Elements.OfType<Word>().Single(); var documentMapper = new DocumentMapperClient( new LightConllxDocumentMapper {AppConfigMapper = mapper, EventAggregator = eventAggregator}); using (var writer = new StreamWriter(newFilepath)) { foreach (var sentence in document.Sentences) { if (!sentence.Words.Any()) { var oldSentence = await documentMapper.LoadSentence(sentence.GetAttributeByName("id"), filepath, configFilePath); WriteSentenceWords(oldSentence, writer); } else { WriteSentenceWords(sentence, writer); } } writer.Flush(); } if (File.Exists(filepath)) { File.Delete(filepath); } File.Move(newFilepath, filepath); }
public void InsertDocument(Document doc) { try { using (var connect = new Connect()) { connect.BeginTrx(); SqlConnection sqlconn = connect.GetConnection(); var docmapper = new DocumentDataMapper(sqlconn); docmapper.SetTransaction(connect.Transaction); docmapper.Insert(doc); connect.Commit(); } } catch (Exception exception) { throw new FaultException<ServerError>(new ServerError()); } }
private static DocumentDto DocumentToDto(Document document) { if (document == null) return null; var documentDto = new DocumentDto { Name = document.Name, Id = document.Id }; var fieldsCollection = new Collection<DynamicFieldDto>(); foreach (var field in document.Fields) { var dFieldDto = new DynamicFieldDto { Configuration = new DynamicFieldTemplateDto() { Code = field.Configuration.Code, Id = field.Configuration.Id, IsEnabled = field.Configuration.IsEnabled, Label = field.Configuration.Label, Length = field.Configuration.Length, Type = field.Configuration.Type, Weight = field.Configuration.Weight, }, Id = field.Id }; dFieldDto.SetValue(field.GetValue()); fieldsCollection.Add(dFieldDto); } documentDto.Fields = fieldsCollection; return documentDto; }