Search in sources :

Example 1 with CreateRequest

use of org.xwiki.refactoring.job.CreateRequest in project xwiki-platform by xwiki.

the class RefactoringScriptServiceTest method create.

@Test
public void create() throws Exception {
    DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
    getService().create(documentReference);
    ArgumentCaptor<CreateRequest> request = ArgumentCaptor.forClass(CreateRequest.class);
    verify(this.jobExecutor).execute(eq(RefactoringJobs.CREATE), request.capture());
    assertEquals(RefactoringJobs.CREATE, request.getValue().getJobType());
    assertEquals(Arrays.asList(documentReference), request.getValue().getEntityReferences());
    assertTrue(request.getValue().isDeep());
}
Also used : CreateRequest(org.xwiki.refactoring.job.CreateRequest) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 2 with CreateRequest

use of org.xwiki.refactoring.job.CreateRequest in project xwiki-platform by xwiki.

the class CreateJobTest method createRequest.

private CreateRequest createRequest(EntityReference targetReference, EntityReference templateReference) {
    CreateRequest request = new CreateRequest();
    request.setEntityReferences(Collections.singletonList(targetReference));
    request.setTemplateReference(templateReference);
    return request;
}
Also used : CreateRequest(org.xwiki.refactoring.job.CreateRequest)

Example 3 with CreateRequest

use of org.xwiki.refactoring.job.CreateRequest in project xwiki-platform by xwiki.

the class CreateJobTest method createSpaceFromTemplate.

@Test
public void createSpaceFromTemplate() throws Throwable {
    SpaceReference newSpaceReference = new SpaceReference("wiki", "Space");
    SpaceReference templateSpaceReference = new SpaceReference("wiki", "Code", "Template");
    DocumentReference templateDocumentReference = new DocumentReference("Index", templateSpaceReference);
    when(this.modelBridge.getDocumentReferences(templateSpaceReference)).thenReturn(Arrays.asList(templateDocumentReference));
    when(this.modelBridge.exists(templateDocumentReference)).thenReturn(true);
    DocumentReference newDocumentReference = new DocumentReference("Index", newSpaceReference);
    when(this.modelBridge.exists(newDocumentReference)).thenReturn(false);
    DocumentReference userReference = new DocumentReference("wiki", "Users", "Alice");
    CreateRequest request = createRequest(newSpaceReference, templateSpaceReference);
    request.setUserReference(userReference);
    request.setCheckRights(false);
    run(request);
    verify(this.modelBridge).setContextUserReference(userReference);
    verify(this.modelBridge).copy(templateDocumentReference, newDocumentReference);
    verify(this.modelBridge, never()).removeLock(any(DocumentReference.class));
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) CreateRequest(org.xwiki.refactoring.job.CreateRequest) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with CreateRequest

use of org.xwiki.refactoring.job.CreateRequest in project xwiki-platform by xwiki.

the class RefactoringScriptService method createCreateRequest.

/**
 * Creates a request to create the specified entities.
 *
 * @param entityReferences the entities to create
 * @return the create request
 * @since 7.4M2
 */
public CreateRequest createCreateRequest(Collection<EntityReference> entityReferences) {
    CreateRequest request = new CreateRequest();
    initEntityRequest(request, RefactoringJobs.CREATE, entityReferences);
    // Set deep create by default, to copy (if possible) any existing hierarchy of a specified template document.
    // TODO: expose this in the create UI to advanced users to allow them to opt-out?
    request.setDeep(true);
    return request;
}
Also used : CreateRequest(org.xwiki.refactoring.job.CreateRequest)

Example 5 with CreateRequest

use of org.xwiki.refactoring.job.CreateRequest in project xwiki-platform by xwiki.

the class SaveAction method startCreateJob.

private Job startCreateJob(EntityReference entityReference, EditForm editForm) throws XWikiException {
    if (StringUtils.isBlank(editForm.getTemplate())) {
        // No template specified, nothing more to do.
        return null;
    }
    // If a template is set in the request, then this is a create action which needs to be handled by a create job,
    // but skipping the target document, which is now already saved by the save action.
    RefactoringScriptService refactoring = (RefactoringScriptService) Utils.getComponent(ScriptService.class, "refactoring");
    CreateRequest request = refactoring.createCreateRequest(Arrays.asList(entityReference));
    // Set the target document.
    request.setEntityReferences(Arrays.asList(entityReference));
    // Set the template to use.
    DocumentReferenceResolver<String> resolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
    EntityReference templateReference = resolver.resolve(editForm.getTemplate());
    request.setTemplateReference(templateReference);
    // We`ve already created and populated the fields of the target document, focus only on the remaining children
    // specified in the template.
    request.setSkippedEntities(Arrays.asList(entityReference));
    Job createJob = refactoring.create(request);
    if (createJob != null) {
        return createJob;
    } else {
        throw new XWikiException(String.format("Failed to schedule the create job for [%s]", entityReference), refactoring.getLastError());
    }
}
Also used : RefactoringScriptService(org.xwiki.refactoring.script.RefactoringScriptService) ScriptService(org.xwiki.script.service.ScriptService) CreateRequest(org.xwiki.refactoring.job.CreateRequest) EntityReference(org.xwiki.model.reference.EntityReference) RefactoringScriptService(org.xwiki.refactoring.script.RefactoringScriptService) Job(org.xwiki.job.Job) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

CreateRequest (org.xwiki.refactoring.job.CreateRequest)13 Test (org.junit.Test)10 DocumentReference (org.xwiki.model.reference.DocumentReference)10 SpaceReference (org.xwiki.model.reference.SpaceReference)2 XWikiException (com.xpn.xwiki.XWikiException)1 Job (org.xwiki.job.Job)1 EntityReference (org.xwiki.model.reference.EntityReference)1 RefactoringScriptService (org.xwiki.refactoring.script.RefactoringScriptService)1 ScriptService (org.xwiki.script.service.ScriptService)1