use of com.tyndalehouse.step.core.models.StringAndCount in project step by STEPBible.
the class AbstractSubjectSearchServiceImpl method getLuceneInputReferenceRestriction.
/**
* This is part 1 of 2 that gives us a retriction on a book reference, as input by the user.
* @param version the master version
* @param mainRange the main range input by the user
* @return the shortest viable prefix
*/
StringAndCount getLuceneInputReferenceRestriction(String version, String mainRange) {
if (StringUtils.isBlank(mainRange)) {
return new StringAndCount("", 0);
}
// strip out any + and square brackets
Matcher matcher = IndividualSearch.MAIN_RANGE.matcher(mainRange);
final boolean hasReference = matcher.find();
String key;
if (!hasReference || matcher.groupCount() < 2) {
// assume un-wrapped reference
key = mainRange;
} else {
key = matcher.group(2);
}
final Book master = this.jSwordVersificationService.getBookFromVersion(version);
final Key k;
try {
k = master.getKey(key);
} catch (NoSuchKeyException e) {
throw new TranslatedException(e, "invalid_reference_in_book", key, version);
}
// now work out what we're looking at
String keyOsisID = k.getOsisID();
boolean hasSpaces = keyOsisID.indexOf(' ') != -1;
int firstDot = keyOsisID.indexOf('.');
boolean hasDots = firstDot != -1;
if (!hasSpaces && !hasDots) {
// no spaces and no ., so has to be a whole book
return wrapRefForLucene(keyOsisID, true);
}
if (hasSpaces) {
// then we're looking at a list of things, so, let's make one last attempt, in case we're looking at whole books...
String osisRef = k.getOsisRef();
if (osisRef.indexOf('.') == -1) {
// (Gen Lev Mar)
return getBooksFromRefs(this.jSwordVersificationService.getVersificationForVersion(version), osisRef);
}
return prefixWithNaveRefTerm(keyOsisID);
}
// so no spaces, but does have dots
// then we're looking at a single chapter - has to be. Because OSIS IDs for ranges gets expanded.
// Or at a single verse
int numDots = StringUtils.countMatches(keyOsisID, ".");
if (numDots > 1) {
// then definitely a verse, so return the exact osis id with no *
return wrapRefForLucene(keyOsisID, false);
}
// only 1 dot, so we're either looking at a chapter (Matt.1, or a verse Obad.1)
// otherwise, either looking at a chapter or a short book
String bookName = keyOsisID.substring(0, firstDot);
BibleBook bibleBook = BibleBook.fromExactOSIS(bookName);
if (bibleBook.isShortBook()) {
// then we're definitely looking at a verse
return wrapRefForLucene(keyOsisID, false);
}
// long book, so chapter ref
return wrapRefForLucene(keyOsisID, true);
}
use of com.tyndalehouse.step.core.models.StringAndCount in project step by STEPBible.
the class SubjectSearchServiceImpl method searchByMultipleReferences.
@Override
public SearchResult searchByMultipleReferences(final String[] versions, final String references) {
final StringAndCount allReferencesAndCounts = this.getInputReferenceForNaveSearch(versions, references);
int count = allReferencesAndCounts.getCount();
if (count > JSwordPassageService.MAX_VERSES_RETRIEVED) {
throw new TranslatedException("subject_reference_search_too_big", Integer.valueOf(count).toString(), Integer.valueOf(JSwordPassageService.MAX_VERSES_RETRIEVED).toString());
}
return searchByReference(allReferencesAndCounts.getValue());
}
use of com.tyndalehouse.step.core.models.StringAndCount in project step by STEPBible.
the class JSwordPassageServiceImpl method getAllReferencesAndCounts.
/**
* @param references a list of references to be parsed
* @param version the version against which the refs are parsed
* @return a String representing all the references
*/
@Override
public StringAndCount getAllReferencesAndCounts(final String references, final String version) {
int count = 0;
// TODO - can be refactored to optimize the reference query when used in subject searches...
final PassageKeyFactory keyFactory = PassageKeyFactory.instance();
final Versification av11n = this.versificationService.getVersificationForVersion(version);
final StringBuilder referenceString = new StringBuilder(1024);
try {
final Key k = keyFactory.getKey(av11n, references);
final Iterator<Key> iterator = k.iterator();
while (iterator.hasNext()) {
referenceString.append(iterator.next().getOsisID());
count++;
if (iterator.hasNext()) {
referenceString.append(' ');
}
}
return new StringAndCount(referenceString.toString(), count);
} catch (final NoSuchKeyException e) {
throw new TranslatedException(e, "invalid_reference_in_book", references, version);
}
}
use of com.tyndalehouse.step.core.models.StringAndCount in project step by STEPBible.
the class AbstractSubjectSearchServiceImpl method getBooksFromRefs.
private StringAndCount getBooksFromRefs(final Versification v11n, final String osisRef) {
final StringBuilder lucenePrefix = new StringBuilder(32);
lucenePrefix.append("+(");
final String[] ranges = StringUtils.split(osisRef);
int count = 0;
for (String r : ranges) {
if (r.indexOf('-') != -1) {
final String[] bookStartEnd = StringUtils.split(r, "-");
final BibleBook start = BibleBook.fromExactOSIS(bookStartEnd[0]);
final BibleBook end = BibleBook.fromExactOSIS(bookStartEnd[1]);
count++;
appendLuceneBookPrefix(lucenePrefix, bookStartEnd[0]);
BibleBook b = start;
while ((b = v11n.getNextBook(b)) != null && !b.equals(end)) {
count++;
appendLuceneBookPrefix(lucenePrefix, b.getOSIS());
}
appendLuceneBookPrefix(lucenePrefix, bookStartEnd[1]);
count++;
} else {
count++;
// single book
appendLuceneBookPrefix(lucenePrefix, r);
}
}
lucenePrefix.append(")");
return new StringAndCount(lucenePrefix.toString(), count);
}
Aggregations