Remove inner classes from UserForm

This commit is contained in:
Denis Natusch 2024-01-17 13:31:06 +01:00
parent c5653f85e6
commit de6b9cfd0f
No known key found for this signature in database
GPG key ID: 5E57BD8EDACFA985
10 changed files with 116 additions and 102 deletions

View file

@ -175,7 +175,7 @@ image:models/design/clientServer.svg[]
==== Design Patterns ==== Design Patterns
* Spring MVC * Spring MVC
* Template Method in `UserForm` (hook method `validatePasswordGeneric`) * Template Method in `UserFormWithPassword` (hook method `validatePasswordGeneric`)
* Template Method in `InventoryMutateForm` (hook method `modifyProductPrimitive`) * Template Method in `InventoryMutateForm` (hook method `modifyProductPrimitive`)
==== Persistence ==== Persistence
@ -293,7 +293,11 @@ image:models/design/user.svg[class design diagram - User]
|UserManagement |A class that manages the UserRepository. |UserManagement |A class that manages the UserRepository.
|UserRepository |An extension of 'CrudRepository' to store Users. |UserRepository |An extension of 'CrudRepository' to store Users.
|User |A class that allows a person to associate system data with themselves. |User |A class that allows a person to associate system data with themselves.
|UserForm |A Form to cache a user input that was made during registration or updating the profile. |UserForm |A form to cache a user input that was made during registration or updating the profile.
|AdminForm |A form to cache the admin input that was made during updating the profile of a customer.
|UserFormWithPassword |An extension of UserForm with a password attribute.
|RegistrationForm |A form to cache a user input that was made during registration.
|ProfileForm |A form to cache a user input that was made during updating the profile.
|FormValidator |A validator that validates the UserForm. |FormValidator |A validator that validates the UserForm.
|=== |===

View file

