Search in sources :

Example 1 with RestDataSource

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);
    }
}
Also used : RenderContext(org.structr.web.common.RenderContext) FrameworkException(org.structr.common.error.FrameworkException) Query(org.structr.core.app.Query) Gson(com.google.gson.Gson) GraphObject(org.structr.core.GraphObject) RestDataSource(org.structr.web.datasource.RestDataSource) FrameworkException(org.structr.common.error.FrameworkException) Result(org.structr.core.Result) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

Gson (com.google.gson.Gson)1 Map (java.util.Map)1 SecurityContext (org.structr.common.SecurityContext)1 FrameworkException (org.structr.common.error.FrameworkException)1 GraphObject (org.structr.core.GraphObject)1 Result (org.structr.core.Result)1 Query (org.structr.core.app.Query)1 PropertyKey (org.structr.core.property.PropertyKey)1 RenderContext (org.structr.web.common.RenderContext)1 RestDataSource (org.structr.web.datasource.RestDataSource)1