use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class AdapterFactoryTest method testCreatedNestedModel.
@Test
public void testCreatedNestedModel() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("required", "required");
ValueMap vm = new ValueMapDecorator(map);
when(resource.adaptTo(ValueMap.class)).thenReturn(vm);
NestedModel model = factory.createModel(resource, NestedModel.class);
Assert.assertNotNull(model);
Assert.assertEquals("required", model.getNestedModel().getRequired());
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class BasicQueryLanguageProvider method queryResources.
@Override
public Iterator<ValueMap> queryResources(final ResolveContext<JcrProviderState> ctx, final String query, final String language) {
final String queryLanguage = ArrayUtils.contains(getSupportedLanguages(ctx), language) ? language : DEFAULT_QUERY_LANGUAGE;
try {
final QueryResult result = JcrResourceUtil.query(ctx.getProviderState().getSession(), query, queryLanguage);
final String[] colNames = result.getColumnNames();
final RowIterator rows = result.getRows();
return new Iterator<ValueMap>() {
private ValueMap next;
{
next = seek();
}
@Override
public boolean hasNext() {
return next != null;
}
;
@Override
public ValueMap next() {
if (next == null) {
throw new NoSuchElementException();
}
final ValueMap result = next;
next = seek();
return result;
}
private ValueMap seek() {
ValueMap result = null;
while (result == null && rows.hasNext()) {
try {
final Row jcrRow = rows.nextRow();
final String resourcePath = jcrRow.getPath();
if (resourcePath != null && providerContext.getExcludedPaths().matches(resourcePath) == null) {
final Map<String, Object> row = new HashMap<String, Object>();
boolean didPath = false;
boolean didScore = false;
final Value[] values = jcrRow.getValues();
for (int i = 0; i < values.length; i++) {
Value v = values[i];
if (v != null) {
String colName = colNames[i];
row.put(colName, JcrResourceUtil.toJavaObject(values[i]));
if (colName.equals(QUERY_COLUMN_PATH)) {
didPath = true;
row.put(colName, JcrResourceUtil.toJavaObject(values[i]).toString());
}
if (colName.equals(QUERY_COLUMN_SCORE)) {
didScore = true;
}
}
}
if (!didPath) {
row.put(QUERY_COLUMN_PATH, jcrRow.getPath());
}
if (!didScore) {
row.put(QUERY_COLUMN_SCORE, jcrRow.getScore());
}
result = new ValueMapDecorator(row);
}
} catch (final RepositoryException re) {
logger.error("queryResources$next: Problem accessing row values", re);
}
}
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
};
} catch (final javax.jcr.query.InvalidQueryException iqe) {
throw new QuerySyntaxException(iqe.getMessage(), query, language, iqe);
} catch (final RepositoryException re) {
throw new SlingException(re.getMessage(), re);
}
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class TenantImpl method loadProperties.
void loadProperties(Resource resource) {
ValueMap jcrVM = ResourceUtil.getValueMap(resource);
Map<String, Object> localMap = new HashMap<String, Object>();
// copy all items to local map
localMap.putAll(jcrVM);
// decoarate it as value map
ValueMapDecorator localVM = new ValueMapDecorator(localMap);
this.vm = localVM;
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class ValidationServiceImplTest method testValueMapWithWrongDataType.
@Test()
public void testValueMapWithWrongDataType() throws Exception {
propertyBuilder.validator(DATE_VALIDATOR_ID, 10);
modelBuilder.resourceProperty(propertyBuilder.build("field1"));
ValidationModel vm = modelBuilder.build("sling/validation/test", "some source");
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("field1", "1");
ValidationResult vr = validationService.validate(new ValueMapDecorator(hashMap), vm);
Assert.assertThat(vr.getFailures(), Matchers.<ValidationFailure>contains(new DefaultValidationFailure("field1", 10, defaultResourceBundle, ValidationServiceImpl.I18N_KEY_WRONG_PROPERTY_TYPE, Date.class)));
}
use of org.apache.sling.api.wrappers.ValueMapDecorator in project sling by apache.
the class ValidationServiceImpl method validate.
@Nonnull
protected ValidationResult validate(@Nonnull Resource resource, @Nonnull ValidationModel model, @Nonnull String relativePath) {
if (resource == null || model == null || relativePath == null) {
throw new IllegalArgumentException("ValidationService.validate - cannot accept null parameters");
}
ResourceBundle defaultResourceBundle = getDefaultResourceBundle();
CompositeValidationResult result = new CompositeValidationResult();
ValueMap valueMap = resource.adaptTo(ValueMap.class);
if (valueMap == null) {
// SyntheticResources can not adapt to a ValueMap, therefore just use the empty map here
valueMap = new ValueMapDecorator(Collections.emptyMap());
}
// validate direct properties of the resource
validateValueMap(valueMap, resource, relativePath, model.getResourceProperties(), result, defaultResourceBundle);
// validate child resources, if any
validateChildren(resource, relativePath, model.getChildren(), result, defaultResourceBundle);
// optionally put result to cache
if (configuration.cacheValidationResultsOnResources()) {
ResourceToValidationResultAdapterFactory.putValidationResultToCache(result, resource);
}
return result;
}
Aggregations