mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
Remove inner classes from UserForm
This commit is contained in:
parent
c5653f85e6
commit
de6b9cfd0f
|
@ -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.
|
||||||
|===
|
|===
|
||||||
|
|
||||||
|
|
|
@ -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)
BIN
src/main/asciidoc/models/design/user.svg
(Stored with Git LFS)
Binary file not shown.
13
src/main/java/catering/users/AdminForm.java
Normal file
13
src/main/java/catering/users/AdminForm.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
|
23
src/main/java/catering/users/ProfileForm.java
Normal file
23
src/main/java/catering/users/ProfileForm.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
src/main/java/catering/users/RegistrationForm.java
Normal file
26
src/main/java/catering/users/RegistrationForm.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
45
src/main/java/catering/users/UserFormWithPassword.java
Normal file
45
src/main/java/catering/users/UserFormWithPassword.java
Normal 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);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue