Search in sources :

Example 21 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue in project jackrabbit by apache.

the class NodeImpl method setMixinTypesProperty.

void setMixinTypesProperty(Set<Name> mixinNames) throws RepositoryException {
    NodeState thisState = data.getNodeState();
    // get or create jcr:mixinTypes property
    PropertyImpl prop;
    if (thisState.hasPropertyName(NameConstants.JCR_MIXINTYPES)) {
        prop = (PropertyImpl) itemMgr.getItem(new PropertyId(thisState.getNodeId(), NameConstants.JCR_MIXINTYPES));
    } else {
        // find definition for the jcr:mixinTypes property and create property
        PropertyDefinitionImpl def = getApplicablePropertyDefinition(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true, true);
        prop = createChildProperty(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, def);
    }
    if (mixinNames.isEmpty()) {
        // purge empty jcr:mixinTypes property
        removeChildProperty(NameConstants.JCR_MIXINTYPES);
        return;
    }
    // call internalSetValue for setting the jcr:mixinTypes property
    // to avoid checking of the 'protected' flag
    InternalValue[] vals = new InternalValue[mixinNames.size()];
    Iterator<Name> iter = mixinNames.iterator();
    int cnt = 0;
    while (iter.hasNext()) {
        vals[cnt++] = InternalValue.create(iter.next());
    }
    prop.internalSetValue(vals, PropertyType.NAME);
}
Also used : NodeState(org.apache.jackrabbit.core.state.NodeState) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) InternalValue(org.apache.jackrabbit.core.value.InternalValue) PropertyId(org.apache.jackrabbit.core.id.PropertyId) Name(org.apache.jackrabbit.spi.Name)

Example 22 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue 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 23 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue in project jackrabbit by apache.

the class PropertyImpl method internalSetValue.

/**
     * @param values
     * @param type
     * @throws ConstraintViolationException
     * @throws RepositoryException
     */
protected void internalSetValue(InternalValue[] values, int type) throws ConstraintViolationException, RepositoryException {
    // check for null value
    if (values == null) {
        // setting a property to null removes it automatically
        ((NodeImpl) getParent()).removeChildProperty(((PropertyId) id).getName());
        return;
    }
    ArrayList<InternalValue> list = new ArrayList<InternalValue>();
    // compact array (purge null entries)
    for (InternalValue v : values) {
        if (v != null) {
            list.add(v);
        }
    }
    values = list.toArray(new InternalValue[list.size()]);
    // modify the state of this property
    PropertyState thisState = (PropertyState) getOrCreateTransientItemState();
    // free old values as necessary
    InternalValue[] oldValues = thisState.getValues();
    if (oldValues != null) {
        for (InternalValue old : oldValues) {
            if (old != null && old.getType() == BINARY) {
                // make sure temporarily allocated data is discarded
                // before overwriting it
                old.discard();
            }
        }
    }
    // set new values
    thisState.setValues(values);
    // set type
    if (type == UNDEFINED) {
        // fallback to default type
        type = STRING;
    }
    thisState.setType(type);
}
Also used : ArrayList(java.util.ArrayList) InternalValue(org.apache.jackrabbit.core.value.InternalValue) PropertyState(org.apache.jackrabbit.core.state.PropertyState)

Example 24 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue 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 25 with InternalValue

use of org.apache.jackrabbit.core.value.InternalValue 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)

Aggregations

InternalValue (org.apache.jackrabbit.core.value.InternalValue)62 Name (org.apache.jackrabbit.spi.Name)21 NodeId (org.apache.jackrabbit.core.id.NodeId)20 PropertyState (org.apache.jackrabbit.core.state.PropertyState)17 RepositoryException (javax.jcr.RepositoryException)16 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)13 PropertyId (org.apache.jackrabbit.core.id.PropertyId)12 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)11 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)11 NodeState (org.apache.jackrabbit.core.state.NodeState)10 Value (javax.jcr.Value)9 IOException (java.io.IOException)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)6 FileSystemResource (org.apache.jackrabbit.core.fs.FileSystemResource)6 ArrayList (java.util.ArrayList)5 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)4 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)4 Path (org.apache.jackrabbit.spi.Path)4