use of org.structr.core.property.StringProperty in project structr by structr.
the class ValidationTest method testConcurrentValidationOnDynamicProperty.
@Test
public void testConcurrentValidationOnDynamicProperty() {
final int count = 100;
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_testXYZ"), "+String!"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final Class type = StructrApp.getConfiguration().getNodeEntityClass("Item");
assertNotNull(type);
final PropertyKey testXYZ = StructrApp.key(type, "testXYZ");
assertNotNull(testXYZ);
final Runnable tester = new Runnable() {
@Override
public void run() {
for (int i = 0; i < count; i++) {
// testing must be done in an isolated transaction
try (final Tx tx = app.tx()) {
app.create(type, new NodeAttribute(testXYZ, "Item" + i));
tx.success();
} catch (FrameworkException fex) {
}
}
}
};
// submit three test instances
final ExecutorService executor = Executors.newCachedThreadPool();
final Future f1 = executor.submit(tester);
final Future f2 = executor.submit(tester);
final Future f3 = executor.submit(tester);
try {
f1.get();
f2.get();
f3.get();
} catch (Throwable ex) {
}
List<GraphObject> result = null;
try (final Tx tx = app.tx()) {
result = app.nodeQuery(type).getAsList();
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// verify that only count entities have been created.
assertEquals("Invalid concurrent validation result", count, result.size());
executor.shutdownNow();
}
use of org.structr.core.property.StringProperty in project structr by structr.
the class ValidationTest method testStringPropertyNotNull.
@Test
public void testStringPropertyNotNull() {
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_nonempty"), "+String"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final Class testType = StructrApp.getConfiguration().getNodeEntityClass("Test");
if (testType != null) {
final PropertyKey key = StructrApp.key(testType, "nonempty");
if (key != null) {
try (final Tx tx = app.tx()) {
app.create(testType, new NodeAttribute<>(key, null));
tx.success();
fail("Not empty constraint violated!");
} catch (FrameworkException fex) {
final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
final ErrorToken token = tokens.get(0);
assertEquals("Invalid uniqueness validation result", 1, tokens.size());
assertEquals("Invalid uniqueness validation result", 422, fex.getStatus());
assertEquals("Invalid uniqueness validation result", "nonempty", token.getProperty());
assertEquals("Invalid uniqueness validation result", "Test", token.getType());
assertEquals("Invalid uniqueness validation result", "must_not_be_empty", token.getToken());
}
}
}
}
use of org.structr.core.property.StringProperty in project structr by structr.
the class SchemaImporter method analyzeSchema.
public void analyzeSchema() {
final App app = StructrApp.getInstance();
final FileBasedHashLongMap<NodeInfo> nodeIdMap = new FileBasedHashLongMap<>(userHome + File.separator + ".structrSchemaAnalyzer");
final DatabaseService graphDb = app.getDatabaseService();
final ConfigurationProvider configuration = Services.getInstance().getConfigurationProvider();
final Set<NodeInfo> nodeTypes = new LinkedHashSet<>();
final Set<RelationshipInfo> relationships = new LinkedHashSet<>();
final Map<String, SchemaNode> schemaNodes = new LinkedHashMap<>();
final Map<String, List<TypeInfo>> typeInfoTypeMap = new LinkedHashMap<>();
final List<TypeInfo> reducedTypeInfos = new LinkedList<>();
final List<TypeInfo> typeInfos = new LinkedList<>();
Iterator<Relationship> relIterator = null;
Iterator<Node> nodeIterator = null;
info("Fetching all nodes iterator..");
try (final Tx tx = app.tx()) {
nodeIterator = Iterables.filter(new StructrAndSpatialPredicate(false, false, true), graphDb.getAllNodes()).iterator();
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
info("Starting to analyze nodes..");
bulkGraphOperation(SecurityContext.getSuperUserInstance(), nodeIterator, 100000, "Analyzing nodes", new BulkGraphOperation<Node>() {
@Override
public void handleGraphObject(final SecurityContext securityContext, final Node node) throws FrameworkException {
final NodeInfo nodeInfo = new NodeInfo(node);
// hashcode of nodeInfo is derived from its property and type signature!
nodeTypes.add(nodeInfo);
// add node ID to our new test datastructure
nodeIdMap.add(nodeInfo, node.getId());
}
});
info("Identifying common base classes..");
try (final Tx tx = app.tx(true, false, false)) {
// nodeTypes now contains all existing node types and their property sets
identifyCommonBaseClasses(app, nodeTypes, nodeIdMap, typeInfos);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
info("Collecting type information..");
try (final Tx tx = app.tx(true, false, false)) {
// group type infos by type
collectTypeInfos(typeInfos, typeInfoTypeMap);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
info("Aggregating type information..");
try (final Tx tx = app.tx(true, false, false)) {
// reduce type infos with more than one type
reduceTypeInfos(typeInfoTypeMap, reducedTypeInfos);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
info("Identifying property sets..");
try (final Tx tx = app.tx(true, false, false)) {
// intersect property sets of type infos
intersectPropertySets(reducedTypeInfos);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
info("Sorting result..");
try (final Tx tx = app.tx(true, false, false)) {
// sort type infos
Collections.sort(reducedTypeInfos, new HierarchyComparator(false));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
final Map<String, TypeInfo> reducedTypeInfoMap = new LinkedHashMap<>();
for (final TypeInfo info : reducedTypeInfos) {
final String type = info.getPrimaryType();
// map TypeInfo to type for later use
reducedTypeInfoMap.put(type, info);
info("Starting with setting of type and ID for type {}", type);
bulkGraphOperation(SecurityContext.getSuperUserInstance(), info.getNodeIds().iterator(), 10000, "Setting type and ID", new BulkGraphOperation<Long>() {
@Override
public void handleGraphObject(SecurityContext securityContext, Long nodeId) throws FrameworkException {
final Node node = graphDb.getNodeById(nodeId);
node.setProperty(GraphObject.id.dbName(), NodeServiceCommand.getNextUuid());
node.setProperty(GraphObject.type.dbName(), type);
}
});
}
info("Fetching all relationships iterator..");
try (final Tx tx = app.tx(true, false, false)) {
relIterator = Iterables.filter(new StructrAndSpatialPredicate(false, false, true), graphDb.getAllRelationships()).iterator();
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
info("Starting with analyzing relationships..");
bulkGraphOperation(SecurityContext.getSuperUserInstance(), relIterator, 10000, "Analyzing relationships", new BulkGraphOperation<Relationship>() {
@Override
public void handleGraphObject(SecurityContext securityContext, Relationship rel) throws FrameworkException {
final Node startNode = rel.getStartNode();
final Node endNode = rel.getEndNode();
// make sure node has been successfully identified above
if (startNode.hasProperty("type") && endNode.hasProperty("type")) {
final String relationshipType = rel.getType().name();
final String startNodeType = (String) startNode.getProperty("type");
final String endNodeType = (String) endNode.getProperty("type");
relationships.add(new RelationshipInfo(startNodeType, endNodeType, relationshipType));
// create combined type on imported relationship
if (startNodeType != null && endNodeType != null) {
final String combinedType = getCombinedType(startNodeType, relationshipType, endNodeType);
logger.debug("Combined relationship type {} found for rel type {}, start node type {}, end node type {}", new Object[] { combinedType, relationshipType, startNodeType, endNodeType });
rel.setProperty(GraphObject.type.dbName(), combinedType);
}
// create ID on imported relationship
rel.setProperty(GraphObject.id.dbName(), NodeServiceCommand.getNextUuid());
}
}
});
info("Grouping relationships..");
// group relationships by type
final Map<String, List<RelationshipInfo>> relTypeInfoMap = new LinkedHashMap<>();
for (final RelationshipInfo relInfo : relationships) {
// final String relType = relInfo.getRelType();
final String combinedType = getCombinedType(relInfo.getStartNodeType(), relInfo.getRelType(), relInfo.getEndNodeType());
List<RelationshipInfo> infos = relTypeInfoMap.get(combinedType);
if (infos == null) {
infos = new LinkedList<>();
relTypeInfoMap.put(combinedType, infos);
}
infos.add(relInfo);
}
info("Aggregating relationship information..");
final List<RelationshipInfo> reducedRelationshipInfos = new ArrayList<>();
if (Settings.InheritanceDetection.getValue()) {
// reduce relationship infos into one
for (final List<RelationshipInfo> infos : relTypeInfoMap.values()) {
reducedRelationshipInfos.addAll(reduceNodeTypes(infos, reducedTypeInfoMap));
}
} else {
reducedRelationshipInfos.addAll(relationships);
}
info("Starting with schema node creation..");
bulkGraphOperation(SecurityContext.getSuperUserInstance(), reducedTypeInfos.iterator(), 100000, "Creating schema nodes", new BulkGraphOperation<TypeInfo>() {
@Override
public void handleGraphObject(SecurityContext securityContext, TypeInfo typeInfo) throws FrameworkException {
final String type = typeInfo.getPrimaryType();
if (!"ReferenceNode".equals(type)) {
final Map<String, Class> props = typeInfo.getPropertySet();
final PropertyMap propertyMap = new PropertyMap();
// add properties
for (final Map.Entry<String, Class> propertyEntry : props.entrySet()) {
final String propertyName = propertyEntry.getKey();
final Class propertyType = propertyEntry.getValue();
// handle array types differently
String propertyTypeName = propertyType.getSimpleName();
if (propertyType.isArray()) {
// remove "[]" from the end and append "Array" to match the appropriate parser
propertyTypeName = propertyTypeName.substring(0, propertyTypeName.length() - 2).concat("Array");
}
propertyMap.put(new StringProperty("_".concat(propertyName)), propertyTypeName);
}
// set node type which is in "name" property
propertyMap.put(AbstractNode.name, type);
// check if there is an existing Structr entity with the same type
// and make the dynamic class extend the existing class if yes.
final Class existingType = configuration.getNodeEntityClass(type);
if (existingType != null) {
propertyMap.put(SchemaNode.extendsClass, existingType.getName());
} else if (!typeInfo.getOtherTypes().isEmpty()) {
// only the first supertype is supported
propertyMap.put(SchemaNode.extendsClass, typeInfo.getSuperclass(reducedTypeInfoMap));
}
final SchemaNode existingNode = app.nodeQuery(SchemaNode.class).andName(type).getFirst();
if (existingNode != null) {
for (final Entry<PropertyKey, Object> entry : propertyMap.entrySet()) {
existingNode.setProperty(entry.getKey(), entry.getValue());
}
schemaNodes.put(type, existingNode);
} else {
// create schema node
schemaNodes.put(type, app.create(SchemaNode.class, propertyMap));
}
}
}
});
info("Starting with schema relationship creation..");
bulkGraphOperation(SecurityContext.getSuperUserInstance(), reducedRelationshipInfos.iterator(), 100000, "Creating schema relationships", new BulkGraphOperation<RelationshipInfo>() {
@Override
public void handleGraphObject(SecurityContext securityContext, RelationshipInfo template) throws FrameworkException {
final String startNodeType = template.getStartNodeType();
final String endNodeType = template.getEndNodeType();
if (startNodeType != null && endNodeType != null) {
final SchemaNode startNode = schemaNodes.get(startNodeType);
final SchemaNode endNode = schemaNodes.get(endNodeType);
if (startNode != null && endNode != null) {
final String relationshipType = template.getRelType();
final PropertyMap propertyMap = new PropertyMap();
propertyMap.put(SchemaRelationshipNode.sourceId, startNode.getUuid());
propertyMap.put(SchemaRelationshipNode.targetId, endNode.getUuid());
propertyMap.put(SchemaRelationshipNode.relationshipType, relationshipType);
app.create(SchemaRelationshipNode.class, propertyMap);
} else {
info("Unable to create schema relationship node for {} -> {}, no schema nodes found", startNodeType, endNodeType);
}
}
}
});
info("Starting with index rebuild..");
// rebuild index
app.command(BulkRebuildIndexCommand.class).execute(Collections.EMPTY_MAP);
}
use of org.structr.core.property.StringProperty in project structr by structr.
the class PropertySourceGenerator method createSchemaPropertyNode.
public void createSchemaPropertyNode(final AbstractSchemaNode schemaNode, final String underscorePropertyName) throws FrameworkException {
final App app = StructrApp.getInstance();
final String propertyName = getSourcePropertyName();
if (app.nodeQuery(SchemaProperty.class).and(SchemaProperty.schemaNode, schemaNode).and(AbstractNode.name, propertyName).getFirst() == null) {
app.create(SchemaProperty.class, new NodeAttribute<>(AbstractNode.name, propertyName), new NodeAttribute<>(SchemaProperty.schemaNode, schemaNode), new NodeAttribute<>(SchemaProperty.propertyType, getKey().name()), new NodeAttribute<>(SchemaProperty.contentType, source.getContentType()), new NodeAttribute<>(SchemaProperty.dbName, source.getDbName()), new NodeAttribute<>(SchemaProperty.defaultValue, source.getDefaultValue()), new NodeAttribute<>(SchemaProperty.format, source.getFormat()), new NodeAttribute<>(SchemaProperty.hint, source.getHint()), new NodeAttribute<>(SchemaProperty.category, source.getCategory()), new NodeAttribute<>(SchemaProperty.fqcn, source.getFqcn()), new NodeAttribute<>(SchemaProperty.compound, source.isCompound()), new NodeAttribute<>(SchemaProperty.unique, source.isUnique()), new NodeAttribute<>(SchemaProperty.indexed, source.isIndexed()), new NodeAttribute<>(SchemaProperty.notNull, source.isNotNull()), new NodeAttribute<>(SchemaProperty.isPartOfBuiltInSchema, source.isPartOfBuiltInSchema()), new NodeAttribute<>(SchemaProperty.readFunction, source.getReadFunction()), new NodeAttribute<>(SchemaProperty.writeFunction, source.getWriteFunction()), new NodeAttribute<>(SchemaProperty.transformers, source.getTransformators()), new NodeAttribute<>(SchemaProperty.validators, source.getValidators()));
schemaNode.removeProperty(new StringProperty(underscorePropertyName));
}
}
use of org.structr.core.property.StringProperty in project structr by structr.
the class PropertyViewRestTest method testOutputDepthScriptingProperty.
@Test
public void testOutputDepthScriptingProperty() {
try (final Tx tx = app.tx()) {
final SchemaNode node = app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "ScriptTest"), new NodeAttribute<>(new StringProperty("_depth"), "Function(depth)"), new NodeAttribute<>(new StringProperty("__public"), "name, depth, children, parents"));
app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, node), new NodeAttribute<>(SchemaRelationshipNode.targetNode, node), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "test"), new NodeAttribute<>(SchemaRelationshipNode.sourceJsonName, "parents"), new NodeAttribute<>(SchemaRelationshipNode.targetJsonName, "children"));
tx.success();
} catch (Throwable t) {
t.printStackTrace();
fail("Unexpected exception.");
}
// the new test setup method requires a whole new test class for
// configuration changes, so this test class is a duplicate of
// the existing StructrRestTest.. :(
String resource = "/ScriptTest";
// create entity
final String uuid = getUuidFromLocation(RestAssured.given().contentType("application/json; charset=UTF-8").header("Accept", "application/json; charset=UTF-8").body(" { 'name' : 'ScriptTest1' } ").expect().statusCode(201).when().post(resource).getHeader("Location"));
// create second entity
RestAssured.given().contentType("application/json; charset=UTF-8").header("Accept", "application/json; charset=UTF-8").body(" { 'name' : 'ScriptTest2', 'parents': [{ 'id': '" + uuid + "' }] } ").expect().statusCode(201).when().post(resource).getHeader("Location");
// test default view with properties in it
RestAssured.given().contentType("application/json; charset=UTF-8").header("Accept", "application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).expect().statusCode(200).body("query_time", notNullValue()).body("serialization_time", notNullValue()).body("result_count", equalTo(2)).body("result", hasSize(2)).body("result[0].type", equalTo("ScriptTest")).body("result[0].depth", equalTo(0)).body("result[0].name", equalTo("ScriptTest1")).body("result[0].children[0].type", equalTo("ScriptTest")).body("result[0].children[0].depth", equalTo(1)).body("result[0].children[0].name", equalTo("ScriptTest2")).body("result[0].children[0].parents[0].type", equalTo("ScriptTest")).body("result[0].children[0].parents[0].depth", equalTo(2)).body("result[0].children[0].parents[0].name", equalTo("ScriptTest1")).body("result[0].children[0].parents[0].children[0].type", equalTo("ScriptTest")).body("result[0].children[0].parents[0].children[0].depth", equalTo(3)).body("result[0].children[0].parents[0].children[0].name", equalTo("ScriptTest2")).body("result[1].type", equalTo("ScriptTest")).body("result[1].depth", equalTo(0)).body("result[1].name", equalTo("ScriptTest2")).body("result[1].parents[0].type", equalTo("ScriptTest")).body("result[1].parents[0].depth", equalTo(1)).body("result[1].parents[0].name", equalTo("ScriptTest1")).when().get(resource);
RestAssured.given().contentType("application/json; charset=UTF-8").header("Accept", "application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).expect().statusCode(200).body("query_time", notNullValue()).body("serialization_time", notNullValue()).body("result_count", equalTo(1)).body("result.type", equalTo("ScriptTest")).body("result.depth", equalTo(0)).body("result.name", equalTo("ScriptTest1")).body("result.children[0].type", equalTo("ScriptTest")).body("result.children[0].depth", equalTo(1)).body("result.children[0].name", equalTo("ScriptTest2")).body("result.children[0].parents[0].type", equalTo("ScriptTest")).body("result.children[0].parents[0].depth", equalTo(2)).body("result.children[0].parents[0].name", equalTo("ScriptTest1")).body("result.children[0].parents[0].children[0].type", equalTo("ScriptTest")).body("result.children[0].parents[0].children[0].depth", equalTo(3)).body("result.children[0].parents[0].children[0].name", equalTo("ScriptTest2")).when().get(resource.concat("/").concat(uuid));
}
Aggregations