Search in sources :

Example 6 with BookName

use of com.tyndalehouse.step.core.models.BookName in project step by STEPBible.

the class ReferenceSuggestionServiceImpl method getExactTerms.

@Override
public BookName[] getExactTerms(final SuggestionContext context, final int max, final boolean popularSort) {
    final String masterBook = getDefaultedVersion(context);
    final Book master = this.versificationService.getBookFromVersion(masterBook);
    final Versification masterV11n = this.versificationService.getVersificationForVersion(masterBook);
    final String input = prepInput(context.getInput());
    try {
        Key k = master.getKey(input);
        if (k != null) {
            // check this book actually contains this key, based on the scope...
            if (!JSwordUtils.containsAny(master, k)) {
                return new BookName[0];
            // return getExactRange(input);
            }
            BookName bk;
            if (k instanceof VerseKey) {
                final VerseKey verseKey = (VerseKey) k;
                final boolean wholeBook = isBook(masterV11n, verseKey);
                if (wholeBook) {
                    final BibleBook book = ((Verse) verseKey.iterator().next()).getBook();
                    bk = getBookFromBibleBook(book, masterV11n);
                } else {
                    bk = new BookName(verseKey.getName(), verseKey.getName(), BookName.Section.PASSAGE, wholeBook, ((Verse) verseKey.iterator().next()).getBook(), k.getOsisRef());
                }
                return new BookName[] { bk };
            } else {
                return new BookName[] { new BookName(k.getName(), k.getName(), BookName.Section.OTHER_NON_BIBLICAL, false, k.getOsisRef()) };
            }
        }
    } catch (NoSuchKeyException ex) {
    // silently fail
    }
    // return getExactRange(input);
    return new BookName[0];
}
Also used : BibleBook(org.crosswire.jsword.versification.BibleBook) NoSuchKeyException(org.crosswire.jsword.passage.NoSuchKeyException) BibleBook(org.crosswire.jsword.versification.BibleBook) Book(org.crosswire.jsword.book.Book) AbstractPassageBook(org.crosswire.jsword.book.basic.AbstractPassageBook) BookName(com.tyndalehouse.step.core.models.BookName) Versification(org.crosswire.jsword.versification.Versification) VerseKey(org.crosswire.jsword.passage.VerseKey) Key(org.crosswire.jsword.passage.Key) VerseKey(org.crosswire.jsword.passage.VerseKey) Verse(org.crosswire.jsword.passage.Verse)

Example 7 with BookName

use of com.tyndalehouse.step.core.models.BookName in project step by STEPBible.

the class ReferenceSuggestionServiceImpl method collectNonExactMatches.

/**
 * //     * @param input the input
 *
 * @return the list of matching ranges
 */
