/fineract-provider/src/main/java/org/apache/fineract/infrastructure/sms/data/SmsDataValidator.java

https://gitlab.com/skylabase/incubator-fineract · Java · 137 lines · 87 code · 29 blank · 21 comment · 18 complexity · d42b014e1392abbd78e2834329aaac03 MD5 · raw file

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.fineract.infrastructure.sms.data;
  20. import java.lang.reflect.Type;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.commons.lang.StringUtils;
  25. import org.apache.fineract.infrastructure.core.data.ApiParameterError;
  26. import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
  27. import org.apache.fineract.infrastructure.core.exception.InvalidJsonException;
  28. import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
  29. import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
  30. import org.apache.fineract.infrastructure.sms.SmsApiConstants;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.stereotype.Component;
  33. import com.google.gson.JsonElement;
  34. import com.google.gson.reflect.TypeToken;
  35. @Component
  36. public final class SmsDataValidator {
  37. private final FromJsonHelper fromApiJsonHelper;
  38. @Autowired
  39. public SmsDataValidator(final FromJsonHelper fromApiJsonHelper) {
  40. this.fromApiJsonHelper = fromApiJsonHelper;
  41. }
  42. public void validateForCreate(final String json) {
  43. if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
  44. final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
  45. this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, SmsApiConstants.CREATE_REQUEST_DATA_PARAMETERS);
  46. final JsonElement element = this.fromApiJsonHelper.parse(json);
  47. final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
  48. final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
  49. .resource(SmsApiConstants.RESOURCE_NAME);
  50. if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.groupIdParamName, element)) {
  51. final Long groupId = this.fromApiJsonHelper.extractLongNamed(SmsApiConstants.groupIdParamName, element);
  52. baseDataValidator.reset().parameter(SmsApiConstants.groupIdParamName).value(groupId).notNull().integerGreaterThanZero();
  53. // ensure clientId and staffId are not passed
  54. if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.clientIdParamName, element)) {
  55. baseDataValidator.reset().parameter(SmsApiConstants.clientIdParamName).failWithCode("cannot.be.passed.with.groupId");
  56. }
  57. if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.staffIdParamName, element)) {
  58. baseDataValidator.reset().parameter(SmsApiConstants.staffIdParamName).failWithCode("cannot.be.passed.with.groupId");
  59. }
  60. } else if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.clientIdParamName, element)) {
  61. final Long clientId = this.fromApiJsonHelper.extractLongNamed(SmsApiConstants.clientIdParamName, element);
  62. baseDataValidator.reset().parameter(SmsApiConstants.clientIdParamName).value(clientId).notNull().integerGreaterThanZero();
  63. // ensure groupId and staffId are not passed
  64. if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.groupIdParamName, element)) {
  65. baseDataValidator.reset().parameter(SmsApiConstants.groupIdParamName).failWithCode("cannot.be.passed.with.clientId");
  66. }
  67. if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.staffIdParamName, element)) {
  68. baseDataValidator.reset().parameter(SmsApiConstants.staffIdParamName).failWithCode("cannot.be.passed.with.clientId");
  69. }
  70. } else if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.staffIdParamName, element)) {
  71. final Long staffId = this.fromApiJsonHelper.extractLongNamed(SmsApiConstants.staffIdParamName, element);
  72. baseDataValidator.reset().parameter(SmsApiConstants.staffIdParamName).value(staffId).ignoreIfNull().longGreaterThanZero();
  73. // ensure groupId and clientId are not passed
  74. if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.groupIdParamName, element)) {
  75. baseDataValidator.reset().parameter(SmsApiConstants.groupIdParamName).failWithCode("cannot.be.passed.with.staffId");
  76. }
  77. if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.clientIdParamName, element)) {
  78. baseDataValidator.reset().parameter(SmsApiConstants.clientIdParamName).failWithCode("cannot.be.passed.with.staffId");
  79. }
  80. }
  81. if (!this.fromApiJsonHelper.parameterExists(SmsApiConstants.groupIdParamName, element)
  82. && !this.fromApiJsonHelper.parameterExists(SmsApiConstants.clientIdParamName, element)
  83. && !this.fromApiJsonHelper.parameterExists(SmsApiConstants.staffIdParamName, element)) {
  84. baseDataValidator.reset().parameter(SmsApiConstants.staffIdParamName)
  85. .failWithCodeNoParameterAddedToErrorCode("no.entity.provided");
  86. }
  87. final String message = this.fromApiJsonHelper.extractStringNamed(SmsApiConstants.messageParamName, element);
  88. baseDataValidator.reset().parameter(SmsApiConstants.messageParamName).value(message).notBlank().notExceedingLengthOf(1000);
  89. throwExceptionIfValidationWarningsExist(dataValidationErrors);
  90. }
  91. public void validateForUpdate(final String json) {
  92. if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
  93. final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
  94. this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, SmsApiConstants.UPDATE_REQUEST_DATA_PARAMETERS);
  95. final JsonElement element = this.fromApiJsonHelper.parse(json);
  96. final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
  97. final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors)
  98. .resource(SmsApiConstants.RESOURCE_NAME);
  99. if (this.fromApiJsonHelper.parameterExists(SmsApiConstants.messageParamName, element)) {
  100. final String message = this.fromApiJsonHelper.extractStringNamed(SmsApiConstants.messageParamName, element);
  101. baseDataValidator.reset().parameter(SmsApiConstants.messageParamName).value(message).notBlank().notExceedingLengthOf(1000);
  102. }
  103. throwExceptionIfValidationWarningsExist(dataValidationErrors);
  104. }
  105. private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
  106. if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException(dataValidationErrors); }
  107. }
  108. }