Search in sources :

Example 1 with QPropertyDefinitionBuilder

use of org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder in project jackrabbit by apache.

the class DefinitionUtil method createQPropertyDefinition.

/**
     * 
     * @param declaringNodeType
     * @param pdefElement
     * @param resolver
     * @param qValueFactory
     * @return
     * @throws RepositoryException
     */
static QPropertyDefinition createQPropertyDefinition(Name declaringNodeType, Element pdefElement, NamePathResolver resolver, QValueFactory qValueFactory) throws RepositoryException {
    QPropertyDefinitionBuilder builder = new QPropertyDefinitionBuilder();
    buildQItemDefinition(declaringNodeType, pdefElement, resolver, builder);
    if (pdefElement.hasAttribute(REQUIREDTYPE_ATTRIBUTE)) {
        builder.setRequiredType(PropertyType.valueFromName(pdefElement.getAttribute(REQUIREDTYPE_ATTRIBUTE)));
    }
    if (pdefElement.hasAttribute(MULTIPLE_ATTRIBUTE)) {
        builder.setMultiple(Boolean.valueOf(pdefElement.getAttribute(MULTIPLE_ATTRIBUTE)));
    }
    if (pdefElement.hasAttribute(FULL_TEXT_SEARCHABLE_ATTRIBUTE)) {
        builder.setFullTextSearchable(Boolean.valueOf(pdefElement.getAttribute(FULL_TEXT_SEARCHABLE_ATTRIBUTE)));
    }
    if (pdefElement.hasAttribute(QUERY_ORDERABLE_ATTRIBUTE)) {
        builder.setQueryOrderable(Boolean.valueOf(pdefElement.getAttribute(QUERY_ORDERABLE_ATTRIBUTE)));
    }
    int requiredType = builder.getRequiredType();
    Element child = DomUtil.getChildElement(pdefElement, DEFAULTVALUES_ELEMENT, null);
    if (child != null) {
        ElementIterator it = DomUtil.getChildren(child, DEFAULTVALUE_ELEMENT, null);
        while (it.hasNext()) {
            String jcrVal = DomUtil.getText(it.nextElement());
            if (jcrVal == null) {
                jcrVal = "";
            }
            QValue qValue;
            if (requiredType == PropertyType.BINARY) {
                // TODO: improve
                Value v = new ValueFactoryQImpl(qValueFactory, resolver).createValue(jcrVal, requiredType);
                qValue = ValueFormat.getQValue(v, resolver, qValueFactory);
            } else {
                qValue = ValueFormat.getQValue(jcrVal, requiredType, resolver, qValueFactory);
            }
            builder.addDefaultValue(qValue);
        }
    }
    // else: no default values defined.
    child = DomUtil.getChildElement(pdefElement, VALUECONSTRAINTS_ELEMENT, null);
    if (child != null) {
        ElementIterator it = DomUtil.getChildren(child, VALUECONSTRAINT_ELEMENT, null);
        while (it.hasNext()) {
            String qValue = DomUtil.getText(it.nextElement());
            // in case of name and path constraint, the value must be
            // converted to SPI values
            // TODO: tobefixed. path-constraint may contain trailing *
            builder.addValueConstraint(ValueConstraint.create(requiredType, qValue, resolver));
        }
    }
    child = DomUtil.getChildElement(pdefElement, AVAILABLE_QUERY_OPERATORS_ELEMENT, null);
    if (child == null) {
        builder.setAvailableQueryOperators(new String[0]);
    } else {
        List<String> names = new ArrayList<String>();
        ElementIterator it = DomUtil.getChildren(child, AVAILABLE_QUERY_OPERATOR_ELEMENT, null);
        while (it.hasNext()) {
            String str = DomUtil.getText(it.nextElement());
            names.add(str);
        }
        builder.setAvailableQueryOperators(names.toArray(new String[names.size()]));
    }
    return builder.build();
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) QValue(org.apache.jackrabbit.spi.QValue) QPropertyDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder) Element(org.w3c.dom.Element) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) ArrayList(java.util.ArrayList) ValueFactoryQImpl(org.apache.jackrabbit.spi.commons.value.ValueFactoryQImpl) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)

Example 2 with QPropertyDefinitionBuilder

use of org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder in project jackrabbit by apache.

the class NodeTypeReader method getNodeTypeDef.

