sessions/fall24/books-genai-vertex-langchain4j/src/main/java/services/domain/BooksService.java [38:161]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Service
public class BooksService {
    @Autowired
    DataAccess dao;

    private static final Logger logger = LoggerFactory.getLogger(BooksService.class);

    @Autowired
    CloudStorageService cloudStorageService;
    public List<Map<String, Object>>  prompt(String prompt) {
        return dao.promptForBooks(prompt, 0);
    }

    public List<Map<String, Object>>  prompt(String prompt, Integer characterLimit) {
        return dao.promptForBooks(prompt, characterLimit);
    }

    public List<Map<String, Object>>  prompt(BookRequest bookRequest, Integer characterLimit) {
        String prompt = PromptUtility.formatPromptBookKeywords(bookRequest.keyWords());
        return dao.promptForBooks(prompt, bookRequest.book(), bookRequest.author(), characterLimit);
    }

    public Integer insertBook(String fileName) {
        String author = FileUtility.getAuthor(fileName);
        author = SqlUtility.replaceUnderscoresWithSpaces(author);
        String title = FileUtility.getTitle(fileName);
        title = SqlUtility.replaceUnderscoresWithSpaces(title);
        String year = FileUtility.getYear(fileName);
        String publicPrivate = FileUtility.getPublicPrivate(fileName);
        Map<String, Object> book = dao.findBook(title);
        Map<String, Object> authorMap = dao.findAuthor(author);
        Object authorId = authorMap.get("author_id");
        Integer bookId = 0;
        if(!book.isEmpty()){
            bookId = (Integer) book.get("book_id");
        } else {
            if(authorId==null)
                authorId = dao.insertAuthor("famous author", author);
            logger.info("publicPrivate:"+publicPrivate);
            bookId = dao.insertBook( (Integer) authorId, title, year, ScopeType.fromValue(publicPrivate));
        }

        return bookId;
    }

    public String getBookSummary(String bookTitle) {
        Map<String, Object> book = dao.findBook(bookTitle);
        Map<String, Object> summary = new HashMap<>();
        if(!book.isEmpty()){
            Integer bookId = (Integer) book.get("book_id");
            summary = dao.findSummaries(bookId);
        }
        return summary.isEmpty() ? "" : (String) summary.get("summary");
    }

