use of org.structr.core.converter.PropertyConverter in project structr by structr.
the class GetOrCreateFunction 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 PropertyMap properties = new PropertyMap();
// 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) {
logger.warn("Error in get_or_create(): type \"{}\" not found.", typeString);
return ERROR_MESSAGE_TYPE_NOT_FOUND + typeString;
}
}
// exit gracefully instead of crashing..
if (type == null) {
logger.warn("Error in get_or_create(): no type specified. Parameters: {}", getParametersAsString(sources));
return ERROR_MESSAGE_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) {
properties.putAll(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
} 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_GET_OR_CREATE);
}
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);
}
properties.put(key, value);
}
}
}
final GraphObject obj = app.nodeQuery(type).disableSorting().pageSize(1).and(properties).getFirst();
if (obj != null) {
// return existing object
return obj;
}
// create new object
return app.create(type, properties);
} 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 PrivilegedFindFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, Object[] sources) throws FrameworkException {
if (sources != null) {
final SecurityContext securityContext = SecurityContext.getSuperUserInstance();
final ConfigurationProvider config = StructrApp.getConfiguration();
final Query query = StructrApp.getInstance(securityContext).nodeQuery().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.getNodeEntityClass(typeString);
if (type != null) {
query.andTypes(type);
} else {
logger.warn("Error in find_privileged(): type \"{}\" not found.", typeString);
return "Error in find_privileged(): type " + typeString + " not found.";
}
}
// exit gracefully instead of crashing..
if (type == null) {
logger.warn("Error in find_privileged(): no type specified. Parameters: {}", getParametersAsString(sources));
return "Error in find_privileged(): 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());
final int resultCount = query.getResult().size();
switch(resultCount) {
case 1:
return query.getFirst();
case 0:
return null;
default:
throw new FrameworkException(400, "Multiple Objects found for id! [" + sources[1].toString() + "]");
}
} 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_PRIVILEGEDFIND);
}
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);
}
query.and(key, value);
}
}
}
return query.getAsList();
}
return "";
}
use of org.structr.core.converter.PropertyConverter in project structr by structr.
the class PropertyTest method testInputConverterOnCollectionProperty.
/**
* Test of inputConverter method, of class CollectionProperty.
*/
@Test
public void testInputConverterOnCollectionProperty() {
Property<List<TestOne>> instance = TestSix.manyToManyTestOnes;
PropertyConverter result = instance.inputConverter(securityContext);
assertTrue(result != null);
}
use of org.structr.core.converter.PropertyConverter in project structr by structr.
the class AbstractRelationship method getComparableProperty.
@Override
public final <T> Comparable getComparableProperty(final PropertyKey<T> key) {
if (key != null) {
// get "raw" property without converter
final T propertyValue = getProperty(key, false, null);
// check property converter
PropertyConverter converter = key.databaseConverter(securityContext, this);
if (converter != null) {
try {
return converter.convertForSorting(propertyValue);
} catch (FrameworkException fex) {
logger.warn("Unable to convert property {} of type {}: {}", new Object[] { key.dbName(), getClass().getSimpleName(), fex.getMessage() });
}
}
// conversion failed, may the property value itself is comparable
if (propertyValue instanceof Comparable) {
return (Comparable) propertyValue;
}
// last try: convertFromInput to String to make comparable
if (propertyValue != null) {
return propertyValue.toString();
}
}
return null;
}
use of org.structr.core.converter.PropertyConverter in project structr by structr.
the class SourcePattern method extractAndSetValue.
private void extractAndSetValue(final NodeInterface obj, final Document doc, final String selector, final String mappedType, final String mappedAttribute, final String mappedAttributeFunction, final SourcePage subPage) throws FrameworkException {
// If the sub pattern has a mapped attribute, set the extracted value
if (StringUtils.isNotEmpty(mappedAttribute)) {
// Extract the value for this sub pattern's selector
final String ex = doc.select(selector).text();
final PropertyKey key = StructrApp.key(type(mappedType), mappedAttribute);
if (StringUtils.isNotBlank(ex) && key != null) {
Object convertedValue = ex;
if (StringUtils.isNotBlank(mappedAttributeFunction)) {
// input transformation requested
ActionContext ctx = new ActionContext(securityContext);
ctx.setConstant("input", convertedValue);
convertedValue = Scripting.evaluate(ctx, null, "${" + mappedAttributeFunction + "}", " virtual property " + mappedAttribute);
} else {
// if no custom transformation is given, try input converter
final PropertyConverter inputConverter = key.inputConverter(securityContext);
if (inputConverter != null) {
convertedValue = inputConverter.convert(convertedValue);
}
}
obj.setProperty(key, convertedValue);
}
// If the sub pattern has no mapped attribute but a sub page defined, query the patterns of the sub page
} else if (subPage != null) {
final String pageUrl = subPage.getProperty(SourcePage.url);
final URI uri;
try {
uri = new URI(pageUrl);
} catch (URISyntaxException ex) {
throw new FrameworkException(422, "Unable to parse sub page url: " + pageUrl);
}
// This is the URL of the linked page derived from the enclosing selector
final String subUrl = uri.getScheme() + "://" + uri.getAuthority() + doc.select(selector).attr("href");
// Extract the content of the linked page
final String subContent = getContent(subUrl);
// Parse the content into a document
final Document subDoc = Jsoup.parse(subContent);
final List<SourcePattern> subPagePatterns = subPage.getProperty(SourcePage.patterns);
// Loop through all patterns of the sub page
for (final SourcePattern subPagePattern : subPagePatterns) {
final Map<String, Object> params = new HashMap<>();
params.put("document", subDoc);
params.put("object", obj);
subPagePattern.extract(params);
// final String subPagePatternSelector = subPagePattern.getProperty(SourcePattern.selectorProperty);
//
//
// // Extract
// final String subEx = subDoc.select(subPagePatternSelector).text();
// final String subPagePatternType = subPagePattern.getProperty(SourcePattern.mappedTypeProperty);
//
// if (subPagePatternType != null) {
//
//
// final Elements subParts = subDoc.select(subPagePatternSelector);
//
// final Long j = 1L;
//
// for (final Element subPart : subParts) {
//
// final NodeInterface subObj = create(subPagePatternType);
//
// final List<SourcePattern> subPagePatternPatterns = subPagePattern.getProperty(SourcePattern.subPatternsProperty);
//
// for (final SourcePattern subPageSubPattern : subPagePatternPatterns) {
//
//
// final String subPagePatternSelector = subPageSubPattern.getProperty(SourcePattern.selectorProperty);
//
//
//
// final String subPageSubPatternSelector = subPagePatternSelector + ":nth-child(" + j + ") > " + subPagePatternSelector;
//
// extractAndSetValue(subObj, subDoc, subSelector, mappedType, subPatternMappedAttribute);
//
//
// final String subSubEx = subDoc.select(subPageSubPatternSelector).text();
//
// if (subSubEx != null && subSubEx != = '' && subPageSubPattern.mappedAttribute != null) {
//
// final PropertyKey key = StructrApp.key(type(mappedType), subPatternMappedAttribute);
// if (key != null) {
//
// subObj.setProperty(key, subSubEx);
// }
//
// }
//
// final String subPagePatternMappedAttribute = subPagePattern.getProperty(SourcePattern.mappedAttributeProperty);
//
// final PropertyKey key = StructrApp.key(type(mappedType), subPagePatternMappedAttribute);
// if (key != null) {
//
// obj.setProperty(key, subSubEx);
// }
//
// }
//
// } else {
//
// if (subEx != null && subEx != = '' && subPagePattern.mappedAttribute != null) {
// obj[subPagePattern.mappedAttribute] = subEx;
// }
}
}
}
Aggregations