Search in sources :

Example 1 with InvalidValueException

use of com.vaadin.v7.data.Validator.InvalidValueException in project SORMAS-Project by hzi-braunschweig.

the class CampaignController method getCampaignFormDataComponent.

public CommitDiscardWrapperComponent<CampaignFormDataEditForm> getCampaignFormDataComponent(CampaignFormDataDto campaignFormData, CampaignReferenceDto campaign, CampaignFormMetaReferenceDto campaignForm, boolean revertFormOnDiscard, boolean showDeleteButton, Runnable commitCallback, Runnable discardCallback) {
    CampaignFormDataEditForm form = new CampaignFormDataEditForm(campaignFormData == null);
    if (campaignFormData == null) {
        final UserDto currentUser = UserProvider.getCurrent().getUser();
        campaignFormData = CampaignFormDataDto.build(campaign, campaignForm, currentUser.getRegion(), currentUser.getDistrict(), currentUser.getCommunity());
        campaignFormData.setCreatingUser(UserProvider.getCurrent().getUserReference());
    }
    form.setValue(campaignFormData);
    final CommitDiscardWrapperComponent<CampaignFormDataEditForm> component = new CommitDiscardWrapperComponent<>(form, form.getFieldGroup());
    component.addCommitListener(() -> {
        if (!form.getFieldGroup().isModified()) {
            try {
                form.validate();
            } catch (InvalidValueException e) {
                Notification.show(I18nProperties.getValidationError(Validations.errorsInForm), Type.ERROR_MESSAGE);
                return;
            }
            CampaignFormDataDto formData = form.getValue();
            FacadeProvider.getCampaignFormDataFacade().saveCampaignFormData(formData);
            if (commitCallback != null) {
                commitCallback.run();
                UI.getCurrent().getNavigator().navigateTo(CampaignDataView.VIEW_NAME + "/?" + CAMPAIGN + "=" + campaign.getUuid());
            }
        }
    });
    component.addDiscardListener(() -> UI.getCurrent().getNavigator().navigateTo(CampaignDataView.VIEW_NAME + "/?" + CAMPAIGN + "=" + campaign.getUuid()));
    if (revertFormOnDiscard) {
        component.addDiscardListener(form::resetFormValues);
    }
    if (discardCallback != null) {
        component.addDiscardListener(discardCallback::run);
    }
    if (showDeleteButton && UserProvider.getCurrent().hasUserRight(UserRight.CAMPAIGN_DELETE)) {
        String campaignFormDataUuid = campaignFormData.getUuid();
        component.addDeleteListener(() -> {
            FacadeProvider.getCampaignFormDataFacade().deleteCampaignFormData(campaignFormDataUuid);
            UI.getCurrent().getNavigator().navigateTo(CampaignFormDataView.VIEW_NAME);
        }, I18nProperties.getString(Strings.entityCampaignDataForm));
    }
    return component;
}
Also used : InvalidValueException(com.vaadin.v7.data.Validator.InvalidValueException) CampaignFormDataDto(de.symeda.sormas.api.campaign.data.CampaignFormDataDto) UserDto(de.symeda.sormas.api.user.UserDto) CampaignFormDataEditForm(de.symeda.sormas.ui.campaign.campaigndata.CampaignFormDataEditForm) CommitDiscardWrapperComponent(de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent)

Example 2 with InvalidValueException

use of com.vaadin.v7.data.Validator.InvalidValueException 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 3 with InvalidValueException

use of com.vaadin.v7.data.Validator.InvalidValueException 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)

Example 4 with InvalidValueException

use of com.vaadin.v7.data.Validator.InvalidValueException in project opencms-core by alkacon.

the class CmsContextMenuEditHandler method textChange.

/**
 * @see com.vaadin.event.FieldEvents.TextChangeListener#textChange(com.vaadin.event.FieldEvents.TextChangeEvent)
 */
public void textChange(TextChangeEvent event) {
    TextField tf = (TextField) event.getSource();
    try {
        validate(event.getText());
        tf.setComponentError(null);
    } catch (InvalidValueException e) {
        tf.setComponentError(new UserError(e.getHtmlMessage(), ContentMode.HTML, null));
    }
}
Also used : UserError(com.vaadin.server.UserError) TextField(com.vaadin.v7.ui.TextField)

