Search in sources :

Example 31 with ModifiableValueMap

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

the class LoaderContentHandler method createResource.

private Resource createResource(Resource parentResource, String childName, Map<String, Object> content) throws PersistenceException {
    // collect all properties first
    boolean hasJcrData = false;
    Map<String, Object> props = new HashMap<String, Object>();
    for (Map.Entry<String, Object> entry : content.entrySet()) {
        final String name = entry.getKey();
        if (StringUtils.equals(name, JCR_DATA_PLACEHOLDER)) {
            hasJcrData = true;
        } else {
            props.put(name, entry.getValue());
        }
    }
    // create resource
    Resource resource = resourceResolver.create(parentResource, childName, props);
    if (hasJcrData) {
        ModifiableValueMap valueMap = resource.adaptTo(ModifiableValueMap.class);
        // we cannot import binary data here - but to avoid complaints by JCR we create it with empty binary data
        valueMap.put(JcrConstants.JCR_DATA, new ByteArrayInputStream(new byte[0]));
    }
    return resource;
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Map(java.util.Map) HashMap(java.util.HashMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 32 with ModifiableValueMap

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

the class SlingCrudResourceResolverTest method testStringToCalendarConversion.

@Test
public void testStringToCalendarConversion() throws IOException {
    Resource resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
    ModifiableValueMap modProps = resource1.adaptTo(ModifiableValueMap.class);
    modProps.put("dateISO8601String", ISO8601.format(CALENDAR_VALUE));
    resourceResolver.commit();
    resource1 = resourceResolver.getResource(testRoot.getPath() + "/node1");
    ValueMap props = ResourceUtil.getValueMap(resource1);
    assertEquals(CALENDAR_VALUE.getTime(), props.get("calendarProp", Calendar.class).getTime());
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Test(org.junit.Test)

Example 33 with ModifiableValueMap

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

the class PlumberImpl method writeStatus.

/**
     * writes the status of the pipe
     * @param pipe
     * @param status
     */
protected void writeStatus(Pipe pipe, String status) throws RepositoryException {
    if (StringUtils.isNotBlank(status)) {
        ModifiableValueMap vm = pipe.getResource().adaptTo(ModifiableValueMap.class);
        vm.put(PN_STATUS, status);
        Calendar cal = new GregorianCalendar();
        cal.setTime(new Date());
        vm.put(PN_STATUS_MODIFIED, cal);
    }
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date)

Example 34 with ModifiableValueMap

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

the class WritePipe method copyProperties.

/**
     * Write properties from the configuration to the target resource,
     * instantiating both property names & values
     *
     * @param conf configured resource that holds all properties to write (and children)
     * @param target target resource on which configured values will be written
     * @throws RepositoryException issues occuring when traversing nodes
     */
private void copyProperties(Resource conf, Resource target) throws RepositoryException {
    ValueMap writeMap = conf.adaptTo(ValueMap.class);
    ModifiableValueMap properties = target.adaptTo(ModifiableValueMap.class);
    //writing current node
    if (properties != null && writeMap != null) {
        for (Map.Entry<String, Object> entry : writeMap.entrySet()) {
            if (!IGNORED_PROPERTIES.contains(entry.getKey())) {
                String key = parent != null ? bindings.instantiateExpression(entry.getKey()) : entry.getKey();
                Object value = computeValue(target, key, entry.getValue());
                if (value == null) {
                    //null value are not handled by modifiable value maps,
                    //removing the property if it exists
                    addPropertyToRemove(target.getChild(key));
                } else {
                    logger.info("writing {}={}", target.getPath() + "@" + key, value);
                    if (!isDryRun()) {
                        properties.put(key, value);
                    }
                }
            }
        }
    }
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 35 with ModifiableValueMap

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

the class AbstractCreateOperation method updateMixins.

protected void updateMixins(final ResourceResolver resolver, final String path, final Map<String, RequestProperty> reqProperties, final List<Modification> changes, final VersioningConfiguration versioningConfiguration) throws PersistenceException {
    final String[] mixins = getMixinTypes(reqProperties, path);
    if (mixins != null) {
        final Resource rsrc = resolver.getResource(path);
        final ModifiableValueMap mvm = rsrc.adaptTo(ModifiableValueMap.class);
        if (mvm != null) {
            this.jcrSsupport.checkoutIfNecessary(rsrc, changes, versioningConfiguration);
            mvm.put(JcrConstants.JCR_MIXINTYPES, mixins);
            for (final String mixin : mixins) {
                // the mix:versionable mixin does an implicit checkout
                if (mixin.equals(JcrConstants.MIX_VERSIONABLE) && versioningConfiguration.isCheckinOnNewVersionableNode()) {
                    changes.add(Modification.onCheckout(path));
                }
            }
        }
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

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