use of javax.jcr.PropertyIterator in project jackrabbit by apache.
the class FilteringItemVisitor method visit.
/**
* Called when the Visitor is passed to a <code>Node</code>.
* <p>
* It calls <code>TraversingItemVisitor.entering(Node, int)</code> followed by
* <code>TraversingItemVisitor.leaving(Node, int)</code>. Implement these abstract methods to
* specify behaviour on 'arrival at' and 'after leaving' the <code>Node</code>.
* <p>
* If this method throws, the visiting process is aborted.
*
* @param node the <code>Node</code> that is accepting this visitor.
* @throws RepositoryException if an error occurrs
*/
public void visit(Node node) throws RepositoryException {
if (this.traversalPredicate.evaluate(node)) {
if (this.includePredicate == this.traversalPredicate || this.includePredicate.evaluate(node)) {
try {
if (!breadthFirst) {
// depth-first traversal
entering(node, currentLevel);
if (maxLevel == -1 || currentLevel < maxLevel) {
currentLevel++;
if (this.walkProperties) {
PropertyIterator propIter = node.getProperties();
while (propIter.hasNext()) {
propIter.nextProperty().accept(this);
}
}
NodeIterator nodeIter = node.getNodes();
while (nodeIter.hasNext()) {
nodeIter.nextNode().accept(this);
}
currentLevel--;
}
leaving(node, currentLevel);
} else {
// breadth-first traversal
entering(node, currentLevel);
leaving(node, currentLevel);
if (maxLevel == -1 || currentLevel < maxLevel) {
if (this.walkProperties) {
PropertyIterator propIter = node.getProperties();
while (propIter.hasNext()) {
nextQueue.addLast(propIter.nextProperty());
}
}
NodeIterator nodeIter = node.getNodes();
while (nodeIter.hasNext()) {
nextQueue.addLast(nodeIter.nextNode());
}
}
while (!currentQueue.isEmpty() || !nextQueue.isEmpty()) {
if (currentQueue.isEmpty()) {
currentLevel++;
currentQueue = nextQueue;
nextQueue = new LinkedList();
}
Item e = (Item) currentQueue.removeFirst();
e.accept(this);
}
currentLevel = 0;
}
} catch (RepositoryException re) {
currentLevel = 0;
throw re;
}
}
}
}
use of javax.jcr.PropertyIterator 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 javax.jcr.PropertyIterator in project jackrabbit by apache.
the class NodeImpl method setPrimaryType.
/**
* {@inheritDoc}
*/
public void setPrimaryType(String nodeTypeName) throws NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
// check state of this instance
sanityCheck();
// make sure this node is checked-out, neither protected nor locked and
// the editing session has sufficient permission to change the primary type.
int options = ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CONSTRAINTS | ItemValidator.CHECK_HOLD;
sessionContext.getItemValidator().checkModify(this, options, Permission.NODE_TYPE_MNGMT);
final NodeState state = data.getNodeState();
if (state.getParentId() == null) {
String msg = "changing the primary type of the root node is not supported";
log.debug(msg);
throw new RepositoryException(msg);
}
Name ntName = sessionContext.getQName(nodeTypeName);
if (ntName.equals(state.getNodeTypeName())) {
log.debug("Node already has " + nodeTypeName + " as primary node type.");
return;
}
NodeTypeManagerImpl ntMgr = sessionContext.getNodeTypeManager();
NodeType nt = ntMgr.getNodeType(ntName);
if (nt.isMixin()) {
throw new ConstraintViolationException(nodeTypeName + ": not a primary node type.");
} else if (nt.isAbstract()) {
throw new ConstraintViolationException(nodeTypeName + ": is an abstract node type.");
}
// build effective node type of new primary type & existing mixin's
// in order to detect conflicts
NodeTypeRegistry ntReg = ntMgr.getNodeTypeRegistry();
EffectiveNodeType entNew, entOld, entAll;
try {
entNew = ntReg.getEffectiveNodeType(ntName);
entOld = ntReg.getEffectiveNodeType(state.getNodeTypeName());
// try to build new effective node type (will throw in case of conflicts)
entAll = ntReg.getEffectiveNodeType(ntName, state.getMixinTypeNames());
} catch (NodeTypeConflictException ntce) {
throw new ConstraintViolationException(ntce.getMessage());
}
// get applicable definition for this node using new primary type
QNodeDefinition nodeDef;
try {
NodeImpl parent = (NodeImpl) getParent();
nodeDef = parent.getApplicableChildNodeDefinition(getQName(), ntName).unwrap();
} catch (RepositoryException re) {
String msg = this + ": no applicable definition found in parent node's node type";
log.debug(msg);
throw new ConstraintViolationException(msg, re);
}
if (!nodeDef.equals(itemMgr.getDefinition(state).unwrap())) {
onRedefine(nodeDef);
}
Set<QItemDefinition> oldDefs = new HashSet<QItemDefinition>(Arrays.asList(entOld.getAllItemDefs()));
Set<QItemDefinition> newDefs = new HashSet<QItemDefinition>(Arrays.asList(entNew.getAllItemDefs()));
Set<QItemDefinition> allDefs = new HashSet<QItemDefinition>(Arrays.asList(entAll.getAllItemDefs()));
// added child item definitions
Set<QItemDefinition> addedDefs = new HashSet<QItemDefinition>(newDefs);
addedDefs.removeAll(oldDefs);
// referential integrity check
boolean referenceableOld = entOld.includesNodeType(NameConstants.MIX_REFERENCEABLE);
boolean referenceableNew = entNew.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 primary type 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");
}
}
// do the actual modifications in content as mandated by the new primary type
// modify the state of this node
NodeState thisState = (NodeState) getOrCreateTransientItemState();
thisState.setNodeTypeName(ntName);
// set jcr:primaryType property
internalSetProperty(NameConstants.JCR_PRIMARYTYPE, InternalValue.create(ntName));
// 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) {
try {
PropertyState propState = (PropertyState) stateMgr.getItemState(new PropertyId(thisState.getNodeId(), propName));
if (!allDefs.contains(itemMgr.getDefinition(propState).unwrap())) {
// redefine property if possible
try {
PropertyImpl prop = (PropertyImpl) itemMgr.getItem(propState.getId());
if (prop.getDefinition().isProtected()) {
// remove 'orphaned' protected properties immediately
removeChildProperty(propName);
continue;
}
PropertyDefinitionImpl pdi = getApplicablePropertyDefinition(propName, propState.getType(), propState.isMultiValued(), false);
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 cve) {
// 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);
try {
NodeState nodeState = (NodeState) stateMgr.getItemState(entry.getId());
if (!allDefs.contains(itemMgr.getDefinition(nodeState).unwrap())) {
// redefine node if possible
try {
NodeImpl node = (NodeImpl) itemMgr.getItem(nodeState.getId());
if (node.getDefinition().isProtected()) {
// remove 'orphaned' protected child node immediately
removeChildNode(entry.getId());
continue;
}
NodeDefinitionImpl ndi = getApplicableChildNodeDefinition(entry.getName(), nodeState.getNodeTypeName());
// redefine node
node.onRedefine(ndi.unwrap());
// update collection of added definitions
addedDefs.remove(ndi.unwrap());
} catch (ConstraintViolationException cve) {
// no suitable definition found for this child node,
// remove it
removeChildNode(entry.getId());
}
}
} catch (ItemStateException ise) {
String msg = entry.getName() + ": failed to retrieve node state";
log.error(msg, ise);
throw new RepositoryException(msg, ise);
}
}
// type and at the same time were not present with the old nt
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 javax.jcr.PropertyIterator in project jackrabbit by apache.
the class ItemSequenceTest method checkTreeProperty.
private static void checkTreeProperty(Node root, int minChildren, int maxChildren, Comparator<String> order) throws RepositoryException {
int depth = -1;
for (Node node : new TreeTraverser(root)) {
String parentName = node.getName();
if (node.hasNodes()) {
int childCount = 0;
for (NodeIterator nodes = node.getNodes(); nodes.hasNext(); ) {
Node child = nodes.nextNode();
childCount++;
if (!root.isSame(node)) {
assertTrue("Mismatching order: node " + node + " contains child " + child, order.compare(parentName, child.getName()) <= 0);
}
}
if (!root.isSame(node)) {
assertTrue("Node " + node + " should have at least " + minChildren + " child nodes", minChildren <= childCount);
}
assertTrue("Node " + node + " should have no more than " + maxChildren + " child nodes", maxChildren >= childCount);
} else {
if (depth == -1) {
depth = node.getDepth();
} else {
assertEquals("Node " + node + " has depth " + node.getDepth() + " instead of " + depth, depth, node.getDepth());
}
int propCount = 0;
for (PropertyIterator properties = node.getProperties(); properties.hasNext(); ) {
Property property = properties.nextProperty();
String propertyName = property.getName();
if (!JcrConstants.JCR_PRIMARYTYPE.equals(propertyName)) {
propCount++;
assertTrue("Mismatching order: node " + node + " contains property " + property, order.compare(parentName, propertyName) <= 0);
}
}
if (propCount > 0) {
assertTrue("Node " + node + " should have at least " + minChildren + " properties", minChildren <= propCount);
assertTrue("Node" + node + " should have no more than " + maxChildren + " properties", maxChildren >= propCount);
}
}
}
}
use of javax.jcr.PropertyIterator in project jackrabbit by apache.
the class ItemStateHierarchyManagerDeadlockTest method retrieveNodesUnderInvRootNode.
public void retrieveNodesUnderInvRootNode() {
System.out.println("Start retrieveNodesUnderInvRootNode ");
Session session = null;
try {
session = login();
// start from the bottom of the tree and move up
Node inventoryRoot = getInvRootNode(session);
NodeIterator nodes = inventoryRoot.getNodes();
while (nodes.hasNext()) {
Node node = nodes.nextNode();
// System.out.println(" Node: " + node.getName());
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property prop = properties.nextProperty();
// System.out.println(" Prop: " + prop.getName() + " - " + prop.getString());
}
}
session.save();
System.out.println("End retrieveNodesUnderInvRootNode");
} catch (Exception e) {
System.err.println("Exception in retrieveNodesUnderInvRootNode:" + e.getMessage());
} finally {
if (session != null)
session.logout();
}
}
Aggregations