/**
     * Returns the node type definition specified by the current element.
     *
     * @return node type definition
     * @throws InvalidNodeTypeDefException if the definition is invalid
     * @throws NameException               if the definition contains an
     *                                     illegal name
     * @throws NamespaceException if a namespace is not defined
     */
private QNodeTypeDefinition getNodeTypeDef() throws InvalidNodeTypeDefException, NameException, NamespaceException {
    QNodeTypeDefinitionBuilder type = new QNodeTypeDefinitionBuilder();
    type.setName(resolver.getQName(walker.getAttribute(Constants.NAME_ATTRIBUTE)));
    type.setMixin(Boolean.valueOf(walker.getAttribute(Constants.ISMIXIN_ATTRIBUTE)));
    type.setOrderableChildNodes(Boolean.valueOf(walker.getAttribute(Constants.HASORDERABLECHILDNODES_ATTRIBUTE)));
    type.setAbstract(Boolean.valueOf(walker.getAttribute(Constants.ISABSTRACT_ATTRIBUTE)));
    if (walker.getAttribute(Constants.ISQUERYABLE_ATTRIBUTE) != null) {
        type.setQueryable(Boolean.valueOf(walker.getAttribute(Constants.ISQUERYABLE_ATTRIBUTE)));
    }
    String primaryItemName = walker.getAttribute(Constants.PRIMARYITEMNAME_ATTRIBUTE);
    if (primaryItemName != null && primaryItemName.length() > 0) {
        type.setPrimaryItemName(resolver.getQName(primaryItemName));
    }
    // supertype declarations
    if (walker.enterElement(Constants.SUPERTYPES_ELEMENT)) {
        List<Name> supertypes = new ArrayList<Name>();
        while (walker.iterateElements(Constants.SUPERTYPE_ELEMENT)) {
            supertypes.add(resolver.getQName(walker.getContent()));
        }
        type.setSupertypes(supertypes.toArray(new Name[supertypes.size()]));
        walker.leaveElement();
    }
    // property definitions
    List<QPropertyDefinition> properties = new ArrayList<QPropertyDefinition>();
    while (walker.iterateElements(Constants.PROPERTYDEFINITION_ELEMENT)) {
        QPropertyDefinitionBuilder def = getPropDef();
        def.setDeclaringNodeType(type.getName());
        properties.add(def.build());
    }
    type.setPropertyDefs(properties.toArray(new QPropertyDefinition[properties.size()]));
    // child node definitions
    List<QNodeDefinition> nodes = new ArrayList<QNodeDefinition>();
    while (walker.iterateElements(Constants.CHILDNODEDEFINITION_ELEMENT)) {
        QNodeDefinitionBuilder def = getChildNodeDef();
        def.setDeclaringNodeType(type.getName());
        nodes.add(def.build());
    }
    type.setChildNodeDefs(nodes.toArray(new QNodeDefinition[nodes.size()]));
    return type.build();
}
Also used : QNodeDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QNodeDefinitionBuilder) QPropertyDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ArrayList(java.util.ArrayList) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) QNodeTypeDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QNodeTypeDefinitionBuilder) Name(org.apache.jackrabbit.spi.Name)

Example 3 with QPropertyDefinitionBuilder

use of org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder in project jackrabbit by apache.

the class NodeTypeReader method getPropDef.

/**
     * Returns the property definition specified by the current element.
     *
     * @return property definition
     * @throws InvalidNodeTypeDefException if the definition is invalid
     * @throws NameException               if the definition contains an
     *                                     illegal name
     * @throws NamespaceException if a namespace is not defined
     */
