use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class AddNodeTest method testConstraintViolation.
/**
* Tests if a ConstraintViolationException is thrown when one attempts
* to add a node at a path that references a property.
*/
public void testConstraintViolation() throws RepositoryException {
try {
Node rootNode = superuser.getRootNode();
String propPath = testPath + "/" + jcrPrimaryType;
rootNode.addNode(propPath + "/" + nodeName1, testNodeType);
fail("Expected ConstraintViolationException.");
} catch (ConstraintViolationException e) {
// correct.
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class AddNodeTest method testAbstractNodeType.
/**
* Tests if addNode() throws a ConstraintViolationException in case
* of an abstract node type.
*/
public void testAbstractNodeType() throws RepositoryException {
NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
NodeTypeIterator nts = ntMgr.getPrimaryNodeTypes();
while (nts.hasNext()) {
NodeType nt = nts.nextNodeType();
if (nt.isAbstract()) {
try {
testRootNode.addNode(nodeName1, nt.getName());
superuser.save();
fail("Expected ConstraintViolationException.");
} catch (ConstraintViolationException e) {
// correct.
} finally {
superuser.refresh(false);
}
}
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeTypeImpl method canSetProperty.
/**
* {@inheritDoc}
*/
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 (Value value : values) {
if (value == null) {
// skip null values as those would be purged
continue;
}
if (type == PropertyType.UNDEFINED) {
type = value.getType();
} else if (type != value.getType()) {
// inhomogeneous types
return false;
}
}
QPropertyDefinition def;
try {
// try to get definition that matches the given value type
def = ent.getApplicablePropertyDef(name, type, true);
} catch (ConstraintViolationException cve) {
// fallback: ignore type
def = ent.getApplicablePropertyDef(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;
}
List<InternalValue> list = new ArrayList<InternalValue>();
// convert values and compact array (purge null entries)
for (Value value : values) {
if (value != null) {
// perform type conversion as necessary and create InternalValue
// from (converted) Value
InternalValue internalValue;
if (targetType != type) {
// type conversion required
Value targetVal = ValueHelper.convert(value, targetType, valueFactory);
internalValue = InternalValue.create(targetVal, resolver, store);
} else {
// no type conversion required
internalValue = InternalValue.create(value, resolver, store);
}
list.add(internalValue);
}
}
InternalValue[] internalValues = list.toArray(new InternalValue[list.size()]);
EffectiveNodeType.checkSetPropertyValueConstraints(def, internalValues);
return true;
} catch (NameException be) {
// implementation specific exception, fall through
} catch (RepositoryException re) {
// fall through
}
return false;
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class EffectiveNodeType method getApplicableChildNodeDef.
/**
* Returns the applicable child node definition for a child node with the
* specified name and node type. If there are multiple applicable definitions
* named definitions will take precedence over residual definitions.
*
* @param name
* @param nodeTypeName
* @param ntReg
* @return
* @throws NoSuchNodeTypeException
* @throws ConstraintViolationException if no applicable child node definition
* could be found
*/
public QNodeDefinition getApplicableChildNodeDef(Name name, Name nodeTypeName, NodeTypeRegistry ntReg) throws NoSuchNodeTypeException, ConstraintViolationException {
EffectiveNodeType entTarget;
if (nodeTypeName != null) {
entTarget = ntReg.getEffectiveNodeType(nodeTypeName);
} else {
entTarget = null;
}
// try named node definitions first
QItemDefinition[] defs = getNamedItemDefs(name);
for (QItemDefinition def : defs) {
if (def.definesNode()) {
QNodeDefinition nd = (QNodeDefinition) def;
Name[] types = nd.getRequiredPrimaryTypes();
// node definition with that name exists
if (entTarget != null && types != null) {
// check 'required primary types' constraint
if (entTarget.includesNodeTypes(types)) {
// found named node definition
return nd;
}
} else if (nd.getDefaultPrimaryType() != null) {
// found node definition with default node type
return nd;
}
}
}
// no item with that name defined;
// try residual node definitions
QNodeDefinition[] nda = getUnnamedNodeDefs();
for (QNodeDefinition nd : nda) {
if (entTarget != null && nd.getRequiredPrimaryTypes() != null) {
// check 'required primary types' constraint
if (!entTarget.includesNodeTypes(nd.getRequiredPrimaryTypes())) {
continue;
}
// found residual node definition
return nd;
} else {
// it must be determined from the default node type;
if (nd.getDefaultPrimaryType() != null) {
// found residual node definition with default node type
return nd;
}
}
}
// no applicable definition found
throw new ConstraintViolationException("no matching child node definition found for " + name);
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class EffectiveNodeType method checkSetPropertyValueConstraints.
/**
* Tests if the value constraints defined in the property definition
* <code>pd</code> are satisfied by the the specified <code>values</code>.
* <p>
* Note that the <i>protected</i> flag is not checked. Also note that no
* type conversions are attempted if the type of the given values does not
* match the required type as specified in the given definition.
*
* @param pd The definiton of the property
* @param values An array of <code>InternalValue</code> objects.
* @throws ConstraintViolationException if the value constraints defined in
* the property definition are satisfied
* by the the specified values
* @throws RepositoryException if another error occurs
*/
public static void checkSetPropertyValueConstraints(QPropertyDefinition pd, InternalValue[] values) throws ConstraintViolationException, RepositoryException {
// check multi-value flag
if (!pd.isMultiple() && values != null && values.length > 1) {
throw new ConstraintViolationException("the property is not multi-valued");
}
QValueConstraint[] constraints = pd.getValueConstraints();
if (constraints == null || constraints.length == 0) {
// no constraints to check
return;
}
if (values != null && values.length > 0) {
// check value constraints on every value
for (InternalValue value : values) {
// constraints are OR-ed together
boolean satisfied = false;
ConstraintViolationException cve = null;
for (QValueConstraint constraint : constraints) {
try {
constraint.check(value);
satisfied = true;
break;
} catch (ConstraintViolationException e) {
cve = e;
}
}
if (!satisfied) {
// re-throw last exception we encountered
throw cve;
}
}
}
}
Aggregations