Search in sources :

Example 51 with ValueMapDecorator

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());
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) Test(org.junit.Test)

Example 52 with ValueMapDecorator

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);
    }
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) RepositoryException(javax.jcr.RepositoryException) QueryResult(javax.jcr.query.QueryResult) QuerySyntaxException(org.apache.sling.api.resource.QuerySyntaxException) RowIterator(javax.jcr.query.RowIterator) Iterator(java.util.Iterator) RowIterator(javax.jcr.query.RowIterator) Value(javax.jcr.Value) SlingException(org.apache.sling.api.SlingException) Row(javax.jcr.query.Row) NoSuchElementException(java.util.NoSuchElementException)

Example 53 with ValueMapDecorator

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;
}
Also used : HashMap(java.util.HashMap) ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator)

Example 54 with ValueMapDecorator

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)));
}
Also used : ValidationModel(org.apache.sling.validation.model.ValidationModel) HashMap(java.util.HashMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) DefaultValidationFailure(org.apache.sling.validation.spi.support.DefaultValidationFailure) DefaultValidationResult(org.apache.sling.validation.spi.support.DefaultValidationResult) ValidationResult(org.apache.sling.validation.ValidationResult) Test(org.junit.Test)

Example 55 with ValueMapDecorator

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;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ValueMapDecorator(org.apache.sling.api.wrappers.ValueMapDecorator) ResourceBundle(java.util.ResourceBundle) Nonnull(javax.annotation.Nonnull)

Aggregations

ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)63 Test (org.junit.Test)45 ValueMap (org.apache.sling.api.resource.ValueMap)44 HashMap (java.util.HashMap)36 Resource (org.apache.sling.api.resource.Resource)36 ValidationModel (org.apache.sling.validation.model.ValidationModel)9 ValidationResult (org.apache.sling.validation.ValidationResult)8 DefaultValidationResult (org.apache.sling.validation.spi.support.DefaultValidationResult)6 ArrayList (java.util.ArrayList)3 RepositoryException (javax.jcr.RepositoryException)3 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)3 ResourceModelWithRequiredField (org.apache.sling.models.testmodels.classes.ResourceModelWithRequiredField)3 MockResource (org.apache.sling.testing.resourceresolver.MockResource)3 DefaultValidationFailure (org.apache.sling.validation.spi.support.DefaultValidationFailure)3 Calendar (java.util.Calendar)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2