|
| 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.fineract.messagegateway.tenants.serialization; |
| 20 | + |
| 21 | +import java.lang.reflect.Type; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import org.fineract.messagegateway.exception.PlatformApiDataValidationException; |
| 27 | +import org.fineract.messagegateway.helpers.ApiParameterError; |
| 28 | +import org.fineract.messagegateway.helpers.DataValidatorBuilder; |
| 29 | +import org.fineract.messagegateway.helpers.FromJsonHelper; |
| 30 | +import org.fineract.messagegateway.tenants.constants.TenantConstants; |
| 31 | +import org.fineract.messagegateway.tenants.domain.Tenant; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.stereotype.Component; |
| 34 | + |
| 35 | +import com.google.gson.JsonElement; |
| 36 | +import com.google.gson.reflect.TypeToken; |
| 37 | + |
| 38 | +@Component |
| 39 | +public class TenantSerializer { |
| 40 | + |
| 41 | + private final FromJsonHelper fromJsonHelper; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + public TenantSerializer(FromJsonHelper fromJsonHelper) { |
| 45 | + this.fromJsonHelper = fromJsonHelper; |
| 46 | + } |
| 47 | + |
| 48 | + public Tenant validateCreateRequest(final String json) { |
| 49 | + final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); |
| 50 | + this.fromJsonHelper.checkForUnsupportedParameters(typeOfMap, json, |
| 51 | + TenantConstants.CREATE_REQUEST_PARAMETERS); |
| 52 | + |
| 53 | + final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); |
| 54 | + final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) |
| 55 | + .resource(TenantConstants.TENANTS_RESOURCE_NAME); |
| 56 | + final JsonElement element = this.fromJsonHelper.parse(json); |
| 57 | + |
| 58 | + |
| 59 | + final String tenantId = this.fromJsonHelper.extractStringNamed(TenantConstants.TENANT_ID, element); |
| 60 | + baseDataValidator.reset().parameter(TenantConstants.TENANT_ID) |
| 61 | + .value(tenantId).notBlank().notExceedingLengthOf(32); |
| 62 | + |
| 63 | + String tenantDescription = null; |
| 64 | + if(this.fromJsonHelper.parameterExists(TenantConstants.TENANT_DESCRIPTION, element)) { |
| 65 | + tenantDescription = this.fromJsonHelper.extractStringNamed(TenantConstants.TENANT_DESCRIPTION, |
| 66 | + element); |
| 67 | + baseDataValidator.reset().parameter(TenantConstants.TENANT_DESCRIPTION).value(tenantDescription) |
| 68 | + .notBlank().notExceedingLengthOf(500); |
| 69 | + } |
| 70 | + |
| 71 | + if (!dataValidationErrors.isEmpty()) { |
| 72 | + throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", |
| 73 | + "Validation errors exist.", dataValidationErrors); |
| 74 | + } |
| 75 | + |
| 76 | + return new Tenant(tenantId, null, tenantDescription); |
| 77 | + } |
| 78 | +} |
0 commit comments