/** * Create a request to move a branch from one location into another. * * @param from the location of the top node in the existing branch that is to be moved * @param into the location of the existing node into which the branch should be moved * @param workspaceName the name of the workspace * @return the request; never null * @throws IllegalArgumentException if any of the parameters are null */ public MoveBranchRequest moveBranch( Location from, Location into, String workspaceName ) { return process(new MoveBranchRequest(from, into, workspaceName, MoveBranchRequest.DEFAULT_CONFLICT_BEHAVIOR)); }
if (!updatesAllowed(request)) return; WorkspaceType workspace = getWorkspace(request, request.inWorkspace()); if (workspace == null) return; NodeType beforeNode = request.before() != null ? getTargetNode(workspace, request, request.before()) : null; NodeType node = getTargetNode(workspace, request, request.from()); if (node == null) return; if (request.hasError()) return; // if beforeNode could not be found Location oldLocation = getActualLocation(workspace, request.from(), node); if (request.into() != null) { newParent = txn.getNode(workspace, request.into()); // this will fail if there is no node newLocation = txn.addChild(workspace, newParent, node, null, request.desiredName()); } else { if (newParent == null) { request.setError(new PathNotFoundException(request.into(), pathFactory.createRootPath(), GraphI18n.nodeDoesNotExist.text("parent of root"))); return; newLocation = txn.addChild(workspace, newParent, node, beforeNode, request.desiredName()); request.setActualLocations(oldLocation, newLocation); recordChange(request);
/** * {@inheritDoc} * * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals( Object obj ) { if (obj == this) return true; if (this.getClass().isInstance(obj)) { MoveBranchRequest that = (MoveBranchRequest)obj; if (!this.from().isSame(that.from())) return false; if (!this.into().isSame(that.into())) return false; if (!this.conflictBehavior().equals(that.conflictBehavior())) return false; if (!this.workspaceName.equals(that.workspaceName)) return false; return true; } return false; }
/** * {@inheritDoc} * <p> * This method does not clone the results. * </p> * * @see org.modeshape.graph.request.ChangeRequest#clone() */ @Override public MoveBranchRequest clone() { MoveBranchRequest request = new MoveBranchRequest(actualOldLocation != null ? actualOldLocation : from, into, before, workspaceName, desiredNameForNode, conflictBehavior); request.setActualLocations(actualOldLocation, actualNewLocation); return request; }
/** * {@inheritDoc} * * @see org.modeshape.graph.request.processor.RequestProcessor#process(org.modeshape.graph.request.MoveBranchRequest) */ @Override public void process( MoveBranchRequest request ) { MoveBranchRequest source = (MoveBranchRequest)federatedRequest.getFirstProjectedRequest().getRequest(); if (checkErrorOrCancel(request, source)) return; request.setActualLocations(source.getActualLocationBefore(), source.getActualLocationAfter()); }
@Test public void shouldCreateValidRequestWithValidFromLocationAndValidToLocation() { request = new MoveBranchRequest(validPathLocation1, validPathLocation2, workspace1); assertThat(request.from(), is(sameInstance(validPathLocation1))); assertThat(request.into(), is(sameInstance(validPathLocation2))); assertThat(request.inWorkspace(), is(sameInstance(workspace1))); assertThat(request.hasError(), is(false)); assertThat(request.getError(), is(nullValue())); }
ProjectedNode projectedFromNode = project(request.from(), request.inWorkspace(), request, true); if (projectedFromNode == null) { submit(new FederatedRequest(request)); ProjectedNode projectedBeforeNode = request.before() != null ? project(request.before(), request.inWorkspace(), request, true) : null; if (request.into() != null) { ProjectedNode projectedIntoNode = project(request.into(), request.inWorkspace(), request, true); if (projectedIntoNode == null) return; String msg = GraphI18n.moveLimitedToBeWithinSingleSource.text(readable(request.from()), request.inWorkspace(), readable(request.into()), request.inWorkspace(), getSourceName()); request.setError(new UnsupportedRequestException(msg)); return; ProxyNode beforeProxy = request.before() != null ? projectedBeforeNode.asProxy() : null; assert fromProxy.projection().getSourceName().equals(intoProxy.projection().getSourceName()); sameLocation = fromProxy.isSameLocationAsOriginal() && intoProxy.isSameLocationAsOriginal(); MoveBranchRequest pushDown = new MoveBranchRequest(fromProxy.location(), intoProxy.location(), beforeProxyLocation, intoProxy.workspaceName(), request.desiredName(), request.conflictBehavior());
/** * {@inheritDoc} * * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("read ").append(printable(from())).append(" (in "); if (inWorkspace() != null) { sb.append('\'').append(inWorkspace()).append('\''); } else { sb.append("default"); } sb.append(" workspace) "); if (desiredName() != null) { sb.append("with name '").append(desiredName()).append("' "); } else { sb.append("into "); } if (into() != null) { sb.append("into ").append(printable(into())); } else { sb.append("before ").append(printable(before())); } return sb.toString(); }
/** * {@inheritDoc} * * @see org.modeshape.graph.request.processor.RequestProcessor#process(org.modeshape.graph.request.MoveBranchRequest) */ @Override public void process( MoveBranchRequest request ) { ProjectedRequest projected = federatedRequest.getFirstProjectedRequest(); // Check the projection first ... if (checkErrorOrCancel(request, federatedRequest)) return; MoveBranchRequest source = (MoveBranchRequest)projected.getRequest(); if (checkErrorOrCancel(request, source)) return; Location locationBefore = source.getActualLocationBefore(); Location locationAfter = source.getActualLocationAfter(); locationBefore = projectToFederated(request.from(), projected.getProjection(), locationBefore, request); Projection afterProjection = projected.getSecondProjection(); if (afterProjection == null) projected.getProjection(); locationAfter = projectToFederated(request.into(), afterProjection, locationAfter, request); request.setActualLocations(locationBefore, locationAfter); }
@Override public void process( MoveBranchRequest request ) { // Just update the actual location ... Name newName = request.desiredName(); if (newName == null && request.from().hasPath()) newName = request.from().getPath().getLastSegment().getName(); if (newName == null) newName = context.getValueFactories().getNameFactory().create("d"); // Figure out the new name and path (if needed)... Path newPath = null; if (request.into().hasPath()) { newPath = context.getValueFactories().getPathFactory().create(request.into().getPath(), newName); } else if (request.from().hasPath()) { newPath = context.getValueFactories().getPathFactory().create("/a/b/c"); newPath = context.getValueFactories().getPathFactory().create(newPath, newName); } else { newPath = context.getValueFactories().getPathFactory().create("/a/b/c"); newPath = context.getValueFactories().getPathFactory().create(newPath, newName); } // Figure out the old name and path ... Path oldPath = null; if (request.from().hasPath()) { oldPath = request.from().getPath(); } else { oldPath = context.getValueFactories().getPathFactory().create("/x/y/z"); oldPath = context.getValueFactories().getPathFactory().create(oldPath, newName); } Location fromLocation = request.from().hasIdProperties() ? Location.create(oldPath, request.from().getIdProperties()) : Location.create(oldPath); Location intoLocation = request.into().hasIdProperties() ? Location.create(newPath, request.into().getIdProperties()) : Location.create(newPath); request.setActualLocations(fromLocation, intoLocation); }
original = move.getActualLocationBefore(); Location before = move.before(); boolean isReorder = move.desiredName() == null; changeContext.move(workspace, original, location, before, isReorder); break;
/** * Process a request to rename a node specified location into a different location. * <p> * This method does nothing if the request is null. Unless overridden, this method converts the rename into a * {@link MoveBranchRequest move}. However, this only works if the <code>request</code> has a {@link Location#hasPath() path} * for its {@link RenameNodeRequest#at() location}. (If not, this method throws an {@link UnsupportedOperationException} and * must be overridden.) * </p> * * @param request the rename request */ public void process( RenameNodeRequest request ) { if (request == null) return; Location from = request.at(); if (!from.hasPath()) { throw new UnsupportedOperationException(); } Path newPath = getExecutionContext().getValueFactories().getPathFactory().create(from.getPath(), request.toName()); Location to = Location.create(newPath); MoveBranchRequest move = new MoveBranchRequest(from, to, request.inWorkspace()); process(move); // Set the actual locations ... request.setActualLocations(move.getActualLocationBefore(), move.getActualLocationAfter()); }
/** * {@inheritDoc} * * @see org.modeshape.graph.request.ChangeRequest#changes(java.lang.String, org.modeshape.graph.property.Path) */ @Override public boolean changes( String workspace, Path path ) { if (this.into() != null) { return this.workspaceName.equals(workspace) && (into.hasPath() && into.getPath().isAtOrBelow(path) || from.hasPath() && from.getPath().isAtOrBelow(path)); } // into or before must be non-null assert before() != null; return this.workspaceName.equals(workspace) && (before.hasPath() && before.getPath().getParent().isAtOrBelow(path) || from.hasPath() && from.getPath().isAtOrBelow(path)); }
@Test public void shouldConsiderNotEqualTwoRequestsWithDifferentLocations() { request = new MoveBranchRequest(validPathLocation1, validPathLocation2, workspace2); MoveBranchRequest request2 = new MoveBranchRequest(validPathLocation2, validPathLocation1, workspace2); assertThat(request.equals(request2), is(false)); } }
/** * Sets the actual and complete location of the node being renamed and its new location. This method must be called when * processing the request, and the actual location must have a {@link Location#getPath() path}. * * @param oldLocation the actual location of the node before being moved * @param newLocation the actual new location of the node * @throws IllegalArgumentException if the either location is null, or if the either location does not have a path * @throws IllegalStateException if the request is frozen */ public void setActualLocations( Location oldLocation, Location newLocation ) { checkNotFrozen(); CheckArg.isNotNull(oldLocation, "oldLocation"); CheckArg.isNotNull(newLocation, "newLocation"); if (!oldLocation.hasPath()) { throw new IllegalArgumentException(GraphI18n.actualOldLocationMustHavePath.text(oldLocation)); } if (!newLocation.hasPath()) { throw new IllegalArgumentException(GraphI18n.actualNewLocationMustHavePath.text(newLocation)); } Name actualNewName = newLocation.getPath().getLastSegment().getName(); Name expectedNewName = desiredName() != null ? desiredName() : oldLocation.getPath().getLastSegment().getName(); if (!actualNewName.equals(expectedNewName)) { throw new IllegalArgumentException(GraphI18n.actualLocationNotEqualToInputLocation.text(newLocation, into)); } this.actualOldLocation = oldLocation; this.actualNewLocation = newLocation; }
/** * Determine whether this move request can be determined to have no effect. * <p> * A move is known to have no effect when all of the following conditions are true: * <ul> * <li>the {@link #into() into} location has a {@link Location#hasPath() path} but no {@link Location#hasIdProperties() * identification properties};</li> * <li>the {@link #from() from} location has a {@link Location#getPath() path}; and</li> * <li>the {@link #from() from} location's {@link Path#getParent() parent} is the same as the {@link #into() into} location's * path.</li> * </ul> * If all of these conditions are not true, this method returns false. * </p> * * @return true if this move request really doesn't change the parent of the node, or false if it cannot be determined */ public boolean hasNoEffect() { if (into != null && into.hasPath() && into.hasIdProperties() == false && from.hasPath()) { if (!from.getPath().getParent().equals(into.getPath())) return false; if (desiredName() != null && !desiredName().equals(from.getPath().getLastSegment().getName())) return false; if (before != null) return false; return true; } // Can't be determined for certain return false; }
@Test public void shouldCreateValidRequestWithValidFromLocationAndValidToLocationAndValidBeforeLocation() { Name newName = new BasicName("", "newName"); request = new MoveBranchRequest(validPathLocation1, validPathLocation2, validPathLocation, workspace1, newName, NodeConflictBehavior.DO_NOT_REPLACE); assertThat(request.from(), is(sameInstance(validPathLocation1))); assertThat(request.into(), is(sameInstance(validPathLocation2))); assertThat(request.before(), is(sameInstance(validPathLocation))); assertThat(request.inWorkspace(), is(sameInstance(workspace1))); assertThat(request.hasError(), is(false)); assertThat(request.getError(), is(nullValue())); }
PathWorkspace workspace = getWorkspace(request, request.inWorkspace()); if (workspace == null) return; PathNode beforeNode = request.before() != null ? getTargetNode(workspace, request, request.before()) : null; PathNode node = getTargetNode(workspace, request, request.from()); if (node == null) return; if (request.hasError()) return; // if beforeNode could not be found if (request.into() != null) { newParentPath = request.into().getPath(); } else { if (newParent == null) { Path lowestExisting = workspace.getLowestExistingPath(newParentPath); request.setError(new PathNotFoundException(request.into(), lowestExisting, GraphI18n.nodeDoesNotExist.text(newParentPath))); return; request.setError(new InvalidRequestException(msg.text(repository.getSourceName(), workspace.getName()))); return; node = newWorkspace.moveNode(getExecutionContext(), node, request.desiredName(), newWorkspace, newParent, beforeNode); assert node.getPath().getParent().equals(newParent.getPath()); Location oldLocation = Location.create(request.from().getPath()); Location newLocation = Location.create(node.getPath(), node.getUuid()); request.setActualLocations(oldLocation, newLocation); recordChange(request);
/** * Create a request to move a branch from one location into another. * * @param from the location of the top node in the existing branch that is to be moved * @param into the location of the existing node into which the branch should be moved * @param workspaceName the name of the workspace * @return this builder for method chaining; never null * @throws IllegalArgumentException if any of the parameters are null */ public BatchRequestBuilder moveBranch( Location from, Location into, String workspaceName ) { return add(new MoveBranchRequest(from, into, workspaceName, MoveBranchRequest.DEFAULT_CONFLICT_BEHAVIOR)); }