Search in sources :

Example 66 with QPropertyDefinition

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

the class NodeTypeImpl method canSetProperty.

/**
     * @see javax.jcr.nodetype.NodeType#canSetProperty(String, Value)
     */
public boolean canSetProperty(String propertyName, Value value) {
    if (value == null) {
        // setting a property to null is equivalent of removing it
        return canRemoveItem(propertyName);
    }
    try {
        Name name = resolver().getQName(propertyName);
        QPropertyDefinition def;
        try {
            // try to get definition that matches the given value type
            def = getApplicablePropDef(name, value.getType(), false);
        } catch (ConstraintViolationException cve) {
            // fallback: ignore type
            def = getApplicablePropDef(name, PropertyType.UNDEFINED, false);
        }
        if (def.isProtected()) {
            return false;
        }
        if (def.isMultiple()) {
            return false;
        }
        Value v;
        if (def.getRequiredType() != PropertyType.UNDEFINED && def.getRequiredType() != value.getType()) {
            // type conversion required
            v = ValueHelper.convert(value, def.getRequiredType(), mgrProvider.getJcrValueFactory());
        } else {
            // no type conversion required
            v = value;
        }
        // create QValue from Value
        QValue qValue = ValueFormat.getQValue(v, resolver(), mgrProvider.getQValueFactory());
        checkSetPropertyValueConstraints(def, new QValue[] { qValue });
        return true;
    } catch (NameException re) {
    // fall through
    } catch (RepositoryException e) {
    // 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) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) Name(org.apache.jackrabbit.spi.Name)

Example 67 with QPropertyDefinition

use of org.apache.jackrabbit.spi.QPropertyDefinition 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 68 with QPropertyDefinition

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

the class NodeTypeImpl method getPropertyDefinitions.

/**
     * @see javax.jcr.nodetype.NodeType#getPropertyDefinitions()
     */
public PropertyDefinition[] getPropertyDefinitions() {
    QPropertyDefinition[] pda = ent.getAllQPropertyDefinitions();
    PropertyDefinition[] propDefs = new PropertyDefinition[pda.length];
    for (int i = 0; i < pda.length; i++) {
        propDefs[i] = ntMgr.getPropertyDefinition(pda[i]);
    }
    return propDefs;
}
Also used : QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)

Example 69 with QPropertyDefinition

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

the class NodeEntryImpl method complete.

private void complete(AddProperty operation) throws RepositoryException {
    if (operation.getParentState().getHierarchyEntry() != this) {
        throw new IllegalArgumentException();
    }
    PropertyEntry pe = getPropertyEntry(operation.getPropertyName());
    if (pe != null && pe.getStatus() == Status.NEW) {
        switch(operation.getStatus()) {
            case Operation.STATUS_PERSISTED:
                // for autocreated/protected props, mark to be reloaded
                // upon next access.
                PropertyState addedState = (PropertyState) ((PropertyEntryImpl) pe).internalGetItemState();
                addedState.setStatus(Status.EXISTING);
                QPropertyDefinition pd = addedState.getDefinition();
                if (pd.isAutoCreated() || pd.isProtected()) {
                    pe.invalidate(true);
                }
                // else: assume added property is up to date.
                break;
            case Operation.STATUS_UNDO:
                pe.revert();
                break;
            // ignore
            default:
        }
    }
// else: no such prop entry or entry has already been persisted
//       e.g due to external modifications merged into this NodeEntry.
}
Also used : QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState)

Example 70 with QPropertyDefinition

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

the class SessionImporter method importNode.

/**
     *
     * @param nodeInfo
     * @param parent
     * @return
     * @throws ConstraintViolationException
     * @throws ItemNotFoundException
     * @throws RepositoryException
     */
private NodeState importNode(NodeInfo nodeInfo, NodeState parent) throws ConstraintViolationException, ItemNotFoundException, RepositoryException {
    Name[] parentNtNames = parent.getAllNodeTypeNames();
    if (parent.hasPropertyName(nodeInfo.getName())) {
        /**
             * a property with the same name already exists; if this property
             * has been imported as well (e.g. through document view import
             * where an element can have the same name as one of the attributes
             * of its parent element) we have to rename the conflicting property;
             *
             * see http://issues.apache.org/jira/browse/JCR-61
             */
        PropertyState conflicting = parent.getPropertyState(nodeInfo.getName());
        if (conflicting.getStatus() == Status.NEW) {
            // assume this property has been imported as well;
            // rename conflicting property
            // TODO: use better reversible escaping scheme to create unique name
            Name newName = session.getNameFactory().create(nodeInfo.getName().getNamespaceURI(), nodeInfo.getName().getLocalName() + "_");
            if (parent.hasPropertyName(newName)) {
                newName = session.getNameFactory().create(newName.getNamespaceURI(), newName.getLocalName() + "_");
            }
            // since name changes, need to find new applicable definition
            QPropertyDefinition propDef;
            if (conflicting.getValues().length == 1) {
                // could be single- or multi-valued (n == 1)
                try {
                    // try single-valued
                    propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), false);
                } catch (ConstraintViolationException cve) {
                    // try multi-valued
                    propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
                }
            } else {
                // can only be multi-valued (n == 0 || n > 1)
                propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
            }
            Operation ap = AddProperty.create(parent, newName, conflicting.getType(), propDef, conflicting.getValues());
            stateMgr.execute(ap);
            Operation rm = Remove.create(conflicting);
            stateMgr.execute(rm);
        }
    }
    // do create new nodeState
    QNodeDefinition def = session.getItemDefinitionProvider().getQNodeDefinition(parentNtNames, nodeInfo.getName(), nodeInfo.getNodeTypeName());
    if (def.isProtected()) {
        log.debug("Skipping protected nodeState (" + nodeInfo.getName() + ")");
        return null;
    } else {
        Name ntName = nodeInfo.getNodeTypeName();
        if (ntName == null) {
            // use default node type
            ntName = def.getDefaultPrimaryType();
        }
        Operation an = AddNode.create(parent, nodeInfo.getName(), ntName, nodeInfo.getUUID());
        stateMgr.execute(an);
        // retrieve id of state that has been created during execution of AddNode
        NodeState childState = (NodeState) ((AddNode) an).getAddedStates().get(0);
        // and set mixin types
        Name[] mixinNames = nodeInfo.getMixinNames();
        if (mixinNames != null && mixinNames.length > 0) {
            Operation sm = SetMixin.create(childState, nodeInfo.getMixinNames());
            stateMgr.execute(sm);
        }
        return childState;
    }
}
Also used : NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) AddNode(org.apache.jackrabbit.jcr2spi.operation.AddNode) Name(org.apache.jackrabbit.spi.Name) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState)

Aggregations

QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)81 Name (org.apache.jackrabbit.spi.Name)32 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)22 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)19 RepositoryException (javax.jcr.RepositoryException)14 QValue (org.apache.jackrabbit.spi.QValue)12 QItemDefinition (org.apache.jackrabbit.spi.QItemDefinition)9 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)9 ArrayList (java.util.ArrayList)8 Value (javax.jcr.Value)7 InternalValue (org.apache.jackrabbit.core.value.InternalValue)7 ItemExistsException (javax.jcr.ItemExistsException)6 NodeId (org.apache.jackrabbit.core.id.NodeId)6 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)5 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)5 PropertyState (org.apache.jackrabbit.core.state.PropertyState)5 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)5 ValueConstraint (org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)5 NodeState (org.apache.jackrabbit.core.state.NodeState)4 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)4