Skip to content

Commit

Permalink
Merge branch 'main' into fix/fe/FSADT1-1503-2
Browse files Browse the repository at this point in the history
  • Loading branch information
mamartinezmejia authored Oct 8, 2024
2 parents 6b517dc + 59e8571 commit 5a511a3
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 151 deletions.
95 changes: 47 additions & 48 deletions backend/src/main/java/ca/bc/gov/app/dto/client/LegalTypeEnum.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
package ca.bc.gov.app.dto.client;

import com.fasterxml.jackson.annotation.JsonCreator;
import ca.bc.gov.app.exception.UnsupportedLegalTypeException;
import java.util.HashMap;
import java.util.Map;

/**
* Enumeration representing different legal entity types.
* <p>
* This enum defines various legal entity types identified by unique codes (e.g., A, B, BC, C,
* etc.). It includes a static block to initialize a map for reverse lookup, allowing retrieval of
* enum instances by their name. This feature facilitates the conversion from string values to their
* corresponding enum instances, especially useful in parsing data from external sources or user
* input.
* </p>
*/
public enum LegalTypeEnum {
// Enum constants representing legal entity types
A, B, BC, C, CP, EPR, FOR, LIC, REG, S, XS, XCP, SP, GP, LP, XL, XP;

// A map for reverse lookup of enum constants by their name
private static final Map<String, LegalTypeEnum> CONSTANTS = new HashMap<>();

static {
// Populates the CONSTANTS map with enum names and their corresponding enum instances
for (LegalTypeEnum c : values()) {
CONSTANTS.put(c.name(), c);
}
}

/**
* Retrieves the enum instance corresponding to the given string value.
* <p>
* This method allows for reverse lookup of enum instances by their name, facilitating the
* conversion from strings to enum instances. It supports dynamic retrieval of enum instances in
* scenarios where the enum type is determined at runtime.
* </p>
*
* @param value The string representation of the enum constant to be retrieved.
* @return The {@link LegalTypeEnum} instance corresponding to the given string value, or null if
* no matching instance is found.
*/
@JsonCreator
public static LegalTypeEnum fromValue(String value) {
return CONSTANTS.get(value);
}
}
package ca.bc.gov.app.dto.client;

import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.HashMap;
import java.util.Map;

/**
* Enumeration representing different legal entity types.
* <p>
* This enum defines various legal entity types identified by unique codes (e.g., A, B, BC, C,
* etc.). It includes a static block to initialize a map for reverse lookup, allowing retrieval of
* enum instances by their name. This feature facilitates the conversion from string values to their
* corresponding enum instances, especially useful in parsing data from external sources or user
* input.
* </p>
*/
public enum LegalTypeEnum {
// Enum constants representing legal entity types
A, B, BC, C, CP, EPR, FOR, LIC, REG, S, XS, XCP, SP, GP, LP, XL, XP, LL;

// A map for reverse lookup of enum constants by their name
private static final Map<String, LegalTypeEnum> CONSTANTS = new HashMap<>();

static {
// Populates the CONSTANTS map with enum names and their corresponding enum instances
for (LegalTypeEnum c : values()) {
CONSTANTS.put(c.name(), c);
}
}

/**
* Retrieves the enum instance corresponding to the given string value.
* <p>
* This method allows for reverse lookup of enum instances by their name, facilitating the
* conversion from strings to enum instances. It supports dynamic retrieval of enum instances in
* scenarios where the enum type is determined at runtime.
* </p>
*
* @param value The string representation of the enum constant to be retrieved.
* @return The {@link LegalTypeEnum} instance corresponding to the given string value, or null if
* no matching instance is found.
*/
@JsonCreator
public static LegalTypeEnum fromValue(String value) {
return CONSTANTS.get(value);
}
}
206 changes: 103 additions & 103 deletions backend/src/main/java/ca/bc/gov/app/util/ClientValidationUtils.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,103 @@
package ca.bc.gov.app.util;

import static java.util.function.Predicate.not;

import ca.bc.gov.app.dto.ValidationError;
import ca.bc.gov.app.dto.client.ClientTypeEnum;
import ca.bc.gov.app.dto.client.LegalTypeEnum;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import reactor.core.publisher.Mono;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClientValidationUtils {

private static final String EMAIL_REGEX = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);
public static final Pattern US7ASCII_PATTERN = Pattern.compile("^[\\x00-\\x7F]+$");

public static Mono<ValidationError> validateEmail(String email, String field) {
if (StringUtils.isBlank(email)) {
return Mono.just(new ValidationError(field, "You must enter an email address"));
}

if (StringUtils.length(email) > 100) {
return Mono.just(new ValidationError(field, "This field has a 100 character limit."));
}

return
Mono
.just(email)
.map(EMAIL_PATTERN::matcher)
.filter(not(Matcher::matches))
.map(matcher ->
new ValidationError(
field,
"You must enter an email address in a valid format. For example: [email protected]"
)
);
}

public static Mono<ValidationError> validatePhoneNumber(String field,String phoneNumber) {
if (StringUtils.isBlank(phoneNumber)) {
return Mono.just(new ValidationError(field, "The phone number must be a 10-digit number"));
}
//This is just to make sure we removed the mask from FE
String localPhoneNumber = phoneNumber.replaceAll("\\D", "");
if (!StringUtils.isNumeric(localPhoneNumber) || StringUtils.length(localPhoneNumber) != 10) {
return Mono.just(new ValidationError(field, "The phone number must be a 10-digit number"));
}
return Mono.empty();
}

