Search in sources :

Example 76 with ConstraintViolationException

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

the class NodeTypeCreationTest method testPropertyDefinitionTemplate.

public void testPropertyDefinitionTemplate() throws Exception {
    PropertyDefinitionTemplate pdt = createBooleanPropTemplate();
    assertEquals(jcrPropName, pdt.getName());
    try {
        pdt.setName(null);
        fail("null isn't a valid JCR name");
    } catch (ConstraintViolationException e) {
    // success
    }
    assertEquals(false, pdt.isAutoCreated());
    assertEquals(false, pdt.isMandatory());
    assertEquals(OnParentVersionAction.IGNORE, pdt.getOnParentVersion());
    assertEquals(false, pdt.isProtected());
    assertEquals(PropertyType.BOOLEAN, pdt.getRequiredType());
    assertEquals(null, pdt.getValueConstraints());
    assertEquals(null, pdt.getDefaultValues());
    assertEquals(false, pdt.isMultiple());
    String[] qo = pdt.getAvailableQueryOperators();
    assertEquals(1, qo.length);
    assertEquals(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, qo[0]);
    assertEquals(false, pdt.isFullTextSearchable());
    assertEquals(false, pdt.isQueryOrderable());
}
Also used : PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 77 with ConstraintViolationException

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

the class NodeTypeCreationTest method testInvalidJCRNames.

