Search in sources :

Example 71 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class NodeTypeImpl method canSetProperty.

/**
     * @see javax.jcr.nodetype.NodeType#canSetProperty(String, Value[])
     */
public boolean canSetProperty(String propertyName, Value[] values) {
    if (values == null) {
        // setting a property to null is equivalent of removing it
        return canRemoveItem(propertyName);
    }
    try {
        Name name = resolver().getQName(propertyName);
        // determine type of values
        int type = PropertyType.UNDEFINED;
        for (int i = 0; i < values.length; i++) {
            if (values[i] == null) {
                // skip null values as those would be purged
                continue;
            }
            if (type == PropertyType.UNDEFINED) {
                type = values[i].getType();
            } else if (type != values[i].getType()) {
                // inhomogeneous types
                return false;
            }
        }
        QPropertyDefinition def;
        try {
            // try to get definition that matches the given value type
            def = getApplicablePropDef(name, type, true);
        } catch (ConstraintViolationException cve) {
            // fallback: ignore type
            def = getApplicablePropDef(name, PropertyType.UNDEFINED, true);
        }
        if (def.isProtected()) {
            return false;
        }
        if (!def.isMultiple()) {
            return false;
        }
        // determine target type
        int targetType;
        if (def.getRequiredType() != PropertyType.UNDEFINED && def.getRequiredType() != type) {
            // type conversion required
            targetType = def.getRequiredType();
        } else {
            // no type conversion required
            targetType = type;
        }
        ArrayList<QValue> list = new ArrayList<QValue>();
        // convert values and compact array (purge null entries)
        for (int i = 0; i < values.length; i++) {
            if (values[i] != null) {
                // create QValue from Value and perform
                // type conversion as necessary
                Value v = ValueHelper.convert(values[i], targetType, mgrProvider.getJcrValueFactory());
                QValue qValue = ValueFormat.getQValue(v, resolver(), mgrProvider.getQValueFactory());
                list.add(qValue);
            }
        }
        QValue[] internalValues = list.toArray(new QValue[list.size()]);
        checkSetPropertyValueConstraints(def, internalValues);
        return true;
    } catch (NameException be) {
    // implementation specific exception, fall through
    } catch (RepositoryException re) {
    // fall through
    }
    return false;
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ArrayList(java.util.ArrayList) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint) Name(org.apache.jackrabbit.spi.Name)

Example 72 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class SessionImporter method importProperty.

/**
     *
     * @param pi
     * @param parentState
     * @param resolver
     * @throws RepositoryException
     * @throws ConstraintViolationException
     */