public static Mono<ValidationError> validateNotes(String notes, String field) {
return validateBySize(notes, field, 4000, 0, "notes");
}

public static Mono<ValidationError> validateBySize(
String value,
String field,
int maxSize,
int minSize,
String name
) {

if (StringUtils.isEmpty(value)) {
return Mono.empty();
}

if (StringUtils.length(value) > maxSize) {
return Mono.just(new ValidationError(field, "This field has a "+maxSize+" character limit."));
}

if (StringUtils.length(value) < minSize) {
return Mono.just(new ValidationError(field, "This field should have at least "+minSize+" characters."));
}

if (!US7ASCII_PATTERN.matcher(value).matches()) {
return Mono.just(new ValidationError(field, name+" has an invalid character."));
}
return Mono.empty();
}

public static String fieldIsMissingErrorMessage(String fieldName) {
return String.format("%s is missing", fieldName);
}

public static ClientTypeEnum getClientType(LegalTypeEnum legalType) {
if (legalType == null) {
return null;
}
return switch (legalType) {
case A, B, BC, C, CP, EPR, FOR, LIC, REG -> ClientTypeEnum.C;
case S, XS -> ClientTypeEnum.S;
case XCP -> ClientTypeEnum.A;
case SP -> ClientTypeEnum.RSP;
case GP -> ClientTypeEnum.P;
case LP, XL, XP -> ClientTypeEnum.L;
};
}
}
package ca.bc.gov.app.util;

import static java.util.function.Predicate.not;

import ca.bc.gov.app.dto.ValidationError;
import ca.bc.gov.app.dto.client.ClientTypeEnum;
import ca.bc.gov.app.dto.client.LegalTypeEnum;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import reactor.core.publisher.Mono;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClientValidationUtils {

private static final String EMAIL_REGEX = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);
public static final Pattern US7ASCII_PATTERN = Pattern.compile("^[\\x00-\\x7F]+$");

public static Mono<ValidationError> validateEmail(String email, String field) {
if (StringUtils.isBlank(email)) {
return Mono.just(new ValidationError(field, "You must enter an email address"));
}

if (StringUtils.length(email) > 100) {
return Mono.just(new ValidationError(field, "This field has a 100 character limit."));
}

return
Mono
.just(email)
.map(EMAIL_PATTERN::matcher)
.filter(not(Matcher::matches))
.map(matcher ->
new ValidationError(
field,
"You must enter an email address in a valid format. For example: [email protected]"
)
);
}

public static Mono<ValidationError> validatePhoneNumber(String field,String phoneNumber) {
if (StringUtils.isBlank(phoneNumber)) {
return Mono.just(new ValidationError(field, "The phone number must be a 10-digit number"));
}
//This is just to make sure we removed the mask from FE
String localPhoneNumber = phoneNumber.replaceAll("\\D", "");
if (!StringUtils.isNumeric(localPhoneNumber) || StringUtils.length(localPhoneNumber) != 10) {
return Mono.just(new ValidationError(field, "The phone number must be a 10-digit number"));
}
return Mono.empty();
}

public static Mono<ValidationError> validateNotes(String notes, String field) {
return validateBySize(notes, field, 4000, 0, "notes");
}

public static Mono<ValidationError> validateBySize(
String value,
String field,
int maxSize,
int minSize,
String name
) {

if (StringUtils.isEmpty(value)) {
return Mono.empty();
}

if (StringUtils.length(value) > maxSize) {
return Mono.just(new ValidationError(field, "This field has a "+maxSize+" character limit."));
}

if (StringUtils.length(value) < minSize) {
return Mono.just(new ValidationError(field, "This field should have at least "+minSize+" characters."));
}

if (!US7ASCII_PATTERN.matcher(value).matches()) {
return Mono.just(new ValidationError(field, name+" has an invalid character."));
}
return Mono.empty();
}

public static String fieldIsMissingErrorMessage(String fieldName) {
return String.format("%s is missing", fieldName);
}

public static ClientTypeEnum getClientType(LegalTypeEnum legalType) {
if (legalType == null) {
return null;
}
return switch (legalType) {
case A, B, BC, C, CP, EPR, FOR, LIC, REG -> ClientTypeEnum.C;
case S, XS -> ClientTypeEnum.S;
case XCP -> ClientTypeEnum.A;
case SP -> ClientTypeEnum.RSP;
case GP -> ClientTypeEnum.P;
case LL, LP, XL, XP -> ClientTypeEnum.L;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private static Stream<Arguments> getClientType() {
Arguments.of(LegalTypeEnum.SP, ClientTypeEnum.RSP),
Arguments.of(LegalTypeEnum.GP, ClientTypeEnum.P),
Arguments.of(LegalTypeEnum.LP, ClientTypeEnum.L),
Arguments.of(LegalTypeEnum.LL, ClientTypeEnum.L),
Arguments.of(LegalTypeEnum.XL, ClientTypeEnum.L),
Arguments.of(LegalTypeEnum.XP, ClientTypeEnum.L),
Arguments.of(null, null)
Expand Down

0 comments on commit 5a511a3

Please sign in to comment.