Search in sources :

Example 31 with NodeTypeIterator

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

the class GQL method collectNodeTypes.

/**
     * Resolves and collects all node types that match <code>ntName</code>.
     *
     * @param ntName the name of a node type (optionally without prefix).
     * @throws RepositoryException if an error occurs while reading from the
     *                             node type manager.
     */
private void collectNodeTypes(String ntName) throws RepositoryException {
    NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
    String[] resolvedNames = resolveNodeTypeName(ntName);
    // now resolve node type hierarchy
    for (String resolvedName : resolvedNames) {
        try {
            NodeType base = ntMgr.getNodeType(resolvedName);
            if (base.isMixin()) {
                // search for nodes where jcr:mixinTypes is set to this mixin
                addTypeConstraint(new MixinComparision(resolvedName));
            } else {
                // search for nodes where jcr:primaryType is set to this type
                addTypeConstraint(new PrimaryTypeComparision(resolvedName));
            }
            // now search for all node types that are derived from base
            NodeTypeIterator allTypes = ntMgr.getAllNodeTypes();
            while (allTypes.hasNext()) {
                NodeType nt = allTypes.nextNodeType();
                NodeType[] superTypes = nt.getSupertypes();
                if (Arrays.asList(superTypes).contains(base)) {
                    if (nt.isMixin()) {
                        addTypeConstraint(new MixinComparision(nt.getName()));
                    } else {
                        addTypeConstraint(new PrimaryTypeComparision(nt.getName()));
                    }
                }
            }
        } catch (NoSuchNodeTypeException e) {
            // add anyway -> will not match anything
            addTypeConstraint(new PrimaryTypeComparision(resolvedName));
        }
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) NoSuchNodeTypeException(javax.jcr.nodetype.NoSuchNodeTypeException)

Example 32 with NodeTypeIterator

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

the class GQL method resolvePropertyName.

/**
     * Resolves the given property name. If the name has a prefix then the name
     * is returned immediately as is. Otherwise the node type manager is
     * searched for a property definition that defines a named property with
     * a local name that matches the provided <code>name</code>. If such a match
     * is found the name of the property definition is returned.
     *
     * @param name the name of a property (optionally without a prefix).
     * @return the resolved property name.
     * @throws RepositoryException if an error occurs while reading from the
     *                             node type manager.
     */
private String resolvePropertyName(String name) throws RepositoryException {
    if (isPrefixed(name)) {
        return name;
    }
    if (propertyNames == null) {
        propertyNames = new HashMap<String, String>();
        if (session != null) {
            NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
            NodeTypeIterator it = ntMgr.getAllNodeTypes();
            while (it.hasNext()) {
                NodeType nt = it.nextNodeType();
                PropertyDefinition[] defs = nt.getDeclaredPropertyDefinitions();
                for (PropertyDefinition def : defs) {
                    String pn = def.getName();
                    if (!pn.equals("*")) {
                        String localName = pn;
                        int idx = pn.indexOf(':');
                        if (idx != -1) {
                            localName = pn.substring(idx + 1);
                        }
                        propertyNames.put(localName, pn);
                    }
                }
            }
        }
    }
    String pn = propertyNames.get(name);
    if (pn != null) {
        return pn;
    } else {
        return name;
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 33 with NodeTypeIterator

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

the class CndImporter method registerNodeTypes.

/**
     * Registers nodetypes in <code>cnd</code> format.
     * @param cnd  a reader to the cnd. The reader is closed on return.
     * @param systemId  a informative id of the given cnd input.
     * @param nodeTypeManager  the {@link NodeTypeManager} used for creating and registering the
     *     {@link NodeTypeTemplate}s, {@link NodeDefinitionTemplate}s and {@link PropertyDefinitionTemplate}s
     *     defined in the cnd.
     * @param namespaceRegistry  the {@link NamespaceRegistry} used for registering namespaces defined in
     *     the cnd.
     * @param valueFactory  the {@link ValueFactory} used to create
     *     {@link PropertyDefinitionTemplate#setDefaultValues(javax.jcr.Value[]) default value(s)}.
     * @param reregisterExisting  <code>true</code> if existing node types should be re-registered
     *     with those present in the cnd. <code>false</code> otherwise.
     * @return  the registered node types
     *
     * @throws ParseException  if the cnd cannot be parsed
     * @throws InvalidNodeTypeDefinitionException  if a <code>NodeTypeDefinition</code> is invalid.
     * @throws NodeTypeExistsException  if <code>reregisterExisting</code> is <code>false</code> and a
     *     <code>NodeTypeDefinition</code> specifies a node type name that is already registered.
     * @throws UnsupportedRepositoryOperationException  if the <code>NodeTypeManager</code> does not
     *     support node type registration.
     * @throws IOException  if closing the cnd reader fails
     * @throws RepositoryException  if another error occurs.
     */
public static NodeType[] registerNodeTypes(Reader cnd, String systemId, NodeTypeManager nodeTypeManager, NamespaceRegistry namespaceRegistry, ValueFactory valueFactory, boolean reregisterExisting) throws ParseException, InvalidNodeTypeDefinitionException, NodeTypeExistsException, UnsupportedRepositoryOperationException, RepositoryException, IOException {
    try {
        DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(nodeTypeManager, valueFactory, namespaceRegistry);
        CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(cnd, systemId, factory);
        Map<String, NodeTypeTemplate> templates = new HashMap<String, NodeTypeTemplate>();
        for (NodeTypeTemplate template : cndReader.getNodeTypeDefinitions()) {
            templates.put(template.getName(), template);
        }
        List<NodeTypeTemplate> toRegister = new ArrayList<NodeTypeTemplate>(templates.size());
        for (NodeTypeTemplate ntt : templates.values()) {
            if (reregisterExisting || !nodeTypeManager.hasNodeType(ntt.getName())) {
                ensureNtBase(ntt, templates, nodeTypeManager);
                toRegister.add(ntt);
            }
        }
        NodeTypeIterator registered = nodeTypeManager.registerNodeTypes(toRegister.toArray(new NodeTypeTemplate[toRegister.size()]), true);
        return toArray(registered);
    } finally {
        cnd.close();
    }
}
Also used : NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) NamespaceRegistry(javax.jcr.NamespaceRegistry) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator)

Example 34 with NodeTypeIterator

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

the class SetValueConstraintViolationExceptionTest method testMultipleReferenceProperty.

/**
     * Tests if setValue(Value[] values) where values are of type ReferenceValue
     * throw a ConstraintViolationException if the change would violate a value
     * constraint
     */
public void testMultipleReferenceProperty() throws NotExecutableException, RepositoryException {
    // locate a PropertyDefinition with ValueConstraints
    PropertyDefinition propDef = NodeTypeUtil.locatePropertyDef(superuser, PropertyType.REFERENCE, true, false, true, false);
    if (propDef == null) {
        throw new NotExecutableException("No multiple reference property def with " + "testable value constraints has been found");
    }
    String[] valueConstraints = propDef.getValueConstraints();
    if (valueConstraints == null || valueConstraints.length == 0) {
        throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
    }
    List<String> constraints = Arrays.asList(valueConstraints);
    String nodeTypeSatisfied = constraints.get(0);
    String nodeTypeNotSatisfied = null;
    NodeTypeManager manager = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeIterator types = manager.getAllNodeTypes();
    // find a NodeType which is not satisfying the constraints
    while (types.hasNext()) {
        NodeType type = types.nextNodeType();
        String name = type.getName();
        if (constraints.contains(name) || ntFrozenNode.equals(name)) {
            continue;
        }
        if (type.getChildNodeDefinitions() != null && type.getChildNodeDefinitions().length > 0) {
            continue;
        }
        nodeTypeNotSatisfied = name;
        break;
    }
    if (nodeTypeNotSatisfied == null) {
        throw new NotExecutableException("No reference property def with " + "testable value constraints has been found");
    }
    // create a sub node of testRootNode of type propDef.getDeclaringNodeType()
    // and add a property with constraints to this node
    Node node;
    Property prop;
    Node nodeSatisfied;
    Node nodeNotSatisfied;
    try {
        String nodeType = propDef.getDeclaringNodeType().getName();
        node = testRootNode.addNode(nodeName2, nodeType);
        // create a referenceable node satisfying the constraint
        nodeSatisfied = testRootNode.addNode(nodeName3, nodeTypeSatisfied);
        ensureMixinType(nodeSatisfied, mixReferenceable);
        // create a referenceable node not satisfying the constraint
        nodeNotSatisfied = testRootNode.addNode(nodeName4, nodeTypeNotSatisfied);
        ensureMixinType(nodeNotSatisfied, mixReferenceable);
        // some implementations may require a save after addMixin()
        testRootNode.getSession().save();
        Value valueSatisfied = superuser.getValueFactory().createValue(nodeSatisfied);
        prop = node.setProperty(propDef.getName(), new Value[] { valueSatisfied });
        testRootNode.getSession().save();
    } catch (ConstraintViolationException e) {
        // implementation specific constraints do not allow to set up test environment
        throw new NotExecutableException("Not able to create required test items.");
    }
    // test of signature setValue(Value value)
    try {
        Value valueNotSatisfied = superuser.getValueFactory().createValue(nodeNotSatisfied);
        prop.setValue(new Value[] { valueNotSatisfied });
        node.save();
        fail("setValue(Value[] values) must throw a ConstraintViolationException " + "if the change would violate a node type constraint " + "either immediately or on save");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeType(javax.jcr.nodetype.NodeType) Node(javax.jcr.Node) Value(javax.jcr.Value) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Property(javax.jcr.Property)

Example 35 with NodeTypeIterator

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

the class LuceneQueryBuilder method visit.

public Object visit(NodeTypeQueryNode node, Object data) {
    List<Term> terms = new ArrayList<Term>();
    try {
        String mixinTypesField = resolver.getJCRName(NameConstants.JCR_MIXINTYPES);
        String primaryTypeField = resolver.getJCRName(NameConstants.JCR_PRIMARYTYPE);
        NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
        NodeType base = ntMgr.getNodeType(session.getJCRName(node.getValue()));
        if (base.isMixin()) {
            // search for nodes where jcr:mixinTypes is set to this mixin
            Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, resolver.getJCRName(node.getValue())));
            terms.add(t);
        } else {
            // search for nodes where jcr:primaryType is set to this type
            Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, resolver.getJCRName(node.getValue())));
            terms.add(t);
        }
        // now search for all node types that are derived from base
        NodeTypeIterator allTypes = ntMgr.getAllNodeTypes();
        while (allTypes.hasNext()) {
            NodeType nt = allTypes.nextNodeType();
            NodeType[] superTypes = nt.getSupertypes();
            if (Arrays.asList(superTypes).contains(base)) {
                Name n = session.getQName(nt.getName());
                String ntName = nsMappings.translateName(n);
                Term t;
                if (nt.isMixin()) {
                    // search on jcr:mixinTypes
                    t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(mixinTypesField, ntName));
                } else {
                    // search on jcr:primaryType
                    t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(primaryTypeField, ntName));
                }
                terms.add(t);
            }
        }
    } catch (NameException e) {
        exceptions.add(e);
    } catch (RepositoryException e) {
        exceptions.add(e);
    }
    if (terms.size() == 0) {
        // exception occured
        return new BooleanQuery();
    } else if (terms.size() == 1) {
        return new JackrabbitTermQuery(terms.get(0));
    } else {
        BooleanQuery b = new BooleanQuery();
        for (Term term : terms) {
            b.add(new JackrabbitTermQuery(term), Occur.SHOULD);
        }
        return b;
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) ArrayList(java.util.ArrayList) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) RepositoryException(javax.jcr.RepositoryException) Term(org.apache.lucene.index.Term) Name(org.apache.jackrabbit.spi.Name) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeType(javax.jcr.nodetype.NodeType)

Aggregations

NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)65 NodeType (javax.jcr.nodetype.NodeType)53 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)28 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)13 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 Node (javax.jcr.Node)9 RepositoryException (javax.jcr.RepositoryException)9 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)8 ArrayList (java.util.ArrayList)6 Session (javax.jcr.Session)6 HashSet (java.util.HashSet)4 Value (javax.jcr.Value)3 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)3 NodeTypeIteratorAdapter (org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Property (javax.jcr.Property)2