use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class SessionImporter method startNode.
/**
* {@inheritDoc}
*/
public void startNode(NodeInfo nodeInfo, List<PropInfo> propInfos, NamePathResolver resolver) throws RepositoryException {
if (isClosed()) {
// workspace-importer only: ignore if import has been aborted before.
return;
}
checkSession();
NodeState parent = parents.peek();
if (parent == null) {
// parent node was skipped, skip this child node also
// push null onto stack for skipped node
parents.push(null);
log.debug("Skipping node '" + nodeInfo.getName() + "'.");
return;
}
NodeEntry parentEntry = (NodeEntry) parent.getHierarchyEntry();
NodeState nodeState = null;
if (parentEntry.hasNodeEntry(nodeInfo.getName())) {
try {
// a valid child node with that name already exists
NodeEntry entry = parentEntry.getNodeEntry(nodeInfo.getName(), Path.INDEX_DEFAULT);
NodeState existing = entry.getNodeState();
QNodeDefinition def = existing.getDefinition();
if (!def.allowsSameNameSiblings()) {
// existing doesn't allow same-name siblings, check for conflicts
EffectiveNodeTypeProvider provider = session.getEffectiveNodeTypeProvider();
Name[] ntNames = existing.getAllNodeTypeNames();
EffectiveNodeType entExisting = provider.getEffectiveNodeType(ntNames);
if (def.isProtected() && entExisting.includesNodeType(nodeInfo.getNodeTypeName())) {
// skip protected node
// push null onto stack for skipped node
parents.push(null);
log.debug("skipping protected node " + LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
return;
}
if (def.isAutoCreated() && entExisting.includesNodeType(nodeInfo.getNodeTypeName())) {
// this node has already been auto-created, no need to create it
nodeState = existing;
} else {
throw new ItemExistsException(LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
}
}
} catch (ItemNotFoundException e) {
// 'existing' doesn't exist any more -> ignore
}
}
if (nodeState == null) {
// node does not exist -> create new one
if (nodeInfo.getUUID() == null) {
// no potential uuid conflict, add new node from given info
nodeState = importNode(nodeInfo, parent);
} else {
// make sure the import does not define a uuid without having
// a primaryType or mixin that makes the new node referenceable
checkIncludesMixReferenceable(nodeInfo);
// potential uuid conflict
try {
NodeId conflictingId = session.getIdFactory().createNodeId(nodeInfo.getUUID());
NodeEntry conflicting = session.getHierarchyManager().getNodeEntry(conflictingId);
// assert that the entry is available
conflicting.getItemState();
nodeState = resolveUUIDConflict(parent, conflicting, nodeInfo);
} catch (ItemNotFoundException e) {
// no conflict: create new with given uuid
nodeState = importNode(nodeInfo, parent);
}
}
}
// node state may be 'null' if applicable def is protected
if (nodeState != null) {
// process properties
for (PropInfo pi : propInfos) {
importProperty(pi, nodeState, resolver);
}
}
// push current nodeState onto stack of parents
parents.push(nodeState);
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class SessionImporter method importProperty.
/**
*
* @param pi
* @param parentState
* @param resolver
* @throws RepositoryException
* @throws ConstraintViolationException
*/
private void importProperty(PropInfo pi, NodeState parentState, NamePathResolver resolver) throws RepositoryException, ConstraintViolationException {
Name propName = pi.getName();
TextValue[] tva = pi.getValues();
int infoType = pi.getType();
PropertyState propState = null;
QPropertyDefinition def = null;
NodeEntry parentEntry = (NodeEntry) parentState.getHierarchyEntry();
PropertyEntry pEntry = parentEntry.getPropertyEntry(propName);
if (pEntry != null) {
// a property with that name already exists...
try {
PropertyState existing = pEntry.getPropertyState();
def = existing.getDefinition();
if (def.isProtected()) {
// skip protected property
log.debug("skipping protected property " + LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
return;
}
if (def.isAutoCreated() && (existing.getType() == infoType || infoType == PropertyType.UNDEFINED) && def.isMultiple() == existing.isMultiValued()) {
// this property has already been auto-created, no need to create it
propState = existing;
} else {
throw new ItemExistsException(LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
}
} catch (ItemNotFoundException e) {
// property doesn't exist any more
// -> ignore
}
}
Name[] parentNtNames = parentState.getAllNodeTypeNames();
if (def == null) {
// there's no property with that name, find applicable definition
if (tva.length == 1) {
// could be single- or multi-valued (n == 1)
def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType);
} else {
// can only be multi-valued (n == 0 || n > 1)
def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType, true);
}
if (def.isProtected()) {
// skip protected property
log.debug("skipping protected property " + propName);
return;
}
}
// retrieve the target property type needed for creation of QValue(s)
// including an eventual conversion. the targetType is then needed for
// setting/updating the type of the property-state.
int targetType = def.getRequiredType();
if (targetType == PropertyType.UNDEFINED) {
if (infoType == PropertyType.UNDEFINED) {
targetType = PropertyType.STRING;
} else {
targetType = infoType;
}
}
QValue[] values = getPropertyValues(pi, targetType, def.isMultiple(), resolver);
if (propState == null) {
// create new property
Operation ap = AddProperty.create(parentState, propName, targetType, def, values);
stateMgr.execute(ap);
propState = parentEntry.getPropertyEntry(propName).getPropertyState();
} else {
// modify value of existing property
Operation sp = SetPropertyValue.create(propState, values, targetType);
stateMgr.execute(sp);
}
// store reference for later resolution
if (propState.getType() == PropertyType.REFERENCE) {
refTracker.processedReference(propState);
}
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class WorkspaceItemStateFactory method createNodeState.
/**
* Create the node state with the information from <code>info</code>.
*
* @param info the <code>NodeInfo</code> to use to create the <code>NodeState</code>.
* @param entry the hierarchy entry for of this state
* @return the new <code>NodeState</code>.
* @throws ItemNotFoundException
* @throws RepositoryException
*/
private NodeState createNodeState(NodeInfo info, NodeEntry entry) throws ItemNotFoundException, RepositoryException {
// Make sure the entry has the correct ItemId
// this may not be the case, if the hierarchy has not been completely
// resolved yet -> if uniqueID is present, set it on this entry or on
// the appropriate parent entry
String uniqueID = info.getId().getUniqueID();
Path path = info.getId().getPath();
if (path == null) {
entry.setUniqueID(uniqueID);
} else if (uniqueID != null) {
// uniqueID that applies to a parent NodeEntry -> get parentEntry
NodeEntry parent = getAncestor(entry, path.getLength());
parent.setUniqueID(uniqueID);
}
int previousStatus = entry.getStatus();
if (Status.isTransient(previousStatus) || Status.isStale(previousStatus)) {
log.debug("Node has pending changes; omit resetting the state.");
return entry.getNodeState();
}
// update NodeEntry from the information present in the NodeInfo (prop entries)
List<Name> propNames = new ArrayList<Name>();
for (Iterator<PropertyId> it = info.getPropertyIds(); it.hasNext(); ) {
PropertyId pId = it.next();
Name propertyName = pId.getName();
propNames.add(propertyName);
}
try {
entry.setPropertyEntries(propNames);
} catch (ItemExistsException e) {
// should not get here
log.error("Internal error", e);
}
// unless the child-info are omitted by the SPI impl -> make sure
// the child entries the node entry are initialized or updated.
Iterator<ChildInfo> childInfos = info.getChildInfos();
if (childInfos != null) {
entry.setNodeEntries(childInfos);
}
// now build or update the nodestate itself
NodeState tmp = new NodeState(entry, info, this, definitionProvider);
entry.setItemState(tmp);
NodeState nState = entry.getNodeState();
if (previousStatus == Status._UNDEFINED_) {
// tmp state was used as resolution for the given entry i.e. the
// entry was not available before. otherwise the 2 states were
// merged. see HierarchyEntryImpl#setItemState
notifyCreated(nState);
} else {
notifyUpdated(nState, previousStatus);
}
return nState;
}
use of javax.jcr.ItemExistsException in project jackrabbit-oak by apache.
the class WorkspaceDelegate method copy.
/**
* Copy a node
* @param srcPath oak path to the source node to copy
* @param destPath oak path to the destination
* @throws RepositoryException
*/
public void copy(String srcPath, String destPath) throws RepositoryException {
SessionDelegate sessionDelegate = context.getSessionDelegate();
AccessManager accessManager = context.getAccessManager();
Root root = sessionDelegate.getContentSession().getLatestRoot();
// check destination
Tree dest = root.getTree(destPath);
if (dest.exists()) {
throw new ItemExistsException(destPath);
}
// check parent of destination
Tree destParent = dest.getParent();
if (!destParent.exists()) {
throw new PathNotFoundException(destParent.getPath());
}
// check source exists
Tree src = root.getTree(srcPath);
if (src.isRoot()) {
throw new RepositoryException("Cannot copy the root node");
}
if (!src.exists()) {
throw new PathNotFoundException(srcPath);
}
accessManager.checkPermissions(destPath, Permissions.getString(Permissions.NODE_TYPE_MANAGEMENT));
String userId = sessionDelegate.getAuthInfo().getUserID();
new WorkspaceCopy(src, destParent, Text.getName(destPath)).perform(root, userId);
sessionDelegate.refresh(true);
}
use of javax.jcr.ItemExistsException in project jackrabbit-oak by apache.
the class SessionDelegate method move.
/**
* Move a node
*
* @param srcPath oak path to the source node to copy
* @param destPath oak path to the destination
* @param transientOp whether or not to perform the move in transient space
* @throws RepositoryException
*/
public void move(String srcPath, String destPath, boolean transientOp) throws RepositoryException {
Root moveRoot = transientOp ? root : contentSession.getLatestRoot();
// check destination
Tree dest = moveRoot.getTree(destPath);
if (dest.exists()) {
throw new ItemExistsException(destPath);
}
// check parent of destination
String destParentPath = PathUtils.getParentPath(destPath);
Tree destParent = moveRoot.getTree(destParentPath);
if (!destParent.exists()) {
throw new PathNotFoundException(PathUtils.getParentPath(destPath));
}
// check source exists
Tree src = moveRoot.getTree(srcPath);
if (!src.exists()) {
throw new PathNotFoundException(srcPath);
}
try {
if (!moveRoot.move(srcPath, destPath)) {
throw new RepositoryException("Cannot move node at " + srcPath + " to " + destPath);
}
if (!transientOp) {
sessionCounters.saveTime = clock.getTime();
sessionCounters.saveCount++;
commit(moveRoot);
refresh(true);
}
} catch (CommitFailedException e) {
throw newRepositoryException(e);
}
}
Aggregations