use of org.structr.core.property.RelationProperty in project structr by structr.
the class SchemaResource method getSchemaOverviewResult.
// ----- public static methods -----
public static Result getSchemaOverviewResult() throws FrameworkException {
final List<GraphObjectMap> resultList = new LinkedList<>();
final ConfigurationProvider config = StructrApp.getConfiguration();
// extract types from ModuleService
final Set<String> nodeEntityKeys = config.getNodeEntities().keySet();
final Set<String> relEntityKeys = config.getRelationshipEntities().keySet();
Set<String> entityKeys = new HashSet<>();
entityKeys.addAll(nodeEntityKeys);
entityKeys.addAll(relEntityKeys);
for (String rawType : entityKeys) {
// create & add schema information
Class type = SchemaHelper.getEntityClassForRawType(rawType);
GraphObjectMap schema = new GraphObjectMap();
resultList.add(schema);
if (type != null) {
String url = "/".concat(rawType);
final boolean isRel = AbstractRelationship.class.isAssignableFrom(type);
schema.setProperty(urlProperty, url);
schema.setProperty(typeProperty, type.getSimpleName());
schema.setProperty(nameProperty, type.getSimpleName());
schema.setProperty(classNameProperty, type.getName());
schema.setProperty(extendsClassNameProperty, type.getSuperclass().getName());
schema.setProperty(isRelProperty, isRel);
schema.setProperty(flagsProperty, SecurityContext.getResourceFlags(rawType));
if (!isRel) {
final List<GraphObjectMap> relatedTo = new LinkedList<>();
final List<GraphObjectMap> relatedFrom = new LinkedList<>();
for (final PropertyKey key : config.getPropertySet(type, PropertyView.All)) {
if (key instanceof RelationProperty) {
final RelationProperty relationProperty = (RelationProperty) key;
final Relation relation = relationProperty.getRelation();
if (!relation.isHidden()) {
switch(relation.getDirectionForType(type)) {
case OUTGOING:
relatedTo.add(relationPropertyToMap(config, relationProperty));
break;
case INCOMING:
relatedFrom.add(relationPropertyToMap(config, relationProperty));
break;
case BOTH:
relatedTo.add(relationPropertyToMap(config, relationProperty));
relatedFrom.add(relationPropertyToMap(config, relationProperty));
break;
}
}
}
}
if (!relatedTo.isEmpty()) {
schema.setProperty(relatedToProperty, relatedTo);
}
if (!relatedFrom.isEmpty()) {
schema.setProperty(relatedFromProperty, relatedFrom);
}
}
}
}
return new Result(resultList, resultList.size(), false, false);
}
use of org.structr.core.property.RelationProperty in project structr by structr.
the class StaticRelationshipResource method doPost.
@Override
public RestMethodResult doPost(final Map<String, Object> propertySet) throws FrameworkException {
final GraphObject sourceNode = typedIdResource.getEntity();
RestMethodResult result = null;
if (sourceNode != null && propertyKey != null && propertyKey instanceof RelationProperty) {
final RelationProperty relationProperty = (RelationProperty) propertyKey;
final Class sourceNodeType = sourceNode.getClass();
NodeInterface newNode = null;
if (propertyKey.isReadOnly()) {
logger.info("Read-only property on {}: {}", new Object[] { sourceNodeType, typeResource.getRawType() });
return null;
}
// fetch notion
final Notion notion = relationProperty.getNotion();
final PropertyKey primaryPropertyKey = notion.getPrimaryPropertyKey();
// apply notion if the property set contains the ID property as the only element
if (primaryPropertyKey != null && propertySet.containsKey(primaryPropertyKey.jsonName()) && propertySet.size() == 1) {
// FIXME: what happens here?
} else {
// the notion can not deserialize objects with a single key, or the POSTed propertySet did not contain a key to deserialize,
// so we create a new node from the POSTed properties and link the source node to it. (this is the "old" implementation)
newNode = typeResource.createNode(propertySet);
if (newNode != null) {
relationProperty.addSingleElement(securityContext, sourceNode, newNode);
}
}
if (newNode != null) {
result = new RestMethodResult(HttpServletResponse.SC_CREATED);
result.addHeader("Location", buildLocationHeader(newNode));
return result;
}
} else {
final Class entityType = typedIdResource.getTypeResource().getEntityClass();
final String methodName = typeResource.getRawType();
try {
final String source = SchemaMethodResource.findMethodSource(entityType, methodName);
result = SchemaMethodResource.invoke(securityContext, typedIdResource.getEntity(), source, propertySet, methodName);
} catch (IllegalPathException ex) {
// try direct invocation of the schema method on the node type
try {
result = SchemaMethodResource.wrapInResult(typedIdResource.getEntity().invokeMethod(methodName, propertySet, true));
} catch (Throwable t) {
logger.warn("Unable to execute {}.{}: {}", entityType.getSimpleName(), methodName, t.getMessage());
}
}
}
if (result == null) {
throw new IllegalPathException("Illegal path");
} else {
return result;
}
}
use of org.structr.core.property.RelationProperty in project structr by structr.
the class StaticRelationshipResource method doPut.
@Override
public RestMethodResult doPut(final Map<String, Object> propertySet) throws FrameworkException {
final List<? extends GraphObject> results = typedIdResource.doGet(null, false, NodeFactory.DEFAULT_PAGE_SIZE, NodeFactory.DEFAULT_PAGE).getResults();
final App app = StructrApp.getInstance(securityContext);
if (results != null) {
// fetch static relationship definition
if (propertyKey != null && propertyKey instanceof RelationProperty) {
final GraphObject sourceEntity = typedIdResource.getEntity();
if (sourceEntity != null) {
if (propertyKey.isReadOnly()) {
logger.info("Read-only property on {}: {}", new Object[] { sourceEntity.getClass(), typeResource.getRawType() });
return new RestMethodResult(HttpServletResponse.SC_FORBIDDEN);
}
final List<GraphObject> nodes = new LinkedList<>();
// Now add new relationships for any new id: This should be the rest of the property set
for (final Object obj : propertySet.values()) {
nodes.add(app.getNodeById(obj.toString()));
}
// set property on source node
sourceEntity.setProperty(propertyKey, nodes);
}
}
}
return new RestMethodResult(HttpServletResponse.SC_OK);
}
use of org.structr.core.property.RelationProperty in project structr by structr.
the class RelatedNodePropertyMapper method getRelatedNode.
// ~--- get methods ----------------------------------------------------
private NodeInterface getRelatedNode(boolean add) {
T relatedNode = null;
if ((currentObject != null) && (currentObject instanceof NodeInterface)) {
NodeInterface localNode = (NodeInterface) currentObject;
relatedNode = localNode.getProperty(sourceKey);
if (relatedNode == null && add && sourceKey instanceof RelationProperty) {
final RelationProperty relationProperty = (RelationProperty) sourceKey;
final App app = StructrApp.getInstance();
final Class<T> relatedType = relationProperty.getTargetType();
if (relatedType != null) {
try {
relatedNode = app.create(relatedType);
relationProperty.addSingleElement(securityContext, localNode, relatedNode);
} catch (FrameworkException fex) {
logger.warn("", fex);
}
} else {
logger.error("Related type was null for {}", currentObject);
}
}
}
return relatedNode;
}
use of org.structr.core.property.RelationProperty in project structr by structr.
the class SchemaHelper method getPropertyInfo.
public static Map<String, Object> getPropertyInfo(final SecurityContext securityContext, final PropertyKey property) {
final Map<String, Object> map = new LinkedHashMap();
map.put("dbName", property.dbName());
map.put("jsonName", property.jsonName());
map.put("className", property.getClass().getName());
final Class declaringClass = property.getDeclaringClass();
if (declaringClass != null) {
map.put("declaringClass", declaringClass.getSimpleName());
}
map.put("defaultValue", property.defaultValue());
if (property instanceof StringProperty) {
map.put("contentType", ((StringProperty) property).contentType());
}
map.put("format", property.format());
map.put("readOnly", property.isReadOnly());
map.put("system", property.isSystemInternal());
map.put("indexed", property.isIndexed());
map.put("indexedWhenEmpty", property.isIndexedWhenEmpty());
map.put("compound", property.isCompound());
map.put("unique", property.isUnique());
map.put("notNull", property.isNotNull());
map.put("dynamic", property.isDynamic());
map.put("hint", property.hint());
map.put("category", property.category());
final Class<? extends GraphObject> relatedType = property.relatedType();
if (relatedType != null) {
map.put("relatedType", relatedType.getName());
map.put("type", relatedType.getSimpleName());
map.put("uiType", relatedType.getSimpleName() + (property.isCollection() ? "[]" : ""));
} else {
map.put("type", property.typeName());
map.put("uiType", property.typeName() + (property.isCollection() ? "[]" : ""));
}
map.put("isCollection", property.isCollection());
final PropertyConverter databaseConverter = property.databaseConverter(securityContext, null);
final PropertyConverter inputConverter = property.inputConverter(securityContext);
if (databaseConverter != null) {
map.put("databaseConverter", databaseConverter.getClass().getName());
}
if (inputConverter != null) {
map.put("inputConverter", inputConverter.getClass().getName());
}
// if (declaringClass != null && ("org.structr.dynamic".equals(declaringClass.getPackage().getName()))) {
if (declaringClass != null && property instanceof RelationProperty) {
Relation relation = ((RelationProperty) property).getRelation();
if (relation != null) {
map.put("relationshipType", relation.name());
}
}
return map;
}
Aggregations