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);
        }
Example #2
0
        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);
        }
Example #3
0
        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);
        }