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) {
}
}
}
}
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;
}
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");
}
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();
}
}
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();
}
}
Aggregations