    public Integer insertPagesBook(String filePath, String bookTitle) {
        //0 = failure, 1 = success
        Integer success = 0;
        if( filePath == null || filePath.equals("") || bookTitle==null || bookTitle.equals("")) {
            return success;
        }

        Map<String, Object> book = dao.findBook(bookTitle);

        if(book.isEmpty()){
            return success;
        }

        BufferedReader reader = null;
        Integer bookId = (Integer) book.get("book_id");
        logger.info(filePath+" "+bookTitle+" bookId:"+bookId);
        try {
            //replace with cloud storage eventually
            ClassPathResource classPathResource = new ClassPathResource(filePath);
            InputStream inputStream = classPathResource.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            success = insertPagesBook(reader, bookTitle);
        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return success;
    }

    public Integer insertPagesBook(BufferedReader reader, Integer bookId) {
        Integer success = 0;
        try {
            String content;
            Integer page = 1;
            List<Map<String, Object>> pages = dao.findPages(bookId);
            if(!pages.isEmpty()) {
                return success;
            }
            char[] cbuf = new char[6000];

            int charsRead;
            while ((charsRead = reader.read(cbuf)) != -1) {
                content = new String(cbuf, 0, charsRead);
                dao.insertPages( bookId,content,page );
                page++;
            }
            reader.close();
            success=1;
        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return success;

    }

    public Integer insertPagesBook(BufferedReader reader, String bookTitle) {
        Integer success = 0;
        Map<String, Object> book = dao.findBook(bookTitle);

        if(book.isEmpty()){
            return success;
        }
        Integer bookId = (Integer) book.get("book_id");
        logger.info("bookId:"+bookId);
        success = insertPagesBook(reader, bookId);
        return success;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



sessions/next24/books-genai-vertex-langchain4j/src/main/java/services/domain/BooksService.java [38:161]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@Service
public class BooksService {
    @Autowired
    DataAccess dao;

    private static final Logger logger = LoggerFactory.getLogger(BooksService.class);

    @Autowired
    CloudStorageService cloudStorageService;
    public List<Map<String, Object>>  prompt(String prompt) {
        return dao.promptForBooks(prompt, 0);
    }

    public List<Map<String, Object>>  prompt(String prompt, Integer characterLimit) {
        return dao.promptForBooks(prompt, characterLimit);
    }

    public List<Map<String, Object>>  prompt(BookRequest bookRequest, Integer characterLimit) {
        String prompt = PromptUtility.formatPromptBookKeywords(bookRequest.keyWords());
        return dao.promptForBooks(prompt, bookRequest.book(), bookRequest.author(), characterLimit);
    }

    public Integer insertBook(String fileName) {
        String author = FileUtility.getAuthor(fileName);
        author = SqlUtility.replaceUnderscoresWithSpaces(author);
        String title = FileUtility.getTitle(fileName);
        title = SqlUtility.replaceUnderscoresWithSpaces(title);
        String year = FileUtility.getYear(fileName);
        String publicPrivate = FileUtility.getPublicPrivate(fileName);
        Map<String, Object> book = dao.findBook(title);
        Map<String, Object> authorMap = dao.findAuthor(author);
        Object authorId = authorMap.get("author_id");
        Integer bookId = 0;
        if(!book.isEmpty()){
            bookId = (Integer) book.get("book_id");
        } else {
            if(authorId==null)
                authorId = dao.insertAuthor("famous author", author);
            logger.info("publicPrivate:"+publicPrivate);
            bookId = dao.insertBook( (Integer) authorId, title, year, ScopeType.fromValue(publicPrivate));
        }

        return bookId;
    }

    public String getBookSummary(String bookTitle) {
        Map<String, Object> book = dao.findBook(bookTitle);
        Map<String, Object> summary = new HashMap<>();
        if(!book.isEmpty()){
            Integer bookId = (Integer) book.get("book_id");
            summary = dao.findSummaries(bookId);
        }
        return summary.isEmpty() ? "" : (String) summary.get("summary");
    }

    public Integer insertPagesBook(String filePath, String bookTitle) {
        //0 = failure, 1 = success
        Integer success = 0;
        if( filePath == null || filePath.equals("") || bookTitle==null || bookTitle.equals("")) {
            return success;
        }

        Map<String, Object> book = dao.findBook(bookTitle);

        if(book.isEmpty()){
            return success;
        }

        BufferedReader reader = null;
        Integer bookId = (Integer) book.get("book_id");
        logger.info(filePath+" "+bookTitle+" bookId:"+bookId);
        try {
            //replace with cloud storage eventually
            ClassPathResource classPathResource = new ClassPathResource(filePath);
            InputStream inputStream = classPathResource.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            success = insertPagesBook(reader, bookTitle);
        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return success;
    }

    public Integer insertPagesBook(BufferedReader reader, Integer bookId) {
        Integer success = 0;
        try {
            String content;
            Integer page = 1;
            List<Map<String, Object>> pages = dao.findPages(bookId);
            if(!pages.isEmpty()) {
                return success;
            }
            char[] cbuf = new char[6000];

            int charsRead;
            while ((charsRead = reader.read(cbuf)) != -1) {
                content = new String(cbuf, 0, charsRead);
                dao.insertPages( bookId,content,page );
                page++;
            }
            reader.close();
            success=1;
        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return success;

    }

    public Integer insertPagesBook(BufferedReader reader, String bookTitle) {
        Integer success = 0;
        Map<String, Object> book = dao.findBook(bookTitle);

        if(book.isEmpty()){
            return success;
        }
        Integer bookId = (Integer) book.get("book_id");
        logger.info("bookId:"+bookId);
        success = insertPagesBook(reader, bookId);
        return success;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



