Search in sources :

Example 16 with InternalValue

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

the class ItemValidator method validate.

/**
     * Checks whether the given property state satisfies the constraints
     * specified by its definition. The following validations/checks are
     * performed:
     * <ul>
     * <li>check if the type of the property values does comply with the
     * requiredType specified in the property's definition</li>
     * <li>check if the property values satisfy the value constraints
     * specified in the property's definition</li>
     * </ul>
     *
     * @param propState state of property to be validated
     * @throws ConstraintViolationException if any of the validations fail
     * @throws RepositoryException          if another error occurs
     */
public void validate(PropertyState propState) throws ConstraintViolationException, RepositoryException {
    QPropertyDefinition def = context.getItemManager().getDefinition(propState).unwrap();
    InternalValue[] values = propState.getValues();
    int type = PropertyType.UNDEFINED;
    for (InternalValue value : values) {
        if (type == PropertyType.UNDEFINED) {
            type = value.getType();
        } else if (type != value.getType()) {
            throw new ConstraintViolationException(safeGetJCRPath(propState.getPropertyId()) + ": inconsistent value types");
        }
        if (def.getRequiredType() != PropertyType.UNDEFINED && def.getRequiredType() != type) {
            throw new ConstraintViolationException(safeGetJCRPath(propState.getPropertyId()) + ": requiredType constraint is not satisfied");
        }
    }
    EffectiveNodeType.checkSetPropertyValueConstraints(def, values);
}
Also used : QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) InternalValue(org.apache.jackrabbit.core.value.InternalValue)

Example 17 with InternalValue

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

the class LengthOperand method getValues.

/**
     * {@inheritDoc}
     */
public Value[] getValues(ScoreNode sn, EvaluationContext context) throws RepositoryException {
    PropertyState ps = property.getPropertyState(sn, context);
    if (ps == null) {
        return EMPTY;
    } else {
        ValueFactoryImpl vf = (ValueFactoryImpl) context.getSession().getValueFactory();
        QValueFactory qvf = vf.getQValueFactory();
        InternalValue[] values = ps.getValues();
        Value[] lengths = new Value[values.length];
        for (int i = 0; i < lengths.length; i++) {
            long len;
            int type = values[i].getType();
            if (type == PropertyType.NAME) {
                len = vf.createValue(qvf.create(values[i].getName())).getString().length();
            } else if (type == PropertyType.PATH) {
                len = vf.createValue(qvf.create(values[i].getPath())).getString().length();
            } else {
                len = Util.getLength(values[i]);
            }
            lengths[i] = vf.createValue(len);
        }
        return lengths;
    }
}
Also used : ValueFactoryImpl(org.apache.jackrabbit.core.value.ValueFactoryImpl) Value(javax.jcr.Value) InternalValue(org.apache.jackrabbit.core.value.InternalValue) InternalValue(org.apache.jackrabbit.core.value.InternalValue) QValueFactory(org.apache.jackrabbit.spi.QValueFactory) PropertyState(org.apache.jackrabbit.core.state.PropertyState)

Example 18 with InternalValue

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

the class NodeTypeInstanceHandler method computeSystemGeneratedPropertyValues.

/**
     * Computes the values of well-known system (i.e. protected) properties.
     *
     * @param parent the parent node state
     * @param def the definition of the property to compute
     * @return the computed values
     */
public InternalValue[] computeSystemGeneratedPropertyValues(NodeState parent, QPropertyDefinition def) {
    InternalValue[] genValues = null;
    Name name = def.getName();
    Name declaringNT = def.getDeclaringNodeType();
    if (NameConstants.JCR_UUID.equals(name)) {
        // jcr:uuid property of the mix:referenceable node type
        if (NameConstants.MIX_REFERENCEABLE.equals(declaringNT)) {
            genValues = new InternalValue[] { InternalValue.create(parent.getNodeId().toString()) };
        }
    } else if (NameConstants.JCR_PRIMARYTYPE.equals(name)) {
        // jcr:primaryType property (of any node type)
        genValues = new InternalValue[] { InternalValue.create(parent.getNodeTypeName()) };
    } else if (NameConstants.JCR_MIXINTYPES.equals(name)) {
        // jcr:mixinTypes property (of any node type)
        Set<Name> mixins = parent.getMixinTypeNames();
        genValues = new InternalValue[mixins.size()];
        int i = 0;
        for (Name n : mixins) {
            genValues[i++] = InternalValue.create(n);
        }
    } else if (NameConstants.JCR_CREATED.equals(name)) {
        // jcr:created property of a version or a mix:created
        if (NameConstants.MIX_CREATED.equals(declaringNT) || NameConstants.NT_VERSION.equals(declaringNT)) {
            genValues = new InternalValue[] { InternalValue.create(Calendar.getInstance()) };
        }
    } else if (NameConstants.JCR_CREATEDBY.equals(name)) {
        // jcr:createdBy property of a mix:created
        if (NameConstants.MIX_CREATED.equals(declaringNT)) {
            genValues = new InternalValue[] { InternalValue.create(userId) };
        }
    } else if (NameConstants.JCR_LASTMODIFIED.equals(name)) {
        // jcr:lastModified property of a mix:lastModified
        if (NameConstants.MIX_LASTMODIFIED.equals(declaringNT)) {
            genValues = new InternalValue[] { InternalValue.create(Calendar.getInstance()) };
        }
    } else if (NameConstants.JCR_LASTMODIFIEDBY.equals(name)) {
        // jcr:lastModifiedBy property of a mix:lastModified
        if (NameConstants.MIX_LASTMODIFIED.equals(declaringNT)) {
            genValues = new InternalValue[] { InternalValue.create(userId) };
        }
    } else if (NameConstants.JCR_ETAG.equals(name)) {
        // jcr:etag property of a mix:etag
        if (NameConstants.MIX_ETAG.equals(declaringNT)) {
            // TODO: provide real implementation
            genValues = new InternalValue[] { InternalValue.create("") };
        }
    }
    return genValues;
}
Also used : InternalValue(org.apache.jackrabbit.core.value.InternalValue) Name(org.apache.jackrabbit.spi.Name)

Example 19 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 20 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)

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