use of org.structr.schema.ConfigurationProvider 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.schema.ConfigurationProvider in project structr by structr.
the class FindRelationshipFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (sources == null) {
throw new IllegalArgumentException();
}
final SecurityContext securityContext = ctx.getSecurityContext();
final ConfigurationProvider config = StructrApp.getConfiguration();
final Query query = StructrApp.getInstance(securityContext).relationshipQuery().sort(GraphObject.createdDate).order(false);
// the type to query for
Class type = null;
if (sources.length >= 1 && sources[0] != null) {
final String typeString = sources[0].toString();
type = config.getRelationshipEntityClass(typeString);
if (type != null) {
query.andTypes(type);
} else {
logger.warn("Error in find_relationship(): type \"{}\" not found.", typeString);
return ERROR_MESSAGE_FIND_RELATIONSHIP_TYPE_NOT_FOUND + typeString;
}
}
// exit gracefully instead of crashing..
if (type == null) {
logger.warn("Error in find_relationship(): no type specified. Parameters: {}", getParametersAsString(sources));
return ERROR_MESSAGE_FIND_RELATIONSHIP_NO_TYPE_SPECIFIED;
}
// experimental: disable result count, prevents instantiation
// of large collections just for counting all the objects..
securityContext.ignoreResultCount(true);
// extension for native javascript objects
if (sources.length == 2 && sources[1] instanceof Map) {
query.and(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
} else if (sources.length == 2) {
if (sources[1] == null) {
throw new IllegalArgumentException();
}
// special case: second parameter is a UUID
final PropertyKey key = StructrApp.key(type, "id");
query.and(key, sources[1].toString());
return query.getFirst();
} else {
final int parameter_count = sources.length;
if (parameter_count % 2 == 0) {
throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_FIND_RELATIONSHIP);
}
for (int c = 1; c < parameter_count; c += 2) {
if (sources[c] == null) {
throw new IllegalArgumentException();
}
final PropertyKey key = StructrApp.key(type, sources[c].toString());
if (key != null) {
final PropertyConverter inputConverter = key.inputConverter(securityContext);
Object value = sources[c + 1];
if (inputConverter != null) {
value = inputConverter.convert(value);
}
query.and(key, value);
}
}
}
return query.getAsList();
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class EnumInfoFunction method apply.
@Override
public Object apply(ActionContext ctx, Object caller, Object[] sources) throws FrameworkException {
try {
if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final String typeName = sources[0].toString();
final String enumPropertyName = sources[1].toString();
final boolean rawList = (sources.length == 3) ? Boolean.parseBoolean(sources[2].toString()) : false;
final Class type = SchemaHelper.getEntityClassForRawType(typeName);
if (type != null) {
final PropertyKey key = StructrApp.key(type, enumPropertyName);
if (key != null) {
if (key instanceof EnumProperty) {
final String formatString = SchemaHelper.getPropertyInfo(ctx.getSecurityContext(), key).get("format").toString();
final List<String> valueList = Arrays.asList(formatString.replace(" ", "").split(","));
if (rawList) {
return valueList;
} else {
final ArrayList<GraphObjectMap> resultList = new ArrayList();
for (final String value : valueList) {
final GraphObjectMap valueMap = new GraphObjectMap();
resultList.add(valueMap);
valueMap.put(new StringProperty("value"), value);
}
return resultList;
}
} else {
logger.warn("Error: Not an Enum property \"{}.{}\"", typeName, enumPropertyName);
return "Not an Enum property " + typeName + "." + enumPropertyName;
}
} else {
logger.warn("Error: Unknown property \"{}.{}\"", typeName, enumPropertyName);
return "Unknown property " + typeName + "." + enumPropertyName;
}
} else {
logger.warn("Error: Unknown type \"{}\"", typeName);
return "Unknown type " + typeName;
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class ExtractFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (sources == null) {
throw new IllegalArgumentException();
}
if (sources.length == 1) {
// no property key given, maybe we should extract a list of lists?
if (sources[0] instanceof Collection) {
final List extraction = new LinkedList();
for (final Object obj : (Collection) sources[0]) {
if (obj instanceof Collection) {
extraction.addAll((Collection) obj);
}
}
return extraction;
}
}
if (sources.length == 2) {
if (sources[0] == null) {
return null;
}
if (sources[0] instanceof Collection && sources[1] instanceof String) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final List extraction = new LinkedList();
final String keyName = (String) sources[1];
for (final Object obj : (Collection) sources[0]) {
if (obj instanceof GraphObject) {
final PropertyKey key = StructrApp.key(obj.getClass(), keyName);
final Object value = ((GraphObject) obj).getProperty(key);
if (value != null) {
extraction.add(value);
}
}
}
return extraction;
}
}
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
return null;
}
use of org.structr.schema.ConfigurationProvider in project structr by structr.
the class FindFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (sources == null) {
throw new IllegalArgumentException();
}
final SecurityContext securityContext = ctx.getSecurityContext();
final ConfigurationProvider config = StructrApp.getConfiguration();
final App app = StructrApp.getInstance(securityContext);
final Query query = app.nodeQuery().sort(GraphObject.createdDate).order(false);
// paging applied by surrounding slice() function
applyRange(securityContext, query);
// the type to query for
Class type = null;
if (sources.length >= 1 && sources[0] != null) {
final String typeString = sources[0].toString();
type = config.getNodeEntityClass(typeString);
if (type != null) {
query.andTypes(type);
} else {
logger.warn("Error in find(): type \"{}\" not found.", typeString);
return ERROR_MESSAGE_FIND_TYPE_NOT_FOUND + typeString;
}
}
// exit gracefully instead of crashing..
if (type == null) {
logger.warn("Error in find(): no type specified. Parameters: {}", getParametersAsString(sources));
return ERROR_MESSAGE_FIND_NO_TYPE_SPECIFIED;
}
// experimental: disable result count, prevents instantiation
// of large collections just for counting all the objects..
securityContext.ignoreResultCount(true);
// extension for native javascript objects
if (sources.length == 2 && sources[1] instanceof Map) {
query.and(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
} else if (sources.length == 2) {
if (sources[1] == null) {
throw new IllegalArgumentException();
}
// special case: second parameter is a UUID
final PropertyKey key = StructrApp.key(type, "id");
query.and(key, sources[1].toString());
return query.getFirst();
} else {
final int parameter_count = sources.length;
if (parameter_count % 2 == 0) {
throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_FIND);
}
for (int c = 1; c < parameter_count; c += 2) {
if (sources[c] == null) {
throw new IllegalArgumentException();
}
final PropertyKey key = StructrApp.key(type, sources[c].toString());
if (key != null) {
final PropertyConverter inputConverter = key.inputConverter(securityContext);
Object value = sources[c + 1];
if (inputConverter != null) {
value = inputConverter.convert(value);
}
query.and(key, value);
}
}
}
return query.getAsList();
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
} finally {
resetRange();
}
}
Aggregations