// private BookName[] getExactRange(final String input) {
// final List<BookName> ranges = internationalRangeService.getRanges(input, true);
// return ranges.toArray(new BookName[ranges.size()]);
// }
@Override
public BookName[] collectNonExactMatches(final TermsAndMaxCount<BookName> collector, final SuggestionContext context, final BookName[] alreadyRetrieved, final int leftToCollect) {
    if (context.isExampleData()) {
        return this.getSampleData(context);
    }
    // we've already attempted to parse the whole key, so left to do here, is to iterate through the books
    // and match against those names that make sense.
    final List<BookName> books = new ArrayList<BookName>();
    final String masterBook = getDefaultedVersion(context);
    final Book master = this.versificationService.getBookFromVersion(masterBook);
    final Versification masterV11n = this.versificationService.getVersificationForVersion(master);
    final String input = context.getInput().toLowerCase();
    final Iterator<BibleBook> bookIterator = getBestIterator(master, masterV11n);
    addMatchingBooks(books, masterV11n, input, bookIterator);
    // de-duplicate by adding to a set
    final Set<BookName> bookNames = new LinkedHashSet<BookName>();
    bookNames.addAll(Arrays.asList(alreadyRetrieved));
    bookNames.addAll(books);
    // now, how many items do we have, and do we need to add a few chapters here?
    int spaceLeft = collector.getTotalCount() - bookNames.size();
    if (spaceLeft > 0) {
        final List<BookName> extras = new ArrayList<BookName>();
        // find a 'whole' book
        for (BookName bn : bookNames) {
            if (bn.isWholeBook() && bn.getBibleBook() != null) {
                int lastChapter = masterV11n.getLastChapter(bn.getBibleBook());
                for (int ii = 1; ii < lastChapter && spaceLeft > 0; ii++) {
                    extras.add(addChapter(masterV11n, bn.getBibleBook(), ii));
                    spaceLeft--;
                }
                collector.setTotalCount(lastChapter);
            } else if (Character.isDigit(bn.getShortName().charAt(bn.getShortName().length() - 1))) {
                String shortName = bn.getShortName();
                int lastPart = shortName.lastIndexOf(' ');
                if (lastPart != -1) {
                    try {
                        int chapter = Integer.parseInt(shortName.substring(lastPart + 1));
                        // we'll add all the chapters that exist.
                        int lastChapter = masterV11n.getLastChapter(bn.getBibleBook());
                        for (int ii = chapter * 10; ii < lastChapter && ii < chapter * 10 + 10; ii++) {
                            extras.add(addChapter(masterV11n, bn.getBibleBook(), ii));
                            spaceLeft--;
                        }
                        for (int ii = chapter * 100; ii < lastChapter && chapter < chapter * 100 + 100; ii++) {
                            extras.add(addChapter(masterV11n, bn.getBibleBook(), ii));
                            spaceLeft--;
                        }
                    } catch (NumberFormatException ex) {
                    // ignore
                    }
                }
            }
        }
        bookNames.addAll(extras);
    }
    // }
    return bookNames.toArray(new BookName[bookNames.size()]);
}
Also used : BibleBook(org.crosswire.jsword.versification.BibleBook) BibleBook(org.crosswire.jsword.versification.BibleBook) Book(org.crosswire.jsword.book.Book) AbstractPassageBook(org.crosswire.jsword.book.basic.AbstractPassageBook) BookName(com.tyndalehouse.step.core.models.BookName) Versification(org.crosswire.jsword.versification.Versification)

Example 8 with BookName

use of com.tyndalehouse.step.core.models.BookName in project step by STEPBible.

the class JSwordMetadataServiceImpl method getBooks.

/**
 * Looks through a versification for a particular type of book
 *
 * @param bookStart             the string to match
 * @param versification         the versification we are interested in
 * @param bookScope             the actual book required, usually to get chapters
 * @param autoLookupSingleBooks autoLookupSingleBooks true to indicate that for a single book, we should lookup
 *                              the chapters inside
 * @return the list of matching names
 */
private List<BookName> getBooks(final String bookStart, final Versification versification, final String bookScope, final boolean autoLookupSingleBooks) {
    final String searchPattern = bookStart.toLowerCase(Locale.getDefault()).trim();
    final List<BookName> matchingNames = new ArrayList<BookName>();
    final Iterator<BibleBook> bookIterator = versification.getBookIterator();
    if (StringUtils.isNotBlank(bookScope)) {
        final List<BookName> optionsInBook = getChapters(versification, versification.getBook(bookScope));
        return optionsInBook;
    }
    BibleBook b = null;
    while (bookIterator.hasNext()) {
        final BibleBook book = bookIterator.next();
        if (versification.getLongName(book).toLowerCase().startsWith(searchPattern) || versification.getPreferredName(book).toLowerCase().startsWith(searchPattern) || versification.getShortName(book).toLowerCase().startsWith(searchPattern)) {
            b = book;
            addBookName(matchingNames, book, versification);
        }
    }
    if (autoLookupSingleBooks && matchingNames.size() == 1) {
        final List<BookName> optionsInBook = getChapters(versification, b);
        if (!optionsInBook.isEmpty()) {
            return optionsInBook;
        }
    }
    return matchingNames;
}
Also used : BibleBook(org.crosswire.jsword.versification.BibleBook) BookName(com.tyndalehouse.step.core.models.BookName)

Aggregations

BookName (com.tyndalehouse.step.core.models.BookName)8 BibleBook (org.crosswire.jsword.versification.BibleBook)4 Versification (org.crosswire.jsword.versification.Versification)4 Book (org.crosswire.jsword.book.Book)2 AbstractPassageBook (org.crosswire.jsword.book.basic.AbstractPassageBook)2 Locale (java.util.Locale)1 ResourceBundle (java.util.ResourceBundle)1 Key (org.crosswire.jsword.passage.Key)1 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)1 Verse (org.crosswire.jsword.passage.Verse)1 VerseKey (org.crosswire.jsword.passage.VerseKey)1 Test (org.junit.Test)1