use of org.structr.core.entity.AbstractSchemaNode 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();
}
use of org.structr.core.entity.AbstractSchemaNode in project structr by structr.
the class SchemaHelper method extractMethods.
public static void extractMethods(final AbstractSchemaNode entity, final Map<String, List<ActionEntry>> actions) throws FrameworkException {
final PropertyContainer propertyContainer = entity.getPropertyContainer();
for (final String rawActionName : getActions(propertyContainer)) {
if (propertyContainer.hasProperty(rawActionName)) {
final String value = propertyContainer.getProperty(rawActionName).toString();
if (entity instanceof AbstractSchemaNode) {
final AbstractSchemaNode schemaNode = (AbstractSchemaNode) entity;
final App app = StructrApp.getInstance();
final String methodName = rawActionName.substring(3);
if (app.nodeQuery(SchemaMethod.class).and(SchemaMethod.schemaNode, schemaNode).and(AbstractNode.name, methodName).getFirst() == null) {
app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.schemaNode, schemaNode), new NodeAttribute<>(SchemaMethod.name, methodName), new NodeAttribute<>(SchemaMethod.source, value));
schemaNode.removeProperty(new StringProperty(rawActionName));
}
}
}
}
final List<SchemaMethod> schemaMethods = entity.getSchemaMethods();
if (schemaMethods != null) {
for (final SchemaMethod schemaMethod : schemaMethods) {
final ActionEntry entry = schemaMethod.getActionEntry(entity);
final String name = entry.getName();
List<ActionEntry> actionList = actions.get(name);
if (actionList == null) {
actionList = new LinkedList<>();
actions.put(name, actionList);
}
actionList.add(entry);
Collections.sort(actionList);
}
}
}
use of org.structr.core.entity.AbstractSchemaNode in project structr by structr.
the class SchemaHelper method extractViews.
public static void extractViews(final Schema entity, final Map<String, Set<String>> views, final Set<String> relPropertyNames, final ErrorBuffer errorBuffer) throws FrameworkException {
final PropertyContainer propertyContainer = entity.getPropertyContainer();
final ConfigurationProvider config = StructrApp.getConfiguration();
Class superClass = config.getNodeEntityClass(entity.getSuperclassName());
if (superClass == null) {
superClass = config.getRelationshipEntityClass(entity.getSuperclassName());
}
if (superClass == null) {
superClass = AbstractNode.class;
}
for (final String rawViewName : getViews(propertyContainer)) {
if (!rawViewName.startsWith("___") && propertyContainer.hasProperty(rawViewName)) {
final String value = propertyContainer.getProperty(rawViewName).toString();
final String[] parts = value.split("[,\\s]+");
final String viewName = rawViewName.substring(2);
if (entity instanceof AbstractSchemaNode) {
final List<String> nonGraphProperties = new LinkedList<>();
final List<SchemaProperty> properties = new LinkedList<>();
final AbstractSchemaNode schemaNode = (AbstractSchemaNode) entity;
final App app = StructrApp.getInstance();
if (app.nodeQuery(SchemaView.class).and(SchemaView.schemaNode, schemaNode).and(AbstractNode.name, viewName).getFirst() == null) {
// add parts to view, overrides defaults (because of clear() above)
for (int i = 0; i < parts.length; i++) {
String propertyName = parts[i].trim();
while (propertyName.startsWith("_")) {
propertyName = propertyName.substring(1);
}
// append this as a workaround to include remote properties
if (propertyName.endsWith("Property")) {
propertyName = propertyName.substring(0, propertyName.length() - "Property".length());
}
final SchemaProperty propertyNode = app.nodeQuery(SchemaProperty.class).and(SchemaProperty.schemaNode, schemaNode).andName(propertyName).getFirst();
if (propertyNode != null) {
properties.add(propertyNode);
} else {
nonGraphProperties.add(propertyName);
}
}
app.create(SchemaView.class, new NodeAttribute<>(SchemaView.schemaNode, schemaNode), new NodeAttribute<>(SchemaView.schemaProperties, properties), new NodeAttribute<>(SchemaView.name, viewName), new NodeAttribute<>(SchemaView.nonGraphProperties, StringUtils.join(nonGraphProperties, ",")));
schemaNode.removeProperty(new StringProperty(rawViewName));
}
}
}
}
final List<SchemaView> schemaViews = entity.getSchemaViews();
if (schemaViews != null) {
for (final SchemaView schemaView : schemaViews) {
final String nonGraphProperties = schemaView.getProperty(SchemaView.nonGraphProperties);
final String viewName = schemaView.getName();
// clear view before filling it again
Set<String> view = views.get(viewName);
if (view == null) {
view = new LinkedHashSet<>();
views.put(viewName, view);
}
final List<SchemaProperty> schemaProperties = schemaView.getProperty(SchemaView.schemaProperties);
for (final SchemaProperty property : schemaProperties) {
if (property.getProperty(SchemaProperty.isBuiltinProperty) && !property.getProperty(SchemaProperty.isDynamic)) {
view.add(SchemaHelper.cleanPropertyName(property.getPropertyName()));
} else {
view.add(SchemaHelper.cleanPropertyName(property.getPropertyName() + "Property"));
}
}
// add properties that are not part of the graph
if (StringUtils.isNotBlank(nonGraphProperties)) {
for (final String propertyName : nonGraphProperties.split("[, ]+")) {
if (SchemaHelper.isDynamic(entity.getClassName(), propertyName)) {
view.add(SchemaHelper.cleanPropertyName(propertyName + "Property"));
} else if (relPropertyNames.contains(propertyName)) {
view.add(SchemaHelper.cleanPropertyName(propertyName) + "Property");
} else if (basePropertyNames.contains(propertyName)) {
view.add(SchemaHelper.cleanPropertyName(propertyName));
} else {
logger.warn("Unknown property {} in non-graph properties, ignoring.", propertyName);
SchemaHelper.isDynamic(entity.getClassName(), propertyName);
}
}
}
final String order = schemaView.getProperty(SchemaView.sortOrder);
if (order != null) {
applySortOrder(view, order);
}
}
}
}
use of org.structr.core.entity.AbstractSchemaNode in project structr by structr.
the class StructrRelationshipTypeDefinition method resolveEndpointTypesForDatabaseSchemaCreation.
void resolveEndpointTypesForDatabaseSchemaCreation(final App app) throws FrameworkException {
// this method is called when the creation of type and relationship
// nodes is completed and all references can be resolved
final SchemaNode sourceSchemaNode = resolveSchemaNode(app, sourceType);
final SchemaNode targetSchemaNode = resolveSchemaNode(app, targetType);
if (sourceSchemaNode != null && targetSchemaNode != null) {
final AbstractSchemaNode thisSchemaRelationship = getSchemaNode();
if (thisSchemaRelationship != null) {
thisSchemaRelationship.setProperty(SchemaRelationshipNode.sourceNode, sourceSchemaNode);
thisSchemaRelationship.setProperty(SchemaRelationshipNode.targetNode, targetSchemaNode);
} else {
throw new IllegalStateException("Unable to resolve schema node endpoints for type " + getName());
}
} else {
throw new IllegalStateException("Unable to resolve schema node endpoints for type " + getName());
}
}
use of org.structr.core.entity.AbstractSchemaNode in project structr by structr.
the class Actions method callWithSecurityContext.
public static Object callWithSecurityContext(final String key, final SecurityContext securityContext, final Map<String, Object> parameters) throws FrameworkException, UnlicensedException {
final App app = StructrApp.getInstance(securityContext);
// we might want to introduce caching here at some point in the future..
// Cache can be invalidated when the schema is rebuilt for example..
final List<SchemaMethod> methods = app.nodeQuery(SchemaMethod.class).andName(key).getAsList();
if (methods.isEmpty()) {
if (!NOTIFICATION_LOGIN.equals(key) && !NOTIFICATION_LOGOUT.equals(key)) {
logger.warn("Tried to call method {} but no SchemaMethod entity was found.", key);
}
} else {
for (final SchemaMethod method : methods) {
// only call methods that are NOT part of a schema node
final AbstractSchemaNode entity = method.getProperty(SchemaMethod.schemaNode);
if (entity == null) {
final String source = method.getProperty(SchemaMethod.source);
if (source != null) {
return Actions.execute(securityContext, null, "${" + source + "}", parameters, method.getName());
} else {
logger.warn("Schema method {} has no source code, will NOT be executed.", key);
}
} else {
logger.warn("Schema method {} is attached to an entity, will NOT be executed.", key);
}
}
}
return null;
}
Aggregations