use of org.structr.core.converter.PropertyConverter 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.core.converter.PropertyConverter in project structr by structr.
the class GetOrNullFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
final SecurityContext securityContext = ctx.getSecurityContext();
try {
if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
return null;
}
GraphObject dataObject = null;
if (sources[0] instanceof GraphObject) {
dataObject = (GraphObject) sources[0];
}
if (sources[0] instanceof List) {
final List list = (List) sources[0];
if (list.size() == 1 && list.get(0) instanceof GraphObject) {
dataObject = (GraphObject) list.get(0);
}
}
if (dataObject != null) {
final String keyName = sources[1].toString();
final PropertyKey key = StructrApp.key(dataObject.getClass(), keyName);
if (key != null) {
final PropertyConverter inputConverter = key.inputConverter(securityContext);
Object value = dataObject.getProperty(key);
if (inputConverter != null) {
return inputConverter.revert(value);
}
return dataObject.getProperty(key);
}
return "";
}
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
return null;
}
use of org.structr.core.converter.PropertyConverter 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();
}
}
use of org.structr.core.converter.PropertyConverter in project structr by structr.
the class CreateFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (sources != null && sources.length > 0) {
final SecurityContext securityContext = ctx.getSecurityContext();
final ConfigurationProvider config = StructrApp.getConfiguration();
PropertyMap propertyMap;
Class type = null;
if (sources.length >= 1 && sources[0] != null) {
type = config.getNodeEntityClass(sources[0].toString());
}
if (type == null) {
throw new FrameworkException(422, "Unknown type '" + sources[0].toString() + "' in create() method!");
}
// extension for native javascript objects
if (sources.length == 2 && sources[1] instanceof Map) {
propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
} else if (sources.length == 2 && sources[1] instanceof GraphObjectMap) {
propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, ((GraphObjectMap) sources[1]).toMap());
} else {
propertyMap = new PropertyMap();
final int parameter_count = sources.length;
if (parameter_count % 2 == 0) {
throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + (ctx.isJavaScriptContext() ? ERROR_MESSAGE_CREATE_JS : ERROR_MESSAGE_CREATE));
}
for (int c = 1; c < parameter_count; c += 2) {
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);
}
propertyMap.put(key, value);
}
}
}
return StructrApp.getInstance(securityContext).create(type, propertyMap);
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.core.converter.PropertyConverter in project structr by structr.
the class PropertyTest method testDatabaseConverterOnCollectionProperty.
/**
* Test of databaseConverter method, of class CollectionProperty.
*/
@Test
public void testDatabaseConverterOnCollectionProperty() {
Property<List<TestOne>> instance = TestSix.manyToManyTestOnes;
PropertyConverter expResult = null;
PropertyConverter result = instance.databaseConverter(securityContext, null);
assertEquals(expResult, result);
}
Aggregations