Search in sources :

Example 51 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.

the class PropertyImpl method setValue.

/**
     * Same as <code>{@link Property#setValue(String[])}</code> except that
     * this method takes an array of <code>Name</code> instead of
     * <code>String</code> values.
     *
     * @param names
     * @throws ValueFormatException
     * @throws VersionException
     * @throws LockException
     * @throws ConstraintViolationException
     * @throws RepositoryException
     */
public void setValue(Name[] names) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
    // check state of this instance
    sanityCheck();
    // check pre-conditions for setting property value
    checkSetValue(true);
    // check type according to definition of this property
    final PropertyDefinition definition = data.getPropertyDefinition();
    int reqType = definition.getRequiredType();
    if (reqType == UNDEFINED) {
        reqType = NAME;
    }
    InternalValue[] internalValues = null;
    // convert to internal values of correct type
    if (names != null) {
        internalValues = new InternalValue[names.length];
        for (int i = 0; i < names.length; i++) {
            Name name = names[i];
            InternalValue internalValue = null;
            if (name != null) {
                if (reqType != NAME) {
                    // type conversion required
                    Value targetValue = ValueHelper.convert(ValueFormat.getJCRValue(InternalValue.create(name), sessionContext, getSession().getValueFactory()), reqType, getSession().getValueFactory());
                    internalValue = InternalValue.create(targetValue, sessionContext, sessionContext.getDataStore());
                } else {
                    // no type conversion required
                    internalValue = InternalValue.create(name);
                }
            }
            internalValues[i] = internalValue;
        }
    }
    internalSetValue(internalValues, reqType);
}
Also used : InternalValue(org.apache.jackrabbit.core.value.InternalValue) Value(javax.jcr.Value) InternalValue(org.apache.jackrabbit.core.value.InternalValue) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Name(org.apache.jackrabbit.spi.Name)

Example 52 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.

the class PropertyImpl method setValue.

public synchronized void setValue(Value value) throws ValueFormatException, VersionException, LockException, ConstraintViolationException, RepositoryException {
    // check state of this instance
    sanityCheck();
    // check pre-conditions for setting property value
    checkSetValue(false);
    // check type according to definition of this property
    final PropertyDefinition definition = data.getPropertyDefinition();
    int reqType = definition.getRequiredType();
    if (reqType == UNDEFINED) {
        if (value != null) {
            reqType = value.getType();
        } else {
            reqType = STRING;
        }
    }
    if (value == null) {
        internalSetValue(null, reqType);
        return;
    }
    InternalValue internalValue;
    if (reqType != value.getType()) {
        // type conversion required
        Value targetVal = ValueHelper.convert(value, reqType, getSession().getValueFactory());
        internalValue = InternalValue.create(targetVal, sessionContext, sessionContext.getDataStore());
    } else {
        // no type conversion required
        internalValue = InternalValue.create(value, sessionContext, sessionContext.getDataStore());
    }
    internalSetValue(new InternalValue[] { internalValue }, reqType);
}
Also used : InternalValue(org.apache.jackrabbit.core.value.InternalValue) Value(javax.jcr.Value) InternalValue(org.apache.jackrabbit.core.value.InternalValue) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 53 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.

the class PropertyImpl method setValue.

/**
     * Sets the values of this property.
     *
     * @param values property values (possibly <code>null</code>)
     * @param valueType default value type if not set in the node type,
     *                  may be {@link PropertyType#UNDEFINED}
     * @throws RepositoryException if the property values could not be set
     */
