Skip to content

Commit

Permalink
#37 Validation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dmajirsky committed Jan 5, 2021
1 parent d3f971d commit e2eab65
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ public AttributeStep(RadiusUserService radiusUserService, List<WizardStep<NewEnt
attrType.addValueChangeListener(event -> {
if (event.getValue().equals(AUTH_ATTR)) {
formLayout.removeAll();
// AttributeForm<AuthenticationAttributeDto> authForm = new AttributeForm<>(AuthenticationAttributeDto.class);
attributeForm = new AttributeForm<>(AuthenticationAttributeDto.class);
formLayout.add(attributeForm);
} else {
formLayout.removeAll();
// AttributeForm<AuthorizationAttributeDto> autzForm = new AttributeForm<>(AuthorizationAttributeDto.class);
attributeForm = new AttributeForm<>(AuthorizationAttributeDto.class);
formLayout.add(attributeForm);
}
Expand Down Expand Up @@ -116,7 +114,7 @@ public Component getContent() {

@Override
public boolean isValid() {
return true;
return authForm.isValid();
}

@Override
Expand Down Expand Up @@ -163,7 +161,7 @@ public Component getContent() {

@Override
public boolean isValid() {
return false;
return authForm.isValid();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import org.springframework.data.domain.Sort;
import org.vaadin.firitin.components.orderedlayout.VHorizontalLayout;
import org.vaadin.firitin.components.orderedlayout.VVerticalLayout;
import org.vaadin.firitin.components.textfield.VTextField;
import software.netcore.radman.buisness.service.nas.NasService;
import software.netcore.radman.buisness.service.nas.dto.NasDto;
import software.netcore.radman.buisness.service.nas.dto.NasGroupDto;
import software.netcore.radman.ui.component.wizard.WizardStep;
import software.netcore.radman.ui.view.nas.widget.NasForm;

import java.util.List;
import java.util.Objects;

/**
* @author daniel
Expand All @@ -42,7 +44,6 @@ public NasStep(NasService nasService, List<WizardStep<NewEntityWizardDataStorage

@Override
public Component getContent() {
steps.add(new NasStepSecond(nasService, "ip address")); //TODO wizard - use nas address from nasForm
return contentLayout;
}

Expand All @@ -56,6 +57,11 @@ public void writeDataToStorage(@NonNull NewEntityWizardDataStorage dataStorage)
dataStorage.setNasDto(nasForm.getBean());
}

@Override
public void onTransition() {
steps.add(new NasStepSecond(nasService, nasForm.getBean().getNasName()));
}

private static class NasStepSecond implements WizardStep<NewEntityWizardDataStorage> {

private static final String NO = "No";
Expand All @@ -75,6 +81,13 @@ public NasStepSecond(NasService nasService, String nasIpAddress) {
VHorizontalLayout groupHolder = new VHorizontalLayout();
radioGroup = new RadioButtonGroup<>();
radioGroup.setItems(NO, EXISTING_GROUPS, NEW_GROUP);
radioGroup.setItemEnabledProvider(item -> {
if (EXISTING_GROUPS.equals((String) item) && nasService.countNasGroupRecords("") == 0) {
return false;
}

return true;
});

existingGroups = new ComboBox<>("Group name");
existingGroups.setItemLabelGenerator(NasGroupDto::getGroupName);
Expand All @@ -84,7 +97,9 @@ public NasStepSecond(NasService nasService, String nasIpAddress) {
query -> (int) nasService.countNasGroupRecords(query.getFilter().orElse(null))));

newGroupName = new TextField("New group name");
newGroupName.setRequired(true);
binder.forField(newGroupName)
.asRequired("Group name is required")
.bind(NasGroupDto::getGroupName, NasGroupDto::setGroupName);

radioGroup.addValueChangeListener(event -> {
Expand Down Expand Up @@ -119,7 +134,7 @@ public boolean isValid() {
public void writeDataToStorage(@NonNull NewEntityWizardDataStorage dataStorage) {
if (!radioGroup.getValue().equals(NO)) {
NasGroupDto nasGroupDto = new NasGroupDto();
if (radioGroup.getValue().equals(EXISTING_GROUPS)) {
if (radioGroup.getValue().equals(EXISTING_GROUPS) && Objects.nonNull(existingGroups.getValue())) {
nasGroupDto.setGroupName(existingGroups.getValue().getGroupName());
} else if (radioGroup.getValue().equals(NEW_GROUP)) {
nasGroupDto.setGroupName(newGroupName.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.vaadin.flow.component.html.Hr;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.provider.CallbackDataProvider;
import com.vaadin.flow.data.provider.InMemoryDataProvider;
import com.vaadin.flow.data.provider.ListDataProvider;
Expand Down Expand Up @@ -145,11 +146,17 @@ public boolean hasNextStep() {
private class AddUserDialog extends Dialog {

AddUserDialog(String groupName, UpdateListener<RadiusUserToGroupDto> updateListener) {
BeanValidationBinder<RadiusUserDto> binder = new BeanValidationBinder<>(RadiusUserDto.class);
add(new H3("Add user to group"));

ComboBox<RadiusUserDto> username = new ComboBox<>("Username");
username.setItemLabelGenerator(RadiusUserDto::getUsername);
username.setAllowCustomValue(false);
username.setAutofocus(true);
username.setRequired(true);

binder.forField(username)
.asRequired();

username.setDataProvider(new CallbackDataProvider<>(query ->
userService.pageRadiusUsers(new RadiusUserFilter(query.getFilter().orElse(null),
Expand All @@ -160,13 +167,13 @@ private class AddUserDialog extends Dialog {
.orElse(null), true, false))));

Button add = new Button("Add", event -> {
if (StringUtils.isNotEmpty(groupName)) {
if (StringUtils.isNotEmpty(groupName) && binder.isValid() && Objects.nonNull(username.getValue())) {
RadiusUserToGroupDto dto = new RadiusUserToGroupDto();
dto.setUsername(username.getValue().getUsername());
dto.setGroupName(groupName);
updateListener.onUpdated(this, dto);
close();
}
close();
});
Button cancel = new Button("Cancel", event -> setOpened(false));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,16 @@ void saveNewEntityWizardData(NewEntityWizardDataStorage dataStorage) {
nasService.createNas(dataStorage.getNasDto());
}

if (Objects.nonNull(dataStorage.getNasGroupDtos())) {
if (!dataStorage.getNasGroupDtos().isEmpty()) {
for (NasGroupDto dto : dataStorage.getNasGroupDtos()) {
nasService.createNasGroup(dto);
}
}

if (Objects.nonNull(dataStorage.getRadiusUserDto())) {
radiusUserService.createRadiusUser(dataStorage.getRadiusUserDto());
}

if (Objects.nonNull(dataStorage.getRadiusGroupDtos())) {
for (RadiusGroupDto dto : dataStorage.getRadiusGroupDtos()) {
radiusUserService.createRadiusUsersGroup(dto);
Expand All @@ -186,7 +190,7 @@ void saveNewEntityWizardData(NewEntityWizardDataStorage dataStorage) {
try {
radiusUserService.addRadiusUserToGroup(dto);
} catch (DuplicityException e) {
e.printStackTrace(); //TODO wizard
// e.printStackTrace(); //TODO wizard
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ public AuthForm(@NonNull Class<T> clazz,
.withComponents(authTargetConfigLayout)
.withComponents(attrConfigLayout));
} else if (formConfig.isPredefinedUser()) {
username.setValue(formConfig.getPredefinedUserDto());
authTargetSelect.setValue(AuthTarget.RADIUS_USER);
username.setValue(formConfig.getPredefinedUserDto());
attrConfigLayout.add(attribute, opSelect, value);
add(new VVerticalLayout()
.withComponents(attrConfigLayout));
Expand Down

0 comments on commit e2eab65

Please sign in to comment.