use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.
the class PersistentStoreImpl method add.
@Override
public void add(String type, Collection<Map<String, Object>> items) throws PersistenceException {
LOGGER.debug("type = {}", type);
if (StringUtils.isEmpty(type)) {
throw new PersistenceException("The type of object(s) to be added must be non-null and not blank, e.g., notification, metacard, etc.");
}
if (CollectionUtils.isEmpty(items)) {
return;
}
// Set Solr Core name to type and create solr client
SolrClient solrClient = getSolrClient(type);
List<SolrInputDocument> inputDocuments = new ArrayList<>();
for (Map<String, Object> properties : items) {
if (MapUtils.isEmpty(properties)) {
continue;
}
LOGGER.debug("Adding entry of type {}", type);
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("createddate_tdt", new Date());
for (Map.Entry<String, Object> entry : properties.entrySet()) {
solrInputDocument.addField(entry.getKey(), entry.getValue());
}
inputDocuments.add(solrInputDocument);
}
if (inputDocuments.isEmpty()) {
return;
}
try {
UpdateResponse response = solrClient.add(inputDocuments, commitNrtCommitWithinMs);
LOGGER.debug("UpdateResponse from add of SolrInputDocument: {}", response);
} catch (SolrServerException | SolrException | IOException e) {
LOGGER.info("Exception while adding Solr index for persistent type {}", type, e);
doRollback(solrClient, type);
throw new PersistenceException("Exception while adding Solr index for persistent type " + type, e);
} catch (RuntimeException e) {
LOGGER.info("RuntimeException while adding Solr index for persistent type {}", type, e);
doRollback(solrClient, type);
throw new PersistenceException("RuntimeException while adding Solr index for persistent type " + type, e);
}
}
use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.
the class PersistentStoreImpl method get.
@Override
public List<Map<String, Object>> get(String type, String cql, int startIndex, int pageSize) throws PersistenceException {
if (StringUtils.isBlank(type)) {
throw new PersistenceException("The type of object(s) to retrieve must be non-null and not blank, e.g., notification, metacard, etc.");
}
if (startIndex < 0) {
throw new IllegalArgumentException("The start index must be nonnegative.");
}
if (pageSize <= 0 || pageSize > MAX_PAGE_SIZE) {
throw new IllegalArgumentException(String.format("The page size must be greater than 0 and less than or equal to %d.", MAX_PAGE_SIZE));
}
// Set Solr Core name to type and create/connect to Solr Core
SolrClient solrClient = getSolrClient(type);
SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrClient, type);
try {
SolrQuery solrQuery;
// If not cql specified, then return all items
if (StringUtils.isBlank(cql)) {
solrQuery = new SolrQuery("*:*");
} else {
Filter filter = ECQL.toFilter(cql);
solrQuery = (SolrQuery) filter.accept(visitor, null);
}
if (solrQuery == null) {
throw new PersistenceException("Unsupported query " + cql);
}
solrQuery.setRows(pageSize);
solrQuery.setStart(startIndex);
solrQuery.addSort(PersistentItem.ID, SolrQuery.ORDER.asc);
QueryResponse solrResponse = solrClient.query(solrQuery, METHOD.POST);
long numResults = solrResponse.getResults().getNumFound();
LOGGER.debug("numResults = {}", numResults);
final SolrDocumentList docs = solrResponse.getResults();
return documentListToResultList(docs);
} catch (CQLException e) {
throw new PersistenceException("CQLException while getting Solr data with cql statement " + cql, e);
} catch (SolrServerException | SolrException | IOException e) {
throw new PersistenceException("Exception while getting Solr data with cql statement " + cql, e);
}
}
use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.
the class ActivityListener method handleEvent.
@Override
public void handleEvent(Event event) throws IllegalArgumentException {
LOGGER.debug("Received activity on topic {}", event.getTopic());
String id = (String) event.getProperty(ActivityEvent.ID_KEY);
String session = (String) event.getProperty(ActivityEvent.SESSION_ID_KEY);
String status = (String) event.getProperty(ActivityEvent.STATUS_KEY);
if (status.equals(ActivityEvent.ActivityStatus.RUNNING.toString())) {
return;
}
String title = (String) event.getProperty(ActivityEvent.TITLE_KEY);
String message = (String) event.getProperty(ActivityEvent.MESSAGE_KEY);
String timestamp = (String) event.getProperty(ActivityEvent.TIMESTAMP_KEY);
Map<String, String> operations = (Map<String, String>) event.getProperty(ActivityEvent.OPERATIONS_KEY);
String progress = event.getProperty(ActivityEvent.PROGRESS_KEY).toString();
String user = (String) event.getProperty(ActivityEvent.USER_ID_KEY);
String category = (String) event.getProperty(ActivityEvent.CATEGORY_KEY);
Long bytes = (Long) event.getProperty(ActivityEvent.BYTES_READ_KEY);
String downloadId = (String) event.getProperty(ActivityEvent.DOWNLOAD_ID_KEY);
PersistentItem activityToStore = new PersistentItem();
activityToStore.addIdProperty(id);
activityToStore.addProperty(ActivityEvent.SESSION_ID_KEY, session);
activityToStore.addProperty(ActivityEvent.STATUS_KEY, status);
activityToStore.addProperty(ActivityEvent.TITLE_KEY, title);
activityToStore.addProperty(ActivityEvent.MESSAGE_KEY, message);
activityToStore.addProperty(ActivityEvent.TIMESTAMP_KEY, timestamp);
for (Map.Entry<String, String> entry : operations.entrySet()) {
activityToStore.addProperty(ActivityEvent.OPERATIONS_KEY + "_" + entry.getKey(), entry.getValue());
}
activityToStore.addProperty(ActivityEvent.PROGRESS_KEY, progress);
activityToStore.addProperty(ActivityEvent.USER_ID_KEY, user);
activityToStore.addProperty(ActivityEvent.CATEGORY_KEY, category);
activityToStore.addProperty(ActivityEvent.BYTES_READ_KEY, bytes);
activityToStore.addProperty(ActivityEvent.DOWNLOAD_ID_KEY, downloadId);
try {
persistentStore.add(PersistenceType.ACTIVITY_TYPE.toString(), activityToStore);
} catch (PersistenceException e) {
LOGGER.info("Error while handling activity event", e);
}
}
use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.
the class NotificationListener method handleEvent.
@Override
public void handleEvent(Event event) throws IllegalArgumentException {
LOGGER.debug("Received notification on topic {}", event.getTopic());
String id = (String) event.getProperty(Notification.NOTIFICATION_KEY_ID);
String application = (String) event.getProperty(Notification.NOTIFICATION_KEY_APPLICATION);
String message = (String) event.getProperty(Notification.NOTIFICATION_KEY_MESSAGE);
String timestamp = (String) event.getProperty(Notification.NOTIFICATION_KEY_TIMESTAMP);
String title = (String) event.getProperty(Notification.NOTIFICATION_KEY_TITLE);
String userId = (String) event.getProperty(Notification.NOTIFICATION_KEY_USER_ID);
if (StringUtils.isEmpty(userId)) {
throw new IllegalArgumentException("Event \"" + Notification.NOTIFICATION_KEY_USER_ID + "\" property is blank");
}
// TODO: Do we need to get extra properties out of event for Notification, i.e., STATUS and
// BYTES?
PersistentItem item = new PersistentItem();
item.addIdProperty(id);
item.addProperty(Notification.NOTIFICATION_KEY_USER_ID, userId);
item.addProperty(Notification.NOTIFICATION_KEY_TIMESTAMP, timestamp);
item.addProperty(Notification.NOTIFICATION_KEY_APPLICATION, application);
item.addProperty(Notification.NOTIFICATION_KEY_TITLE, title);
item.addProperty(Notification.NOTIFICATION_KEY_MESSAGE, message);
try {
persistentStore.add(PersistenceType.NOTIFICATION_TYPE.toString(), item);
} catch (PersistenceException e) {
}
}
use of org.codice.ddf.persistence.PersistenceException in project ddf by codice.
the class AttributesStoreImplTest method testPersistenceStoreThrowsExceptionOnGet.
@Test(expected = PersistenceException.class)
public void testPersistenceStoreThrowsExceptionOnGet() throws PersistenceException {
when(persistentStore.get(anyString(), anyString())).thenThrow(new PersistenceException());
attributesStore.updateUserDataUsage(USER, LONG_5);
}
Aggregations