Search in sources :

Example 41 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class JSwordStrongNumberHelper method applySearchCounts.

/**
 * Applies the search counts for every strong number.
 *
 * @param bookName the book name
 * @param augmentedByStrong the augmented strongs found in the original augmentation querys
 */
private void applySearchCounts(final String bookName, final Map<String, EntityDoc> augmentedByStrong) {
    try {
        final IndexSearcher is = jSwordSearchService.getIndexSearcher(this.isOT ? STRONG_OT_VERSION_BOOK.getInitials() : STRONG_NT_VERSION_BOOK.getInitials());
        final TermDocs termDocs = is.getIndexReader().termDocs();
        for (final Entry<String, BookAndBibleCount> strong : this.allStrongs.entrySet()) {
            final String strongKey = strong.getKey();
            termDocs.seek(new Term(LuceneIndex.FIELD_STRONG, this.strongAugmentationService.reduce(strongKey)));
            final EntityDoc entityDoc = augmentedByStrong.get(strongKey);
            final String references = entityDoc != null ? entityDoc.get("references") : null;
            // we'll never need more than 200 documents as this is the cut off point
            int bible = 0;
            int book = 0;
            while (termDocs.next()) {
                final int freq = termDocs.freq();
                final Document doc = is.doc(termDocs.doc());
                final String docRef = doc.get(LuceneIndex.FIELD_KEY);
                if ((references == null || augmentedVersionInVerse(docRef, references))) {
                    if (docRef != null && docRef.startsWith(bookName)) {
                        book += freq;
                    }
                    bible += freq;
                }
            }
            final BookAndBibleCount value = strong.getValue();
            value.setBible(bible);
            value.setBook(book);
        }
    } catch (final IOException e) {
        throw new StepInternalException(e.getMessage(), e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) TermDocs(org.apache.lucene.index.TermDocs) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) BookAndBibleCount(com.tyndalehouse.step.core.models.search.BookAndBibleCount) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) Document(org.apache.lucene.document.Document)

Example 42 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class SupportRequestServiceImpl method readImage.

private byte[] readImage(final InputStream imageData) {
    ByteArrayOutputStream outputStream = null;
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;
    try {
        outputStream = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(outputStream);
        bis = new BufferedInputStream(imageData);
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }
        bos.flush();
        return outputStream.toByteArray();
    } catch (IOException ex) {
        throw new StepInternalException("Unable to read image data");
    } finally {
        IOUtils.closeQuietly(bos);
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(imageData);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 43 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class SupportRequestServiceImpl method readResponse.

private String readResponse(final HttpEntity entity) {
    if (entity == null) {
        return "";
    }
    InputStream content = null;
    BufferedReader reader = null;
    InputStreamReader inputStreamReader = null;
    try {
        content = entity.getContent();
        if (content == null) {
            return "";
        }
        inputStreamReader = new InputStreamReader(content);
        reader = new BufferedReader(inputStreamReader);
        final StringBuilder response = new StringBuilder(256);
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        return response.toString().replaceAll("[\\n\\r]", "");
    } catch (IOException e) {
        throw new StepInternalException("Unable to parse response", e);
    } finally {
        EntityUtils.consumeQuietly(entity);
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(inputStreamReader);
        IOUtils.closeQuietly(content);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) StringUtils.getNonNullString(com.tyndalehouse.step.core.utils.StringUtils.getNonNullString) IOException(java.io.IOException)

Example 44 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class SwingServiceImpl method addDirectoryInstaller.

@Override
public BibleInstaller addDirectoryInstaller() {
    ResourceBundle bundle = ResourceBundle.getBundle("HtmlBundle", clientSessionProvider.get().getLocale());
    setDefaultUILookAndFeel();
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setDialogTitle(bundle.getString("select_directory"));
    chooser.setRequestFocusEnabled(true);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        return moduleService.addDirectoryInstaller(chooser.getSelectedFile().getAbsolutePath());
    }
    throw new StepInternalException("Unable to select a directory - exit ungracefully");
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) ResourceBundle(java.util.ResourceBundle)

Example 45 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class UserServiceImpl method readUsersFile.

/**
 * @return a set of users
 */
private synchronized Set<String> readUsersFile() {
    final Set<String> usersFromFile = new HashSet<String>();
    if (!this.usersFile.exists()) {
        return usersFromFile;
    }
    FileInputStream fis = null;
    BufferedReader reader = null;
    InputStreamReader isr = null;
    try {
        fis = new FileInputStream(this.usersFile);
        isr = new InputStreamReader(fis);
        reader = new BufferedReader(isr);
        String line = null;
        while ((line = reader.readLine()) != null) {
            final String[] userEntry = line.split("[,]+");
            if (userEntry.length < 2) {
                LOGGER.warn("Invalid user entry: [{}]", line);
                continue;
            }
            // add email to hashset
            usersFromFile.add(userEntry[0]);
        }
    } catch (final IOException e) {
        throw new StepInternalException("Unable to read file", e);
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(reader);
    }
    return usersFromFile;
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet)

Aggregations

StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)62 IOException (java.io.IOException)25 Book (org.crosswire.jsword.book.Book)9 Key (org.crosswire.jsword.passage.Key)7 EntityDoc (com.tyndalehouse.step.core.data.EntityDoc)5 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 ParseException (org.apache.lucene.queryParser.ParseException)4 BookException (org.crosswire.jsword.book.BookException)4 Versification (org.crosswire.jsword.versification.Versification)4 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)3 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)3 KeyWrapper (com.tyndalehouse.step.core.models.KeyWrapper)3 FileInputStream (java.io.FileInputStream)3 TransformingSAXEventProvider (org.crosswire.common.xml.TransformingSAXEventProvider)3 XMLUtil.writeToString (org.crosswire.common.xml.XMLUtil.writeToString)3 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)3 BibleBook (org.crosswire.jsword.versification.BibleBook)3 AllResultsCollector (com.tyndalehouse.step.core.data.AllResultsCollector)2