batcheditemoperations.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 1,596 行 · 第 1/5 页
JAVA
1,596 行
// newly generated uuid's on copy/clone Iterator iter = refTracker.getProcessedReferences(); while (iter.hasNext()) { PropertyState prop = (PropertyState) iter.next(); // being paranoid... if (prop.getType() != PropertyType.REFERENCE) { continue; } boolean modified = false; InternalValue[] values = prop.getValues(); InternalValue[] newVals = new InternalValue[values.length]; for (int i = 0; i < values.length; i++) { InternalValue val = values[i]; UUID original = (UUID) val.internalValue(); UUID adjusted = refTracker.getMappedUUID(original); if (adjusted != null) { newVals[i] = InternalValue.create(adjusted); modified = true; } else { // reference doesn't need adjusting, just copy old value newVals[i] = val; } } if (modified) { prop.setValues(newVals); stateMgr.store(prop); } } refTracker.clear(); // store states stateMgr.store(newState); stateMgr.store(destParentState); } /** * Moves the tree at <code>srcPath</code> to the new location at * <code>destPath</code>. * <p/> * <b>Precondition:</b> the state manager needs to be in edit mode. * * @param srcPath * @param destPath * @throws ConstraintViolationException * @throws VersionException * @throws AccessDeniedException * @throws PathNotFoundException * @throws ItemExistsException * @throws LockException * @throws RepositoryException * @throws IllegalStateException if the state mananger is not in edit mode */ public void move(Path srcPath, Path destPath) throws ConstraintViolationException, VersionException, AccessDeniedException, PathNotFoundException, ItemExistsException, LockException, RepositoryException, IllegalStateException { // check precondition if (!stateMgr.inEditMode()) { throw new IllegalStateException("not in edit mode"); } // 1. check paths & retrieve state try { if (srcPath.isAncestorOf(destPath)) { String msg = safeGetJCRPath(destPath) + ": invalid destination path (cannot be descendant of source path)"; log.debug(msg); throw new RepositoryException(msg); } } catch (MalformedPathException mpe) { String msg = "invalid path: " + safeGetJCRPath(destPath); log.debug(msg); throw new RepositoryException(msg, mpe); } Path.PathElement srcName = srcPath.getNameElement(); Path srcParentPath = srcPath.getAncestor(1); NodeState target = getNodeState(srcPath); NodeState srcParent = getNodeState(srcParentPath); Path.PathElement destName = destPath.getNameElement(); Path destParentPath = destPath.getAncestor(1); NodeState destParent = getNodeState(destParentPath); int ind = destName.getIndex(); if (ind > 0) { // subscript in name element String msg = safeGetJCRPath(destPath) + ": invalid destination path (subscript in name element is not allowed)"; log.debug(msg); throw new RepositoryException(msg); } // 2. check if target state can be removed from old/added to new parent checkRemoveNode(target, srcParent.getNodeId(), CHECK_ACCESS | CHECK_LOCK | CHECK_VERSIONING | CHECK_CONSTRAINTS); checkAddNode(destParent, destName.getName(), target.getNodeTypeName(), CHECK_ACCESS | CHECK_LOCK | CHECK_VERSIONING | CHECK_CONSTRAINTS); // 3. do move operation (modify and store affected states) boolean renameOnly = srcParent.getNodeId().equals(destParent.getNodeId()); int srcNameIndex = srcName.getIndex(); if (srcNameIndex == 0) { srcNameIndex = 1; } if (renameOnly) { // change child node entry destParent.renameChildNodeEntry(srcName.getName(), srcNameIndex, destName.getName()); } else { // remove child node entry from old parent srcParent.removeChildNodeEntry(srcName.getName(), srcNameIndex); // re-parent target node target.setParentId(destParent.getNodeId()); // add child node entry to new parent destParent.addChildNodeEntry(destName.getName(), target.getNodeId()); } // change definition (id) of target node NodeDef newTargetDef = findApplicableNodeDefinition(destName.getName(), target.getNodeTypeName(), destParent); target.setDefinitionId(newTargetDef.getId()); // store states stateMgr.store(target); if (renameOnly) { stateMgr.store(srcParent); } else { stateMgr.store(destParent); stateMgr.store(srcParent); } } /** * Removes the specified node, recursively removing its properties and * child nodes. * <p/> * <b>Precondition:</b> the state manager needs to be in edit mode. * * @param nodePath * @throws ConstraintViolationException * @throws AccessDeniedException * @throws VersionException * @throws LockException * @throws ItemNotFoundException * @throws ReferentialIntegrityException * @throws RepositoryException * @throws IllegalStateException */ public void removeNode(Path nodePath) throws ConstraintViolationException, AccessDeniedException, VersionException, LockException, ItemNotFoundException, ReferentialIntegrityException, RepositoryException, IllegalStateException { // check precondition if (!stateMgr.inEditMode()) { throw new IllegalStateException("not in edit mode"); } // 1. retrieve affected state NodeState target = getNodeState(nodePath); NodeId parentId = target.getParentId(); // 2. check if target state can be removed from parent checkRemoveNode(target, parentId, CHECK_ACCESS | CHECK_LOCK | CHECK_VERSIONING | CHECK_CONSTRAINTS | CHECK_REFERENCES); // 3. do remove operation removeNodeState(target); } //--------------------------------------< misc. high-level helper methods > /** * Checks if adding a child node called <code>nodeName</code> of node type * <code>nodeTypeName</code> to the given parent node is allowed in the * current context. * * @param parentState * @param nodeName * @param nodeTypeName * @param options bit-wise OR'ed flags specifying the checks that should be * performed; any combination of the following constants: * <ul> * <li><code>{@link #CHECK_ACCESS}</code>: make sure * current session is granted read & write access on * parent node</li> * <li><code>{@link #CHECK_LOCK}</code>: make sure * there's no foreign lock on parent node</li> * <li><code>{@link #CHECK_VERSIONING}</code>: make sure * parent node is checked-out</li> * <li><code>{@link #CHECK_CONSTRAINTS}</code>: * make sure no node type constraints would be violated</li> * <li><code>{@link #CHECK_REFERENCES}</code></li> * </ul> * @throws ConstraintViolationException * @throws AccessDeniedException * @throws VersionException * @throws LockException * @throws ItemNotFoundException * @throws ItemExistsException * @throws RepositoryException */ public void checkAddNode(NodeState parentState, QName nodeName, QName nodeTypeName, int options) throws ConstraintViolationException, AccessDeniedException, VersionException, LockException, ItemNotFoundException, ItemExistsException, RepositoryException { Path parentPath = hierMgr.getPath(parentState.getNodeId()); // 1. locking status if ((options & CHECK_LOCK) == CHECK_LOCK) { // make sure there's no foreign lock on parent node verifyUnlocked(parentPath); } // 2. versioning status if ((options & CHECK_VERSIONING) == CHECK_VERSIONING) { // make sure parent node is checked-out verifyCheckedOut(parentPath); } // 3. access rights if ((options & CHECK_ACCESS) == CHECK_ACCESS) { AccessManager accessMgr = session.getAccessManager(); // make sure current session is granted read access on parent node if (!accessMgr.isGranted(parentState.getNodeId(), AccessManager.READ)) { throw new ItemNotFoundException(safeGetJCRPath(parentState.getNodeId())); } // make sure current session is granted write access on parent node if (!accessMgr.isGranted(parentState.getNodeId(), AccessManager.WRITE)) { throw new AccessDeniedException(safeGetJCRPath(parentState.getNodeId()) + ": not allowed to add child node"); } } // 4. node type constraints if ((options & CHECK_CONSTRAINTS) == CHECK_CONSTRAINTS) { NodeDef parentDef = ntReg.getNodeDef(parentState.getDefinitionId()); // make sure parent node is not protected if (parentDef.isProtected()) { throw new ConstraintViolationException(safeGetJCRPath(parentState.getNodeId()) + ": cannot add child node to protected parent node"); } // make sure there's an applicable definition for new child node EffectiveNodeType entParent = getEffectiveNodeType(parentState); entParent.checkAddNodeConstraints(nodeName, nodeTypeName, ntReg); NodeDef newNodeDef = findApplicableNodeDefinition(nodeName, nodeTypeName, parentState); // check for name collisions if (parentState.hasPropertyName(nodeName)) { // there's already a property with that name throw new ItemExistsException("cannot add child node '" + nodeName.getLocalName() + "' to " + safeGetJCRPath(parentState.getNodeId()) + ": colliding with same-named existing property"); } if (parentState.hasChildNodeEntry(nodeName)) { // there's already a node with that name... // get definition of existing conflicting node NodeState.ChildNodeEntry entry = parentState.getChildNodeEntry(nodeName, 1); NodeState conflictingState; NodeId conflictingId = entry.getId(); try { conflictingState = (NodeState) stateMgr.getItemState(conflictingId); } catch (ItemStateException ise) { String msg = "internal error: failed to retrieve state of " + safeGetJCRPath(conflictingId); log.debug(msg); throw new RepositoryException(msg, ise); } NodeDef conflictingTargetDef = ntReg.getNodeDef(conflictingState.getDefinitionId()); // check same-name sibling setting of both target and existing node if (!conflictingTargetDef.allowsSameNameSiblings() || !newNodeDef.allowsSameNameSiblings()) { throw new ItemExistsException("cannot add child node '" + nodeName.getLocalName() + "' to " + safeGetJCRPath(parentState.getNodeId()) + ": colliding with same-named existing node"); } } } } /** * Checks if removing the given target node is allowed in the current context. * * @param targetState * @param options bit-wise OR'ed flags specifying the checks that should be * performed; any combination of the following constants: * <ul> * <li><code>{@link #CHECK_ACCESS}</code>: make sure * current session is granted read access on parent * and remove privilege on target node</li> * <li><code>{@link #CHECK_LOCK}</code>: make sure * there's no foreign lock on parent node</li> * <li><code>{@link #CHECK_VERSIONING}</code>: make sure * parent node is checked-out</li> * <li><code>{@link #CHECK_CONSTRAINTS}</code>: * make sure no node type constraints would be violated</li> * <li><code>{@link #CHECK_REFERENCES}</code>: * make sure no references exist on target node</li> * </ul>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?