use of datawave.webservice.result.DefaultEventQueryResponse in project datawave by NationalSecurityAgency.
the class TermFrequencyQueryTransformer method createResponse.
@Override
public BaseQueryResponse createResponse(List<Object> resultList) {
DefaultEventQueryResponse response = new DefaultEventQueryResponse();
List<EventBase> eventList = new ArrayList<>();
for (Object o : resultList) {
EventBase result = (EventBase) o;
eventList.add(result);
}
response.setEvents(eventList);
response.setReturnedEvents(new Long(eventList.size()));
return response;
}
use of datawave.webservice.result.DefaultEventQueryResponse in project datawave by NationalSecurityAgency.
the class MutableMetadataHandler method findMatchingEventUuid.
/**
* Finds the event by creating a query by UUID from the keys and values in the runtime parameters.
*
* @param uuid
* @param uuidType
* @param userAuths
* @return Event
* @throws Exception
*/
protected EventBase<?, ?> findMatchingEventUuid(String uuid, String uuidType, Set<Authorizations> userAuths, ModificationOperation operation) throws Exception {
String field = operation.getFieldName();
String columnVisibility = operation.getColumnVisibility();
// query in format uuidType:uuid
StringBuilder query = new StringBuilder();
query.append(uuidType.toUpperCase()).append(":\"").append(uuid).append("\"");
// make the query only return the field to be modified and the UUIDType (avoids NoResultsException on an insert where the field has no values)
StringBuilder queryOptions = new StringBuilder();
queryOptions.append("query.syntax:LUCENE-UUID;raw.data.only:true");
if (field != null) {
queryOptions.append(";return.fields:").append(field.toUpperCase()).append(",").append(uuidType.toUpperCase());
}
String logicName = "LuceneUUIDEventQuery";
DefaultEvent e = null;
QueryExecutorBean queryService = this.getQueryService();
String id = null;
HashSet<String> auths = new HashSet<>();
for (Authorizations a : userAuths) auths.addAll(Arrays.asList(a.toString().split(",")));
Date expiration = new Date();
expiration = new Date(expiration.getTime() + (1000 * 60 * 60 * 24));
try {
MultivaluedMap<String, String> paramsMap = new MultivaluedMapImpl<>();
paramsMap.putAll(QueryParametersImpl.paramsToMap(logicName, query.toString(), "Query to find matching records for metadata modification", columnVisibility, new Date(0), new Date(), StringUtils.join(auths, ','), expiration, 2, -1, null, QueryPersistence.TRANSIENT, queryOptions.toString(), false));
GenericResponse<String> createResponse = queryService.createQuery(logicName, paramsMap);
id = createResponse.getResult();
BaseQueryResponse response = queryService.next(id);
if (response instanceof DefaultEventQueryResponse) {
DefaultEventQueryResponse eResponse = (DefaultEventQueryResponse) response;
if (eResponse.getEvents().size() > 1) {
throw new IllegalStateException("More than one event matched " + uuid + " (" + eResponse.getEvents().size() + " matched)");
}
if (eResponse.getEvents().isEmpty()) {
throw new IllegalStateException("No event matched " + uuid);
}
e = (DefaultEvent) eResponse.getEvents().get(0);
}
} catch (Exception ex) {
log.error(ex);
} finally {
if (id != null) {
queryService.close(id);
}
}
return e;
}
use of datawave.webservice.result.DefaultEventQueryResponse in project datawave by NationalSecurityAgency.
the class GroupingTest method runTestQueryWithGrouping.
protected BaseQueryResponse runTestQueryWithGrouping(Map<String, Integer> expected, String querystr, Date startDate, Date endDate, Map<String, String> extraParms, Connector connector) throws Exception {
log.debug("runTestQueryWithGrouping");
QueryImpl settings = new QueryImpl();
settings.setBeginDate(startDate);
settings.setEndDate(endDate);
settings.setPagesize(Integer.MAX_VALUE);
settings.setQueryAuthorizations(auths.serialize());
settings.setQuery(querystr);
settings.setParameters(extraParms);
settings.setId(UUID.randomUUID());
log.debug("query: " + settings.getQuery());
log.debug("logic: " + settings.getQueryLogicName());
GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
logic.setupQuery(config);
DocumentTransformer transformer = (DocumentTransformer) (logic.getTransformer(settings));
TransformIterator iter = new DatawaveTransformIterator(logic.iterator(), transformer);
List<Object> eventList = new ArrayList<>();
while (iter.hasNext()) {
eventList.add(iter.next());
}
BaseQueryResponse response = transformer.createResponse(eventList);
// un-comment to look at the json output
ObjectMapper mapper = new ObjectMapper();
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
mapper.writeValue(temporaryFolder.newFile(), response);
Assert.assertTrue(response instanceof DefaultEventQueryResponse);
DefaultEventQueryResponse eventQueryResponse = (DefaultEventQueryResponse) response;
Assert.assertEquals("Got the wrong number of events", expected.size(), (long) eventQueryResponse.getReturnedEvents());
for (EventBase event : eventQueryResponse.getEvents()) {
String firstKey = "";
String secondKey = "";
Integer value = null;
for (Object field : event.getFields()) {
FieldBase fieldBase = (FieldBase) field;
switch(fieldBase.getName()) {
case "COUNT":
value = Integer.valueOf(fieldBase.getValueString());
break;
case "GENDER":
case "GEN":
case "BIRTHDAY":
firstKey = fieldBase.getValueString();
break;
case "AGE":
case "AG":
case "RECORD":
secondKey = fieldBase.getValueString();
break;
}
}
log.debug("mapping is " + firstKey + "-" + secondKey + " count:" + value);
String key;
if (!firstKey.isEmpty() && !secondKey.isEmpty()) {
key = firstKey + "-" + secondKey;
} else if (!firstKey.isEmpty()) {
key = firstKey;
} else {
key = secondKey;
}
Assert.assertEquals(expected.get(key), value);
}
return response;
}
use of datawave.webservice.result.DefaultEventQueryResponse in project datawave by NationalSecurityAgency.
the class ContentQueryTransformer method createResponse.
@Override
public BaseQueryResponse createResponse(List<Object> resultList) {
DefaultEventQueryResponse response = new DefaultEventQueryResponse();
List<EventBase> eventList = new ArrayList<>();
for (Object o : resultList) {
DefaultEvent result = (DefaultEvent) o;
eventList.add(result);
}
response.setEvents(eventList);
response.setReturnedEvents((long) eventList.size());
return response;
}
use of datawave.webservice.result.DefaultEventQueryResponse in project datawave by NationalSecurityAgency.
the class ExtendedQueryExecutorBeanTest method testNext_UserNotOwner.
@Test
public void testNext_UserNotOwner() throws Exception {
// Set local test input
String userName = "userName";
String userSid = "userSid";
String otherSid = "otherSid";
UUID queryId = UUID.randomUUID();
// Set expectations
expect(this.context.getCallerPrincipal()).andReturn(this.principal).anyTimes();
expect(this.principal.getName()).andReturn(userName);
expect(this.principal.getShortName()).andReturn(otherSid);
expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0));
expect(this.context.getUserTransaction()).andReturn(this.transaction).anyTimes();
this.transaction.begin();
expect(this.cache.get(queryId.toString())).andReturn(this.runningQuery);
expect(this.cache.lock(queryId.toString())).andReturn(true);
expect(this.responseObjectFactory.getEventQueryResponse()).andReturn(new DefaultEventQueryResponse());
expect(this.runningQuery.getConnection()).andReturn(this.connector);
expect(this.runningQuery.getSettings()).andReturn(this.query);
expect(this.query.getOwner()).andReturn(userSid);
expect(this.runningQuery.getSettings()).andReturn(this.query);
expect(this.query.getOwner()).andReturn(userSid);
cache.unlock(queryId.toString());
expect(this.transaction.getStatus()).andReturn(Status.STATUS_PREPARING).times(2);
this.transaction.setRollbackOnly();
this.transaction.commit();
this.runningQuery.setActiveCall(false);
expect(this.runningQuery.getLogic()).andReturn((QueryLogic) this.queryLogic1);
expect(this.queryLogic1.getCollectQueryMetrics()).andReturn(true);
expect(this.runningQuery.getMetric()).andReturn(this.queryMetric).times(2);
expectLastCall();
this.queryMetric.setError(isA(Throwable.class));
// Run the test
PowerMock.replayAll();
QueryExecutorBean subject = new QueryExecutorBean();
setInternalState(subject, EJBContext.class, context);
setInternalState(subject, QueryCache.class, cache);
setInternalState(subject, ClosedQueryCache.class, closedCache);
setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
Exception result1 = null;
try {
subject.next(queryId.toString());
} catch (DatawaveWebApplicationException e) {
result1 = e;
assertTrue(e.getCause() instanceof QueryException);
assertEquals("401-1", ((QueryException) e.getCause().getCause()).getErrorCode());
}
PowerMock.verifyAll();
// Verify results
assertNotNull("Expected a DatawaveWebApplicationException to be thrown due an unchecked exception", result1);
}
Aggregations