use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class ScriptingTest method testSetPropertyWithDynamicNodes.
@Test
public void testSetPropertyWithDynamicNodes() {
/**
* This test creates two connected SchemaNodes and tests the script-based
* association of one instance with several others in the onCreate method.
*/
final long currentTimeMillis = System.currentTimeMillis();
Class sourceType = null;
Class targetType = null;
PropertyKey targetsProperty = null;
EnumProperty testEnumProperty = null;
PropertyKey testBooleanProperty = null;
PropertyKey testIntegerProperty = null;
PropertyKey testStringProperty = null;
PropertyKey testDoubleProperty = null;
PropertyKey testDateProperty = null;
Class testEnumType = null;
// setup phase: create schema nodes
try (final Tx tx = app.tx()) {
// create two nodes and associate them with each other
final SchemaNode sourceNode = createTestNode(SchemaNode.class, "Source");
final SchemaNode targetNode = createTestNode(SchemaNode.class, "Target");
final List<SchemaProperty> properties = new LinkedList<>();
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testBoolean"), new NodeAttribute(SchemaProperty.propertyType, "Boolean")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testInteger"), new NodeAttribute(SchemaProperty.propertyType, "Integer")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testString"), new NodeAttribute(SchemaProperty.propertyType, "String")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDouble"), new NodeAttribute(SchemaProperty.propertyType, "Double")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testEnum"), new NodeAttribute(SchemaProperty.propertyType, "Enum"), new NodeAttribute(SchemaProperty.format, "OPEN, CLOSED, TEST")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDate"), new NodeAttribute(SchemaProperty.propertyType, "Date")));
sourceNode.setProperty(SchemaNode.schemaProperties, properties);
final List<SchemaMethod> methods = new LinkedList<>();
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "onCreate"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.targets = Structr.find('Target'); }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'OPEN'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest02"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'CLOSED'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest03"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'TEST'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest04"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'INVALID'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest05"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testBoolean = true; e.testInteger = 123; e.testString = 'testing..'; e.testDouble = 1.2345; e.testDate = new Date(" + currentTimeMillis + "); }")));
sourceNode.setProperty(SchemaNode.schemaMethods, methods);
final PropertyMap propertyMap = new PropertyMap();
propertyMap.put(SchemaRelationshipNode.sourceId, sourceNode.getUuid());
propertyMap.put(SchemaRelationshipNode.targetId, targetNode.getUuid());
propertyMap.put(SchemaRelationshipNode.sourceJsonName, "source");
propertyMap.put(SchemaRelationshipNode.targetJsonName, "targets");
propertyMap.put(SchemaRelationshipNode.sourceMultiplicity, "*");
propertyMap.put(SchemaRelationshipNode.targetMultiplicity, "*");
propertyMap.put(SchemaRelationshipNode.relationshipType, "HAS");
app.create(SchemaRelationshipNode.class, propertyMap);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
sourceType = config.getNodeEntityClass("Source");
targetType = config.getNodeEntityClass("Target");
targetsProperty = StructrApp.key(sourceType, "targets");
// we need to cast to EnumProperty in order to obtain the dynamic enum type
testEnumProperty = (EnumProperty) StructrApp.key(sourceType, "testEnum");
testEnumType = testEnumProperty.getEnumType();
testBooleanProperty = StructrApp.key(sourceType, "testBoolean");
testIntegerProperty = StructrApp.key(sourceType, "testInteger");
testStringProperty = StructrApp.key(sourceType, "testString");
testDoubleProperty = StructrApp.key(sourceType, "testDouble");
testDateProperty = StructrApp.key(sourceType, "testDate");
assertNotNull(sourceType);
assertNotNull(targetType);
assertNotNull(targetsProperty);
// create 5 target nodes
createTestNodes(targetType, 5);
// create source node
createTestNodes(sourceType, 5);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// check phase: source node should have all five target nodes associated with HAS
try (final Tx tx = app.tx()) {
// check all source nodes
for (final Object obj : app.nodeQuery(sourceType).getAsList()) {
assertNotNull("Invalid nodeQuery result", obj);
final GraphObject sourceNode = (GraphObject) obj;
// test contents of "targets" property
final Object targetNodesObject = sourceNode.getProperty(targetsProperty);
assertTrue("Invalid getProperty result for scripted association", targetNodesObject instanceof List);
final List list = (List) targetNodesObject;
assertEquals("Invalid getProperty result for scripted association", 5, list.size());
}
final GraphObject sourceNode = app.nodeQuery(sourceType).getFirst();
// set testEnum property to OPEN via doTest01 function call, check result
sourceNode.invokeMethod("doTest01", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[0], sourceNode.getProperty(testEnumProperty));
// set testEnum property to CLOSED via doTest02 function call, check result
sourceNode.invokeMethod("doTest02", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[1], sourceNode.getProperty(testEnumProperty));
// set testEnum property to TEST via doTest03 function call, check result
sourceNode.invokeMethod("doTest03", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
// set testEnum property to INVALID via doTest03 function call, expect previous value & error
try {
sourceNode.invokeMethod("doTest04", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
fail("Setting EnumProperty to invalid value should result in an Exception!");
} catch (FrameworkException fx) {
}
// test other property types
sourceNode.invokeMethod("doTest05", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for BooleanProperty", true, sourceNode.getProperty(testBooleanProperty));
assertEquals("Invalid setProperty result for IntegerProperty", 123, sourceNode.getProperty(testIntegerProperty));
assertEquals("Invalid setProperty result for StringProperty", "testing..", sourceNode.getProperty(testStringProperty));
assertEquals("Invalid setProperty result for DoubleProperty", 1.2345, sourceNode.getProperty(testDoubleProperty));
assertEquals("Invalid setProperty result for DateProperty", new Date(currentTimeMillis), sourceNode.getProperty(testDateProperty));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class CsvImportTest method testCsvFileImportDoubleQuotes.
@Test
public void testCsvFileImportDoubleQuotes() {
String newFileId = null;
// test setup
try (final Tx tx = app.tx()) {
final String csvData = "\"id\";\"type\";\"name\";\"Test header with whitespace\";\"ümläüt header\"\n" + "\"0\";\"One\";\"name: one\";\"11\";\"22\"\n" + "\"1\";\"Two\";\"name: two\";\"22\";\"33\"\n" + "\"2\";\"Three\";\"name: three\";\"33\";\"44\"";
final byte[] fileData = csvData.getBytes("utf-8");
final File file = FileHelper.createFile(securityContext, fileData, "text/csv", File.class, "test.csv");
// extract UUID for later use
newFileId = file.getUuid();
// create new type
final JsonSchema schema = StructrSchema.createEmptySchema();
final JsonType newType = schema.addType("Item");
newType.addStringProperty("name");
newType.addIntegerProperty("originId").isIndexed();
newType.addStringProperty("typeName");
newType.addIntegerProperty("test1");
newType.addIntegerProperty("test2");
StructrSchema.extendDatabaseSchema(app, schema);
// create test user
app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
tx.success();
} catch (Throwable t) {
t.printStackTrace();
fail("Unexpected exception.");
}
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
final Map<String, Object> params = new LinkedHashMap<>();
final Map<String, Object> mappings = new LinkedHashMap<>();
// import parameters
params.put("targetType", "Item");
params.put("quoteChar", "\"");
params.put("delimiter", ";");
params.put("mappings", mappings);
// property mapping
mappings.put("originId", "id");
mappings.put("typeName", "type");
mappings.put("name", "name");
mappings.put("test1", "Test header with whitespace");
mappings.put("test2", "ümläüt header");
RestAssured.given().contentType("application/json; charset=UTF-8").header("X-User", "admin").header("X-Password", "admin").filter(RequestLoggingFilter.logRequestTo(System.out)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).body(gson.toJson(params)).expect().statusCode(200).when().post("/File/" + newFileId + "/doCSVImport");
// wait for result (import is async.)
try {
Thread.sleep(2000);
} catch (Throwable t) {
}
// check imported data for correct import
try (final Tx tx = app.tx()) {
final ConfigurationProvider conf = StructrApp.getConfiguration();
final Class type = conf.getNodeEntityClass("Item");
final List<NodeInterface> items = app.nodeQuery(type).sort(conf.getPropertyKeyForJSONName(type, "originId")).getAsList();
assertEquals("Invalid CSV import result, expected 3 items to be created from CSV import. ", 3, items.size());
final NodeInterface one = items.get(0);
final NodeInterface two = items.get(1);
final NodeInterface three = items.get(2);
assertEquals("Invalid CSV mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid CSV mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid CSV mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
assertEquals("Invalid CSV mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid CSV mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid CSV mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
assertEquals("Invalid CSV mapping result", "name: one", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid CSV mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid CSV mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
assertEquals("Invalid CSV mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid CSV mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid CSV mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
assertEquals("Invalid CSV mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
assertEquals("Invalid CSV mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
assertEquals("Invalid CSV mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class PropertyMap method cmisTypeToJavaType.
public static PropertyMap cmisTypeToJavaType(final SecurityContext securityContext, final Class type, final Properties properties) throws FrameworkException {
final Map<String, PropertyData<?>> map = properties.getProperties();
final ConfigurationProvider config = StructrApp.getConfiguration();
final PropertyMap propertyMap = new PropertyMap();
for (final Entry<String, PropertyData<?>> entry : map.entrySet()) {
final PropertyData<?> propertyValue = entry.getValue();
Object value = propertyValue.getFirstValue();
String key = entry.getKey();
// convert CMIS properties to Structr properties
if (CMIS_PROPERTY_MAPPING.containsKey(key)) {
key = CMIS_PROPERTY_MAPPING.get(key);
}
final PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForJSONName(type, key);
if (propertyKey != null) {
final PropertyConverter converter = propertyKey.inputConverter(securityContext);
if (converter != null) {
value = converter.convert(value);
}
propertyMap.put(propertyKey, value);
} else {
throw new FrameworkException(500, "Invalid property key " + key + " for type " + type.getSimpleName() + " provided.");
}
}
return propertyMap;
}
use of org.structr.schema.ConfigurationProvider 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);
}
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class OWLInstance method resolveRelationships.
public void resolveRelationships(final JsonSchema schema, final Map<String, OWLClass> owlClassesByFragment, final Map<URI, OWLInstance> instances, final Map<String, RDFDescription> descriptions, final Map<String, OWLProperty> properties) throws FrameworkException {
if (instance != null && type != null) {
OWLParserv2.logger.println("#################################################################################################");
OWLParserv2.logger.println("Resolving relationships of " + type.getStructrName(true) + ": " + getId());
final ConfigurationProvider config = StructrApp.getConfiguration();
final NodeList propertyElements = getElement().getChildNodes();
if (propertyElements != null) {
final int len = propertyElements.getLength();
for (int i = 0; i < len; i++) {
final Node propertyElement = propertyElements.item(i);
if (propertyElement instanceof Element) {
final Element element = (Element) propertyElement;
final String tagName = RDFItem.cleanName(element.getTagName());
final String reference = getAttribute(element, "rdf:resource");
if (reference != null) {
final OWLInstance relatedInstance = instances.get(URI.create(reference));
if (relatedInstance != null) {
final OWLClass relationshipType = owlClassesByFragment.get(tagName);
if (relationshipType != null) {
final List<OWLClass> sourceTypes = relationshipType.getActualSourceTypes();
final List<OWLClass> targetTypes = relationshipType.getActualTargetTypes();
final Class hyperRelationshipType = config.getNodeEntityClass(tagName);
if (hyperRelationshipType != null) {
if (sourceTypes.size() == 1 && targetTypes.size() == 1) {
final OWLClass sourceType = sourceTypes.get(0);
final OWLClass targetType = targetTypes.get(0);
final String sourcePropertyName = sourceType.getStructrName(false);
final String targetPropertyName = targetType.getStructrName(false);
final PropertyKey sourceKey = StructrApp.key(hyperRelationshipType, sourcePropertyName);
final PropertyKey targetKey = StructrApp.key(hyperRelationshipType, targetPropertyName);
if (sourceKey != null && targetKey != null) {
if (this.instance != null && relatedInstance.instance != null) {
final NodeInterface hyperNode = StructrApp.getInstance().create(hyperRelationshipType, new NodeAttribute(sourceKey, this.instance), new NodeAttribute(targetKey, relatedInstance.instance));
// resolve properties that come via rdf:Description
final String referenceId = getAttribute(element, "rdf:ID");
if (referenceId != null && hyperNode != null) {
final RDFDescription description = descriptions.get(referenceId);
if (description != null) {
description.resolveProperties(hyperNode, owlClassesByFragment, properties);
}
}
} else {
System.out.println("!!!!!!!! No instance found to set on " + getId());
}
} else {
System.out.println(" No property keys found for " + sourcePropertyName + ", " + targetPropertyName);
}
} else {
System.out.println(" Ambiguous source or target types: " + sourceTypes + ", " + targetTypes);
}
} else {
System.out.println(" Relationship type " + tagName + " not found for " + getId());
}
} else {
System.out.println("No type found for " + tagName);
}
} else {
System.out.println(" No instance found for " + reference);
}
}
}
}
}
}
}
Aggregations