Search in sources :

Example 66 with Property

use of javax.jcr.Property in project jackrabbit by apache.

the class AuthorizableImpl method setProperty.

/**
     * Sets the Value for the given name. If a value existed, it is replaced,
     * if not it is created.
     *
     * @param relPath The relative path to the property or the property name.
     * @param value The property value.
     * @throws RepositoryException If the specified name defines a property
     * that needs to be modified by this user API or setting the corresponding
     * JCR property fails.
     * @see Authorizable#setProperty(String, Value)
     */
public synchronized void setProperty(String relPath, Value value) throws RepositoryException {
    String name = Text.getName(relPath);
    String intermediate = (relPath.equals(name)) ? null : Text.getRelativeParent(relPath, 1);
    checkProtectedProperty(name);
    try {
        Node n = getOrCreateTargetNode(intermediate);
        // ValueFormatException.
        if (n.hasProperty(name)) {
            Property p = n.getProperty(name);
            if (p.isMultiple()) {
                p.remove();
            }
        }
        n.setProperty(name, value);
        if (userManager.isAutoSave()) {
            node.save();
        }
    } catch (RepositoryException e) {
        log.debug("Failed to set Property " + name + " for " + this, e);
        node.refresh(false);
        throw e;
    }
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 67 with Property

use of javax.jcr.Property in project jackrabbit by apache.

the class AuthorizableImpl method removeProperty.

/**
     * @see Authorizable#removeProperty(String)
     */
public synchronized boolean removeProperty(String relPath) throws RepositoryException {
    String name = Text.getName(relPath);
    checkProtectedProperty(name);
    try {
        if (node.hasProperty(relPath)) {
            Property p = node.getProperty(relPath);
            if (isAuthorizableProperty(p, true)) {
                p.remove();
                if (userManager.isAutoSave()) {
                    node.save();
                }
                return true;
            }
        }
        // no such property or wasn't a property of this authorizable.
        return false;
    } catch (RepositoryException e) {
        log.debug("Failed to remove Property " + relPath + " from " + this, e);
        node.refresh(false);
        throw e;
    }
}
Also used : RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 68 with Property

use of javax.jcr.Property in project jackrabbit by apache.

the class AuthorizableImpl method setProperty.

/**
     * Sets the Value[] for the given name. If a value existed, it is replaced,
     * if not it is created.
     *
     * @param relPath The relative path to the property or the property name.
     * @param values The property values.
     * @throws RepositoryException If the specified name defines a property
     * that needs to be modified by this user API or setting the corresponding
     * JCR property fails.
     * @see Authorizable#setProperty(String, Value[])
     */
public synchronized void setProperty(String relPath, Value[] values) throws RepositoryException {
    String name = Text.getName(relPath);
    String intermediate = (relPath.equals(name)) ? null : Text.getRelativeParent(relPath, 1);
    checkProtectedProperty(name);
    try {
        Node n = getOrCreateTargetNode(intermediate);
        // ValueFormatException.
        if (n.hasProperty(name)) {
            Property p = n.getProperty(name);
            if (!p.isMultiple()) {
                p.remove();
            }
        }
        n.setProperty(name, values);
        if (userManager.isAutoSave()) {
            node.save();
        }
    } catch (RepositoryException e) {
        log.debug("Failed to set Property " + name + " for " + this, e);
        node.refresh(false);
        throw e;
    }
}
Also used : Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 69 with Property

use of javax.jcr.Property in project jackrabbit by apache.

the class TraversingNodeResolver method getMatchingNode.

/**
     *
     * @param node
     * @param predicate
     * @param relPath
     * @param value
     * @param exact
     * @return
     * @throws RepositoryException
     */
private static Node getMatchingNode(NodeImpl node, AuthorizableTypePredicate predicate, String relPath, String value, boolean exact) throws RepositoryException {
    boolean match = false;
    Node authNode = predicate.getAuthorizableNode(node);
    if (authNode != null && node.hasProperty(relPath)) {
        try {
            Property prop = node.getProperty(relPath);
            if (prop.isMultiple()) {
                Value[] values = prop.getValues();
                for (int i = 0; i < values.length && !match; i++) {
                    match = matches(value, values[i].getString(), exact);
                }
            } else {
                match = matches(value, prop.getString(), exact);
            }
        } catch (PatternSyntaxException pe) {
            log.debug("couldn't search for {}, pattern invalid: {}", value, pe.getMessage());
        }
    }
    return (match) ? authNode : null;
}
Also used : Node(javax.jcr.Node) Value(javax.jcr.Value) Property(javax.jcr.Property) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 70 with Property

use of javax.jcr.Property in project jackrabbit by apache.

the class JcrUtils method toString.

/**
     * Returns a string representation of the given item. The returned string
     * is designed to be easily readable while providing maximum amount of
     * information for logging and debugging purposes.
     * <p>
     * The returned string is not meant to be parsed and the exact contents
     * can change in future releases. The current string representation of
     * a node is "/path/to/node [type]" and the representation of a property is
     * "@name = value(s)". Binary values are expressed like "&lt;123 bytes&gt;"
     * and other values as their standard binary representation. Multi-valued
     * properties have their values listed in like "[ v1, v2, v3, ... ]". No
     * more than the three first values are included. Long string values are
     * truncated.
     *
     * @param item given node or property
     * @return string representation of the given item
     */
public static String toString(Item item) {
    StringBuilder builder = new StringBuilder();
    try {
        if (item.isNode()) {
            builder.append(item.getPath());
            builder.append(" [");
            builder.append(((Node) item).getPrimaryNodeType().getName());
            builder.append("]");
        } else {
            builder.append("@");
            builder.append(item.getName());
            builder.append(" = ");
            Property property = (Property) item;
            if (property.isMultiple()) {
                builder.append("[ ");
                Value[] values = property.getValues();
                for (int i = 0; i < values.length && i < 3; i++) {
                    if (i > 0) {
                        builder.append(", ");
                    }
                    append(builder, values[i]);
                }
                if (values.length >= 3) {
                    builder.append(", ...");
                }
                builder.append(" ]");
            } else {
                append(builder, property.getValue());
            }
        }
    } catch (RepositoryException e) {
        builder.append("!!! ");
        builder.append(e.getMessage());
        builder.append(" !!!");
    }
    return builder.toString();
}
Also used : Node(javax.jcr.Node) Value(javax.jcr.Value) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Aggregations

Property (javax.jcr.Property)445 Node (javax.jcr.Node)252 Value (javax.jcr.Value)99 Test (org.junit.Test)87 Session (javax.jcr.Session)78 PropertyIterator (javax.jcr.PropertyIterator)68 RepositoryException (javax.jcr.RepositoryException)64 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)47 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)37 ValueFormatException (javax.jcr.ValueFormatException)34 QValue (org.apache.jackrabbit.spi.QValue)29 ArrayList (java.util.ArrayList)24 NodeIterator (javax.jcr.NodeIterator)24 QValueValue (org.apache.jackrabbit.spi.commons.value.QValueValue)23 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)20 Item (javax.jcr.Item)19 PathNotFoundException (javax.jcr.PathNotFoundException)17 InvalidItemStateException (javax.jcr.InvalidItemStateException)16 Version (javax.jcr.version.Version)16 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)15