/fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/api/JsonQuery.java

https://gitlab.com/skylabase/incubator-fineract · Java · 279 lines · 199 code · 50 blank · 30 comment · 28 complexity · 6c9f983663bd61dc1a54a18930363421 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.core.api;
  20. import java.lang.reflect.Type;
  21. import java.math.BigDecimal;
  22. import java.util.Arrays;
  23. import java.util.Map;
  24. import org.apache.commons.lang.ObjectUtils;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
  27. import org.apache.fineract.infrastructure.security.domain.BasicPasswordEncodablePlatformUser;
  28. import org.apache.fineract.infrastructure.security.domain.PlatformUser;
  29. import org.apache.fineract.infrastructure.security.service.PlatformPasswordEncoder;
  30. import org.joda.time.LocalDate;
  31. import com.google.gson.JsonElement;
  32. import com.google.gson.reflect.TypeToken;
  33. /**
  34. * Immutable representation of a query.
  35. *
  36. * Wraps the provided JSON with convenience functions for extracting parameter
  37. * values.
  38. */
  39. public final class JsonQuery {
  40. private final String jsonQuery;
  41. private final JsonElement parsedQuery;
  42. private final FromJsonHelper fromApiJsonHelper;
  43. public static JsonQuery from(final String jsonCommand, final JsonElement parsedQuery, final FromJsonHelper fromApiJsonHelper) {
  44. return new JsonQuery(jsonCommand, parsedQuery, fromApiJsonHelper);
  45. }
  46. public JsonQuery(final String jsonCommand, final JsonElement parsedCommand, final FromJsonHelper fromApiJsonHelper) {
  47. this.jsonQuery = jsonCommand;
  48. this.parsedQuery = parsedCommand;
  49. this.fromApiJsonHelper = fromApiJsonHelper;
  50. }
  51. public String json() {
  52. return this.jsonQuery;
  53. }
  54. public JsonElement parsedJson() {
  55. return this.parsedQuery;
  56. }
  57. private boolean differenceExists(final LocalDate baseValue, final LocalDate workingCopyValue) {
  58. boolean differenceExists = false;
  59. if (baseValue != null) {
  60. differenceExists = !baseValue.equals(workingCopyValue);
  61. } else {
  62. differenceExists = workingCopyValue != null;
  63. }
  64. return differenceExists;
  65. }
  66. private boolean differenceExists(final String baseValue, final String workingCopyValue) {
  67. boolean differenceExists = false;
  68. if (StringUtils.isNotBlank(baseValue)) {
  69. differenceExists = !baseValue.equals(workingCopyValue);
  70. } else {
  71. differenceExists = StringUtils.isNotBlank(workingCopyValue);
  72. }
  73. return differenceExists;
  74. }
  75. private boolean differenceExists(final String[] baseValue, final String[] workingCopyValue) {
  76. Arrays.sort(baseValue);
  77. Arrays.sort(workingCopyValue);
  78. return !Arrays.equals(baseValue, workingCopyValue);
  79. }
  80. private boolean differenceExists(final Number baseValue, final Number workingCopyValue) {
  81. boolean differenceExists = false;
  82. if (baseValue != null) {
  83. differenceExists = !baseValue.equals(workingCopyValue);
  84. } else {
  85. differenceExists = workingCopyValue != null;
  86. }
  87. return differenceExists;
  88. }
  89. private boolean differenceExists(final BigDecimal baseValue, final BigDecimal workingCopyValue) {
  90. boolean differenceExists = false;
  91. if (baseValue != null) {
  92. differenceExists = baseValue.compareTo(workingCopyValue) != 0;
  93. } else {
  94. differenceExists = workingCopyValue != null;
  95. }
  96. return differenceExists;
  97. }
  98. private boolean differenceExists(final Boolean baseValue, final Boolean workingCopyValue) {
  99. boolean differenceExists = false;
  100. if (baseValue != null) {
  101. differenceExists = !baseValue.equals(workingCopyValue);
  102. } else {
  103. differenceExists = workingCopyValue != null;
  104. }
  105. return differenceExists;
  106. }
  107. private boolean parameterExists(final String parameterName) {
  108. return this.fromApiJsonHelper.parameterExists(parameterName, this.parsedQuery);
  109. }
  110. public boolean hasParameter(final String parameterName) {
  111. return parameterExists(parameterName);
  112. }
  113. public String dateFormat() {
  114. return stringValueOfParameterNamed("dateFormat");
  115. }
  116. public String locale() {
  117. return stringValueOfParameterNamed("locale");
  118. }
  119. public Map<String, Boolean> mapValueOfParameterNamed(final String parameterName) {
  120. final Type typeOfMap = new TypeToken<Map<String, Boolean>>() {}.getType();
  121. if (this.parsedQuery.getAsJsonObject().has(parameterName)) {
  122. this.parsedQuery.getAsJsonObject().get(parameterName);
  123. }
  124. return this.fromApiJsonHelper.extractMap(typeOfMap, this.jsonQuery);
  125. }
  126. public boolean isChangeInLongParameterNamed(final String parameterName, final Long existingValue) {
  127. boolean isChanged = false;
  128. if (parameterExists(parameterName)) {
  129. final Long workingValue = longValueOfParameterNamed(parameterName);
  130. isChanged = differenceExists(existingValue, workingValue);
  131. }
  132. return isChanged;
  133. }
  134. public Long longValueOfParameterNamed(final String parameterName) {
  135. return this.fromApiJsonHelper.extractLongNamed(parameterName, this.parsedQuery);
  136. }
  137. public boolean isChangeInLocalDateParameterNamed(final String parameterName, final LocalDate existingValue) {
  138. boolean isChanged = false;
  139. if (parameterExists(parameterName)) {
  140. final LocalDate workingValue = localDateValueOfParameterNamed(parameterName);
  141. isChanged = differenceExists(existingValue, workingValue);
  142. }
  143. return isChanged;
  144. }
  145. public LocalDate localDateValueOfParameterNamed(final String parameterName) {
  146. return this.fromApiJsonHelper.extractLocalDateNamed(parameterName, this.parsedQuery);
  147. }
  148. public boolean isChangeInStringParameterNamed(final String parameterName, final String existingValue) {
  149. boolean isChanged = false;
  150. if (parameterExists(parameterName)) {
  151. final String workingValue = stringValueOfParameterNamed(parameterName);
  152. isChanged = differenceExists(existingValue, workingValue);
  153. }
  154. return isChanged;
  155. }
  156. public String stringValueOfParameterNamed(final String parameterName) {
  157. final String value = this.fromApiJsonHelper.extractStringNamed(parameterName, this.parsedQuery);
  158. return StringUtils.defaultIfEmpty(value, "");
  159. }
  160. public boolean isChangeInBigDecimalParameterNamed(final String parameterName, final BigDecimal existingValue) {
  161. boolean isChanged = false;
  162. if (parameterExists(parameterName)) {
  163. final BigDecimal workingValue = bigDecimalValueOfParameterNamed(parameterName);
  164. isChanged = differenceExists(existingValue, workingValue);
  165. }
  166. return isChanged;
  167. }
  168. public BigDecimal bigDecimalValueOfParameterNamed(final String parameterName) {
  169. return this.fromApiJsonHelper.extractBigDecimalWithLocaleNamed(parameterName, this.parsedQuery);
  170. }
  171. public boolean isChangeInIntegerParameterNamed(final String parameterName, final Integer existingValue) {
  172. boolean isChanged = false;
  173. if (parameterExists(parameterName)) {
  174. final Integer workingValue = integerValueOfParameterNamed(parameterName);
  175. isChanged = differenceExists(existingValue, workingValue);
  176. }
  177. return isChanged;
  178. }
  179. public Integer integerValueOfParameterNamed(final String parameterName) {
  180. return this.fromApiJsonHelper.extractIntegerWithLocaleNamed(parameterName, this.parsedQuery);
  181. }
  182. public boolean isChangeInBooleanParameterNamed(final String parameterName, final Boolean existingValue) {
  183. boolean isChanged = false;
  184. if (parameterExists(parameterName)) {
  185. final Boolean workingValue = booleanObjectValueOfParameterNamed(parameterName);
  186. isChanged = differenceExists(existingValue, workingValue);
  187. }
  188. return isChanged;
  189. }
  190. /**
  191. * Returns {@link Boolean} that could possibly be null.
  192. */
  193. public Boolean booleanObjectValueOfParameterNamed(final String parameterName) {
  194. return this.fromApiJsonHelper.extractBooleanNamed(parameterName, this.parsedQuery);
  195. }
  196. /**
  197. * always returns true or false
  198. */
  199. public boolean booleanPrimitiveValueOfParameterNamed(final String parameterName) {
  200. final Boolean value = this.fromApiJsonHelper.extractBooleanNamed(parameterName, this.parsedQuery);
  201. return (Boolean) ObjectUtils.defaultIfNull(value, Boolean.FALSE);
  202. }
  203. public boolean isChangeInArrayParameterNamed(final String parameterName, final String[] existingValue) {
  204. boolean isChanged = false;
  205. if (parameterExists(parameterName)) {
  206. final String[] workingValue = arrayValueOfParameterNamed(parameterName);
  207. isChanged = differenceExists(existingValue, workingValue);
  208. }
  209. return isChanged;
  210. }
  211. public String[] arrayValueOfParameterNamed(final String parameterName) {
  212. return this.fromApiJsonHelper.extractArrayNamed(parameterName, this.parsedQuery);
  213. }
  214. public boolean isChangeInPasswordParameterNamed(final String parameterName, final String existingValue,
  215. final PlatformPasswordEncoder platformPasswordEncoder, final Long saltValue) {
  216. boolean isChanged = false;
  217. if (parameterExists(parameterName)) {
  218. final String workingValue = passwordValueOfParameterNamed(parameterName, platformPasswordEncoder, saltValue);
  219. isChanged = differenceExists(existingValue, workingValue);
  220. }
  221. return isChanged;
  222. }
  223. public String passwordValueOfParameterNamed(final String parameterName, final PlatformPasswordEncoder platformPasswordEncoder,
  224. final Long saltValue) {
  225. final String passwordPlainText = stringValueOfParameterNamed(parameterName);
  226. final PlatformUser dummyPlatformUser = new BasicPasswordEncodablePlatformUser(saltValue, "", passwordPlainText);
  227. return platformPasswordEncoder.encode(dummyPlatformUser);
  228. }
  229. }