public void testInvalidJCRNames() throws Exception {
    String invalidName = ":ab[2]";
    // invalid name(s) passed to NT-template methods
    NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
    try {
        ntt.setName(invalidName);
        fail("ConstraintViolationException expected. Nt-name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ntt.setDeclaredSuperTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
        fail("ConstraintViolationException expected. One of the super type names is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ntt.setPrimaryItemName(invalidName);
        fail("ConstraintViolationException expected. Primary item name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    // invalid name(s) passed to NodeDefinitionTemplate
    NodeDefinitionTemplate ndt = ntm.createNodeDefinitionTemplate();
    try {
        ndt.setName(invalidName);
        fail("ConstraintViolationException expected. Name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ndt.setRequiredPrimaryTypeNames(new String[] { "{" + NS_MIX_URI + "}" + "littlemixin", invalidName });
        fail("ConstraintViolationException expected. One of the required primary type names is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        ndt.setDefaultPrimaryTypeName(invalidName);
        fail("ConstraintViolationException expected. Default primary type name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
    // invalid name(s) passed to PropertyDefinitionTemplate
    PropertyDefinitionTemplate pdt = ntm.createPropertyDefinitionTemplate();
    try {
        pdt.setName(invalidName);
        fail("ConstraintViolationException expected. Name is invalid");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NodeDefinitionTemplate(javax.jcr.nodetype.NodeDefinitionTemplate) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 78 with ConstraintViolationException

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

the class NodeTypeRegistryImpl method getEffectiveNodeType.

/**
 * @see EffectiveNodeTypeProvider#getEffectiveNodeType(QNodeTypeDefinition, Map)
 */
public EffectiveNodeType getEffectiveNodeType(QNodeTypeDefinition ntd, Map<Name, QNodeTypeDefinition> ntdMap) throws ConstraintViolationException, NoSuchNodeTypeException {
    TreeSet<Name> mergedNodeTypes = new TreeSet<Name>();
    TreeSet<Name> inheritedNodeTypes = new TreeSet<Name>();
    TreeSet<Name> allNodeTypes = new TreeSet<Name>();
    Map<Name, List<QItemDefinition>> namedItemDefs = new HashMap<Name, List<QItemDefinition>>();
    List<QItemDefinition> unnamedItemDefs = new ArrayList<QItemDefinition>();
    Set<Name> supportedMixins = null;
    Name ntName = ntd.getName();
    // prepare new instance
    mergedNodeTypes.add(ntName);
    allNodeTypes.add(ntName);
    Name[] smixins = ntd.getSupportedMixinTypes();
    if (smixins != null) {
        supportedMixins = new HashSet<Name>();
        supportedMixins.addAll(Arrays.asList(smixins));
    }
    // map of all item definitions (maps id to definition)
    // used to effectively detect ambiguous child definitions where
    // ambiguity is defined in terms of definition identity
    Set<QItemDefinition> itemDefIds = new HashSet<QItemDefinition>();
    for (QNodeDefinition nd : ntd.getChildNodeDefs()) {
        // this node type definition
        if (itemDefIds.contains(nd)) {
            // conflict
            String msg;
            if (nd.definesResidual()) {
                msg = ntName + " contains ambiguous residual child node definitions";
            } else {
                msg = ntName + " contains ambiguous definitions for child node named " + nd.getName();
            }
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        } else {
            itemDefIds.add(nd);
        }
        if (nd.definesResidual()) {
            // residual node definition
            unnamedItemDefs.add(nd);
        } else {
            // named node definition
            Name name = nd.getName();
            List<QItemDefinition> defs = namedItemDefs.get(name);
            if (defs == null) {
                defs = new ArrayList<QItemDefinition>();
                namedItemDefs.put(name, defs);
            }
            if (defs.size() > 0) {
                /**
                 * there already exists at least one definition with that
                 * name; make sure none of them is auto-create
                 */
                for (QItemDefinition qDef : defs) {
                    if (nd.isAutoCreated() || qDef.isAutoCreated()) {
                        // conflict
                        String msg = "There are more than one 'auto-create' item definitions for '" + name + "' in node type '" + ntName + "'";
                        log.debug(msg);
                        throw new ConstraintViolationException(msg);
                    }
                }
            }
            defs.add(nd);
        }
    }
    for (QPropertyDefinition pd : ntd.getPropertyDefs()) {
        // this node type definition
        if (itemDefIds.contains(pd)) {
            // conflict
            String msg;
            if (pd.definesResidual()) {
                msg = ntName + " contains ambiguous residual property definitions";
            } else {
                msg = ntName + " contains ambiguous definitions for property named " + pd.getName();
            }
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        } else {
            itemDefIds.add(pd);
        }
        if (pd.definesResidual()) {
            // residual property definition
            unnamedItemDefs.add(pd);
        } else {
            // named property definition
            Name name = pd.getName();
            List<QItemDefinition> defs = namedItemDefs.get(name);
            if (defs == null) {
                defs = new ArrayList<QItemDefinition>();
                namedItemDefs.put(name, defs);
            }
            if (defs.size() > 0) {
                /**
                 * there already exists at least one definition with that
                 * name; make sure none of them is auto-create
                 */
                for (QItemDefinition qDef : defs) {
                    if (pd.isAutoCreated() || qDef.isAutoCreated()) {
                        // conflict
                        String msg = "There are more than one 'auto-create' item definitions for '" + name + "' in node type '" + ntName + "'";
                        log.debug(msg);
                        throw new ConstraintViolationException(msg);
                    }
                }
            }
            defs.add(pd);
        }
    }
    // create empty effective node type instance
    EffectiveNodeTypeImpl ent = new EffectiveNodeTypeImpl(mergedNodeTypes, inheritedNodeTypes, allNodeTypes, namedItemDefs, unnamedItemDefs, supportedMixins);
    // resolve supertypes recursively
    Name[] supertypes = ntd.getSupertypes();
    if (supertypes.length > 0) {
        EffectiveNodeTypeImpl effSuperType = (EffectiveNodeTypeImpl) getEffectiveNodeType(supertypes, ntdMap);
        ent.internalMerge(effSuperType, true);
    }
    return ent;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) Name(org.apache.jackrabbit.spi.Name) TreeSet(java.util.TreeSet) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 79 with ConstraintViolationException

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

the class ItemDefinitionProviderImpl method getMatchingPropDef.

private static QPropertyDefinition getMatchingPropDef(QPropertyDefinition[] defs, int type, boolean multiValued, boolean throwWhenAmbiguous) throws ConstraintViolationException {
    QPropertyDefinition match = null;
    for (int i = 0; i < defs.length; i++) {
        QItemDefinition qDef = defs[i];
        if (!qDef.definesNode()) {
            QPropertyDefinition pd = (QPropertyDefinition) qDef;
            int reqType = pd.getRequiredType();
            // match type
            if (reqType == PropertyType.UNDEFINED || type == PropertyType.UNDEFINED || reqType == type) {
                // match multiValued flag
                if (multiValued == pd.isMultiple()) {
                    // found match
                    if (pd.getRequiredType() != PropertyType.UNDEFINED) {
                        if (match != null && throwWhenAmbiguous) {
                            throw new ConstraintViolationException("ambiguous property definitions found: " + match + " vs " + pd);
                        }
                        // thus obviously specified a String.
                        if (match == null || match.getRequiredType() != PropertyType.STRING) {
                            // found best possible match
                            match = pd;
                        }
                    } else {
                        if (match == null) {
                            match = pd;
                        }
                    }
                }
            }
        }
    }
    return match;
}
Also used : QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition)

Example 80 with ConstraintViolationException

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

the class StringConstraint method check.

/**
 * @see org.apache.jackrabbit.spi.QValueConstraint#check(QValue)
 */
public void check(QValue value) throws ConstraintViolationException, RepositoryException {
    if (value == null) {
        throw new ConstraintViolationException("null value does not satisfy the constraint '" + getString() + "'");
    }
    switch(value.getType()) {
        case PropertyType.STRING:
        case PropertyType.URI:
            String text = value.getString();
            Matcher matcher = pattern.matcher(text);
            if (!matcher.matches()) {
                throw new ConstraintViolationException("'" + text + "' does not satisfy the constraint '" + getString() + "'");
            }
            return;
        default:
            String msg = "String constraint can not be applied to value of type: " + PropertyType.nameFromValue(value.getType());
            log.debug(msg);
            throw new RepositoryException(msg);
    }
}
Also used : Matcher(java.util.regex.Matcher) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException)

Aggregations

ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)180 Node (javax.jcr.Node)74 RepositoryException (javax.jcr.RepositoryException)40 Name (org.apache.jackrabbit.spi.Name)33 Value (javax.jcr.Value)31 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)28 Test (org.junit.Test)27 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)23 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)23 Session (javax.jcr.Session)18 ItemExistsException (javax.jcr.ItemExistsException)17 NodeState (org.apache.jackrabbit.core.state.NodeState)16 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)16 Property (javax.jcr.Property)14 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 ArrayList (java.util.ArrayList)13 NodeId (org.apache.jackrabbit.core.id.NodeId)13 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)13 NodeType (javax.jcr.nodetype.NodeType)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)12