use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class AdvancedSchemaTest method test03InheritanceOfFileAttributesToSubclassOfImage.
@Test
public void test03InheritanceOfFileAttributesToSubclassOfImage() {
try (final Tx tx = app.tx()) {
createAdminUser();
createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName("File").getFirst();
SchemaProperty testFileProperty = app.create(SchemaProperty.class);
final PropertyMap testFileProperties = new PropertyMap();
testFileProperties.put(SchemaProperty.name, "testFile");
testFileProperties.put(SchemaProperty.propertyType, "String");
testFileProperties.put(SchemaProperty.schemaNode, fileNodeDef);
testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
// Create new schema node for dynamic class SubFile which extends File
SchemaNode subFile = app.create(SchemaNode.class);
final PropertyMap subFileProperties = new PropertyMap();
subFileProperties.put(SchemaNode.name, "SubFile");
subFileProperties.put(SchemaNode.extendsClass, "org.structr.dynamic.Image");
subFile.setProperties(subFile.getSecurityContext(), subFileProperties);
// Add String property "testSubFile" to new dynamic class
SchemaProperty testFileProperty = app.create(SchemaProperty.class);
final PropertyMap testFileProperties = new PropertyMap();
testFileProperties.put(SchemaProperty.name, "testSubFile");
testFileProperties.put(SchemaProperty.propertyType, "String");
testFileProperties.put(SchemaProperty.schemaNode, subFile);
testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
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).body("result", hasSize(count2 + 1)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testSubFile"), hasEntry("declaringClass", "SubFile")))).when().get("/_schema/SubFile/ui");
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class AdvancedSchemaTest method test02InheritanceOfFileAttributesToSubclass.
@Test
public void test02InheritanceOfFileAttributesToSubclass() {
try (final Tx tx = app.tx()) {
createAdminUser();
createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName("File").getFirst();
SchemaProperty testFileProperty = app.create(SchemaProperty.class);
final PropertyMap changedProperties = new PropertyMap();
changedProperties.put(SchemaProperty.name, "testFile");
changedProperties.put(SchemaProperty.propertyType, "String");
changedProperties.put(SchemaProperty.schemaNode, fileNodeDef);
testFileProperty.setProperties(testFileProperty.getSecurityContext(), changedProperties);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
// Create new schema node for dynamic class SubFile which extends File
SchemaNode subFile = app.create(SchemaNode.class);
final PropertyMap subFileProperties = new PropertyMap();
subFileProperties.put(SchemaNode.name, "SubFile");
subFileProperties.put(SchemaNode.extendsClass, "org.structr.dynamic.File");
subFile.setProperties(subFile.getSecurityContext(), subFileProperties);
// Add String property "testSubFile" to new dynamic class
SchemaProperty testFileProperty = app.create(SchemaProperty.class);
final PropertyMap testFileProperties = new PropertyMap();
testFileProperties.put(SchemaProperty.name, "testSubFile");
testFileProperties.put(SchemaProperty.propertyType, "String");
testFileProperties.put(SchemaProperty.schemaNode, subFile);
testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
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).body("result", hasSize(count1 + 1)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testSubFile"), hasEntry("declaringClass", "SubFile")))).when().get("/_schema/SubFile/ui");
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class StructrSchemaPropertiesPath method getDirectoryStream.
@Override
public DirectoryStream<Path> getDirectoryStream(DirectoryStream.Filter<? super Path> filter) {
if (schemaNode != null) {
return new DirectoryStream() {
boolean closed = false;
@Override
public Iterator iterator() {
final App app = StructrApp.getInstance(fs.getSecurityContext());
final List<StructrPath> nodes = new LinkedList<>();
try (final Tx tx = app.tx()) {
for (final SchemaProperty schemaProperty : schemaNode.getProperty(SchemaNode.schemaProperties)) {
nodes.add(new StructrSchemaPropertyPath(fs, StructrSchemaPropertiesPath.this, schemaNode, schemaProperty));
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return nodes.iterator();
}
@Override
public void close() throws IOException {
closed = true;
}
};
}
return null;
}
use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class StructrSchemaPropertiesPath method resolveStructrPath.
@Override
public StructrPath resolveStructrPath(final String pathComponent) {
final App app = StructrApp.getInstance(fs.getSecurityContext());
StructrPath path = null;
try (final Tx tx = app.tx()) {
for (final SchemaProperty schemaProperty : schemaNode.getProperty(SchemaNode.schemaProperties)) {
if (pathComponent.equals(schemaProperty.getName())) {
path = new StructrSchemaPropertyPath(fs, StructrSchemaPropertiesPath.this, schemaNode, schemaProperty);
}
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
return path;
}
use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class SchemaHelper method extractProperties.
public static String extractProperties(final Schema entity, final Set<String> propertyNames, final Set<Validator> validators, final Set<String> compoundIndexKeys, final Set<String> enums, final Map<String, Set<String>> views, final List<String> propertyValidators, final ErrorBuffer errorBuffer) throws FrameworkException {
final PropertyContainer propertyContainer = entity.getPropertyContainer();
final StringBuilder src = new StringBuilder();
// output property source code and collect views
for (String propertyName : SchemaHelper.getProperties(propertyContainer)) {
if (!propertyName.startsWith("__") && propertyContainer.hasProperty(propertyName)) {
String rawType = propertyContainer.getProperty(propertyName).toString();
PropertySourceGenerator parser = SchemaHelper.getSourceGenerator(errorBuffer, entity.getClassName(), new StringBasedPropertyDefinition(propertyName, rawType));
if (parser != null) {
// migrate properties
if (entity instanceof AbstractSchemaNode) {
parser.createSchemaPropertyNode((AbstractSchemaNode) entity, propertyName);
}
}
}
}
final List<SchemaProperty> schemaProperties = entity.getSchemaProperties();
if (schemaProperties != null) {
for (final SchemaProperty schemaProperty : schemaProperties) {
String propertyName = schemaProperty.getPropertyName();
String oldName = propertyName;
int count = 1;
if (propertyNames.contains(propertyName)) {
while (propertyNames.contains(propertyName)) {
propertyName = propertyName + count++;
}
logger.warn("Property name {} already present in type {}, renaming to {}", oldName, entity.getClassName(), propertyName);
schemaProperty.setProperty(SchemaProperty.name, propertyName);
}
propertyNames.add(propertyName);
if (!schemaProperty.getProperty(SchemaProperty.isBuiltinProperty)) {
// migrate property source
if (Type.Function.equals(schemaProperty.getPropertyType())) {
// Note: This is a temporary migration from the old format to the new readFunction property
final String format = schemaProperty.getFormat();
if (format != null) {
try {
schemaProperty.setProperty(SchemaProperty.readFunction, format);
schemaProperty.setProperty(SchemaProperty.format, null);
} catch (FrameworkException ex) {
logger.warn("", ex);
}
}
}
final PropertySourceGenerator parser = SchemaHelper.getSourceGenerator(errorBuffer, entity.getClassName(), schemaProperty);
if (parser != null) {
// add property name to set for later use
propertyNames.add(schemaProperty.getPropertyName());
// append created source from parser
parser.getPropertySource(src, entity);
// register global elements created by parser
validators.addAll(parser.getGlobalValidators());
compoundIndexKeys.addAll(parser.getCompoundIndexKeys());
enums.addAll(parser.getEnumDefinitions());
// built-in schema properties are configured manually
if (!schemaProperty.isPartOfBuiltInSchema()) {
// register property in default view
addPropertyToView(PropertyView.Ui, propertyName, views);
}
}
}
final String[] propertyValidatorsArray = schemaProperty.getProperty(SchemaProperty.validators);
if (propertyValidatorsArray != null) {
propertyValidators.addAll(Arrays.asList(propertyValidatorsArray));
}
}
}
return src.toString();
}
Aggregations