Skip to content

Commit

Permalink
Finish i18n
Browse files Browse the repository at this point in the history
This solved #74.
  • Loading branch information
Kaiser-Yang committed Oct 22, 2024
1 parent c09a8e0 commit e04e17e
Show file tree
Hide file tree
Showing 19 changed files with 234 additions and 223 deletions.
15 changes: 4 additions & 11 deletions src/main/java/edu/cmipt/gcs/controller/RepositoryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ public ResponseEntity<RepositoryVO> updateRepository(
.repositoryName()
.equals(repositoryService.getById(id).getRepositoryName())) {
throw new GenericException(
ErrorCodeEnum.OPERATION_NOT_IMPLEMENTED,
"update repository name is not implemented");
ErrorCodeEnum.OPERATION_NOT_IMPLEMENTED);
}
if (!repositoryService.updateById(new RepositoryPO(repository))) {
throw new GenericException(ErrorCodeEnum.REPOSITORY_UPDATE_FAILED, repository);
Expand Down Expand Up @@ -215,18 +214,12 @@ public void checkRepositoryNameValidity(
@Size(
min = ValidationConstant.MIN_REPOSITORY_NAME_LENGTH,
max = ValidationConstant.MAX_REPOSITORY_NAME_LENGTH,
message =
"REPOSITORYDTO_REPOSITORYNAME_SIZE"
+ " {RepositoryDTO.repositoryName.Size}")
message ="{Size.repositoryController#checkRepositoryNameValidity.repositoryName}")
@NotBlank(
message =
"REPOSITORYDTO_REPOSITORYNAME_NOTBLANK"
+ " {RepositoryDTO.repositoryName.NotBlank}")
message ="{NotBlank.repositoryController#checkRepositoryNameValidity.repositoryName}")
@Pattern(
regexp = ValidationConstant.REPOSITORY_NAME_PATTERN,
message =
"REPOSITORYNAME_PATTERN_MISMATCH"
+ " {REPOSITORYNAME_PATTERN_MISMATCH}")
message = "{Pattern.repositoryController#checkRepositoryNameValidity.repositoryName}")
String repositoryName,
@RequestParam("userId") Long userId) {
QueryWrapper<RepositoryPO> queryWrapper = new QueryWrapper<>();
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/edu/cmipt/gcs/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ public List<RepositoryVO> pageUserRepository(
})
public void checkEmailValidity(
@RequestParam("email")
@Email(message = "USERDTO_EMAIL_EMAIL {UserDTO.email.Email}")
@NotBlank(message = "USERDTO_EMAIL_NOTBLANK {UserDTO.email.NotBlank}")
@Email(message = "{Email.userController#checkEmailValidity.email}")
@NotBlank(message = "{NotBlank.userController#checkEmailValidity.email}")
String email) {
QueryWrapper<UserPO> wrapper = new QueryWrapper<UserPO>();
wrapper.eq("email", email);
Expand Down Expand Up @@ -295,11 +295,11 @@ public void checkUsernameValidity(
@Size(
min = ValidationConstant.MIN_USERNAME_LENGTH,
max = ValidationConstant.MAX_USERNAME_LENGTH,
message = "USERDTO_USERNAME_SIZE {UserDTO.username.Size}")
@NotBlank(message = "USERDTO_USERNAME_NOTBLANK {UserDTO.username.NotBlank}")
message = "{Size.userController#checkUsernameValidity.username}")
@NotBlank(message = "{NotBlank.userController#checkUsernameValidity.username}")
@Pattern(
regexp = ValidationConstant.USERNAME_PATTERN,
message = "USERNAME_PATTERN_MISMATCH {USERNAME_PATTERN_MISMATCH}")
message = "{Pattern.userController#checkUsernameValidity.username}")
String username) {
QueryWrapper<UserPO> wrapper = new QueryWrapper<UserPO>();
wrapper.eq("username", username);
Expand Down Expand Up @@ -332,10 +332,10 @@ public void checkPasswordValidity(
@Size(
min = ValidationConstant.MIN_PASSWORD_LENGTH,
max = ValidationConstant.MAX_PASSWORD_LENGTH,
message = "USERDTO_PASSWORD_SIZE {UserDTO.password.Size}")
@NotBlank(message = "USERDTO_PASSWORD_NOTBLANK {UserDTO.password.NotBlank}")
message = "{Size.userController#checkPasswordValidity.password}")
@NotBlank(message = "{NotBlank.userController#checkPasswordValidity.password}")
@Pattern(
regexp = ValidationConstant.PASSWORD_PATTERN,
message = "PASSWORD_PATTERN_MISMATCH {PASSWORD_PATTERN_MISMATCH}")
message = "{Pattern.userController#checkPasswordValidity.password}")
String password) {}
}
35 changes: 1 addition & 34 deletions src/main/java/edu/cmipt/gcs/enumeration/ErrorCodeEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,7 @@ public enum ErrorCodeEnum {
// This should be ignored, this is to make the ordinal of the enum start from 1
ZERO_PLACEHOLDER,

USERDTO_ID_NULL("UserDTO.id.Null"),
USERDTO_ID_NOTNULL("UserDTO.id.NotNull"),
USERDTO_USERNAME_SIZE("UserDTO.username.Size"),
USERDTO_USERNAME_NOTBLANK("UserDTO.username.NotBlank"),
USERDTO_EMAIL_NOTBLANK("UserDTO.email.NotBlank"),
USERDTO_EMAIL_EMAIL("UserDTO.email.Email"),
USERDTO_USERPASSWORD_SIZE("UserDTO.userPassword.Size"),
USERDTO_USERPASSWORD_NOTBLANK("UserDTO.userPassword.NotBlank"),

USERSIGNINDTO_USERNAME_NOTBLANK("UserSignInDTO.username.NotBlank"),
USERSIGNINDTO_USERPASSWORD_NOTBLANK("UserSignInDTO.userPassword.NotBlank"),

USERNAME_PATTERN_MISMATCH("USERNAME_PATTERN_MISMATCH"),
PASSWORD_PATTERN_MISMATCH("PASSWORD_PATTERN_MISMATCH"),
VALIDATION_ERROR("VALIDATION_ERROR"),

USERNAME_ALREADY_EXISTS("USERNAME_ALREADY_EXISTS"),
EMAIL_ALREADY_EXISTS("EMAIL_ALREADY_EXISTS"),
Expand All @@ -35,19 +22,6 @@ public enum ErrorCodeEnum {
USER_UPDATE_FAILED("USER_UPDATE_FAILED"),
USER_DELETE_FAILED("USER_DELETE_FAILED"),

REPOSITORYDTO_ID_NULL("RepositoryDTO.id.Null"),
REPOSITORYDTO_ID_NOTNULL("RepositoryDTO.id.NotNull"),
REPOSITORYDTO_REPOSITORYNAME_SIZE("RepositoryDTO.repositoryName.Size"),
REPOSITORYDTO_REPOSITORYNAME_NOTBLANK("RepositoryDTO.repositoryName.NotBlank"),
REPOSITORYDTO_REPOSITORYDESCRIPTION_SIZE("RepositoryDTO.repositoryDescription.Size"),
REPOSITORYDTO_STAR_NULL("RepositoryDTO.star.Null"),
REPOSITORYDTO_STAR_MIN("RepositoryDTO.star.Min"),
REPOSITORYDTO_FORK_NULL("RepositoryDTO.fork.Null"),
REPOSITORYDTO_FORK_MIN("RepositoryDTO.fork.Min"),
REPOSITORYDTO_WATCHER_NULL("RepositoryDTO.watcher.Null"),
REPOSITORYDTO_WATCHER_MIN("RepositoryDTO.watcher.Min"),

REPOSITORYNAME_PATTERN_MISMATCH("REPOSITORYNAME_PATTERN_MISMATCH"),
REPOSITORY_NOT_FOUND("REPOSITORY_NOT_FOUND"),
REPOSITORY_ALREADY_EXISTS("REPOSITORY_ALREADY_EXISTS"),
REPOSITORY_CREATE_FAILED("REPOSITORY_CREATE_FAILED"),
Expand All @@ -59,13 +33,6 @@ public enum ErrorCodeEnum {
COLLABORATION_ALREADY_EXISTS("COLLABORATION_ALREADY_EXISTS"),
COLLABORATION_NOT_FOUND("COLLABORATION_NOT_FOUND"),

SSHKEYDTO_ID_NULL("SshKeyDTO.id.Null"),
SSHKEYDTO_ID_NOTNULL("SshKeyDTO.id.NotNull"),
SSHKEYDTO_NAME_NOTBLANK("SshKeyDTO.name.NotBlank"),
SSHKEYDTO_NAME_SIZE("SshKeyDTO.name.Size"),
SSHKEYDTO_PUBLICKEY_NOTBLANK("SshKeyDTO.publicKey.NotBlank"),
SSHKEYDTO_PUBLICKEY_SIZE("SshKeyDTO.publicKey.Size"),

SSH_KEY_UPLOAD_FAILED("SSH_KEY_UPLOAD_FAILED"),
SSH_KEY_UPDATE_FAILED("SSH_KEY_UPDATE_FAILED"),
SSH_KEY_DELETE_FAILED("SSH_KEY_DELETE_FAILED"),
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/edu/cmipt/gcs/exception/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import edu.cmipt.gcs.enumeration.ErrorCodeEnum;
import edu.cmipt.gcs.pojo.error.ErrorVO;

import edu.cmipt.gcs.util.MessageSourceUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolationException;

Expand Down Expand Up @@ -37,7 +37,8 @@ public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorVO> handleMethodArgumentNotValidException(
MethodArgumentNotValidException e, HttpServletRequest request) {
return handleValidationException(e.getFieldError().getDefaultMessage(), request);
var fieldError = e.getBindingResult().getFieldError();
return handleValidationException(MessageSourceUtil.getMessage(fieldError.getCodes()[0], fieldError.getArguments()), request);
}

/**
Expand Down Expand Up @@ -108,13 +109,7 @@ public ResponseEntity<ErrorVO> handleException(Exception e, HttpServletRequest r
}

private ResponseEntity<ErrorVO> handleValidationException(
String codeAndMessage, HttpServletRequest request) {
int firstSpaceIndex = codeAndMessage.indexOf(" ");
// There must be a space and not at the end of the message
assert firstSpaceIndex != -1;
assert firstSpaceIndex != codeAndMessage.length() - 1;
var exception = new GenericException(codeAndMessage.substring(firstSpaceIndex + 1));
exception.setCode(ErrorCodeEnum.valueOf(codeAndMessage.substring(0, firstSpaceIndex)));
return handleGenericException(exception, request);
String message, HttpServletRequest request) {
return handleGenericException(new GenericException(ErrorCodeEnum.VALIDATION_ERROR, message), request);
}
}
25 changes: 6 additions & 19 deletions src/main/java/edu/cmipt/gcs/pojo/repository/RepositoryDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
public record RepositoryDTO(
@Schema(description = "Repository ID")
@Null(
groups = CreateGroup.class,
message = "REPOSITORYDTO_ID_NULL {RepositoryDTO.id.Null}")
groups = CreateGroup.class)
@NotNull(
groups = UpdateGroup.class,
message = "REPOSITORYDTO_ID_NOTNULL {RepositoryDTO.id.NotNull}")
groups = UpdateGroup.class)
String id,
@Schema(
description = "Repository Name",
Expand All @@ -29,29 +27,18 @@ public record RepositoryDTO(
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_REPOSITORY_NAME_LENGTH,
max = ValidationConstant.MAX_REPOSITORY_NAME_LENGTH,
message =
"REPOSITORYDTO_REPOSITORYNAME_SIZE"
+ " {RepositoryDTO.repositoryName.Size}")
max = ValidationConstant.MAX_REPOSITORY_NAME_LENGTH)
@NotBlank(
groups = CreateGroup.class,
message =
"REPOSITORYDTO_REPOSITORYNAME_NOTBLANK"
+ " {RepositoryDTO.repositoryName.NotBlank}")
groups = CreateGroup.class)
@Pattern(
regexp = ValidationConstant.REPOSITORY_NAME_PATTERN,
groups = {CreateGroup.class, UpdateGroup.class},
message =
"REPOSITORYNAME_PATTERN_MISMATCH {REPOSITORYNAME_PATTERN_MISMATCH}")
groups = {CreateGroup.class, UpdateGroup.class})
String repositoryName,
@Schema(description = "Repository Description")
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_REPOSITORY_DESCRIPTION_LENGTH,
max = ValidationConstant.MAX_REPOSITORY_DESCRIPTION_LENGTH,
message =
"REPOSITORYDTO_REPOSITORYDESCRIPTION_SIZE"
+ " {RepositoryDTO.repositoryDescription.Size}")
max = ValidationConstant.MAX_REPOSITORY_DESCRIPTION_LENGTH)
String repositoryDescription,
@Schema(description = "Whether or Not Private Repo", example = "false")
Boolean isPrivate) {}
17 changes: 6 additions & 11 deletions src/main/java/edu/cmipt/gcs/pojo/ssh/SshKeyDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,23 @@
@Schema(description = "SSH Key Data Transfer Object")
public record SshKeyDTO(
@Schema(description = "SSH Key ID")
@Null(groups = CreateGroup.class, message = "SSHKEYDTO_ID_NULL {SshKeyDTO.id.Null}")
@Null(groups = CreateGroup.class)
@NotNull(
groups = UpdateGroup.class,
message = "SSHKEYDTO_ID_NOTNULL {SshKeyDTO.id.NotNull}")
groups = UpdateGroup.class)
String id,
@Schema(description = "Name", example = "My SSH Key")
@NotBlank(
groups = {CreateGroup.class, UpdateGroup.class},
message = "SSHKEYDTO_NAME_NOTBLANK {SshKeyDTO.name.NotBlank}")
groups = {CreateGroup.class, UpdateGroup.class})
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_SSH_KEY_NAME_LENGTH,
max = ValidationConstant.MAX_SSH_KEY_NAME_LENGTH,
message = "SSHKEYDTO_NAME_SIZE {SshKeyDTO.name.Size}")
max = ValidationConstant.MAX_SSH_KEY_NAME_LENGTH)
String name,
@Schema(description = "Public Key")
@NotBlank(
groups = CreateGroup.class,
message = "SSHKEYDTO_PUBLICKEY_NOTBLANK {SshKeyDTO.publicKey.NotBlank}")
groups = CreateGroup.class)
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_SSH_KEY_PUBLICKEY_LENGTH,
max = ValidationConstant.MAX_SSH_KEY_PUBLICKEY_LENGTH,
message = "SSHKEYDTO_PUBLICKEY_SIZE {SshKeyDTO.publicKey.Size}")
max = ValidationConstant.MAX_SSH_KEY_PUBLICKEY_LENGTH)
String publicKey) {}
30 changes: 10 additions & 20 deletions src/main/java/edu/cmipt/gcs/pojo/user/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
@Schema(description = "User Data Transfer Object")
public record UserDTO(
@Schema(description = "User ID")
@Null(groups = CreateGroup.class, message = "USERDTO_ID_NULL {UserDTO.id.Null}")
@NotNull(
groups = UpdateGroup.class,
message = "USERDTO_ID_NOTNULL {UserDTO.id.NotNull}")
@Null(groups = CreateGroup.class)
@NotNull(groups = UpdateGroup.class)
// The Long can not be expressed correctly in json, so use String instead
String id,
@Schema(
Expand All @@ -34,26 +32,21 @@ public record UserDTO(
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_USERNAME_LENGTH,
max = ValidationConstant.MAX_USERNAME_LENGTH,
message = "USERDTO_USERNAME_SIZE {UserDTO.username.Size}")
max = ValidationConstant.MAX_USERNAME_LENGTH)
@NotBlank(
groups = {CreateGroup.class},
message = "USERDTO_USERNAME_NOTBLANK {UserDTO.username.NotBlank}")
groups = {CreateGroup.class})
@Pattern(
regexp = ValidationConstant.USERNAME_PATTERN,
groups = {CreateGroup.class, UpdateGroup.class},
message = "USERNAME_PATTERN_MISMATCH {USERNAME_PATTERN_MISMATCH}")
groups = {CreateGroup.class, UpdateGroup.class})
String username,
@Schema(
description = "Email",
requiredMode = Schema.RequiredMode.REQUIRED,
example = "[email protected]")
@Email(
groups = {CreateGroup.class, UpdateGroup.class},
message = "USERDTO_EMAIL_EMAIL {UserDTO.email.Email}")
groups = {CreateGroup.class, UpdateGroup.class})
@NotBlank(
groups = {CreateGroup.class},
message = "USERDTO_EMAIL_NOTBLANK {UserDTO.email.NotBlank}")
groups = {CreateGroup.class})
String email,
@Schema(
description = "User Password (Unencrypted)",
Expand All @@ -62,13 +55,10 @@ public record UserDTO(
@Size(
groups = {CreateGroup.class, UpdateGroup.class},
min = ValidationConstant.MIN_PASSWORD_LENGTH,
max = ValidationConstant.MAX_PASSWORD_LENGTH,
message = "USERDTO_USERPASSWORD_SIZE {UserDTO.userPassword.Size}")
max = ValidationConstant.MAX_PASSWORD_LENGTH)
@NotBlank(
groups = {CreateGroup.class},
message = "USERDTO_USERPASSWORD_NOTBLANK {UserDTO.userPassword.NotBlank}")
groups = {CreateGroup.class})
@Pattern(
regexp = ValidationConstant.PASSWORD_PATTERN,
groups = {CreateGroup.class, UpdateGroup.class},
message = "PASSWORD_PATTERN_MISMATCH {PASSWORD_PATTERN_MISMATCH}")
groups = {CreateGroup.class, UpdateGroup.class})
String userPassword) {}
9 changes: 2 additions & 7 deletions src/main/java/edu/cmipt/gcs/pojo/user/UserSignInDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ public record UserSignInDTO(
description = "Username",
requiredMode = Schema.RequiredMode.REQUIRED,
example = "admin")
@NotBlank(
message =
"USERSIGNINDTO_USERNAME_NOTBLANK {UserSignInDTO.username.NotBlank}")
@NotBlank
String username,
@Schema(
description = "User Password (Unencrypted)",
requiredMode = Schema.RequiredMode.REQUIRED,
example = "123456")
@NotBlank(
message =
"USERSIGNINDTO_USERPASSWORD_NOTBLANK"
+ " {UserSignInDTO.userPassword.NotBlank}")
@NotBlank
String userPassword) {}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public boolean save(RepositoryPO repositoryPO) {
repositoryPO.getIsPrivate())) {
logger.error("Failed to create repository in gitolite");
throw new GenericException(
ErrorCodeEnum.REPOSITORY_CREATE_FAILED,
"Failed to create repository in gitolite");
ErrorCodeEnum.REPOSITORY_CREATE_FAILED, repositoryPO);
}
return true;
}
Expand All @@ -61,8 +60,7 @@ public boolean removeById(Serializable id) {
repositoryPO.getIsPrivate())) {
logger.error("Failed to remove repository from gitolite");
throw new GenericException(
ErrorCodeEnum.REPOSITORY_DELETE_FAILED,
"Failed to remove repository from gitolite");
ErrorCodeEnum.REPOSITORY_DELETE_FAILED, repositoryPO);
}
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/edu/cmipt/gcs/service/SshKeyServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean save(SshKeyPO sshKeyPO) {
sshKeyPO.getId(), sshKeyPO.getPublicKey(), sshKeyPO.getUserId())) {
logger.error("Failed to add SSH key to gitolite");
throw new GenericException(
ErrorCodeEnum.SSH_KEY_UPLOAD_FAILED, "Failed to add SSH key to gitolite");
ErrorCodeEnum.SSH_KEY_UPLOAD_FAILED, sshKeyPO);
}
return true;
}
Expand All @@ -48,7 +48,7 @@ public boolean removeById(Serializable id) {
if (!GitoliteUtil.removeSshKey(sshKeyPO.getId(), sshKeyPO.getUserId())) {
logger.error("Failed to remove SSH key from gitolite");
throw new GenericException(
ErrorCodeEnum.SSH_KEY_DELETE_FAILED, "Failed to remove SSH key from gitolite");
ErrorCodeEnum.SSH_KEY_DELETE_FAILED, sshKeyPO);
}
return true;
}
Expand Down
Loading

0 comments on commit e04e17e

Please sign in to comment.