public void setValue(Value[] values, int valueType) throws RepositoryException {
    // check state of this instance
    sanityCheck();
    // check pre-conditions for setting property value
    checkSetValue(true);
    if (values != null) {
        // check type of values
        int firstValueType = UNDEFINED;
        for (Value value : values) {
            if (value != null) {
                if (firstValueType == UNDEFINED) {
                    firstValueType = value.getType();
                } else if (firstValueType != value.getType()) {
                    throw new ValueFormatException("inhomogeneous type of values");
                }
            }
        }
    }
    final PropertyDefinition definition = data.getPropertyDefinition();
    int reqType = definition.getRequiredType();
    if (reqType == UNDEFINED) {
        // use the given type as property type
        reqType = valueType;
    }
    InternalValue[] internalValues = null;
    // convert to internal values of correct type
    if (values != null) {
        internalValues = new InternalValue[values.length];
        // check type of values
        for (int i = 0; i < values.length; i++) {
            Value value = values[i];
            if (value != null) {
                if (reqType == UNDEFINED) {
                    // Use the type of the fist value as the type
                    reqType = value.getType();
                }
                if (reqType != value.getType()) {
                    value = ValueHelper.convert(value, reqType, getSession().getValueFactory());
                }
                internalValues[i] = InternalValue.create(value, sessionContext, sessionContext.getDataStore());
            } else {
                internalValues[i] = null;
            }
        }
    }
    internalSetValue(internalValues, reqType);
}
Also used : InternalValue(org.apache.jackrabbit.core.value.InternalValue) Value(javax.jcr.Value) ValueFormatException(javax.jcr.ValueFormatException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 54 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.

the class GQL method resolvePropertyName.

/**
     * Resolves the given property name. If the name has a prefix then the name
     * is returned immediately as is. Otherwise the node type manager is
     * searched for a property definition that defines a named property with
     * a local name that matches the provided <code>name</code>. If such a match
     * is found the name of the property definition is returned.
     *
     * @param name the name of a property (optionally without a prefix).
     * @return the resolved property name.
     * @throws RepositoryException if an error occurs while reading from the
     *                             node type manager.
     */
private String resolvePropertyName(String name) throws RepositoryException {
    if (isPrefixed(name)) {
        return name;
    }
    if (propertyNames == null) {
        propertyNames = new HashMap<String, String>();
        if (session != null) {
            NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
            NodeTypeIterator it = ntMgr.getAllNodeTypes();
            while (it.hasNext()) {
                NodeType nt = it.nextNodeType();
                PropertyDefinition[] defs = nt.getDeclaredPropertyDefinitions();
                for (PropertyDefinition def : defs) {
                    String pn = def.getName();
                    if (!pn.equals("*")) {
                        String localName = pn;
                        int idx = pn.indexOf(':');
                        if (idx != -1) {
                            localName = pn.substring(idx + 1);
                        }
                        propertyNames.put(localName, pn);
                    }
                }
            }
        }
    }
    String pn = propertyNames.get(name);
    if (pn != null) {
        return pn;
    } else {
        return name;
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 55 with PropertyDefinition

use of javax.jcr.nodetype.PropertyDefinition in project jackrabbit by apache.

the class SetPropertyConstraintViolationExceptionTest method testBinaryProperty.

/**
     * Tests if setProperty(String name, InputStream value) and
     * setProperty(String name, Value value) where value is a BinaryValue throw
     * a ConstraintViolationException either immediately (by setProperty()), or
     * on save, if the change would violate a node type constraint
     */
public void testBinaryProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.BINARY, false, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No binary property def with " + "testable value constraints has been found");
    }
    // find a Value that does not satisfy the ValueConstraints of propDef
    Value valueNotSatisfied1 = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, false);
    Value valueNotSatisfied2 = NodeTypeUtil.getValueAccordingToValueConstraints(superuser, propDef, false);
    if (valueNotSatisfied1 == null || valueNotSatisfied2 == null) {
        throw new NotExecutableException("No binary property def with " + "testable value constraints has been found");
    }
    // create a sub node of testRootNode of type propDef.getDeclaringNodeType()
    Node node;
    try {
        String nodeType = propDef.getDeclaringNodeType().getName();
        node = testRootNode.addNode(nodeName2, nodeType);
        testRootNode.getSession().save();
    } catch (ConstraintViolationException e) {
        // implementation specific constraints do not allow to set up test environment
        throw new NotExecutableException("Not able to create required test items.");
    }
    // test of signature setProperty(String name, InputStream value)
    InputStream in = valueNotSatisfied1.getStream();
    try {
        node.setProperty(propDef.getName(), in);
        node.save();
        fail("setProperty(String name, InputStream value) must throw a " + "ConstraintViolationException if the change would violate a " + "node type constraint either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    } finally {
        try {
            in.close();
        } catch (IOException ignore) {
        }
    }
    // test of signature setProperty(String name, Value value)
    try {
        node.setProperty(propDef.getName(), valueNotSatisfied2);
        node.save();
        fail("setProperty(String name, Value value) must throw a " + "ConstraintViolationException if the change would violate a " + "node type constraint either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) InputStream(java.io.InputStream) Node(javax.jcr.Node) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) IOException(java.io.IOException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Aggregations

PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)141 NodeType (javax.jcr.nodetype.NodeType)79 Value (javax.jcr.Value)70 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)61 Node (javax.jcr.Node)27 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)22 Property (javax.jcr.Property)20 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)15 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 RepositoryException (javax.jcr.RepositoryException)13 NodeDefinition (javax.jcr.nodetype.NodeDefinition)13 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)11 Test (org.junit.Test)11 InputStream (java.io.InputStream)5 Session (javax.jcr.Session)5 ValueFormatException (javax.jcr.ValueFormatException)5 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 Name (org.apache.jackrabbit.spi.Name)4