use of de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent in project SORMAS-Project by hzi-braunschweig.
the class ActionController method create.
/**
* Show a create form for an action.
*
* @param context of the action
* @param entityRef of the action
* @param callback on save
*/
public void create(ActionContext context, ReferenceDto entityRef, Runnable callback) {
ActionEditForm createForm = new ActionEditForm(true);
createForm.setValue(createNewAction(context, entityRef));
final CommitDiscardWrapperComponent<ActionEditForm> editView = new CommitDiscardWrapperComponent<>(createForm, UserProvider.getCurrent().hasUserRight(UserRight.ACTION_CREATE), createForm.getFieldGroup());
editView.addCommitListener(() -> {
if (!createForm.getFieldGroup().isModified()) {
ActionDto dto = createForm.getValue();
FacadeProvider.getActionFacade().saveAction(dto);
callback.run();
}
});
VaadinUiUtil.showModalPopupWindow(editView, I18nProperties.getString(Strings.headingCreateNewAction));
}
use of de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent in project SORMAS-Project by hzi-braunschweig.
the class ActionController method edit.
/**
* Show an edit form for an action.
*
* @param dto of the action
* @param callback on save
*/
public void edit(ActionDto dto, Runnable callback) {
// get fresh data
ActionDto newDto = FacadeProvider.getActionFacade().getByUuid(dto.getUuid());
ActionEditForm form = new ActionEditForm(false);
form.setValue(newDto);
final CommitDiscardWrapperComponent<ActionEditForm> editView = new CommitDiscardWrapperComponent<>(form, UserProvider.getCurrent().hasUserRight(UserRight.ACTION_EDIT), form.getFieldGroup());
Window popupWindow = VaadinUiUtil.showModalPopupWindow(editView, I18nProperties.getString(Strings.headingEditAction));
editView.addCommitListener(() -> {
if (!form.getFieldGroup().isModified()) {
ActionDto dto1 = form.getValue();
dto1.setLastModifiedBy(FacadeProvider.getUserFacade().getCurrentUser().toReference());
FacadeProvider.getActionFacade().saveAction(dto1);
popupWindow.close();
callback.run();
}
});
// Add delete button if user has role
if (UserProvider.getCurrent().hasUserRole(UserRole.ADMIN)) {
editView.addDeleteListener(() -> {
FacadeProvider.getActionFacade().deleteAction(newDto);
UI.getCurrent().removeWindow(popupWindow);
callback.run();
}, I18nProperties.getString(Strings.entityAction));
}
editView.addDiscardListener(popupWindow::close);
}
use of de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent 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 de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent in project SORMAS-Project by hzi-braunschweig.
the class CampaignController method getCampaignComponent.
public CommitDiscardWrapperComponent<CampaignEditForm> getCampaignComponent(CampaignDto campaignDto, Runnable callback) {
CampaignEditForm campaignEditForm = new CampaignEditForm(campaignDto);
boolean isCreate = false;
if (campaignDto == null) {
isCreate = true;
campaignDto = CampaignDto.build();
campaignDto.setCreatingUser(UserProvider.getCurrent().getUserReference());
}
campaignEditForm.setValue(campaignDto);
final CommitDiscardWrapperComponent<CampaignEditForm> campaignComponent = new CommitDiscardWrapperComponent<CampaignEditForm>(campaignEditForm, UserProvider.getCurrent().hasUserRight(UserRight.CAMPAIGN_EDIT), campaignEditForm.getFieldGroup()) {
@Override
public void discard() {
super.discard();
campaignEditForm.discard();
}
};
if (UserProvider.getCurrent().hasUserRight(UserRight.CAMPAIGN_DELETE) && !isCreate) {
CampaignDto finalCampaignDto = campaignDto;
campaignComponent.addDeleteListener(() -> {
FacadeProvider.getCampaignFacade().deleteCampaign(finalCampaignDto.getUuid());
UI.getCurrent().getNavigator().navigateTo(CampaignsView.VIEW_NAME);
}, I18nProperties.getString(Strings.entityCampaign));
}
// Initialize 'Archive' button
if (UserProvider.getCurrent().hasUserRight(UserRight.CAMPAIGN_ARCHIVE) && !isCreate) {
final CampaignDto campaign = campaignDto;
createArchiveButton(campaignComponent, campaign);
}
campaignComponent.addCommitListener(() -> {
if (!campaignEditForm.getFieldGroup().isModified()) {
CampaignDto dto = campaignEditForm.getValue();
FacadeProvider.getCampaignFacade().save(dto);
SormasUI.refreshView();
callback.run();
}
});
return campaignComponent;
}
use of de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent in project SORMAS-Project by hzi-braunschweig.
the class EventParticipantsController method createEventParticipant.
public EventParticipantDto createEventParticipant(EventReferenceDto eventRef, Consumer<EventParticipantReferenceDto> doneConsumer, EventParticipantDto eventParticipant, boolean navigateOnCommit) {
EventParticipantCreateForm createForm = new EventParticipantCreateForm(!FacadeProvider.getEventFacade().hasRegionAndDistrict(eventRef.getUuid()));
createForm.setValue(eventParticipant);
final CommitDiscardWrapperComponent<EventParticipantCreateForm> createComponent = new CommitDiscardWrapperComponent<>(createForm, UserProvider.getCurrent().hasUserRight(UserRight.EVENTPARTICIPANT_CREATE), createForm.getFieldGroup());
createComponent.addCommitListener(() -> {
if (!createForm.getFieldGroup().isModified()) {
final EventParticipantDto dto = createForm.getValue();
PersonDto searchedPerson = createForm.getSearchedPerson();
if (searchedPerson != null) {
dto.setPerson(searchedPerson);
}
if (dto.getPerson() == null) {
final PersonDto person = PersonDto.build();
person.setFirstName(createForm.getPersonFirstName());
person.setLastName(createForm.getPersonLastName());
person.setSex(createForm.getPersonSex());
ControllerProvider.getPersonController().selectOrCreatePerson(person, I18nProperties.getString(Strings.infoSelectOrCreatePersonForEventParticipant), selectedPerson -> {
if (selectedPerson != null) {
if (FacadeProvider.getEventParticipantFacade().exists(selectedPerson.getUuid(), eventRef.getUuid())) {
throw new Validator.InvalidValueException(I18nProperties.getString(Strings.messageAlreadyEventParticipant));
} else {
dto.setPerson(FacadeProvider.getPersonFacade().getPersonByUuid(selectedPerson.getUuid()));
EventParticipantDto savedDto = eventParticipantFacade.saveEventParticipant(dto);
Notification notification = new Notification(I18nProperties.getString(Strings.messagePersonAddedAsEventParticipant), "", Type.HUMANIZED_MESSAGE);
notification.show(Page.getCurrent());
Notification.show(I18nProperties.getString(Strings.messageEventParticipantCreated), Type.ASSISTIVE_NOTIFICATION);
if (navigateOnCommit) {
navigateToData(savedDto.getUuid());
} else {
SormasUI.refreshView();
}
}
}
}, true);
} else {
if (FacadeProvider.getEventParticipantFacade().exists(dto.getPerson().getUuid(), eventRef.getUuid())) {
throw new Validator.InvalidValueException(I18nProperties.getString(Strings.messageAlreadyEventParticipant));
}
EventParticipantDto savedDto = eventParticipantFacade.saveEventParticipant(dto);
Notification.show(I18nProperties.getString(Strings.messageEventParticipantCreated), Type.ASSISTIVE_NOTIFICATION);
if (navigateOnCommit) {
navigateToData(savedDto.getUuid());
} else {
SormasUI.refreshView();
}
}
}
});
Window window = VaadinUiUtil.showModalPopupWindow(createComponent, I18nProperties.getString(Strings.headingCreateNewEventParticipant));
window.addCloseListener(e -> {
doneConsumer.accept(null);
});
return createComponent.getWrappedComponent().getValue();
}
Aggregations