Search in sources :

Example 1 with ProgressCallback

use of org.codice.ddf.spatial.geocoding.ProgressCallback in project ddf by codice.

the class Geocoding method updateIndex.

private boolean updateIndex(String resource, boolean createIndex) {
    final ProgressCallback progressCallback = new ProgressCallback() {

        @Override
        public void updateProgress(final int progress) {
            setProgress(progress);
        }
    };
    LOGGER.trace("Updating GeoNames Index...");
    try {
        geoEntryIndexer.updateIndex(resource, geoEntryExtractor, createIndex, progressCallback);
        LOGGER.trace("\nDone Updating GeoNames Index.");
        LOGGER.debug("Done Updating GeoNames Index with : {}", resource);
        return true;
    } catch (GeoEntryExtractionException e) {
        LOGGER.debug("Error extracting GeoNames data from resource {}", resource, e);
        return false;
    } catch (GeoEntryIndexingException e) {
        LOGGER.debug("Error indexing GeoNames data", e);
        return false;
    } catch (GeoNamesRemoteDownloadException e) {
        LOGGER.debug("Error downloading resource from remote source {}", resource, e);
        return false;
    }
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)

Example 2 with ProgressCallback

use of org.codice.ddf.spatial.geocoding.ProgressCallback in project ddf by codice.

the class TestGeoNamesLuceneIndexer method testCreateIndexFromListWithProgressUpdates.

@Test
public void testCreateIndexFromListWithProgressUpdates() throws GeoEntryIndexingException, IOException {
    configureMocks();
    final ProgressCallback progressCallback = mock(ProgressCallback.class);
    geoNamesLuceneIndexer.updateIndex(GEO_ENTRY_LIST, true, progressCallback);
    verify(indexWriter, times(GEO_ENTRY_LIST.size())).addDocument(documentArgumentCaptor.capture());
    final List<Document> documentList = documentArgumentCaptor.getAllValues();
    verifyDocumentList(documentList);
    verify(progressCallback, times(1)).updateProgress(0);
    verify(progressCallback, times(1)).updateProgress(100);
}
Also used : ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) Document(org.apache.lucene.document.Document) Test(org.junit.Test)

Example 3 with ProgressCallback

use of org.codice.ddf.spatial.geocoding.ProgressCallback in project ddf by codice.

the class TestGeoNamesLuceneIndexerExceptions method testExceptionWhenAddingDocumentByExtractor.

