Search in sources :

Example 1 with SourceException

use of com.vaadin.v7.data.Buffered.SourceException in project SORMAS-Project by hzi-braunschweig.

the class CommitDiscardWrapperComponent method doCommit.

private void doCommit() throws InvalidValueException, SourceException, CommitRuntimeException {
    if (fieldGroups != null) {
        if (fieldGroups.size() > 1) {
            List<InvalidValueException> invalidValueExceptions = fieldGroups.stream().filter(fieldGroup -> !fieldGroup.isValid()).map(fieldGroup -> {
                try {
                    // all invalid fieldGroups are committed to fetch the CommitExceptions
                    fieldGroup.commit();
                } catch (CommitException e) {
                    return e;
                }
                // when the fieldGroup did not throw a CommitException, it is invalid and committed
                throw new IllegalStateException();
            }).map(e -> {
                // keep invalid value exceptions, throw the rest
                Throwable c = e.getCause();
                if (c instanceof InvalidValueException) {
                    return (InvalidValueException) c;
                } else if (c instanceof SourceException) {
                    throw (SourceException) c;
                } else {
                    throw new CommitRuntimeException(e);
                }
            }).collect(Collectors.toList());
            if (invalidValueExceptions.isEmpty()) {
            // NOOP
            } else if (invalidValueExceptions.size() == 1) {
                throw invalidValueExceptions.get(0);
            } else {
                throw new InvalidValueException(null, invalidValueExceptions.stream().map(InvalidValueException::getCauses).flatMap(Arrays::stream).toArray(InvalidValueException[]::new));
            }
        }
        try {
            for (FieldGroup fieldGroup : fieldGroups) {
                fieldGroup.commit();
            }
        } catch (CommitException e) {
            Throwable c = e.getCause();
            if (c instanceof InvalidValueException) {
                throw (InvalidValueException) c;
            } else if (c instanceof SourceException) {
                throw (SourceException) c;
            } else {
                throw new CommitRuntimeException(e);
            }
        }
    } else if (wrappedComponent instanceof Buffered) {
        ((Buffered) wrappedComponent).commit();
    } else {
    // NOOP
    }
    dirty = false;
    onCommit();
    commited = true;
    onDone();
}
Also used : InvalidValueException(com.vaadin.v7.data.Validator.InvalidValueException) Panel(com.vaadin.ui.Panel) EventDataForm(de.symeda.sormas.ui.events.EventDataForm) Arrays(java.util.Arrays) ClickListener(com.vaadin.ui.Button.ClickListener) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) VerticalLayout(com.vaadin.ui.VerticalLayout) Alignment(com.vaadin.ui.Alignment) Notifier(com.vaadin.event.Action.Notifier) ArrayUtils(org.apache.commons.lang3.ArrayUtils) AccessibleTextField(de.symeda.sormas.ui.location.AccessibleTextField) PersonDto(de.symeda.sormas.api.person.PersonDto) ArrayList(java.util.ArrayList) Buffered(com.vaadin.v7.data.Buffered) DeletionDetails(de.symeda.sormas.api.common.DeletionDetails) Notification(com.vaadin.ui.Notification) RichTextArea(com.vaadin.v7.ui.RichTextArea) Page(com.vaadin.server.Page) KeyCode(com.vaadin.event.ShortcutAction.KeyCode) AbstractLegacyComponent(com.vaadin.v7.ui.AbstractLegacyComponent) CannotProceedException(javax.naming.CannotProceedException) ValoTheme(com.vaadin.ui.themes.ValoTheme) LocationDto(de.symeda.sormas.api.location.LocationDto) ClickEvent(com.vaadin.ui.Button.ClickEvent) Collection(java.util.Collection) Field(com.vaadin.v7.ui.Field) EventDto(de.symeda.sormas.api.event.EventDto) InvalidValueException(com.vaadin.v7.data.Validator.InvalidValueException) Collectors(java.util.stream.Collectors) Captions(de.symeda.sormas.api.i18n.Captions) FieldGroup(com.vaadin.v7.data.fieldgroup.FieldGroup) List(java.util.List) Button(com.vaadin.ui.Button) Stream(java.util.stream.Stream) Type(com.vaadin.ui.Notification.Type) PersonEditForm(de.symeda.sormas.ui.person.PersonEditForm) HorizontalLayout(com.vaadin.ui.HorizontalLayout) LocationEditForm(de.symeda.sormas.ui.location.LocationEditForm) TextArea(com.vaadin.v7.ui.TextArea) CommitException(com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException) Descriptions(de.symeda.sormas.api.i18n.Descriptions) Objects.nonNull(java.util.Objects.nonNull) Strings(de.symeda.sormas.api.i18n.Strings) Component(com.vaadin.ui.Component) CommitException(com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException) FieldGroup(com.vaadin.v7.data.fieldgroup.FieldGroup) Buffered(com.vaadin.v7.data.Buffered)