private QPropertyDefinitionBuilder getPropDef() throws InvalidNodeTypeDefException, NameException, NamespaceException {
    QPropertyDefinitionBuilder def = new QPropertyDefinitionBuilder();
    String name = walker.getAttribute(Constants.NAME_ATTRIBUTE);
    if (name.equals("*")) {
        def.setName(NameConstants.ANY_NAME);
    } else {
        def.setName(resolver.getQName(name));
    }
    // simple attributes
    def.setAutoCreated(Boolean.valueOf(walker.getAttribute(Constants.AUTOCREATED_ATTRIBUTE)));
    def.setMandatory(Boolean.valueOf(walker.getAttribute(Constants.MANDATORY_ATTRIBUTE)));
    def.setProtected(Boolean.valueOf(walker.getAttribute(Constants.PROTECTED_ATTRIBUTE)));
    def.setOnParentVersion(OnParentVersionAction.valueFromName(walker.getAttribute(Constants.ONPARENTVERSION_ATTRIBUTE)));
    def.setMultiple(Boolean.valueOf(walker.getAttribute(Constants.MULTIPLE_ATTRIBUTE)));
    def.setFullTextSearchable(Boolean.valueOf(walker.getAttribute(Constants.ISFULLTEXTSEARCHABLE_ATTRIBUTE)));
    def.setQueryOrderable(Boolean.valueOf(walker.getAttribute(Constants.ISQUERYORDERABLE_ATTRIBUTE)));
    String s = walker.getAttribute(Constants.AVAILABLEQUERYOPERATORS_ATTRIBUTE);
    if (s != null && s.length() > 0) {
        String[] ops = s.split(" ");
        List<String> queryOps = new ArrayList<String>();
        for (String op1 : ops) {
            String op = op1.trim();
            if (op.equals(Constants.EQ_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO);
            } else if (op.equals(Constants.NE_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_NOT_EQUAL_TO);
            } else if (op.equals(Constants.LT_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN);
            } else if (op.equals(Constants.LE_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_LESS_THAN_OR_EQUAL_TO);
            } else if (op.equals(Constants.GT_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN);
            } else if (op.equals(Constants.GE_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_GREATER_THAN_OR_EQUAL_TO);
            } else if (op.equals(Constants.LIKE_ENTITY)) {
                queryOps.add(QueryObjectModelConstants.JCR_OPERATOR_LIKE);
            } else {
                throw new InvalidNodeTypeDefException("'" + op + "' is not a valid query operator");
            }
        }
        def.setAvailableQueryOperators(queryOps.toArray(new String[queryOps.size()]));
    }
    def.setRequiredType(PropertyType.valueFromName(walker.getAttribute(Constants.REQUIREDTYPE_ATTRIBUTE)));
    // value constraints
    if (walker.enterElement(Constants.VALUECONSTRAINTS_ELEMENT)) {
        List<QValueConstraint> constraints = new ArrayList<QValueConstraint>();
        int type = def.getRequiredType();
        while (walker.iterateElements(Constants.VALUECONSTRAINT_ELEMENT)) {
            String constraint = walker.getContent();
            try {
                constraints.add(ValueConstraint.create(type, constraint.trim(), resolver));
            } catch (InvalidConstraintException e) {
                throw new InvalidNodeTypeDefException("Invalid value constraint " + constraint, e);
            }
        }
        def.setValueConstraints(constraints.toArray(new QValueConstraint[constraints.size()]));
        walker.leaveElement();
    }
    // default values
    if (walker.enterElement(Constants.DEFAULTVALUES_ELEMENT)) {
        List<InternalValue> values = new ArrayList<InternalValue>();
        int type = def.getRequiredType();
        if (type == PropertyType.UNDEFINED) {
            type = PropertyType.STRING;
        }
        while (walker.iterateElements(Constants.DEFAULTVALUE_ELEMENT)) {
            String value = walker.getContent();
            try {
                Value v = ValueHelper.convert(value, type, valueFactory);
                values.add((InternalValue) ValueFormat.getQValue(v, resolver, qValueFactory));
            } catch (RepositoryException e) {
                throw new InvalidNodeTypeDefException("Unable to create default value: " + value, e);
            }
        }
        def.setDefaultValues(values.toArray(new InternalValue[values.size()]));
        walker.leaveElement();
    }
    return def;
}
Also used : InvalidConstraintException(org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException) QPropertyDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) InternalValue(org.apache.jackrabbit.core.value.InternalValue) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint) QValueConstraint(org.apache.jackrabbit.spi.QValueConstraint) InvalidNodeTypeDefException(org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) Value(javax.jcr.Value)

Example 4 with QPropertyDefinitionBuilder

use of org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder in project jackrabbit by apache.

the class CyclicNodeTypeRegistrationTest method testRegisterCyclicChildNodeTypesAndSupertypes.

/**
     * Basically a test of a Graffito use case.
     */
