use of org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction in project kie-wb-common by kiegroup.
the class DocumentUpload method doUpload.
@Override
public void doUpload(final Document document, File file) {
if (!enabled) {
return;
}
DocumentData documentData = new DocumentData(document.getId(), document.getName(), document.getSize(), document.getUrl(), (long) document.getLastModified());
DocumentPreview preview = render(documentData);
DocumentPreviewStateActionsHandlerImpl handler = new DocumentPreviewStateActionsHandlerImpl(DocumentPreviewState.PENDING);
DocumentPreviewStateAction abortAction = new DocumentPreviewStateAction(translationService.getTranslation(Constants.DocumentUploadViewImplAbort), () -> uploader.remove(document.getId(), () -> doRemove(preview)));
handler.addStateActions(DocumentPreviewState.UPLOADING, Collections.singletonList(abortAction));
DocumentPreviewStateAction removeAction = new DocumentPreviewStateAction(translationService.getTranslation(Constants.DocumentUploadViewImplRemove), () -> uploader.remove(document.getId(), () -> doRemove(preview)));
handler.addStateActions(DocumentPreviewState.PENDING, Collections.singletonList(removeAction));
handler.addStateActions(DocumentPreviewState.UPLOADED, Collections.singletonList(removeAction));
final Command startUploadCallback = () -> handler.notifyStateChange(DocumentPreviewState.UPLOADING);
final ParameterizedCommand<Boolean> onFinishUpload = success -> {
if (success) {
handler.notifyStateChange(DocumentPreviewState.UPLOADED);
} else {
handler.notifyStateChange(DocumentPreviewState.ERROR);
}
};
DocumentPreviewStateAction retryAction = new DocumentPreviewStateAction(translationService.getTranslation(Constants.DocumentUploadViewImplRetry), () -> {
uploader.remove(document.getId(), () -> uploader.upload(document.getId(), file, startUploadCallback, onFinishUpload));
});
handler.addStateActions(DocumentPreviewState.ERROR, Arrays.asList(removeAction, retryAction));
preview.setStateHandler(handler);
uploader.upload(document.getId(), file, startUploadCallback, onFinishUpload);
ValueChangeEvent.fire(DocumentUpload.this, getValue());
}
use of org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction in project kie-wb-common by kiegroup.
the class DocumentUploadTest method testDrop.
@Test
public void testDrop() {
Document doc = mock(Document.class);
when(doc.getId()).thenReturn(DOC_1);
when(doc.getName()).thenReturn(DOC_1);
when(doc.getSize()).thenReturn(1024);
when(doc.getLastModified()).thenReturn((double) System.currentTimeMillis());
File file = mock(File.class);
documentUpload.doUpload(doc, file);
verify(instance).get();
verify(view).addDocument(any());
List<DocumentPreview> previews = (List<DocumentPreview>) documentUpload.getCurrentPreviews();
Assertions.assertThat(previews).isNotNull().hasSize(1);
ArgumentCaptor<DocumentData> documentDataArgumentCaptor = ArgumentCaptor.forClass(DocumentData.class);
DocumentPreview preview = previews.get(0);
verify(preview).init(documentDataArgumentCaptor.capture());
DocumentData documentData = documentDataArgumentCaptor.getValue();
Assertions.assertThat(documentData).isNotNull().hasFieldOrPropertyWithValue("contentId", DOC_1).hasFieldOrPropertyWithValue("fileName", DOC_1).hasFieldOrPropertyWithValue("size", Long.valueOf(1024));
ArgumentCaptor<DocumentPreviewStateActionsHandler> handlerCaptor = ArgumentCaptor.forClass(DocumentPreviewStateActionsHandler.class);
verify(preview).setStateHandler(handlerCaptor.capture());
DocumentPreviewStateActionsHandler handler = handlerCaptor.getValue();
ArgumentCaptor<Command> startUploadCaptor = ArgumentCaptor.forClass(Command.class);
ArgumentCaptor<ParameterizedCommand> uploadResultCaptor = ArgumentCaptor.forClass(ParameterizedCommand.class);
verify(uploader).upload(eq(DOC_1), any(), startUploadCaptor.capture(), uploadResultCaptor.capture());
startUploadCaptor.getValue().execute();
verify(preview).setState(DocumentPreviewState.UPLOADING);
uploadResultCaptor.getValue().execute(false);
verify(preview).setState(DocumentPreviewState.ERROR);
DocumentPreviewStateAction action = ((List<DocumentPreviewStateAction>) handler.getCurrentStateActions()).get(1);
action.execute();
verify(uploader).remove(eq(DOC_1), any());
uploadResultCaptor.getValue().execute(true);
verify(preview).setState(DocumentPreviewState.UPLOADED);
}
use of org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction in project kie-wb-common by kiegroup.
the class DocumentUpload method setValue.
@Override
public void setValue(List<DocumentData> value, boolean fireEvents) {
if (value == null) {
value = new ArrayList<>();
}
if (getValue().containsAll(value)) {
return;
}
this.clear();
value.forEach(documentData -> {
DocumentPreview preview = render(documentData);
DocumentPreviewStateActionsHandlerImpl handler = new DocumentPreviewStateActionsHandlerImpl(DocumentPreviewState.STORED);
DocumentPreviewStateAction action = new DocumentPreviewStateAction(translationService.getTranslation(Constants.DocumentUploadViewImplRemove), () -> doRemove(preview));
handler.addStateActions(DocumentPreviewState.STORED, Collections.singletonList(action));
preview.setStateHandler(handler);
});
if (fireEvents) {
ValueChangeEvent.fire(this, value);
}
}
Aggregations