Example 2 with SourceException

use of com.vaadin.v7.data.Buffered.SourceException in project SORMAS-Project by hzi-braunschweig.

the class SampleController method addReferOrLinkToOtherLabButton.

/**
 * Initialize 'Refer to another laboratory' button or link to referred sample
 *
 * @param editForm
 *            the edit form to attach the 'Refer to another laboratory' button to.
 * @param disease
 *            required for field visibility checks in the sample create form opened when a sample reference shall be created.
 * @param createReferral
 *            instructions for what shall happen when the user chooses to create a referral
 * @param openReferredSample
 *            instructions for what shall happen when the user chooses to open the referred sample
 */
public void addReferOrLinkToOtherLabButton(CommitDiscardWrapperComponent<SampleEditForm> editForm, Disease disease, Consumer<Disease> createReferral, Consumer<SampleDto> openReferredSample) {
    Button referOrLinkToOtherLabButton = null;
    SampleDto sample = editForm.getWrappedComponent().getValue();
    if (sample.getReferredTo() == null) {
        if (sample.getSamplePurpose() == SamplePurpose.EXTERNAL && UserProvider.getCurrent().hasUserRight(UserRight.SAMPLE_TRANSFER)) {
            referOrLinkToOtherLabButton = ButtonHelper.createButton("referOrLinkToOtherLab", I18nProperties.getCaption(Captions.sampleRefer), new ClickListener() {

                private static final long serialVersionUID = 1L;

                @Override
                public void buttonClick(ClickEvent event) {
                    try {
                        createReferral.accept(disease);
                    } catch (SourceException | InvalidValueException e) {
                        Notification.show(I18nProperties.getString(Strings.messageSampleErrors), Type.ERROR_MESSAGE);
                    }
                }
            }, ValoTheme.BUTTON_LINK);
        }
    } else {
        SampleDto referredDto = FacadeProvider.getSampleFacade().getSampleByUuid(sample.getReferredTo().getUuid());
        FacilityReferenceDto referredDtoLab = referredDto.getLab();
        String referOrLinkToOtherLabButtonCaption = referredDtoLab == null ? I18nProperties.getCaption(Captions.sampleReferredToInternal) + " (" + DateFormatHelper.formatLocalDateTime(referredDto.getSampleDateTime()) + ")" : I18nProperties.getCaption(Captions.sampleReferredTo) + " " + referredDtoLab.toString();
        referOrLinkToOtherLabButton = ButtonHelper.createButton("referOrLinkToOtherLab", referOrLinkToOtherLabButtonCaption, new ClickListener() {

            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                openReferredSample.accept(referredDto);
            }
        });
    }
    if (referOrLinkToOtherLabButton != null) {
        editForm.getButtonsPanel().addComponentAsFirst(referOrLinkToOtherLabButton);
        editForm.getButtonsPanel().setComponentAlignment(referOrLinkToOtherLabButton, Alignment.BOTTOM_LEFT);
    }
}
Also used : InvalidValueException(com.vaadin.v7.data.Validator.InvalidValueException) Button(com.vaadin.ui.Button) FacilityReferenceDto(de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto) ClickEvent(com.vaadin.ui.Button.ClickEvent) SourceException(com.vaadin.v7.data.Buffered.SourceException) SampleDto(de.symeda.sormas.api.sample.SampleDto) ClickListener(com.vaadin.ui.Button.ClickListener)

Aggregations

Button (com.vaadin.ui.Button)2 ClickEvent (com.vaadin.ui.Button.ClickEvent)2 ClickListener (com.vaadin.ui.Button.ClickListener)2 InvalidValueException (com.vaadin.v7.data.Validator.InvalidValueException)2 Notifier (com.vaadin.event.Action.Notifier)1 KeyCode (com.vaadin.event.ShortcutAction.KeyCode)1 Page (com.vaadin.server.Page)1 Alignment (com.vaadin.ui.Alignment)1 Component (com.vaadin.ui.Component)1 HorizontalLayout (com.vaadin.ui.HorizontalLayout)1 Notification (com.vaadin.ui.Notification)1 Type (com.vaadin.ui.Notification.Type)1 Panel (com.vaadin.ui.Panel)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 ValoTheme (com.vaadin.ui.themes.ValoTheme)1 Buffered (com.vaadin.v7.data.Buffered)1 SourceException (com.vaadin.v7.data.Buffered.SourceException)1 FieldGroup (com.vaadin.v7.data.fieldgroup.FieldGroup)1 CommitException (com.vaadin.v7.data.fieldgroup.FieldGroup.CommitException)1 AbstractLegacyComponent (com.vaadin.v7.ui.AbstractLegacyComponent)1