Search in sources :

Example 1 with DocumentPreviewStateAction

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());
}
Also used : ValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) ValueChangeEvent(com.google.gwt.event.logical.shared.ValueChangeEvent) Arrays(java.util.Arrays) ManagedInstance(org.jboss.errai.ioc.client.api.ManagedInstance) Document(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.js.Document) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) HandlerRegistration(com.google.gwt.event.shared.HandlerRegistration) PreDestroy(javax.annotation.PreDestroy) DocumentData(org.kie.workbench.common.forms.jbpm.model.document.DocumentData) GwtEvent(com.google.gwt.event.shared.GwtEvent) IsWidget(com.google.gwt.user.client.ui.IsWidget) DocumentPreview(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview) Command(org.uberfire.mvp.Command) DocumentUploadManager(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.upload.DocumentUploadManager) TranslationService(org.jboss.errai.ui.client.local.spi.TranslationService) DocumentPreviewStateActionsHandlerImpl(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.handlers.DocumentPreviewStateActionsHandlerImpl) Collection(java.util.Collection) File(elemental2.dom.File) DocumentPreviewStateAction(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction) Collectors(java.util.stream.Collectors) Constants(org.kie.workbench.common.forms.jbpm.client.resources.i18n.Constants) List(java.util.List) Widget(com.google.gwt.user.client.ui.Widget) Dependent(javax.enterprise.context.Dependent) PostConstruct(javax.annotation.PostConstruct) DocumentPreviewState(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewState) Collections(java.util.Collections) DocumentData(org.kie.workbench.common.forms.jbpm.model.document.DocumentData) DocumentPreview(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview) DocumentPreviewStateAction(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) Command(org.uberfire.mvp.Command) DocumentPreviewStateActionsHandlerImpl(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.handlers.DocumentPreviewStateActionsHandlerImpl)

Example 2 with DocumentPreviewStateAction

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);
}
Also used : Document(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.js.Document) DocumentData(org.kie.workbench.common.forms.jbpm.model.document.DocumentData) DocumentPreviewStateActionsHandler(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateActionsHandler) DocumentPreview(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview) DocumentPreviewStateAction(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) Command(org.uberfire.mvp.Command) List(java.util.List) File(elemental2.dom.File) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) Test(org.junit.Test)

Example 3 with DocumentPreviewStateAction

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);
    }
}
Also used : DocumentPreview(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview) DocumentPreviewStateAction(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction) DocumentPreviewStateActionsHandlerImpl(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.handlers.DocumentPreviewStateActionsHandlerImpl)

Aggregations

DocumentPreview (org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview)3 DocumentPreviewStateAction (org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction)3 File (elemental2.dom.File)2 List (java.util.List)2 Document (org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.js.Document)2 DocumentPreviewStateActionsHandlerImpl (org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.handlers.DocumentPreviewStateActionsHandlerImpl)2 DocumentData (org.kie.workbench.common.forms.jbpm.model.document.DocumentData)2 Command (org.uberfire.mvp.Command)2 ParameterizedCommand (org.uberfire.mvp.ParameterizedCommand)2 ValueChangeEvent (com.google.gwt.event.logical.shared.ValueChangeEvent)1 ValueChangeHandler (com.google.gwt.event.logical.shared.ValueChangeHandler)1 GwtEvent (com.google.gwt.event.shared.GwtEvent)1 HandlerRegistration (com.google.gwt.event.shared.HandlerRegistration)1 IsWidget (com.google.gwt.user.client.ui.IsWidget)1 Widget (com.google.gwt.user.client.ui.Widget)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collectors (java.util.stream.Collectors)1