use of org.structr.core.GraphObjectMap in project structr by structr.
the class LocalizeFunction method getLocalizedList.
public static List getLocalizedList(final Locale locale, final List<String> keyList, final String domain) throws FrameworkException {
final ArrayList<GraphObjectMap> resultList = new ArrayList();
for (final String key : keyList) {
final GraphObjectMap localizedEntry = new GraphObjectMap();
resultList.add(localizedEntry);
localizedEntry.put(new StringProperty("name"), key);
if (domain == null) {
localizedEntry.put(new StringProperty("localizedName"), getLocalization(locale, key));
} else {
localizedEntry.put(new StringProperty("localizedName"), getLocalization(locale, key, domain));
}
}
return resultList;
}
use of org.structr.core.GraphObjectMap in project structr by structr.
the class AdvancedSchemaTest method test04SchemaPropertyOrderInBuiltInViews.
@Test
public void test04SchemaPropertyOrderInBuiltInViews() {
try (final Tx tx = app.tx()) {
createAdminUser();
createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
final GenericProperty jsonName = new GenericProperty("jsonName");
SchemaNode test = null;
String id = null;
try (final Tx tx = app.tx()) {
// create test type
test = app.create(SchemaNode.class, "Test");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
try (final Tx tx = app.tx()) {
// create view with sort order
final List<SchemaView> list = test.getProperty(SchemaNode.schemaViews);
// create properties
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "one"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "two"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "three"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "four"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
final List<PropertyKey> list = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "public"));
Assert.assertEquals("Invalid number of properties in sorted view", 6, list.size());
Assert.assertEquals("id", list.get(0).dbName());
Assert.assertEquals("type", list.get(1).dbName());
Assert.assertEquals("one", list.get(2).dbName());
Assert.assertEquals("two", list.get(3).dbName());
Assert.assertEquals("three", list.get(4).dbName());
Assert.assertEquals("four", list.get(5).dbName());
try (final Tx tx = app.tx()) {
for (final SchemaView testView : test.getProperty(SchemaNode.schemaViews)) {
// modify sort order
testView.setProperty(SchemaView.sortOrder, "type, one, id, two, three, four, name");
}
// create test entity
final NodeInterface node = app.create(StructrApp.getConfiguration().getNodeEntityClass("Test"));
// save UUID for later
id = node.getUuid();
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final List<PropertyKey> list2 = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "public"));
Assert.assertEquals("Invalid number of properties in sorted view", 6, list2.size());
Assert.assertEquals("id", list2.get(0).dbName());
Assert.assertEquals("type", list2.get(1).dbName());
Assert.assertEquals("one", list2.get(2).dbName());
Assert.assertEquals("two", list2.get(3).dbName());
Assert.assertEquals("three", list2.get(4).dbName());
Assert.assertEquals("four", list2.get(5).dbName());
// test schema resource
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).body("result", hasSize(6)).body("result[0].jsonName", equalTo("id")).body("result[1].jsonName", equalTo("type")).body("result[2].jsonName", equalTo("one")).body("result[3].jsonName", equalTo("two")).body("result[4].jsonName", equalTo("three")).body("result[5].jsonName", equalTo("four")).when().get("/_schema/Test/public");
// test actual REST resource (not easy to extract and verify
// JSON property order, that's why we're using replaceAll and
// string comparison..
final String[] actual = RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).when().get("/Test").body().asString().replaceAll("[\\s]+", "").split("[\\W]+");
// we can only test the actual ORDER of the JSON result object by splitting it on whitespace and validating the resulting array
assertEquals("Invalid JSON result for sorted property view", "", actual[0]);
assertEquals("Invalid JSON result for sorted property view", "query_time", actual[1]);
assertEquals("Invalid JSON result for sorted property view", "0", actual[2]);
assertEquals("Invalid JSON result for sorted property view", "result_count", actual[4]);
assertEquals("Invalid JSON result for sorted property view", "1", actual[5]);
assertEquals("Invalid JSON result for sorted property view", "result", actual[6]);
assertEquals("Invalid JSON result for sorted property view", "id", actual[7]);
assertEquals("Invalid JSON result for sorted property view", id, actual[8]);
assertEquals("Invalid JSON result for sorted property view", "type", actual[9]);
assertEquals("Invalid JSON result for sorted property view", "Test", actual[10]);
assertEquals("Invalid JSON result for sorted property view", "one", actual[11]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[12]);
assertEquals("Invalid JSON result for sorted property view", "two", actual[13]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[14]);
assertEquals("Invalid JSON result for sorted property view", "three", actual[15]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[16]);
assertEquals("Invalid JSON result for sorted property view", "four", actual[17]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[18]);
assertEquals("Invalid JSON result for sorted property view", "serialization_time", actual[19]);
// try built-in function
try {
final List<GraphObjectMap> list3 = (List) new TypeInfoFunction().apply(new ActionContext(securityContext), null, new Object[] { "Test", "public" });
Assert.assertEquals("Invalid number of properties in sorted view", 6, list3.size());
Assert.assertEquals("id", list3.get(0).get(jsonName));
Assert.assertEquals("type", list3.get(1).get(jsonName));
Assert.assertEquals("one", list3.get(2).get(jsonName));
Assert.assertEquals("two", list3.get(3).get(jsonName));
Assert.assertEquals("three", list3.get(4).get(jsonName));
Assert.assertEquals("four", list3.get(5).get(jsonName));
} catch (FrameworkException fex) {
fex.printStackTrace();
}
// try scripting call
try {
final List<GraphObjectMap> list4 = (List) Scripting.evaluate(new ActionContext(securityContext), null, "${type_info('Test', 'public')}", "test");
Assert.assertEquals("Invalid number of properties in sorted view", 6, list4.size());
Assert.assertEquals("id", list4.get(0).get(jsonName));
Assert.assertEquals("type", list4.get(1).get(jsonName));
Assert.assertEquals("one", list4.get(2).get(jsonName));
Assert.assertEquals("two", list4.get(3).get(jsonName));
Assert.assertEquals("three", list4.get(4).get(jsonName));
Assert.assertEquals("four", list4.get(5).get(jsonName));
} catch (FrameworkException fex) {
fex.printStackTrace();
}
}
use of org.structr.core.GraphObjectMap in project structr by structr.
the class SnapshotsCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final SecurityContext securityContext = getWebSocket().getSecurityContext();
final App app = StructrApp.getInstance(securityContext);
final Map<String, Object> data = webSocketData.getNodeData();
final String mode = (String) data.get("mode");
final String name = (String) data.get("name");
final String typesString = (String) data.get("types");
final List<String> types;
if (typesString != null) {
types = Arrays.asList(StringUtils.split(typesString, ","));
} else {
types = null;
}
if (mode != null) {
final List<GraphObject> result = new LinkedList<>();
switch(mode) {
case "list":
final List<String> snapshots = SnapshotCommand.listSnapshots();
if (snapshots != null) {
final GraphObjectMap snapshotContainer = new GraphObjectMap();
snapshotContainer.put(snapshotsProperty, snapshots);
result.add(snapshotContainer);
}
break;
case "get":
final Path snapshotFile = Paths.get(SnapshotCommand.getSnapshotsPath() + name);
if (Files.exists(snapshotFile)) {
try {
final String content = new String(Files.readAllBytes(snapshotFile));
// Send content directly
getWebSocket().send(MessageBuilder.finished().callback(callback).data("schemaJson", content).build(), true);
return;
} catch (IOException ex) {
LoggerFactory.getLogger(SnapshotsCommand.class.getName()).error("", ex);
}
}
break;
default:
final GraphObjectMap msg = new GraphObjectMap();
result.add(msg);
try {
app.command(SnapshotCommand.class).execute(mode, name, types);
msg.put(statusProperty, "success");
} catch (Throwable t) {
logger.warn("", t);
msg.put(statusProperty, t.getMessage());
}
}
// set full result list
webSocketData.setResult(result);
webSocketData.setRawResultCount(1);
getWebSocket().send(webSocketData, true);
} else {
getWebSocket().send(MessageBuilder.status().code(422).message("Mode must be one of list, export, add or restore.").build(), true);
}
}
use of org.structr.core.GraphObjectMap in project structr by structr.
the class SchemaHelper method getSchemaTypeInfo.
// ----- public static methods -----
public static List<GraphObjectMap> getSchemaTypeInfo(final SecurityContext securityContext, final String rawType, final Class type, final String propertyView) throws FrameworkException {
List<GraphObjectMap> resultList = new LinkedList<>();
if (type != null) {
if (propertyView != null) {
for (final Map.Entry<String, Object> entry : getPropertiesForView(securityContext, type, propertyView).entrySet()) {
final GraphObjectMap property = new GraphObjectMap();
for (final Map.Entry<String, Object> prop : ((Map<String, Object>) entry.getValue()).entrySet()) {
property.setProperty(new GenericProperty(prop.getKey()), prop.getValue());
}
resultList.add(property);
}
} else {
final GraphObjectMap schema = new GraphObjectMap();
resultList.add(schema);
String url = "/".concat(rawType);
schema.setProperty(new StringProperty("url"), url);
schema.setProperty(new StringProperty("type"), type.getSimpleName());
schema.setProperty(new StringProperty("className"), type.getName());
schema.setProperty(new StringProperty("extendsClass"), type.getSuperclass().getName());
schema.setProperty(new BooleanProperty("isRel"), AbstractRelationship.class.isAssignableFrom(type));
schema.setProperty(new LongProperty("flags"), SecurityContext.getResourceFlags(rawType));
Set<String> propertyViews = new LinkedHashSet<>(StructrApp.getConfiguration().getPropertyViewsForType(type));
// list property sets for all views
Map<String, Map<String, Object>> views = new TreeMap();
schema.setProperty(new GenericProperty("views"), views);
for (final String view : propertyViews) {
if (!View.INTERNAL_GRAPH_VIEW.equals(view)) {
views.put(view, getPropertiesForView(securityContext, type, view));
}
}
}
}
return resultList;
}
use of org.structr.core.GraphObjectMap in project structr by structr.
the class Function method recursivelyConvertMapToGraphObjectMap.
protected static void recursivelyConvertMapToGraphObjectMap(final GraphObjectMap destination, final Map<String, Object> source, final int depth) {
if (depth > 20) {
return;
}
for (final Map.Entry<String, Object> entry : source.entrySet()) {
final ConfigurationProvider provider = StructrApp.getConfiguration();
final String key = entry.getKey();
final Object value = entry.getValue();
if (value instanceof Map) {
final Map<String, Object> map = (Map<String, Object>) value;
final GraphObjectMap obj = new GraphObjectMap();
destination.put(provider.getPropertyKeyForJSONName(obj.getClass(), key), obj);
recursivelyConvertMapToGraphObjectMap(obj, map, depth + 1);
} else if (value instanceof Collection) {
final List list = new LinkedList();
final Collection collection = (Collection) value;
for (final Object obj : collection) {
if (obj instanceof Map) {
final GraphObjectMap container = new GraphObjectMap();
list.add(container);
recursivelyConvertMapToGraphObjectMap(container, (Map<String, Object>) obj, depth + 1);
} else {
list.add(obj);
}
}
destination.put(provider.getPropertyKeyForJSONName(list.getClass(), key), list);
} else {
destination.put(value != null ? provider.getPropertyKeyForJSONName(value.getClass(), key) : new StringProperty(key), value);
}
}
}
Aggregations