private void importProperty(PropInfo pi, NodeState parentState, NamePathResolver resolver) throws RepositoryException, ConstraintViolationException {
    Name propName = pi.getName();
    TextValue[] tva = pi.getValues();
    int infoType = pi.getType();
    PropertyState propState = null;
    QPropertyDefinition def = null;
    NodeEntry parentEntry = (NodeEntry) parentState.getHierarchyEntry();
    PropertyEntry pEntry = parentEntry.getPropertyEntry(propName);
    if (pEntry != null) {
        // a property with that name already exists...
        try {
            PropertyState existing = pEntry.getPropertyState();
            def = existing.getDefinition();
            if (def.isProtected()) {
                // skip protected property
                log.debug("skipping protected property " + LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
                return;
            }
            if (def.isAutoCreated() && (existing.getType() == infoType || infoType == PropertyType.UNDEFINED) && def.isMultiple() == existing.isMultiValued()) {
                // this property has already been auto-created, no need to create it
                propState = existing;
            } else {
                throw new ItemExistsException(LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
            }
        } catch (ItemNotFoundException e) {
        // property doesn't exist any more
        // -> ignore
        }
    }
    Name[] parentNtNames = parentState.getAllNodeTypeNames();
    if (def == null) {
        // there's no property with that name, find applicable definition
        if (tva.length == 1) {
            // could be single- or multi-valued (n == 1)
            def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType);
        } else {
            // can only be multi-valued (n == 0 || n > 1)
            def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType, true);
        }
        if (def.isProtected()) {
            // skip protected property
            log.debug("skipping protected property " + propName);
            return;
        }
    }
    // retrieve the target property type needed for creation of QValue(s)
    // including an eventual conversion. the targetType is then needed for
    // setting/updating the type of the property-state.
    int targetType = def.getRequiredType();
    if (targetType == PropertyType.UNDEFINED) {
        if (infoType == PropertyType.UNDEFINED) {
            targetType = PropertyType.STRING;
        } else {
            targetType = infoType;
        }
    }
    QValue[] values = getPropertyValues(pi, targetType, def.isMultiple(), resolver);
    if (propState == null) {
        // create new property
        Operation ap = AddProperty.create(parentState, propName, targetType, def, values);
        stateMgr.execute(ap);
        propState = parentEntry.getPropertyEntry(propName).getPropertyState();
    } else {
        // modify value of existing property
        Operation sp = SetPropertyValue.create(propState, values, targetType);
        stateMgr.execute(sp);
    }
    // store reference for later resolution
    if (propState.getType() == PropertyType.REFERENCE) {
        refTracker.processedReference(propState);
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) Name(org.apache.jackrabbit.spi.Name) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) PropertyEntry(org.apache.jackrabbit.jcr2spi.hierarchy.PropertyEntry) ItemExistsException(javax.jcr.ItemExistsException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 73 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class ReferenceChangeTracker method getMappedReference.

/**
     * Returns the new UUID to which <code>oldUUID</code> has been mapped
     * or <code>null</code> if no such mapping exists.
     *
     * @param oldReference old uuid represented by the given <code>QValue</code>.
     * @param factory
     * @return mapped new QValue of the reference value or <code>null</code> if no such mapping exists
     * @see #mappedUUIDs(String,String)
     */
public QValue getMappedReference(QValue oldReference, QValueFactory factory) {
    QValue remapped = null;
    if (oldReference.getType() == PropertyType.REFERENCE) {
        try {
            String oldValue = oldReference.getString();
            if (uuidMap.containsKey(oldValue)) {
                String newValue = uuidMap.get(oldValue);
                remapped = factory.create(newValue, PropertyType.REFERENCE);
            }
        } catch (RepositoryException e) {
            log.error("Unexpected error while creating internal value.", e);
        }
    }
    return remapped;
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) RepositoryException(javax.jcr.RepositoryException)

Example 74 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class AbstractJCR2SPITest method getDescriptors.

protected Map<String, QValue[]> getDescriptors() throws RepositoryException {
    Map<String, QValue[]> descriptorKeys = new HashMap<String, QValue[]>();
    QValueFactory qvf = QValueFactoryImpl.getInstance();
    descriptorKeys.put(Repository.REP_NAME_DESC, new QValue[] { qvf.create("Mock Repository", PropertyType.STRING) });
    descriptorKeys.put(Repository.REP_VENDOR_DESC, new QValue[] { qvf.create("Apache Software Foundation", PropertyType.STRING) });
    descriptorKeys.put(Repository.REP_VENDOR_URL_DESC, new QValue[] { qvf.create("http://www.apache.org/", PropertyType.STRING) });
    descriptorKeys.put(Repository.REP_VERSION_DESC, new QValue[] { qvf.create("2.0", PropertyType.STRING) });
    descriptorKeys.put(Repository.SPEC_NAME_DESC, new QValue[] { qvf.create("Content Repository API for Java(TM) Technology Specification", PropertyType.STRING) });
    descriptorKeys.put(Repository.SPEC_VERSION_DESC, new QValue[] { qvf.create("2.0", PropertyType.STRING) });
    return descriptorKeys;
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) HashMap(java.util.HashMap) QValueFactory(org.apache.jackrabbit.spi.QValueFactory)

Example 75 with QValue

use of org.apache.jackrabbit.spi.QValue in project jackrabbit by apache.

the class SessionItemStateManager method visit.

/**
     * @see OperationVisitor#visit(SetMixin)
     */
public void visit(SetMixin operation) throws ConstraintViolationException, AccessDeniedException, NoSuchNodeTypeException, UnsupportedRepositoryOperationException, VersionException, RepositoryException {
    // NOTE: nodestate is only modified upon save of the changes!
    Name[] mixinNames = operation.getMixinNames();
    NodeState nState = operation.getNodeState();
    NodeEntry nEntry = nState.getNodeEntry();
    // assert the existence of the property entry and set the array of
    // mixinNames to be set on the corresponding property state
    PropertyEntry mixinEntry = nEntry.getPropertyEntry(NameConstants.JCR_MIXINTYPES);
    if (mixinNames.length > 0) {
        // update/create corresponding property state
        if (mixinEntry != null) {
            // execute value of existing property
            PropertyState pState = mixinEntry.getPropertyState();
            setPropertyStateValue(pState, getQValues(mixinNames, qValueFactory), PropertyType.NAME, operation.getOptions());
        } else {
            // create new jcr:mixinTypes property
            ItemDefinitionProvider defProvider = mgrProvider.getItemDefinitionProvider();
            QPropertyDefinition pd = defProvider.getQPropertyDefinition(nState.getAllNodeTypeNames(), NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true);
            QValue[] mixinValue = getQValues(mixinNames, qValueFactory);
            addPropertyState(nState, pd.getName(), pd.getRequiredType(), mixinValue, pd, operation.getOptions());
        }
        nState.markModified();
        transientStateMgr.addOperation(operation);
    } else if (mixinEntry != null) {
        // remove the jcr:mixinTypes property state if already present
        PropertyState pState = mixinEntry.getPropertyState();
        removeItemState(pState, operation.getOptions());
        nState.markModified();
        transientStateMgr.addOperation(operation);
    }
// else: empty Name array and no mixin-prop-entry (should not occur)
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) PropertyEntry(org.apache.jackrabbit.jcr2spi.hierarchy.PropertyEntry) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ItemDefinitionProvider(org.apache.jackrabbit.jcr2spi.nodetype.ItemDefinitionProvider) Name(org.apache.jackrabbit.spi.Name)

Aggregations

QValue (org.apache.jackrabbit.spi.QValue)125 Name (org.apache.jackrabbit.spi.Name)40 RepositoryException (javax.jcr.RepositoryException)23 Value (javax.jcr.Value)21 NodeId (org.apache.jackrabbit.spi.NodeId)21 Batch (org.apache.jackrabbit.spi.Batch)19 PropertyInfo (org.apache.jackrabbit.spi.PropertyInfo)18 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 ValueFormatException (javax.jcr.ValueFormatException)8 Path (org.apache.jackrabbit.spi.Path)8 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)8 HashMap (java.util.HashMap)7 ValueConstraint (org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)7 InputStream (java.io.InputStream)5 Node (javax.jcr.Node)5 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)5 PropertyId (org.apache.jackrabbit.spi.PropertyId)5