Bloom.Book.BookStarter.CreateBookOnDiskFromTemplate C# (CSharp) Method

CreateBookOnDiskFromTemplate() public method

Given a template, make a new book
public CreateBookOnDiskFromTemplate ( string sourceBookFolder, string parentCollectionPath ) : string
sourceBookFolder string
parentCollectionPath string
return string
        public string CreateBookOnDiskFromTemplate(string sourceBookFolder, string parentCollectionPath)
        {
            Logger.WriteEvent("BookStarter.CreateBookOnDiskFromTemplate({0}, {1})", sourceBookFolder, parentCollectionPath);

            // We use the "initial name" to make the intial copy, and it gives us something
            //to name the folder and file until such time as the user enters a title in for the book.
            string initialBookName = GetInitialName(sourceBookFolder, parentCollectionPath);
            var newBookFolder = Path.Combine(parentCollectionPath, initialBookName);
            CopyFolder(sourceBookFolder, newBookFolder);
            //if something bad happens from here on out, we need to delete that folder we just made
            try
            {
                var oldNamedFile = Path.Combine(newBookFolder, Path.GetFileName(GetPathToHtmlFile(sourceBookFolder)));
                var newNamedFile = Path.Combine(newBookFolder, initialBookName + ".htm");
                RobustFile.Move(oldNamedFile, newNamedFile);

                //the destination may change here...
                newBookFolder = SetupNewDocumentContents(sourceBookFolder, newBookFolder);

                if(OnNextRunSimulateFailureMakingBook)
                    throw new ApplicationException("Simulated failure for unit test");

            }
            catch (Exception)
            {
                SIL.IO.RobustIO.DeleteDirectory(newBookFolder, true);
                throw;
            }
            return newBookFolder;
        }

Usage Example

Example #1
0
        private static Book MakeBook(CollectionSettings collectionSettings, string sourceBookFolderPath)
        {
            var xmatterLocations = new List<string>();
            xmatterLocations.Add(ProjectContext.XMatterAppDataFolder);
            xmatterLocations.Add(FileLocator.GetDirectoryDistributedWithApplication( kpathToSHRPTemplates));
            xmatterLocations.Add(FileLocator.GetDirectoryDistributedWithApplication("xMatter"));
            var locator = new BloomFileLocator(collectionSettings, new XMatterPackFinder(xmatterLocations), new string[] {});

            var starter = new BookStarter(locator,
                                          path =>
                                          new BookStorage(path, locator, new BookRenamedEvent(),
                                                          collectionSettings),
                                          collectionSettings);
            var pathToFolderOfNewBook = starter.CreateBookOnDiskFromTemplate(sourceBookFolderPath, collectionSettings.FolderPath);

            var newBookInfo = new BookInfo(pathToFolderOfNewBook, false /*saying not editable works us around a langdisplay issue*/);

            BookStorage bookStorage = new BookStorage(pathToFolderOfNewBook, locator, new BookRenamedEvent(), collectionSettings);
            var book = new Book(newBookInfo, bookStorage, null,
                            collectionSettings, null, null, null, null);

            return book;
        }