Search in sources :

Example 56 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class AbstractHintProvider method alignHintDescriptions.

protected void alignHintDescriptions(final List<GraphObject> hints, final int maxNameLength) {
    // insert appropriate number of spaces into description to align function names
    for (final GraphObject item : hints) {
        final String text = item.getProperty(displayText);
        final int pos = text.indexOf(" - ");
        if (pos < maxNameLength) {
            final StringBuilder buf = new StringBuilder(text);
            buf.insert(pos, StringUtils.leftPad("", maxNameLength - pos));
            // ignore exception, won't happen on a GraphObjectMap anyway
            try {
                item.setProperty(displayText, buf.toString());
            } catch (FrameworkException fex) {
            }
        }
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) Hint(org.structr.schema.action.Hint)

Example 57 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class AbstractHintProvider method getHints.

public List<GraphObject> getHints(final GraphObject currentEntity, final String type, final String currentToken, final String previousToken, final String thirdToken, final int cursorLine, final int cursorPosition) {
    final List<Hint> allHints = getAllHints(currentEntity, currentToken, previousToken, thirdToken);
    final List<GraphObject> hints = new LinkedList<>();
    int maxNameLength = 0;
    if (StringUtils.isBlank(currentToken) || startChars.contains(currentToken)) {
        // display all possible hints
        for (final Hint hint : allHints) {
            final GraphObjectMap item = new GraphObjectMap();
            String functionName = getFunctionName(hint.getReplacement());
            if (hint.mayModify()) {
                item.put(text, visitReplacement(functionName));
            } else {
                item.put(text, functionName);
            }
            item.put(displayText, getFunctionName(hint.getName()) + " - " + textOrPlaceholder(hint.shortDescription()));
            addPosition(item, hint, cursorLine, cursorPosition, cursorPosition);
            if (functionName.length() > maxNameLength) {
                maxNameLength = functionName.length();
            }
            hints.add(item);
        }
    } else {
        final int currentTokenLength = currentToken.length();
        for (final Hint hint : allHints) {
            final String functionName = getFunctionName(hint.getName());
            final String replacement = hint.getReplacement();
            if (functionName.startsWith(currentToken) || (currentToken.length() > 2 && functionName.contains(currentToken))) {
                final GraphObjectMap item = new GraphObjectMap();
                if (hint.mayModify()) {
                    item.put(text, visitReplacement(replacement));
                } else {
                    item.put(text, replacement);
                }
                item.put(displayText, getFunctionName(hint.getName()) + " - " + textOrPlaceholder(hint.shortDescription()));
                addPosition(item, hint, cursorLine, cursorPosition - currentTokenLength, cursorPosition);
                if (functionName.length() > maxNameLength) {
                    maxNameLength = functionName.length();
                }
                hints.add(item);
            }
        }
    }
    alignHintDescriptions(hints, maxNameLength);
    return hints;
}
Also used : Hint(org.structr.schema.action.Hint) GraphObjectMap(org.structr.core.GraphObjectMap) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Hint(org.structr.schema.action.Hint)

Example 58 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class TypedIdResource method getEntity.

// ----- public methods -----
public GraphObject getEntity() throws FrameworkException {
    final GraphObject entity = idResource.getEntity();
    if (entity != null) {
        final String type = SchemaHelper.normalizeEntityName(typeResource.getRawType());
        final String entityType = entity.getClass().getSimpleName();
        if (GenericNode.class.equals(entity.getClass()) || SearchCommand.getAllSubtypesAsStringSet(type).contains(entityType)) {
            return entity;
        }
    }
    throw new NotFoundException("Entity with ID " + idResource.getUuid() + " not found");
}
Also used : GenericNode(org.structr.core.entity.GenericNode) NotFoundException(org.structr.rest.exception.NotFoundException) GraphObject(org.structr.core.GraphObject)

Example 59 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class GraphQLWriter method stream.

public void stream(final SecurityContext securityContext, final Writer output, final GraphQLRequest request) throws IOException, FrameworkException {
    final RestWriter writer = new StructrJsonWriter(securityContext, output);
    if (indent) {
        writer.setIndent("	");
    }
    if (request.hasSchemaQuery()) {
        // schema query is serialized from GraphQL execution result, doesn't need enclosing JSON object
        for (final GraphQLQuery query : request.getQueries()) {
            if (query.isSchemaQuery()) {
                // use graphql-java schema response
                final String originalQuery = request.getOriginalQuery();
                final GraphQL graphQL = GraphQL.newGraphQL(SchemaService.getGraphQLSchema()).build();
                final ExecutionResult result = graphQL.execute(originalQuery);
                final Gson gson = new GsonBuilder().setPrettyPrinting().create();
                if (result != null) {
                    final Map<String, Object> data = result.getData();
                    if (data != null) {
                        gson.toJson(data, output);
                    }
                }
            }
        }
    } else {
        writer.beginDocument(null, null);
        writer.beginObject();
        for (final GraphQLQuery query : request.getQueries()) {
            if (query.isSchemaQuery()) {
                // use graphql-java schema response
                final String originalQuery = request.getOriginalQuery();
                final GraphQL graphQL = GraphQL.newGraphQL(SchemaService.getGraphQLSchema()).build();
                final ExecutionResult result = graphQL.execute(originalQuery);
                final Gson gson = new GsonBuilder().setPrettyPrinting().create();
                if (result != null) {
                    final Map<String, Object> data = result.getData();
                    if (data != null) {
                        gson.toJson(data, output);
                    }
                }
            } else {
                writer.name(query.getFieldName());
                writer.beginArray();
                for (final GraphObject object : query.getEntities(securityContext)) {
                    root.serialize(writer, null, object, query, query.getRootPath());
                }
                writer.endArray();
            }
        }
        // finished
        writer.endObject();
        writer.endDocument();
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) GraphQL(graphql.GraphQL) Gson(com.google.gson.Gson) ExecutionResult(graphql.ExecutionResult) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) GraphQLQuery(org.structr.core.graphql.GraphQLQuery)

Example 60 with GraphObject

use of org.structr.core.GraphObject in project structr by structr.

the class ToCsvFunction method writeCsv.

public static void writeCsv(final List list, final Writer out, final String propertyView, final List<String> properties, final char quoteChar, final char delimiterChar, final String recordSeparator, final boolean includeHeader, final boolean localizeHeader, final String headerLocalizationDomain, final Locale locale) throws IOException {
    final StringBuilder row = new StringBuilder();
    if (includeHeader) {
        row.setLength(0);
        boolean isFirstCol = true;
        if (propertyView != null) {
            final Object obj = list.get(0);
            if (obj instanceof GraphObject) {
                for (PropertyKey key : ((GraphObject) obj).getPropertyKeys(propertyView)) {
                    String value = key.dbName();
                    if (localizeHeader) {
                        try {
                            value = LocalizeFunction.getLocalization(locale, value, headerLocalizationDomain);
                        } catch (FrameworkException fex) {
                            logger.warn("to_csv(): Exception", fex);
                        }
                    }
                    isFirstCol = appendColumnString(row, value, isFirstCol, quoteChar, delimiterChar);
                }
            } else {
                row.append("Error: Object is not of type GraphObject, can not determine properties of view for header row");
            }
        } else if (properties != null) {
            for (final String colName : properties) {
                String value = colName;
                if (localizeHeader) {
                    try {
                        value = LocalizeFunction.getLocalization(locale, value, headerLocalizationDomain);
                    } catch (FrameworkException fex) {
                        logger.warn("to_csv(): Exception", fex);
                    }
                }
                isFirstCol = appendColumnString(row, value, isFirstCol, quoteChar, delimiterChar);
            }
        }
        out.append(row).append(recordSeparator).flush();
    }
    for (final Object obj : list) {
        row.setLength(0);
        boolean isFirstCol = true;
        if (propertyView != null) {
            if (obj instanceof GraphObject) {
                for (PropertyKey key : ((GraphObject) obj).getPropertyKeys(propertyView)) {
                    final Object value = ((GraphObject) obj).getProperty(key);
                    isFirstCol = appendColumnString(row, value, isFirstCol, quoteChar, delimiterChar);
                }
            } else {
                row.append("Error: Object is not of type GraphObject, can not determine properties of object");
            }
        } else if (properties != null) {
            if (obj instanceof GraphObject) {
                final GraphObject castedObj = (GraphObject) obj;
                for (final String colName : properties) {
                    final PropertyKey key = StructrApp.key(obj.getClass(), colName);
                    final Object value = castedObj.getProperty(key);
                    isFirstCol = appendColumnString(row, value, isFirstCol, quoteChar, delimiterChar);
                }
            } else if (obj instanceof Map) {
                final Map castedObj = (Map) obj;
                for (final String colName : properties) {
                    final Object value = castedObj.get(colName);
                    isFirstCol = appendColumnString(row, value, isFirstCol, quoteChar, delimiterChar);
                }
            }
        }
        // Replace \r and \n so we dont get multi-line CSV (needs to be four backslashes because regex)
        final String rowWithoutRecordSeparator = row.toString().replaceAll("\n", "\\\\n").replaceAll("\r", "\\\\r");
        out.append(rowWithoutRecordSeparator).append(recordSeparator).flush();
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

GraphObject (org.structr.core.GraphObject)151 FrameworkException (org.structr.common.error.FrameworkException)58 PropertyKey (org.structr.core.property.PropertyKey)39 LinkedList (java.util.LinkedList)35 SecurityContext (org.structr.common.SecurityContext)25 App (org.structr.core.app.App)25 StructrApp (org.structr.core.app.StructrApp)24 Tx (org.structr.core.graph.Tx)23 List (java.util.List)22 PropertyConverter (org.structr.core.converter.PropertyConverter)22 AbstractNode (org.structr.core.entity.AbstractNode)18 GraphObjectMap (org.structr.core.GraphObjectMap)17 NodeInterface (org.structr.core.graph.NodeInterface)17 LinkedHashSet (java.util.LinkedHashSet)15 PropertyMap (org.structr.core.property.PropertyMap)13 Map (java.util.Map)12 RestMethodResult (org.structr.rest.RestMethodResult)11 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 Result (org.structr.core.Result)9 ArrayList (java.util.ArrayList)8