public void testRegisterCyclicChildNodeTypesAndSupertypes() {
    /**
         * Constructs node types with the following structure:
         * [Folder] > CmsObject
         * + folders (Folder)
         * + documents (Document)
         *
         * [CmsObject]
         * + parentFolder (Folder)
         *
         * [Document] > CmsObject
         * - size (long)
         *
         */
    final QNodeTypeDefinitionBuilder folder = new QNodeTypeDefinitionBuilder();
    folder.setName(nameFactory.create("", "Folder"));
    final QNodeTypeDefinitionBuilder cmsObject = new QNodeTypeDefinitionBuilder();
    cmsObject.setName(nameFactory.create("", "CmsObject"));
    cmsObject.setSupertypes(new Name[] { NameConstants.NT_BASE });
    QNodeDefinitionBuilder parentFolder = new QNodeDefinitionBuilder();
    parentFolder.setRequiredPrimaryTypes(new Name[] { folder.getName() });
    parentFolder.setName(nameFactory.create("", "parentFolder"));
    parentFolder.setDeclaringNodeType(cmsObject.getName());
    cmsObject.setChildNodeDefs(new QNodeDefinition[] { parentFolder.build() });
    final QNodeTypeDefinitionBuilder document = new QNodeTypeDefinitionBuilder();
    document.setName(nameFactory.create("", "Document"));
    document.setSupertypes(new Name[] { cmsObject.getName() });
    QPropertyDefinitionBuilder sizeProp = new QPropertyDefinitionBuilder();
    sizeProp.setName(nameFactory.create("", "size"));
    sizeProp.setRequiredType(PropertyType.LONG);
    sizeProp.setDeclaringNodeType(document.getName());
    document.setPropertyDefs(new QPropertyDefinition[] { sizeProp.build() });
    folder.setSupertypes(new Name[] { cmsObject.getName() });
    QNodeDefinitionBuilder folders = new QNodeDefinitionBuilder();
    folders.setRequiredPrimaryTypes(new Name[] { folder.getName() });
    folders.setName(nameFactory.create("", "folders"));
    folders.setDeclaringNodeType(folder.getName());
    QNodeDefinitionBuilder documents = new QNodeDefinitionBuilder();
    documents.setRequiredPrimaryTypes(new Name[] { document.getName() });
    documents.setName(nameFactory.create("", "documents"));
    documents.setDeclaringNodeType(folder.getName());
    folder.setChildNodeDefs(new QNodeDefinition[] { folders.build(), documents.build() });
    ntDefCollection = new LinkedList<QNodeTypeDefinition>();
    ntDefCollection.add(folder.build());
    ntDefCollection.add(document.build());
    ntDefCollection.add(cmsObject.build());
    try {
        ntreg.registerNodeTypes(ntDefCollection);
    } catch (InvalidNodeTypeDefException e) {
        assertFalse(e.getMessage(), true);
        e.printStackTrace();
    } catch (RepositoryException e) {
        assertFalse(e.getMessage(), true);
        e.printStackTrace();
    }
    boolean allNTsAreRegistered = ntreg.isRegistered(folder.getName()) && ntreg.isRegistered(document.getName()) && ntreg.isRegistered(cmsObject.getName());
    assertTrue(allNTsAreRegistered);
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) QNodeDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QNodeDefinitionBuilder) QPropertyDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder) RepositoryException(javax.jcr.RepositoryException) QNodeTypeDefinitionBuilder(org.apache.jackrabbit.spi.commons.nodetype.QNodeTypeDefinitionBuilder)

Aggregations

QPropertyDefinitionBuilder (org.apache.jackrabbit.spi.commons.nodetype.QPropertyDefinitionBuilder)4 ArrayList (java.util.ArrayList)3 RepositoryException (javax.jcr.RepositoryException)2 Value (javax.jcr.Value)2 QNodeDefinitionBuilder (org.apache.jackrabbit.spi.commons.nodetype.QNodeDefinitionBuilder)2 QNodeTypeDefinitionBuilder (org.apache.jackrabbit.spi.commons.nodetype.QNodeTypeDefinitionBuilder)2 ValueConstraint (org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)2 InvalidNodeTypeDefException (org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException)1 InternalValue (org.apache.jackrabbit.core.value.InternalValue)1 Name (org.apache.jackrabbit.spi.Name)1 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)1 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)1 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)1 QValue (org.apache.jackrabbit.spi.QValue)1 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)1 InvalidConstraintException (org.apache.jackrabbit.spi.commons.nodetype.InvalidConstraintException)1 ValueFactoryQImpl (org.apache.jackrabbit.spi.commons.value.ValueFactoryQImpl)1 ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)1 Element (org.w3c.dom.Element)1