use of org.apache.jackrabbit.core.nodetype.EffectiveNodeType in project jackrabbit by apache.
the class NodeImpl method canAddMixin.
/**
* {@inheritDoc}
*/
public boolean canAddMixin(String mixinName) throws NoSuchNodeTypeException, RepositoryException {
// check state of this instance
sanityCheck();
Name ntName = sessionContext.getQName(mixinName);
NodeTypeManagerImpl ntMgr = sessionContext.getNodeTypeManager();
NodeTypeImpl mixin = ntMgr.getNodeType(ntName);
if (!mixin.isMixin()) {
return false;
}
int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS | ItemValidator.CHECK_HOLD;
int permissions = Permission.NODE_TYPE_MNGMT;
// in addition.
if (NameConstants.MIX_VERSIONABLE.equals(ntName) || NameConstants.MIX_SIMPLE_VERSIONABLE.equals(ntName)) {
permissions |= Permission.VERSION_MNGMT;
}
if (!sessionContext.getItemValidator().canModify(this, options, permissions)) {
return false;
}
final Name primaryTypeName = data.getNodeState().getNodeTypeName();
NodeTypeImpl primaryType = ntMgr.getNodeType(primaryTypeName);
if (primaryType.isDerivedFrom(ntName)) {
// mixin already inherited -> addMixin is allowed but has no effect.
return true;
}
// build effective node type of mixins & primary type
// in order to detect conflicts
NodeTypeRegistry ntReg = ntMgr.getNodeTypeRegistry();
EffectiveNodeType entExisting;
try {
// existing mixin's
Set<Name> mixins = new HashSet<Name>(data.getNodeState().getMixinTypeNames());
// build effective node type representing primary type including existing mixin's
entExisting = ntReg.getEffectiveNodeType(primaryTypeName, mixins);
if (entExisting.includesNodeType(ntName)) {
// addMixin would succeed without modifying the node.
return true;
}
// add new mixin
mixins.add(ntName);
// try to build new effective node type (will throw in case of conflicts)
ntReg.getEffectiveNodeType(primaryTypeName, mixins);
} catch (NodeTypeConflictException ntce) {
return false;
}
return true;
}
use of org.apache.jackrabbit.core.nodetype.EffectiveNodeType 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);
}
}
use of org.apache.jackrabbit.core.nodetype.EffectiveNodeType in project jackrabbit by apache.
the class BatchedItemOperations method isShareable.
/**
* Determines whether the specified node is <i>shareable</i>, i.e.
* whether the mixin type <code>mix:shareable</code> is either
* directly assigned or indirectly inherited.
*
* @param state node state to check
* @return true if the specified node is <i>shareable</i>, false otherwise.
* @throws RepositoryException if an error occurs
*/
private boolean isShareable(NodeState state) throws RepositoryException {
// shortcut: check some wellknown built-in types first
Name primary = state.getNodeTypeName();
Set<Name> mixins = state.getMixinTypeNames();
if (mixins.contains(NameConstants.MIX_SHAREABLE)) {
return true;
}
try {
NodeTypeRegistry registry = context.getNodeTypeRegistry();
EffectiveNodeType type = registry.getEffectiveNodeType(primary, mixins);
return type.includesNodeType(NameConstants.MIX_REFERENCEABLE);
} catch (NodeTypeConflictException ntce) {
String msg = "internal error: failed to build effective node type for node " + state.getNodeId();
log.debug(msg);
throw new RepositoryException(msg, ntce);
}
}
use of org.apache.jackrabbit.core.nodetype.EffectiveNodeType in project jackrabbit by apache.
the class AddMixinOperation method perform.
public Object perform(SessionContext context) throws RepositoryException {
int permissions = Permission.NODE_TYPE_MNGMT;
// is required in addition.
if (MIX_VERSIONABLE.equals(mixinName) || MIX_SIMPLE_VERSIONABLE.equals(mixinName)) {
permissions |= Permission.VERSION_MNGMT;
}
context.getItemValidator().checkModify(node, CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD, permissions);
NodeTypeManagerImpl ntMgr = context.getNodeTypeManager();
NodeTypeImpl mixin = ntMgr.getNodeType(mixinName);
if (!mixin.isMixin()) {
throw new RepositoryException(context.getJCRName(mixinName) + " is not a mixin node type");
}
Name primaryTypeName = node.getNodeState().getNodeTypeName();
NodeTypeImpl primaryType = ntMgr.getNodeType(primaryTypeName);
if (primaryType.isDerivedFrom(mixinName)) {
// new mixin is already included in primary type
return this;
}
// build effective node type of mixin's & primary type in order
// to detect conflicts
NodeTypeRegistry ntReg = context.getNodeTypeRegistry();
EffectiveNodeType entExisting;
try {
// existing mixin's
Set<Name> mixins = new HashSet<Name>(node.getNodeState().getMixinTypeNames());
// build effective node type representing primary type including
// existing mixin's
entExisting = ntReg.getEffectiveNodeType(primaryTypeName, mixins);
if (entExisting.includesNodeType(mixinName)) {
// new mixin is already included in existing mixin type(s)
return this;
}
// add new mixin
mixins.add(mixinName);
// try to build new effective node type (will throw in case
// of conflicts)
ntReg.getEffectiveNodeType(primaryTypeName, mixins);
} catch (NodeTypeConflictException e) {
throw new ConstraintViolationException(e.getMessage(), e);
}
// try to revert the changes in case an exception occurs
try {
// modify the state of this node
NodeState thisState = (NodeState) node.getOrCreateTransientItemState();
// add mixin name
Set<Name> mixins = new HashSet<Name>(thisState.getMixinTypeNames());
mixins.add(mixinName);
thisState.setMixinTypeNames(mixins);
// set jcr:mixinTypes property
node.setMixinTypesProperty(mixins);
// add 'auto-create' properties defined in mixin type
for (PropertyDefinition aPda : mixin.getAutoCreatedPropertyDefinitions()) {
PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
// make sure that the property is not already defined
// by primary type or existing mixin's
NodeTypeImpl declaringNT = (NodeTypeImpl) pd.getDeclaringNodeType();
if (!entExisting.includesNodeType(declaringNT.getQName())) {
node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
}
}
// recursively add 'auto-create' child nodes defined in mixin type
for (NodeDefinition aNda : mixin.getAutoCreatedNodeDefinitions()) {
NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
// make sure that the child node is not already defined
// by primary type or existing mixin's
NodeTypeImpl declaringNT = (NodeTypeImpl) nd.getDeclaringNodeType();
if (!entExisting.includesNodeType(declaringNT.getQName())) {
node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
}
}
} catch (RepositoryException re) {
// try to undo the modifications by removing the mixin
try {
node.removeMixin(mixinName);
} catch (RepositoryException re1) {
// silently ignore & fall through
}
throw re;
}
return this;
}
use of org.apache.jackrabbit.core.nodetype.EffectiveNodeType in project jackrabbit by apache.
the class BatchedItemOperations method createNodeState.
/**
* Creates a new node based on the given definition.
* <p>
* Note that access rights are <b><i>not</i></b> enforced!
* <p>
* <b>Precondition:</b> the state manager needs to be in edit mode.
*
* @param parent
* @param nodeName
* @param nodeTypeName
* @param mixinNames
* @param id
* @param def
* @return
* @throws ItemExistsException
* @throws ConstraintViolationException
* @throws RepositoryException
* @throws IllegalStateException
*/
public NodeState createNodeState(NodeState parent, Name nodeName, Name nodeTypeName, Name[] mixinNames, NodeId id, QNodeDefinition def) throws ItemExistsException, ConstraintViolationException, RepositoryException, IllegalStateException {
// check for name collisions with existing nodes
if (!def.allowsSameNameSiblings() && parent.hasChildNodeEntry(nodeName)) {
NodeId errorId = parent.getChildNodeEntry(nodeName, 1).getId();
throw new ItemExistsException(safeGetJCRPath(errorId));
}
if (nodeTypeName == null) {
// no primary node type specified,
// try default primary type from definition
nodeTypeName = def.getDefaultPrimaryType();
if (nodeTypeName == null) {
String msg = "an applicable node type could not be determined for " + nodeName;
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
NodeState node = stateMgr.createNew(id, nodeTypeName, parent.getNodeId());
if (mixinNames != null && mixinNames.length > 0) {
node.setMixinTypeNames(new HashSet<Name>(Arrays.asList(mixinNames)));
}
// now add new child node entry to parent
parent.addChildNodeEntry(nodeName, node.getNodeId());
EffectiveNodeType ent = getEffectiveNodeType(node);
// check shareable
if (ent.includesNodeType(NameConstants.MIX_SHAREABLE)) {
node.addShare(parent.getNodeId());
}
if (!node.getMixinTypeNames().isEmpty()) {
// create jcr:mixinTypes property
QPropertyDefinition pd = ent.getApplicablePropertyDef(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true);
createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
}
// add 'auto-create' properties defined in node type
for (QPropertyDefinition pd : ent.getAutoCreatePropDefs()) {
createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
}
// recursively add 'auto-create' child nodes defined in node type
for (QNodeDefinition nd : ent.getAutoCreateNodeDefs()) {
createNodeState(node, nd.getName(), nd.getDefaultPrimaryType(), null, null, nd);
}
// store node
stateMgr.store(node);
// store parent
stateMgr.store(parent);
return node;
}
Aggregations