@ -119,22 +119,18 @@ package catering.users {
} }
UserFormWithPassword --> Errors UserFormWithPassword --> Errors
UserFormWithPassword --> ValidationUtils UserFormWithPassword --> ValidationUtils
UserFormWithPassword ----+ UserForm
UserFormWithPassword --|> UserForm UserFormWithPassword --|> UserForm
class RegistrationForm extends UserFormWithPassword { class RegistrationForm extends UserFormWithPassword {
+ RegistrationForm(username:String, address:String, fullName:String, password:String) + RegistrationForm(username:String, address:String, fullName:String, password:String)
+ {static} empty() : RegistrationForm + {static} empty() : RegistrationForm
} }
RegistrationForm --+ UserFormWithPassword
class ProfileForm { class ProfileForm {
+ ProfileForm(username:String, address:String, fullName:String, password:String) + ProfileForm(username:String, address:String, fullName:String, password:String)
} }
ProfileForm --+ UserFormWithPassword
ProfileForm --|> UserFormWithPassword ProfileForm --|> UserFormWithPassword
class AdminForm { class AdminForm {
+ AdminForm(username:String, address:String, fullName:String) + AdminForm(username:String, address:String, fullName:String)
} }
AdminForm --+ UserForm
AdminForm --|> UserForm AdminForm --|> UserForm
class FormValidator<F extends UserForm> { class FormValidator<F extends UserForm> {
supports(c:Class<?>) : boolean supports(c:Class<?>) : boolean

BIN
src/main/asciidoc/models/design/user.svg (Stored with Git LFS)

Binary file not shown.

View file

@ -0,0 +1,13 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.users;
/**
* An extension of {@link UserForm} to validate input during customer profile
* editing.
*/
public class AdminForm extends UserForm {
AdminForm(String username, String address, String fullName) {
super(username, address, fullName);
}
}

View file

@ -7,8 +7,6 @@ import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils; import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
import catering.users.UserForm.UserFormWithPassword;
@Component @Component
public class FormValidator<F extends UserForm> implements Validator { public class FormValidator<F extends UserForm> implements Validator {

View file

@ -0,0 +1,23 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.users;
import org.springframework.validation.Errors;
/**
* An extension of {@link UserFormWithPassword} to validate input during profile
* editing.
*
* The password can be empty.
*/
public class ProfileForm extends UserFormWithPassword {
ProfileForm(String username, String address, String fullName, String password) {
super(username, address, fullName, password);
}
public void validatePasswordGeneric(Errors e) {
if (e.getFieldValue("password") == null) {
e.rejectValue("password", "password must not be null");
}
}
}

View file

@ -0,0 +1,26 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.users;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
/**
* An extension of {@link UserFormWithPassword} to validate input during
* registration.
*
* It requires the password to be not empty.
*/
public class RegistrationForm extends UserFormWithPassword {
RegistrationForm(String username, String address, String fullName, String password) {
super(username, address, fullName, password);
}
public static RegistrationForm empty() {
return new RegistrationForm("", "", "", "");
}
public void validatePasswordGeneric(Errors e) {
ValidationUtils.rejectIfEmptyOrWhitespace(e, "password", "password must not be empty");
}
}

View file

@ -2,9 +2,6 @@
// SPDX-FileCopyrightText: 2023-2024 swt23w23 // SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.users; package catering.users;
import static catering.users.UserForm.UserFormWithPassword.RegistrationForm;
import static catering.users.UserForm.UserFormWithPassword.ProfileForm;
import static catering.users.UserForm.AdminForm;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.salespointframework.useraccount.Role; import org.salespointframework.useraccount.Role;
import org.salespointframework.useraccount.UserAccount; import org.salespointframework.useraccount.UserAccount;

View file

@ -1,13 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023 swt23w23 // SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.users; package catering.users;
import org.springframework.validation.ValidationUtils;
import java.util.Optional;
import org.springframework.validation.Errors;
/** /**
* An abstract class to hold form input for {@link User} data. * An abstract class to hold form input for {@link User} data.
* *
@ -34,86 +28,4 @@ public abstract class UserForm {
public String getFullName() { public String getFullName() {
return fullName; return fullName;
} }
/**
* An extension of {@link UserForm} with a password attribute.
*/
public abstract static class UserFormWithPassword extends UserForm {
private final String password;
public UserFormWithPassword(String username, String address, String fullName, String password) {
super(username, address, fullName);
this.password = password;
}
public String getPassword() {
return password;
}
/**
* Template Method for validating the password.
*
* @param e {@link Errors} from {@link FormValidator}
*/
public void validatePassword(Errors e) {
validatePasswordGeneric(e);
Optional.ofNullable(e.getFieldValue("password")).map(s -> (String) s).ifPresent(s -> {
if (s.chars().anyMatch(Character::isISOControl)) {
e.rejectValue("password", "password must only contain printable characters");
}
});
}
/**
* Primitive Operation for validating the password depending on the subclass.
*
* @param e {@link Errors} from {@link FormValidator}
*/
public abstract void validatePasswordGeneric(Errors e);
/**
* An extension of {@link UserFormWithPassword} to validate input during registration.
*
* It requires the password to be not empty.
*/
public static class RegistrationForm extends UserFormWithPassword {
RegistrationForm(String username, String address, String fullName, String password) {
super(username, address, fullName, password);
}
public static RegistrationForm empty() {
return new UserFormWithPassword.RegistrationForm("", "", "", "");
}
public void validatePasswordGeneric(Errors e) {
ValidationUtils.rejectIfEmptyOrWhitespace(e, "password", "password must not be empty");
}
}
/**
* An extension of {@link UserFormWithPassword} to validate input during profile editing.
*
* The password can be empty.
*/
public static class ProfileForm extends UserFormWithPassword {
ProfileForm(String username, String address, String fullName, String password) {
super(username, address, fullName, password);
}
public void validatePasswordGeneric(Errors e) {
if (e.getFieldValue("password") == null) {
e.rejectValue("password", "password must not be null");
}
}
}
}
/**
* An extension of {@link UserForm} to validate input during customer profile editing.
*/
public static class AdminForm extends UserForm {
AdminForm(String username, String address, String fullName) {
super(username, address, fullName);
}
}
} }

View file

@ -0,0 +1,45 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// SPDX-FileCopyrightText: 2023-2024 swt23w23
package catering.users;
import java.util.Optional;
import org.springframework.validation.Errors;
/**
* An extension of {@link UserForm} with a password attribute.
*/
public abstract class UserFormWithPassword extends UserForm {
private final String password;
public UserFormWithPassword(String username, String address, String fullName, String password) {
super(username, address, fullName);
this.password = password;
}
public String getPassword() {
return password;
}
/**
* Template Method for validating the password.
*
* @param e {@link Errors} from {@link FormValidator}
*/
public void validatePassword(Errors e) {
validatePasswordGeneric(e);
Optional.ofNullable(e.getFieldValue("password")).map(s -> (String) s).ifPresent(s -> {
if (s.chars().anyMatch(Character::isISOControl)) {
e.rejectValue("password", "password must only contain printable characters");
}
});
}
/**
* Primitive Operation for validating the password depending on the subclass.
*
* @param e {@link Errors} from {@link FormValidator}
*/
public abstract void validatePasswordGeneric(Errors e);
}