public Book addBook(Book book) throws DuplicateBookException { SessionProvider sProvider = SessionProvider.createSystemProvider(); String nodeId = IdGenerator.generate(); book.setId(nodeId); try { Node parentNode = getNodeByPath(DEFAULT_PARENT_PATH, sProvider); Node bookNode = parentNode.addNode(nodeId, BookNodeTypes.EXO_BOOK); bookNode.setProperty(BookNodeTypes.EXP_BOOK_NAME, book.getName()); bookNode.setProperty( BookNodeTypes.EXP_BOOK_CATEGORY, Utils.bookCategoryEnumToString(book.getCategory())); bookNode.setProperty(BookNodeTypes.EXP_BOOK_CONTENT, book.getContent()); parentNode.getSession().save(); return book; } catch (PathNotFoundException e) { return null; } catch (Exception e) { log.error("Failed to add book", e); return null; } finally { sProvider.close(); } }
private Book createBookByNode(Node bookNode) throws Exception { if (bookNode == null) { return null; } Book bookNew = new Book(); bookNew.setId(bookNode.getName()); PropertyReader reader = new PropertyReader(bookNode); bookNew.setName(reader.string(BookNodeTypes.EXP_BOOK_NAME)); bookNew.setCategory( Utils.bookCategoryStringToEnum(reader.string(BookNodeTypes.EXP_BOOK_CATEGORY))); bookNew.setContent(reader.string(BookNodeTypes.EXP_BOOK_CONTENT)); return bookNew; }