Search in sources :

Example 26 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class RatingsServiceImpl method setRating.

/**
     * @see org.apache.sling.sample.slingshot.ratings.RatingsService#setRating(org.apache.sling.api.resource.Resource, java.lang.String, int)
     */
@Override
public void setRating(final Resource resource, final String userId, final double rating) throws PersistenceException {
    final String ratingsPath = getRatingsResourcePath(resource);
    final Map<String, Object> props = new HashMap<String, Object>();
    props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, RESOURCETYPE_RATINGS);
    final Resource ratingsResource = ResourceUtil.getOrCreateResource(resource.getResourceResolver(), ratingsPath, props, null, true);
    final Resource ratingRsrc = resource.getResourceResolver().getResource(ratingsResource, userId);
    if (ratingRsrc == null) {
        props.clear();
        props.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, RatingsUtil.RESOURCETYPE_RATING);
        props.put(RatingsUtil.PROPERTY_RATING, String.valueOf(rating));
        resource.getResourceResolver().create(ratingsResource, userId, props);
    } else {
        final ModifiableValueMap mvm = ratingRsrc.adaptTo(ModifiableValueMap.class);
        mvm.put(RatingsUtil.PROPERTY_RATING, String.valueOf(rating));
    }
    resource.getResourceResolver().commit();
}
Also used : HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 27 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class StreamedChunk method processChunks.

/**
     * process all chunks formed so far to create the final body.
     * @param contentResource
     * @param changes
     * @throws PersistenceException
     */
private void processChunks(Resource contentResource, List<Modification> changes) throws PersistenceException {
    if (completed) {
        // have to commit before processing chunks.
        contentResource.getResourceResolver().commit();
        ModifiableValueMap vm = contentResource.adaptTo(ModifiableValueMap.class);
        vm.put("jcr:data", getChunksInputStream(contentResource));
        // might have to commit before removing chunk data, depending on if the InputStream still works.
        removeChunkData(contentResource, vm);
    }
}
Also used : ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 28 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class ValueMapTest method testModifiableValueMap.

@Test
public void testModifiableValueMap() throws IOException {
    Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
    ValueMap map = resource1.adaptTo(ModifiableValueMap.class);
    assertTrue(map instanceof ValueMap && map instanceof ModifiableValueMap);
    assertEquals("value1", map.get("prop1"));
    map.put("prop1", "value2");
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 29 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class ValueMapTest method testMap.

@SuppressWarnings("unchecked")
@Test
public void testMap() throws IOException {
    Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
    Map<String, Object> map = resource1.adaptTo(Map.class);
    assertTrue(map instanceof ValueMap && !(map instanceof ModifiableValueMap));
    assertEquals("value1", map.get("prop1"));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 30 with ModifiableValueMap

use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.

the class ValueMapTest method testValueMap.

@Test
public void testValueMap() throws IOException {
    Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
    ValueMap map = resource1.adaptTo(ValueMap.class);
    assertTrue(map instanceof ValueMap && !(map instanceof ModifiableValueMap));
    assertEquals("value1", map.get("prop1"));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Aggregations

ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)111 Resource (org.apache.sling.api.resource.Resource)74 PersistenceException (org.apache.sling.api.resource.PersistenceException)32 Test (org.junit.Test)28 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)26 HashMap (java.util.HashMap)22 ValueMap (org.apache.sling.api.resource.ValueMap)22 Calendar (java.util.Calendar)13 LoginException (org.apache.sling.api.resource.LoginException)9 ChildResource (org.apache.sling.validation.model.ChildResource)9 Date (java.util.Date)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 ValidationModel (org.apache.sling.validation.model.ValidationModel)7 InputStream (java.io.InputStream)6 Node (javax.jcr.Node)6 RepositoryException (javax.jcr.RepositoryException)6