Search in sources :

Example 1 with DocumentPreview

use of org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview 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 DocumentPreview

use of org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview in project kie-wb-common by kiegroup.

the class DocumentUpload method render.

private DocumentPreview render(DocumentData documentData) {
    DocumentPreview preview = instance.get();
    preview.init(documentData);
    preview.setEnabled(enabled);
    previews.add(preview);
    view.addDocument(preview);
    return preview;
}
Also used : DocumentPreview(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview)

Example 3 with DocumentPreview

use of org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview in project kie-wb-common by kiegroup.

the class DocumentUploadTest method testSetValues.

@Test
public void testSetValues() {
    DocumentData doc1 = new DocumentData(DOC_1, DOC_1, 1024, DOC_1, System.currentTimeMillis());
    DocumentData doc2 = new DocumentData(DOC_2, DOC_2, 1024, DOC_2, System.currentTimeMillis());
    DocumentData doc3 = new DocumentData(DOC_3, DOC_3, 1024, DOC_3, System.currentTimeMillis());
    documentUpload.setValue(Arrays.asList(doc1, doc2, doc3), false);
    verify(view).clear();
    verify(instance).destroyAll();
    verify(instance, times(3)).get();
    verify(view, times(3)).addDocument(any());
    List<DocumentPreview> previews = (List<DocumentPreview>) documentUpload.getCurrentPreviews();
    Assertions.assertThat(previews).isNotNull().hasSize(3);
    verifyPreview(previews.get(0), doc1, true);
    verifyPreview(previews.get(1), doc2, true);
    verifyPreview(previews.get(2), doc3, true);
    documentUpload.getValue();
    verify(previews.get(0)).getDocumentData();
    verify(previews.get(1)).getDocumentData();
    verify(previews.get(2)).getDocumentData();
}
Also used : DocumentData(org.kie.workbench.common.forms.jbpm.model.document.DocumentData) DocumentPreview(org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview) List(java.util.List) Test(org.junit.Test)

Example 4 with DocumentPreview

use of org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview 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 5 with DocumentPreview

use of org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreview 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)5 List (java.util.List)3 DocumentPreviewStateAction (org.kie.workbench.common.forms.jbpm.client.rendering.documents.control.preview.DocumentPreviewStateAction)3 DocumentData (org.kie.workbench.common.forms.jbpm.model.document.DocumentData)3 File (elemental2.dom.File)2 Test (org.junit.Test)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 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