use of org.apache.jackrabbit.spi.commons.name.PathBuilder in project jackrabbit by apache.
the class NodeImpl method orderBefore.
/**
* Same as <code>{@link Node#orderBefore(String, String)}</code> except that
* this method takes a <code>Path.Element</code> arguments instead of
* <code>String</code>s.
*
* @param srcName
* @param dstName
* @throws UnsupportedRepositoryOperationException
* @throws VersionException
* @throws ConstraintViolationException
* @throws ItemNotFoundException
* @throws LockException
* @throws RepositoryException
*/
public synchronized void orderBefore(Path.Element srcName, Path.Element dstName) throws UnsupportedRepositoryOperationException, VersionException, ConstraintViolationException, ItemNotFoundException, LockException, RepositoryException {
// check state of this instance
sanityCheck();
if (!getPrimaryNodeType().hasOrderableChildNodes()) {
throw new UnsupportedRepositoryOperationException("child node ordering not supported on " + this);
}
// check arguments
if (srcName.equals(dstName)) {
// there's nothing to do
return;
}
// check existence
if (!hasNode(srcName.getName(), srcName.getIndex())) {
String name;
try {
Path.Element[] path = new Path.Element[] { srcName };
name = sessionContext.getJCRPath(new PathBuilder(path).getPath());
} catch (NameException e) {
name = srcName.toString();
} catch (NamespaceException e) {
name = srcName.toString();
}
throw new ItemNotFoundException(this + " has no child node with name " + name);
}
if (dstName != null && !hasNode(dstName.getName(), dstName.getIndex())) {
String name;
try {
Path.Element[] path = new Path.Element[] { dstName };
name = sessionContext.getJCRPath(new PathBuilder(path).getPath());
} catch (NameException e) {
name = dstName.toString();
} catch (NamespaceException e) {
name = dstName.toString();
}
throw new ItemNotFoundException(this + " has no child node with name " + name);
}
// make sure this node is checked-out and neither protected nor locked
int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS;
sessionContext.getItemValidator().checkModify(this, options, Permission.NONE);
/*
make sure the session is allowed to reorder child nodes.
since there is no specific privilege for reordering child nodes,
test if the the node to be reordered can be removed and added,
i.e. treating reorder similar to a move.
TODO: properly deal with sns in which case the index would change upon reorder.
*/
AccessManager acMgr = sessionContext.getAccessManager();
PathBuilder pb = new PathBuilder(getPrimaryPath());
pb.addLast(srcName.getName(), srcName.getIndex());
Path childPath = pb.getPath();
if (!acMgr.isGranted(childPath, Permission.MODIFY_CHILD_NODE_COLLECTION)) {
String msg = "Not allowed to reorder child node " + sessionContext.getJCRPath(childPath) + ".";
log.debug(msg);
throw new AccessDeniedException(msg);
}
ArrayList<ChildNodeEntry> list = new ArrayList<ChildNodeEntry>(data.getNodeState().getChildNodeEntries());
int srcInd = -1, destInd = -1;
for (int i = 0; i < list.size(); i++) {
ChildNodeEntry entry = list.get(i);
if (srcInd == -1) {
if (entry.getName().equals(srcName.getName()) && (entry.getIndex() == srcName.getIndex() || srcName.getIndex() == 0 && entry.getIndex() == 1)) {
srcInd = i;
}
}
if (destInd == -1 && dstName != null) {
if (entry.getName().equals(dstName.getName()) && (entry.getIndex() == dstName.getIndex() || dstName.getIndex() == 0 && entry.getIndex() == 1)) {
destInd = i;
if (srcInd != -1) {
break;
}
}
} else {
if (srcInd != -1) {
break;
}
}
}
// check if resulting order would be different to current order
if (destInd == -1) {
if (srcInd == list.size() - 1) {
// no change, we're done
return;
}
} else {
if ((destInd - srcInd) == 1) {
// no change, we're done
return;
}
}
// reorder list
if (destInd == -1) {
list.add(list.remove(srcInd));
} else {
if (srcInd < destInd) {
list.add(destInd, list.get(srcInd));
list.remove(srcInd);
} else {
list.add(destInd, list.remove(srcInd));
}
}
// modify the state of 'this', i.e. the parent node
NodeState thisState = (NodeState) getOrCreateTransientItemState();
thisState.setChildNodeEntries(list);
}
use of org.apache.jackrabbit.spi.commons.name.PathBuilder in project jackrabbit by apache.
the class AggregateRuleImpl method getNodeIncludes.
/**
* Creates node includes defined in the <code>config</code>.
*
* @param config the indexing aggregate configuration.
* @return the node includes defined in the <code>config</code>.
* @throws MalformedPathException if a path in the configuration is
* malformed.
* @throws IllegalNameException if the node type name contains illegal
* characters.
* @throws NamespaceException if the node type contains an unknown
* prefix.
*/
private NodeInclude[] getNodeIncludes(Node config) throws MalformedPathException, IllegalNameException, NamespaceException {
List<NodeInclude> includes = new ArrayList<NodeInclude>();
NodeList childNodes = config.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node n = childNodes.item(i);
if (n.getNodeName().equals("include")) {
Name ntName = null;
Node ntAttr = n.getAttributes().getNamedItem("primaryType");
if (ntAttr != null) {
ntName = resolver.getQName(ntAttr.getNodeValue());
}
PathBuilder builder = new PathBuilder();
for (String element : Text.explode(getTextContent(n), '/')) {
if (element.equals("*")) {
builder.addLast(NameConstants.ANY_NAME);
} else {
builder.addLast(resolver.getQName(element));
}
}
includes.add(new NodeInclude(builder.getPath(), ntName));
}
}
return includes.toArray(new NodeInclude[includes.size()]);
}
use of org.apache.jackrabbit.spi.commons.name.PathBuilder in project jackrabbit by apache.
the class AncestorPathNodeJoin method getMatchingScoreNodes.
/**
* {@inheritDoc}
* <p>
* The outer query hits loop contains the ancestor nodes.
*/
public ScoreNode[][] getMatchingScoreNodes(ScoreNode ancestor) throws IOException {
try {
Path ancestorPath = hmgr.getPath(ancestor.getNodeId());
PathBuilder builder = new PathBuilder(ancestorPath);
builder.addAll(relPath.getElements());
return contextIndex.getScoreNodes(builder.getPath().getNormalizedPath());
} catch (RepositoryException e) {
// ignore, probably does not exist anymore
return null;
}
}
use of org.apache.jackrabbit.spi.commons.name.PathBuilder in project jackrabbit by apache.
the class NodeImpl method getPrimaryPath.
/**
* {@inheritDoc}
*
* Overridden to return a different path for shareable nodes.
*
* TODO SN: copies functionality in that is already available in
* HierarchyManagerImpl, namely composing a path by
* concatenating the parent path + this node's name and index:
* rather use hierarchy manager to do this
*/
@Override
public Path getPrimaryPath() throws RepositoryException {
if (!isShareable()) {
return super.getPrimaryPath();
}
NodeId parentId = getParentId();
NodeImpl parentNode = (NodeImpl) getParent();
Path parentPath = parentNode.getPrimaryPath();
PathBuilder builder = new PathBuilder(parentPath);
ChildNodeEntry entry = parentNode.getNodeState().getChildNodeEntry(getNodeId());
if (entry == null) {
String msg = "failed to build path of " + id + ": " + parentId + " has no child entry for " + id;
log.debug(msg);
throw new ItemNotFoundException(msg);
}
// add to path
if (entry.getIndex() == 1) {
builder.addLast(entry.getName());
} else {
builder.addLast(entry.getName(), entry.getIndex());
}
return builder.getPath();
}
use of org.apache.jackrabbit.spi.commons.name.PathBuilder in project jackrabbit by apache.
the class NodeEntryImpl method getDeepNodeEntry.
/**
* @see NodeEntry#getDeepNodeEntry(Path)
*/
public NodeEntry getDeepNodeEntry(Path path) throws PathNotFoundException, RepositoryException {
NodeEntryImpl entry = this;
Path.Element[] elems = path.getElements();
for (int i = 0; i < elems.length; i++) {
Path.Element elem = elems[i];
// check for root element
if (elem.denotesRoot()) {
if (entry.getParent() != null) {
throw new RepositoryException("NodeEntry out of 'hierarchy' " + path.toString());
}
continue;
}
int index = elem.getNormalizedIndex();
Name name = elem.getName();
// first try to resolve to known node or property entry
NodeEntry cne = entry.getNodeEntry(name, index, false);
if (cne != null) {
entry = (NodeEntryImpl) cne;
} else {
// on the persistent layer.
if (entry.childNodeEntries.isComplete()) {
throw new PathNotFoundException(factory.saveGetJCRPath(path));
}
// -> check for moved child entry in node-attic
// -> check if child points to a removed/moved sns
List<NodeEntry> siblings = entry.childNodeEntries.get(name);
if (entry.containsAtticChild(siblings, name, index)) {
throw new PathNotFoundException(factory.saveGetJCRPath(path));
}
// elements -> hierarchy doesn't exist anyway.
if (entry.getStatus() == Status.NEW) {
throw new PathNotFoundException(factory.saveGetJCRPath(path));
}
/*
* Unknown entry (not-existing or not yet loaded):
* Skip all intermediate entries and directly try to load the ItemState
* (including building the intermediate entries. If that fails
* ItemNotFoundException is thrown.
*
* Since 'path' might be ambiguous (Node or Property):
* 1) first try Node
* 2) if the NameElement does not have SNS-index => try Property
* 3) else throw
*/
PathBuilder pb = new PathBuilder(getPathFactory());
for (int j = i; j < elems.length; j++) {
pb.addLast(elems[j]);
}
Path remainingPath = pb.getPath();
NodeId parentId = entry.getWorkspaceId();
IdFactory idFactory = factory.getIdFactory();
NodeId nodeId = idFactory.createNodeId(parentId, remainingPath);
NodeEntry ne = entry.loadNodeEntry(nodeId);
if (ne != null) {
return ne;
} else {
throw new PathNotFoundException(factory.saveGetJCRPath(path));
}
}
}
return entry;
}
Aggregations