/** * {@inheritDoc} * <p> * This method does not clone the results. * </p> * * @see org.modeshape.graph.request.ChangeRequest#clone() */ @Override public CreateWorkspaceRequest clone() { CreateWorkspaceRequest request = new CreateWorkspaceRequest( actualWorkspaceName != null ? actualWorkspaceName : desiredNameOfNewWorkspace, createConflictBehavior); request.setActualWorkspaceName(actualWorkspaceName); request.setActualRootLocation(actualLocationOfRoot); return request; }
/** * {@inheritDoc} * * @see java.lang.Object#toString() */ @Override public String toString() { return "create workspace '" + desiredNameOfNewWorkspace() + "'"; }
/** * Set the actual name of the workspace that was created. This should be the same as the {@link #desiredNameOfNewWorkspace() * desired name} unless there was a conflict and the {@link #conflictBehavior() desired behavior} was to * {@link CreateConflictBehavior#CREATE_WITH_ADJUSTED_NAME alter the name}. * * @param actualWorkspaceName the actual name of the workspace that was created, or null if a workspace was not created * @throws IllegalStateException if the request is frozen */ public void setActualWorkspaceName( String actualWorkspaceName ) { checkNotFrozen(); this.actualWorkspaceName = actualWorkspaceName; }
/** * {@inheritDoc} * * @see org.modeshape.graph.Graph.NameWorkspace#named(java.lang.String) */ public Workspace named( String workspaceName ) { CreateWorkspaceRequest request = requests.createWorkspace(workspaceName, CreateConflictBehavior.DO_NOT_CREATE); return setWorkspace(request.getActualWorkspaceName(), request.getActualLocationOfRoot()); }
/** * {@inheritDoc} * * @see org.modeshape.graph.request.processor.RequestProcessor#process(org.modeshape.graph.request.CreateWorkspaceRequest) */ @Override public void process( CreateWorkspaceRequest request ) { if (!creatingWorkspacesAllowed(request)) return; String clonedWorkspaceName = null; WorkspaceType workspace = repository.createWorkspace(txn, request.desiredNameOfNewWorkspace(), request.conflictBehavior(), clonedWorkspaceName); if (workspace == null) { String msg = GraphI18n.workspaceAlreadyExistsInRepository.text(request.desiredNameOfNewWorkspace(), repository.getSourceName()); request.setError(new InvalidWorkspaceException(msg)); } else { Node root = txn.getRootNode(workspace); request.setActualRootLocation(Location.create(pathFactory.createRootPath(), root.getUuid())); request.setActualWorkspaceName(workspace.getName()); recordChange(request); } }
@Test public void shouldCreateValidRequestWithValidLocation() { request = new CreateWorkspaceRequest(workspace1, CreateConflictBehavior.CREATE_WITH_ADJUSTED_NAME); assertThat(request.desiredNameOfNewWorkspace(), is(workspace1)); assertThat(request.conflictBehavior(), is(CreateConflictBehavior.CREATE_WITH_ADJUSTED_NAME)); assertThat(request.hasError(), is(false)); assertThat(request.getError(), is(nullValue())); }
@Override public void process( CreateWorkspaceRequest request ) { // Just update the actual location String workspaceName = request.desiredNameOfNewWorkspace(); if (workspaceName == null) workspaceName = "default"; request.setActualWorkspaceName(workspaceName); request.setActualRootLocation(Location.create(context.getValueFactories().getPathFactory().createRootPath())); }
/** * Add a request to create a new workspace, and specify the behavior should a workspace already exists with a name that * matches the desired name for the new workspace. * * @param desiredNameOfNewWorkspace the desired name of the new workspace * @param createConflictBehavior the behavior if a workspace already exists with the same name, or null if the default * behavior should be used * @return the request; never null */ public CreateWorkspaceRequest createWorkspace( String desiredNameOfNewWorkspace, CreateConflictBehavior createConflictBehavior ) { return process(new CreateWorkspaceRequest(desiredNameOfNewWorkspace, createConflictBehavior)); }
@Test public void shouldAllowCreatingRequestWithNullCreateConflictBehavior() { request = new CreateWorkspaceRequest(workspace1, null); assertThat(request.conflictBehavior(), is(CreateWorkspaceRequest.DEFAULT_CREATE_CONFLICT_BEHAVIOR)); }
protected void assertNextRequestIsCreateWorkspace( String workspaceName, CreateConflictBehavior createConflictBehavior ) { Request request = executedRequests.poll(); assertThat(request, is(instanceOf(CreateWorkspaceRequest.class))); CreateWorkspaceRequest create = (CreateWorkspaceRequest)request; assertThat(create.desiredNameOfNewWorkspace(), is(workspaceName)); assertThat(create.conflictBehavior(), is(createConflictBehavior)); }
@Test public void shouldConsiderNotEqualTwoRequestsWithDifferentDesiredWorkspaceNames() { request = new CreateWorkspaceRequest(workspace1, CreateConflictBehavior.CREATE_WITH_ADJUSTED_NAME); CreateWorkspaceRequest request2 = new CreateWorkspaceRequest(workspace2, CreateConflictBehavior.CREATE_WITH_ADJUSTED_NAME); assertThat(request.equals(request2), is(false)); } }
@Override public void process( CreateWorkspaceRequest request ) { // There's a separate flag to allow creating workspaces (which may not require modifying existing data) // if (!updatesAllowed(request)) return; if (!repository.isWritable()) { String msg = GraphI18n.sourceIsReadOnly.text(repository.getSourceName()); request.setError(new InvalidRequestException(msg)); return; } WritablePathRepository writableRepo = (WritablePathRepository)repository; PathWorkspace workspace = writableRepo.createWorkspace(getExecutionContext(), request.desiredNameOfNewWorkspace(), request.conflictBehavior()); if (workspace == null) { String msg = GraphI18n.workspaceAlreadyExistsInRepository.text(request.desiredNameOfNewWorkspace(), repository.getSourceName()); request.setError(new InvalidWorkspaceException(msg)); } else { request.setActualRootLocation(Location.create(pathFactory.createRootPath(), repository.getRootNodeUuid())); request.setActualWorkspaceName(workspace.getName()); recordChange(request); } }
@Override protected Request createRequest() { return new CreateWorkspaceRequest(workspace1, CreateConflictBehavior.DO_NOT_CREATE); }
/** * {@inheritDoc} * * @see org.modeshape.graph.Graph.CreateWorkspace#namedSomethingLike(java.lang.String) */ public Workspace namedSomethingLike( String workspaceName ) { CreateWorkspaceRequest request = requests.createWorkspace(workspaceName, CreateConflictBehavior.CREATE_WITH_ADJUSTED_NAME); return setWorkspace(request.getActualWorkspaceName(), request.getActualLocationOfRoot()); }
/** * {@inheritDoc} * * @see org.modeshape.graph.request.processor.RequestProcessor#process(org.modeshape.graph.request.CreateWorkspaceRequest) */ @Override public void process( CreateWorkspaceRequest request ) { if (!updatesAllowed(request)) return; MapWorkspace workspace = repository.createWorkspace(getExecutionContext(), request.desiredNameOfNewWorkspace(), request.conflictBehavior()); if (workspace == null) { String msg = GraphI18n.workspaceAlreadyExistsInRepository.text(request.desiredNameOfNewWorkspace(), repository.getSourceName()); request.setError(new InvalidWorkspaceException(msg)); } else { MapNode root = workspace.getRoot(); request.setActualRootLocation(Location.create(pathFactory.createRootPath(), root.getUuid())); request.setActualWorkspaceName(workspace.getName()); recordChange(request); } }
/** * Add a request to create a new workspace, and specify the behavior should a workspace already exists with a name that * matches the desired name for the new workspace. * * @param desiredNameOfNewWorkspace the desired name of the new workspace * @param createConflictBehavior the behavior if a workspace already exists with the same name, or null if the default * behavior should be used * @return this builder for method chaining; never null */ public BatchRequestBuilder createWorkspace( String desiredNameOfNewWorkspace, CreateConflictBehavior createConflictBehavior ) { return add(new CreateWorkspaceRequest(desiredNameOfNewWorkspace, createConflictBehavior)); }
/** * {@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)) { CreateWorkspaceRequest that = (CreateWorkspaceRequest)obj; if (!this.desiredNameOfNewWorkspace.equals(that.desiredNameOfNewWorkspace())) return false; return true; } return false; }
/** * Set the actual location of the root node in the new workspace. * * @param actualLocationOfRoot the actual location of the workspace's root node. * @throws IllegalStateException if the request is frozen */ public void setActualRootLocation( Location actualLocationOfRoot ) { checkNotFrozen(); this.actualLocationOfRoot = actualLocationOfRoot; }
@Test public void shouldConsiderEqualTwoRequestsWithDifferentCreateConflictBehaviors() { request = new CreateWorkspaceRequest(workspace1, CreateConflictBehavior.DO_NOT_CREATE); CreateWorkspaceRequest request2 = new CreateWorkspaceRequest(workspace1, CreateConflictBehavior.CREATE_WITH_ADJUSTED_NAME); assertThat(request, is(request2)); }
@Test( expected = IllegalArgumentException.class ) public void shouldNotAllowCreatingRequestWithNullWorkspaceName() { new CreateWorkspaceRequest(null, CreateConflictBehavior.DO_NOT_CREATE); }