batcheditemoperations.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 1,596 行 · 第 1/5 页
JAVA
1,596 行
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.core;import org.apache.jackrabbit.core.lock.LockManager;import org.apache.jackrabbit.core.nodetype.EffectiveNodeType;import org.apache.jackrabbit.core.nodetype.NodeDef;import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;import org.apache.jackrabbit.core.nodetype.PropDef;import org.apache.jackrabbit.core.nodetype.PropDefId;import org.apache.jackrabbit.core.security.AccessManager;import org.apache.jackrabbit.core.state.ItemState;import org.apache.jackrabbit.core.state.ItemStateException;import org.apache.jackrabbit.core.state.ItemStateManager;import org.apache.jackrabbit.core.state.NoSuchItemStateException;import org.apache.jackrabbit.core.state.NodeReferences;import org.apache.jackrabbit.core.state.NodeReferencesId;import org.apache.jackrabbit.core.state.NodeState;import org.apache.jackrabbit.core.state.PropertyState;import org.apache.jackrabbit.core.state.UpdatableItemStateManager;import org.apache.jackrabbit.core.util.ReferenceChangeTracker;import org.apache.jackrabbit.core.value.InternalValue;import org.apache.jackrabbit.core.version.VersionManager;import org.apache.jackrabbit.name.MalformedPathException;import org.apache.jackrabbit.name.Path;import org.apache.jackrabbit.name.QName;import org.apache.jackrabbit.uuid.UUID;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.jcr.AccessDeniedException;import javax.jcr.ItemExistsException;import javax.jcr.ItemNotFoundException;import javax.jcr.PathNotFoundException;import javax.jcr.PropertyType;import javax.jcr.ReferentialIntegrityException;import javax.jcr.RepositoryException;import javax.jcr.lock.LockException;import javax.jcr.nodetype.ConstraintViolationException;import javax.jcr.version.VersionException;import javax.jcr.version.VersionHistory;import java.util.ArrayList;import java.util.Arrays;import java.util.Calendar;import java.util.HashSet;import java.util.Iterator;import java.util.Set;/** * <code>BatchedItemOperations</code> is an <i>internal</i> helper class that * provides both high- and low-level operations directly on the * <code>ItemState</code> level. */public class BatchedItemOperations extends ItemValidator { private static Logger log = LoggerFactory.getLogger(BatchedItemOperations.class); // flags used by the copy(...) methods protected static final int COPY = 0; protected static final int CLONE = 1; protected static final int CLONE_REMOVE_EXISTING = 2; /** * option for <code>{@link #checkAddNode}</code> and * <code>{@link #checkRemoveNode}</code> methods:<p/> * check access rights */ public static final int CHECK_ACCESS = 1; /** * option for <code>{@link #checkAddNode}</code> and * <code>{@link #checkRemoveNode}</code> methods:<p/> * check lock status */ public static final int CHECK_LOCK = 2; /** * option for <code>{@link #checkAddNode}</code> and * <code>{@link #checkRemoveNode}</code> methods:<p/> * check checked-out status */ public static final int CHECK_VERSIONING = 4; /** * option for <code>{@link #checkAddNode}</code> and * <code>{@link #checkRemoveNode}</code> methods:<p/> * check constraints defined in node type */ public static final int CHECK_CONSTRAINTS = 16; /** * option for <code>{@link #checkRemoveNode}</code> method:<p/> * check that target node is not being referenced */ public static final int CHECK_REFERENCES = 8; /** * wrapped item state manager */ protected final UpdatableItemStateManager stateMgr; /** * lock manager used for checking locking status */ protected final LockManager lockMgr; /** * current session used for checking access rights and locking status */ protected final SessionImpl session; /** * Creates a new <code>BatchedItemOperations</code> instance. * * @param stateMgr item state manager * @param ntReg node type registry * @param lockMgr lock manager * @param session current session * @param hierMgr hierarchy manager */ public BatchedItemOperations(UpdatableItemStateManager stateMgr, NodeTypeRegistry ntReg, LockManager lockMgr, SessionImpl session, HierarchyManager hierMgr) { super(ntReg, hierMgr, session); this.stateMgr = stateMgr; this.lockMgr = lockMgr; this.session = session; } //-----------------------------------------< controlling batch operations > /** * Starts an edit operation on the wrapped state manager. * At the end of this operation, either {@link #update} or {@link #cancel} * must be invoked. * * @throws IllegalStateException if the state mananger is already in edit mode */ public void edit() throws IllegalStateException { stateMgr.edit(); } /** * Store an item state. * * @param state item state that should be stored * @throws IllegalStateException if the manager is not in edit mode. */ public void store(ItemState state) throws IllegalStateException { stateMgr.store(state); } /** * Destroy an item state. * * @param state item state that should be destroyed * @throws IllegalStateException if the manager is not in edit mode. */ public void destroy(ItemState state) throws IllegalStateException { stateMgr.destroy(state); } /** * End an update operation. This will save all changes made since * the last invokation of {@link #edit()}. If this operation fails, * no item will have been saved. * * @throws RepositoryException if the update operation failed * @throws IllegalStateException if the state mananger is not in edit mode */ public void update() throws RepositoryException, IllegalStateException { try { stateMgr.update(); } catch (ItemStateException ise) { String msg = "update operation failed"; log.debug(msg, ise); throw new RepositoryException(msg, ise); } } /** * Cancel an update operation. This will undo all changes made since * the last invokation of {@link #edit()}. * * @throws IllegalStateException if the state mananger is not in edit mode */ public void cancel() throws IllegalStateException { stateMgr.cancel(); } //-------------------------------------------< high-level item operations > /** * Copies 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 * @param flag one of * <ul> * <li><code>COPY</code></li> * <li><code>CLONE</code></li> * <li><code>CLONE_REMOVE_EXISTING</code></li> * </ul> * @throws ConstraintViolationException * @throws AccessDeniedException * @throws VersionException * @throws PathNotFoundException * @throws ItemExistsException * @throws LockException * @throws RepositoryException */ public void copy(Path srcPath, Path destPath, int flag) throws ConstraintViolationException, AccessDeniedException, VersionException, PathNotFoundException, ItemExistsException, LockException, RepositoryException { copy(srcPath, stateMgr, hierMgr, session.getAccessManager(), destPath, flag); } /** * Copies the tree at <code>srcPath</code> retrieved using the specified * <code>srcStateMgr</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 srcStateMgr * @param srcHierMgr * @param srcAccessMgr * @param destPath * @param flag one of * <ul> * <li><code>COPY</code></li> * <li><code>CLONE</code></li> * <li><code>CLONE_REMOVE_EXISTING</code></li> * </ul> * @throws ConstraintViolationException * @throws AccessDeniedException * @throws VersionException * @throws PathNotFoundException * @throws ItemExistsException * @throws LockException * @throws RepositoryException * @throws IllegalStateException if the state mananger is not in edit mode */ public void copy(Path srcPath, ItemStateManager srcStateMgr, HierarchyManager srcHierMgr, AccessManager srcAccessMgr, Path destPath, int flag) throws ConstraintViolationException, AccessDeniedException, VersionException, PathNotFoundException, ItemExistsException, LockException, RepositoryException, IllegalStateException { // check precondition if (!stateMgr.inEditMode()) { throw new IllegalStateException("not in edit mode"); } // 1. check paths & retrieve state NodeState srcState = getNodeState(srcStateMgr, srcHierMgr, srcPath); Path.PathElement destName = destPath.getNameElement(); Path destParentPath = destPath.getAncestor(1); NodeState destParentState = getNodeState(destParentPath); int ind = destName.getIndex(); if (ind > 0) { // subscript in name element String msg = "invalid destination path (subscript in name element is not allowed)"; log.debug(msg); throw new RepositoryException(msg); } // 2. check access rights, lock status, node type constraints, etc. checkAddNode(destParentState, destName.getName(), srcState.getNodeTypeName(), CHECK_ACCESS | CHECK_LOCK | CHECK_VERSIONING | CHECK_CONSTRAINTS); // check read access right on source node using source access manager try { if (!srcAccessMgr.isGranted(srcState.getNodeId(), AccessManager.READ)) { throw new PathNotFoundException(safeGetJCRPath(srcPath)); } } catch (ItemNotFoundException infe) { String msg = "internal error: failed to check access rights for " + safeGetJCRPath(srcPath); log.debug(msg); throw new RepositoryException(msg, infe); } // 3. do copy operation (modify and store affected states) ReferenceChangeTracker refTracker = new ReferenceChangeTracker(); // create deep copy of source node state NodeState newState = copyNodeState(srcState, srcStateMgr, srcAccessMgr, destParentState.getNodeId(), flag, refTracker); // add to new parent destParentState.addChildNodeEntry(destName.getName(), newState.getNodeId()); // change definition (id) of new node NodeDef newNodeDef = findApplicableNodeDefinition(destName.getName(), srcState.getNodeTypeName(), destParentState); newState.setDefinitionId(newNodeDef.getId()); // adjust references that refer to uuid's which have been mapped to
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?