Search in sources :

Example 56 with PropertyDefinition

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

the class AddMixinOperation method perform.

public Object perform(SessionContext context) throws RepositoryException {
    int permissions = Permission.NODE_TYPE_MNGMT;
    // is required in addition.
    if (MIX_VERSIONABLE.equals(mixinName) || MIX_SIMPLE_VERSIONABLE.equals(mixinName)) {
        permissions |= Permission.VERSION_MNGMT;
    }
    context.getItemValidator().checkModify(node, CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD, permissions);
    NodeTypeManagerImpl ntMgr = context.getNodeTypeManager();
    NodeTypeImpl mixin = ntMgr.getNodeType(mixinName);
    if (!mixin.isMixin()) {
        throw new RepositoryException(context.getJCRName(mixinName) + " is not a mixin node type");
    }
    Name primaryTypeName = node.getNodeState().getNodeTypeName();
    NodeTypeImpl primaryType = ntMgr.getNodeType(primaryTypeName);
    if (primaryType.isDerivedFrom(mixinName)) {
        // new mixin is already included in primary type
        return this;
    }
    // build effective node type of mixin's & primary type in order
    // to detect conflicts
    NodeTypeRegistry ntReg = context.getNodeTypeRegistry();
    EffectiveNodeType entExisting;
    try {
        // existing mixin's
        Set<Name> mixins = new HashSet<Name>(node.getNodeState().getMixinTypeNames());
        // build effective node type representing primary type including
        // existing mixin's
        entExisting = ntReg.getEffectiveNodeType(primaryTypeName, mixins);
        if (entExisting.includesNodeType(mixinName)) {
            // new mixin is already included in existing mixin type(s)
            return this;
        }
        // add new mixin
        mixins.add(mixinName);
        // try to build new effective node type (will throw in case
        // of conflicts)
        ntReg.getEffectiveNodeType(primaryTypeName, mixins);
    } catch (NodeTypeConflictException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    }
    // try to revert the changes in case an exception occurs
    try {
        // modify the state of this node
        NodeState thisState = (NodeState) node.getOrCreateTransientItemState();
        // add mixin name
        Set<Name> mixins = new HashSet<Name>(thisState.getMixinTypeNames());
        mixins.add(mixinName);
        thisState.setMixinTypeNames(mixins);
        // set jcr:mixinTypes property
        node.setMixinTypesProperty(mixins);
        // add 'auto-create' properties defined in mixin type
        for (PropertyDefinition aPda : mixin.getAutoCreatedPropertyDefinitions()) {
            PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
            // make sure that the property is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) pd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
            }
        }
        // recursively add 'auto-create' child nodes defined in mixin type
        for (NodeDefinition aNda : mixin.getAutoCreatedNodeDefinitions()) {
            NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
            // make sure that the child node is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) nd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
            }
        }
    } catch (RepositoryException re) {
        // try to undo the modifications by removing the mixin
        try {
            node.removeMixin(mixinName);
        } catch (RepositoryException re1) {
        // silently ignore & fall through
        }
        throw re;
    }
    return this;
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Name(org.apache.jackrabbit.spi.Name) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) HashSet(java.util.HashSet)

Example 57 with PropertyDefinition

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

the class LuceneQueryFactory method getComparisonQuery.

