public void CreateFile(DocumentTemplateModel t) { string importFile = Convert.ToBase64String(File.ReadAllBytes(t.FilePath)); var doctype = new OptionSetValue(); switch (t.DocumentType) { case ".xlsx": doctype.Value = 1; break; case ".docx": doctype.Value = 2; break; } var template = new DocumentTemplate { Name = t.Name, Content = importFile, DocumentType = doctype, Description = t.Description }; Service.Create(template); }
public BindingSource CopySelectedTemplate(DataGridViewRow selectedRow, BindingSource bs, string directoryPath) { var current = (DocumentTemplateModel)selectedRow.DataBoundItem; var newTemplate = new DocumentTemplateModel { Content = string.Empty, Description = current.Description, DocumentType = current.DocumentType, Prefix = current.Prefix, Id = Guid.Empty, ModifiedBy = string.Empty, Name = current.Name, EntityType = string.Empty, CreatedNew = true }; var fileExists = false; var searchPattern = @"\(\d*\)"; do { newTemplate.FilePath = Path.Combine(directoryPath, newTemplate.GetFileName()); if (File.Exists(newTemplate.FilePath)) { fileExists = true; if (Regex.IsMatch(newTemplate.Name, searchPattern)) { var matches = Regex.Matches(newTemplate.Name, searchPattern); var lastOccurence = matches[matches.Count - 1]; var numberString = lastOccurence.Value; numberString = numberString.Replace(")", ""); numberString = numberString.Replace("(", ""); var highestCount = Convert.ToInt32(numberString); newTemplate.Name = Regex.Replace(newTemplate.Name, searchPattern, $"({highestCount + 1})"); newTemplate.FilePath = Path.Combine(directoryPath, newTemplate.GetFileName()); } else { newTemplate.Name = newTemplate.Name + "(1)"; newTemplate.FilePath = Path.Combine(directoryPath, newTemplate.GetFileName()); } } else { fileExists = false; } }while (fileExists); File.Copy(current.FilePath, newTemplate.FilePath); bs.Add(newTemplate); return(bs); }
private List <DocumentTemplateModel> GetDTObjectsListFromEntityCollection(EntityCollection entities) { var list = new List <DocumentTemplateModel>(); foreach (var e in entities.Entities) { var doctype = string.Empty; switch (((OptionSetValue)e["documenttype"]).Value) { case 1: doctype = ".xlsx"; break; case 2: doctype = ".docx"; break; } var prefix = string.Empty; switch (e.LogicalName) { case "documenttemplate": prefix = _generalPrefix; break; case "personaldocumenttemplate": prefix = _personalPrefix; break; } var modifiedBy = Service.Retrieve("systemuser", ((EntityReference)e["modifiedby"]).Id, new ColumnSet("fullname") ); var dto = new DocumentTemplateModel { Id = e.Id, Description = e.GetAttributeValue <string>("description"), ModifiedBy = (string)modifiedBy["fullname"], ModifiedOn = ((DateTime)e.GetAttributeValue <DateTime?>("modifiedon")).ToLocalTime(), Name = (string)e["name"], Content = (string)e["content"], Prefix = prefix, DocumentType = doctype, EntityType = e.LogicalName, CreatedNew = false }; dto.FilePath = Path.Combine(baseDirectory, dto.GetFileName()); list.Add(dto); } return(list); }
public DocumentTemplateModel RenameFileFromDGV(DocumentTemplateModel template, string directoryPath) { if (File.Exists(Path.Combine(directoryPath, template.GetFileName()))) { throw new FileNameDuplicateException("Filename already exists!"); } File.Move(template.FilePath, Path.Combine(directoryPath, template.GetFileName())); template.FilePath = Path.Combine(directoryPath, template.GetFileName()); return(template); }
public void UpdateFile(DocumentTemplateModel t) { string importFile = Convert.ToBase64String(File.ReadAllBytes(t.FilePath)); var template = new Entity(t.EntityType); template.Id = t.Id; template["content"] = importFile; template["description"] = t.Description; Service.Update(template); }
public void SaveFileToDisk(DocumentTemplateModel template) { if (!string.IsNullOrEmpty(template.FilePath)) { File.WriteAllBytes(template.FilePath, Convert.FromBase64String(template.Content)); } else { var ex = new InvalidDirectoryException(); throw ex; } }
public BindingSource AddNewTemplateToBindingSourceFromFile(DataGridView dgv, BindingSource bs, string filePath) { var fileInfo = new FileInfo(filePath); string regexp = @"[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}"; var id = Guid.Empty; if (Regex.IsMatch(filePath, regexp)) { id = new Guid(Regex.Match(filePath, regexp).Value); } if (!CheckMethods.DoesGridviewContainGuid(dgv, "idDataGridViewTextBoxColumn", id)) { var entityType = string.Empty; if (filePath.Contains(settings.PrefixDocumentTemplate)) { entityType = "documenttemplate"; } if (filePath.Contains(settings.PrefixPersonalDocumentTemplate)) { entityType = "personaldocumenttemplate"; } var template = new DocumentTemplateModel { Id = id, DocumentType = fileInfo.Extension, Name = fileInfo.Name.Replace(fileInfo.Extension, string.Empty), FilePath = filePath, EntityType = entityType, CreatedNew = true }; bs.Add(template); } else { var ex = new DuplicateException("Duplicate detected! A dataset with the given id " + id.ToString() + " was already added to this list!"); throw ex; } return(bs); }