make ApiParameterError (almost) immutable

by making (most of) its fields final, because Immutable is Good

Only "almost" / "most" because of the of the remaining use of setParameterName() in
EntityDatatableChecksWritePlatformServiceImpl.saveDatatables() which unfortunately looks
a lot less trivial to get rid of, so that's for another time/someone else, later (or never).
This commit is contained in:
Michael Vorburger
2020-07-02 21:47:02 +02:00
committed by Michael Vorburger ⛑️
parent 10536c0f91
commit 1623e80a7f
5 changed files with 28 additions and 56 deletions
@@ -44,6 +44,7 @@ import org.springframework.stereotype.Component;
public class AccountNumberFormatDataValidator { public class AccountNumberFormatDataValidator {
private final FromJsonHelper fromApiJsonHelper; private final FromJsonHelper fromApiJsonHelper;
private static final Set<String> ACCOUNT_NUMBER_FORMAT_CREATE_REQUEST_DATA_PARAMETERS = new HashSet<>( private static final Set<String> ACCOUNT_NUMBER_FORMAT_CREATE_REQUEST_DATA_PARAMETERS = new HashSet<>(
Arrays.asList(AccountNumberFormatConstants.accountTypeParamName, AccountNumberFormatConstants.prefixTypeParamName)); Arrays.asList(AccountNumberFormatConstants.accountTypeParamName, AccountNumberFormatConstants.prefixTypeParamName));
@@ -56,7 +57,6 @@ public class AccountNumberFormatDataValidator {
} }
public void validateForCreate(final String json) { public void validateForCreate(final String json) {
if (StringUtils.isBlank(json)) { if (StringUtils.isBlank(json)) {
throw new InvalidJsonException(); throw new InvalidJsonException();
} }
@@ -136,7 +136,6 @@ public class AccountNumberFormatDataValidator {
} }
public void validateForUpdate(final String json, EntityAccountType entityAccountType) { public void validateForUpdate(final String json, EntityAccountType entityAccountType) {
if (StringUtils.isBlank(json)) { if (StringUtils.isBlank(json)) {
throw new InvalidJsonException(); throw new InvalidJsonException();
} }
@@ -166,12 +165,10 @@ public class AccountNumberFormatDataValidator {
} }
throwExceptionIfValidationWarningsExist(dataValidationErrors); throwExceptionIfValidationWarningsExist(dataValidationErrors);
} }
private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) { private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
if (!dataValidationErrors.isEmpty()) { if (!dataValidationErrors.isEmpty()) {
//
throw new PlatformApiDataValidationException(dataValidationErrors); throw new PlatformApiDataValidationException(dataValidationErrors);
} }
} }
@@ -29,10 +29,6 @@ public class ApiErrorMessageArg {
return new ApiErrorMessageArg(object); return new ApiErrorMessageArg(object);
} }
protected ApiErrorMessageArg() {
//
}
public ApiErrorMessageArg(final Object object) { public ApiErrorMessageArg(final Object object) {
this.value = object; this.value = object;
} }
@@ -23,23 +23,23 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public class ApiParameterError { public final class ApiParameterError {
/** /**
* A developer friendly plain English description of why the HTTP error response was returned from the API. * A developer friendly plain English description of why the HTTP error response was returned from the API.
*/ */
private String developerMessage; private final String developerMessage;
/** /**
* A user friendly plain English description of why the HTTP error response was returned from the API that can be * A user friendly plain English description of why the HTTP error response was returned from the API that can be
* presented to end users. * presented to end users.
*/ */
private String defaultUserMessage; private final String defaultUserMessage;
/** /**
* A code that can be used for globalisation support by client applications of the API. * A code that can be used for globalisation support by client applications of the API.
*/ */
private String userMessageGlobalisationCode; private final String userMessageGlobalisationCode;
/** /**
* The name of the field or parameter passed to the API that this error relates to. * The name of the field or parameter passed to the API that this error relates to.
@@ -49,39 +49,44 @@ public class ApiParameterError {
/** /**
* The actual value of the parameter (if any) as passed to API. * The actual value of the parameter (if any) as passed to API.
*/ */
private Object value; private final Object value;
/** /**
* Arguments related to the user error message. * Arguments related to the user error message.
*/ */
private List<ApiErrorMessageArg> args = new ArrayList<>(); private final List<ApiErrorMessageArg> args;
public static ApiParameterError generalError(final String globalisationMessageCode, final String defaultUserMessage, public static ApiParameterError generalError(final String globalisationMessageCode, final String defaultUserMessage,
final Object... defaultUserMessageArgs) { final Object... defaultUserMessageArgs) {
return new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs); return new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs, "id", null);
} }
public static ApiParameterError resourceIdentifierNotFound(final String globalisationMessageCode, final String defaultUserMessage, public static ApiParameterError resourceIdentifierNotFound(final String globalisationMessageCode, final String defaultUserMessage,
final Object... defaultUserMessageArgs) { final Object... defaultUserMessageArgs) {
return new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs); return new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs, "id", null);
} }
public static ApiParameterError parameterError(final String globalisationMessageCode, final String defaultUserMessage, public static ApiParameterError parameterError(final String globalisationMessageCode, final String defaultUserMessage,
final String parameterName, final Object... defaultUserMessageArgs) { final String parameterName, final Object... defaultUserMessageArgs) {
final ApiParameterError error = new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs); final ApiParameterError error = new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs,
error.setParameterName(parameterName); parameterName, null);
return error; return error;
} }
protected ApiParameterError() { public static ApiParameterError parameterErrorWithValue(final String globalisationMessageCode, final String defaultUserMessage,
// final String parameterName, final String value, final Object... defaultUserMessageArgs) {
final ApiParameterError error = new ApiParameterError(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs,
parameterName, value);
return error;
} }
private ApiParameterError(final String globalisationMessageCode, final String defaultUserMessage, private ApiParameterError(final String globalisationMessageCode, final String defaultUserMessage, final Object[] defaultUserMessageArgs,
final Object[] defaultUserMessageArgs) { String parameterName, String value) {
this.userMessageGlobalisationCode = globalisationMessageCode; this.userMessageGlobalisationCode = globalisationMessageCode;
this.developerMessage = defaultUserMessage; this.developerMessage = defaultUserMessage;
this.defaultUserMessage = defaultUserMessage; this.defaultUserMessage = defaultUserMessage;
this.parameterName = parameterName;
this.value = value;
final List<ApiErrorMessageArg> messageArgs = new ArrayList<>(); final List<ApiErrorMessageArg> messageArgs = new ArrayList<>();
if (defaultUserMessageArgs != null) { if (defaultUserMessageArgs != null) {
@@ -96,34 +101,20 @@ public class ApiParameterError {
} }
} }
this.args = messageArgs; this.args = messageArgs;
this.parameterName = "id";
} }
public String getDeveloperMessage() { public String getDeveloperMessage() {
return this.developerMessage; return this.developerMessage;
} }
public void setDeveloperMessage(final String developerMessage) {
this.developerMessage = developerMessage;
}
public String getDefaultUserMessage() { public String getDefaultUserMessage() {
return this.defaultUserMessage; return this.defaultUserMessage;
} }
public void setDefaultUserMessage(final String defaultUserMessage) {
this.defaultUserMessage = defaultUserMessage;
}
public String getUserMessageGlobalisationCode() { public String getUserMessageGlobalisationCode() {
return this.userMessageGlobalisationCode; return this.userMessageGlobalisationCode;
} }
public void setUserMessageGlobalisationCode(final String userMessageGlobalisationCode) {
this.userMessageGlobalisationCode = userMessageGlobalisationCode;
}
public String getParameterName() { public String getParameterName() {
return this.parameterName; return this.parameterName;
} }
@@ -136,15 +127,7 @@ public class ApiParameterError {
return this.value; return this.value;
} }
public void setValue(final Object value) {
this.value = value;
}
public List<ApiErrorMessageArg> getArgs() { public List<ApiErrorMessageArg> getArgs() {
return this.args; return this.args;
} }
public void setArgs(final List<ApiErrorMessageArg> args) {
this.args = args;
}
} }
@@ -560,11 +560,10 @@ public class JsonParserHelper {
} catch (final ParseException e) { } catch (final ParseException e) {
final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final ApiParameterError error = ApiParameterError.parameterError("validation.msg.invalid.integer.format", final ApiParameterError error = ApiParameterError.parameterErrorWithValue("validation.msg.invalid.integer.format",
"The parameter " + parameterName + " has value: " + numericalValueFormatted "The parameter " + parameterName + " has value: " + numericalValueFormatted
+ " which is invalid integer value for provided locale of [" + clientApplicationLocale.toString() + "].", + " which is invalid integer value for provided locale of [" + clientApplicationLocale.toString() + "].",
parameterName, numericalValueFormatted, clientApplicationLocale); parameterName, numericalValueFormatted, numericalValueFormatted, clientApplicationLocale);
error.setValue(numericalValueFormatted);
dataValidationErrors.add(error); dataValidationErrors.add(error);
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.",
@@ -585,10 +584,9 @@ public class JsonParserHelper {
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final ApiParameterError error = ApiParameterError.parameterError("validation.msg.invalid.integer", final ApiParameterError error = ApiParameterError.parameterErrorWithValue("validation.msg.invalid.integer",
"The parameter " + parameterName + " has value: " + numericalValueFormatted + " which is invalid integer.", "The parameter " + parameterName + " has value: " + numericalValueFormatted + " which is invalid integer.",
parameterName, numericalValueFormatted); parameterName, numericalValueFormatted, numericalValueFormatted);
error.setValue(numericalValueFormatted);
dataValidationErrors.add(error); dataValidationErrors.add(error);
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.",
@@ -632,7 +630,7 @@ public class JsonParserHelper {
if (parsedNumber instanceof BigDecimal) { if (parsedNumber instanceof BigDecimal) {
number = (BigDecimal) parsedNumber; number = (BigDecimal) parsedNumber;
} else { } else {
number = BigDecimal.valueOf(Double.valueOf(parsedNumber.doubleValue())); number = BigDecimal.valueOf(parsedNumber.doubleValue());
} }
} }
@@ -640,11 +638,10 @@ public class JsonParserHelper {
} catch (final ParseException e) { } catch (final ParseException e) {
final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final ApiParameterError error = ApiParameterError.parameterError("validation.msg.invalid.decimal.format", final ApiParameterError error = ApiParameterError.parameterErrorWithValue("validation.msg.invalid.decimal.format",
"The parameter " + parameterName + " has value: " + numericalValueFormatted "The parameter " + parameterName + " has value: " + numericalValueFormatted
+ " which is invalid decimal value for provided locale of [" + clientApplicationLocale.toString() + "].", + " which is invalid decimal value for provided locale of [" + clientApplicationLocale.toString() + "].",
parameterName, numericalValueFormatted, clientApplicationLocale); parameterName, numericalValueFormatted, numericalValueFormatted, clientApplicationLocale);
error.setValue(numericalValueFormatted);
dataValidationErrors.add(error); dataValidationErrors.add(error);
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.",
@@ -107,7 +107,7 @@ public class EntityDatatableChecksWritePlatformServiceImpl implements EntityData
final String foreignKeyColumnName = EntityTables.getForeignKeyColumnNameOnDatatable(entity); final String foreignKeyColumnName = EntityTables.getForeignKeyColumnNameOnDatatable(entity);
final boolean columnExist = datatableData.hasColumn(foreignKeyColumnName); final boolean columnExist = datatableData.hasColumn(foreignKeyColumnName);
LOG.info("{} has column {} ? {}", new Object[] { datatableData.getRegisteredTableName(), foreignKeyColumnName, columnExist }); LOG.info("{} has column {} ? {}", datatableData.getRegisteredTableName(), foreignKeyColumnName, columnExist);
if (!columnExist) { if (!columnExist) {
throw new EntityDatatableCheckNotSupportedException(datatableData.getRegisteredTableName(), entity); throw new EntityDatatableCheckNotSupportedException(datatableData.getRegisteredTableName(), entity);
@@ -237,7 +237,6 @@ public class EntityDatatableChecksWritePlatformServiceImpl implements EntityData
try { try {
this.readWriteNonCoreDataService.createNewDatatableEntry(datatableName, entityId, datatableData.toString()); this.readWriteNonCoreDataService.createNewDatatableEntry(datatableName, entityId, datatableData.toString());
} catch (PlatformApiDataValidationException e) { } catch (PlatformApiDataValidationException e) {
List<ApiParameterError> errors = e.getErrors();
for (ApiParameterError error : e.getErrors()) { for (ApiParameterError error : e.getErrors()) {
error.setParameterName("datatables." + datatableName + "." + error.getParameterName()); error.setParameterName("datatables." + datatableName + "." + error.getParameterName());
} }