use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class BatchedItemOperations method createNodeState.
/**
* Creates a new node based on the given definition.
* <p>
* Note that access rights are <b><i>not</i></b> enforced!
* <p>
* <b>Precondition:</b> the state manager needs to be in edit mode.
*
* @param parent
* @param nodeName
* @param nodeTypeName
* @param mixinNames
* @param id
* @param def
* @return
* @throws ItemExistsException
* @throws ConstraintViolationException
* @throws RepositoryException
* @throws IllegalStateException
*/
public NodeState createNodeState(NodeState parent, Name nodeName, Name nodeTypeName, Name[] mixinNames, NodeId id, QNodeDefinition def) throws ItemExistsException, ConstraintViolationException, RepositoryException, IllegalStateException {
// check for name collisions with existing nodes
if (!def.allowsSameNameSiblings() && parent.hasChildNodeEntry(nodeName)) {
NodeId errorId = parent.getChildNodeEntry(nodeName, 1).getId();
throw new ItemExistsException(safeGetJCRPath(errorId));
}
if (nodeTypeName == null) {
// no primary node type specified,
// try default primary type from definition
nodeTypeName = def.getDefaultPrimaryType();
if (nodeTypeName == null) {
String msg = "an applicable node type could not be determined for " + nodeName;
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
NodeState node = stateMgr.createNew(id, nodeTypeName, parent.getNodeId());
if (mixinNames != null && mixinNames.length > 0) {
node.setMixinTypeNames(new HashSet<Name>(Arrays.asList(mixinNames)));
}
// now add new child node entry to parent
parent.addChildNodeEntry(nodeName, node.getNodeId());
EffectiveNodeType ent = getEffectiveNodeType(node);
// check shareable
if (ent.includesNodeType(NameConstants.MIX_SHAREABLE)) {
node.addShare(parent.getNodeId());
}
if (!node.getMixinTypeNames().isEmpty()) {
// create jcr:mixinTypes property
QPropertyDefinition pd = ent.getApplicablePropertyDef(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true);
createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
}
// add 'auto-create' properties defined in node type
for (QPropertyDefinition pd : ent.getAutoCreatePropDefs()) {
createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
}
// recursively add 'auto-create' child nodes defined in node type
for (QNodeDefinition nd : ent.getAutoCreateNodeDefs()) {
createNodeState(node, nd.getName(), nd.getDefaultPrimaryType(), null, null, nd);
}
// store node
stateMgr.store(node);
// store parent
stateMgr.store(parent);
return node;
}
use of javax.jcr.nodetype.ConstraintViolationException 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;
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class AbstractWriteTest method testACItemsAreProtected.
public void testACItemsAreProtected() throws NotExecutableException, RepositoryException {
// search for a rep:policy node
Node policyNode = findPolicyNode(superuser.getRootNode());
if (policyNode == null) {
throw new NotExecutableException("no policy node found.");
}
assertTrue("The rep:Policy node must be protected", policyNode.getDefinition().isProtected());
try {
policyNode.remove();
fail("rep:Policy node must be protected.");
} catch (ConstraintViolationException e) {
// success
}
for (NodeIterator it = policyNode.getNodes(); it.hasNext(); ) {
Node n = it.nextNode();
if (n.isNodeType("rep:ACE")) {
try {
n.remove();
fail("ACE node must be protected.");
} catch (ConstraintViolationException e) {
// success
}
break;
}
}
try {
policyNode.setProperty("test", "anyvalue");
fail("rep:policy node must be protected.");
} catch (ConstraintViolationException e) {
// success
}
try {
policyNode.addNode("test", "rep:ACE");
fail("rep:policy node must be protected.");
} catch (ConstraintViolationException e) {
// success
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class AddNodeTest method testMixinNodeType.
/**
* Tests if addNode() throws a ConstraintViolationException in case
* of an mixin node type.
*/
public void testMixinNodeType() throws RepositoryException, NotExecutableException {
NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
NodeTypeIterator nts = ntMgr.getMixinNodeTypes();
if (nts.hasNext()) {
try {
testRootNode.addNode(nodeName1, nts.nextNodeType().getName());
superuser.save();
fail("Expected ConstraintViolationException.");
} catch (ConstraintViolationException e) {
// correct.
}
} else {
throw new NotExecutableException("no mixins.");
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class AddNodeTest method internalTestSimilarNodeNames.
/**
* Tests behavior for creation of "similarly" named nodes
* @throws RepositoryException
*/
private void internalTestSimilarNodeNames(String name1, String name2) throws RepositoryException {
Node n1 = null, n2 = null;
Session s = testRootNode.getSession();
try {
n1 = testRootNode.addNode(name1);
assertEquals(name1, n1.getName());
s.save();
assertFalse(testRootNode.hasNode(name2));
} catch (ConstraintViolationException e) {
// accepted
}
try {
n2 = testRootNode.addNode(name2);
assertEquals(name2, n2.getName());
s.save();
} catch (ConstraintViolationException e) {
// accepted
}
// If both nodes have been created, do further checks
if (n1 != null && n2 != null) {
assertFalse(n1.isSame(n2));
assertFalse(n1.getIdentifier().equals(n2.getIdentifier()));
String n2path = n2.getPath();
n1.remove();
s.save();
Node n3 = s.getNode(n2path);
assertTrue(n3.isSame(n2));
}
}
Aggregations