use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class NodeImpl method removeChildNode.
protected void removeChildNode(NodeId childId) throws RepositoryException {
// modify the state of 'this', i.e. the parent node
NodeState thisState = (NodeState) getOrCreateTransientItemState();
ChildNodeEntry entry = thisState.getChildNodeEntry(childId);
if (entry == null) {
String msg = "failed to remove child " + childId + " of " + this;
log.debug(msg);
throw new RepositoryException(msg);
}
// notify target of removal
try {
NodeImpl childNode = itemMgr.getNode(childId, getNodeId());
childNode.onRemove(getNodeId());
} catch (ItemNotFoundException e) {
boolean ignoreError = false;
if (sessionContext.getSessionImpl().autoFixCorruptions()) {
// it might be an access right problem
// we need to check if the item doesn't exist in the ism
ItemStateManager ism = sessionContext.getItemStateManager();
if (!ism.hasItemState(childId)) {
log.warn("Node " + childId + " not found, ignore", e);
ignoreError = true;
}
}
if (!ignoreError) {
throw e;
}
}
// remove the child node entry
if (!thisState.removeChildNodeEntry(childId)) {
String msg = "failed to remove child " + childId + " of " + this;
log.debug(msg);
throw new RepositoryException(msg);
}
}
use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class NodeImpl method setMixins.
/**
* {@inheritDoc}
*/
public void setMixins(String[] mixinNames) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
// check state of this instance
sanityCheck();
NodeTypeManagerImpl ntMgr = sessionContext.getNodeTypeManager();
Set<Name> newMixins = new HashSet<Name>();
for (String name : mixinNames) {
Name qName = sessionContext.getQName(name);
if (!ntMgr.getNodeType(qName).isMixin()) {
throw new RepositoryException(sessionContext.getJCRName(qName) + " is not a mixin node type");
}
newMixins.add(qName);
}
// make sure this node is checked-out, neither protected nor locked and
// the editing session has sufficient permission to change the mixin types.
// special handling of mix:(simple)versionable. since adding the
// mixin alters the version storage jcr:versionManagement privilege
// is required in addition.
int permissions = Permission.NODE_TYPE_MNGMT;
if (newMixins.contains(MIX_VERSIONABLE) || newMixins.contains(MIX_SIMPLE_VERSIONABLE)) {
permissions |= Permission.VERSION_MNGMT;
}
int options = ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CONSTRAINTS | ItemValidator.CHECK_HOLD;
sessionContext.getItemValidator().checkModify(this, options, permissions);
final NodeState state = data.getNodeState();
// build effective node type of primary type & new mixin's
// in order to detect conflicts
NodeTypeRegistry ntReg = ntMgr.getNodeTypeRegistry();
EffectiveNodeType entNew, entOld, entAll;
try {
entNew = ntReg.getEffectiveNodeType(newMixins);
entOld = ntReg.getEffectiveNodeType(state.getMixinTypeNames());
// try to build new effective node type (will throw in case of conflicts)
entAll = ntReg.getEffectiveNodeType(state.getNodeTypeName(), newMixins);
} catch (NodeTypeConflictException ntce) {
throw new ConstraintViolationException(ntce.getMessage());
}
// added child item definitions
Set<QItemDefinition> addedDefs = new HashSet<QItemDefinition>(Arrays.asList(entNew.getAllItemDefs()));
addedDefs.removeAll(Arrays.asList(entOld.getAllItemDefs()));
// referential integrity check
boolean referenceableOld = getEffectiveNodeType().includesNodeType(NameConstants.MIX_REFERENCEABLE);
boolean referenceableNew = entAll.includesNodeType(NameConstants.MIX_REFERENCEABLE);
if (referenceableOld && !referenceableNew) {
// node would become non-referenceable;
// make sure no references exist
PropertyIterator iter = getReferences();
if (iter.hasNext()) {
throw new ConstraintViolationException("the new mixin types cannot be set as it would render " + "this node 'non-referenceable' while it is still being " + "referenced through at least one property of type REFERENCE");
}
}
// gather currently assigned definitions *before* doing actual modifications
Map<ItemId, ItemDefinition> oldDefs = new HashMap<ItemId, ItemDefinition>();
for (Name name : getNodeState().getPropertyNames()) {
PropertyId id = new PropertyId(getNodeId(), name);
try {
PropertyState propState = (PropertyState) stateMgr.getItemState(id);
oldDefs.put(id, itemMgr.getDefinition(propState));
} catch (ItemStateException ise) {
String msg = name + ": failed to retrieve property state";
log.error(msg, ise);
throw new RepositoryException(msg, ise);
}
}
for (ChildNodeEntry cne : getNodeState().getChildNodeEntries()) {
try {
NodeState nodeState = (NodeState) stateMgr.getItemState(cne.getId());
oldDefs.put(cne.getId(), itemMgr.getDefinition(nodeState));
} catch (ItemStateException ise) {
String msg = cne + ": failed to retrieve node state";
log.error(msg, ise);
throw new RepositoryException(msg, ise);
}
}
// now do the actual modifications in content as mandated by the new mixins
// modify the state of this node
NodeState thisState = (NodeState) getOrCreateTransientItemState();
thisState.setMixinTypeNames(newMixins);
// set jcr:mixinTypes property
setMixinTypesProperty(newMixins);
// walk through properties and child nodes and change definition as necessary
// use temp set to avoid ConcurrentModificationException
HashSet<Name> set = new HashSet<Name>(thisState.getPropertyNames());
for (Name propName : set) {
PropertyState propState = null;
try {
propState = (PropertyState) stateMgr.getItemState(new PropertyId(thisState.getNodeId(), propName));
// the following call triggers ConstraintViolationException
// if there isn't any suitable definition anymore
itemMgr.getDefinition(propState);
} catch (ConstraintViolationException cve) {
// redefine property if possible
try {
if (oldDefs.get(propState.getId()).isProtected()) {
// remove 'orphaned' protected properties immediately
removeChildProperty(propName);
continue;
}
PropertyDefinitionImpl pdi = getApplicablePropertyDefinition(propName, propState.getType(), propState.isMultiValued(), false);
PropertyImpl prop = (PropertyImpl) itemMgr.getItem(propState.getId());
if (pdi.getRequiredType() != PropertyType.UNDEFINED && pdi.getRequiredType() != propState.getType()) {
// value conversion required
if (propState.isMultiValued()) {
// convert value
Value[] values = ValueHelper.convert(prop.getValues(), pdi.getRequiredType(), getSession().getValueFactory());
// redefine property
prop.onRedefine(pdi.unwrap());
// set converted values
prop.setValue(values);
} else {
// convert value
Value value = ValueHelper.convert(prop.getValue(), pdi.getRequiredType(), getSession().getValueFactory());
// redefine property
prop.onRedefine(pdi.unwrap());
// set converted values
prop.setValue(value);
}
} else {
// redefine property
prop.onRedefine(pdi.unwrap());
}
// update collection of added definitions
addedDefs.remove(pdi.unwrap());
} catch (ValueFormatException vfe) {
// value conversion failed, remove it
removeChildProperty(propName);
} catch (ConstraintViolationException cve1) {
// no suitable definition found for this property,
// remove it
removeChildProperty(propName);
}
} catch (ItemStateException ise) {
String msg = propName + ": failed to retrieve property state";
log.error(msg, ise);
throw new RepositoryException(msg, ise);
}
}
// use temp array to avoid ConcurrentModificationException
ArrayList<ChildNodeEntry> list = new ArrayList<ChildNodeEntry>(thisState.getChildNodeEntries());
// start from tail to avoid problems with same-name siblings
for (int i = list.size() - 1; i >= 0; i--) {
ChildNodeEntry entry = list.get(i);
NodeState nodeState = null;
try {
nodeState = (NodeState) stateMgr.getItemState(entry.getId());
// the following call triggers ConstraintViolationException
// if there isn't any suitable definition anymore
itemMgr.getDefinition(nodeState);
} catch (ConstraintViolationException cve) {
// redefine node if possible
try {
if (oldDefs.get(nodeState.getId()).isProtected()) {
// remove 'orphaned' protected child node immediately
removeChildNode(entry.getId());
continue;
}
NodeDefinitionImpl ndi = getApplicableChildNodeDefinition(entry.getName(), nodeState.getNodeTypeName());
NodeImpl node = (NodeImpl) itemMgr.getItem(nodeState.getId());
// redefine node
node.onRedefine(ndi.unwrap());
// update collection of added definitions
addedDefs.remove(ndi.unwrap());
} catch (ConstraintViolationException cve1) {
// no suitable definition found for this child node,
// remove it
removeChildNode(entry.getId());
}
} catch (ItemStateException ise) {
String msg = entry + ": failed to retrieve node state";
log.error(msg, ise);
throw new RepositoryException(msg, ise);
}
}
// and at the same time were not present with the old mixins
for (QItemDefinition def : addedDefs) {
if (def.isAutoCreated()) {
if (def.definesNode()) {
NodeDefinitionImpl ndi = ntMgr.getNodeDefinition((QNodeDefinition) def);
createChildNode(def.getName(), (NodeTypeImpl) ndi.getDefaultPrimaryType(), null);
} else {
PropertyDefinitionImpl pdi = ntMgr.getPropertyDefinition((QPropertyDefinition) def);
createChildProperty(pdi.unwrap().getName(), pdi.getRequiredType(), pdi);
}
}
}
}
use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class ProtectedItemModifier method addNode.
protected NodeImpl addNode(NodeImpl parentImpl, Name name, Name ntName, NodeId nodeId) throws RepositoryException {
checkPermission(parentImpl, name, getPermission(true, false));
// validation: make sure Node is not locked or checked-in.
parentImpl.checkSetProperty();
NodeTypeImpl nodeType = parentImpl.sessionContext.getNodeTypeManager().getNodeType(ntName);
org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl def = parentImpl.getApplicableChildNodeDefinition(name, ntName);
// check for name collisions
// TODO: improve. copied from NodeImpl
NodeState thisState = parentImpl.getNodeState();
ChildNodeEntry cne = thisState.getChildNodeEntry(name, 1);
if (cne != null) {
// check same-name sibling setting of new node
if (!def.allowsSameNameSiblings()) {
throw new ItemExistsException();
}
// check same-name sibling setting of existing node
NodeId newId = cne.getId();
NodeImpl n = (NodeImpl) parentImpl.sessionContext.getItemManager().getItem(newId);
if (!n.getDefinition().allowsSameNameSiblings()) {
throw new ItemExistsException();
}
}
return parentImpl.createChildNode(name, nodeType, nodeId);
}
use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class PersistenceCopier method copy.
/**
* Recursively copies the identified node and all its descendants.
* Explicitly excluded nodes and nodes that have already been copied
* are automatically skipped.
*
* @param id identifier of the node to be copied
* @throws RepositoryException if the copy operation fails
*/
public void copy(NodeId id) throws RepositoryException {
if (!exclude.contains(id)) {
try {
NodeState node = source.load(id);
for (ChildNodeEntry entry : node.getChildNodeEntries()) {
copy(entry.getId());
}
copy(node);
exclude.add(id);
} catch (ItemStateException e) {
throw new RepositoryException("Unable to copy " + id, e);
}
}
}
use of org.apache.jackrabbit.core.state.ChildNodeEntry in project jackrabbit by apache.
the class BatchedItemOperations method copyNodeState.
/**
* Recursively copies the specified node state including its properties and
* child nodes.
*
* @param srcState
* @param srcPath
* @param srcStateMgr
* @param srcAccessMgr
* @param destParentId
* @param flag one of
* <ul>
* <li><code>COPY</code></li>
* <li><code>CLONE</code></li>
* <li><code>CLONE_REMOVE_EXISTING</code></li>
* </ul>
* @param refTracker tracks uuid mappings and processed reference properties
* @return a deep copy of the given node state and its children
* @throws RepositoryException if an error occurs
*/
private NodeState copyNodeState(NodeState srcState, Path srcPath, ItemStateManager srcStateMgr, AccessManager srcAccessMgr, NodeId destParentId, int flag, ReferenceChangeTracker refTracker) throws RepositoryException {
NodeState newState;
try {
NodeId id = null;
EffectiveNodeType ent = getEffectiveNodeType(srcState);
boolean referenceable = ent.includesNodeType(NameConstants.MIX_REFERENCEABLE);
boolean versionable = ent.includesNodeType(NameConstants.MIX_SIMPLE_VERSIONABLE);
boolean fullVersionable = ent.includesNodeType(NameConstants.MIX_VERSIONABLE);
boolean shareable = ent.includesNodeType(NameConstants.MIX_SHAREABLE);
switch(flag) {
case COPY:
/* if this node is shareable and another node in the same shared set
* has been already been copied and given a new uuid, use this one
* (see section 14.5 of the specification)
*/
if (shareable && refTracker.getMappedId(srcState.getNodeId()) != null) {
NodeId newId = refTracker.getMappedId(srcState.getNodeId());
NodeState sharedState = (NodeState) stateMgr.getItemState(newId);
sharedState.addShare(destParentId);
return sharedState;
}
break;
case CLONE:
if (!referenceable) {
// non-referenceable node: always create new node id
break;
}
// use same uuid as source node
id = srcState.getNodeId();
if (stateMgr.hasItemState(id)) {
if (shareable) {
NodeState sharedState = (NodeState) stateMgr.getItemState(id);
sharedState.addShare(destParentId);
return sharedState;
}
// node with this uuid already exists
throw new ItemExistsException(safeGetJCRPath(id));
}
break;
case CLONE_REMOVE_EXISTING:
if (!referenceable) {
// non-referenceable node: always create new node id
break;
}
// use same uuid as source node
id = srcState.getNodeId();
if (stateMgr.hasItemState(id)) {
NodeState existingState = (NodeState) stateMgr.getItemState(id);
// or an ancestor thereof
if (id.equals(destParentId) || hierMgr.isAncestor(id, destParentId)) {
String msg = "cannot remove node " + safeGetJCRPath(srcPath) + " because it is an ancestor of the destination";
log.debug(msg);
throw new RepositoryException(msg);
}
// check if existing can be removed
// (access rights, locking & versioning status,
// node type constraints and retention/hold)
checkRemoveNode(existingState, CHECK_ACCESS | CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD | CHECK_RETENTION);
// do remove existing
removeNodeState(existingState);
}
break;
default:
throw new IllegalArgumentException("unknown flag for copying node state: " + flag);
}
newState = stateMgr.createNew(id, srcState.getNodeTypeName(), destParentId);
id = newState.getNodeId();
if (flag == COPY && referenceable) {
// remember uuid mapping
refTracker.mappedId(srcState.getNodeId(), id);
}
// copy node state
newState.setMixinTypeNames(srcState.getMixinTypeNames());
if (shareable) {
// initialize shared set
newState.addShare(destParentId);
}
// copy child nodes
for (ChildNodeEntry entry : srcState.getChildNodeEntries()) {
Path srcChildPath = PathFactoryImpl.getInstance().create(srcPath, entry.getName(), true);
if (!srcAccessMgr.isGranted(srcChildPath, Permission.READ)) {
continue;
}
NodeId nodeId = entry.getId();
NodeState srcChildState = (NodeState) srcStateMgr.getItemState(nodeId);
/**
* If child is shareble and its UUID has already been remapped,
* then simply add a reference to the state with that remapped
* UUID instead of copying the whole subtree.
*/
if (srcChildState.isShareable()) {
NodeId mappedId = refTracker.getMappedId(srcChildState.getNodeId());
if (mappedId != null) {
if (stateMgr.hasItemState(mappedId)) {
NodeState destState = (NodeState) stateMgr.getItemState(mappedId);
if (!destState.isShareable()) {
String msg = "Remapped child (" + safeGetJCRPath(srcPath) + ") is not shareable.";
throw new ItemStateException(msg);
}
if (!destState.addShare(id)) {
String msg = "Unable to add share to node: " + id;
throw new ItemStateException(msg);
}
stateMgr.store(destState);
newState.addChildNodeEntry(entry.getName(), mappedId);
continue;
}
}
}
// recursive copying of child node
NodeState newChildState = copyNodeState(srcChildState, srcChildPath, srcStateMgr, srcAccessMgr, id, flag, refTracker);
// store new child node
stateMgr.store(newChildState);
// add new child node entry to new node
newState.addChildNodeEntry(entry.getName(), newChildState.getNodeId());
}
// init version history if needed
VersionHistoryInfo history = null;
if (versionable && flag == COPY) {
NodeId copiedFrom = null;
if (fullVersionable) {
// base version of copied versionable node is reference value of
// the histories jcr:copiedFrom property
PropertyId propId = new PropertyId(srcState.getNodeId(), NameConstants.JCR_BASEVERSION);
PropertyState prop = (PropertyState) srcStateMgr.getItemState(propId);
copiedFrom = prop.getValues()[0].getNodeId();
}
InternalVersionManager manager = session.getInternalVersionManager();
history = manager.getVersionHistory(session, newState, copiedFrom);
}
// copy properties
for (Name propName : srcState.getPropertyNames()) {
Path propPath = PathFactoryImpl.getInstance().create(srcPath, propName, true);
PropertyId propId = new PropertyId(srcState.getNodeId(), propName);
if (!srcAccessMgr.canRead(propPath, propId)) {
continue;
}
PropertyState srcChildState = (PropertyState) srcStateMgr.getItemState(propId);
/**
* special handling required for properties with special semantics
* (e.g. those defined by mix:referenceable, mix:versionable,
* mix:lockable, et.al.)
*
* todo FIXME delegate to 'node type instance handler'
*/
QPropertyDefinition def = ent.getApplicablePropertyDef(srcChildState.getName(), srcChildState.getType(), srcChildState.isMultiValued());
if (NameConstants.MIX_LOCKABLE.equals(def.getDeclaringNodeType())) {
// skip properties defined by mix:lockable
continue;
}
PropertyState newChildState = copyPropertyState(srcChildState, id, propName, def);
if (history != null) {
if (fullVersionable) {
if (propName.equals(NameConstants.JCR_VERSIONHISTORY)) {
// jcr:versionHistory
InternalValue value = InternalValue.create(history.getVersionHistoryId());
newChildState.setValues(new InternalValue[] { value });
} else if (propName.equals(NameConstants.JCR_BASEVERSION) || propName.equals(NameConstants.JCR_PREDECESSORS)) {
// jcr:baseVersion or jcr:predecessors
InternalValue value = InternalValue.create(history.getRootVersionId());
newChildState.setValues(new InternalValue[] { value });
} else if (propName.equals(NameConstants.JCR_ISCHECKEDOUT)) {
// jcr:isCheckedOut
newChildState.setValues(new InternalValue[] { InternalValue.create(true) });
}
} else {
// version history when we see the jcr:isCheckedOut
if (propName.equals(NameConstants.JCR_ISCHECKEDOUT)) {
// jcr:isCheckedOut
newChildState.setValues(new InternalValue[] { InternalValue.create(true) });
}
}
}
if (newChildState.getType() == PropertyType.REFERENCE || newChildState.getType() == PropertyType.WEAKREFERENCE) {
refTracker.processedReference(newChildState);
}
// store new property
stateMgr.store(newChildState);
// add new property entry to new node
newState.addPropertyName(propName);
}
return newState;
} catch (ItemStateException ise) {
String msg = "internal error: failed to copy state of " + srcState.getNodeId();
log.debug(msg);
throw new RepositoryException(msg, ise);
}
}
Aggregations