data validation error holiday name appearing twice(fixed)
This commit is contained in:
+34
-35
@@ -1,4 +1,4 @@
|
||||
package org.mifosplatform.organisation.holiday.data;
|
||||
package org.mifosplatform.organisation.holiday.data;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
@@ -26,57 +26,56 @@ import com.google.gson.reflect.TypeToken;
|
||||
@Component
|
||||
public class HolidayDataValidator {
|
||||
|
||||
private final FromJsonHelper fromApiJsonHelper;
|
||||
|
||||
@Autowired
|
||||
private final FromJsonHelper fromApiJsonHelper;
|
||||
|
||||
@Autowired
|
||||
public HolidayDataValidator(final FromJsonHelper fromApiJsonHelper) {
|
||||
this.fromApiJsonHelper = fromApiJsonHelper;
|
||||
}
|
||||
|
||||
public void validateForCreate(final String json) {
|
||||
|
||||
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
|
||||
|
||||
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
|
||||
|
||||
public void validateForCreate(final String json) {
|
||||
|
||||
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
|
||||
|
||||
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
|
||||
fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, HolidayApiConstants.HOLIDAY_CREATE_REQUEST_DATA_PARAMETERS);
|
||||
final JsonElement element = fromApiJsonHelper.parse(json);
|
||||
|
||||
|
||||
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||
|
||||
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
|
||||
.resource(HolidayApiConstants.HOLIDAY_RESOURCE_NAME);
|
||||
|
||||
|
||||
final String name = this.fromApiJsonHelper.extractStringNamed(HolidayApiConstants.name, element);
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.name).value(name).notBlank().notNull().notExceedingLengthOf(100);
|
||||
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.name).value(name).notNull().notExceedingLengthOf(100);
|
||||
|
||||
final LocalDate fromDate = this.fromApiJsonHelper.extractLocalDateNamed(HolidayApiConstants.fromDate, element);
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.fromDate).value(fromDate).notNull();
|
||||
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.fromDate).value(fromDate).notNull();
|
||||
|
||||
final LocalDate toDate = this.fromApiJsonHelper.extractLocalDateNamed(HolidayApiConstants.toDate, element);
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.toDate).value(toDate).notNull();
|
||||
|
||||
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.toDate).value(toDate).notNull();
|
||||
|
||||
Set<Long> offices = null;
|
||||
final JsonObject topLevelJsonElement = element.getAsJsonObject();
|
||||
|
||||
if (topLevelJsonElement.has(HolidayApiConstants.offices)&&topLevelJsonElement.get(HolidayApiConstants.offices).isJsonArray()) {
|
||||
|
||||
final JsonArray array = topLevelJsonElement.get(HolidayApiConstants.offices).getAsJsonArray();
|
||||
if(array.size()>0) {
|
||||
offices = new HashSet<Long>(array.size());
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
final JsonObject officeElement = array.get(i).getAsJsonObject();
|
||||
final Long officeId = this.fromApiJsonHelper.extractLongNamed("officeId", officeElement);
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.offices).value(officeId).notNull();
|
||||
offices.add(officeId);
|
||||
}
|
||||
}
|
||||
|
||||
if (topLevelJsonElement.has(HolidayApiConstants.offices) && topLevelJsonElement.get(HolidayApiConstants.offices).isJsonArray()) {
|
||||
|
||||
final JsonArray array = topLevelJsonElement.get(HolidayApiConstants.offices).getAsJsonArray();
|
||||
if (array.size() > 0) {
|
||||
offices = new HashSet<Long>(array.size());
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
final JsonObject officeElement = array.get(i).getAsJsonObject();
|
||||
final Long officeId = this.fromApiJsonHelper.extractLongNamed("officeId", officeElement);
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.offices).value(officeId).notNull();
|
||||
offices.add(officeId);
|
||||
}
|
||||
}
|
||||
}
|
||||
baseDataValidator.reset().parameter(HolidayApiConstants.offices).value(offices).notNull();
|
||||
throwExceptionIfValidationWarningsExist(dataValidationErrors);
|
||||
}
|
||||
|
||||
private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
|
||||
}
|
||||
|
||||
private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
|
||||
if (!dataValidationErrors.isEmpty()) {
|
||||
//
|
||||
throw new PlatformApiDataValidationException(dataValidationErrors);
|
||||
|
||||
Reference in New Issue
Block a user