Search in sources :

Example 1 with JcrModifiableValueMap

use of org.apache.sling.jcr.resource.internal.JcrModifiableValueMap in project sling by apache.

the class JcrResourceProvider method create.

@Override
public Resource create(@Nonnull final ResolveContext<JcrProviderState> ctx, final String path, final Map<String, Object> properties) throws PersistenceException {
    // check for node type
    final Object nodeObj = (properties != null ? properties.get(NodeUtil.NODE_TYPE) : null);
    // check for sling:resourcetype
    final String nodeType;
    if (nodeObj != null) {
        nodeType = nodeObj.toString();
    } else {
        final Object rtObj = (properties != null ? properties.get(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY) : null);
        boolean isNodeType = false;
        if (rtObj != null) {
            final String resourceType = rtObj.toString();
            if (resourceType.indexOf(':') != -1 && resourceType.indexOf('/') == -1) {
                try {
                    ctx.getProviderState().getSession().getWorkspace().getNodeTypeManager().getNodeType(resourceType);
                    isNodeType = true;
                } catch (final RepositoryException ignore) {
                // we expect this, if this isn't a valid node type, therefore ignoring
                }
            }
        }
        if (isNodeType) {
            nodeType = rtObj.toString();
        } else {
            nodeType = null;
        }
    }
    final String jcrPath = path;
    if (jcrPath == null) {
        throw new PersistenceException("Unable to create node at " + path, null, path, null);
    }
    Node node = null;
    try {
        final int lastPos = jcrPath.lastIndexOf('/');
        final Node parent;
        if (lastPos == 0) {
            parent = ctx.getProviderState().getSession().getRootNode();
        } else {
            parent = (Node) ctx.getProviderState().getSession().getItem(jcrPath.substring(0, lastPos));
        }
        final String name = jcrPath.substring(lastPos + 1);
        if (nodeType != null) {
            node = parent.addNode(name, nodeType);
        } else {
            node = parent.addNode(name);
        }
        if (properties != null) {
            // create modifiable map
            final JcrModifiableValueMap jcrMap = new JcrModifiableValueMap(node, ctx.getProviderState().getHelperData());
            // check mixin types first
            final Object value = properties.get(NodeUtil.MIXIN_TYPES);
            if (value != null) {
                jcrMap.put(NodeUtil.MIXIN_TYPES, value);
            }
            for (final Map.Entry<String, Object> entry : properties.entrySet()) {
                if (!IGNORED_PROPERTIES.contains(entry.getKey())) {
                    try {
                        jcrMap.put(entry.getKey(), entry.getValue());
                    } catch (final IllegalArgumentException iae) {
                        try {
                            node.remove();
                        } catch (final RepositoryException re) {
                        // we ignore this
                        }
                        throw new PersistenceException(iae.getMessage(), iae, path, entry.getKey());
                    }
                }
            }
        }
        return new JcrNodeResource(ctx.getResourceResolver(), path, null, node, ctx.getProviderState().getHelperData());
    } catch (final RepositoryException e) {
        throw new PersistenceException("Unable to create node at " + jcrPath, e, path, null);
    }
}
Also used : JcrModifiableValueMap(org.apache.sling.jcr.resource.internal.JcrModifiableValueMap) Node(javax.jcr.Node) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException) Map(java.util.Map) HashMap(java.util.HashMap) JcrModifiableValueMap(org.apache.sling.jcr.resource.internal.JcrModifiableValueMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Node (javax.jcr.Node)1 RepositoryException (javax.jcr.RepositoryException)1 PersistenceException (org.apache.sling.api.resource.PersistenceException)1 JcrModifiableValueMap (org.apache.sling.jcr.resource.internal.JcrModifiableValueMap)1