use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class WorkspaceCopyVersionableTest method testCopyNodesVersionableAndCheckedIn.
/**
* A VersionException is thrown if the parent node of destAbsPath is
* versionable and checked-in, or is non-versionable but its nearest
* versionable ancestor is checked-in.
*/
public void testCopyNodesVersionableAndCheckedIn() throws RepositoryException, NotExecutableException {
// prepare the test data
// create a non-versionable node below a versionable node
// required for having a nearest versionable ancestor to a nonversionable sub node
String dstAbsPath = node1.getPath() + "/" + node2.getName();
workspace.copy(node2.getPath(), dstAbsPath);
try {
// make parent node versionable and check-in
addMixinVersionableToNode(testRootNode, node1);
node1.checkin();
} catch (ConstraintViolationException ex) {
throw new NotExecutableException("server does not support making the parent versionable: " + ex.getMessage());
}
// 1. parent node of destAbsPath is non-versionable but its nearest versionable ancestor is checked-in
try {
workspace.copy(node2.getPath(), dstAbsPath + "/" + node2.getName());
fail("Copying a node to a node's versionable and checked-in nearest ancestor node of destAbsPath should throw VersionException.");
} catch (VersionException e) {
// successful
}
// 2. parent node of destAbsPath is versionable and checked-in
try {
workspace.copy(node2.getPath(), node1.getPath() + "/" + node2.getName());
fail("Copying a node to a versionable and checked-in parent node of destAbsPath should throw VersionException.");
} catch (VersionException e) {
// successful
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class SessionItemStateManager method addNodeState.
private List<ItemState> addNodeState(NodeState parent, Name nodeName, Name nodeTypeName, String uuid, QNodeDefinition definition, int options) throws RepositoryException, ConstraintViolationException, AccessDeniedException, UnsupportedRepositoryOperationException, NoSuchNodeTypeException, ItemExistsException, VersionException {
// check if add node is possible. note, that the options differ if
// the 'addNode' is called from inside a regular add-node to create
// autocreated child nodes that may be 'protected'.
validator.checkAddNode(parent, nodeName, nodeTypeName, options);
// a new NodeState doesn't have mixins defined yet -> ent is ent of primarytype
EffectiveNodeType ent = mgrProvider.getEffectiveNodeTypeProvider().getEffectiveNodeType(nodeTypeName);
if (nodeTypeName == null) {
// no primary node type specified,
// try default primary type from definition
nodeTypeName = definition.getDefaultPrimaryType();
if (nodeTypeName == null) {
String msg = "No applicable node type could be determined for " + nodeName;
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
List<ItemState> addedStates = new ArrayList<ItemState>();
// create new nodeState. NOTE, that the uniqueID is not added to the
// state for consistency between 'addNode' and importXML
NodeState nodeState = transientStateMgr.createNewNodeState(nodeName, null, nodeTypeName, definition, parent);
addedStates.add(nodeState);
if (uuid != null) {
QValue[] value = getQValues(uuid, qValueFactory);
ItemDefinitionProvider defProvider = mgrProvider.getItemDefinitionProvider();
QPropertyDefinition pDef = defProvider.getQPropertyDefinition(NameConstants.MIX_REFERENCEABLE, NameConstants.JCR_UUID, PropertyType.STRING, false);
addedStates.add(addPropertyState(nodeState, NameConstants.JCR_UUID, PropertyType.STRING, value, pDef, 0));
}
// add 'auto-create' properties defined in node type
for (QPropertyDefinition pd : ent.getAutoCreateQPropertyDefinitions()) {
if (!nodeState.hasPropertyName(pd.getName())) {
QValue[] autoValue = computeSystemGeneratedPropertyValues(nodeState, pd);
if (autoValue != null) {
int propOptions = ItemStateValidator.CHECK_NONE;
// execute 'addProperty' without adding operation.
addedStates.add(addPropertyState(nodeState, pd.getName(), pd.getRequiredType(), autoValue, pd, propOptions));
}
}
}
// recursively add 'auto-create' child nodes defined in node type
for (QNodeDefinition nd : ent.getAutoCreateQNodeDefinitions()) {
// execute 'addNode' without adding the operation.
int opt = ItemStateValidator.CHECK_LOCK | ItemStateValidator.CHECK_COLLISION;
addedStates.addAll(addNodeState(nodeState, nd.getName(), nd.getDefaultPrimaryType(), null, nd, opt));
}
return addedStates;
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class TransientItemStateManager method getChangeLog.
/**
* Create the change log for the tree starting at <code>target</code>. This
* includes a check if the ChangeLog to be created is totally 'self-contained'
* and independent; items within the scope of this update operation (i.e.
* below the target) must not have dependencies outside of this tree (e.g.
* moving a node requires that the target node including both old and new
* parents are saved).
*
* @param target
* @param throwOnStale Throws InvalidItemStateException if either the given
* <code>ItemState</code> or any of its descendants is stale and the flag is true.
* @return
* @throws InvalidItemStateException if a stale <code>ItemState</code> is
* encountered while traversing the state hierarchy. The <code>changeLog</code>
* might have been populated with some transient item states. A client should
* therefore not reuse the <code>changeLog</code> if such an exception is thrown.
* @throws RepositoryException if <code>state</code> is a new item state.
*/
ChangeLog getChangeLog(ItemState target, boolean throwOnStale) throws InvalidItemStateException, ConstraintViolationException, RepositoryException {
// fail-fast test: check status of this item's state
if (target.getStatus() == Status.NEW) {
String msg = "Cannot save/revert an item with status NEW (" + target + ").";
log.debug(msg);
throw new RepositoryException(msg);
}
if (throwOnStale && Status.isStale(target.getStatus())) {
String msg = "Attempt to save/revert an item, that has been externally modified (" + target + ").";
log.debug(msg);
throw new InvalidItemStateException(msg);
}
Set<Operation> ops = new LinkedHashSet<Operation>();
Set<ItemState> affectedStates = new LinkedHashSet<ItemState>();
HierarchyEntry he = target.getHierarchyEntry();
if (he.getParent() == null) {
// simplicity. collecting ops, states can be omitted.
if (throwOnStale && !staleStates.isEmpty()) {
String msg = "Cannot save changes: States has been modified externally.";
log.debug(msg);
throw new InvalidItemStateException(msg);
} else {
affectedStates.addAll(staleStates);
}
ops.addAll(operations);
affectedStates.addAll(addedStates);
affectedStates.addAll(modifiedStates);
affectedStates.addAll(removedStates);
} else {
// - check if there is a stale state in the scope (save only)
if (throwOnStale) {
for (ItemState state : staleStates) {
if (containedInTree(target, state)) {
String msg = "Cannot save changes: States has been modified externally.";
log.debug(msg);
throw new InvalidItemStateException(msg);
}
}
}
// - collect all affected states within the scope of save/undo
Iterator[] its = new Iterator[] { addedStates.iterator(), removedStates.iterator(), modifiedStates.iterator() };
IteratorChain chain = new IteratorChain(its);
if (!throwOnStale) {
chain.addIterator(staleStates.iterator());
}
while (chain.hasNext()) {
ItemState state = (ItemState) chain.next();
if (containedInTree(target, state)) {
affectedStates.add(state);
}
}
// changelog.
for (Operation op : operations) {
Collection<ItemState> opStates = op.getAffectedItemStates();
for (ItemState state : opStates) {
if (affectedStates.contains(state)) {
// operation needs to be included
if (!affectedStates.containsAll(opStates)) {
// incomplete changelog: need to save a parent as well
String msg = "ChangeLog is not self contained.";
throw new ConstraintViolationException(msg);
}
// no violation: add operation an stop iteration over
// all affected states present in the operation.
ops.add(op);
break;
}
}
}
}
ChangeLog cl = new ChangeLog(target, ops, affectedStates);
return cl;
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeImpl method addMixin.
/**
* @see Node#addMixin(String)
*/
public void addMixin(String mixinName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
checkIsWritable();
Name mixinQName = getQName(mixinName);
// get mixin types present in the jcr:mixinTypes property without
// modifying the NodeState.
List<Name> mixinValue = getMixinTypes();
if (!mixinValue.contains(mixinQName) && !isNodeType(mixinQName)) {
if (!canAddMixin(mixinQName)) {
throw new ConstraintViolationException("Cannot add '" + mixinName + "' mixin type.");
}
mixinValue.add(mixinQName);
// perform the operation
Operation op = SetMixin.create(getNodeState(), mixinValue.toArray(new Name[mixinValue.size()]));
session.getSessionItemStateManager().execute(op);
}
}
use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.
the class NodeImpl method addNode.
/**
* @see Node#addNode(String, String)
*/
public Node addNode(String relPath, String primaryNodeTypeName) throws ItemExistsException, PathNotFoundException, NoSuchNodeTypeException, LockException, VersionException, ConstraintViolationException, RepositoryException {
checkIsWritable();
// build path object and retrieve parent node
Path nodePath = getPath(relPath).getNormalizedPath();
if (nodePath.getIndex() != Path.INDEX_UNDEFINED) {
String msg = "Illegal subscript specified: " + relPath;
log.debug(msg);
throw new RepositoryException(msg);
}
NodeImpl parentNode;
if (nodePath.getLength() == 1) {
parentNode = this;
} else {
Path parentPath = nodePath.getAncestor(1);
ItemManager itemMgr = getItemManager();
if (itemMgr.nodeExists(parentPath)) {
parentNode = (NodeImpl) itemMgr.getNode(parentPath);
} else if (itemMgr.propertyExists(parentPath)) {
String msg = "Cannot add a node to property " + LogUtil.safeGetJCRPath(parentPath, session.getPathResolver());
log.debug(msg);
throw new ConstraintViolationException(msg);
} else {
throw new PathNotFoundException("Cannot add a new node to a non-existing parent at " + LogUtil.safeGetJCRPath(parentPath, session.getPathResolver()));
}
}
// get names objects for node and nt
Name nodeName = nodePath.getName();
Name ntName = (primaryNodeTypeName == null) ? null : getQName(primaryNodeTypeName);
// create new node (including validation checks)
return parentNode.createNode(nodeName, ntName);
}
Aggregations