예제 #1
0
        public static XmlAuthorDao AuthorDao(DocumentHolder forAuthors, DocumentHolder forBooks)
        {
            var authorDao = AuthorDao(forAuthors);
            var bookDao   = BookDao(forBooks);

            authorDao.BookDao = bookDao;
            bookDao.AuthorDao = authorDao;

            authorDao.Parser = new AuthorProxyNodeParser <Author>()
            {
                BookDao = bookDao
            };

            bookDao.Parser = new BookProxyNodeParser <Book>()
            {
                AuthorDao = authorDao
            };

            var nodeHandler = NodeHandler.GetNodeHandlerFor(forBooks, forAuthors);

            authorDao.NodeHandler = nodeHandler;
            bookDao.NodeHandler   = nodeHandler;

            return(authorDao);
        }
예제 #2
0
        private static XmlAuthorDao AuthorDao(DocumentHolder holder)
        {
            if (holder == null)
            {
                throw new ArgumentException("Specify document holder");
            }

            if (AuthorDaoCache.TryGetValue(holder, out var authorDao))
            {
                return(authorDao);
            }

            authorDao = new XmlAuthorDao(holder);
            AuthorDaoCache.Add(holder, authorDao);

            return(authorDao);
        }
예제 #3
0
        private static XmlBookDao BookDao(DocumentHolder holder)
        {
            if (holder == null)
            {
                throw new ArgumentException("Specify document holder");
            }

            if (BookDaoCache.TryGetValue(holder, out XmlBookDao bookDao))
            {
                return(bookDao);
            }

            bookDao = new XmlBookDao(holder);
            BookDaoCache.Add(holder, bookDao);

            return(bookDao);
        }
예제 #4
0
        public void Save(Author author, SaveOption option, ISet <int> savedAuthors, ISet <int> savedBooks)
        {
            if (FindById(author.Id) != null)
            {
                switch (option)
                {
                case SaveOption.UPDATE_IF_EXIST:
                    Update(author, savedAuthors, savedBooks);
                    return;

                case SaveOption.SAVE_ONLY:
                    return;;
                }
            }

            var xDoc = DocumentHolder.Document;
            var root = xDoc.DocumentElement;

            DocumentHolder.IncrementLastId();
            author.Id = DocumentHolder.GetLastInsertedId();

            var idAttr = xDoc.CreateAttribute("id");

            idAttr.Value = author.Id.ToString();

            var authorNode = xDoc.CreateElement("author");

            var firstNameNode = xDoc.CreateElement("firstName");

            firstNameNode.AppendChild(xDoc.CreateTextNode(author.FirstName));

            var lastNameNode = xDoc.CreateElement("lastName");

            lastNameNode.AppendChild(xDoc.CreateTextNode(author.LastName));

            var booksNode = xDoc.CreateElement("books");

            authorNode.Attributes.Append(idAttr);
            authorNode.AppendChild(firstNameNode);
            authorNode.AppendChild(lastNameNode);
            authorNode.AppendChild(booksNode);

            root.AppendChild(authorNode);
            savedAuthors.Add(author.Id);

            foreach (var book in author.Books)
            {
                if (!savedBooks.Contains(book.Id))
                {
                    BookDao.Save(book, SaveOption.UPDATE_IF_EXIST,
                                 savedAuthors, savedBooks);
                }

                var idNode = xDoc.CreateElement("id");
                idNode.AppendChild(xDoc.CreateTextNode(book.Id.ToString()));
                booksNode.AppendChild(idNode);
            }

            cache.TryAdd(author.Id, new AuthorProxy(author)
            {
                BookDao = BookDao
            });

            xDoc.Save(DocumentHolder.Path);
        }
예제 #5
0
 public XmlAuthorDao(DocumentHolder documentHolder)
 {
     DocumentHolder = documentHolder ??
                      throw new ArgumentException("Specify documentHolder");
 }
예제 #6
0
        public void Save(Book book, SaveOption option, ISet <int> savedAuthors, ISet <int> savedBooks)
        {
            if (BookIsPresent(book))
            {
                switch (option)
                {
                case SaveOption.SAVE_ONLY:
                    return;

                case SaveOption.UPDATE_IF_EXIST:
                    Update(book, savedAuthors, savedBooks);
                    return;
                }
            }

            //Implementation of AUTO_INCREMENT
            DocumentHolder.IncrementLastId();
            var id = DocumentHolder.GetLastInsertedId();

            book.Id = id;

            var bRating = BookLibrary.Utils.FloatToString(book.Rating);

            var xDoc = DocumentHolder.Document;
            var root = xDoc.DocumentElement;

            //book
            var bookNode = xDoc.CreateElement("book");
            var idAttr   = xDoc.CreateAttribute("id");

            idAttr.Value = book.Id.ToString();

            //title
            var titleNode = xDoc.CreateElement("title");

            titleNode.AppendChild(xDoc.CreateTextNode(book.Title));

            //description
            var descriptionNode = xDoc.CreateElement("description");

            descriptionNode.AppendChild(xDoc.CreateTextNode(book.Description));

            //rating
            var ratingNode = xDoc.CreateElement("rating");

            ratingNode.AppendChild(xDoc.CreateTextNode(bRating));

            //section
            var sectionNode = xDoc.CreateElement("section");

            sectionNode.AppendChild(xDoc.CreateTextNode(book.Section.ToString()));

            //authors
            var authorsNode = xDoc.CreateElement("authors");

            bookNode.Attributes.Append(idAttr);
            bookNode.AppendChild(sectionNode);
            bookNode.AppendChild(titleNode);
            bookNode.AppendChild(descriptionNode);
            bookNode.AppendChild(ratingNode);
            bookNode.AppendChild(authorsNode);

            root.AppendChild(bookNode);
            savedBooks.Add(book.Id);

            if (book is BookProxy p && !p.AuthorsAreFetchedOrSet)
            {
                return;
            }

            foreach (var author in book.Authors)
            {
                if (!savedAuthors.Contains(author.Id))
                {
                    AuthorDao.Save(author, SaveOption.UPDATE_IF_EXIST,
                                   savedAuthors, savedBooks);
                }

                var idNode = xDoc.CreateElement("id");
                idNode.AppendChild(xDoc.CreateTextNode(author.Id.ToString()));
                authorsNode.AppendChild(idNode);
            }

            cache.TryAdd(book.Id, new BookProxy(book)
            {
                AuthorDao = AuthorDao
            });

            xDoc.Save(DocumentHolder.Path);
        }