Example 5 with InvalidValueException

use of com.vaadin.v7.data.Validator.InvalidValueException in project opencms-core by alkacon.

the class CmsEditSiteForm method setupValidators.

/**
 * Setup validators which get called on click.<p>
 * Site-template gets validated separately.<p>
 */
void setupValidators() {
    if (m_simpleFieldServer.getValidators().size() == 0) {
        if (m_site == null) {
            m_simpleFieldFolderName.addValidator(new FolderPathValidator());
            m_simpleFieldParentFolderName.addValidator(new ParentFolderValidator());
        }
        if ((m_simpleFieldSiteRoot != null) && m_simpleFieldSiteRoot.isVisible()) {
            m_simpleFieldSiteRoot.addValidator(value -> {
                String siteRoot = (String) value;
                try {
                    OpenCms.getSiteManager().validateSiteRoot(siteRoot);
                } catch (Exception e) {
                    LOG.warn(e.getLocalizedMessage(), e);
                    throw new InvalidValueException(e.getMessage());
                }
            });
        }
        m_simpleFieldServer.addValidator(new ServerValidator());
        if (m_fieldSecureServer.isVisible()) {
            m_fieldSecureServer.addValidator(new AliasValidator());
        }
        m_simpleFieldTitle.addValidator(new TitleValidator());
        if (m_site == null) {
            m_fieldSelectOU.addValidator(new SelectOUValidator());
        }
        if (m_fieldCreateOU.getValue().booleanValue()) {
            m_fieldSelectParentOU.addValidator(new SelectParentOUValidator());
        }
        m_altSiteRoot.addValidator(fieldValue -> {
            String path = (String) fieldValue;
            if (path == null) {
                return;
            }
            path = path.trim();
            CmsSite site = OpenCms.getSiteManager().getSiteForRootPath(path);
            if (site != null) {
                if (site.isGenerated()) {
                    if (!m_site.getSiteMatcher().equals(site.getSiteMatcher())) {
                        throw new InvalidValueException(CmsVaadinUtils.getMessageText(Messages.GUI_SITE_FOLDERNAME_ALREADYUSED_1, path));
                    }
                } else {
                    throw new InvalidValueException(CmsVaadinUtils.getMessageText(Messages.GUI_SITE_FOLDERNAME_ALREADYUSED_1, path));
                }
            }
            for (String forbiddenPrefix : new String[] { OpenCms.getSiteManager().getSharedFolder(), "/system" }) {
                if (CmsStringUtil.isPrefixPath(forbiddenPrefix, path)) {
                    throw new InvalidValueException("Forbidden path for alternative site root rule.");
                }
            }
            try {
                CmsObject cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
                cms.getRequestContext().setSiteRoot("");
                cms.readResource(path);
            } catch (CmsException e) {
                throw new InvalidValueException(e.getLocalizedMessage(A_CmsUI.get().getLocale()));
            }
        });
    }
}
Also used : CmsSite(org.opencms.site.CmsSite) CmsException(org.opencms.main.CmsException) InvalidValueException(com.vaadin.v7.data.Validator.InvalidValueException) CmsIllegalArgumentException(org.opencms.main.CmsIllegalArgumentException) IOException(java.io.IOException) InvalidValueException(com.vaadin.v7.data.Validator.InvalidValueException) CmsObject(org.opencms.file.CmsObject) CmsException(org.opencms.main.CmsException)

Aggregations

InvalidValueException (com.vaadin.v7.data.Validator.InvalidValueException)4 Button (com.vaadin.ui.Button)2 ClickEvent (com.vaadin.ui.Button.ClickEvent)2 ClickListener (com.vaadin.ui.Button.ClickListener)2 Notifier (com.vaadin.event.Action.Notifier)1 KeyCode (com.vaadin.event.ShortcutAction.KeyCode)1 Page (com.vaadin.server.Page)1 UserError (com.vaadin.server.UserError)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