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;
}
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();
}
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);
}
}
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));
}
}
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()));
}
});
}
}
Aggregations