protected Query getComparisonQuery(DynamicOperand left, int transform, String operator, StaticOperand rigth, Map<String, NodeType> selectorMap) throws RepositoryException {
    if (left instanceof PropertyValue) {
        PropertyValue pv = (PropertyValue) left;
        String field = npResolver.getJCRName(session.getQName(pv.getPropertyName()));
        int type = PropertyType.UNDEFINED;
        NodeType nt = selectorMap.get(pv.getSelectorName());
        if (nt != null) {
            for (PropertyDefinition pd : nt.getPropertyDefinitions()) {
                if (pd.getName().equals(pv.getPropertyName())) {
                    type = pd.getRequiredType();
                    break;
                }
            }
        }
        return getPropertyValueQuery(field, operator, evaluator.getValue(rigth), type, transform);
    } else if (left instanceof NodeName) {
        return getNodeNameQuery(transform, operator, rigth);
    } else if (left instanceof NodeLocalName) {
        return getNodeLocalNameQuery(transform, operator, rigth);
    } else {
        throw new UnsupportedRepositoryOperationException(// FIXME
        "Unknown operand type: " + left);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) NodeName(javax.jcr.query.qom.NodeName) NodeType(javax.jcr.nodetype.NodeType) PropertyValue(javax.jcr.query.qom.PropertyValue) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Constraint(javax.jcr.query.qom.Constraint) NodeLocalName(javax.jcr.query.qom.NodeLocalName)

Example 58 with PropertyDefinition

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

the class GetPropertyNamesTest method testGetPropertyNames.

/**
     * Check if the property names from the search results match the
     * non-residual ones from the base node type
     */
public void testGetPropertyNames() throws RepositoryException {
    String queryStatement = "/" + jcrRoot;
    // build and execute search query
    Query query = superuser.getWorkspace().getQueryManager().createQuery(queryStatement, qsXPATH);
    QueryResult result = query.execute();
    // Get the node's non-residual properties
    PropertyDefinition[] pd = superuser.getWorkspace().getNodeTypeManager().getNodeType(ntBase).getDeclaredPropertyDefinitions();
    List<String> singleValPropNames = new ArrayList<String>();
    for (int i = 0; i < pd.length; i++) {
        // only keep the single-value properties
        if (!pd[i].isMultiple()) {
            singleValPropNames.add(pd[i].getName());
        }
    }
    // add jcr:path
    singleValPropNames.add(jcrPath);
    singleValPropNames.add(jcrScore);
    String[] foundPropertyNames = result.getColumnNames();
    Object[] realPropertyNames = singleValPropNames.toArray();
    // sort the 2 arrays before comparing them
    Arrays.sort(foundPropertyNames);
    Arrays.sort(realPropertyNames);
    assertTrue("Property names don't match", Arrays.equals(foundPropertyNames, realPropertyNames));
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) ArrayList(java.util.ArrayList) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 59 with PropertyDefinition

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

the class NodeTypeDefinitionImpl method getDeclaredPropertyDefinitions.

/**
     * {@inheritDoc}
     */
public PropertyDefinition[] getDeclaredPropertyDefinitions() {
    QPropertyDefinition[] pda = ntd.getPropertyDefs();
    PropertyDefinition[] propDefs = new PropertyDefinition[pda.length];
    for (int i = 0; i < pda.length; i++) {
        propDefs[i] = new PropertyDefinitionImpl(pda[i], null, resolver, valueFactory);
    }
    return propDefs;
}
Also used : QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 60 with PropertyDefinition

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

the class AuthorizablePropertiesImpl method getAuthorizableProperty.

/**
     * Returns the valid authorizable property identified by the specified
     * property location or {@code null} if that property does not exist or
     * isn't a authorizable property because it is protected or outside of the
     * scope of the {@code authorizableTree}.
     *
     * @param authorizableTree The tree of the target authorizable.
     * @param propertyLocation Location to be tested.
     * @param verifyAncestor   If true the property is tested to be a descendant
     *                         of the node of this authorizable; otherwise it is expected that this
     *                         test has been executed by the caller.
     * @return a valid authorizable property or {@code null} if no such property
     *         exists or fi the property is protected or not defined by the rep:authorizable
     *         node type or one of it's sub-node types.
     * @throws RepositoryException If an error occurs.
     */
@CheckForNull
private PropertyState getAuthorizableProperty(@Nonnull Tree authorizableTree, @Nonnull TreeLocation propertyLocation, boolean verifyAncestor) throws RepositoryException {
    PropertyState property = propertyLocation.getProperty();
    if (property == null) {
        return null;
    }
    String authorizablePath = authorizableTree.getPath();
    if (verifyAncestor && !Text.isDescendant(authorizablePath, propertyLocation.getPath())) {
        log.debug("Attempt to access property outside of authorizable scope.");
        return null;
    }
    Tree parent = propertyLocation.getParent().getTree();
    if (parent == null) {
        log.debug("Unable to determine definition of authorizable property at " + propertyLocation.getPath());
        return null;
    }
    ReadOnlyNodeTypeManager nodeTypeManager = authorizable.getUserManager().getNodeTypeManager();
    PropertyDefinition def = nodeTypeManager.getDefinition(parent, property, true);
    if (def.isProtected() || (authorizablePath.equals(parent.getPath()) && !def.getDeclaringNodeType().isNodeType(UserConstants.NT_REP_AUTHORIZABLE))) {
        return null;
    }
    return property;
}
Also used : Tree(org.apache.jackrabbit.oak.api.Tree) ReadOnlyNodeTypeManager(org.apache.jackrabbit.oak.plugins.nodetype.ReadOnlyNodeTypeManager) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) CheckForNull(javax.annotation.CheckForNull)

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