use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class StructrSchema method replaceDatabaseSchema.
/**
* Replaces the current Structr schema with the given new schema. This
* method is the reverse of createFromDatabase above.
*
* @param app
* @param newSchema the new schema to replace the current Structr schema
*
* @throws FrameworkException
* @throws URISyntaxException
*/
public static void replaceDatabaseSchema(final App app, final JsonSchema newSchema) throws FrameworkException, URISyntaxException {
Services.getInstance().setOverridingSchemaTypesAllowed(true);
try (final Tx tx = app.tx()) {
for (final SchemaRelationshipNode schemaRelationship : app.nodeQuery(SchemaRelationshipNode.class).getAsList()) {
app.delete(schemaRelationship);
}
for (final SchemaNode schemaNode : app.nodeQuery(SchemaNode.class).getAsList()) {
app.delete(schemaNode);
}
for (final SchemaMethod schemaMethod : app.nodeQuery(SchemaMethod.class).getAsList()) {
app.delete(schemaMethod);
}
for (final SchemaMethodParameter schemaMethodParameter : app.nodeQuery(SchemaMethodParameter.class).getAsList()) {
app.delete(schemaMethodParameter);
}
for (final SchemaProperty schemaProperty : app.nodeQuery(SchemaProperty.class).getAsList()) {
app.delete(schemaProperty);
}
for (final SchemaView schemaView : app.nodeQuery(SchemaView.class).getAsList()) {
app.delete(schemaView);
}
newSchema.createDatabaseSchema(app, JsonSchema.ImportMode.replace);
tx.success();
}
}
use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class StructrScriptProperty method createDatabaseSchema.
@Override
SchemaProperty createDatabaseSchema(final App app, final AbstractSchemaNode schemaNode) throws FrameworkException {
final SchemaProperty property = super.createDatabaseSchema(app, schemaNode);
final PropertyMap properties = new PropertyMap();
final String contentType = getContentType();
if (contentType != null) {
switch(contentType) {
case "application/x-structr-javascript":
case "application/x-structr-script":
properties.put(SchemaProperty.propertyType, Type.Function.name());
break;
case "application/x-cypher":
properties.put(SchemaProperty.propertyType, Type.Cypher.name());
}
} else {
// default
properties.put(SchemaProperty.propertyType, Type.Function.name());
}
properties.put(SchemaProperty.format, source);
// set properties in bulk
property.setProperties(SecurityContext.getSuperUserInstance(), properties);
return property;
}
use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class StructrThumbnailProperty method createDatabaseSchema.
@Override
SchemaProperty createDatabaseSchema(final App app, final AbstractSchemaNode schemaNode) throws FrameworkException {
final SchemaProperty property = super.createDatabaseSchema(app, schemaNode);
final PropertyMap properties = new PropertyMap();
properties.put(SchemaProperty.propertyType, Type.Thumbnail.name());
properties.put(SchemaProperty.format, getFormat());
property.setProperties(SecurityContext.getSuperUserInstance(), properties);
return property;
}
use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class StructrTypeDefinition method createDatabaseSchema.
AbstractSchemaNode createDatabaseSchema(final App app) throws FrameworkException {
final Map<String, SchemaProperty> schemaProperties = new TreeMap<>();
final Map<String, SchemaMethod> schemaMethods = new TreeMap<>();
final PropertyMap createProperties = new PropertyMap();
final PropertyMap nodeProperties = new PropertyMap();
// properties that always need to be set
createProperties.put(SchemaNode.isInterface, isInterface);
createProperties.put(SchemaNode.isAbstract, isAbstract);
createProperties.put(SchemaNode.category, category);
createProperties.put(SchemaNode.isBuiltinType, SchemaService.DynamicSchemaRootURI.equals(root.getId()));
final T schemaNode = createSchemaNode(app, createProperties);
for (final StructrPropertyDefinition property : properties) {
final SchemaProperty schemaProperty = property.createDatabaseSchema(app, schemaNode);
if (schemaProperty != null) {
schemaProperties.put(schemaProperty.getName(), schemaProperty);
}
}
// create views and associate the properties
for (final Entry<String, Set<String>> view : views.entrySet()) {
final List<SchemaProperty> viewProperties = new LinkedList<>();
final List<String> nonGraphProperties = new LinkedList<>();
for (final String propertyName : view.getValue()) {
final SchemaProperty property = schemaProperties.get(propertyName);
if (property != null) {
viewProperties.add(property);
} else {
nonGraphProperties.add(propertyName);
}
}
SchemaView viewNode = app.nodeQuery(SchemaView.class).and(SchemaView.schemaNode, schemaNode).and(SchemaView.name, view.getKey()).getFirst();
if (viewNode == null) {
viewNode = app.create(SchemaView.class, new NodeAttribute<>(SchemaView.schemaNode, schemaNode), new NodeAttribute<>(SchemaView.name, view.getKey()));
}
final PropertyMap updateProperties = new PropertyMap();
updateProperties.put(SchemaView.schemaProperties, viewProperties);
updateProperties.put(SchemaView.nonGraphProperties, StringUtils.join(nonGraphProperties, ", "));
// update properties of existing or new schema view node
viewNode.setProperties(SecurityContext.getSuperUserInstance(), updateProperties);
}
for (final StructrMethodDefinition method : methods) {
final SchemaMethod schemaMethod = method.createDatabaseSchema(app, schemaNode);
if (schemaMethod != null) {
schemaMethods.put(schemaMethod.getName(), schemaMethod);
}
}
// extends
if (baseTypeReference != null) {
final Object def = root.resolveURI(baseTypeReference);
if (def != null && def instanceof JsonType) {
final JsonType jsonType = (JsonType) def;
final String superclassName = "org.structr.dynamic." + jsonType.getName();
nodeProperties.put(SchemaNode.extendsClass, superclassName);
} else {
final Class superclass = StructrApp.resolveSchemaId(baseTypeReference);
if (superclass != null) {
if (superclass.isInterface()) {
nodeProperties.put(SchemaNode.implementsInterfaces, superclass.getName());
} else {
nodeProperties.put(SchemaNode.extendsClass, superclass.getName());
}
} else if ("https://structr.org/v1.1/definitions/FileBase".equals(baseTypeReference.toString())) {
// FileBase doesn't exist any more, but we need to support it for some time..
nodeProperties.put(SchemaNode.implementsInterfaces, "org.structr.web.entity.File");
} else if (!StructrApp.getSchemaBaseURI().relativize(baseTypeReference).isAbsolute()) {
// resolve internal type referenced in special URI
final URI base = StructrApp.getSchemaBaseURI().resolve("definitions/");
final URI type = base.relativize(baseTypeReference);
final String typeName = type.getPath();
final String parameters = type.getQuery();
final Class internal = StructrApp.getConfiguration().getNodeEntityClass(typeName);
if (internal != null) {
nodeProperties.put(SchemaNode.extendsClass, getParameterizedType(internal, parameters));
}
}
}
}
// implements
if (!implementedInterfaces.isEmpty()) {
final Set<String> interfaces = new LinkedHashSet<>();
for (final URI implementedInterface : implementedInterfaces) {
final Object def = root.resolveURI(implementedInterface);
if (def != null && def instanceof JsonType) {
final JsonType jsonType = (JsonType) def;
final String superclassName = "org.structr.dynamic." + jsonType.getName();
if (jsonType.isInterface()) {
interfaces.add(superclassName);
} else {
nodeProperties.put(SchemaNode.extendsClass, superclassName);
}
} else {
final Class superclass = StructrApp.resolveSchemaId(implementedInterface);
if (superclass != null) {
interfaces.add(superclass.getName());
} else {
logger.warn("Unable to resolve built-in type {} against Structr schema", implementedInterface);
StructrApp.resolveSchemaId(implementedInterface);
}
}
}
nodeProperties.put(SchemaNode.implementsInterfaces, StringUtils.join(interfaces, ", "));
}
schemaNode.setProperties(SecurityContext.getSuperUserInstance(), nodeProperties);
return schemaNode;
}
use of org.structr.core.entity.SchemaProperty in project structr by structr.
the class StructrTypeDefinition method deserialize.
void deserialize(final T schemaNode) {
for (final SchemaProperty property : schemaNode.getProperty(AbstractSchemaNode.schemaProperties)) {
final StructrPropertyDefinition propertyDefinition = StructrPropertyDefinition.deserialize(this, property);
if (propertyDefinition != null) {
properties.add(propertyDefinition);
}
}
for (final SchemaView view : schemaNode.getProperty(AbstractSchemaNode.schemaViews)) {
if (!View.INTERNAL_GRAPH_VIEW.equals(view.getName())) {
final Set<String> propertySet = new TreeSet<>();
for (final SchemaProperty property : view.getProperty(SchemaView.schemaProperties)) {
propertySet.add(property.getName());
}
final String nonGraphProperties = view.getProperty(SchemaView.nonGraphProperties);
if (nonGraphProperties != null) {
for (final String property : nonGraphProperties.split("[, ]+")) {
final String trimmed = property.trim();
if (StringUtils.isNotBlank(trimmed)) {
propertySet.add(trimmed);
}
}
}
if (!propertySet.isEmpty()) {
views.put(view.getName(), propertySet);
}
}
}
for (final SchemaMethod method : schemaNode.getProperty(AbstractSchemaNode.schemaMethods)) {
final StructrMethodDefinition newMethod = StructrMethodDefinition.deserialize(this, method);
if (newMethod != null) {
methods.add(newMethod);
}
}
// $extends
final String extendsClass = schemaNode.getProperty(SchemaNode.extendsClass);
if (extendsClass != null) {
final String typeName = resolveParameterizedType(extendsClass);
if (extendsClass.startsWith("org.structr.dynamic.")) {
this.baseTypeReference = root.getId().resolve("definitions/" + typeName);
} else {
this.baseTypeReference = StructrApp.getSchemaBaseURI().resolve("definitions/" + typeName);
}
}
// $implements
final String implementsInterfaces = schemaNode.getProperty(SchemaNode.implementsInterfaces);
if (implementsInterfaces != null) {
for (final String impl : implementsInterfaces.split("[, ]+")) {
final String trimmed = impl.trim();
if (StringUtils.isNotEmpty(trimmed)) {
final String typeName = trimmed.substring(trimmed.lastIndexOf(".") + 1);
if (trimmed.startsWith("org.structr.dynamic.")) {
this.implementedInterfaces.add(root.getId().resolve("definitions/" + typeName));
} else {
this.implementedInterfaces.add(StructrApp.getSchemaBaseURI().resolve("definitions/" + typeName));
}
}
}
}
this.isInterface = schemaNode.getProperty(SchemaNode.isInterface);
this.isAbstract = schemaNode.getProperty(SchemaNode.isAbstract);
this.category = schemaNode.getProperty(SchemaNode.category);
}
Aggregations