use of org.structr.core.entity.SchemaNode in project structr by structr.
the class ValidationTest method testInheritedStringPropertyUniqueness.
@Test
public void testInheritedStringPropertyUniqueness() {
try (final Tx tx = app.tx()) {
final SchemaNode testType = app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testUnique"), "String!"));
app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "TestDerived"), new NodeAttribute<>(SchemaNode.extendsClass, "org.structr.dynamic.Test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
final Class testType = StructrApp.getConfiguration().getNodeEntityClass("TestDerived");
if (testType != null) {
final PropertyKey key = StructrApp.key(testType, "testUnique");
if (key != null) {
Settings.CypherDebugLogging.setValue(true);
try (final Tx tx = app.tx()) {
// key must be unique, but can empty
app.create(testType, new NodeAttribute<>(key, "unique"));
app.create(testType, new NodeAttribute<>(key, ""));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
Settings.CypherDebugLogging.setValue(true);
for (int i = 0; i < 5; i++) {
try (final Tx tx = app.tx()) {
app.create(testType, new NodeAttribute<>(key, "unique"));
tx.success();
fail("Uniqueness 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", "testUnique", token.getProperty());
assertEquals("Invalid uniqueness validation result", "TestDerived", token.getType());
assertEquals("Invalid uniqueness validation result", "already_taken", token.getToken());
}
}
}
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class SchemaHelper method hasSchemaProperty.
private static boolean hasSchemaProperty(final String typeName, final String propertyName) throws FrameworkException {
final Set<String> visited = new LinkedHashSet<>();
final Queue<String> types = new LinkedList<>();
final App app = StructrApp.getInstance();
types.add(typeName);
while (!types.isEmpty()) {
final String type = types.poll();
if (!visited.contains(type)) {
visited.add(type);
final SchemaNode schemaNode = app.nodeQuery(SchemaNode.class).andName(type).getFirst();
if (schemaNode != null) {
final SchemaProperty schemaProperty = app.nodeQuery(SchemaProperty.class).and(SchemaProperty.schemaNode, schemaNode).andName(propertyName).getFirst();
if (schemaProperty != null || hasRelationshipNode(schemaNode, propertyName)) {
return true;
} else {
// add superclass AND interfaces
String localTypeName = schemaNode.getProperty(SchemaNode.extendsClass);
if (localTypeName != null) {
localTypeName = cleanTypeName(localTypeName);
localTypeName = localTypeName.substring(localTypeName.lastIndexOf(".") + 1);
types.add(localTypeName);
}
final String interfaces = schemaNode.getProperty(SchemaNode.implementsInterfaces);
if (StringUtils.isNotBlank(interfaces)) {
for (final String iface : collectInterfaces(interfaces)) {
String cleaned = cleanTypeName(iface);
cleaned = cleaned.substring(cleaned.lastIndexOf(".") + 1);
types.add(cleaned);
}
}
}
} else {
break;
}
}
}
return false;
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class SchemaHelper method cleanUnusedDynamicGrants.
public static void cleanUnusedDynamicGrants(final List<SchemaNode> existingSchemaNodes) {
try {
final List<DynamicResourceAccess> existingDynamicGrants = StructrApp.getInstance().nodeQuery(DynamicResourceAccess.class).getAsList();
final Set<String> existingSchemaNodeNames = new HashSet<>();
for (final SchemaNode schemaNode : existingSchemaNodes) {
existingSchemaNodeNames.add(schemaNode.getResourceSignature());
}
for (final DynamicResourceAccess grant : existingDynamicGrants) {
boolean foundAllParts = true;
final String sig;
try {
sig = grant.getResourceSignature();
} catch (NotFoundException nfe) {
logger.debug("Unable to get signature from grant");
continue;
}
// Try to find schema nodes for all parts of the grant signature
final String[] parts = StringUtils.split(sig, "/");
if (parts != null) {
for (final String sigPart : parts) {
if ("/".equals(sigPart) || sigPart.startsWith("_")) {
continue;
}
// If one of the signature parts doesn't have an equivalent existing schema node, remove it
foundAllParts &= existingSchemaNodeNames.contains(sigPart);
}
}
if (!foundAllParts) {
logger.info("Did not find all parts of signature, will be removed: {}, ", new Object[] { sig });
removeDynamicGrants(sig);
}
}
} catch (Throwable t) {
logger.warn("", t);
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class SchemaHelper method reloadSchema.
public static boolean reloadSchema(final ErrorBuffer errorBuffer, final String initiatedBySessionId) {
try {
final App app = StructrApp.getInstance();
final List<SchemaNode> existingSchemaNodes = app.nodeQuery(SchemaNode.class).getAsList();
cleanUnusedDynamicGrants(existingSchemaNodes);
for (final SchemaNode schemaNode : existingSchemaNodes) {
createDynamicGrants(schemaNode.getResourceSignature());
}
for (final SchemaRelationshipNode schemaRelationship : StructrApp.getInstance().nodeQuery(SchemaRelationshipNode.class).getAsList()) {
createDynamicGrants(schemaRelationship.getResourceSignature());
createDynamicGrants(schemaRelationship.getInverseResourceSignature());
}
} catch (Throwable t) {
logger.warn("", t);
}
return SchemaService.reloadSchema(errorBuffer, initiatedBySessionId);
}
Aggregations