Search in sources :

Example 6 with PersistenceException

use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.

the class StoreExportCommand method storeCommand.

@Override
public void storeCommand() throws PersistenceException {
    if (dirPath == null) {
        console.println("Export directory is not specified");
        return;
    }
    if (FilenameUtils.getExtension(dirPath).equals("") && !dirPath.endsWith(File.separator)) {
        dirPath += File.separator;
    }
    final File dumpDir = new File(dirPath);
    if (!dumpDir.exists()) {
        console.println("Directory does not exist. If the directory does indeed exist, try putting the path in quotes.");
        return;
    }
    if (!dumpDir.isDirectory()) {
        console.println("Specified path is not a directory.");
        return;
    }
    cql = addUserConstraintToCql(user, cql);
    Function<List<Map<String, Object>>, Integer> exportFunction = results -> {
        return results.stream().map(gson::toJson).map(json -> writeRecordToFile(json, dumpDir)).reduce(0, (a, b) -> a + b);
    };
    long count = getResults(exportFunction);
    console.println("Exported: " + count + " records\n");
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Argument(org.apache.karaf.shell.api.action.Argument) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) File(java.io.File) Command(org.apache.karaf.shell.api.action.Command) PersistenceException(org.codice.ddf.persistence.PersistenceException) List(java.util.List) PersistenceMapTypeAdapter(org.codice.gsonsupport.GsonTypeAdapters.PersistenceMapTypeAdapter) Gson(com.google.gson.Gson) Map(java.util.Map) Service(org.apache.karaf.shell.api.action.lifecycle.Service) DigestUtils(org.apache.commons.codec.digest.DigestUtils) Option(org.apache.karaf.shell.api.action.Option) FilenameUtils(org.apache.commons.io.FilenameUtils) List(java.util.List) File(java.io.File)

Example 7 with PersistenceException

use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.

the class PersistentStoreImpl method delete.

@Override
public int delete(String type, String cql, int startIndex, int pageSize) throws PersistenceException {
    List<Map<String, Object>> itemsToDelete = this.get(type, cql, startIndex, pageSize);
    SolrClient solrClient = getSolrClient(type);
    List<String> idsToDelete = new ArrayList<>();
    for (Map<String, Object> item : itemsToDelete) {
        String uuid = (String) item.get(PersistentItem.ID);
        if (StringUtils.isNotBlank(uuid)) {
            idsToDelete.add(uuid);
        }
    }
    if (!idsToDelete.isEmpty()) {
        try {
            LOGGER.debug("Deleting {} items by ID", idsToDelete.size());
            solrClient.deleteById(idsToDelete);
        } catch (SolrServerException | SolrException | IOException e) {
            LOGGER.info("Exception while trying to delete items by ID for persistent type {}", type, e);
            doRollback(solrClient, type);
            throw new PersistenceException("Exception while trying to delete items by ID for persistent type " + type, e);
        } catch (RuntimeException e) {
            LOGGER.info("RuntimeException while trying to delete items by ID for persistent type {}", type, e);
            doRollback(solrClient, type);
            throw new PersistenceException("RuntimeException while trying to delete items by ID for persistent type " + type, e);
        }
    }
    return idsToDelete.size();
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SolrClient(org.codice.solr.client.solrj.SolrClient) PersistenceException(org.codice.ddf.persistence.PersistenceException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SolrException(org.apache.solr.common.SolrException)

Example 8 with PersistenceException

use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.

the class AttributesStoreImplTest method testGetAllUsersThrowsException.

@Test(expected = PersistenceException.class)
public void testGetAllUsersThrowsException() throws PersistenceException {
    when(persistentStore.get(anyString())).thenThrow(new PersistenceException());
    attributesStore.getAllUsers();
}
Also used : PersistenceException(org.codice.ddf.persistence.PersistenceException) Test(org.junit.Test)

Example 9 with PersistenceException

use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.

the class DataUsage method setPersistentCronTime.

private void setPersistentCronTime() {
    PersistentItem persistentItem = new PersistentItem();
    persistentItem.addIdProperty(CRON_TIME_ID_KEY);
    persistentItem.addProperty(CRON_TIME_KEY, this.cronTime);
    try {
        readWriteLock.readLock().lock();
        persistentStore.add(PREFERENCES_TYPE, persistentItem);
    } catch (PersistenceException e) {
        LOGGER.error("Error adding Cron Time to Persistent Store.", e);
    } finally {
        readWriteLock.readLock().unlock();
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) PersistenceException(org.codice.ddf.persistence.PersistenceException)

Example 10 with PersistenceException

use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.

the class UserApplication method setUserPreferences.

private void setUserPreferences(Subject subject, Map<String, Object> preferences) {
    String json = JsonFactory.create().toJson(preferences);
    LOGGER.debug("preferences JSON text:\n {}", json);
    String username = SubjectUtils.getName(subject);
    PersistentItem item = new PersistentItem();
    item.addIdProperty(username);
    item.addProperty("user", username);
    item.addProperty("preferences_json", "_bin", Base64.getEncoder().encodeToString(json.getBytes(Charset.defaultCharset())));
    try {
        persistentStore.add(PersistentStore.PREFERENCES_TYPE, item);
    } catch (PersistenceException e) {
        LOGGER.info("PersistenceException while trying to persist preferences for user {}", username, e);
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) PersistenceException(org.codice.ddf.persistence.PersistenceException)

Aggregations

PersistenceException (org.codice.ddf.persistence.PersistenceException)28 Map (java.util.Map)16 PersistentItem (org.codice.ddf.persistence.PersistentItem)10 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 SolrServerException (org.apache.solr.client.solrj.SolrServerException)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 List (java.util.List)4 Listener (org.cometd.annotation.Listener)4 Date (java.util.Date)3 SolrClient (org.apache.solr.client.solrj.SolrClient)3 UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)3 PersistentStore (org.codice.ddf.persistence.PersistentStore)3 Subject (ddf.security.Subject)2 StringReader (java.io.StringReader)2 ParseException (java.text.ParseException)2 Collection (java.util.Collection)2 Collectors (java.util.stream.Collectors)2