@Test
public void testExceptionWhenAddingDocumentByExtractor() throws GeoEntryIndexingException, GeoEntryExtractionException, GeoNamesRemoteDownloadException, IOException {
    configureMocks();
    geoNamesLuceneIndexer.setIndexLocation(ABSOLUTE_PATH + TEST_PATH + "index");
    try {
        final GeoEntryExtractor geoEntryExtractor = new GeoEntryExtractor() {

            @Override
            public List<GeoEntry> getGeoEntries(final String resource, final ProgressCallback progressCallback) {
                return null;
            }

            @Override
            public void pushGeoEntriesToExtractionCallback(final String resource, final ExtractionCallback extractionCallback) throws GeoEntryExtractionException {
                extractionCallback.updateProgress(0);
                try {
                    extractionCallback.extracted(GEO_ENTRY);
                } catch (GeoEntryIndexingException e) {
                    throw new GeoEntryExtractionException("Unable to add entry.", e);
                }
            }

            @Override
            public void setUrl(String url) {
                return;
            }
        };
        geoNamesLuceneIndexer.updateIndex("", geoEntryExtractor, true, null);
        fail("Should have thrown a GeoEntryExtractionException; extractionCallback.extract() threw " + "a GeoEntryIndexingException because addDocument() threw an " + "IOException.");
    } catch (GeoEntryExtractionException e) {
        assertThat(e.getCause(), instanceOf(GeoEntryIndexingException.class));
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Test(org.junit.Test)

Example 4 with ProgressCallback

use of org.codice.ddf.spatial.geocoding.ProgressCallback in project ddf by codice.

the class GeoNamesUpdateCommand method execute.

@Override
public Object execute() {
    final PrintStream console = System.out;
    final ProgressCallback progressCallback = new ProgressCallback() {

        @Override
        public void updateProgress(final int progress) {
            console.printf("\r%d%%", progress);
            console.flush();
        }
    };
    console.println("Updating...");
    try {
        geoEntryIndexer.updateIndex(resource, geoEntryExtractor, create, progressCallback);
        console.println("\nDone.");
    } catch (GeoEntryExtractionException e) {
        LOGGER.debug("Error extracting GeoNames data from resource {}", resource, e);
        console.printf("Could not extract GeoNames data from resource %s.%n" + "Message: %s%n" + "Check the logs for more details.%n", resource, e.getMessage());
    } catch (GeoEntryIndexingException e) {
        LOGGER.debug("Error indexing GeoNames data", e);
        console.printf("Could not index the GeoNames data.%n" + "Message: %s%n" + "Check the logs for more details.%n", e.getMessage());
    } catch (GeoNamesRemoteDownloadException e) {
        LOGGER.debug("Error downloading resource from remote source {}", resource, e);
        console.printf("Could not download the GeoNames file %s.%n  Message: %s%n" + "Check the logs for more details.%n", resource, e.getMessage());
    }
    return null;
}
Also used : GeoNamesRemoteDownloadException(org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException) PrintStream(java.io.PrintStream) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)

Example 5 with ProgressCallback

use of org.codice.ddf.spatial.geocoding.ProgressCallback in project ddf by codice.

the class TestGeoNamesUpdateCommand method testExceptionDuringExtraction.

@Test
public void testExceptionDuringExtraction() throws IOException, GeoNamesRemoteDownloadException, GeoEntryExtractionException, GeoEntryIndexingException {
    final String errorText = "Extraction error text";
    final GeoEntryExtractor geoEntryExtractor = mock(GeoEntryExtractor.class);
    final GeoEntryExtractionException geoEntryExtractionException = new GeoEntryExtractionException(errorText);
    doThrow(geoEntryExtractionException).when(geoEntryExtractor).pushGeoEntriesToExtractionCallback(anyString(), any(ExtractionCallback.class));
    final GeoEntryIndexer geoEntryIndexer = new GeoEntryIndexer() {

        @Override
        public void updateIndex(final List<GeoEntry> newEntries, final boolean create, final ProgressCallback progressCallback) {
        }

        @Override
        public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoNamesRemoteDownloadException, GeoEntryExtractionException, GeoEntryIndexingException {
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, mock(ExtractionCallback.class));
        }
    };
    geoNamesUpdateCommand.setGeoEntryIndexer(geoEntryIndexer);
    geoNamesUpdateCommand.setGeoEntryExtractor(geoEntryExtractor);
    geoNamesUpdateCommand.setResource("temp.txt");
    geoNamesUpdateCommand.execute();
    assertThat(consoleInterceptor.getOutput(), containsString(errorText));
    consoleInterceptor.resetSystemOut();
    consoleInterceptor.closeBuffer();
}
Also used : GeoEntryIndexer(org.codice.ddf.spatial.geocoding.GeoEntryIndexer) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) ProgressCallback(org.codice.ddf.spatial.geocoding.ProgressCallback) ArrayList(java.util.ArrayList) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.containsString(org.hamcrest.Matchers.containsString) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) GeoEntryExtractor(org.codice.ddf.spatial.geocoding.GeoEntryExtractor) Test(org.junit.Test)

Aggregations

ProgressCallback (org.codice.ddf.spatial.geocoding.ProgressCallback)7 GeoEntryExtractionException (org.codice.ddf.spatial.geocoding.GeoEntryExtractionException)6 GeoEntryIndexingException (org.codice.ddf.spatial.geocoding.GeoEntryIndexingException)5 Test (org.junit.Test)5 GeoEntryExtractor (org.codice.ddf.spatial.geocoding.GeoEntryExtractor)4 List (java.util.List)3 GeoNamesRemoteDownloadException (org.codice.ddf.spatial.geocoding.GeoNamesRemoteDownloadException)3 ArrayList (java.util.ArrayList)2 Document (org.apache.lucene.document.Document)2 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)2 ExtractionCallback (org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback)2 GeoEntryIndexer (org.codice.ddf.spatial.geocoding.GeoEntryIndexer)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Matchers.anyString (org.mockito.Matchers.anyString)2 PrintStream (java.io.PrintStream)1