use of org.structr.web.datasource.RestDataSource in project structr by structr.
the class SearchCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final SecurityContext securityContext = getWebSocket().getSecurityContext();
final String searchString = (String) webSocketData.getNodeData().get("searchString");
final Boolean exactSearch = (Boolean) webSocketData.getNodeData().get("exact");
final String restQuery = (String) webSocketData.getNodeData().get("restQuery");
final String cypherQuery = (String) webSocketData.getNodeData().get("cypherQuery");
final String paramString = (String) webSocketData.getNodeData().get("cypherParams");
final String typeString = (String) webSocketData.getNodeData().get("type");
final int pageSize = webSocketData.getPageSize();
final int page = webSocketData.getPage();
Class type = null;
if (typeString != null) {
type = SchemaHelper.getEntityClassForRawType(typeString);
}
if (searchString == null) {
if (cypherQuery != null) {
try {
Map<String, Object> obj = null;
if (StringUtils.isNoneBlank(paramString)) {
obj = new Gson().fromJson(paramString, Map.class);
}
final List<GraphObject> result = StructrApp.getInstance(securityContext).cypher(cypherQuery, obj);
int resultCountBeforePaging = result.size();
webSocketData.setRawResultCount(resultCountBeforePaging);
if (page != 0 && pageSize != 0) {
webSocketData.setResult(result.subList((page - 1) * pageSize, Math.min(page * pageSize, resultCountBeforePaging)));
} else {
webSocketData.setResult(result);
}
getWebSocket().send(webSocketData, true);
return;
} catch (Exception ex) {
logger.warn("Exception occured", ex);
getWebSocket().send(MessageBuilder.status().code(400).message(ex.getMessage()).build(), true);
return;
}
}
if (restQuery != null) {
final RestDataSource restDataSource = new RestDataSource();
try {
securityContext.setRequest(getWebSocket().getRequest());
webSocketData.setResult(restDataSource.getData(new RenderContext(securityContext), restQuery));
getWebSocket().send(webSocketData, true);
return;
} catch (FrameworkException ex) {
logger.error("", ex);
return;
}
}
}
final String sortOrder = webSocketData.getSortOrder();
final String sortKey = webSocketData.getSortKey();
final PropertyKey sortProperty = StructrApp.key(AbstractNode.class, sortKey);
final Query query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden().sort(sortProperty).order("desc".equals(sortOrder));
query.and(AbstractNode.name, searchString, exactSearch);
if (type != null) {
query.andType(type);
}
try {
// do search
final Result result = query.getResult();
// set full result list
webSocketData.setResult(result.getResults());
// send only over local connection
getWebSocket().send(webSocketData, true);
} catch (FrameworkException fex) {
logger.warn("Exception occured", fex);
getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
}
}
Aggregations